Quest.cpp 24 KB

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