FireBasedProcessingBlockComponent.cpp 17 KB

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