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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
#include "ObjectiveEntity.h"
#include "ObjectiveKeyExtractor.h"
#include "TargetList.h"
#include "i18n.h"
#include "itextstream.h"
#include "iscenegraph.h"
#include "iundo.h"
#include "ientity.h"
#include "string/convert.h"
#include "string/predicate.h"
#include "string/split.h"
#include <fmt/format.h>
#include <regex>
#include <wx/choice.h>
namespace objectives {
namespace
{
const std::string KV_SUCCESS_LOGIC("mission_logic_success");
const std::string KV_FAILURE_LOGIC("mission_logic_failure");
const int INVALID_LEVEL_INDEX = -9999;
const std::string OBJ_COND_PREFIX("obj_condition_");
}
// Constructor
ObjectiveEntity::ObjectiveEntity(const scene::INodePtr& node) :
_entityNode(node)
{
Entity* entity = Node_getEntity(node);
assert(entity != NULL);
// Use an ObjectiveKeyExtractor to populate the ObjectiveMap from the keys
// on the entity
ObjectiveKeyExtractor extractor(_objectives);
entity->forEachKeyValue(extractor);
// Parse the logic strings from the entity
readMissionLogic(*entity);
readObjectiveConditions(*entity);
}
void ObjectiveEntity::readObjectiveConditions(Entity& ent)
{
_objConditions.clear(); // remove any previously parsed conditions
Entity::KeyValuePairs condSpawnargs = ent.getKeyValuePairs(OBJ_COND_PREFIX);
static const std::regex objCondExpr(OBJ_COND_PREFIX + "(\\d+)_(.*)");
for (Entity::KeyValuePairs::const_iterator kv = condSpawnargs.begin();
kv != condSpawnargs.end(); kv++)
{
std::smatch results;
if (!std::regex_match(kv->first, results, objCondExpr))
{
continue; // No match, abort
}
int index = string::convert<int>(results[1].str());
// Valid indices are [1..infinity)
if (index < 1)
{
continue; // invalid index, continue
}
const ObjectiveConditionPtr& cond = getOrCreateObjectiveCondition(index);
std::string postfix = results[2];
if (postfix == "src_mission")
{
cond->sourceMission = string::convert<int>(kv->second);
}
else if (postfix == "src_obj")
{
cond->sourceObjective = string::convert<int>(kv->second);
}
else if (postfix == "src_state")
{
int val = string::convert<int>(kv->second);
if (val >= Objective::INCOMPLETE && val < Objective::NUM_STATES)
{
cond->sourceState = static_cast<Objective::State>(val);
}
else
{
rWarning() << "Unsupported objective condition source state encountered: "
<< kv->second << std::endl;
}
}
else if (postfix == "target_obj")
{
cond->targetObjective = string::convert<int>(kv->second);
}
else if (postfix == "type")
{
if (kv->second == "changestate")
{
cond->type = ObjectiveCondition::CHANGE_STATE;
}
else if (kv->second == "changevisibility")
{
cond->type = ObjectiveCondition::CHANGE_VISIBILITY;
}
else if (kv->second == "changemandatory")
{
cond->type = ObjectiveCondition::CHANGE_MANDATORY;
}
else
{
rWarning() << "Unsupported objective condition type encountered: "
<< kv->second << std::endl;
}
}
else if (postfix == "value")
{
cond->value = string::convert<int>(kv->second);
}
}
}
void ObjectiveEntity::writeObjectiveConditions(Entity& ent)
{
// No need to clear previous set of obj_condition_ spawnargs,
// as they've been removed by clearEntity() already
// Spawnargs are numbered starting with 1 as first index
std::size_t index = 1;
// Go through all the conditions and save them. Skip invalid ones such that the
// set of conditions will be "compressed" in terms of their indices.
for (ObjectiveEntity::ConditionMap::const_iterator i = _objConditions.begin();
i != _objConditions.end(); ++i)
{
const ObjectiveCondition& cond = *i->second;
if (!cond.isValid())
{
continue; // skip invalid conditions without increasing the index
}
std::string prefix = fmt::format(OBJ_COND_PREFIX + "{0:d}_", index);
ent.setKeyValue(prefix + "src_mission", string::to_string(cond.sourceMission));
ent.setKeyValue(prefix + "src_obj", string::to_string(cond.sourceObjective));
ent.setKeyValue(prefix + "src_state", string::to_string(cond.sourceState));
ent.setKeyValue(prefix + "target_obj", string::to_string(cond.targetObjective));
std::string typeKey = prefix + "type";
switch (cond.type)
{
case ObjectiveCondition::CHANGE_STATE:
ent.setKeyValue(typeKey, "changestate");
break;
case ObjectiveCondition::CHANGE_VISIBILITY:
ent.setKeyValue(typeKey, "changevisibility");
break;
case ObjectiveCondition::CHANGE_MANDATORY:
ent.setKeyValue(typeKey, "changemandatory");
break;
default:
ent.setKeyValue(typeKey, ""); // empty value to be sure
rWarning() << "Invalid objective condition type encountered on saving." << std::endl;
break;
};
ent.setKeyValue(prefix + "value", string::to_string(cond.value));
++index; // next index
}
}
void ObjectiveEntity::readMissionLogic(Entity& ent)
{
// Find the success logic strings
Entity::KeyValuePairs successLogics = ent.getKeyValuePairs(KV_SUCCESS_LOGIC);
for (Entity::KeyValuePairs::const_iterator kv = successLogics.begin();
kv != successLogics.end(); kv++)
{
std::string postfix = kv->first.substr(KV_SUCCESS_LOGIC.size());
if (postfix.empty()) {
// Empty postfix means that we've found the default logic
LogicPtr logic = getMissionLogic(-1);
logic->successLogic = kv->second;
}
else if (string::starts_with(postfix, "_diff_"))
{
// We seem to have a difficulty-related logic, get the level
int level = string::convert<int>(postfix.substr(6), INVALID_LEVEL_INDEX);
if (level == INVALID_LEVEL_INDEX) {
rError() << "[ObjectivesEditor]: Cannot parse difficulty-specific " <<
"logic strings: " << kv->second << std::endl;
continue;
}
LogicPtr logic = getMissionLogic(level);
logic->successLogic = kv->second;
}
}
// Find the failure logic strings
Entity::KeyValuePairs failureLogics = ent.getKeyValuePairs(KV_FAILURE_LOGIC);
for (Entity::KeyValuePairs::const_iterator kv = failureLogics.begin();
kv != failureLogics.end(); kv++)
{
std::string postfix = kv->first.substr(KV_FAILURE_LOGIC.size());
if (postfix.empty()) {
// Empty postfix means that we've found the default logic
LogicPtr logic = getMissionLogic(-1);
logic->failureLogic = kv->second;
}
else if (string::starts_with(postfix, "_diff_"))
{
// We seem to have a difficulty-related logic, get the level
int level = string::convert<int>(postfix.substr(6), INVALID_LEVEL_INDEX);
if (level == INVALID_LEVEL_INDEX) {
rError() << "[ObjectivesEditor]: Cannot parse difficulty-specific " <<
"logic strings: " << kv->second << std::endl;
continue;
}
LogicPtr logic = getMissionLogic(level);
logic->failureLogic = kv->second;
}
}
}
void ObjectiveEntity::writeMissionLogic(Entity& ent)
{
for (LogicMap::iterator i = _logics.begin(); i != _logics.end(); i++) {
int index = i->first;
if (index == -1) {
// Default logic
ent.setKeyValue(KV_SUCCESS_LOGIC, i->second->successLogic);
ent.setKeyValue(KV_FAILURE_LOGIC, i->second->failureLogic);
}
else {
// Difficulty-specific logic
ent.setKeyValue(KV_SUCCESS_LOGIC + "_diff_" + string::to_string(index), i->second->successLogic);
ent.setKeyValue(KV_FAILURE_LOGIC + "_diff_" + string::to_string(index), i->second->failureLogic);
}
}
}
// Delete the entity's world node
void ObjectiveEntity::deleteWorldNode() {
// Try to convert the weak_ptr reference to a shared_ptr
scene::INodePtr node = _entityNode.lock();
if (node != NULL) {
GlobalSceneGraph().root()->removeChildNode(node);
}
}
// Add a new objective
void ObjectiveEntity::addObjective() {
// Locate the first unused id
int index = 1;
while (_objectives.find(index) != _objectives.end())
++index;
// Insert a new Objective at this ID.
Objective o;
o.description = fmt::format(_("New objective {0:d}"), index);
_objectives.insert(ObjectiveMap::value_type(index, o));
}
int ObjectiveEntity::moveObjective(int index, int delta)
{
// Calculate the target index
int targetIndex = index + delta;
if (targetIndex < getLowestObjIndex())
{
targetIndex = getLowestObjIndex() -1;
}
// Constrain the obj index to sane values
if (targetIndex < 0)
{
targetIndex = 0;
}
if (targetIndex > getHighestObjIndex())
{
targetIndex = getHighestObjIndex() + 1;
}
if (targetIndex == index) return index; // nothing to do
// Try to look up the command indices in the conversation
ObjectiveMap::iterator oldObj = _objectives.find(index);
ObjectiveMap::iterator newObj = _objectives.find(targetIndex);
if (oldObj == _objectives.end()) return -1; // invalid source objective
if (newObj == _objectives.end())
{
// no objective at the target index, just re-locate the source objective
Objective temp(oldObj->second);
_objectives.erase(oldObj);
_objectives[targetIndex] = temp;
}
else
{
// Both source and target indices exist, swap them
Objective temp(oldObj->second);
_objectives[index] = _objectives[targetIndex];
_objectives[targetIndex] = temp;
}
return targetIndex;
}
void ObjectiveEntity::deleteObjective(int index) {
// Look up the objective with the given index
ObjectiveMap::iterator i = _objectives.find(index);
if (i == _objectives.end()) {
// not found, nothing to do
return;
}
// Delete the found element
_objectives.erase(i++);
// Then iterate all the way to the highest index
while (i != _objectives.end()) {
// Decrease the index of this objective
int newIndex = i->first - 1;
// Copy the objective into a temporary object
Objective temp = i->second;
// Remove the old one
_objectives.erase(i++);
// Re-insert with new index
_objectives.insert(
ObjectiveMap::value_type(newIndex, temp)
);
}
}
// Test for targeting
bool ObjectiveEntity::isOnTargetList(const TargetList& list) const {
// Try to convert the weak_ptr reference to a shared_ptr
Entity* entity = Node_getEntity(_entityNode.lock());
assert(entity != NULL);
return list.isTargeted(entity);
}
LogicPtr ObjectiveEntity::getMissionLogic(int difficultyLevel) {
// The usual game, look up and insert if not found
LogicMap::iterator i = _logics.find(difficultyLevel);
if (i == _logics.end()) {
std::pair<LogicMap::iterator, bool> result = _logics.insert(
LogicMap::value_type(difficultyLevel, LogicPtr(new Logic))
);
i = result.first;
}
// At this point, the iterator is pointing to something valid
return i->second;
}
// Returns the full list of objective conditions by value
ObjectiveEntity::ConditionMap ObjectiveEntity::getObjectiveConditions() const
{
return _objConditions;
}
// Replaces the existing set of objective conditions with this new one
void ObjectiveEntity::setObjectiveConditions(const ObjectiveEntity::ConditionMap& conditions)
{
_objConditions = conditions;
}
std::size_t ObjectiveEntity::getNumObjectiveConditions() const
{
return _objConditions.size();
}
const ObjectiveConditionPtr& ObjectiveEntity::getOrCreateObjectiveCondition(int index)
{
ConditionMap::iterator i = _objConditions.find(index);
if (i == _objConditions.end())
{
// Insert and get iterator to new object
i = _objConditions.insert(ConditionMap::value_type(
index, ObjectiveConditionPtr(new ObjectiveCondition))).first;
}
return i->second;
}
void ObjectiveEntity::clearObjectiveConditions()
{
_objConditions.clear();
}
void ObjectiveEntity::populateChoice(wxChoice* choice) const
{
for (ObjectiveMap::const_iterator i = _objectives.begin();
i != _objectives.end();
++i)
{
choice->Append(i->second.description,
new wxStringClientData(string::to_string(i->first)));
}
}
void ObjectiveEntity::populateListStore(wxutil::TreeModel& store,
const ObjectivesListColumns& columns) const
{
for (ObjectiveMap::const_iterator i = _objectives.begin();
i != _objectives.end();
++i)
{
std::string diffStr = "all";
if (!i->second.difficultyLevels.empty()) {
// clear the string first
diffStr.clear();
// Split the string and increase each index by 1 for display (Level 1 == 0)
std::vector<std::string> parts;
string::split(parts, i->second.difficultyLevels, " ");
for (std::size_t d = 0; d < parts.size(); ++d) {
diffStr += (diffStr.empty()) ? "" : " ";
diffStr += string::to_string(string::convert<int>(parts[d]) + 1);
}
}
wxutil::TreeModel::Row row = store.AddItem();
row[columns.objNumber] = i->first;
row[columns.description] = i->second.description;
row[columns.difficultyLevel] = diffStr;
row.SendItemAdded();
}
}
// Write the Components from a single Objective to the underlying entity
void ObjectiveEntity::writeComponents(Entity* entity,
const std::string& keyPrefix, const Objective& obj
)
{
assert(entity != NULL);
for (Objective::ComponentMap::const_iterator i = obj.components.begin();
i != obj.components.end();
++i)
{
const Component& c = i->second;
// Component prefix is like obj1_2_blah
std::string prefix = keyPrefix + string::to_string(i->first) + "_";
// Write out Component keyvals
entity->setKeyValue(prefix + "state", c.isSatisfied() ? "1" : "0");
entity->setKeyValue(prefix + "not", c.isInverted() ? "1" : "0");
entity->setKeyValue(prefix + "irreversible", c.isIrreversible() ? "1": "0");
entity->setKeyValue(prefix + "player_responsible", c.isPlayerResponsible() ? "1" : "0");
entity->setKeyValue(prefix + "type", c.getType().getName());
entity->setKeyValue(prefix + "clock_interval",
c.getClockInterval() > 0 ? string::to_string(c.getClockInterval()) : "");
// Write out Specifier keyvals
for (int s = Specifier::FIRST_SPECIFIER; s < Specifier::MAX_SPECIFIERS; s++)
{
// The specifier index of the spawnargs is starting from 1, not 0
std::string indexStr = string::to_string(s + 1);
SpecifierPtr spec = c.getSpecifier(static_cast<Specifier::SpecifierNumber>(s));
if (spec != NULL) {
entity->setKeyValue(prefix + "spec" + indexStr, spec->getType().getName());
entity->setKeyValue(prefix + "spec_val" + indexStr, spec->getValue());
}
}
// Export the component arguments
entity->setKeyValue(prefix + "args", c.getArgumentString());
}
}
void ObjectiveEntity::clearEntity(Entity* entity) {
// Get all keyvalues matching the "obj" prefix.
Entity::KeyValuePairs keyValues = entity->getKeyValuePairs("obj");
for (Entity::KeyValuePairs::const_iterator i = keyValues.begin();
i != keyValues.end(); ++i)
{
// Set the spawnarg to empty, which is equivalent to a removal
entity->setKeyValue(i->first, "");
}
}
// Write out Objectives to entity keyvals
void ObjectiveEntity::writeToEntity()
{
UndoableCommand cmd("saveObjectives");
// Try to convert the weak_ptr reference to a shared_ptr
Entity* entity = Node_getEntity(_entityNode.lock());
assert(entity != NULL);
// greebo: Remove all objective-related spawnargs first
clearEntity(entity);
for (ObjectiveMap::const_iterator i = _objectives.begin();
i != _objectives.end();
++i)
{
// Obtain the Objective and construct the key prefix from the index
const Objective& o = i->second;
std::string prefix = "obj" + string::to_string(i->first) + "_";
// Set the entity keyvalues
entity->setKeyValue(prefix + "desc", o.description);
entity->setKeyValue(prefix + "ongoing", o.ongoing ? "1" : "0");
entity->setKeyValue(prefix + "visible", o.visible ? "1" : "0");
entity->setKeyValue(prefix + "mandatory", o.mandatory ? "1" : "0");
entity->setKeyValue(prefix + "irreversible",
o.irreversible ? "1" : "0");
entity->setKeyValue(prefix + "state", string::to_string(o.state));
// Write an empty "objN_difficulty" value when this objective applies to all levels
entity->setKeyValue(prefix + "difficulty", o.difficultyLevels);
entity->setKeyValue(prefix + "enabling_objs", o.enablingObjs);
entity->setKeyValue(prefix + "script_complete", o.completionScript);
entity->setKeyValue(prefix + "script_failed", o.failureScript);
entity->setKeyValue(prefix + "target_complete", o.completionTarget);
entity->setKeyValue(prefix + "target_failed", o.failureTarget);
entity->setKeyValue(prefix + "logic_success", o.logic.successLogic);
entity->setKeyValue(prefix + "logic_failure", o.logic.failureLogic);
// Write the Components for this Objective
writeComponents(entity, prefix, o);
}
// Export the mission success/failure logic
writeMissionLogic(*entity);
// Export objective conditions
writeObjectiveConditions(*entity);
}
} // namespace objectives
|