CraftingStorage.cpp 16 KB

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