FireBasedProcessingBlockComponent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #include "FireBasedProcessingBlockComponent.h"
  2. #include "Block.h"
  3. #include "ItemFilter.h"
  4. #include "Recipie.h"
  5. #include "RecipieList.h"
  6. #include "RecipieLoader.h"
  7. FireBasedProcessingBlockComponent::FireBasedProcessingBlockComponent()
  8. : BlockComponent()
  9. {}
  10. FireBasedProcessingBlockComponent::~FireBasedProcessingBlockComponent() {}
  11. void FireBasedProcessingBlockComponent::findRecipie()
  12. {
  13. RecipieList* zRecipies
  14. = Game::INSTANCE->zRecipies()->zRecipieList(recipieGroup.getText());
  15. if (zRecipies)
  16. {
  17. zBlock->lock();
  18. Recipie* recipie = zRecipies->zFirstRecipie(this);
  19. if (recipie)
  20. {
  21. recipie->consumeInputs(this);
  22. currentRecipie = recipie;
  23. ticksNeeded = recipie->getTicksNeeded();
  24. }
  25. zBlock->unlock();
  26. }
  27. }
  28. void FireBasedProcessingBlockComponent::consumeFuel()
  29. {
  30. zBlock->lock();
  31. ItemSlot* fireStartingSlot = 0;
  32. ItemSlot* fuelSlot = 0;
  33. for (ItemSlot* slot : *zBlock)
  34. {
  35. if (slot->zStack())
  36. {
  37. if (!burning && !fireStartingSlot
  38. && slot->getName().istGleich(fireStartingInventorySlotName)
  39. && fireStartingItemFilter->matchItem(slot->zStack()->zItem()))
  40. {
  41. fireStartingSlot = slot;
  42. }
  43. if (!fuelSlot && slot->getName().istGleich(fuelInventorySlotName)
  44. && fuelItemFilter->matchItem(slot->zStack()->zItem()))
  45. {
  46. fuelSlot = slot;
  47. }
  48. if (fuelSlot && fireStartingSlot)
  49. {
  50. break;
  51. }
  52. }
  53. }
  54. if (burning)
  55. {
  56. if (fuelSlot)
  57. {
  58. ItemStack* fuelStack
  59. = fuelSlot->takeItemsOut(1, Direction::NO_DIRECTION);
  60. if (fuelStack)
  61. {
  62. // TODO: check if item is burnable and how much fuel it provides
  63. fuelBuffer += 100;
  64. fuelStack->release();
  65. }
  66. else
  67. {
  68. if (fuelBuffer == 0)
  69. {
  70. burning = false;
  71. }
  72. }
  73. }
  74. else
  75. {
  76. if (fuelBuffer == 0)
  77. {
  78. burning = false;
  79. }
  80. }
  81. }
  82. else
  83. {
  84. if (fuelSlot && fireStartingSlot)
  85. {
  86. ItemStack* fuelStack
  87. = fuelSlot->takeItemsOut(1, Direction::NO_DIRECTION);
  88. ItemStack* fireStartingStack
  89. = fireStartingSlot->takeItemsOut(1, Direction::NO_DIRECTION);
  90. if (fuelStack && fireStartingStack)
  91. {
  92. // TODO: check if item is burnable and how much fuel it provides
  93. fuelBuffer += 100;
  94. burning = true;
  95. fuelStack->release();
  96. fireStartingStack->release();
  97. }
  98. }
  99. }
  100. zBlock->unlock();
  101. }
  102. void FireBasedProcessingBlockComponent::initialize(Block* zBlock)
  103. {
  104. this->zBlock = zBlock;
  105. }
  106. bool FireBasedProcessingBlockComponent::tick(int numTicks)
  107. {
  108. bool active = 0;
  109. while (numTicks > 0)
  110. {
  111. if (!fuelBuffer)
  112. {
  113. consumeFuel();
  114. }
  115. if (!burning)
  116. {
  117. break;
  118. }
  119. if (!currentRecipie)
  120. {
  121. findRecipie();
  122. }
  123. bool processed = false;
  124. if (currentRecipie)
  125. {
  126. int possibleTicks
  127. = fuelBuffer / currentRecipie->getFuelPerTickNeeded();
  128. if (!possibleTicks)
  129. {
  130. consumeFuel();
  131. possibleTicks
  132. = fuelBuffer / currentRecipie->getFuelPerTickNeeded();
  133. }
  134. if (possibleTicks >= numTicks)
  135. {
  136. if (numTicks >= ticksNeeded)
  137. {
  138. numTicks -= ticksNeeded;
  139. fuelBuffer
  140. -= currentRecipie->getFuelPerTickNeeded() * ticksNeeded;
  141. zBlock->lock();
  142. currentRecipie->produceOutputs(this);
  143. zBlock->unlock();
  144. ticksNeeded = 0;
  145. currentRecipie = 0;
  146. }
  147. else
  148. {
  149. ticksNeeded -= numTicks;
  150. fuelBuffer
  151. -= currentRecipie->getFuelPerTickNeeded() * numTicks;
  152. numTicks = 0;
  153. }
  154. processed = true;
  155. }
  156. else
  157. {
  158. if (possibleTicks >= ticksNeeded)
  159. {
  160. numTicks -= ticksNeeded;
  161. fuelBuffer
  162. -= currentRecipie->getFuelPerTickNeeded() * ticksNeeded;
  163. zBlock->lock();
  164. currentRecipie->produceOutputs(this);
  165. zBlock->unlock();
  166. ticksNeeded = 0;
  167. currentRecipie = 0;
  168. processed = true;
  169. }
  170. else
  171. {
  172. numTicks -= possibleTicks;
  173. fuelBuffer -= currentRecipie->getFuelPerTickNeeded()
  174. * possibleTicks;
  175. ticksNeeded -= possibleTicks;
  176. processed = possibleTicks > 0;
  177. }
  178. }
  179. }
  180. if (!processed)
  181. {
  182. // burning without recipie
  183. if (fuelBuffer >= numTicks)
  184. {
  185. fuelBuffer -= numTicks;
  186. numTicks = 0;
  187. }
  188. else
  189. {
  190. numTicks -= fuelBuffer;
  191. fuelBuffer = 0;
  192. }
  193. }
  194. active = 1;
  195. }
  196. return active;
  197. }
  198. Framework::XML::Element* FireBasedProcessingBlockComponent::getUIML() const
  199. {
  200. return nullptr;
  201. }
  202. bool FireBasedProcessingBlockComponent::isAllAvailable(
  203. Framework::RCArray<RecipieInput>& inputs)
  204. {
  205. return zBlock->isAllAvailable(inputs, inputInventorySlotName);
  206. }
  207. bool FireBasedProcessingBlockComponent::hasFreeSpace(
  208. const Item* zItem, int amount)
  209. {
  210. int addable = zBlock->numberOfAddableItems(
  211. zItem, NO_DIRECTION, inputInventorySlotName);
  212. return addable >= amount;
  213. }
  214. void FireBasedProcessingBlockComponent::consume(
  215. Framework::RCArray<RecipieInput>& inputs)
  216. {
  217. zBlock->consume(inputs, inputInventorySlotName);
  218. }
  219. void FireBasedProcessingBlockComponent::addCraftingResult(ItemStack* zStack)
  220. {
  221. TargetSlotNameItemFilter filter(outputInventorySlotName);
  222. zBlock->unsaveAddItem(zStack, NO_DIRECTION, &filter);
  223. }
  224. Framework::Vec3<float>
  225. FireBasedProcessingBlockComponent::getStorageLocation() const
  226. {
  227. return zBlock->getLocation();
  228. }
  229. int FireBasedProcessingBlockComponent::getStorageDimensionId() const
  230. {
  231. return zBlock->getDimensionId();
  232. }
  233. void FireBasedProcessingBlockComponent::loadComponent(
  234. Framework::StreamReader* zReader)
  235. {
  236. zReader->lese((char*)&ticksNeeded, 4);
  237. zReader->lese((char*)&fuelBuffer, 4);
  238. zReader->lese((char*)&burning, 1);
  239. int index = 0;
  240. zReader->lese((char*)&index, 4);
  241. if (index >= 0)
  242. {
  243. // TODO: add unique recipie ids to enshure correct loading after
  244. // recipies were changed, added or removed
  245. RecipieList* recipies
  246. = Game::INSTANCE->zRecipies()->zRecipieList(recipieGroup);
  247. if (recipies && index < recipies->getRecipieCount())
  248. {
  249. currentRecipie = recipies->zRecipie(index);
  250. }
  251. }
  252. }
  253. void FireBasedProcessingBlockComponent::saveComponent(
  254. Framework::StreamWriter* zWriter) const
  255. {
  256. zWriter->schreibe((char*)&ticksNeeded, 4);
  257. zWriter->schreibe((char*)&fuelBuffer, 4);
  258. zWriter->schreibe((char*)&burning, 1);
  259. int index = -1;
  260. if (currentRecipie)
  261. {
  262. // TODO: add unique recipie ids to enshure correct loading after
  263. // recipies were changed, added or removed
  264. RecipieList* recipies
  265. = Game::INSTANCE->zRecipies()->zRecipieList(recipieGroup);
  266. if (recipies)
  267. {
  268. index = recipies->getRecipieIndex(currentRecipie);
  269. }
  270. }
  271. zWriter->schreibe((char*)&index, 4);
  272. }
  273. FireBasedProcessingBlockComponentFactory::
  274. FireBasedProcessingBlockComponentFactory()
  275. : SubTypeFactory()
  276. {}
  277. FireBasedProcessingBlockComponent*
  278. FireBasedProcessingBlockComponentFactory::fromJson(
  279. Framework::JSON::JSONObject* zJson) const
  280. {
  281. FireBasedProcessingBlockComponent* component
  282. = new FireBasedProcessingBlockComponent();
  283. if (zJson->hasValue("fireStartingItemFilter"))
  284. {
  285. component->fireStartingItemFilter
  286. = Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(
  287. zJson->zValue("fireStartingItemFilter"));
  288. }
  289. else
  290. {
  291. component->fireStartingItemFilter = new AnyItemFilter();
  292. }
  293. if (zJson->hasValue("fuelItemFilter"))
  294. {
  295. component->fuelItemFilter
  296. = Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(
  297. zJson->zValue("fuelItemFilter"));
  298. }
  299. else
  300. {
  301. component->fuelItemFilter = new AnyItemFilter();
  302. }
  303. component->recipieGroup
  304. = zJson->zValue("recipieGroup")->asString()->getString();
  305. component->fuelInventorySlotName
  306. = zJson->zValue("fuelInventorySlotName")->asString()->getString();
  307. component->fireStartingInventorySlotName
  308. = zJson->zValue("fireStartingInventorySlotName")
  309. ->asString()
  310. ->getString();
  311. component->inputInventorySlotName
  312. = zJson->zValue("inputInventorySlotName")->asString()->getString();
  313. component->outputInventorySlotName
  314. = zJson->zValue("outputInventorySlotName")->asString()->getString();
  315. return component;
  316. }
  317. Framework::JSON::JSONObject*
  318. FireBasedProcessingBlockComponentFactory::toJsonObject(
  319. FireBasedProcessingBlockComponent* zObject) const
  320. {
  321. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  322. result->addValue("fireStartingItemFilter",
  323. Game::INSTANCE->zTypeRegistry()->toJson(
  324. zObject->fireStartingItemFilter));
  325. result->addValue("fuelItemFilter",
  326. Game::INSTANCE->zTypeRegistry()->toJson(zObject->fuelItemFilter));
  327. result->addValue("recipieGroup",
  328. new Framework::JSON::JSONString(zObject->recipieGroup.getText()));
  329. result->addValue("fuelInventorySlotName",
  330. new Framework::JSON::JSONString(
  331. zObject->fuelInventorySlotName.getText()));
  332. result->addValue("fireStartingInventorySlotName",
  333. new Framework::JSON::JSONString(
  334. zObject->fireStartingInventorySlotName.getText()));
  335. result->addValue("inputInventorySlotName",
  336. new Framework::JSON::JSONString(
  337. zObject->inputInventorySlotName.getText()));
  338. result->addValue("outputInventorySlotName",
  339. new Framework::JSON::JSONString(
  340. zObject->outputInventorySlotName.getText()));
  341. return result;
  342. }
  343. JSONObjectValidationBuilder*
  344. FireBasedProcessingBlockComponentFactory::addToValidator(
  345. JSONObjectValidationBuilder* builder) const
  346. {
  347. return builder
  348. ->withRequiredAttribute("fireStartingItemFilter",
  349. Game::INSTANCE->zTypeRegistry()->getValidator<ItemFilter>())
  350. ->withRequiredAttribute("fuelItemFilter",
  351. Game::INSTANCE->zTypeRegistry()->getValidator<ItemFilter>())
  352. ->withRequiredAttribute("recipieGroup",
  353. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>())
  354. ->withRequiredAttribute("fuelInventorySlotName",
  355. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>())
  356. ->withRequiredAttribute("fireStartingInventorySlotName",
  357. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>())
  358. ->withRequiredAttribute("inputInventorySlotName",
  359. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>())
  360. ->withRequiredAttribute("outputInventorySlotName",
  361. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>());
  362. return builder;
  363. }
  364. const char* FireBasedProcessingBlockComponentFactory::getTypeToken() const
  365. {
  366. return "fireBasedProcessing";
  367. }