CraftingStorage.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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->addressGui(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(this);
  56. Framework::InMemoryBuffer buffer;
  57. int count = 0;
  58. for (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(Framework::RCArray<ItemFilter>& filters,
  92. Framework::Array<int>& inputAmount,
  93. int width,
  94. 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. ItemFilter* f = filters.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()
  111. >= inputAmount.get(h * width + w))
  112. item = s->zStack()->zItem();
  113. wrong |= (item && !f) || (!item && f);
  114. wrong |= item && f && !f->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<ItemFilter>& filters, Framework::Array<int>& inputAmount)
  139. {
  140. bool* used = new bool[craftingInput.getEintragAnzahl()];
  141. memset(used, 0, sizeof(bool) * craftingInput.getEintragAnzahl());
  142. int index = 0;
  143. for (ItemFilter* filter : filters)
  144. {
  145. bool found = 0;
  146. for (int i = 0; i < craftingInput.getEintragAnzahl(); i++)
  147. {
  148. if (used[i]) continue;
  149. ItemSlot* slot = craftingInput.get(i);
  150. if (slot && slot->zStack()
  151. && slot->zStack()->getSize() >= inputAmount.get(index)
  152. && slot->zStack()->zItem()
  153. && filter->matchItem(slot->zStack()->zItem()))
  154. {
  155. found = 1;
  156. used[i] = 1;
  157. break;
  158. }
  159. }
  160. if (!found)
  161. {
  162. delete[] used;
  163. return 0;
  164. }
  165. index++;
  166. }
  167. delete[] used;
  168. return 1;
  169. }
  170. bool BasicShapedCrafter::hasFreeSpace(const Item* zItem, int amount)
  171. {
  172. ItemStack* stack
  173. = new ItemStack(zItem->zItemType()->cloneItem(zItem), amount);
  174. int addable = zInventory->numberOfAddableItems(stack, NO_DIRECTION);
  175. stack->release();
  176. return addable >= amount;
  177. }
  178. bool BasicShapedCrafter::consume(Framework::RCArray<ItemFilter>& filters,
  179. Framework::RCArray<ItemModifier>& modifiers,
  180. Framework::Array<int>& inputAmount,
  181. int width,
  182. int height)
  183. {
  184. int beginX = this->width;
  185. int beginY = this->height;
  186. SourceSlotBlacklistFilter otherSlotsSource;
  187. TargetSlotBlacklistFilter otherSlotsTarget;
  188. for (int i = 0; i < craftingInput.getEintragAnzahl(); i++)
  189. {
  190. if (!craftingInput.get(i)->isEmpty())
  191. {
  192. int x = i % this->width;
  193. int y = i / this->width;
  194. beginX = MIN(beginX, x);
  195. beginY = MIN(beginY, y);
  196. }
  197. otherSlotsSource.addBlackListSlotId(craftingInput.get(i)->getId());
  198. otherSlotsTarget.addBlackListSlotId(craftingInput.get(i)->getId());
  199. }
  200. for (int x = 0; x < width; x++)
  201. {
  202. for (int y = 0; y < height; y++)
  203. {
  204. ItemSlot* target
  205. = craftingInput.get((y + beginY) * this->width + x + beginX);
  206. ItemStack* stack = zInventory->takeItemsOut(
  207. target, target->getNumberOfItems(), INSIDE);
  208. if (stack)
  209. {
  210. if (stack->getSize() > inputAmount.get(y * width + x))
  211. {
  212. ItemStack* overflow = stack->split(
  213. stack->getSize() - inputAmount.get(y * width + x));
  214. zInventory->unsaveAddItem(
  215. overflow, INSIDE, &otherSlotsTarget);
  216. if (overflow->getSize() > 0)
  217. {
  218. Game::INSTANCE->spawnItem(zInventory->getLocation(),
  219. zInventory->getDimensionId(),
  220. overflow);
  221. }
  222. else
  223. {
  224. overflow->release();
  225. }
  226. }
  227. if (stack->getSize() > 0 && stack->zItem())
  228. {
  229. ItemModifier* m = modifiers.z(y * width + x);
  230. if (m) m->applyOn((Item*)stack->zItem());
  231. }
  232. if (stack->zItem()->getHp() == 0)
  233. stack->release();
  234. else if (stack->zItem()->getDurability() == 0)
  235. {
  236. Item* broken = stack->zItem()->zItemType()->breakItem(
  237. stack->zItem());
  238. if (broken)
  239. {
  240. ItemStack* brokenStack
  241. = new ItemStack(broken, stack->getSize());
  242. zInventory->unsaveAddItem(
  243. brokenStack, INSIDE, &otherSlotsTarget);
  244. if (brokenStack->getSize() > 0)
  245. {
  246. Game::INSTANCE->spawnItem(zInventory->getLocation(),
  247. zInventory->getDimensionId(),
  248. brokenStack);
  249. }
  250. else
  251. {
  252. brokenStack->release();
  253. }
  254. }
  255. stack->release();
  256. }
  257. else
  258. {
  259. zInventory->addItems(target, stack, INSIDE);
  260. if (stack->getSize() > 0)
  261. {
  262. Game::INSTANCE->spawnItem(zInventory->getLocation(),
  263. zInventory->getDimensionId(),
  264. stack);
  265. }
  266. else
  267. {
  268. stack->release();
  269. }
  270. }
  271. }
  272. ItemFilter* f = filters.z(y * width + x);
  273. if (f)
  274. {
  275. if (target->isEmpty())
  276. {
  277. Framework::Array<ItemSlot*> tmp;
  278. tmp.add(target);
  279. CombinedItemFilter combinedFilter(
  280. dynamic_cast<ItemFilter*>(f->getThis()),
  281. dynamic_cast<ItemFilter*>(otherSlotsSource.getThis()),
  282. [](bool a, bool b) { return a && b; });
  283. zInventory->localTransaction(
  284. 0, &tmp, &combinedFilter, 1, NO_DIRECTION, INSIDE);
  285. }
  286. }
  287. }
  288. }
  289. return 1;
  290. }
  291. bool BasicShapedCrafter::consume(Framework::RCArray<ItemFilter>& filters,
  292. Framework::RCArray<ItemModifier>& modifiers,
  293. Framework::Array<int>& inputAmount)
  294. {
  295. SourceSlotBlacklistFilter otherSlotsSource;
  296. TargetSlotBlacklistFilter otherSlotsTarget;
  297. for (int i = 0; i < craftingInput.getEintragAnzahl(); i++)
  298. {
  299. otherSlotsSource.addBlackListSlotId(craftingInput.get(i)->getId());
  300. otherSlotsTarget.addBlackListSlotId(craftingInput.get(i)->getId());
  301. }
  302. bool* used = new bool[craftingInput.getEintragAnzahl()];
  303. memset(used, 0, sizeof(bool) * craftingInput.getEintragAnzahl());
  304. for (int i = 0; i < filters.getEintragAnzahl(); i++)
  305. {
  306. for (int j = 0; j < craftingInput.getEintragAnzahl(); j++)
  307. {
  308. if (used[j]) continue;
  309. ItemSlot* target = craftingInput.get(j);
  310. if (target && target->zStack()
  311. && target->zStack()->getSize() >= inputAmount.get(i)
  312. && target->zStack()->zItem()
  313. && filters.z(i)->matchItem(target->zStack()->zItem()))
  314. {
  315. used[i] = 1;
  316. ItemStack* stack = zInventory->takeItemsOut(
  317. target, target->getNumberOfItems(), INSIDE);
  318. if (stack)
  319. {
  320. if (stack->getSize() > inputAmount.get(i))
  321. {
  322. ItemStack* overflow = stack->split(
  323. stack->getSize() - inputAmount.get(i));
  324. zInventory->unsaveAddItem(
  325. overflow, INSIDE, &otherSlotsTarget);
  326. if (overflow->getSize() > 0)
  327. {
  328. Game::INSTANCE->spawnItem(zInventory->getLocation(),
  329. zInventory->getDimensionId(),
  330. overflow);
  331. }
  332. else
  333. {
  334. overflow->release();
  335. }
  336. }
  337. if (stack->getSize() > 0 && stack->zItem())
  338. {
  339. ItemModifier* m = modifiers.z(i);
  340. if (m) m->applyOn((Item*)stack->zItem());
  341. }
  342. if (stack->zItem()->getHp() == 0)
  343. stack->release();
  344. else if (stack->zItem()->getDurability() == 0)
  345. {
  346. Item* broken = stack->zItem()->zItemType()->breakItem(
  347. stack->zItem());
  348. if (broken)
  349. {
  350. ItemStack* brokenStack
  351. = new ItemStack(broken, stack->getSize());
  352. zInventory->unsaveAddItem(
  353. brokenStack, INSIDE, &otherSlotsTarget);
  354. if (brokenStack->getSize() > 0)
  355. {
  356. Game::INSTANCE->spawnItem(
  357. zInventory->getLocation(),
  358. zInventory->getDimensionId(),
  359. brokenStack);
  360. }
  361. else
  362. {
  363. brokenStack->release();
  364. }
  365. }
  366. stack->release();
  367. }
  368. else
  369. {
  370. zInventory->addItems(target, stack, INSIDE);
  371. if (stack->getSize() > 0)
  372. {
  373. Game::INSTANCE->spawnItem(zInventory->getLocation(),
  374. zInventory->getDimensionId(),
  375. stack);
  376. }
  377. else
  378. {
  379. stack->release();
  380. }
  381. }
  382. }
  383. ItemFilter* f = filters.z(i);
  384. if (f)
  385. {
  386. if (target->isEmpty())
  387. {
  388. Framework::Array<ItemSlot*> tmp;
  389. tmp.add(target);
  390. CombinedItemFilter combinedFilter(
  391. dynamic_cast<ItemFilter*>(f->getThis()),
  392. dynamic_cast<ItemFilter*>(
  393. otherSlotsSource.getThis()),
  394. [](bool a, bool b) { return a && b; });
  395. zInventory->localTransaction(
  396. 0, &tmp, &combinedFilter, 1, NO_DIRECTION, INSIDE);
  397. }
  398. }
  399. break;
  400. }
  401. }
  402. }
  403. delete[] used;
  404. return 1;
  405. }
  406. void BasicShapedCrafter::addCraftingResult(ItemStack* stack)
  407. {
  408. TargetSlotBlacklistFilter otherSlotsTarget;
  409. for (int i = 0; i < craftingInput.getEintragAnzahl(); i++)
  410. otherSlotsTarget.addBlackListSlotId(craftingInput.get(i)->getId());
  411. zInventory->unsaveAddItem(stack, NO_DIRECTION, &otherSlotsTarget);
  412. }
  413. void BasicShapedCrafter::applyCurrentRecipie()
  414. {
  415. if (currentRecipie && currentRecipie->testApplicability(this))
  416. currentRecipie->apply(this);
  417. }
  418. void BasicShapedCrafter::calculateOutputPreview()
  419. {
  420. RecipieList* recipies
  421. = Game::INSTANCE->getRecipies().zRecipieList(recipieList);
  422. if (recipies)
  423. {
  424. Recipie* recipie = recipies->zFirstRecipie(this);
  425. if (recipie != currentRecipie)
  426. {
  427. currentRecipie = recipie;
  428. NetworkMessage* message = new NetworkMessage();
  429. getOutputPreview(message);
  430. zInventory->notifyObservers(message);
  431. }
  432. }
  433. }