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