Quest.cpp 25 KB

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