Recipie.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. #include "Recipie.h"
  2. #include "CraftingStorage.h"
  3. #include "Game.h"
  4. #include "Item.h"
  5. RecipieInput::RecipieInput()
  6. : ReferenceCounter(),
  7. filter(0),
  8. modifier(0),
  9. amount(0)
  10. {}
  11. RecipieInput::~RecipieInput()
  12. {
  13. if (filter) filter->release();
  14. if (modifier) modifier->release();
  15. }
  16. void RecipieInput::setFilter(ItemFilter* filter)
  17. {
  18. if (filter) filter->release();
  19. filter = filter;
  20. }
  21. ItemFilter* RecipieInput::zFilter() const
  22. {
  23. return filter;
  24. }
  25. void RecipieInput::setModifier(ItemModifier* modifier)
  26. {
  27. if (modifier) modifier->release();
  28. modifier = modifier;
  29. }
  30. ItemModifier* RecipieInput::zModifier() const
  31. {
  32. return modifier;
  33. }
  34. void RecipieInput::setAmount(int amount)
  35. {
  36. this->amount = amount;
  37. }
  38. int RecipieInput::getAmount() const
  39. {
  40. return amount;
  41. }
  42. RecipieInputFactory::RecipieInputFactory()
  43. : TypeFactory()
  44. {}
  45. RecipieInput* RecipieInputFactory::createValue(
  46. Framework::JSON::JSONObject* zJson) const
  47. {
  48. return new RecipieInput();
  49. }
  50. void RecipieInputFactory::fromJson(
  51. RecipieInput* zResult, Framework::JSON::JSONObject* zJson) const
  52. {
  53. zResult->setFilter(Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(
  54. zJson->asObject()->zValue("filter")));
  55. zResult->setModifier(
  56. Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(
  57. zJson->asObject()->zValue("modifier")));
  58. zResult->setAmount(
  59. (int)zJson->asObject()->zValue("amount")->asNumber()->getNumber());
  60. }
  61. void RecipieInputFactory::toJson(
  62. RecipieInput* zObject, Framework::JSON::JSONObject* zResult) const
  63. {
  64. zResult->addValue(
  65. "filter", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zFilter()));
  66. zResult->addValue("modifier",
  67. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModifier()));
  68. zResult->addValue("amount",
  69. new Framework::JSON::JSONNumber((double)zObject->getAmount()));
  70. }
  71. JSONObjectValidationBuilder* RecipieInputFactory::addToValidator(
  72. JSONObjectValidationBuilder* builder) const
  73. {
  74. Framework::JSON::JSONObject* defaultModifier
  75. = new Framework::JSON::JSONObject();
  76. defaultModifier->addValue(
  77. "type", new Framework::JSON::JSONString("consume"));
  78. return builder
  79. ->withRequiredAttribute("filter",
  80. Game::INSTANCE->zTypeRegistry()->getValidator<ItemFilter>())
  81. ->withRequiredAttribute("modifier",
  82. Game::INSTANCE->zTypeRegistry()->getValidator<ItemModifier>())
  83. ->withRequiredObject("modifier")
  84. ->withRequiredString("type")
  85. ->withExactMatch("consume")
  86. ->finishString()
  87. ->withDefault(defaultModifier)
  88. ->finishObject()
  89. ->withRequiredNumber("amount")
  90. ->whichIsGreaterOrEqual(1.0)
  91. ->withDefault(1.0)
  92. ->finishNumber();
  93. }
  94. RecipieOutput::RecipieOutput()
  95. : ReferenceCounter(),
  96. itemTypeId(0),
  97. amount(0),
  98. modifier(0)
  99. {}
  100. RecipieOutput::~RecipieOutput()
  101. {
  102. modifier->release();
  103. }
  104. void RecipieOutput::setItemTypeId(int itemTypeId)
  105. {
  106. this->itemTypeId = itemTypeId;
  107. }
  108. int RecipieOutput::getItemTypeId() const
  109. {
  110. return itemTypeId;
  111. }
  112. void RecipieOutput::setAmount(int amount)
  113. {
  114. this->amount = amount;
  115. }
  116. int RecipieOutput::getAmount() const
  117. {
  118. return amount;
  119. }
  120. void RecipieOutput::setModifier(ItemModifier* modifier)
  121. {
  122. if (this->modifier) this->modifier->release();
  123. this->modifier = modifier;
  124. }
  125. ItemModifier* RecipieOutput::zModifier() const
  126. {
  127. return modifier;
  128. }
  129. Item* RecipieOutput::createItem() const
  130. {
  131. Item* result = Game::INSTANCE->zItemType(itemTypeId)->createItem();
  132. if (result)
  133. {
  134. modifier->applyOn(result);
  135. }
  136. return result;
  137. }
  138. RecipieOutputFactory::RecipieOutputFactory()
  139. : TypeFactory()
  140. {}
  141. RecipieOutput* RecipieOutputFactory::createValue(
  142. Framework::JSON::JSONObject* zJson) const
  143. {
  144. return new RecipieOutput();
  145. }
  146. void RecipieOutputFactory::fromJson(
  147. RecipieOutput* zResult, Framework::JSON::JSONObject* zJson) const
  148. {
  149. zResult->setItemTypeId(Game::INSTANCE->getItemTypeId(
  150. zJson->asObject()->zValue("itemType")->asString()->getString()));
  151. zResult->setAmount(
  152. (int)zJson->asObject()->zValue("amount")->asNumber()->getNumber());
  153. zResult->setModifier(
  154. Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(
  155. zJson->asObject()->zValue("modifier")));
  156. }
  157. void RecipieOutputFactory::toJson(
  158. RecipieOutput* zObject, Framework::JSON::JSONObject* zResult) const
  159. {
  160. zResult->addValue("itemType",
  161. new Framework::JSON::JSONString(
  162. Game::INSTANCE->zItemType(zObject->getItemTypeId())->getName()));
  163. zResult->addValue("amount",
  164. new Framework::JSON::JSONNumber((double)zObject->getAmount()));
  165. zResult->addValue("modifier",
  166. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModifier()));
  167. }
  168. JSONObjectValidationBuilder* RecipieOutputFactory::addToValidator(
  169. JSONObjectValidationBuilder* builder) const
  170. {
  171. Framework::JSON::JSONObject* defaultModifier
  172. = new Framework::JSON::JSONObject();
  173. defaultModifier->addValue(
  174. "type", new Framework::JSON::JSONString("doNothing"));
  175. Framework::RCArray<Framework::Text> itemTypes;
  176. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  177. {
  178. if (Game::INSTANCE->zItemType(i))
  179. {
  180. itemTypes.add(
  181. new Framework::Text(Game::INSTANCE->zItemType(i)->getName()));
  182. }
  183. }
  184. return builder->withRequiredString("itemType")
  185. ->whichIsOneOf(itemTypes)
  186. ->finishString()
  187. ->withRequiredAttribute("modifier",
  188. Game::INSTANCE->zTypeRegistry()->getValidator<ItemModifier>())
  189. ->withRequiredObject("modifier")
  190. ->withRequiredString("type")
  191. ->withExactMatch("doNothing")
  192. ->finishString()
  193. ->withDefault(defaultModifier)
  194. ->finishObject()
  195. ->withRequiredNumber("amount")
  196. ->whichIsGreaterOrEqual(1.0)
  197. ->withDefault(1.0)
  198. ->finishNumber();
  199. }
  200. Recipie::Recipie()
  201. : ReferenceCounter(),
  202. groupName(groupName),
  203. outputs(outputs)
  204. {}
  205. void Recipie::addOutput(RecipieOutput* outputs)
  206. {
  207. this->outputs.add(outputs);
  208. }
  209. Framework::Array<ItemInfo> Recipie::getOutput() const
  210. {
  211. Framework::Array<ItemInfo> result;
  212. for (const RecipieOutput* output : outputs)
  213. {
  214. Item* item = output->createItem();
  215. if (item)
  216. {
  217. result.add({item->zItemType()->getId(),
  218. output->getAmount(),
  219. item->getHp(),
  220. item->getMaxHp(),
  221. item->getDurability(),
  222. item->getMaxDurability()});
  223. item->release();
  224. }
  225. }
  226. return result;
  227. }
  228. bool Recipie::createsOutput(int itemTypeId)
  229. {
  230. for (RecipieOutput* output : outputs)
  231. {
  232. if (output->getItemTypeId() == itemTypeId)
  233. {
  234. return 1;
  235. }
  236. }
  237. return 0;
  238. }
  239. void Recipie::setGroupName(Framework::Text groupName)
  240. {
  241. this->groupName = groupName;
  242. }
  243. const Framework::RCArray<RecipieOutput>& Recipie::getOutputs() const
  244. {
  245. return outputs;
  246. }
  247. Framework::Text Recipie::getGroupName() const
  248. {
  249. return groupName;
  250. }
  251. UnshapedRecipie::UnshapedRecipie()
  252. : Recipie()
  253. {}
  254. bool UnshapedRecipie::testApplicability(CraftingStorage* zStorage)
  255. {
  256. for (RecipieOutput* output : outputs)
  257. {
  258. Item* item = output->createItem();
  259. if (item)
  260. {
  261. if (!zStorage->hasFreeSpace(item, output->getAmount()))
  262. {
  263. item->release();
  264. return 0;
  265. }
  266. item->release();
  267. }
  268. }
  269. return zStorage->isAllAvailable(inputs);
  270. }
  271. void UnshapedRecipie::apply(CraftingStorage* zStorage)
  272. {
  273. zStorage->consume(inputs);
  274. for (RecipieOutput* output : outputs)
  275. {
  276. Item* item = output->createItem();
  277. if (item)
  278. {
  279. ItemStack* stack = new ItemStack(item, output->getAmount());
  280. zStorage->addCraftingResult(stack);
  281. stack->release();
  282. }
  283. }
  284. }
  285. Framework::Text UnshapedRecipie::getRecipieUIML()
  286. {
  287. Framework::Text result = "<recipie type=\"unshaped\"><ingredients>";
  288. for (RecipieInput* input : inputs)
  289. {
  290. result.append() << "<ingredient amount=\"" << input->getAmount()
  291. << "\">";
  292. if (input->zFilter())
  293. {
  294. result.append()
  295. << "<logic>" << input->zFilter()->getLogicUIML() << "</logic>";
  296. }
  297. result += "</ingredient>";
  298. // TODO: add modifiers
  299. }
  300. result += "</ingredients><outputs>";
  301. for (RecipieOutput* output : outputs)
  302. {
  303. Item* item = output->createItem();
  304. if (item)
  305. {
  306. result.append()
  307. << "<output amount=\"" << output->getAmount()
  308. << "\" itemType=\"" << item->zItemType()->getId() << "\" hp=\""
  309. << item->getHp() << "\" durability=\"" << item->getDurability()
  310. << "\" maxHp=\"" << item->getMaxHp() << "\" maxDurability=\""
  311. << item->getMaxDurability() << "\">" << item->getTooltipUIML()
  312. << "</output>";
  313. item->release();
  314. }
  315. }
  316. result += "</outputs></recipie>";
  317. return result;
  318. }
  319. void UnshapedRecipie::addInput(RecipieInput* input)
  320. {
  321. inputs.add(input);
  322. }
  323. const Framework::RCArray<RecipieInput>& UnshapedRecipie::getInputs() const
  324. {
  325. return inputs;
  326. }
  327. UnshapedRecipieFactory::UnshapedRecipieFactory()
  328. : RecipieFactory()
  329. {}
  330. UnshapedRecipie* UnshapedRecipieFactory::createValue(
  331. Framework::JSON::JSONObject* zJson) const
  332. {
  333. return new UnshapedRecipie();
  334. }
  335. void UnshapedRecipieFactory::fromJson(
  336. UnshapedRecipie* zResult, Framework::JSON::JSONObject* zJson) const
  337. {
  338. for (Framework::JSON::JSONValue* input :
  339. *zJson->zValue("inputs")->asArray())
  340. {
  341. zResult->addInput(
  342. Game::INSTANCE->zTypeRegistry()->fromJson<RecipieInput>(input));
  343. }
  344. RecipieFactory::fromJson(zResult, zJson);
  345. }
  346. void UnshapedRecipieFactory::toJson(
  347. UnshapedRecipie* zObject, Framework::JSON::JSONObject* zResult) const
  348. {
  349. Framework::JSON::JSONArray* inputs = new Framework::JSON::JSONArray();
  350. for (RecipieInput* input : zObject->getInputs())
  351. {
  352. inputs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(input));
  353. }
  354. zResult->addValue("inputs", inputs);
  355. RecipieFactory::toJson(zObject, zResult);
  356. }
  357. JSONObjectValidationBuilder* UnshapedRecipieFactory::addToValidator(
  358. JSONObjectValidationBuilder* builder) const
  359. {
  360. return RecipieFactory::addToValidator(
  361. builder->withRequiredArray("inputs")
  362. ->addAcceptedTypeInArray(
  363. Game::INSTANCE->zTypeRegistry()->getValidator<RecipieInput>())
  364. ->finishArray());
  365. }
  366. Framework::Text UnshapedRecipieFactory::getTypeToken() const
  367. {
  368. return "unshaped";
  369. }
  370. ShapedRecipie::ShapedRecipie()
  371. : Recipie(),
  372. width(0),
  373. height(0)
  374. {}
  375. bool ShapedRecipie::testApplicability(CraftingStorage* zStorage)
  376. {
  377. ShapedCraftingStorage* zShapedStorage
  378. = dynamic_cast<ShapedCraftingStorage*>(zStorage);
  379. if (!zShapedStorage) return 0;
  380. for (RecipieOutput* output : outputs)
  381. {
  382. Item* item = output->createItem();
  383. if (item)
  384. {
  385. if (!zShapedStorage->hasFreeSpace(item, output->getAmount()))
  386. {
  387. item->release();
  388. return 0;
  389. }
  390. item->release();
  391. }
  392. }
  393. return zShapedStorage->isAllAvailable(inputs, width, height);
  394. }
  395. void ShapedRecipie::apply(CraftingStorage* zStorage)
  396. {
  397. ShapedCraftingStorage* zShapedStorage
  398. = dynamic_cast<ShapedCraftingStorage*>(zStorage);
  399. zShapedStorage->consume(inputs, width, height);
  400. for (RecipieOutput* output : outputs)
  401. {
  402. Item* item = output->createItem();
  403. if (item)
  404. {
  405. ItemStack* stack = new ItemStack(item, output->getAmount());
  406. zStorage->addCraftingResult(stack);
  407. stack->release();
  408. }
  409. }
  410. }
  411. Framework::Text ShapedRecipie::getRecipieUIML()
  412. {
  413. Framework::Text result = "<recipie type=\"shaped\" width=\"";
  414. result.append() << width << "\" height=\"" << height << "\"><ingredients>";
  415. for (RecipieInput* input : inputs)
  416. {
  417. result.append() << "<ingredient amount=\"" << input->getAmount()
  418. << "\">";
  419. if (input->zFilter())
  420. {
  421. result.append()
  422. << "<logic>" << input->zFilter()->getLogicUIML() << "</logic>";
  423. }
  424. result += "</ingredient>";
  425. // TODO: add modifiers
  426. }
  427. result += "</ingredients><outputs>";
  428. for (RecipieOutput* output : outputs)
  429. {
  430. Item* item = output->createItem();
  431. if (item)
  432. {
  433. result.append()
  434. << "<output amount=\"" << output->getAmount()
  435. << "\" itemType=\"" << item->zItemType()->getId() << "\" hp=\""
  436. << item->getHp() << "\" durability=\"" << item->getDurability()
  437. << "\" maxHp=\"" << item->getMaxHp() << "\" maxDurability=\""
  438. << item->getMaxDurability() << "\">" << item->getTooltipUIML()
  439. << "</output>";
  440. item->release();
  441. }
  442. }
  443. result += "</outputs></recipie>";
  444. return result;
  445. }
  446. void ShapedRecipie::setWidth(int width)
  447. {
  448. this->width = width;
  449. }
  450. int ShapedRecipie::getWidth() const
  451. {
  452. return width;
  453. }
  454. void ShapedRecipie::setHeight(int height)
  455. {
  456. this->height = height;
  457. }
  458. int ShapedRecipie::getHeight() const
  459. {
  460. return height;
  461. }
  462. void ShapedRecipie::addInput(RecipieInput* input)
  463. {
  464. inputs.add(input);
  465. }
  466. void ShapedRecipie::setInput(int index, RecipieInput* input)
  467. {
  468. inputs.set(input, index);
  469. }
  470. const Framework::RCArray<RecipieInput>& ShapedRecipie::getInputs() const
  471. {
  472. return inputs;
  473. }
  474. ShapedRecipieFactory::ShapedRecipieFactory()
  475. : RecipieFactory()
  476. {}
  477. ShapedRecipie* ShapedRecipieFactory::createValue(
  478. Framework::JSON::JSONObject* zJson) const
  479. {
  480. return new ShapedRecipie();
  481. }
  482. void ShapedRecipieFactory::fromJson(
  483. ShapedRecipie* zResult, Framework::JSON::JSONObject* zJson) const
  484. {
  485. int width = (int)zJson->zValue("width")->asNumber()->getNumber();
  486. int height = (int)zJson->zValue("height")->asNumber()->getNumber();
  487. for (int i = 0; i < width * height; i++)
  488. {
  489. zResult->addInput(new RecipieInput());
  490. }
  491. for (Framework::JSON::JSONValue* input :
  492. *zJson->zValue("inputs")->asArray())
  493. {
  494. int x = (int)input->asObject()->zValue("x")->asNumber()->getNumber();
  495. int y = (int)input->asObject()->zValue("y")->asNumber()->getNumber();
  496. if (x >= width || y >= height)
  497. {
  498. std::cout << "Invalid input position in shaped recipie with width="
  499. << width << ", height=" << height << "\n"
  500. << (x >= width ? x : y) << "\n";
  501. return;
  502. }
  503. zResult->setInput(y * width + x,
  504. Game::INSTANCE->zTypeRegistry()->fromJson<RecipieInput>(
  505. input->asObject()->zValue("input")));
  506. }
  507. zResult->setWidth((int)zJson->zValue("width")->asNumber()->getNumber());
  508. zResult->setHeight((int)zJson->zValue("height")->asNumber()->getNumber());
  509. RecipieFactory::fromJson(zResult, zJson);
  510. }
  511. void ShapedRecipieFactory::toJson(
  512. ShapedRecipie* zObject, Framework::JSON::JSONObject* zResult) const
  513. {
  514. zResult->addValue(
  515. "width", new Framework::JSON::JSONNumber(zObject->getWidth()));
  516. zResult->addValue(
  517. "height", new Framework::JSON::JSONNumber(zObject->getHeight()));
  518. Framework::JSON::JSONArray* inputs = new Framework::JSON::JSONArray();
  519. for (int i = 0; i < zObject->getWidth() * zObject->getHeight(); i++)
  520. {
  521. Framework::JSON::JSONObject* input = new Framework::JSON::JSONObject();
  522. input->addValue(
  523. "x", new Framework::JSON::JSONNumber(i % zObject->getHeight()));
  524. input->addValue(
  525. "y", new Framework::JSON::JSONNumber(i / zObject->getHeight()));
  526. input->addValue("input",
  527. Game::INSTANCE->zTypeRegistry()->toJson(zObject->getInputs().z(i)));
  528. inputs->addValue(input);
  529. }
  530. zResult->addValue("inputs", inputs);
  531. RecipieFactory::toJson(zObject, zResult);
  532. }
  533. JSONObjectValidationBuilder* ShapedRecipieFactory::addToValidator(
  534. JSONObjectValidationBuilder* builder) const
  535. {
  536. return RecipieFactory::addToValidator(
  537. builder->withRequiredArray("inputs")
  538. ->addAcceptedObjectInArray()
  539. ->withRequiredNumber("x")
  540. ->whichIsGreaterOrEqual(0.0)
  541. ->finishNumber()
  542. ->withRequiredNumber("y")
  543. ->whichIsGreaterOrEqual(0.0)
  544. ->finishNumber()
  545. ->withRequiredAttribute("input",
  546. Game::INSTANCE->zTypeRegistry()->getValidator<RecipieInput>())
  547. ->finishObject()
  548. ->finishArray()
  549. ->withRequiredNumber("width")
  550. ->whichIsGreaterOrEqual(1.0)
  551. ->finishNumber()
  552. ->withRequiredNumber("height")
  553. ->whichIsGreaterOrEqual(1.0)
  554. ->finishNumber());
  555. }
  556. Framework::Text ShapedRecipieFactory::getTypeToken() const
  557. {
  558. return "shaped";
  559. }