Quest.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. #include "Quest.h"
  2. #include "Game.h"
  3. QuestRequirementStorage::QuestRequirementStorage(Framework::Text requirementId)
  4. : ReferenceCounter(),
  5. requirementId(requirementId),
  6. fulfilled(0),
  7. data(new Framework::JSON::JSONObject())
  8. {}
  9. QuestRequirementStorage::~QuestRequirementStorage()
  10. {
  11. data->release();
  12. }
  13. void QuestRequirementStorage::putValue(
  14. Framework::Text key, Framework::JSON::JSONValue* value)
  15. {
  16. data->removeValue(key);
  17. data->addValue(key, value);
  18. }
  19. Framework::JSON::JSONValue* QuestRequirementStorage::getValue(
  20. Framework::Text key) const
  21. {
  22. return data->getValue(key);
  23. }
  24. Framework::JSON::JSONValue* QuestRequirementStorage::zValue(
  25. Framework::Text key) const
  26. {
  27. return data->zValue(key);
  28. }
  29. bool QuestRequirementStorage::containsKey(Framework::Text key) const
  30. {
  31. return data->hasValue(key);
  32. }
  33. void QuestRequirementStorage::setFullfilled(bool fullfilled)
  34. {
  35. this->fulfilled = fullfilled;
  36. }
  37. bool QuestRequirementStorage::isFullfilled() const
  38. {
  39. return fulfilled;
  40. }
  41. const Framework::Text& QuestRequirementStorage::getRequirementId() const
  42. {
  43. return requirementId;
  44. }
  45. QuestRequirementStorageType::QuestRequirementStorageType()
  46. : TypeFactory<QuestRequirementStorage>()
  47. {}
  48. QuestRequirementStorage* QuestRequirementStorageType::fromJson(
  49. Framework::JSON::JSONValue* zJson) const
  50. {
  51. QuestRequirementStorage* result = new QuestRequirementStorage(
  52. zJson->asObject()->zValue("requirementId")->asString()->getString());
  53. result->setFullfilled(
  54. zJson->asObject()->zValue("fulfilled")->asBool()->getBool());
  55. result->data->release();
  56. result->data = zJson->asObject()->getValue("data")->asObject();
  57. return result;
  58. }
  59. Framework::JSON::JSONValue* QuestRequirementStorageType::toJson(
  60. QuestRequirementStorage* zObject) const
  61. {
  62. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  63. result->addValue("requirementId",
  64. new Framework::JSON::JSONString(zObject->getRequirementId()));
  65. result->addValue(
  66. "fulfilled", new Framework::JSON::JSONBool(zObject->isFullfilled()));
  67. Framework::Text dataString = zObject->data->toString();
  68. result->addValue("data", Framework::JSON::Parser::getValue(dataString));
  69. return result;
  70. }
  71. Framework::JSON::Validator::JSONValidator*
  72. QuestRequirementStorageType::getValidator() const
  73. {
  74. return Framework::JSON::Validator::JSONValidator::buildForObject()
  75. ->withRequiredString("requirementId")
  76. ->finishString()
  77. ->withRequiredBool("fulfilled")
  78. ->withDefault(false)
  79. ->finishBool()
  80. ->withRequiredObject("data")
  81. ->finishObject()
  82. ->finishObject();
  83. }
  84. QuestStorage::QuestStorage(Framework::Text questId)
  85. : questId(questId),
  86. finished(0),
  87. rewarded(0)
  88. {}
  89. void QuestStorage::setQuestFinished(bool finished)
  90. {
  91. this->finished = finished;
  92. }
  93. void QuestStorage::setQuestRewarded(bool rewarded)
  94. {
  95. this->rewarded = rewarded;
  96. }
  97. bool QuestStorage::isQuestFinished() const
  98. {
  99. return finished;
  100. }
  101. bool QuestStorage::isQuestRewarded() const
  102. {
  103. return rewarded;
  104. }
  105. QuestRequirementStorage* QuestStorage::zStorage(Framework::Text requirementId)
  106. {
  107. for (QuestRequirementStorage* storage : requirements)
  108. {
  109. if (storage->getRequirementId().istGleich(requirementId))
  110. {
  111. return storage;
  112. }
  113. }
  114. QuestRequirementStorage* result
  115. = new QuestRequirementStorage(requirementId);
  116. requirements.add(result);
  117. return result;
  118. }
  119. const Framework::Text& QuestStorage::getQuestId() const
  120. {
  121. return questId;
  122. }
  123. QuestStorageType::QuestStorageType()
  124. : TypeFactory<QuestStorage>()
  125. {}
  126. QuestStorage* QuestStorageType::fromJson(
  127. Framework::JSON::JSONValue* zJson) const
  128. {
  129. QuestStorage* result = new QuestStorage(
  130. zJson->asObject()->zValue("questId")->asString()->getString());
  131. result->finished
  132. = zJson->asObject()->zValue("finished")->asBool()->getBool();
  133. result->rewarded
  134. = zJson->asObject()->zValue("rewarded")->asBool()->getBool();
  135. Framework::JSON::JSONArray* rewardsArray
  136. = zJson->asObject()->zValue("requirements")->asArray();
  137. for (int i = 0; i < rewardsArray->getLength(); i++)
  138. {
  139. result->requirements.add(
  140. Game::INSTANCE->zTypeRegistry()->fromJson<QuestRequirementStorage>(
  141. rewardsArray->zValue(i)->asObject()));
  142. }
  143. return result;
  144. }
  145. Framework::JSON::JSONValue* QuestStorageType::toJson(
  146. QuestStorage* zObject) const
  147. {
  148. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  149. result->addValue(
  150. "questId", new Framework::JSON::JSONString(zObject->questId));
  151. result->addValue(
  152. "finished", new Framework::JSON::JSONBool(zObject->finished));
  153. result->addValue(
  154. "rewarded", new Framework::JSON::JSONBool(zObject->rewarded));
  155. Framework::JSON::JSONArray* rewardsArray = new Framework::JSON::JSONArray();
  156. for (QuestRequirementStorage* storage : zObject->requirements)
  157. {
  158. rewardsArray->addValue(
  159. Game::INSTANCE->zTypeRegistry()->toJson(storage));
  160. }
  161. result->addValue("requirements", rewardsArray);
  162. return result;
  163. }
  164. Framework::JSON::Validator::JSONValidator*
  165. QuestStorageType::getValidator() const
  166. {
  167. return Framework::JSON::Validator::JSONValidator::buildForObject()
  168. ->withRequiredString("questId")
  169. ->finishString()
  170. ->withRequiredBool("finished")
  171. ->withDefault(false)
  172. ->finishBool()
  173. ->withRequiredBool("rewarded")
  174. ->withDefault(false)
  175. ->finishBool()
  176. ->withRequiredArray("requirements")
  177. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  178. ->getValidator<QuestRequirementStorage>())
  179. ->finishArray()
  180. ->finishObject();
  181. }
  182. QuestParty::QuestParty()
  183. : ReferenceCounter()
  184. {}
  185. void QuestParty::addMember(int memberEntityId)
  186. {
  187. memberEntityIds.add(memberEntityId);
  188. }
  189. void QuestParty::removeMember(int memberEntityId)
  190. {
  191. memberEntityIds.removeValue(memberEntityId);
  192. }
  193. bool QuestParty::isMember(int memberEntityId) const
  194. {
  195. return memberEntityIds.hat(memberEntityId);
  196. }
  197. bool QuestParty::isEmpty() const
  198. {
  199. return memberEntityIds.getEintragAnzahl() == 0;
  200. }
  201. QuestStorage* QuestParty::zQuestStorage(Framework::Text questId)
  202. {
  203. for (QuestStorage* storage : questStorage)
  204. {
  205. if (storage->getQuestId().istGleich(questId))
  206. {
  207. return storage;
  208. }
  209. }
  210. QuestStorage* result = new QuestStorage(questId);
  211. questStorage.add(result);
  212. return result;
  213. }
  214. QuestParty* QuestParty::clone() const
  215. {
  216. QuestParty* result = new QuestParty();
  217. for (QuestStorage* storage : questStorage)
  218. {
  219. Framework::JSON::JSONValue* value
  220. = Game::INSTANCE->zTypeRegistry()->toJson(storage);
  221. result->questStorage.add(
  222. Game::INSTANCE->zTypeRegistry()->fromJson<QuestStorage>(value));
  223. value->release();
  224. }
  225. return result;
  226. }
  227. QuestPartyType::QuestPartyType()
  228. : TypeFactory<QuestParty>()
  229. {}
  230. QuestParty* QuestPartyType::fromJson(Framework::JSON::JSONValue* zJson) const
  231. {
  232. QuestParty* result = new QuestParty();
  233. Framework::JSON::JSONArray* membersArray
  234. = zJson->asObject()->zValue("members")->asArray();
  235. for (int i = 0; i < membersArray->getLength(); i++)
  236. {
  237. result->memberEntityIds.add(
  238. (int)membersArray->zValue(i)->asNumber()->getNumber());
  239. }
  240. Framework::JSON::JSONArray* storagesArray
  241. = zJson->asObject()->zValue("quests")->asArray();
  242. for (int i = 0; i < storagesArray->getLength(); i++)
  243. {
  244. result->questStorage.add(
  245. Game::INSTANCE->zTypeRegistry()->fromJson<QuestStorage>(
  246. storagesArray->zValue(i)->asObject()));
  247. }
  248. return result;
  249. }
  250. Framework::JSON::JSONValue* QuestPartyType::toJson(QuestParty* zObject) const
  251. {
  252. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  253. Framework::JSON::JSONArray* membersArray = new Framework::JSON::JSONArray();
  254. for (int memberEntityId : zObject->memberEntityIds)
  255. {
  256. membersArray->addValue(new Framework::JSON::JSONNumber(memberEntityId));
  257. }
  258. result->addValue("members", membersArray);
  259. Framework::JSON::JSONArray* storagesArray
  260. = new Framework::JSON::JSONArray();
  261. for (QuestStorage* storage : zObject->questStorage)
  262. {
  263. storagesArray->addValue(
  264. Game::INSTANCE->zTypeRegistry()->toJson(storage));
  265. }
  266. result->addValue("quests", storagesArray);
  267. return result;
  268. }
  269. Framework::JSON::Validator::JSONValidator* QuestPartyType::getValidator() const
  270. {
  271. return Framework::JSON::Validator::JSONValidator::buildForObject()
  272. ->withRequiredArray("members")
  273. ->addAcceptedNumberInArray()
  274. ->finishNumber()
  275. ->removeInvalidEntries()
  276. ->finishArray()
  277. ->withRequiredArray("quests")
  278. ->addAcceptedTypeInArray(
  279. Game::INSTANCE->zTypeRegistry()->getValidator<QuestStorage>())
  280. ->removeInvalidEntries()
  281. ->finishArray()
  282. ->finishObject();
  283. }
  284. Quest::Quest(Framework::Text questId)
  285. : ReferenceCounter(),
  286. questId(questId)
  287. {}
  288. void Quest::processEvent(QuestEvent* zEvent, QuestStorage* zStorage)
  289. {
  290. bool finished = 1;
  291. for (QuestRequirement* requirement : requirements)
  292. {
  293. QuestRequirementStorage* zRequirementStorage
  294. = zStorage->zStorage(requirement->getRequirementId());
  295. if (!zRequirementStorage->isFullfilled())
  296. {
  297. requirement->processEvent(zEvent, zRequirementStorage);
  298. finished &= zRequirementStorage->isFullfilled();
  299. }
  300. }
  301. if (finished)
  302. {
  303. zStorage->setQuestFinished(1);
  304. }
  305. }
  306. bool Quest::isVisible(QuestParty* zParty)
  307. {
  308. bool visible = 1;
  309. for (Framework::Text* requiredQuestId : requiredQuestsIds)
  310. {
  311. visible &= zParty->zQuestStorage(*requiredQuestId)->isQuestFinished();
  312. }
  313. return visible;
  314. }
  315. void Quest::giveReward(
  316. int choice, Entity* zTargetEntity, QuestStorage* zStorage)
  317. {
  318. if (!zStorage->isQuestFinished() || zStorage->isQuestRewarded())
  319. {
  320. return;
  321. }
  322. zStorage->setQuestRewarded(1);
  323. rewards.z(choice)->giveReward(zTargetEntity);
  324. }
  325. const Framework::Text& Quest::getQuestId() const
  326. {
  327. return questId;
  328. }
  329. QuestType::QuestType()
  330. : TypeFactory<Quest>()
  331. {}
  332. Quest* QuestType::fromJson(Framework::JSON::JSONValue* zJson) const
  333. {
  334. Quest* result = new Quest(
  335. zJson->asObject()->zValue("questId")->asString()->getString());
  336. result->questName
  337. = zJson->asObject()->zValue("questName")->asString()->getString();
  338. result->description
  339. = zJson->asObject()->zValue("description")->asString()->getString();
  340. Framework::JSON::JSONArray* requiredQuestIdsArray
  341. = zJson->asObject()->zValue("requiredQuestIds")->asArray();
  342. for (int i = 0; i < requiredQuestIdsArray->getLength(); i++)
  343. {
  344. result->requiredQuestsIds.add(new Framework::Text(
  345. requiredQuestIdsArray->zValue(i)->asString()->getString()));
  346. }
  347. Framework::JSON::JSONArray* requirementsArray
  348. = zJson->asObject()->zValue("requirements")->asArray();
  349. for (int i = 0; i < requirementsArray->getLength(); i++)
  350. {
  351. result->requirements.add(
  352. Game::INSTANCE->zTypeRegistry()->fromJson<QuestRequirement>(
  353. requirementsArray->zValue(i)));
  354. }
  355. Framework::JSON::JSONArray* rewardsArray
  356. = zJson->asObject()->zValue("rewards")->asArray();
  357. for (int i = 0; i < rewardsArray->getLength(); i++)
  358. {
  359. result->rewards.add(
  360. Game::INSTANCE->zTypeRegistry()->fromJson<QuestReward>(
  361. rewardsArray->zValue(i)));
  362. }
  363. return result;
  364. }
  365. Framework::JSON::JSONValue* QuestType::toJson(Quest* zObject) const
  366. {
  367. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  368. result->addValue(
  369. "questId", new Framework::JSON::JSONString(zObject->questId));
  370. result->addValue(
  371. "questName", new Framework::JSON::JSONString(zObject->questName));
  372. result->addValue(
  373. "description", new Framework::JSON::JSONString(zObject->description));
  374. Framework::JSON::JSONArray* requiredQuestIdsArray
  375. = new Framework::JSON::JSONArray();
  376. for (Framework::Text* requiredQuestId : zObject->requiredQuestsIds)
  377. {
  378. requiredQuestIdsArray->addValue(
  379. new Framework::JSON::JSONString(*requiredQuestId));
  380. }
  381. result->addValue("requiredQuestIds", requiredQuestIdsArray);
  382. Framework::JSON::JSONArray* requirementsArray
  383. = new Framework::JSON::JSONArray();
  384. for (QuestRequirement* requirement : zObject->requirements)
  385. {
  386. requirementsArray->addValue(
  387. Game::INSTANCE->zTypeRegistry()->toJson(requirement));
  388. }
  389. result->addValue("requirements", requirementsArray);
  390. Framework::JSON::JSONArray* rewardsArray = new Framework::JSON::JSONArray();
  391. for (QuestReward* reward : zObject->rewards)
  392. {
  393. rewardsArray->addValue(Game::INSTANCE->zTypeRegistry()->toJson(reward));
  394. }
  395. result->addValue("rewards", rewardsArray);
  396. return result;
  397. }
  398. Framework::JSON::Validator::JSONValidator* QuestType::getValidator() const
  399. {
  400. return Framework::JSON::Validator::JSONValidator::buildForObject()
  401. ->withRequiredString("questId")
  402. ->finishString()
  403. ->withRequiredString("questName")
  404. ->finishString()
  405. ->withRequiredString("description")
  406. ->finishString()
  407. ->withRequiredArray("requiredQuestIds")
  408. ->withDefault(new Framework::JSON::JSONArray())
  409. ->addAcceptedStringInArray()
  410. ->finishString()
  411. ->finishArray()
  412. ->withRequiredArray("requirements")
  413. ->addAcceptedTypeInArray(
  414. Game::INSTANCE->zTypeRegistry()->getValidator<QuestRequirement>())
  415. ->finishArray()
  416. ->withRequiredArray("rewards")
  417. ->addAcceptedTypeInArray(
  418. Game::INSTANCE->zTypeRegistry()->getValidator<QuestReward>())
  419. ->finishArray()
  420. ->finishObject();
  421. }
  422. QuestCollection::QuestCollection(Framework::Text name)
  423. : ReferenceCounter(),
  424. name(name)
  425. {}
  426. void QuestCollection::processEvent(QuestEvent* zEvent, QuestParty* zParty)
  427. {
  428. for (Quest* quest : quests)
  429. {
  430. QuestStorage* zStorage = zParty->zQuestStorage(quest->getQuestId());
  431. if (quest->isVisible(zParty) && !zStorage->isQuestFinished())
  432. {
  433. quest->processEvent(zEvent, zStorage);
  434. }
  435. }
  436. }
  437. bool QuestCollection::isVisible(QuestParty* zParty)
  438. {
  439. bool visible = 1;
  440. for (Quest* quest : quests)
  441. {
  442. visible &= quest->isVisible(zParty);
  443. }
  444. return visible;
  445. }
  446. void QuestCollection::giveReward(Framework::Text questId,
  447. int choice,
  448. Entity* zTargetEntity,
  449. QuestParty* zParty)
  450. {
  451. QuestStorage* zStorage = zParty->zQuestStorage(questId);
  452. if (zStorage->isQuestRewarded() || !zStorage->isQuestFinished())
  453. {
  454. return;
  455. }
  456. for (Quest* quest : quests)
  457. {
  458. if (quest->getQuestId() == questId)
  459. {
  460. quest->giveReward(choice, zTargetEntity, zStorage);
  461. return;
  462. }
  463. }
  464. }
  465. void QuestCollection::addQuest(Quest* zQuest)
  466. {
  467. quests.add(zQuest);
  468. }
  469. QuestCollectionType::QuestCollectionType()
  470. : TypeFactory<QuestCollection>()
  471. {}
  472. QuestCollection* QuestCollectionType::fromJson(
  473. Framework::JSON::JSONValue* zJson) const
  474. {
  475. QuestCollection* result = new QuestCollection(
  476. zJson->asObject()->zValue("name")->asString()->getString());
  477. Framework::JSON::JSONArray* questsArray
  478. = zJson->asObject()->zValue("quests")->asArray();
  479. for (int i = 0; i < questsArray->getLength(); i++)
  480. {
  481. result->quests.add(Game::INSTANCE->zTypeRegistry()->fromJson<Quest>(
  482. questsArray->zValue(i)->asObject()));
  483. }
  484. return result;
  485. }
  486. Framework::JSON::JSONValue* QuestCollectionType::toJson(
  487. QuestCollection* zObject) const
  488. {
  489. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  490. result->addValue("name", new Framework::JSON::JSONString(zObject->name));
  491. Framework::JSON::JSONArray* questsArray = new Framework::JSON::JSONArray();
  492. for (Quest* quest : zObject->quests)
  493. {
  494. questsArray->addValue(Game::INSTANCE->zTypeRegistry()->toJson(quest));
  495. }
  496. result->addValue("quests", questsArray);
  497. return result;
  498. }
  499. Framework::JSON::Validator::JSONValidator*
  500. QuestCollectionType::getValidator() const
  501. {
  502. return Framework::JSON::Validator::JSONValidator::buildForObject()
  503. ->withRequiredString("name")
  504. ->finishString()
  505. ->withRequiredArray("quests")
  506. ->addAcceptedTypeInArray(
  507. Game::INSTANCE->zTypeRegistry()->getValidator<Quest>())
  508. ->removeInvalidEntries()
  509. ->finishArray()
  510. ->finishObject();
  511. }
  512. QuestManager::QuestManager()
  513. : ReferenceCounter()
  514. {}
  515. void QuestManager::loadQuests()
  516. {
  517. Framework::Text worldDir = Game::INSTANCE->getWorldDirectory();
  518. Framework::Text questDir = worldDir + "/quests";
  519. if (!Framework::DateiExistiert(questDir))
  520. {
  521. std::cout << "no quest data found in world save, using default data\n";
  522. questDir = "data/quests";
  523. }
  524. Framework::JSON::JSONValue* value
  525. = Framework::JSON::loadJSONFromFile(questDir + "/quests.json");
  526. if (!value || value->getType() == Framework::JSON::JSONType::NULL_)
  527. {
  528. std::cout << "No valid quests.json found in " << questDir << "\n";
  529. if (value)
  530. {
  531. value->release();
  532. }
  533. }
  534. else
  535. {
  536. std::cout << "loading quests ...\n";
  537. Framework::JSON::Validator::JSONValidator* validator
  538. = Framework::JSON::Validator::JSONValidator::buildForArray()
  539. ->addAcceptedTypeInArray(
  540. Game::INSTANCE->zTypeRegistry()
  541. ->getValidator<QuestCollection>())
  542. ->removeInvalidEntries()
  543. ->finishArray();
  544. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  545. validationResult;
  546. Framework::JSON::JSONArray* valid
  547. = validator->getValidParts(value, &validationResult)->asArray();
  548. for (Framework::JSON::Validator::JSONValidationResult* invalidPart :
  549. validationResult)
  550. {
  551. invalidPart->printInvalidInfo();
  552. }
  553. value->release();
  554. for (Framework::JSON::JSONValue* validPart : *valid)
  555. {
  556. questCollections.add(
  557. Game::INSTANCE->zTypeRegistry()->fromJson<QuestCollection>(
  558. validPart->asObject()));
  559. }
  560. valid->release();
  561. validator->release();
  562. }
  563. value = Framework::JSON::loadJSONFromFile(questDir + "/parties.json");
  564. if (!value || value->getType() == Framework::JSON::JSONType::NULL_)
  565. {
  566. if (value)
  567. {
  568. value->release();
  569. }
  570. }
  571. else
  572. {
  573. std::cout << "loading quest parties ...\n";
  574. Framework::JSON::Validator::JSONValidator* validator
  575. = Framework::JSON::Validator::JSONValidator::buildForArray()
  576. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  577. ->getValidator<QuestParty>())
  578. ->removeInvalidEntries()
  579. ->finishArray();
  580. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  581. validationResult;
  582. Framework::JSON::JSONArray* valid
  583. = validator->getValidParts(value, &validationResult)->asArray();
  584. for (Framework::JSON::Validator::JSONValidationResult* invalidPart :
  585. validationResult)
  586. {
  587. invalidPart->printInvalidInfo();
  588. }
  589. value->release();
  590. for (Framework::JSON::JSONValue* validPart : *valid)
  591. {
  592. parties.add(Game::INSTANCE->zTypeRegistry()->fromJson<QuestParty>(
  593. validPart->asObject()));
  594. }
  595. valid->release();
  596. validator->release();
  597. }
  598. std::cout << "Loaded " << questCollections.getEintragAnzahl()
  599. << " Quest Collections and " << parties.getEintragAnzahl()
  600. << " Quest Parties\n";
  601. }
  602. void QuestManager::saveQuests()
  603. {
  604. Framework::Text worldDir = Game::INSTANCE->getWorldDirectory();
  605. Framework::Text questDir = worldDir + "/quests";
  606. if (!Framework::DateiExistiert(questDir))
  607. {
  608. Framework::DateiPfadErstellen(questDir + "/");
  609. }
  610. Framework::JSON::JSONArray* questCollectionsArray
  611. = new Framework::JSON::JSONArray();
  612. for (QuestCollection* questCollection : questCollections)
  613. {
  614. questCollectionsArray->addValue(
  615. Game::INSTANCE->zTypeRegistry()->toJson(questCollection));
  616. }
  617. Datei datei(questDir + "/quests.json");
  618. if (!datei.existiert()) datei.erstellen();
  619. datei.open(Framework::Datei::Style::schreiben);
  620. Framework::Text jsonStr = questCollectionsArray->toString();
  621. datei.schreibe(jsonStr.getText(), jsonStr.getLength());
  622. datei.close();
  623. questCollectionsArray->release();
  624. Framework::JSON::JSONArray* partiesArray = new Framework::JSON::JSONArray();
  625. for (QuestParty* party : parties)
  626. {
  627. partiesArray->addValue(Game::INSTANCE->zTypeRegistry()->toJson(party));
  628. }
  629. datei.setDatei(questDir + "/parties.json");
  630. if (!datei.existiert()) datei.erstellen();
  631. datei.open(Framework::Datei::Style::schreiben);
  632. jsonStr = partiesArray->toString();
  633. datei.schreibe(jsonStr.getText(), jsonStr.getLength());
  634. datei.close();
  635. partiesArray->release();
  636. }
  637. void QuestManager::processEvent(QuestEvent* event)
  638. {
  639. Entity* zEntity = event->zActingEntity();
  640. for (QuestParty* party : parties)
  641. {
  642. if (party->isMember(zEntity->getId()))
  643. {
  644. for (QuestCollection* questCollection : questCollections)
  645. {
  646. questCollection->processEvent(event, party);
  647. }
  648. break;
  649. }
  650. }
  651. event->release();
  652. }
  653. void QuestManager::giveReward(
  654. Framework::Text questId, int choice, Entity* zTargetEntity)
  655. {
  656. for (QuestParty* party : parties)
  657. {
  658. if (party->isMember(zTargetEntity->getId()))
  659. {
  660. for (QuestCollection* questCollection : questCollections)
  661. {
  662. questCollection->giveReward(
  663. questId, choice, zTargetEntity, party);
  664. }
  665. return;
  666. }
  667. }
  668. }