FireBasedProcessingBlockComponent.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. burnLight(0),
  10. ticksNeeded(0),
  11. currentRecipie(0),
  12. maxFuelBuffer(0),
  13. fuelBuffer(0),
  14. burning(0)
  15. {}
  16. FireBasedProcessingBlockComponent::~FireBasedProcessingBlockComponent() {}
  17. bool FireBasedProcessingBlockComponent::findRecipie()
  18. {
  19. bool changed = 0;
  20. RecipieList* zRecipies
  21. = Game::INSTANCE->zRecipies()->zRecipieList(recipieGroup.getText());
  22. if (zRecipies)
  23. {
  24. zBlock->lock();
  25. Recipie* recipie = zRecipies->zFirstRecipie(this);
  26. if (recipie)
  27. {
  28. recipie->consumeInputs(this);
  29. currentRecipie = recipie;
  30. ticksNeeded = recipie->getTicksNeeded();
  31. changed = 1;
  32. }
  33. zBlock->unlock();
  34. }
  35. return changed;
  36. }
  37. bool FireBasedProcessingBlockComponent::consumeFuel()
  38. {
  39. bool changed = 0;
  40. zBlock->lock();
  41. ItemSlot* fireStartingSlot = 0;
  42. ItemSlot* fuelSlot = 0;
  43. for (ItemSlot* slot : *zBlock)
  44. {
  45. if (slot->zStack())
  46. {
  47. if (!burning && !fireStartingSlot
  48. && slot->getName().istGleich(fireStartingInventorySlotName)
  49. && fireStartingItemFilter->matchItem(slot->zStack()->zItem()))
  50. {
  51. fireStartingSlot = slot;
  52. }
  53. if (!fuelSlot && slot->getName().istGleich(fuelInventorySlotName)
  54. && fuelItemFilter->matchItem(slot->zStack()->zItem()))
  55. {
  56. fuelSlot = slot;
  57. }
  58. if (fuelSlot && fireStartingSlot)
  59. {
  60. break;
  61. }
  62. }
  63. }
  64. if (burning)
  65. {
  66. if (fuelSlot)
  67. {
  68. ItemStack* fuelStack
  69. = fuelSlot->takeItemsOut(1, Direction::NO_DIRECTION);
  70. if (fuelStack)
  71. {
  72. // TODO: check if item is burnable and how much fuel it provides
  73. fuelBuffer += 100;
  74. maxFuelBuffer = fuelBuffer;
  75. fuelStack->release();
  76. changed = 1;
  77. }
  78. else
  79. {
  80. if (fuelBuffer == 0)
  81. {
  82. burning = false;
  83. }
  84. }
  85. }
  86. else
  87. {
  88. if (fuelBuffer == 0)
  89. {
  90. burning = false;
  91. }
  92. }
  93. }
  94. else
  95. {
  96. if (fuelSlot && fireStartingSlot)
  97. {
  98. ItemStack* fuelStack
  99. = fuelSlot->takeItemsOut(1, Direction::NO_DIRECTION);
  100. ItemStack* fireStartingStack
  101. = fireStartingSlot->takeItemsOut(1, Direction::NO_DIRECTION);
  102. if (fuelStack && fireStartingStack)
  103. {
  104. // TODO: check if item is burnable and how much fuel it provides
  105. fuelBuffer += 100;
  106. maxFuelBuffer = fuelBuffer;
  107. burning = true;
  108. fuelStack->release();
  109. fireStartingStack->release();
  110. changed = 1;
  111. }
  112. }
  113. }
  114. zBlock->unlock();
  115. return changed;
  116. }
  117. void FireBasedProcessingBlockComponent::createProgressMessage(
  118. NetworkMessage* message) const
  119. {
  120. char* msg = new char[8];
  121. *(int*)msg = currentRecipie ? currentRecipie->getTicksNeeded() : 0;
  122. *(int*)(msg + 4)
  123. = currentRecipie ? currentRecipie->getTicksNeeded() - ticksNeeded : 0;
  124. message->setMessage(msg, 8);
  125. }
  126. void FireBasedProcessingBlockComponent::createFuelMessage(
  127. NetworkMessage* message) const
  128. {
  129. char* msg = new char[8];
  130. *(int*)msg = maxFuelBuffer;
  131. *(int*)(msg + 4) = fuelBuffer;
  132. message->setMessage(msg, 8);
  133. }
  134. void FireBasedProcessingBlockComponent::initialize(Block* zBlock)
  135. {
  136. this->zBlock = zBlock;
  137. }
  138. bool FireBasedProcessingBlockComponent::tick(int numTicks)
  139. {
  140. bool active = 0;
  141. bool progressChanged = 0;
  142. bool fuelChanged = 0;
  143. while (numTicks > 0)
  144. {
  145. if (!fuelBuffer)
  146. {
  147. fuelChanged |= consumeFuel();
  148. }
  149. if (!burning)
  150. {
  151. break;
  152. }
  153. if (!currentRecipie)
  154. {
  155. progressChanged |= findRecipie();
  156. }
  157. bool processed = false;
  158. if (currentRecipie)
  159. {
  160. int possibleTicks
  161. = fuelBuffer / currentRecipie->getFuelPerTickNeeded();
  162. if (!possibleTicks)
  163. {
  164. fuelChanged |= consumeFuel();
  165. possibleTicks
  166. = fuelBuffer / currentRecipie->getFuelPerTickNeeded();
  167. }
  168. if (possibleTicks >= numTicks)
  169. {
  170. if (numTicks >= ticksNeeded)
  171. {
  172. numTicks -= ticksNeeded;
  173. fuelBuffer
  174. -= currentRecipie->getFuelPerTickNeeded() * ticksNeeded;
  175. zBlock->lock();
  176. currentRecipie->produceOutputs(this);
  177. zBlock->unlock();
  178. ticksNeeded = 0;
  179. currentRecipie = 0;
  180. }
  181. else
  182. {
  183. ticksNeeded -= numTicks;
  184. fuelBuffer
  185. -= currentRecipie->getFuelPerTickNeeded() * numTicks;
  186. numTicks = 0;
  187. }
  188. progressChanged = 1;
  189. fuelChanged = 1;
  190. processed = true;
  191. }
  192. else
  193. {
  194. if (possibleTicks >= ticksNeeded)
  195. {
  196. numTicks -= ticksNeeded;
  197. fuelBuffer
  198. -= currentRecipie->getFuelPerTickNeeded() * ticksNeeded;
  199. zBlock->lock();
  200. currentRecipie->produceOutputs(this);
  201. zBlock->unlock();
  202. ticksNeeded = 0;
  203. currentRecipie = 0;
  204. processed = true;
  205. fuelChanged = 1;
  206. progressChanged = 1;
  207. }
  208. else
  209. {
  210. numTicks -= possibleTicks;
  211. fuelBuffer -= currentRecipie->getFuelPerTickNeeded()
  212. * possibleTicks;
  213. ticksNeeded -= possibleTicks;
  214. processed = possibleTicks > 0;
  215. progressChanged = possibleTicks > 0;
  216. fuelChanged = possibleTicks > 0;
  217. }
  218. }
  219. }
  220. if (!processed)
  221. {
  222. // burning without recipie
  223. if (fuelBuffer >= numTicks)
  224. {
  225. fuelBuffer -= numTicks;
  226. numTicks = 0;
  227. fuelChanged = 1;
  228. }
  229. else
  230. {
  231. fuelChanged = fuelBuffer > 0;
  232. numTicks -= fuelBuffer;
  233. fuelBuffer = 0;
  234. }
  235. }
  236. active = 1;
  237. }
  238. if (fuelChanged)
  239. {
  240. NetworkMessage* fuelMessage = new NetworkMessage();
  241. createFuelMessage(fuelMessage);
  242. fuelObservable.notifyObservers(fuelMessage);
  243. }
  244. if (progressChanged)
  245. {
  246. NetworkMessage* progressMessage = new NetworkMessage();
  247. createProgressMessage(progressMessage);
  248. progressObservable.notifyObservers(progressMessage);
  249. }
  250. return active;
  251. }
  252. void FireBasedProcessingBlockComponent::api(Framework::StreamReader* zRequest,
  253. NetworkMessage* zResponse,
  254. Entity* zSource)
  255. {
  256. char type;
  257. zRequest->lese(&type, 1);
  258. switch (type)
  259. {
  260. case 0: // subscribe to fuel
  261. {
  262. char idLen;
  263. zRequest->lese(&idLen, 1);
  264. char* id = new char[idLen + 1];
  265. zRequest->lese(id, idLen);
  266. id[(int)idLen] = 0;
  267. int processor;
  268. zRequest->lese((char*)&processor, 4);
  269. fuelObservable.addObserver(zSource, id, processor);
  270. createFuelMessage(zResponse);
  271. zResponse->addressUIElement(id, processor);
  272. break;
  273. }
  274. case 1: // subscribe to progress
  275. {
  276. char idLen;
  277. zRequest->lese(&idLen, 1);
  278. char* id = new char[idLen + 1];
  279. zRequest->lese(id, idLen);
  280. id[(int)idLen] = 0;
  281. int processor;
  282. zRequest->lese((char*)&processor, 4);
  283. progressObservable.addObserver(zSource, id, processor);
  284. createProgressMessage(zResponse);
  285. zResponse->addressUIElement(id, processor);
  286. break;
  287. }
  288. case 2: // unsubscribe to fuel
  289. {
  290. char idLen;
  291. zRequest->lese(&idLen, 1);
  292. char* id = new char[idLen + 1];
  293. zRequest->lese(id, idLen);
  294. id[(int)idLen] = 0;
  295. int processor;
  296. zRequest->lese((char*)&processor, 4);
  297. fuelObservable.removeObserver(zSource, id, processor);
  298. break;
  299. }
  300. case 3: // unsubscribe to progress
  301. {
  302. char idLen;
  303. zRequest->lese(&idLen, 1);
  304. char* id = new char[idLen + 1];
  305. zRequest->lese(id, idLen);
  306. id[(int)idLen] = 0;
  307. int processor;
  308. zRequest->lese((char*)&processor, 4);
  309. progressObservable.removeObserver(zSource, id, processor);
  310. break;
  311. }
  312. }
  313. }
  314. Framework::XML::Element*
  315. FireBasedProcessingBlockComponent::getTooltipUIML() const
  316. {
  317. if (currentRecipie)
  318. {
  319. Framework::XML::Element* result = new Framework::XML::Element(
  320. "<text width=\"auto\" height=\"auto\"></text>");
  321. Framework::Text content;
  322. content.append() << "Processing: ";
  323. int first = 1;
  324. for (const ItemInfo& output : currentRecipie->getOutput())
  325. {
  326. if (!first)
  327. {
  328. content.append() << ", ";
  329. }
  330. content.append()
  331. << output.count << " "
  332. << Game::INSTANCE->zItemType(output.type)->getTooltipUIML();
  333. first = 0;
  334. }
  335. content.append()
  336. << " ("
  337. << (int)((ticksNeeded / currentRecipie->getTicksNeeded()) * 100)
  338. << "%)";
  339. result->setText(content);
  340. }
  341. return 0;
  342. }
  343. bool FireBasedProcessingBlockComponent::isAllAvailable(
  344. Framework::RCArray<RecipieInput>& inputs)
  345. {
  346. return zBlock->isAllAvailable(inputs, inputInventorySlotName);
  347. }
  348. bool FireBasedProcessingBlockComponent::hasFreeSpace(
  349. const Item* zItem, int amount)
  350. {
  351. int addable = zBlock->numberOfAddableItems(
  352. zItem, NO_DIRECTION, inputInventorySlotName);
  353. return addable >= amount;
  354. }
  355. void FireBasedProcessingBlockComponent::consume(
  356. Framework::RCArray<RecipieInput>& inputs)
  357. {
  358. zBlock->consume(inputs, inputInventorySlotName);
  359. }
  360. void FireBasedProcessingBlockComponent::addCraftingResult(ItemStack* zStack)
  361. {
  362. TargetSlotNameItemFilter filter(outputInventorySlotName);
  363. zBlock->unsaveAddItem(zStack, NO_DIRECTION, &filter);
  364. }
  365. Framework::Vec3<float>
  366. FireBasedProcessingBlockComponent::getStorageLocation() const
  367. {
  368. return zBlock->getLocation();
  369. }
  370. int FireBasedProcessingBlockComponent::getStorageDimensionId() const
  371. {
  372. return zBlock->getDimensionId();
  373. }
  374. void FireBasedProcessingBlockComponent::loadComponent(
  375. Framework::StreamReader* zReader)
  376. {
  377. zReader->lese((char*)&ticksNeeded, 4);
  378. zReader->lese((char*)&maxFuelBuffer, 4);
  379. zReader->lese((char*)&fuelBuffer, 4);
  380. zReader->lese((char*)&burning, 1);
  381. int index = 0;
  382. zReader->lese((char*)&index, 4);
  383. if (index >= 0)
  384. {
  385. // TODO: add unique recipie ids to enshure correct loading after
  386. // recipies were changed, added or removed
  387. RecipieList* recipies
  388. = Game::INSTANCE->zRecipies()->zRecipieList(recipieGroup);
  389. if (recipies && index < recipies->getRecipieCount())
  390. {
  391. currentRecipie = recipies->zRecipie(index);
  392. }
  393. }
  394. }
  395. void FireBasedProcessingBlockComponent::saveComponent(
  396. Framework::StreamWriter* zWriter) const
  397. {
  398. zWriter->schreibe((char*)&ticksNeeded, 4);
  399. zWriter->schreibe((char*)&maxFuelBuffer, 4);
  400. zWriter->schreibe((char*)&fuelBuffer, 4);
  401. zWriter->schreibe((char*)&burning, 1);
  402. int index = -1;
  403. if (currentRecipie)
  404. {
  405. // TODO: add unique recipie ids to enshure correct loading after
  406. // recipies were changed, added or removed
  407. RecipieList* recipies
  408. = Game::INSTANCE->zRecipies()->zRecipieList(recipieGroup);
  409. if (recipies)
  410. {
  411. index = recipies->getRecipieIndex(currentRecipie);
  412. }
  413. }
  414. zWriter->schreibe((char*)&index, 4);
  415. }
  416. bool FireBasedProcessingBlockComponent::isLightSource() const
  417. {
  418. return 1;
  419. }
  420. int FireBasedProcessingBlockComponent::getLightColor() const
  421. {
  422. return burning ? burnLight : 0;
  423. }
  424. FireBasedProcessingBlockComponentFactory::
  425. FireBasedProcessingBlockComponentFactory()
  426. : SubTypeFactory()
  427. {}
  428. FireBasedProcessingBlockComponent*
  429. FireBasedProcessingBlockComponentFactory::fromJson(
  430. Framework::JSON::JSONObject* zJson) const
  431. {
  432. FireBasedProcessingBlockComponent* component
  433. = new FireBasedProcessingBlockComponent();
  434. if (zJson->hasValue("fireStartingItemFilter"))
  435. {
  436. component->fireStartingItemFilter
  437. = Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(
  438. zJson->zValue("fireStartingItemFilter"));
  439. }
  440. else
  441. {
  442. component->fireStartingItemFilter = new AnyItemFilter();
  443. }
  444. if (zJson->hasValue("fuelItemFilter"))
  445. {
  446. component->fuelItemFilter
  447. = Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(
  448. zJson->zValue("fuelItemFilter"));
  449. }
  450. else
  451. {
  452. component->fuelItemFilter = new AnyItemFilter();
  453. }
  454. component->recipieGroup
  455. = zJson->zValue("recipieGroup")->asString()->getString();
  456. component->fuelInventorySlotName
  457. = zJson->zValue("fuelInventorySlotName")->asString()->getString();
  458. component->fireStartingInventorySlotName
  459. = zJson->zValue("fireStartingInventorySlotName")
  460. ->asString()
  461. ->getString();
  462. component->inputInventorySlotName
  463. = zJson->zValue("inputInventorySlotName")->asString()->getString();
  464. component->outputInventorySlotName
  465. = zJson->zValue("outputInventorySlotName")->asString()->getString();
  466. component->burnLight
  467. = (int)zJson->zValue("lightColor")->asString()->getString();
  468. return component;
  469. }
  470. Framework::JSON::JSONObject*
  471. FireBasedProcessingBlockComponentFactory::toJsonObject(
  472. FireBasedProcessingBlockComponent* zObject) const
  473. {
  474. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  475. result->addValue("fireStartingItemFilter",
  476. Game::INSTANCE->zTypeRegistry()->toJson(
  477. zObject->fireStartingItemFilter));
  478. result->addValue("fuelItemFilter",
  479. Game::INSTANCE->zTypeRegistry()->toJson(zObject->fuelItemFilter));
  480. result->addValue("recipieGroup",
  481. new Framework::JSON::JSONString(zObject->recipieGroup.getText()));
  482. result->addValue("fuelInventorySlotName",
  483. new Framework::JSON::JSONString(
  484. zObject->fuelInventorySlotName.getText()));
  485. result->addValue("fireStartingInventorySlotName",
  486. new Framework::JSON::JSONString(
  487. zObject->fireStartingInventorySlotName.getText()));
  488. result->addValue("inputInventorySlotName",
  489. new Framework::JSON::JSONString(
  490. zObject->inputInventorySlotName.getText()));
  491. result->addValue("outputInventorySlotName",
  492. new Framework::JSON::JSONString(
  493. zObject->outputInventorySlotName.getText()));
  494. Framework::Text color = "0x";
  495. color.appendHex(zObject->burnLight);
  496. result->addValue("lightColor", new Framework::JSON::JSONString(color));
  497. return result;
  498. }
  499. JSONObjectValidationBuilder*
  500. FireBasedProcessingBlockComponentFactory::addToValidator(
  501. JSONObjectValidationBuilder* builder) const
  502. {
  503. return builder
  504. ->withRequiredAttribute("fireStartingItemFilter",
  505. Game::INSTANCE->zTypeRegistry()->getValidator<ItemFilter>())
  506. ->withRequiredAttribute("fuelItemFilter",
  507. Game::INSTANCE->zTypeRegistry()->getValidator<ItemFilter>())
  508. ->withRequiredString("recipieGroup")
  509. ->finishString()
  510. ->withRequiredString("fuelInventorySlotName")
  511. ->finishString()
  512. ->withRequiredString("fireStartingInventorySlotName")
  513. ->finishString()
  514. ->withRequiredString("inputInventorySlotName")
  515. ->finishString()
  516. ->withRequiredString("outputInventorySlotName")
  517. ->finishString()
  518. ->withRequiredString("lightColor")
  519. ->whichStartsWithMatch("0x")
  520. ->withDefault("0x0")
  521. ->finishString();
  522. return builder;
  523. }
  524. const char* FireBasedProcessingBlockComponentFactory::getTypeToken() const
  525. {
  526. return "fireBasedProcessing";
  527. }