File: DifficultySettings.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (428 lines) | stat: -rw-r--r-- 12,640 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "DifficultySettings.h"

#include "i18n.h"
#include "string/convert.h"
#include "eclass.h"

namespace difficulty
{

DifficultySettings::DifficultySettings(int level) :
    _level(level),
    _store(new wxutil::TreeModel(_columns))
{}

DifficultySettings::~DifficultySettings()
{}

const DifficultySettings::TreeModelColumns& DifficultySettings::getColumns() const
{
    return _columns;
}

wxutil::TreeModel::Ptr DifficultySettings::getTreeStore() const
{
    return _store;
}

int DifficultySettings::getLevel() const
{
    return _level;
}

void DifficultySettings::clear()
{
    _settings.clear();
    _settingIds.clear();
    _iterMap.clear();
}

SettingPtr DifficultySettings::getSettingById(int id) const {
    // Search all stored settings matching this classname
    SettingIdMap::const_iterator found = _settingIds.find(id);

    if (found != _settingIds.end()) {
        return found->second;
    }

    return SettingPtr(); // not found
}

SettingPtr DifficultySettings::findOrCreateOverrule(const SettingPtr& existing)
{
    // Get the inheritancekey needed to lookup the classname
    std::string inheritanceKey = getInheritanceKey(existing->className);

    // Check if there is already an override active for the <existing> setting
    for (SettingsMap::iterator i = _settings.find(inheritanceKey);
         i != _settings.upper_bound(inheritanceKey) && i != _settings.end();
         ++i)
    {
        // Avoid self==self comparisons
        if (i->second != existing && i->second->spawnArg == existing->spawnArg) {
            // spawnarg is set on a different setting, check if it is non-default
            if (!i->second->isDefault) {
                // non-default, overriding setting, return it
                return i->second;
            }
        }
    }

    // No overriding setting found, create a new one
    SettingPtr setting = createSetting(existing->className);
    setting->spawnArg = existing->spawnArg;
    setting->isDefault = false;
    setting->appType = Setting::EAssign;

    return setting;
}

int DifficultySettings::save(int id, const SettingPtr& setting) {
    if (id != -1) {
        // We're dealing with an existing setting, fetch it
        SettingPtr existing = getSettingById(id);

        if (existing == NULL) {
            return -1;
        }

        if (existing->isDefault) {
            // We're trying to save a default setting, go into override mode

            if (*setting == *existing) {
                // New settings are identical to the existing ones, skip
                return existing->id;
            }

            // Create a new setting
            SettingPtr overrule = findOrCreateOverrule(existing);
            // Transfer the argument/appType into the new setting
            overrule->argument = setting->argument;
            overrule->appType = setting->appType;
            return overrule->id;
        }
        else {
            // TODO: Check, if there is another, existing setting which we
            // override with this one and whether they are the same

            // Copy the settings over to the existing setting
            *existing = *setting;
            return existing->id;
        }
    }
    else {
        // No setting highlighted, create a new one
        SettingPtr newSetting = createSetting(setting->className);
        // Copy the settings over
        *newSetting = *setting;
        newSetting->isDefault = false;
        return newSetting->id;
    }
}

void DifficultySettings::deleteSetting(int id)
{
    for (SettingsMap::iterator i = _settings.begin(); i != _settings.end(); ++i)
    {
        if (i->second->id == id)
        {
            // Found it, remove it from the tree and all maps
			_store->RemoveItem(i->second->iter);

            _settings.erase(i);
            _settingIds.erase(id);
            break;
        }
    }

    // Override settings might have been changed, update the model
    updateTreeModel();
}

void DifficultySettings::updateTreeModel()
{
    // Go through the settings and check the corresponding iters in the tree
    for (SettingsMap::iterator i = _settings.begin(); i != _settings.end(); ++i)
    {
        const std::string& className = i->second->className;
        Setting& setting = *i->second;

        // Ensure that the classname is in the map
        wxDataViewItem classIter = findOrInsertClassname(className);
		bool settingAdded = false;

        if (!setting.iter.IsOk())
        {
            // No iter corresponding to this setting yet, insert it
			setting.iter = classIter.IsOk() ? _store->AddItemUnderParent(classIter).getItem() : _store->AddItem().getItem();
			settingAdded = true;
        }

        wxutil::TreeModel::Row row(setting.iter, *_store);

		bool overridden = isOverridden(i->second);

		wxDataViewItemAttr colour;
		colour.SetColour(setting.isDefault ? wxColor(112,112,112) : wxColor(0,0,0));

		row[_columns.description] = setting.getDescString() + (overridden ? _(" (overridden)") : "");
        row[_columns.description].setAttr(colour);

        row[_columns.classname] = setting.className;
        row[_columns.settingId] = setting.id;
        row[_columns.isOverridden] = overridden;

		if (settingAdded)
		{
			row.SendItemAdded();
		}
		else
		{
			row.SendItemChanged();
		}
    }
}

void DifficultySettings::clearTreeModel()
{
    _iterMap.clear();
    _store->Clear();

	// Clear any iterators stored in the settings
	for (SettingsMap::iterator i = _settings.begin(); i != _settings.end(); ++i)
	{
		i->second->iter = wxDataViewItem();
	}
}

void DifficultySettings::refreshTreeModel()
{
    clearTreeModel();
    updateTreeModel();
}

bool DifficultySettings::isOverridden(const SettingPtr& setting)
{
    if (!setting->isDefault) {
        return false; // not a default setting, return false
    }

    // Get the inheritancekey needed to lookup the classname
    std::string inheritanceKey = getInheritanceKey(setting->className);

    // Search all other settings for the same className/spawnArg combination
    for (SettingsMap::iterator i = _settings.find(inheritanceKey);
         i != _settings.upper_bound(inheritanceKey) && i != _settings.end();
         ++i)
    {
        // Avoid self==self comparisons
        if (i->second != setting && i->second->spawnArg == setting->spawnArg) {
            // spawnarg is set on a different setting, check if it is non-default
            if (!i->second->isDefault) {
                // non-default, overriding setting, return true
                return true;
            }
        }
    }

    return false;
}

std::string DifficultySettings::getParentClass(const std::string& className)
{
    // Get the parent eclass
    IEntityClassPtr eclass = GlobalEntityClassManager().findClass(className);
    if (!eclass)
        return ""; // Invalid!

    return eclass->getAttributeValue("inherit");
}

wxDataViewItem DifficultySettings::findOrInsertClassname(const std::string& className)
{
    // Try to look up the classname in the tree
    TreeIterMap::iterator found = _iterMap.find(className);

    if (found != _iterMap.end())
    {
        // Name exists, return this
        return found->second;
    }

    // This iter will hold the parent element, if such is found
    wxDataViewItem parentIter;

    // Classname is not yet registered, walk up the inheritance tree
    std::string parentClassName = getParentClass(className);
    while (!parentClassName.empty())
    {
        // Try to look up the classname in the tree
        TreeIterMap::iterator foundParent = _iterMap.find(parentClassName);

        if (foundParent != _iterMap.end())
        {
            parentIter = foundParent->second;
            break;
        }

        parentClassName = getParentClass(parentClassName);
    }

    // Insert the map, using the found iter (or NULL, if nothing was found)
    wxDataViewItem inserted = insertClassName(className, parentIter);

    // Remember the iter
    _iterMap.insert(TreeIterMap::value_type(className, inserted));

    return inserted;
}

wxDataViewItem DifficultySettings::insertClassName(const std::string& className, const wxDataViewItem& parent)
{
    wxutil::TreeModel::Row row = parent.IsOk() ? _store->AddItemUnderParent(parent) : _store->AddItem();
    
    wxDataViewItemAttr black;
	black.SetColour(wxColor(0,0,0));

	row[_columns.description] = className;
    row[_columns.description].setAttr(black);

    row[_columns.classname] = className;
    row[_columns.settingId] = -1;

	row.SendItemAdded();

	return row.getItem();
}

std::string DifficultySettings::getInheritanceKey(const std::string& className)
{
    if (className.empty()) return "";

    IEntityClassPtr eclass = GlobalEntityClassManager().findClass(className);

    // Get the inheritance chain of this class
    std::list<std::string> inheritanceChain;
    for (IEntityClass* currentClass = eclass.get();
         currentClass != NULL;
         currentClass = currentClass->getParent())
    {
        inheritanceChain.push_front(currentClass->getDeclName());
    }

    // Build the inheritance key
    std::string inheritanceKey;
    for (std::list<std::string>::const_iterator c = inheritanceChain.begin();
         c != inheritanceChain.end();
         c++)
    {
        inheritanceKey += (inheritanceKey.empty()) ? "" : "_";
        inheritanceKey += *c;
    }

    return inheritanceKey;
}

SettingPtr DifficultySettings::createSetting(const std::string& className)
{
    SettingPtr setting(new Setting);
    setting->className = className;

    // Insert the parsed setting into our local map
    _settings.insert(SettingsMap::value_type(getInheritanceKey(className), setting));
    _settingIds.insert(SettingIdMap::value_type(setting->id, setting));

    return setting;
}

void DifficultySettings::parseFromEntityDef(const IEntityClassPtr& def)
{
    // Construct the prefix for the desired difficulty level
    std::string diffPrefix = "diff_" + string::to_string(_level) + "_";
    std::string prefix = diffPrefix + "change_";

    eclass::AttributeList spawnargs = eclass::getSpawnargsWithPrefix(def, prefix);

    for (eclass::AttributeList::iterator i = spawnargs.begin();
         i != spawnargs.end(); ++i)
    {
        EntityClassAttribute& attr = *i;

        if (attr.getValue().empty()) {
            continue; // empty spawnarg attribute => invalid
        }

        // Get the index from the string's tail
        std::string indexStr = attr.getName().substr(prefix.length());

        SettingPtr setting = createSetting(
            def->getAttributeValue(diffPrefix + "class_" + indexStr)
        );
        setting->spawnArg = attr.getValue();
        setting->argument = def->getAttributeValue(diffPrefix + "arg_" + indexStr);

        // This has been parsed from the default entityDef
        setting->isDefault = true;

        // Interpret/parse the argument string
        setting->parseAppType();
    }

    clearTreeModel();
    updateTreeModel();
}

void DifficultySettings::parseFromMapEntity(Entity* entity) {
    // Construct the prefix for the desired difficulty level
    std::string diffPrefix = "diff_" + string::to_string(_level) + "_";
    std::string prefix = diffPrefix + "change_";

    Entity::KeyValuePairs spawnargs = entity->getKeyValuePairs(prefix);

    for (Entity::KeyValuePairs::iterator i = spawnargs.begin();
         i != spawnargs.end(); ++i)
    {
        const std::string& key = i->first;
        const std::string& value = i->second;

        if (value.empty()) {
            continue; // empty spawnarg attribute => invalid
        }

        // Get the index from the string's tail
        std::string indexStr = key.substr(prefix.length());

        std::string className = entity->getKeyValue(diffPrefix + "class_" + indexStr);
        SettingPtr setting = createSetting(className);
        setting->spawnArg = value;
        setting->argument = entity->getKeyValue(diffPrefix + "arg_" + indexStr);

        // This has been parsed from the default entityDef
        setting->isDefault = false;

        // Interpret/parse the argument string
        setting->parseAppType();
    }

    clearTreeModel();
    updateTreeModel();
}

void DifficultySettings::saveToEntity(DifficultyEntity& target)
{
    // Cycle through all settings
    for (SettingsMap::iterator i = _settings.begin(); i != _settings.end(); ++i)
    {
        const SettingPtr& setting = i->second;

        if (setting->isDefault) {
            // Don't save default settings
            continue;
        }

        // Write the setting to the entity
        target.writeSetting(setting, _level);
    }
}

} // namespace difficulty