CraftingStorage.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #include "CraftingStorage.h"
  2. #include <InMemoryBuffer.h>
  3. #include "Game.h"
  4. #include "Inventory.h"
  5. #include "Item.h"
  6. #include "RecipieList.h"
  7. #include "RecipieLoader.h"
  8. #undef min
  9. #undef max
  10. BasicShapedCrafter::BasicShapedCrafter(
  11. int width, int height, Inventory* zInventory, Framework::Text recipieList)
  12. : zInventory(zInventory),
  13. currentRecipie(0),
  14. recipieList(recipieList),
  15. width(width),
  16. height(height)
  17. {
  18. for (int i = 0; i < width * height; i++)
  19. {
  20. ItemSlot* slot = new ItemSlot("CraftingGrid",
  21. 1,
  22. std::numeric_limits<int>::max(),
  23. std::numeric_limits<int>::max(),
  24. INSIDE,
  25. INSIDE,
  26. 0);
  27. zInventory->addSlot(slot);
  28. craftingInput.add(slot);
  29. }
  30. std::function<void(
  31. ItemSlot * zSlot, Direction dir, const Item* zItem, int count)>
  32. onChange
  33. = [this, recipieList](
  34. ItemSlot* zSlot, Direction dir, const Item* zItem, int count) {
  35. if (!zSlot->getName().istGleich("CraftingGrid")) return;
  36. calculateOutputPreview();
  37. };
  38. zInventory->registerAfterPullStackCall(onChange);
  39. zInventory->registerAfterPushStackCall(onChange);
  40. zInventory->registerObserverAddedCall(
  41. [this](Entity* zSource, Framework::Text id, int processor) {
  42. Recipie* old = currentRecipie;
  43. calculateOutputPreview();
  44. if (old == currentRecipie)
  45. {
  46. NetworkMessage* message = new NetworkMessage();
  47. getOutputPreview(message);
  48. message->addressUIElement(id, processor);
  49. Game::INSTANCE->sendMessage(message, zSource);
  50. }
  51. });
  52. }
  53. void BasicShapedCrafter::getOutputPreview(NetworkMessage* zMessage)
  54. {
  55. if (currentRecipie)
  56. {
  57. Framework::Array<ItemInfo> output = currentRecipie->getOutput();
  58. Framework::InMemoryBuffer buffer;
  59. int count = 0;
  60. for (const ItemInfo& slot : output)
  61. {
  62. count++;
  63. int itemCount = slot.count;
  64. buffer.schreibe((char*)&itemCount, 4);
  65. if (itemCount > 0)
  66. {
  67. float f = slot.hp;
  68. buffer.schreibe((char*)&f, 4);
  69. f = slot.maxHp;
  70. buffer.schreibe((char*)&f, 4);
  71. f = slot.durability;
  72. buffer.schreibe((char*)&f, 4);
  73. f = slot.maxDurability;
  74. buffer.schreibe((char*)&f, 4);
  75. int id = slot.type;
  76. buffer.schreibe((char*)&id, 4);
  77. }
  78. }
  79. char* msg = new char[5 + buffer.getSize()];
  80. msg[0] = 100; // set crafting result
  81. *(int*)(msg + 1) = count;
  82. buffer.lese(msg + 5, (int)buffer.getSize());
  83. zMessage->setMessage(msg, 5 + (int)buffer.getSize());
  84. }
  85. else
  86. {
  87. char* msg = new char[5];
  88. msg[0] = 100; // set crafting result
  89. *(int*)(msg + 1) = 0;
  90. zMessage->setMessage(msg, 5);
  91. }
  92. }
  93. bool BasicShapedCrafter::isAllAvailable(
  94. Framework::RCArray<RecipieInput>& inputs, int width, int height)
  95. {
  96. for (int x = 0; x <= this->width - width; x++)
  97. {
  98. for (int y = 0; y <= this->height - height; y++)
  99. {
  100. bool wrong = 0;
  101. for (int w = 0; w < width; w++)
  102. {
  103. for (int h = 0; h < height; h++)
  104. {
  105. RecipieInput* i = inputs.z(h * width + w);
  106. ItemSlot* s
  107. = craftingInput.get((h + y) * this->width + (x + w));
  108. const Item* item = 0;
  109. if (s && s->zStack()
  110. && s->zStack()->getSize() >= i->getAmount())
  111. item = s->zStack()->zItem();
  112. wrong |= (item && !i->zFilter()) || (!item && i->zFilter());
  113. wrong |= item && i->zFilter()
  114. && !i->zFilter()->matchItem(item);
  115. if (wrong) break;
  116. }
  117. if (wrong) break;
  118. }
  119. if (!wrong)
  120. {
  121. int i = 0;
  122. for (ItemSlot* slot : craftingInput)
  123. {
  124. int w = i % this->width;
  125. int h = i / this->width;
  126. if ((w < x || w >= x + width || h < y || h >= y + height)
  127. && slot->zStack())
  128. return 0; // more items then needed are in crafting grid
  129. i++;
  130. }
  131. return 1;
  132. }
  133. }
  134. }
  135. return 0;
  136. }
  137. bool BasicShapedCrafter::isAllAvailable(
  138. Framework::RCArray<RecipieInput>& inputs)
  139. {
  140. return zInventory->isAllAvailable(inputs, "CraftingGrid");
  141. }
  142. bool BasicShapedCrafter::hasFreeSpace(const Item* zItem, int amount)
  143. {
  144. int addable = zInventory->numberOfAddableItems(zItem, NO_DIRECTION);
  145. return addable >= amount;
  146. }
  147. bool BasicShapedCrafter::consume(
  148. Framework::RCArray<RecipieInput>& inputs, int width, int height)
  149. {
  150. int beginX = this->width;
  151. int beginY = this->height;
  152. SourceSlotBlacklistFilter otherSlotsSource;
  153. TargetSlotBlacklistFilter otherSlotsTarget;
  154. for (int i = 0; i < craftingInput.getEintragAnzahl(); i++)
  155. {
  156. if (!craftingInput.get(i)->isEmpty())
  157. {
  158. int x = i % this->width;
  159. int y = i / this->width;
  160. beginX = MIN(beginX, x);
  161. beginY = MIN(beginY, y);
  162. }
  163. otherSlotsSource.addBlackListSlotId(craftingInput.get(i)->getId());
  164. otherSlotsTarget.addBlackListSlotId(craftingInput.get(i)->getId());
  165. }
  166. for (int x = 0; x < width; x++)
  167. {
  168. for (int y = 0; y < height; y++)
  169. {
  170. ItemSlot* target
  171. = craftingInput.get((y + beginY) * this->width + x + beginX);
  172. ItemStack* stack = zInventory->takeItemsOut(
  173. target, target->getNumberOfItems(), INSIDE);
  174. if (stack)
  175. {
  176. if (stack->getSize() > inputs.z(y * width + x)->getAmount())
  177. {
  178. ItemStack* overflow
  179. = stack->split(stack->getSize()
  180. - inputs.z(y * width + x)->getAmount());
  181. zInventory->unsaveAddItem(
  182. overflow, INSIDE, &otherSlotsTarget);
  183. if (overflow->getSize() > 0)
  184. {
  185. Game::INSTANCE->spawnItem(zInventory->getLocation(),
  186. zInventory->getDimensionId(),
  187. overflow);
  188. }
  189. else
  190. {
  191. overflow->release();
  192. }
  193. }
  194. if (stack->getSize() > 0 && stack->zItem())
  195. {
  196. ItemModifier* m = inputs.z(y * width + x)->zModifier();
  197. if (m) m->applyOn((Item*)stack->zItem());
  198. }
  199. if (stack->zItem()->getHp() == 0)
  200. stack->release();
  201. else if (stack->zItem()->getDurability() == 0)
  202. {
  203. Item* broken = stack->zItem()->zItemType()->breakItem(
  204. stack->zItem());
  205. if (broken)
  206. {
  207. ItemStack* brokenStack
  208. = new ItemStack(broken, stack->getSize());
  209. zInventory->unsaveAddItem(
  210. brokenStack, INSIDE, &otherSlotsTarget);
  211. if (brokenStack->getSize() > 0)
  212. {
  213. Game::INSTANCE->spawnItem(zInventory->getLocation(),
  214. zInventory->getDimensionId(),
  215. brokenStack);
  216. }
  217. else
  218. {
  219. brokenStack->release();
  220. }
  221. }
  222. stack->release();
  223. }
  224. else
  225. {
  226. zInventory->addItems(target, stack, INSIDE);
  227. if (stack->getSize() > 0)
  228. {
  229. Game::INSTANCE->spawnItem(zInventory->getLocation(),
  230. zInventory->getDimensionId(),
  231. stack);
  232. }
  233. else
  234. {
  235. stack->release();
  236. }
  237. }
  238. }
  239. ItemFilter* f = inputs.z(y * width + x)->zFilter();
  240. if (f)
  241. {
  242. if (target->isEmpty())
  243. {
  244. Framework::RCArray<ItemFilter> filters;
  245. filters.add(dynamic_cast<ItemFilter*>(f->getThis()));
  246. filters.add(
  247. dynamic_cast<ItemFilter*>(otherSlotsSource.getThis()));
  248. Framework::Array<ItemSlot*> tmp;
  249. tmp.add(target);
  250. CombinedItemFilter combinedFilter(
  251. filters, [](bool a, bool b) { return a && b; });
  252. zInventory->localTransaction(
  253. 0, &tmp, &combinedFilter, 1, NO_DIRECTION, INSIDE);
  254. }
  255. }
  256. }
  257. }
  258. return 1;
  259. }
  260. void BasicShapedCrafter::consume(Framework::RCArray<RecipieInput>& inputs)
  261. {
  262. SourceSlotBlacklistFilter otherSlotsSource;
  263. TargetSlotBlacklistFilter otherSlotsTarget;
  264. for (int i = 0; i < craftingInput.getEintragAnzahl(); i++)
  265. {
  266. otherSlotsSource.addBlackListSlotId(craftingInput.get(i)->getId());
  267. otherSlotsTarget.addBlackListSlotId(craftingInput.get(i)->getId());
  268. }
  269. bool* used = new bool[craftingInput.getEintragAnzahl()];
  270. memset(used, 0, sizeof(bool) * craftingInput.getEintragAnzahl());
  271. for (int i = 0; i < inputs.getEintragAnzahl(); i++)
  272. {
  273. for (int j = 0; j < craftingInput.getEintragAnzahl(); j++)
  274. {
  275. if (used[j]) continue;
  276. ItemSlot* target = craftingInput.get(j);
  277. if (target && target->zStack()
  278. && target->zStack()->getSize() >= inputs.z(i)->getAmount()
  279. && target->zStack()->zItem()
  280. && inputs.z(i)->zFilter()->matchItem(target->zStack()->zItem()))
  281. {
  282. used[i] = 1;
  283. ItemStack* stack = zInventory->takeItemsOut(
  284. target, target->getNumberOfItems(), INSIDE);
  285. if (stack)
  286. {
  287. if (stack->getSize() > inputs.z(i)->getAmount())
  288. {
  289. ItemStack* overflow = stack->split(
  290. stack->getSize() - inputs.z(i)->getAmount());
  291. zInventory->unsaveAddItem(
  292. overflow, INSIDE, &otherSlotsTarget);
  293. if (overflow->getSize() > 0)
  294. {
  295. Game::INSTANCE->spawnItem(zInventory->getLocation(),
  296. zInventory->getDimensionId(),
  297. overflow);
  298. }
  299. else
  300. {
  301. overflow->release();
  302. }
  303. }
  304. if (stack->getSize() > 0 && stack->zItem())
  305. {
  306. ItemModifier* m = inputs.z(i)->zModifier();
  307. if (m) m->applyOn((Item*)stack->zItem());
  308. }
  309. if (stack->zItem()->getHp() == 0)
  310. stack->release();
  311. else if (stack->zItem()->getDurability() == 0)
  312. {
  313. Item* broken = stack->zItem()->zItemType()->breakItem(
  314. stack->zItem());
  315. if (broken)
  316. {
  317. ItemStack* brokenStack
  318. = new ItemStack(broken, stack->getSize());
  319. zInventory->unsaveAddItem(
  320. brokenStack, INSIDE, &otherSlotsTarget);
  321. if (brokenStack->getSize() > 0)
  322. {
  323. Game::INSTANCE->spawnItem(
  324. zInventory->getLocation(),
  325. zInventory->getDimensionId(),
  326. brokenStack);
  327. }
  328. else
  329. {
  330. brokenStack->release();
  331. }
  332. }
  333. stack->release();
  334. }
  335. else
  336. {
  337. zInventory->addItems(target, stack, INSIDE);
  338. if (stack->getSize() > 0)
  339. {
  340. Game::INSTANCE->spawnItem(zInventory->getLocation(),
  341. zInventory->getDimensionId(),
  342. stack);
  343. }
  344. else
  345. {
  346. stack->release();
  347. }
  348. }
  349. }
  350. ItemFilter* f = inputs.z(i)->zFilter();
  351. if (f)
  352. {
  353. if (target->isEmpty())
  354. {
  355. Framework::RCArray<ItemFilter> filters;
  356. filters.add(dynamic_cast<ItemFilter*>(f->getThis()));
  357. filters.add(dynamic_cast<ItemFilter*>(
  358. otherSlotsSource.getThis()));
  359. Framework::Array<ItemSlot*> tmp;
  360. tmp.add(target);
  361. CombinedItemFilter combinedFilter(
  362. filters, [](bool a, bool b) { return a && b; });
  363. zInventory->localTransaction(
  364. 0, &tmp, &combinedFilter, 1, NO_DIRECTION, INSIDE);
  365. }
  366. }
  367. break;
  368. }
  369. }
  370. }
  371. delete[] used;
  372. }
  373. void BasicShapedCrafter::addCraftingResult(ItemStack* zStack)
  374. {
  375. TargetSlotBlacklistFilter otherSlotsTarget;
  376. for (int i = 0; i < craftingInput.getEintragAnzahl(); i++)
  377. otherSlotsTarget.addBlackListSlotId(craftingInput.get(i)->getId());
  378. zInventory->unsaveAddItem(zStack, NO_DIRECTION, &otherSlotsTarget);
  379. }
  380. void BasicShapedCrafter::applyCurrentRecipie()
  381. {
  382. if (currentRecipie && currentRecipie->testApplicability(this))
  383. currentRecipie->apply(this);
  384. }
  385. void BasicShapedCrafter::calculateOutputPreview()
  386. {
  387. RecipieList* recipies
  388. = Game::INSTANCE->zRecipies()->zRecipieList(recipieList);
  389. if (recipies)
  390. {
  391. Recipie* recipie = recipies->zFirstRecipie(this);
  392. if (recipie != currentRecipie)
  393. {
  394. currentRecipie = recipie;
  395. NetworkMessage* message = new NetworkMessage();
  396. getOutputPreview(message);
  397. zInventory->notifyObservers(message);
  398. }
  399. }
  400. }
  401. Framework::Vec3<float> BasicShapedCrafter::getStorageLocation() const
  402. {
  403. return zInventory->getLocation();
  404. }
  405. int BasicShapedCrafter::getStorageDimensionId() const
  406. {
  407. return zInventory->getDimensionId();
  408. }