CraftingStorage.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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() >= inputAmount.get(h * width + w))
  111. item = s->zStack()->zItem();
  112. wrong |= (item && !f) || (!item && f);
  113. wrong |= item && f && !f->matchItem(item);
  114. if (wrong) break;
  115. }
  116. if (wrong) break;
  117. }
  118. if (!wrong)
  119. {
  120. int i = 0;
  121. for (ItemSlot* slot : craftingInput)
  122. {
  123. int w = i % this->width;
  124. int h = i / this->width;
  125. if ((w < x || w >= x + width || h < y || h >= y + height)
  126. && slot->zStack())
  127. return 0; // more items then needed are in crafting grid
  128. i++;
  129. }
  130. return 1;
  131. }
  132. }
  133. }
  134. return 0;
  135. }
  136. bool BasicShapedCrafter::isAllAvailable(
  137. Framework::RCArray<ItemFilter>& filters, Framework::Array<int>& inputAmount)
  138. {
  139. bool* used = new bool[craftingInput.getEintragAnzahl()];
  140. memset(used, 0, sizeof(bool) * craftingInput.getEintragAnzahl());
  141. int index = 0;
  142. for (ItemFilter* filter : filters)
  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() >= inputAmount.get(index)
  151. && slot->zStack()->zItem()
  152. && filter->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. index++;
  165. }
  166. delete[] used;
  167. return 1;
  168. }
  169. bool BasicShapedCrafter::hasFreeSpace(const Item* zItem, int amount)
  170. {
  171. ItemStack* stack
  172. = new ItemStack(zItem->zItemType()->cloneItem(zItem), amount);
  173. int addable = zInventory->numberOfAddableItems(stack, NO_DIRECTION);
  174. stack->release();
  175. return addable >= amount;
  176. }
  177. bool BasicShapedCrafter::consume(Framework::RCArray<ItemFilter>& filters,
  178. Framework::RCArray<ItemModifier>& modifiers,
  179. Framework::Array<int>& inputAmount,
  180. int width,
  181. int height)
  182. {
  183. int beginX = this->width;
  184. int beginY = this->height;
  185. SourceSlotBlacklistFilter otherSlotsSource;
  186. TargetSlotBlacklistFilter otherSlotsTarget;
  187. for (int i = 0; i < craftingInput.getEintragAnzahl(); i++)
  188. {
  189. if (!craftingInput.get(i)->isEmpty())
  190. {
  191. int x = i % this->width;
  192. int y = i / this->width;
  193. beginX = MIN(beginX, x);
  194. beginY = MIN(beginY, y);
  195. }
  196. otherSlotsSource.addBlackListSlotId(craftingInput.get(i)->getId());
  197. otherSlotsTarget.addBlackListSlotId(craftingInput.get(i)->getId());
  198. }
  199. for (int x = 0; x < width; x++)
  200. {
  201. for (int y = 0; y < height; y++)
  202. {
  203. ItemSlot* target
  204. = craftingInput.get((y + beginY) * this->width + x + beginX);
  205. ItemStack* stack = zInventory->takeItemsOut(
  206. target, target->getNumberOfItems(), INSIDE);
  207. if (stack)
  208. {
  209. if (stack->getSize()
  210. > inputAmount.get(y * width + x))
  211. {
  212. ItemStack* overflow = stack->split(
  213. stack->getSize()
  214. - inputAmount.get(y * width + x));
  215. zInventory->unsaveAddItem(
  216. overflow, INSIDE, &otherSlotsTarget);
  217. if (overflow->getSize() > 0)
  218. {
  219. // TODO: drop items
  220. }
  221. overflow->release();
  222. }
  223. if (stack->getSize() > 0 && stack->zItem())
  224. {
  225. ItemModifier* m = modifiers.z(y * width + x);
  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. stack->release();
  235. if (broken)
  236. {
  237. ItemStack* brokenStack
  238. = new ItemStack(broken, stack->getSize());
  239. zInventory->unsaveAddItem(
  240. brokenStack, INSIDE, &otherSlotsTarget);
  241. // TODO: if brokenStack is not empty spawn an item
  242. // entity
  243. brokenStack->release();
  244. }
  245. }
  246. else
  247. {
  248. zInventory->addItems(target, stack, INSIDE);
  249. // TODO: if stack is not empty spawn an item entity
  250. }
  251. }
  252. ItemFilter* f = filters.z(y * width + x);
  253. if (f)
  254. {
  255. if (target->isEmpty())
  256. {
  257. Framework::Array<ItemSlot*> tmp;
  258. tmp.add(target);
  259. CombinedItemFilter combinedFilter(
  260. dynamic_cast<ItemFilter*>(f->getThis()),
  261. dynamic_cast<ItemFilter*>(otherSlotsSource.getThis()),
  262. [](bool a, bool b) { return a && b; });
  263. zInventory->localTransaction(
  264. 0, &tmp, &combinedFilter, 1, NO_DIRECTION, INSIDE);
  265. }
  266. }
  267. }
  268. }
  269. return 1;
  270. }
  271. bool BasicShapedCrafter::consume(Framework::RCArray<ItemFilter>& filters,
  272. Framework::RCArray<ItemModifier>& modifiers,
  273. Framework::Array<int>& inputAmount)
  274. {
  275. SourceSlotBlacklistFilter otherSlotsSource;
  276. TargetSlotBlacklistFilter otherSlotsTarget;
  277. for (int i = 0; i < craftingInput.getEintragAnzahl(); i++)
  278. {
  279. otherSlotsSource.addBlackListSlotId(craftingInput.get(i)->getId());
  280. otherSlotsTarget.addBlackListSlotId(craftingInput.get(i)->getId());
  281. }
  282. bool* used = new bool[craftingInput.getEintragAnzahl()];
  283. memset(used, 0, sizeof(bool) * craftingInput.getEintragAnzahl());
  284. for (int i = 0; i < filters.getEintragAnzahl(); i++)
  285. {
  286. for (int j = 0; j < craftingInput.getEintragAnzahl(); j++)
  287. {
  288. if (used[j]) continue;
  289. ItemSlot* target = craftingInput.get(j);
  290. if (target && target->zStack()
  291. && target->zStack()->getSize() >= inputAmount.get(i)
  292. && target->zStack()->zItem()
  293. && filters.z(i)->matchItem(target->zStack()->zItem()))
  294. {
  295. used[i] = 1;
  296. ItemStack* stack = zInventory->takeItemsOut(
  297. target, target->getNumberOfItems(), INSIDE);
  298. if (stack)
  299. {
  300. if (stack->getSize() > inputAmount.get(i))
  301. {
  302. ItemStack* overflow = stack->split(
  303. stack->getSize() - inputAmount.get(i));
  304. zInventory->unsaveAddItem(
  305. overflow, INSIDE, &otherSlotsTarget);
  306. if (overflow->getSize() > 0)
  307. {
  308. // TODO: drop items
  309. }
  310. overflow->release();
  311. }
  312. if (stack->getSize() > 0 && stack->zItem())
  313. {
  314. ItemModifier* m = modifiers.z(i);
  315. if (m) m->applyOn((Item*)stack->zItem());
  316. }
  317. if (stack->zItem()->getHp() == 0)
  318. stack->release();
  319. else if (stack->zItem()->getDurability() == 0)
  320. {
  321. Item* broken = stack->zItem()->zItemType()->breakItem(
  322. stack->zItem());
  323. stack->release();
  324. if (broken)
  325. {
  326. ItemStack* brokenStack
  327. = new ItemStack(broken, stack->getSize());
  328. zInventory->unsaveAddItem(
  329. brokenStack, INSIDE, &otherSlotsTarget);
  330. // TODO: if brokenStack is not empty spawn an item
  331. // entity
  332. brokenStack->release();
  333. }
  334. }
  335. else
  336. {
  337. zInventory->addItems(target, stack, INSIDE);
  338. // TODO: if stack is not empty spawn an item entity
  339. }
  340. }
  341. ItemFilter* f = filters.z(i);
  342. if (f)
  343. {
  344. if (target->isEmpty())
  345. {
  346. Framework::Array<ItemSlot*> tmp;
  347. tmp.add(target);
  348. CombinedItemFilter combinedFilter(
  349. dynamic_cast<ItemFilter*>(f->getThis()),
  350. dynamic_cast<ItemFilter*>(
  351. otherSlotsSource.getThis()),
  352. [](bool a, bool b) { return a && b; });
  353. zInventory->localTransaction(
  354. 0, &tmp, &combinedFilter, 1, NO_DIRECTION, INSIDE);
  355. }
  356. }
  357. break;
  358. }
  359. }
  360. }
  361. delete[] used;
  362. return 1;
  363. }
  364. void BasicShapedCrafter::addCraftingResult(ItemStack* stack)
  365. {
  366. TargetSlotBlacklistFilter otherSlotsTarget;
  367. for (int i = 0; i < craftingInput.getEintragAnzahl(); i++)
  368. otherSlotsTarget.addBlackListSlotId(craftingInput.get(i)->getId());
  369. zInventory->unsaveAddItem(stack, NO_DIRECTION, &otherSlotsTarget);
  370. }
  371. void BasicShapedCrafter::applyCurrentRecipie()
  372. {
  373. if (currentRecipie && currentRecipie->testApplicability(this))
  374. currentRecipie->apply(this);
  375. }
  376. void BasicShapedCrafter::calculateOutputPreview()
  377. {
  378. RecipieList* recipies
  379. = Game::INSTANCE->getRecipies().zRecipieList(recipieList);
  380. if (recipies)
  381. {
  382. Recipie* recipie = recipies->zFirstRecipie(this);
  383. if (recipie != currentRecipie)
  384. {
  385. currentRecipie = recipie;
  386. NetworkMessage* message = new NetworkMessage();
  387. getOutputPreview(message);
  388. zInventory->notifyObservers(message);
  389. }
  390. }
  391. }