Recipie.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. #include "Recipie.h"
  2. #include <Logging.h>
  3. #include "CraftingStorage.h"
  4. #include "Game.h"
  5. #include "Item.h"
  6. #include "UIElement.h"
  7. RecipieInput::RecipieInput()
  8. : ReferenceCounter(),
  9. filter(0),
  10. modifier(0),
  11. amount(0)
  12. {}
  13. RecipieInput::~RecipieInput()
  14. {
  15. if (filter) filter->release();
  16. if (modifier) modifier->release();
  17. }
  18. void RecipieInput::setFilter(ItemFilter* filter)
  19. {
  20. if (this->filter) this->filter->release();
  21. this->filter = filter;
  22. }
  23. ItemFilter* RecipieInput::zFilter() const
  24. {
  25. return filter;
  26. }
  27. void RecipieInput::setModifier(ItemModifier* modifier)
  28. {
  29. if (this->modifier) this->modifier->release();
  30. this->modifier = modifier;
  31. }
  32. ItemModifier* RecipieInput::zModifier() const
  33. {
  34. return modifier;
  35. }
  36. void RecipieInput::setAmount(int amount)
  37. {
  38. this->amount = amount;
  39. }
  40. int RecipieInput::getAmount() const
  41. {
  42. return amount;
  43. }
  44. RecipieInputFactory::RecipieInputFactory()
  45. : ObjectTypeFactory()
  46. {}
  47. RecipieInput* RecipieInputFactory::fromJson(
  48. Framework::JSON::JSONObject* zJson) const
  49. {
  50. RecipieInput* result = new RecipieInput();
  51. result->setFilter(Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(
  52. zJson->asObject()->zValue("filter")));
  53. result->setModifier(Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(
  54. zJson->asObject()->zValue("modifier")));
  55. result->setAmount(
  56. (int)zJson->asObject()->zValue("amount")->asNumber()->getNumber());
  57. return result;
  58. }
  59. Framework::JSON::JSONObject* RecipieInputFactory::toJsonObject(
  60. RecipieInput* zObject) const
  61. {
  62. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  63. result->addValue(
  64. "filter", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zFilter()));
  65. result->addValue("modifier",
  66. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModifier()));
  67. result->addValue("amount",
  68. new Framework::JSON::JSONNumber((double)zObject->getAmount()));
  69. return result;
  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. : ObjectTypeFactory()
  140. {}
  141. RecipieOutput* RecipieOutputFactory::fromJson(
  142. Framework::JSON::JSONObject* zJson) const
  143. {
  144. RecipieOutput* result = new RecipieOutput();
  145. result->setItemTypeId(Game::INSTANCE->getItemTypeId(
  146. zJson->asObject()->zValue("itemType")->asString()->getString()));
  147. result->setAmount(
  148. (int)zJson->asObject()->zValue("amount")->asNumber()->getNumber());
  149. result->setModifier(Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(
  150. zJson->asObject()->zValue("modifier")));
  151. return result;
  152. }
  153. Framework::JSON::JSONObject* RecipieOutputFactory::toJsonObject(
  154. RecipieOutput* zObject) const
  155. {
  156. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  157. result->addValue("itemType",
  158. new Framework::JSON::JSONString(
  159. Game::INSTANCE->zItemType(zObject->getItemTypeId())->getName()));
  160. result->addValue("amount",
  161. new Framework::JSON::JSONNumber((double)zObject->getAmount()));
  162. result->addValue("modifier",
  163. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModifier()));
  164. return result;
  165. }
  166. JSONObjectValidationBuilder* RecipieOutputFactory::addToValidator(
  167. JSONObjectValidationBuilder* builder) const
  168. {
  169. Framework::JSON::JSONObject* defaultModifier
  170. = new Framework::JSON::JSONObject();
  171. defaultModifier->addValue(
  172. "type", new Framework::JSON::JSONString("doNothing"));
  173. return builder
  174. ->withRequiredAttribute("itemType",
  175. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  176. ItemTypeNameFactory::TYPE_ID))
  177. ->withRequiredAttribute("modifier",
  178. Game::INSTANCE->zTypeRegistry()->getValidator<ItemModifier>())
  179. ->withRequiredObject("modifier")
  180. ->withRequiredString("type")
  181. ->withExactMatch("doNothing")
  182. ->finishString()
  183. ->withDefault(defaultModifier)
  184. ->finishObject()
  185. ->withRequiredNumber("amount")
  186. ->whichIsGreaterOrEqual(1.0)
  187. ->withDefault(1.0)
  188. ->finishNumber();
  189. }
  190. Recipie::Recipie()
  191. : ReferenceCounter()
  192. {}
  193. void Recipie::apply(CraftingStorage* zStorage)
  194. {
  195. consumeInputs(zStorage);
  196. produceOutputs(zStorage);
  197. }
  198. void Recipie::addOutput(RecipieOutput* outputs)
  199. {
  200. this->outputs.add(outputs);
  201. }
  202. Framework::Array<ItemInfo> Recipie::getOutput() const
  203. {
  204. Framework::Array<ItemInfo> result;
  205. for (const RecipieOutput* output : outputs)
  206. {
  207. Item* item = output->createItem();
  208. if (item)
  209. {
  210. result.add({item->zItemType()->getId(),
  211. output->getAmount(),
  212. item->getHp(),
  213. item->getMaxHp(),
  214. item->getDurability(),
  215. item->getMaxDurability()});
  216. item->release();
  217. }
  218. }
  219. return result;
  220. }
  221. bool Recipie::createsOutput(int itemTypeId)
  222. {
  223. for (RecipieOutput* output : outputs)
  224. {
  225. if (output->getItemTypeId() == itemTypeId)
  226. {
  227. return 1;
  228. }
  229. }
  230. return 0;
  231. }
  232. void Recipie::setGroupName(Framework::Text groupName)
  233. {
  234. this->groupName = groupName;
  235. }
  236. const Framework::RCArray<RecipieOutput>& Recipie::getOutputs() const
  237. {
  238. return outputs;
  239. }
  240. Framework::Text Recipie::getGroupName() const
  241. {
  242. return groupName;
  243. }
  244. int Recipie::getTicksNeeded() const
  245. {
  246. return 1;
  247. }
  248. int Recipie::getFuelPerTickNeeded() const
  249. {
  250. return 0;
  251. }
  252. UnshapedRecipie::UnshapedRecipie()
  253. : Recipie()
  254. {}
  255. bool UnshapedRecipie::testApplicability(CraftingStorage* zStorage)
  256. {
  257. for (RecipieOutput* output : outputs)
  258. {
  259. Item* item = output->createItem();
  260. if (item)
  261. {
  262. if (!zStorage->hasFreeSpace(item, output->getAmount()))
  263. {
  264. item->release();
  265. return 0;
  266. }
  267. item->release();
  268. }
  269. }
  270. return zStorage->isAllAvailable(inputs);
  271. }
  272. void UnshapedRecipie::consumeInputs(CraftingStorage* zStorage)
  273. {
  274. zStorage->consume(inputs);
  275. }
  276. void UnshapedRecipie::produceOutputs(CraftingStorage* zStorage)
  277. {
  278. for (RecipieOutput* output : outputs)
  279. {
  280. Item* item = output->createItem();
  281. if (item)
  282. {
  283. ItemStack* stack = new ItemStack(item, output->getAmount());
  284. zStorage->addCraftingResult(stack);
  285. if (stack->getSize() > 0)
  286. {
  287. Game::INSTANCE->spawnItem(zStorage->getStorageLocation(),
  288. zStorage->getStorageDimensionId(),
  289. stack);
  290. }
  291. else
  292. {
  293. stack->release();
  294. }
  295. }
  296. }
  297. }
  298. Framework::Text UnshapedRecipie::getRecipieUIML()
  299. {
  300. Framework::Text result = "<recipie type=\"unshaped\"><ingredients>";
  301. for (RecipieInput* input : inputs)
  302. {
  303. result.append() << "<ingredient amount=\"" << input->getAmount()
  304. << "\">";
  305. if (input->zFilter())
  306. {
  307. result.append()
  308. << "<logic>" << input->zFilter()->getLogicUIML() << "</logic>";
  309. }
  310. result += "</ingredient>";
  311. // TODO: add modifiers
  312. }
  313. result += "</ingredients><outputs>";
  314. for (RecipieOutput* output : outputs)
  315. {
  316. Item* item = output->createItem();
  317. if (item)
  318. {
  319. result.append()
  320. << "<output amount=\"" << output->getAmount()
  321. << "\" itemType=\"" << item->zItemType()->getId() << "\" hp=\""
  322. << item->getHp() << "\" durability=\"" << item->getDurability()
  323. << "\" maxHp=\"" << item->getMaxHp() << "\" maxDurability=\""
  324. << item->getMaxDurability() << "\">" << item->getTooltipUIML()
  325. << "</output>";
  326. item->release();
  327. }
  328. }
  329. result += "</outputs></recipie>";
  330. return result;
  331. }
  332. void UnshapedRecipie::addInput(RecipieInput* input)
  333. {
  334. inputs.add(input);
  335. }
  336. const Framework::RCArray<RecipieInput>& UnshapedRecipie::getInputs() const
  337. {
  338. return inputs;
  339. }
  340. ShapedRecipie::ShapedRecipie()
  341. : Recipie(),
  342. width(0),
  343. height(0)
  344. {}
  345. bool ShapedRecipie::testApplicability(CraftingStorage* zStorage)
  346. {
  347. ShapedCraftingStorage* zShapedStorage
  348. = dynamic_cast<ShapedCraftingStorage*>(zStorage);
  349. if (!zShapedStorage) return 0;
  350. for (RecipieOutput* output : outputs)
  351. {
  352. Item* item = output->createItem();
  353. if (item)
  354. {
  355. if (!zShapedStorage->hasFreeSpace(item, output->getAmount()))
  356. {
  357. item->release();
  358. return 0;
  359. }
  360. item->release();
  361. }
  362. }
  363. return zShapedStorage->isAllAvailable(inputs, width, height);
  364. }
  365. void ShapedRecipie::consumeInputs(CraftingStorage* zStorage)
  366. {
  367. ShapedCraftingStorage* zShapedStorage
  368. = dynamic_cast<ShapedCraftingStorage*>(zStorage);
  369. zShapedStorage->consume(inputs, width, height);
  370. }
  371. void ShapedRecipie::produceOutputs(CraftingStorage* zStorage)
  372. {
  373. for (RecipieOutput* output : outputs)
  374. {
  375. Item* item = output->createItem();
  376. if (item)
  377. {
  378. ItemStack* stack = new ItemStack(item, output->getAmount());
  379. zStorage->addCraftingResult(stack);
  380. if (stack->getSize() > 0)
  381. {
  382. Game::INSTANCE->spawnItem(zStorage->getStorageLocation(),
  383. zStorage->getStorageDimensionId(),
  384. stack);
  385. }
  386. else
  387. {
  388. stack->release();
  389. }
  390. }
  391. }
  392. }
  393. Framework::Text ShapedRecipie::getRecipieUIML()
  394. {
  395. Framework::Text result = "<recipie type=\"shaped\" width=\"";
  396. result.append() << width << "\" height=\"" << height << "\"><ingredients>";
  397. for (RecipieInput* input : inputs)
  398. {
  399. result.append() << "<ingredient amount=\"" << input->getAmount()
  400. << "\">";
  401. if (input->zFilter())
  402. {
  403. result.append()
  404. << "<logic>" << input->zFilter()->getLogicUIML() << "</logic>";
  405. }
  406. result += "</ingredient>";
  407. // TODO: add modifiers
  408. }
  409. result += "</ingredients><outputs>";
  410. for (RecipieOutput* output : outputs)
  411. {
  412. Item* item = output->createItem();
  413. if (item)
  414. {
  415. result.append()
  416. << "<output amount=\"" << output->getAmount()
  417. << "\" itemType=\"" << item->zItemType()->getId() << "\" hp=\""
  418. << item->getHp() << "\" durability=\"" << item->getDurability()
  419. << "\" maxHp=\"" << item->getMaxHp() << "\" maxDurability=\""
  420. << item->getMaxDurability() << "\">" << item->getTooltipUIML()
  421. << "</output>";
  422. item->release();
  423. }
  424. }
  425. result += "</outputs></recipie>";
  426. return result;
  427. }
  428. void ShapedRecipie::setWidth(int width)
  429. {
  430. this->width = width;
  431. }
  432. int ShapedRecipie::getWidth() const
  433. {
  434. return width;
  435. }
  436. void ShapedRecipie::setHeight(int height)
  437. {
  438. this->height = height;
  439. }
  440. int ShapedRecipie::getHeight() const
  441. {
  442. return height;
  443. }
  444. void ShapedRecipie::addInput(RecipieInput* input)
  445. {
  446. inputs.add(input);
  447. }
  448. void ShapedRecipie::setInput(int index, RecipieInput* input)
  449. {
  450. inputs.set(input, index);
  451. }
  452. const Framework::RCArray<RecipieInput>& ShapedRecipie::getInputs() const
  453. {
  454. return inputs;
  455. }
  456. ShapedRecipieFactory::ShapedRecipieFactory()
  457. : RecipieFactory()
  458. {}
  459. ShapedRecipie* ShapedRecipieFactory::createValue(
  460. Framework::JSON::JSONObject* zJson) const
  461. {
  462. return new ShapedRecipie();
  463. }
  464. ShapedRecipie* ShapedRecipieFactory::fromJson(
  465. Framework::JSON::JSONObject* zJson) const
  466. {
  467. ShapedRecipie* result = RecipieFactory::fromJson(zJson);
  468. int width = (int)zJson->zValue("width")->asNumber()->getNumber();
  469. int height = (int)zJson->zValue("height")->asNumber()->getNumber();
  470. for (int i = 0; i < width * height; i++)
  471. {
  472. result->addInput(new RecipieInput());
  473. }
  474. for (Framework::JSON::JSONValue* input :
  475. *zJson->zValue("inputs")->asArray())
  476. {
  477. int x = (int)input->asObject()->zValue("x")->asNumber()->getNumber();
  478. int y = (int)input->asObject()->zValue("y")->asNumber()->getNumber();
  479. if (x >= width || y >= height)
  480. {
  481. Framework::Logging::warning()
  482. << "Invalid input position in shaped recipie with width="
  483. << width << ", height=" << height << ", x=" << x << ", y=" << y
  484. << " input " << input->toString() << " will be ignored.";
  485. continue;
  486. }
  487. result->setInput(y * width + x,
  488. Game::INSTANCE->zTypeRegistry()->fromJson<RecipieInput>(
  489. input->asObject()->zValue("input")));
  490. }
  491. result->setWidth((int)zJson->zValue("width")->asNumber()->getNumber());
  492. result->setHeight((int)zJson->zValue("height")->asNumber()->getNumber());
  493. return result;
  494. }
  495. Framework::JSON::JSONObject* ShapedRecipieFactory::toJsonObject(
  496. ShapedRecipie* zObject) const
  497. {
  498. Framework::JSON::JSONObject* result = RecipieFactory::toJsonObject(zObject);
  499. result->addValue(
  500. "width", new Framework::JSON::JSONNumber(zObject->getWidth()));
  501. result->addValue(
  502. "height", new Framework::JSON::JSONNumber(zObject->getHeight()));
  503. Framework::JSON::JSONArray* inputs = new Framework::JSON::JSONArray();
  504. for (int i = 0; i < zObject->getWidth() * zObject->getHeight(); i++)
  505. {
  506. Framework::JSON::JSONObject* input = new Framework::JSON::JSONObject();
  507. input->addValue(
  508. "x", new Framework::JSON::JSONNumber(i % zObject->getHeight()));
  509. input->addValue(
  510. "y", new Framework::JSON::JSONNumber(i / zObject->getHeight()));
  511. input->addValue("input",
  512. Game::INSTANCE->zTypeRegistry()->toJson(zObject->getInputs().z(i)));
  513. inputs->addValue(input);
  514. }
  515. result->addValue("inputs", inputs);
  516. return result;
  517. }
  518. JSONObjectValidationBuilder* ShapedRecipieFactory::addToValidator(
  519. JSONObjectValidationBuilder* builder) const
  520. {
  521. return RecipieFactory::addToValidator(builder->withRequiredArray("inputs")
  522. ->addAcceptedObjectInArray()
  523. ->withRequiredNumber("x")
  524. ->whichIsGreaterOrEqual(0.0)
  525. ->finishNumber()
  526. ->withRequiredNumber("y")
  527. ->whichIsGreaterOrEqual(0.0)
  528. ->finishNumber()
  529. ->withRequiredAttribute("input",
  530. Game::INSTANCE->zTypeRegistry()->getValidator<RecipieInput>())
  531. ->finishObject()
  532. ->finishArray()
  533. ->withRequiredNumber("width")
  534. ->whichIsGreaterOrEqual(1.0)
  535. ->finishNumber()
  536. ->withRequiredNumber("height")
  537. ->whichIsGreaterOrEqual(1.0)
  538. ->finishNumber());
  539. }
  540. const char* ShapedRecipieFactory::getTypeToken() const
  541. {
  542. return "shaped";
  543. }
  544. MachineRecipie::MachineRecipie()
  545. : UnshapedRecipie(),
  546. neededTicks(1),
  547. neededFuelPerTick(0)
  548. {}
  549. void MachineRecipie::setTicksNeeded(int ticks)
  550. {
  551. neededTicks = ticks;
  552. }
  553. int MachineRecipie::getTicksNeeded() const
  554. {
  555. return neededTicks;
  556. }
  557. void MachineRecipie::setFuelPerTickNeeded(int fuel)
  558. {
  559. neededFuelPerTick = fuel;
  560. }
  561. int MachineRecipie::getFuelPerTickNeeded() const
  562. {
  563. return neededFuelPerTick;
  564. }
  565. MachineRecipieFactory::MachineRecipieFactory()
  566. : UnshapedRecipieFactory()
  567. {}
  568. MachineRecipie* MachineRecipieFactory::createValue(
  569. Framework::JSON::JSONObject* zJson) const
  570. {
  571. return new MachineRecipie();
  572. }
  573. MachineRecipie* MachineRecipieFactory::fromJson(
  574. Framework::JSON::JSONObject* zJson) const
  575. {
  576. MachineRecipie* result = UnshapedRecipieFactory::fromJson(zJson);
  577. result->setTicksNeeded(
  578. (int)zJson->zValue("ticksNeeded")->asNumber()->getNumber());
  579. result->setFuelPerTickNeeded(
  580. (int)zJson->zValue("fuelPerTickNeeded")->asNumber()->getNumber());
  581. return result;
  582. }
  583. Framework::JSON::JSONObject* MachineRecipieFactory::toJsonObject(
  584. MachineRecipie* zObject) const
  585. {
  586. Framework::JSON::JSONObject* result
  587. = UnshapedRecipieFactory::toJsonObject(zObject);
  588. result->addValue("ticksNeeded",
  589. new Framework::JSON::JSONNumber(zObject->getTicksNeeded()));
  590. result->addValue("fuelNeeded",
  591. new Framework::JSON::JSONNumber(zObject->getFuelPerTickNeeded()));
  592. return result;
  593. }
  594. JSONObjectValidationBuilder* MachineRecipieFactory::addToValidator(
  595. JSONObjectValidationBuilder* builder) const
  596. {
  597. return UnshapedRecipieFactory::addToValidator(builder)
  598. ->withRequiredNumber("ticksNeeded")
  599. ->whichIsGreaterOrEqual(1.0)
  600. ->withDefault(1.0)
  601. ->finishNumber()
  602. ->withRequiredNumber("fuelNeeded")
  603. ->whichIsGreaterOrEqual(0.0)
  604. ->withDefault(0.0)
  605. ->finishNumber();
  606. }
  607. const char* MachineRecipieFactory::getTypeToken() const
  608. {
  609. return "machine";
  610. }