Quest.cpp 25 KB

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