| 1 |
#include "ObjectiveKeyExtractor.h" |
| 2 |
|
| 3 |
#include "itextstream.h" |
| 4 |
|
| 5 |
#include <regex> |
| 6 |
#include "string/predicate.h" |
| 7 |
|
| 8 |
#include "string/split.h" |
| 9 |
#include "string/convert.h" |
| 10 |
|
| 11 |
namespace objectives { |
| 12 |
|
| 13 |
// Required entity visit function |
| 14 |
void ObjectiveKeyExtractor::operator()(const std::string& key, |
| 15 |
const std::string& value) |
| 16 |
{ |
| 17 |
// Quick discard of any non-objective keys |
| 18 |
if (key.substr(0, 3) != "obj") |
| 19 |
return; |
| 20 |
|
| 21 |
// Extract the objective number |
| 22 |
static const std::regex reObjNum("obj(\\d+)_(.*)"); |
| 23 |
std::smatch results; |
| 24 |
int iNum; |
| 25 |
|
| 26 |
if (std::regex_match(key, results, reObjNum)) { |
| 27 |
// Get the objective number |
| 28 |
iNum = string::convert<int>(results[1].str()); |
| 29 |
} |
| 30 |
else { |
| 31 |
// No match, abort |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
// We now have the objective number and the substring (everything after |
| 36 |
// "obj<n>_" which applies to this objective. |
| 37 |
std::string objSubString = results[2]; |
| 38 |
|
| 39 |
// Switch on the substring |
| 40 |
if (objSubString == "desc") { |
| 41 |
_objMap[iNum].description = value; |
| 42 |
} |
| 43 |
else if (objSubString == "ongoing") { |
| 44 |
_objMap[iNum].ongoing = (value == "1"); |
| 45 |
} |
| 46 |
else if (objSubString == "mandatory") { |
| 47 |
_objMap[iNum].mandatory = (value == "1"); |
| 48 |
} |
| 49 |
else if (objSubString == "visible") { |
| 50 |
_objMap[iNum].visible = (value == "1"); |
| 51 |
} |
| 52 |
else if (objSubString == "irreversible") { |
| 53 |
_objMap[iNum].irreversible = (value == "1"); |
| 54 |
} |
| 55 |
else if (objSubString == "state") { |
| 56 |
_objMap[iNum].state = |
| 57 |
static_cast<Objective::State>(string::convert<int>(value)); |
| 58 |
} |
| 59 |
else if (objSubString == "difficulty") { |
| 60 |
_objMap[iNum].difficultyLevels = value; |
| 61 |
} |
| 62 |
else if (objSubString == "enabling_objs") { |
| 63 |
_objMap[iNum].enablingObjs = value; |
| 64 |
} |
| 65 |
else if (objSubString == "script_complete") { |
| 66 |
_objMap[iNum].completionScript = value; |
| 67 |
} |
| 68 |
else if (objSubString == "script_failed") { |
| 69 |
_objMap[iNum].failureScript = value; |
| 70 |
} |
| 71 |
else if (objSubString == "target_complete") { |
| 72 |
_objMap[iNum].completionTarget = value; |
| 73 |
} |
| 74 |
else if (objSubString == "target_failed") { |
| 75 |
_objMap[iNum].failureTarget = value; |
| 76 |
} |
| 77 |
else if (objSubString == "logic_success") { |
| 78 |
_objMap[iNum].logic.successLogic = value; |
| 79 |
} |
| 80 |
else if (objSubString == "logic_failure") { |
| 81 |
_objMap[iNum].logic.failureLogic = value; |
| 82 |
} |
| 83 |
else { |
| 84 |
|
| 85 |
// Use another regex to check for components (obj1_1_blah) |
| 86 |
static const std::regex reComponent("(\\d+)_(.*)"); |
| 87 |
std::smatch compResults; |
| 88 |
|
| 89 |
if (!std::regex_match(objSubString, compResults, reComponent)) { |
| 90 |
return; |
| 91 |
} |
| 92 |
else { |
| 93 |
|
| 94 |
// Get the component number and key string |
| 95 |
int componentNum = string::convert<int>(compResults[1].str()); |
| 96 |
std::string componentStr = compResults[2]; |
| 97 |
|
| 98 |
Component& comp = _objMap[iNum].components[componentNum]; |
| 99 |
|
| 100 |
// Switch on the key string |
| 101 |
if (componentStr == "type") { |
| 102 |
comp.setType(ComponentType::getComponentType(value)); |
| 103 |
} |
| 104 |
else if (componentStr == "state") { |
| 105 |
comp.setSatisfied(value == "1"); |
| 106 |
} |
| 107 |
else if (componentStr == "not") { |
| 108 |
comp.setInverted(value == "1"); |
| 109 |
} |
| 110 |
else if (componentStr == "irreversible") { |
| 111 |
comp.setIrreversible(value == "1"); |
| 112 |
} |
| 113 |
else if (componentStr == "player_responsible") { |
| 114 |
comp.setPlayerResponsible(value == "1"); |
| 115 |
} |
| 116 |
else if (componentStr == "args") { |
| 117 |
// We have a component argument string |
| 118 |
std::vector<std::string> parts; |
| 119 |
string::split(parts, value, " "); |
| 120 |
|
| 121 |
comp.clearArguments(); |
| 122 |
|
| 123 |
// Add all found arguments to the component |
| 124 |
for (std::size_t i = 0; i < parts.size(); i++) { |
| 125 |
comp.addArgument(parts[i]); |
| 126 |
} |
| 127 |
} |
| 128 |
else if (componentStr == "clock_interval") { |
| 129 |
comp.setClockInterval(string::convert<float>(value)); |
| 130 |
} |
| 131 |
// Check for the spec_val first |
| 132 |
else if (string::starts_with(componentStr, "spec_val")) |
| 133 |
{ |
| 134 |
// We have a component specifier value, see if the specifier itself is there already |
| 135 |
Specifier::SpecifierNumber specNum = getSpecifierNumber( |
| 136 |
string::convert<int>(componentStr.substr(8), -1) |
| 137 |
); |
| 138 |
|
| 139 |
if (specNum == Specifier::MAX_SPECIFIERS) { |
| 140 |
rError() << |
| 141 |
"[ObjectivesEditor]: Could not parse specifier value spawnarg " << |
| 142 |
key << std::endl; |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
if (comp.getSpecifier(specNum) == NULL) { |
| 147 |
// No specifier exists yet, allocate a new one |
| 148 |
comp.setSpecifier(specNum, SpecifierPtr(new Specifier)); |
| 149 |
} |
| 150 |
|
| 151 |
// Component exists, store the variable |
| 152 |
comp.getSpecifier(specNum)->setValue(value); |
| 153 |
} |
| 154 |
// if "check_val" didn't match, look for "spec" alone |
| 155 |
else if (string::starts_with(componentStr, "spec")) |
| 156 |
{ |
| 157 |
// We have a component specifier, see which one |
| 158 |
Specifier::SpecifierNumber specNum = getSpecifierNumber( |
| 159 |
string::convert<int>(componentStr.substr(4), -1) |
| 160 |
); |
| 161 |
|
| 162 |
if (specNum == Specifier::MAX_SPECIFIERS) { |
| 163 |
rError() << |
| 164 |
"[ObjectivesEditor]: Could not parse specifier spawnarg " << |
| 165 |
key << std::endl; |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
if (comp.getSpecifier(specNum) == NULL) { |
| 170 |
// No specifier exists yet, allocate a new one |
| 171 |
comp.setSpecifier(specNum, SpecifierPtr(new Specifier)); |
| 172 |
} |
| 173 |
|
| 174 |
// At this point, a specifier exists, set the parsed type |
| 175 |
comp.getSpecifier(specNum)->setType( |
| 176 |
SpecifierType::getSpecifierType(value) |
| 177 |
); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
Specifier::SpecifierNumber ObjectiveKeyExtractor::getSpecifierNumber(int specNum) { |
| 184 |
// greebo: specifier numbers start with 1 for user convenience, |
| 185 |
// but we need valid array indices starting from 0, so subtract 1 |
| 186 |
specNum--; |
| 187 |
|
| 188 |
// Sanity-check the incoming index |
| 189 |
if (specNum < Specifier::FIRST_SPECIFIER || specNum >= Specifier::MAX_SPECIFIERS) { |
| 190 |
return Specifier::MAX_SPECIFIERS; |
| 191 |
} |
| 192 |
|
| 193 |
// Sanity-checks passed |
| 194 |
return static_cast<Specifier::SpecifierNumber>(specNum); |
| 195 |
} |
| 196 |
|
| 197 |
} // namespace objectives |