Quest.cpp 27 KB

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