FireBasedProcessingBlockComponent.cpp 17 KB

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