CraftingStorage.cpp 16 KB

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