Recipie.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. {}
  203. void Recipie::addOutput(RecipieOutput* outputs)
  204. {
  205. this->outputs.add(outputs);
  206. }
  207. Framework::Array<ItemInfo> Recipie::getOutput() const
  208. {
  209. Framework::Array<ItemInfo> result;
  210. for (const RecipieOutput* output : outputs)
  211. {
  212. Item* item = output->createItem();
  213. if (item)
  214. {
  215. result.add({item->zItemType()->getId(),
  216. output->getAmount(),
  217. item->getHp(),
  218. item->getMaxHp(),
  219. item->getDurability(),
  220. item->getMaxDurability()});
  221. item->release();
  222. }
  223. }
  224. return result;
  225. }
  226. bool Recipie::createsOutput(int itemTypeId)
  227. {
  228. for (RecipieOutput* output : outputs)
  229. {
  230. if (output->getItemTypeId() == itemTypeId)
  231. {
  232. return 1;
  233. }
  234. }
  235. return 0;
  236. }
  237. void Recipie::setGroupName(Framework::Text groupName)
  238. {
  239. this->groupName = groupName;
  240. }
  241. const Framework::RCArray<RecipieOutput>& Recipie::getOutputs() const
  242. {
  243. return outputs;
  244. }
  245. Framework::Text Recipie::getGroupName() const
  246. {
  247. return groupName;
  248. }
  249. UnshapedRecipie::UnshapedRecipie()
  250. : Recipie()
  251. {}
  252. bool UnshapedRecipie::testApplicability(CraftingStorage* zStorage)
  253. {
  254. for (RecipieOutput* output : outputs)
  255. {
  256. Item* item = output->createItem();
  257. if (item)
  258. {
  259. if (!zStorage->hasFreeSpace(item, output->getAmount()))
  260. {
  261. item->release();
  262. return 0;
  263. }
  264. item->release();
  265. }
  266. }
  267. return zStorage->isAllAvailable(inputs);
  268. }
  269. void UnshapedRecipie::apply(CraftingStorage* zStorage)
  270. {
  271. zStorage->consume(inputs);
  272. for (RecipieOutput* output : outputs)
  273. {
  274. Item* item = output->createItem();
  275. if (item)
  276. {
  277. ItemStack* stack = new ItemStack(item, output->getAmount());
  278. zStorage->addCraftingResult(stack);
  279. stack->release();
  280. }
  281. }
  282. }
  283. Framework::Text UnshapedRecipie::getRecipieUIML()
  284. {
  285. Framework::Text result = "<recipie type=\"unshaped\"><ingredients>";
  286. for (RecipieInput* input : inputs)
  287. {
  288. result.append() << "<ingredient amount=\"" << input->getAmount()
  289. << "\">";
  290. if (input->zFilter())
  291. {
  292. result.append()
  293. << "<logic>" << input->zFilter()->getLogicUIML() << "</logic>";
  294. }
  295. result += "</ingredient>";
  296. // TODO: add modifiers
  297. }
  298. result += "</ingredients><outputs>";
  299. for (RecipieOutput* output : outputs)
  300. {
  301. Item* item = output->createItem();
  302. if (item)
  303. {
  304. result.append()
  305. << "<output amount=\"" << output->getAmount()
  306. << "\" itemType=\"" << item->zItemType()->getId() << "\" hp=\""
  307. << item->getHp() << "\" durability=\"" << item->getDurability()
  308. << "\" maxHp=\"" << item->getMaxHp() << "\" maxDurability=\""
  309. << item->getMaxDurability() << "\">" << item->getTooltipUIML()
  310. << "</output>";
  311. item->release();
  312. }
  313. }
  314. result += "</outputs></recipie>";
  315. return result;
  316. }
  317. void UnshapedRecipie::addInput(RecipieInput* input)
  318. {
  319. inputs.add(input);
  320. }
  321. const Framework::RCArray<RecipieInput>& UnshapedRecipie::getInputs() const
  322. {
  323. return inputs;
  324. }
  325. UnshapedRecipieFactory::UnshapedRecipieFactory()
  326. : RecipieFactory()
  327. {}
  328. UnshapedRecipie* UnshapedRecipieFactory::createValue(
  329. Framework::JSON::JSONObject* zJson) const
  330. {
  331. return new UnshapedRecipie();
  332. }
  333. void UnshapedRecipieFactory::fromJson(
  334. UnshapedRecipie* zResult, Framework::JSON::JSONObject* zJson) const
  335. {
  336. for (Framework::JSON::JSONValue* input :
  337. *zJson->zValue("inputs")->asArray())
  338. {
  339. zResult->addInput(
  340. Game::INSTANCE->zTypeRegistry()->fromJson<RecipieInput>(input));
  341. }
  342. RecipieFactory::fromJson(zResult, zJson);
  343. }
  344. void UnshapedRecipieFactory::toJson(
  345. UnshapedRecipie* zObject, Framework::JSON::JSONObject* zResult) const
  346. {
  347. Framework::JSON::JSONArray* inputs = new Framework::JSON::JSONArray();
  348. for (RecipieInput* input : zObject->getInputs())
  349. {
  350. inputs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(input));
  351. }
  352. zResult->addValue("inputs", inputs);
  353. RecipieFactory::toJson(zObject, zResult);
  354. }
  355. JSONObjectValidationBuilder* UnshapedRecipieFactory::addToValidator(
  356. JSONObjectValidationBuilder* builder) const
  357. {
  358. return RecipieFactory::addToValidator(
  359. builder->withRequiredArray("inputs")
  360. ->addAcceptedTypeInArray(
  361. Game::INSTANCE->zTypeRegistry()->getValidator<RecipieInput>())
  362. ->finishArray());
  363. }
  364. Framework::Text UnshapedRecipieFactory::getTypeToken() const
  365. {
  366. return "unshaped";
  367. }
  368. ShapedRecipie::ShapedRecipie()
  369. : Recipie(),
  370. width(0),
  371. height(0)
  372. {}
  373. bool ShapedRecipie::testApplicability(CraftingStorage* zStorage)
  374. {
  375. ShapedCraftingStorage* zShapedStorage
  376. = dynamic_cast<ShapedCraftingStorage*>(zStorage);
  377. if (!zShapedStorage) return 0;
  378. for (RecipieOutput* output : outputs)
  379. {
  380. Item* item = output->createItem();
  381. if (item)
  382. {
  383. if (!zShapedStorage->hasFreeSpace(item, output->getAmount()))
  384. {
  385. item->release();
  386. return 0;
  387. }
  388. item->release();
  389. }
  390. }
  391. return zShapedStorage->isAllAvailable(inputs, width, height);
  392. }
  393. void ShapedRecipie::apply(CraftingStorage* zStorage)
  394. {
  395. ShapedCraftingStorage* zShapedStorage
  396. = dynamic_cast<ShapedCraftingStorage*>(zStorage);
  397. zShapedStorage->consume(inputs, width, height);
  398. for (RecipieOutput* output : outputs)
  399. {
  400. Item* item = output->createItem();
  401. if (item)
  402. {
  403. ItemStack* stack = new ItemStack(item, output->getAmount());
  404. zStorage->addCraftingResult(stack);
  405. stack->release();
  406. }
  407. }
  408. }
  409. Framework::Text ShapedRecipie::getRecipieUIML()
  410. {
  411. Framework::Text result = "<recipie type=\"shaped\" width=\"";
  412. result.append() << width << "\" height=\"" << height << "\"><ingredients>";
  413. for (RecipieInput* input : inputs)
  414. {
  415. result.append() << "<ingredient amount=\"" << input->getAmount()
  416. << "\">";
  417. if (input->zFilter())
  418. {
  419. result.append()
  420. << "<logic>" << input->zFilter()->getLogicUIML() << "</logic>";
  421. }
  422. result += "</ingredient>";
  423. // TODO: add modifiers
  424. }
  425. result += "</ingredients><outputs>";
  426. for (RecipieOutput* output : outputs)
  427. {
  428. Item* item = output->createItem();
  429. if (item)
  430. {
  431. result.append()
  432. << "<output amount=\"" << output->getAmount()
  433. << "\" itemType=\"" << item->zItemType()->getId() << "\" hp=\""
  434. << item->getHp() << "\" durability=\"" << item->getDurability()
  435. << "\" maxHp=\"" << item->getMaxHp() << "\" maxDurability=\""
  436. << item->getMaxDurability() << "\">" << item->getTooltipUIML()
  437. << "</output>";
  438. item->release();
  439. }
  440. }
  441. result += "</outputs></recipie>";
  442. return result;
  443. }
  444. void ShapedRecipie::setWidth(int width)
  445. {
  446. this->width = width;
  447. }
  448. int ShapedRecipie::getWidth() const
  449. {
  450. return width;
  451. }
  452. void ShapedRecipie::setHeight(int height)
  453. {
  454. this->height = height;
  455. }
  456. int ShapedRecipie::getHeight() const
  457. {
  458. return height;
  459. }
  460. void ShapedRecipie::addInput(RecipieInput* input)
  461. {
  462. inputs.add(input);
  463. }
  464. void ShapedRecipie::setInput(int index, RecipieInput* input)
  465. {
  466. inputs.set(input, index);
  467. }
  468. const Framework::RCArray<RecipieInput>& ShapedRecipie::getInputs() const
  469. {
  470. return inputs;
  471. }
  472. ShapedRecipieFactory::ShapedRecipieFactory()
  473. : RecipieFactory()
  474. {}
  475. ShapedRecipie* ShapedRecipieFactory::createValue(
  476. Framework::JSON::JSONObject* zJson) const
  477. {
  478. return new ShapedRecipie();
  479. }
  480. void ShapedRecipieFactory::fromJson(
  481. ShapedRecipie* zResult, Framework::JSON::JSONObject* zJson) const
  482. {
  483. int width = (int)zJson->zValue("width")->asNumber()->getNumber();
  484. int height = (int)zJson->zValue("height")->asNumber()->getNumber();
  485. for (int i = 0; i < width * height; i++)
  486. {
  487. zResult->addInput(new RecipieInput());
  488. }
  489. for (Framework::JSON::JSONValue* input :
  490. *zJson->zValue("inputs")->asArray())
  491. {
  492. int x = (int)input->asObject()->zValue("x")->asNumber()->getNumber();
  493. int y = (int)input->asObject()->zValue("y")->asNumber()->getNumber();
  494. if (x >= width || y >= height)
  495. {
  496. std::cout << "Invalid input position in shaped recipie with width="
  497. << width << ", height=" << height << "\n"
  498. << (x >= width ? x : y) << "\n";
  499. return;
  500. }
  501. zResult->setInput(y * width + x,
  502. Game::INSTANCE->zTypeRegistry()->fromJson<RecipieInput>(
  503. input->asObject()->zValue("input")));
  504. }
  505. zResult->setWidth((int)zJson->zValue("width")->asNumber()->getNumber());
  506. zResult->setHeight((int)zJson->zValue("height")->asNumber()->getNumber());
  507. RecipieFactory::fromJson(zResult, zJson);
  508. }
  509. void ShapedRecipieFactory::toJson(
  510. ShapedRecipie* zObject, Framework::JSON::JSONObject* zResult) const
  511. {
  512. zResult->addValue(
  513. "width", new Framework::JSON::JSONNumber(zObject->getWidth()));
  514. zResult->addValue(
  515. "height", new Framework::JSON::JSONNumber(zObject->getHeight()));
  516. Framework::JSON::JSONArray* inputs = new Framework::JSON::JSONArray();
  517. for (int i = 0; i < zObject->getWidth() * zObject->getHeight(); i++)
  518. {
  519. Framework::JSON::JSONObject* input = new Framework::JSON::JSONObject();
  520. input->addValue(
  521. "x", new Framework::JSON::JSONNumber(i % zObject->getHeight()));
  522. input->addValue(
  523. "y", new Framework::JSON::JSONNumber(i / zObject->getHeight()));
  524. input->addValue("input",
  525. Game::INSTANCE->zTypeRegistry()->toJson(zObject->getInputs().z(i)));
  526. inputs->addValue(input);
  527. }
  528. zResult->addValue("inputs", inputs);
  529. RecipieFactory::toJson(zObject, zResult);
  530. }
  531. JSONObjectValidationBuilder* ShapedRecipieFactory::addToValidator(
  532. JSONObjectValidationBuilder* builder) const
  533. {
  534. return RecipieFactory::addToValidator(
  535. builder->withRequiredArray("inputs")
  536. ->addAcceptedObjectInArray()
  537. ->withRequiredNumber("x")
  538. ->whichIsGreaterOrEqual(0.0)
  539. ->finishNumber()
  540. ->withRequiredNumber("y")
  541. ->whichIsGreaterOrEqual(0.0)
  542. ->finishNumber()
  543. ->withRequiredAttribute("input",
  544. Game::INSTANCE->zTypeRegistry()->getValidator<RecipieInput>())
  545. ->finishObject()
  546. ->finishArray()
  547. ->withRequiredNumber("width")
  548. ->whichIsGreaterOrEqual(1.0)
  549. ->finishNumber()
  550. ->withRequiredNumber("height")
  551. ->whichIsGreaterOrEqual(1.0)
  552. ->finishNumber());
  553. }
  554. Framework::Text ShapedRecipieFactory::getTypeToken() const
  555. {
  556. return "shaped";
  557. }