| 1 |
#include "DifficultySettingsManager.h" |
| 2 |
|
| 3 |
#include "ieclass.h" |
| 4 |
#include "itextstream.h" |
| 5 |
#include "entitylib.h" |
| 6 |
#include "gamelib.h" |
| 7 |
#include "maplib.h" |
| 8 |
#include "string/string.h" |
| 9 |
#include "registry/registry.h" |
| 10 |
#include "DifficultyEntity.h" |
| 11 |
#include "DifficultyEntityFinder.h" |
| 12 |
|
| 13 |
namespace difficulty { |
| 14 |
|
| 15 |
DifficultySettingsPtr DifficultySettingsManager::getSettings(int level) { |
| 16 |
for (std::size_t i = 0; i < _settings.size(); i++) { |
| 17 |
if (_settings[i]->getLevel() == level) { |
| 18 |
return _settings[i]; |
| 19 |
} |
| 20 |
} |
| 21 |
return DifficultySettingsPtr(); |
| 22 |
} |
| 23 |
|
| 24 |
void DifficultySettingsManager::loadSettings() { |
| 25 |
loadDefaultSettings(); |
| 26 |
loadMapSettings(); |
| 27 |
loadDifficultyNames(); |
| 28 |
} |
| 29 |
|
| 30 |
void DifficultySettingsManager::loadDefaultSettings() { |
| 31 |
// Try to lookup the given entityDef |
| 32 |
IEntityClassPtr eclass = GlobalEntityClassManager().findClass( |
| 33 |
game::current::getValue<std::string>(GKEY_DIFFICULTY_ENTITYDEF_DEFAULT) |
| 34 |
); |
| 35 |
|
| 36 |
if (eclass == NULL) { |
| 37 |
rError() << "Could not find default difficulty settings entityDef.\n"; |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
// greebo: Setup the default difficulty levels using the found entityDef |
| 42 |
int numLevels = game::current::getValue<int>(GKEY_DIFFICULTY_LEVELS); |
| 43 |
for (int i = 0; i < numLevels; i++) { |
| 44 |
// Allocate a new settings object |
| 45 |
DifficultySettingsPtr settings(new DifficultySettings(i)); |
| 46 |
|
| 47 |
// Parse the settings from the given default settings entityDef |
| 48 |
settings->parseFromEntityDef(eclass); |
| 49 |
|
| 50 |
// Store the settings object in the local list |
| 51 |
_settings.push_back(settings); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
void DifficultySettingsManager::loadMapSettings() |
| 56 |
{ |
| 57 |
// Construct a helper walker |
| 58 |
DifficultyEntityFinder finder; |
| 59 |
GlobalSceneGraph().root()->traverse(finder); |
| 60 |
|
| 61 |
const DifficultyEntityFinder::EntityList& found = finder.getEntities(); |
| 62 |
|
| 63 |
// Pop all entities into each difficulty setting |
| 64 |
for (DifficultyEntityFinder::EntityList::const_iterator ent = found.begin(); |
| 65 |
ent != found.end(); ent++) |
| 66 |
{ |
| 67 |
for (std::size_t i = 0; i < _settings.size(); i++) { |
| 68 |
_settings[i]->parseFromMapEntity(*ent); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
namespace |
| 74 |
{ |
| 75 |
|
| 76 |
// Class to manage mod-specific difficulty names (which are probably string |
| 77 |
// table entries like "#str_000300") and their English translations (we |
| 78 |
// maintain these translations separately in the .game file since we don't have |
| 79 |
// any support for parsing the mod-specific translation data). |
| 80 |
class ModDifficultyNames |
| 81 |
{ |
| 82 |
// Lookup table of strings and their translations |
| 83 |
std::map<std::string, std::string> _transStrings; |
| 84 |
|
| 85 |
// Mod-specific entity giving default difficulty names |
| 86 |
IEntityClassPtr _menuEclass; |
| 87 |
|
| 88 |
public: |
| 89 |
|
| 90 |
// Construct and initialise |
| 91 |
ModDifficultyNames() |
| 92 |
: _menuEclass( |
| 93 |
GlobalEntityClassManager().findClass( |
| 94 |
game::current::getValue<std::string>(GKEY_DIFFICULTY_ENTITYDEF_MENU) |
| 95 |
) |
| 96 |
) |
| 97 |
{ |
| 98 |
// Load translations from xml nodes in the .game file |
| 99 |
xml::NodeList transNodes = game::current::getNodes( |
| 100 |
GKEY_DIFFICULTY_ENTITYDEF_MENU + "/string" |
| 101 |
); |
| 102 |
for (auto xmlNode: transNodes) |
| 103 |
{ |
| 104 |
_transStrings[xmlNode.getAttributeValue("id")] = xmlNode.getContent(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// Return the translated name for the difficulty level identified by the |
| 109 |
// given key (e.g. the translated name for "diff0Default" might be "Easy"). |
| 110 |
std::string getNameForKey(const std::string& nameKey) const |
| 111 |
{ |
| 112 |
if (_menuEclass) |
| 113 |
{ |
| 114 |
std::string rawName = _menuEclass->getAttributeValue(nameKey); |
| 115 |
if (!rawName.empty()) |
| 116 |
{ |
| 117 |
// Look for a translation, otherwise use the raw name |
| 118 |
auto found = _transStrings.find(rawName); |
| 119 |
if (found != _transStrings.end()) |
| 120 |
return found->second; |
| 121 |
else |
| 122 |
return rawName; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return ""; |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
// Return key for a difficulty level name |
| 131 |
std::string diffNameKeyForLevel(int level) |
| 132 |
{ |
| 133 |
return "diff" + std::to_string(level) + "default"; |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
void DifficultySettingsManager::loadDifficultyNames() |
| 139 |
{ |
| 140 |
// Load mod-specific difficulty names first if possible |
| 141 |
ModDifficultyNames diffNames; |
| 142 |
|
| 143 |
// Look up a name for each difficulty level, starting with a map-specific |
| 144 |
// name set on the worldspawn, then falling back to mod default names and |
| 145 |
// then autogenerated names. |
| 146 |
int numLevels = game::current::getValue<int>(GKEY_DIFFICULTY_LEVELS); |
| 147 |
|
| 148 |
for (int i = 0; i < numLevels; i++) |
| 149 |
{ |
| 150 |
std::string nameKey = diffNameKeyForLevel(i); |
| 151 |
|
| 152 |
// Load the mod default, get the translated value by the helper class |
| 153 |
std::string defaultName = diffNames.getNameForKey(nameKey); |
| 154 |
|
| 155 |
if (!defaultName.empty()) |
| 156 |
{ |
| 157 |
// Use the (hopefully translated) mod-specific default name |
| 158 |
_difficultyNames.push_back(defaultName); |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
// Fall back to a non-empty default |
| 163 |
_difficultyNames.push_back(std::to_string(i)); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// Store the current set as default for later change detection |
| 168 |
_defaultDifficultyNames = _difficultyNames; |
| 169 |
|
| 170 |
// Locate the worldspawn entity to find any map-specific names |
| 171 |
Entity* worldspawn = map::current::getWorldspawn(); |
| 172 |
|
| 173 |
if (worldspawn != nullptr) |
| 174 |
{ |
| 175 |
assert(static_cast<int>(_difficultyNames.size()) == numLevels); |
| 176 |
|
| 177 |
for (int i = 0; i < numLevels; i++) |
| 178 |
{ |
| 179 |
std::string nameKey = diffNameKeyForLevel(i); |
| 180 |
|
| 181 |
// First, try to find a map-specific name |
| 182 |
std::string name = worldspawn->getKeyValue(nameKey); |
| 183 |
|
| 184 |
if (!name.empty()) |
| 185 |
{ |
| 186 |
// Found a setting on worldspawn, take it |
| 187 |
_difficultyNames[i] = name; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
void DifficultySettingsManager::saveSettings() |
| 194 |
{ |
| 195 |
// Locates all difficulty entities |
| 196 |
DifficultyEntityFinder finder; |
| 197 |
GlobalSceneGraph().root()->traverse(finder); |
| 198 |
|
| 199 |
// Copy the list from the finder to a local list |
| 200 |
DifficultyEntityFinder::EntityList entities = finder.getEntities(); |
| 201 |
|
| 202 |
if (entities.empty()) |
| 203 |
{ |
| 204 |
// Create a new difficulty entity |
| 205 |
std::string eclassName = game::current::getValue<std::string>(GKEY_DIFFICULTY_ENTITYDEF_MAP); |
| 206 |
IEntityClassPtr diffEclass = GlobalEntityClassManager().findClass(eclassName); |
| 207 |
|
| 208 |
if (diffEclass == NULL) { |
| 209 |
rError() << "[Diff]: Cannot create difficulty entity!\n"; |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
// Create and insert a new entity node into the scenegraph root |
| 214 |
IEntityNodePtr entNode = GlobalEntityModule().createEntity(diffEclass); |
| 215 |
GlobalSceneGraph().root()->addChildNode(entNode); |
| 216 |
|
| 217 |
// Add the entity to the list |
| 218 |
entities.push_back(&entNode->getEntity()); |
| 219 |
} |
| 220 |
|
| 221 |
// Clear all difficulty-spawnargs from existing entities |
| 222 |
for (DifficultyEntityFinder::EntityList::const_iterator i = entities.begin(); |
| 223 |
i != entities.end(); i++) |
| 224 |
{ |
| 225 |
// Construct a difficulty entity using the raw Entity* pointer |
| 226 |
DifficultyEntity diffEnt(*i); |
| 227 |
// Clear the difficulty-related spawnargs from the entity |
| 228 |
diffEnt.clear(); |
| 229 |
} |
| 230 |
|
| 231 |
// Take the first entity |
| 232 |
DifficultyEntity diffEnt(*entities.begin()); |
| 233 |
|
| 234 |
// Cycle through all settings objects and issue save call |
| 235 |
for (std::size_t i = 0; i < _settings.size(); i++) { |
| 236 |
_settings[i]->saveToEntity(diffEnt); |
| 237 |
} |
| 238 |
|
| 239 |
// Write the changed difficulty (and only those) names into the worldspawn |
| 240 |
Entity* worldspawn = map::current::getWorldspawn(true); |
| 241 |
|
| 242 |
if (!worldspawn) |
| 243 |
{ |
| 244 |
throw std::logic_error("DifficultySettingsManager::saveSettings():" |
| 245 |
" could not find or create worldspawn" |
| 246 |
); |
| 247 |
} |
| 248 |
|
| 249 |
for (std::size_t i = 0; i < _difficultyNames.size(); ++i) |
| 250 |
{ |
| 251 |
// Compare each value to the default set |
| 252 |
if (_defaultDifficultyNames.size() > i && _defaultDifficultyNames[i] != _difficultyNames[i]) |
| 253 |
{ |
| 254 |
worldspawn->setKeyValue(diffNameKeyForLevel(i), _difficultyNames[i]); |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
// Remove the corresponding spawnarg from the worldspawn if there's no override |
| 259 |
worldspawn->setKeyValue(diffNameKeyForLevel(i), ""); |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
std::string DifficultySettingsManager::getDifficultyName(int level) |
| 265 |
{ |
| 266 |
if (level < 0 || level >= static_cast<int>(_difficultyNames.size())) |
| 267 |
{ |
| 268 |
return ""; |
| 269 |
} |
| 270 |
|
| 271 |
return _difficultyNames[level]; |
| 272 |
} |
| 273 |
|
| 274 |
void DifficultySettingsManager::setDifficultyName(int level, const std::string& name) |
| 275 |
{ |
| 276 |
if (level < 0 || level >= static_cast<int>(_difficultyNames.size())) |
| 277 |
{ |
| 278 |
throw std::logic_error( |
| 279 |
"Invalid difficulty level (" + std::to_string(level) + ")" |
| 280 |
); |
| 281 |
} |
| 282 |
|
| 283 |
_difficultyNames[level] = name; |
| 284 |
} |
| 285 |
|
| 286 |
} // namespace difficulty |