FireBasedProcessingBlockComponent.cpp 17 KB

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