| 1 |
#include "DifficultySettings.h" |
| 2 |
|
| 3 |
#include "i18n.h" |
| 4 |
#include "string/convert.h" |
| 5 |
#include "eclass.h" |
| 6 |
|
| 7 |
namespace difficulty |
| 8 |
{ |
| 9 |
|
| 10 |
DifficultySettings::DifficultySettings(int level) : |
| 11 |
_level(level), |
| 12 |
_store(new wxutil::TreeModel(_columns)) |
| 13 |
{} |
| 14 |
|
| 15 |
DifficultySettings::~DifficultySettings() |
| 16 |
{} |
| 17 |
|
| 18 |
const DifficultySettings::TreeModelColumns& DifficultySettings::getColumns() const |
| 19 |
{ |
| 20 |
return _columns; |
| 21 |
} |
| 22 |
|
| 23 |
wxutil::TreeModel::Ptr DifficultySettings::getTreeStore() const |
| 24 |
{ |
| 25 |
return _store; |
| 26 |
} |
| 27 |
|
| 28 |
int DifficultySettings::getLevel() const |
| 29 |
{ |
| 30 |
return _level; |
| 31 |
} |
| 32 |
|
| 33 |
void DifficultySettings::clear() |
| 34 |
{ |
| 35 |
_settings.clear(); |
| 36 |
_settingIds.clear(); |
| 37 |
_iterMap.clear(); |
| 38 |
} |
| 39 |
|
| 40 |
SettingPtr DifficultySettings::getSettingById(int id) const { |
| 41 |
// Search all stored settings matching this classname |
| 42 |
SettingIdMap::const_iterator found = _settingIds.find(id); |
| 43 |
|
| 44 |
if (found != _settingIds.end()) { |
| 45 |
return found->second; |
| 46 |
} |
| 47 |
|
| 48 |
return SettingPtr(); // not found |
| 49 |
} |
| 50 |
|
| 51 |
SettingPtr DifficultySettings::findOrCreateOverrule(const SettingPtr& existing) |
| 52 |
{ |
| 53 |
// Get the inheritancekey needed to lookup the classname |
| 54 |
std::string inheritanceKey = getInheritanceKey(existing->className); |
| 55 |
|
| 56 |
// Check if there is already an override active for the <existing> setting |
| 57 |
for (SettingsMap::iterator i = _settings.find(inheritanceKey); |
| 58 |
i != _settings.upper_bound(inheritanceKey) && i != _settings.end(); |
| 59 |
++i) |
| 60 |
{ |
| 61 |
// Avoid self==self comparisons |
| 62 |
if (i->second != existing && i->second->spawnArg == existing->spawnArg) { |
| 63 |
// spawnarg is set on a different setting, check if it is non-default |
| 64 |
if (!i->second->isDefault) { |
| 65 |
// non-default, overriding setting, return it |
| 66 |
return i->second; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// No overriding setting found, create a new one |
| 72 |
SettingPtr setting = createSetting(existing->className); |
| 73 |
setting->spawnArg = existing->spawnArg; |
| 74 |
setting->isDefault = false; |
| 75 |
setting->appType = Setting::EAssign; |
| 76 |
|
| 77 |
return setting; |
| 78 |
} |
| 79 |
|
| 80 |
int DifficultySettings::save(int id, const SettingPtr& setting) { |
| 81 |
if (id != -1) { |
| 82 |
// We're dealing with an existing setting, fetch it |
| 83 |
SettingPtr existing = getSettingById(id); |
| 84 |
|
| 85 |
if (existing == NULL) { |
| 86 |
return -1; |
| 87 |
} |
| 88 |
|
| 89 |
if (existing->isDefault) { |
| 90 |
// We're trying to save a default setting, go into override mode |
| 91 |
|
| 92 |
if (*setting == *existing) { |
| 93 |
// New settings are identical to the existing ones, skip |
| 94 |
return existing->id; |
| 95 |
} |
| 96 |
|
| 97 |
// Create a new setting |
| 98 |
SettingPtr overrule = findOrCreateOverrule(existing); |
| 99 |
// Transfer the argument/appType into the new setting |
| 100 |
overrule->argument = setting->argument; |
| 101 |
overrule->appType = setting->appType; |
| 102 |
return overrule->id; |
| 103 |
} |
| 104 |
else { |
| 105 |
// TODO: Check, if there is another, existing setting which we |
| 106 |
// override with this one and whether they are the same |
| 107 |
|
| 108 |
// Copy the settings over to the existing setting |
| 109 |
*existing = *setting; |
| 110 |
return existing->id; |
| 111 |
} |
| 112 |
} |
| 113 |
else { |
| 114 |
// No setting highlighted, create a new one |
| 115 |
SettingPtr newSetting = createSetting(setting->className); |
| 116 |
// Copy the settings over |
| 117 |
*newSetting = *setting; |
| 118 |
newSetting->isDefault = false; |
| 119 |
return newSetting->id; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
void DifficultySettings::deleteSetting(int id) |
| 124 |
{ |
| 125 |
for (SettingsMap::iterator i = _settings.begin(); i != _settings.end(); ++i) |
| 126 |
{ |
| 127 |
if (i->second->id == id) |
| 128 |
{ |
| 129 |
// Found it, remove it from the tree and all maps |
| 130 |
_store->RemoveItem(i->second->iter); |
| 131 |
|
| 132 |
_settings.erase(i); |
| 133 |
_settingIds.erase(id); |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// Override settings might have been changed, update the model |
| 139 |
updateTreeModel(); |
| 140 |
} |
| 141 |
|
| 142 |
void DifficultySettings::updateTreeModel() |
| 143 |
{ |
| 144 |
// Go through the settings and check the corresponding iters in the tree |
| 145 |
for (SettingsMap::iterator i = _settings.begin(); i != _settings.end(); ++i) |
| 146 |
{ |
| 147 |
const std::string& className = i->second->className; |
| 148 |
Setting& setting = *i->second; |
| 149 |
|
| 150 |
// Ensure that the classname is in the map |
| 151 |
wxDataViewItem classIter = findOrInsertClassname(className); |
| 152 |
bool settingAdded = false; |
| 153 |
|
| 154 |
if (!setting.iter.IsOk()) |
| 155 |
{ |
| 156 |
// No iter corresponding to this setting yet, insert it |
| 157 |
setting.iter = classIter.IsOk() ? _store->AddItemUnderParent(classIter).getItem() : _store->AddItem().getItem(); |
| 158 |
settingAdded = true; |
| 159 |
} |
| 160 |
|
| 161 |
wxutil::TreeModel::Row row(setting.iter, *_store); |
| 162 |
|
| 163 |
bool overridden = isOverridden(i->second); |
| 164 |
|
| 165 |
wxDataViewItemAttr colour; |
| 166 |
colour.SetColour(setting.isDefault ? wxColor(112,112,112) : wxColor(0,0,0)); |
| 167 |
|
| 168 |
row[_columns.description] = setting.getDescString() + (overridden ? _(" (overridden)") : ""); |
| 169 |
row[_columns.description].setAttr(colour); |
| 170 |
|
| 171 |
row[_columns.classname] = setting.className; |
| 172 |
row[_columns.settingId] = setting.id; |
| 173 |
row[_columns.isOverridden] = overridden; |
| 174 |
|
| 175 |
if (settingAdded) |
| 176 |
{ |
| 177 |
row.SendItemAdded(); |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
row.SendItemChanged(); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
void DifficultySettings::clearTreeModel() |
| 187 |
{ |
| 188 |
_iterMap.clear(); |
| 189 |
_store->Clear(); |
| 190 |
|
| 191 |
// Clear any iterators stored in the settings |
| 192 |
for (SettingsMap::iterator i = _settings.begin(); i != _settings.end(); ++i) |
| 193 |
{ |
| 194 |
i->second->iter = wxDataViewItem(); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void DifficultySettings::refreshTreeModel() |
| 199 |
{ |
| 200 |
clearTreeModel(); |
| 201 |
updateTreeModel(); |
| 202 |
} |
| 203 |
|
| 204 |
bool DifficultySettings::isOverridden(const SettingPtr& setting) |
| 205 |
{ |
| 206 |
if (!setting->isDefault) { |
| 207 |
return false; // not a default setting, return false |
| 208 |
} |
| 209 |
|
| 210 |
// Get the inheritancekey needed to lookup the classname |
| 211 |
std::string inheritanceKey = getInheritanceKey(setting->className); |
| 212 |
|
| 213 |
// Search all other settings for the same className/spawnArg combination |
| 214 |
for (SettingsMap::iterator i = _settings.find(inheritanceKey); |
| 215 |
i != _settings.upper_bound(inheritanceKey) && i != _settings.end(); |
| 216 |
++i) |
| 217 |
{ |
| 218 |
// Avoid self==self comparisons |
| 219 |
if (i->second != setting && i->second->spawnArg == setting->spawnArg) { |
| 220 |
// spawnarg is set on a different setting, check if it is non-default |
| 221 |
if (!i->second->isDefault) { |
| 222 |
// non-default, overriding setting, return true |
| 223 |
return true; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
std::string DifficultySettings::getParentClass(const std::string& className) |
| 232 |
{ |
| 233 |
// Get the parent eclass |
| 234 |
IEntityClassPtr eclass = GlobalEntityClassManager().findClass(className); |
| 235 |
if (!eclass) |
| 236 |
return ""; // Invalid! |
| 237 |
|
| 238 |
return eclass->getAttributeValue("inherit"); |
| 239 |
} |
| 240 |
|
| 241 |
wxDataViewItem DifficultySettings::findOrInsertClassname(const std::string& className) |
| 242 |
{ |
| 243 |
// Try to look up the classname in the tree |
| 244 |
TreeIterMap::iterator found = _iterMap.find(className); |
| 245 |
|
| 246 |
if (found != _iterMap.end()) |
| 247 |
{ |
| 248 |
// Name exists, return this |
| 249 |
return found->second; |
| 250 |
} |
| 251 |
|
| 252 |
// This iter will hold the parent element, if such is found |
| 253 |
wxDataViewItem parentIter; |
| 254 |
|
| 255 |
// Classname is not yet registered, walk up the inheritance tree |
| 256 |
std::string parentClassName = getParentClass(className); |
| 257 |
while (!parentClassName.empty()) |
| 258 |
{ |
| 259 |
// Try to look up the classname in the tree |
| 260 |
TreeIterMap::iterator foundParent = _iterMap.find(parentClassName); |
| 261 |
|
| 262 |
if (foundParent != _iterMap.end()) |
| 263 |
{ |
| 264 |
parentIter = foundParent->second; |
| 265 |
break; |
| 266 |
} |
| 267 |
|
| 268 |
parentClassName = getParentClass(parentClassName); |
| 269 |
} |
| 270 |
|
| 271 |
// Insert the map, using the found iter (or NULL, if nothing was found) |
| 272 |
wxDataViewItem inserted = insertClassName(className, parentIter); |
| 273 |
|
| 274 |
// Remember the iter |
| 275 |
_iterMap.insert(TreeIterMap::value_type(className, inserted)); |
| 276 |
|
| 277 |
return inserted; |
| 278 |
} |
| 279 |
|
| 280 |
wxDataViewItem DifficultySettings::insertClassName(const std::string& className, const wxDataViewItem& parent) |
| 281 |
{ |
| 282 |
wxutil::TreeModel::Row row = parent.IsOk() ? _store->AddItemUnderParent(parent) : _store->AddItem(); |
| 283 |
|
| 284 |
wxDataViewItemAttr black; |
| 285 |
black.SetColour(wxColor(0,0,0)); |
| 286 |
|
| 287 |
row[_columns.description] = className; |
| 288 |
row[_columns.description].setAttr(black); |
| 289 |
|
| 290 |
row[_columns.classname] = className; |
| 291 |
row[_columns.settingId] = -1; |
| 292 |
|
| 293 |
row.SendItemAdded(); |
| 294 |
|
| 295 |
return row.getItem(); |
| 296 |
} |
| 297 |
|
| 298 |
std::string DifficultySettings::getInheritanceKey(const std::string& className) |
| 299 |
{ |
| 300 |
if (className.empty()) return ""; |
| 301 |
|
| 302 |
IEntityClassPtr eclass = GlobalEntityClassManager().findClass(className); |
| 303 |
|
| 304 |
// Get the inheritance chain of this class |
| 305 |
std::list<std::string> inheritanceChain; |
| 306 |
for (IEntityClass* currentClass = eclass.get(); |
| 307 |
currentClass != NULL; |
| 308 |
currentClass = currentClass->getParent()) |
| 309 |
{ |
| 310 |
inheritanceChain.push_front(currentClass->getDeclName()); |
| 311 |
} |
| 312 |
|
| 313 |
// Build the inheritance key |
| 314 |
std::string inheritanceKey; |
| 315 |
for (std::list<std::string>::const_iterator c = inheritanceChain.begin(); |
| 316 |
c != inheritanceChain.end(); |
| 317 |
c++) |
| 318 |
{ |
| 319 |
inheritanceKey += (inheritanceKey.empty()) ? "" : "_"; |
| 320 |
inheritanceKey += *c; |
| 321 |
} |
| 322 |
|
| 323 |
return inheritanceKey; |
| 324 |
} |
| 325 |
|
| 326 |
SettingPtr DifficultySettings::createSetting(const std::string& className) |
| 327 |
{ |
| 328 |
SettingPtr setting(new Setting); |
| 329 |
setting->className = className; |
| 330 |
|
| 331 |
// Insert the parsed setting into our local map |
| 332 |
_settings.insert(SettingsMap::value_type(getInheritanceKey(className), setting)); |
| 333 |
_settingIds.insert(SettingIdMap::value_type(setting->id, setting)); |
| 334 |
|
| 335 |
return setting; |
| 336 |
} |
| 337 |
|
| 338 |
void DifficultySettings::parseFromEntityDef(const IEntityClassPtr& def) |
| 339 |
{ |
| 340 |
// Construct the prefix for the desired difficulty level |
| 341 |
std::string diffPrefix = "diff_" + string::to_string(_level) + "_"; |
| 342 |
std::string prefix = diffPrefix + "change_"; |
| 343 |
|
| 344 |
eclass::AttributeList spawnargs = eclass::getSpawnargsWithPrefix(def, prefix); |
| 345 |
|
| 346 |
for (eclass::AttributeList::iterator i = spawnargs.begin(); |
| 347 |
i != spawnargs.end(); ++i) |
| 348 |
{ |
| 349 |
EntityClassAttribute& attr = *i; |
| 350 |
|
| 351 |
if (attr.getValue().empty()) { |
| 352 |
continue; // empty spawnarg attribute => invalid |
| 353 |
} |
| 354 |
|
| 355 |
// Get the index from the string's tail |
| 356 |
std::string indexStr = attr.getName().substr(prefix.length()); |
| 357 |
|
| 358 |
SettingPtr setting = createSetting( |
| 359 |
def->getAttributeValue(diffPrefix + "class_" + indexStr) |
| 360 |
); |
| 361 |
setting->spawnArg = attr.getValue(); |
| 362 |
setting->argument = def->getAttributeValue(diffPrefix + "arg_" + indexStr); |
| 363 |
|
| 364 |
// This has been parsed from the default entityDef |
| 365 |
setting->isDefault = true; |
| 366 |
|
| 367 |
// Interpret/parse the argument string |
| 368 |
setting->parseAppType(); |
| 369 |
} |
| 370 |
|
| 371 |
clearTreeModel(); |
| 372 |
updateTreeModel(); |
| 373 |
} |
| 374 |
|
| 375 |
void DifficultySettings::parseFromMapEntity(Entity* entity) { |
| 376 |
// Construct the prefix for the desired difficulty level |
| 377 |
std::string diffPrefix = "diff_" + string::to_string(_level) + "_"; |
| 378 |
std::string prefix = diffPrefix + "change_"; |
| 379 |
|
| 380 |
Entity::KeyValuePairs spawnargs = entity->getKeyValuePairs(prefix); |
| 381 |
|
| 382 |
for (Entity::KeyValuePairs::iterator i = spawnargs.begin(); |
| 383 |
i != spawnargs.end(); ++i) |
| 384 |
{ |
| 385 |
const std::string& key = i->first; |
| 386 |
const std::string& value = i->second; |
| 387 |
|
| 388 |
if (value.empty()) { |
| 389 |
continue; // empty spawnarg attribute => invalid |
| 390 |
} |
| 391 |
|
| 392 |
// Get the index from the string's tail |
| 393 |
std::string indexStr = key.substr(prefix.length()); |
| 394 |
|
| 395 |
std::string className = entity->getKeyValue(diffPrefix + "class_" + indexStr); |
| 396 |
SettingPtr setting = createSetting(className); |
| 397 |
setting->spawnArg = value; |
| 398 |
setting->argument = entity->getKeyValue(diffPrefix + "arg_" + indexStr); |
| 399 |
|
| 400 |
// This has been parsed from the default entityDef |
| 401 |
setting->isDefault = false; |
| 402 |
|
| 403 |
// Interpret/parse the argument string |
| 404 |
setting->parseAppType(); |
| 405 |
} |
| 406 |
|
| 407 |
clearTreeModel(); |
| 408 |
updateTreeModel(); |
| 409 |
} |
| 410 |
|
| 411 |
void DifficultySettings::saveToEntity(DifficultyEntity& target) |
| 412 |
{ |
| 413 |
// Cycle through all settings |
| 414 |
for (SettingsMap::iterator i = _settings.begin(); i != _settings.end(); ++i) |
| 415 |
{ |
| 416 |
const SettingPtr& setting = i->second; |
| 417 |
|
| 418 |
if (setting->isDefault) { |
| 419 |
// Don't save default settings |
| 420 |
continue; |
| 421 |
} |
| 422 |
|
| 423 |
// Write the setting to the entity |
| 424 |
target.writeSetting(setting, _level); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
} // namespace difficulty |