Inventory.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. #include "Inventory.h"
  2. #include <InMemoryBuffer.h>
  3. #include "Area.h"
  4. #include "Constants.h"
  5. #include "Entity.h"
  6. #include "Game.h"
  7. #include "ItemFilter.h"
  8. #include "ItemSlot.h"
  9. #include "ItemStack.h"
  10. #include "ItemType.h"
  11. #include "NetworkMessage.h"
  12. #include "UIMLBuilder.h"
  13. using namespace Framework;
  14. InventoryInteraction::InventoryInteraction(
  15. Inventory* current, Inventory* other, Direction dir)
  16. : current(current),
  17. other(other),
  18. dir(dir)
  19. {
  20. lock();
  21. }
  22. InventoryInteraction::InventoryInteraction(
  23. const InventoryInteraction& interaction)
  24. : InventoryInteraction(
  25. interaction.current, interaction.other, interaction.dir)
  26. {}
  27. InventoryInteraction::~InventoryInteraction()
  28. {
  29. unlock();
  30. }
  31. void InventoryInteraction::lock()
  32. {
  33. if (!current || !other) return;
  34. if (current->location.x < other->location.x)
  35. {
  36. current->cs.lock();
  37. other->cs.lock();
  38. return;
  39. }
  40. else if (current->location.x == other->location.x)
  41. {
  42. if (current->location.y < other->location.y)
  43. {
  44. current->cs.lock();
  45. other->cs.lock();
  46. return;
  47. }
  48. else if (current->location.y == other->location.y)
  49. {
  50. if (current->location.z < other->location.z)
  51. {
  52. current->cs.lock();
  53. other->cs.lock();
  54. return;
  55. }
  56. }
  57. }
  58. other->cs.lock();
  59. current->cs.lock();
  60. }
  61. void InventoryInteraction::unlock()
  62. {
  63. if (!current || !other) return;
  64. if (current->location.x < other->location.x)
  65. {
  66. current->cs.unlock();
  67. other->cs.unlock();
  68. return;
  69. }
  70. else if (current->location.x == other->location.x)
  71. {
  72. if (current->location.y < other->location.y)
  73. {
  74. current->cs.unlock();
  75. other->cs.unlock();
  76. return;
  77. }
  78. else if (current->location.y == other->location.y)
  79. {
  80. if (current->location.z < other->location.z)
  81. {
  82. current->cs.unlock();
  83. other->cs.unlock();
  84. return;
  85. }
  86. }
  87. }
  88. other->cs.unlock();
  89. current->cs.unlock();
  90. }
  91. void InventoryInteraction::transaction(Inventory* zSource,
  92. Inventory* zTarget,
  93. ItemFilter* zFilter,
  94. Direction sourceView,
  95. Direction targetView,
  96. int count)
  97. {
  98. for (auto sourceSlot = zSource->pullSlotsOrder->begin(); sourceSlot;)
  99. {
  100. while (sourceSlot
  101. && (sourceSlot->getNumberOfItems() == 0
  102. || (zFilter && !zFilter->matchSourceSlot(sourceSlot))))
  103. sourceSlot++;
  104. if (!sourceSlot) break;
  105. // TODO: use target cache ot get list of slots that already contains the
  106. // source item
  107. bool needNext = 1;
  108. for (auto targetSlot = zTarget->pushSlotsOrder->begin(); targetSlot;)
  109. {
  110. while (targetSlot
  111. && (targetSlot->isFull()
  112. || (zFilter && !zFilter->matchTargetSlot(targetSlot))))
  113. targetSlot++;
  114. if (!targetSlot) break;
  115. needNext &= !Inventory::unsafeMove(zSource,
  116. zTarget,
  117. sourceSlot,
  118. targetSlot,
  119. sourceView,
  120. targetView,
  121. count);
  122. if (count == 0) return;
  123. if (sourceSlot->getNumberOfItems() == 0) break;
  124. }
  125. if (needNext) sourceSlot++;
  126. }
  127. }
  128. InventoryInteraction& InventoryInteraction::operator=(
  129. const InventoryInteraction& data)
  130. {
  131. if (&data == this) return *this;
  132. unlock();
  133. current = data.current;
  134. other = data.other;
  135. dir = data.dir;
  136. lock();
  137. return *this;
  138. }
  139. void InventoryInteraction::endInteraction()
  140. {
  141. unlock();
  142. current = 0;
  143. other = 0;
  144. }
  145. void InventoryInteraction::pullItems(int count, ItemFilter* zFilter)
  146. {
  147. if (!current || !other) return;
  148. transaction(other, current, zFilter, getOppositeDirection(dir), dir, count);
  149. }
  150. void InventoryInteraction::pushItems(int count, ItemFilter* zFilter)
  151. {
  152. if (!current || !other) return;
  153. transaction(current, other, zFilter, dir, getOppositeDirection(dir), count);
  154. }
  155. MultipleInventoryLock::MultipleInventoryLock(Inventory** inventories, int count)
  156. : inventories(new Inventory*[count]),
  157. count(count),
  158. locked(0)
  159. {
  160. // sort given inventories in locking order
  161. bool* used = new bool[count];
  162. memset(used, 0, count);
  163. // TODO: use a performant sorting algorithm
  164. for (int i = 0; i < count; i++)
  165. {
  166. Inventory* min = 0;
  167. int minJ = 0;
  168. for (int j = 0; j < count; j++)
  169. {
  170. if (!used[j])
  171. {
  172. if (!min)
  173. {
  174. min = inventories[j];
  175. minJ = j;
  176. continue;
  177. }
  178. if (inventories[j]->location.x < min->location.x)
  179. {
  180. min = inventories[j];
  181. minJ = j;
  182. continue;
  183. }
  184. if (inventories[j]->location.x == min->location.x)
  185. {
  186. if (inventories[j]->location.y < min->location.y)
  187. {
  188. min = inventories[j];
  189. minJ = j;
  190. continue;
  191. }
  192. if (inventories[j]->location.y == min->location.y)
  193. {
  194. if (inventories[j]->location.z < min->location.z)
  195. {
  196. min = inventories[j];
  197. minJ = j;
  198. continue;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. this->inventories[i] = min;
  205. used[minJ] = 1;
  206. }
  207. lock();
  208. delete[] used;
  209. }
  210. MultipleInventoryLock::~MultipleInventoryLock()
  211. {
  212. unlock();
  213. delete[] inventories;
  214. }
  215. void MultipleInventoryLock::unlock()
  216. {
  217. if (locked)
  218. {
  219. locked = 0;
  220. for (int i = count - 1; i >= 0; i--)
  221. {
  222. inventories[i]->cs.unlock();
  223. }
  224. }
  225. }
  226. void MultipleInventoryLock::lock()
  227. {
  228. if (!locked)
  229. {
  230. locked = 1;
  231. for (int i = 0; i < count; i++)
  232. {
  233. inventories[i]->cs.lock();
  234. }
  235. }
  236. }
  237. Inventory::Inventory(
  238. const Framework::Vec3<float> location, int dimensionId, bool hasInventory)
  239. : ReferenceCounter(),
  240. nextSlotId(1),
  241. dimensionId(dimensionId),
  242. location(location)
  243. {
  244. if (hasInventory)
  245. {
  246. pullSlotsOrder = new Framework::RCArray<ItemSlot>();
  247. pushSlotsOrder = new Framework::RCArray<ItemSlot>();
  248. itemCache = new Framework::HashMap<int, Framework::Array<ItemSlot*>*>(
  249. ITEM_CACHE_SIZE, [](int key) { return key; });
  250. }
  251. else
  252. {
  253. pullSlotsOrder = 0;
  254. pushSlotsOrder = 0;
  255. itemCache = 0;
  256. }
  257. }
  258. Inventory::~Inventory()
  259. {
  260. if (pullSlotsOrder) pullSlotsOrder->release();
  261. if (pushSlotsOrder) pushSlotsOrder->release();
  262. if (itemCache) itemCache->release();
  263. }
  264. void Inventory::updateCache(ItemSlot* zSlot, int beforeKey)
  265. {
  266. if (!itemCache) return;
  267. int key
  268. = zSlot->zStack() ? zSlot->zStack()->zItem()->zItemType()->getId() : -1;
  269. if (key == beforeKey) return;
  270. if (beforeKey >= 0)
  271. {
  272. auto tmp = itemCache->safeGet(key, 0);
  273. if (tmp) tmp->removeValue(zSlot);
  274. }
  275. if (zSlot->zStack())
  276. {
  277. auto tmp = itemCache->safeGet(key, 0);
  278. if (!tmp)
  279. {
  280. tmp = new Array<ItemSlot*>();
  281. itemCache->put(key, tmp);
  282. }
  283. tmp->add(zSlot, 0);
  284. }
  285. }
  286. void Inventory::addSlot(ItemSlot* slot)
  287. {
  288. cs.lock();
  289. ((ItemSlotIDSetter*)slot)->setId(nextSlotId++);
  290. int pullPrio = slot->getPullPriority();
  291. int pushPrio = slot->getPushPriority();
  292. int index = 0;
  293. for (auto stack : *pullSlotsOrder)
  294. {
  295. if (stack->getPullPriority() > pullPrio) break;
  296. index++;
  297. }
  298. pullSlotsOrder->add(dynamic_cast<ItemSlot*>(slot->getThis()), index);
  299. index = 0;
  300. for (auto stack : *pushSlotsOrder)
  301. {
  302. if (stack->getPushPriority() > pushPrio) break;
  303. index++;
  304. }
  305. pushSlotsOrder->add(slot, index);
  306. updateCache(slot, -1);
  307. cs.unlock();
  308. }
  309. int Inventory::countAccessableItems(ItemFilter* zFilter, Direction dir) const
  310. {
  311. int count = 0;
  312. if (itemCache)
  313. {
  314. for (auto slot : *pullSlotsOrder)
  315. {
  316. if (slot->getNumberOfItems() > 0
  317. && (!zFilter || zFilter->matchSourceSlot(slot)))
  318. {
  319. if (allowPullStack(slot, dir))
  320. {
  321. count += slot->getNumberOfItems();
  322. }
  323. }
  324. }
  325. }
  326. return count;
  327. }
  328. bool Inventory::allowPullStack(ItemSlot* zSlot, Direction dir) const
  329. {
  330. return pullSlotsOrder != 0;
  331. }
  332. bool Inventory::allowPushStack(
  333. ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const
  334. {
  335. return pushSlotsOrder != 0;
  336. }
  337. void Inventory::afterPullStack(
  338. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  339. {
  340. NetworkMessage* msg = new NetworkMessage();
  341. char* message = new char[9];
  342. message[0] = 1; // set count of items
  343. *(int*)(message + 1) = zSlot->getId();
  344. *(int*)(message + 5) = zSlot->getNumberOfItems();
  345. msg->setMessage(message, 9);
  346. notifyObservers(msg);
  347. for (auto call : afterPullStackCalls)
  348. call(zSlot, dir, zItem, count);
  349. }
  350. void Inventory::afterPushStack(
  351. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  352. {
  353. updateSlot(zSlot);
  354. for (auto call : afterPushStackCalls)
  355. call(zSlot, dir, zItem, count);
  356. }
  357. void Inventory::updateSlot(ItemSlot* zSlot)
  358. {
  359. NetworkMessage* msg = new NetworkMessage();
  360. char* message = new char[9];
  361. message[0] = 1; // set count of items
  362. *(int*)(message + 1) = zSlot->getId();
  363. *(int*)(message + 5) = 0;
  364. msg->setMessage(message, 9);
  365. notifyObservers(msg);
  366. if (zSlot->getNumberOfItems() > 0)
  367. {
  368. const Item* zItem = zSlot->zStack()->zItem();
  369. NetworkMessage* msg = new NetworkMessage();
  370. char* message = new char[30 + zItem->getName().getLength()];
  371. message[0] = 2; // add new stack
  372. *(int*)(message + 1) = zSlot->getId();
  373. *(int*)(message + 5) = zSlot->getNumberOfItems();
  374. *(float*)(message + 9) = zItem->getHp();
  375. *(float*)(message + 13) = zItem->getMaxHp();
  376. *(float*)(message + 17) = zItem->getDurability();
  377. *(float*)(message + 21) = zItem->getMaxDurability();
  378. *(int*)(message + 25) = zItem->zItemType()->getId();
  379. *(message + 29) = (char)zItem->getName().getLength();
  380. memcpy(message + 30,
  381. zItem->getName().getText(),
  382. zItem->getName().getLength());
  383. msg->setMessage(message, 30 + zItem->getName().getLength());
  384. notifyObservers(msg);
  385. }
  386. }
  387. void Inventory::loadInventory(Framework::StreamReader* zReader)
  388. {
  389. if (itemCache)
  390. {
  391. for (auto stack : *pushSlotsOrder)
  392. {
  393. int size = 0;
  394. zReader->lese((char*)&size, 4);
  395. if (size != 0)
  396. {
  397. int id = 0;
  398. zReader->lese((char*)&id, 4);
  399. Item* item = Game::INSTANCE->zItemType(id)->loadItem(zReader);
  400. stack->addItems(new ItemStack(item, size), NO_DIRECTION);
  401. }
  402. }
  403. }
  404. }
  405. void Inventory::saveInventory(Framework::StreamWriter* zWriter)
  406. {
  407. if (itemCache)
  408. {
  409. for (auto slot : *pushSlotsOrder)
  410. {
  411. const ItemStack* stack = slot->zStack();
  412. int value = 0;
  413. if (!stack || !stack->zItem())
  414. {
  415. zWriter->schreibe((char*)&value, 4);
  416. }
  417. else
  418. {
  419. value = stack->getSize();
  420. zWriter->schreibe((char*)&value, 4);
  421. value = stack->zItem()->zItemType()->getId();
  422. zWriter->schreibe((char*)&value, 4);
  423. stack->zItem()->zItemType()->saveItem(stack->zItem(), zWriter);
  424. }
  425. }
  426. }
  427. }
  428. void Inventory::notifyObservers(NetworkMessage* msg)
  429. {
  430. observable.notifyObservers(msg);
  431. }
  432. void Inventory::lock()
  433. {
  434. cs.lock();
  435. }
  436. void Inventory::unlock()
  437. {
  438. cs.unlock();
  439. }
  440. const ItemSlot* Inventory::zSlot(int id) const
  441. {
  442. if (itemCache)
  443. {
  444. for (auto slot : *pushSlotsOrder)
  445. {
  446. if (slot->getId() == id) return slot;
  447. }
  448. }
  449. return 0;
  450. }
  451. void Inventory::localTransaction(Array<ItemSlot*>* zSourceSlots,
  452. Array<ItemSlot*>* zTargetSlots,
  453. ItemFilter* zFilter,
  454. int count,
  455. Direction outDir,
  456. Direction inDir)
  457. {
  458. if (itemCache)
  459. {
  460. cs.lock();
  461. auto sourceSlot
  462. = zSourceSlots ? zSourceSlots->begin() : pullSlotsOrder->begin();
  463. while (true)
  464. {
  465. while (sourceSlot
  466. && (sourceSlot->getNumberOfItems() == 0
  467. || (zFilter && !zFilter->matchSourceSlot(sourceSlot))))
  468. sourceSlot++;
  469. if (!sourceSlot)
  470. {
  471. cs.unlock();
  472. return;
  473. }
  474. bool needNext = 1;
  475. for (auto targetSlot = zTargetSlots->begin(); targetSlot;)
  476. {
  477. while (
  478. targetSlot
  479. && (targetSlot->isFull()
  480. || (zFilter && !zFilter->matchTargetSlot(targetSlot))))
  481. targetSlot++;
  482. if (!targetSlot) break;
  483. needNext &= !Inventory::unsafeMove(
  484. this, this, sourceSlot, targetSlot, outDir, inDir, count);
  485. if (count == 0)
  486. {
  487. cs.unlock();
  488. return;
  489. }
  490. if (sourceSlot->getNumberOfItems() == 0) break;
  491. }
  492. if (needNext) sourceSlot++;
  493. }
  494. cs.unlock();
  495. }
  496. }
  497. void Inventory::addItems(ItemStack* zItems, Direction dir, ItemFilter* zFilter)
  498. {
  499. if (itemCache && zItems && zItems->getSize() > 0)
  500. {
  501. cs.lock();
  502. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot;
  503. targetSlot++)
  504. {
  505. if (!targetSlot->isFull()
  506. && (!zFilter || zFilter->matchTargetSlot(targetSlot)))
  507. {
  508. if (targetSlot->zStack())
  509. {
  510. if (targetSlot->zStack()->zItem()->canBeStackedWith(
  511. zItems->zItem()))
  512. {
  513. int number = MIN(targetSlot->numberOfAddableItems(
  514. zItems->zItem(), dir),
  515. zItems->getSize());
  516. int tmp = number;
  517. if (number > 0
  518. && allowPushStack(
  519. targetSlot, dir, zItems->zItem(), tmp))
  520. {
  521. number = MIN(number, tmp);
  522. ItemStack* stack = zItems->split(number);
  523. if (stack)
  524. {
  525. targetSlot->addItems(stack, dir);
  526. afterPushStack(targetSlot,
  527. dir,
  528. targetSlot->zStack()->zItem(),
  529. number);
  530. if (stack->getSize()) throw stack;
  531. stack->release();
  532. if (!zItems->getSize()) break;
  533. }
  534. }
  535. }
  536. }
  537. else
  538. {
  539. int number = MIN(
  540. targetSlot->numberOfAddableItems(zItems->zItem(), dir),
  541. zItems->getSize());
  542. int tmp = number;
  543. if (number > 0
  544. && allowPushStack(
  545. targetSlot, dir, zItems->zItem(), tmp))
  546. {
  547. number = MIN(number, tmp);
  548. ItemStack* stack = zItems->split(number);
  549. if (stack)
  550. {
  551. targetSlot->addItems(stack, dir);
  552. updateCache(targetSlot, -1);
  553. afterPushStack(targetSlot,
  554. dir,
  555. targetSlot->zStack()->zItem(),
  556. number);
  557. if (stack->getSize()) throw stack;
  558. stack->release();
  559. if (!zItems->getSize()) break;
  560. }
  561. }
  562. }
  563. }
  564. }
  565. cs.unlock();
  566. }
  567. }
  568. void Inventory::addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir)
  569. {
  570. if (zSlot->zStack()
  571. && !zSlot->zStack()->zItem()->canBeStackedWith(zItems->zItem()))
  572. return;
  573. bool needUpdate = !zSlot->zStack();
  574. int number = MIN(
  575. zSlot->numberOfAddableItems(zItems->zItem(), dir), zItems->getSize());
  576. int tmp = number;
  577. if (number > 0 && allowPushStack(zSlot, dir, zItems->zItem(), tmp))
  578. {
  579. number = MIN(number, tmp);
  580. ItemStack* stack = zItems->split(number);
  581. if (stack)
  582. {
  583. zSlot->addItems(stack, dir);
  584. if (needUpdate) updateCache(zSlot, -1);
  585. afterPushStack(zSlot, dir, zSlot->zStack()->zItem(), number);
  586. if (stack->getSize()) throw stack;
  587. stack->release();
  588. }
  589. }
  590. }
  591. ItemStack* Inventory::takeItemsOut(ItemSlot* zSlot, int count, Direction dir)
  592. {
  593. if (allowPullStack(zSlot, dir))
  594. {
  595. ItemStack* stack = zSlot->takeItemsOut(count, dir);
  596. if (stack)
  597. {
  598. updateCache(zSlot, stack->zItem()->zItemType()->getId());
  599. if (stack->getSize() > 0)
  600. afterPullStack(zSlot, dir, stack->zItem(), stack->getSize());
  601. }
  602. return stack;
  603. }
  604. return 0;
  605. }
  606. InventoryInteraction Inventory::interactWith(
  607. Inventory* zInventory, Direction dir)
  608. {
  609. return InventoryInteraction(this, zInventory, dir);
  610. }
  611. void Inventory::unsaveAddItem(
  612. ItemStack* zStack, Direction dir, ItemFilter* zFilter)
  613. {
  614. addItems(zStack, dir, zFilter);
  615. }
  616. int Inventory::numberOfAddableItems(const Item* zItem, Direction dir) const
  617. {
  618. int count = 0;
  619. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
  620. {
  621. int maxCount = targetSlot->numberOfAddableItems(zItem, dir);
  622. int allowed = maxCount;
  623. if (allowPushStack(targetSlot, dir, zItem, allowed))
  624. count += MIN(maxCount, allowed);
  625. }
  626. return count;
  627. }
  628. int Inventory::numberOfAddableItems(
  629. const Item* zItem, Direction dir, const Framework::Text& slotName) const
  630. {
  631. int count = 0;
  632. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
  633. {
  634. if (targetSlot->getName().istGleich(slotName))
  635. {
  636. int maxCount = targetSlot->numberOfAddableItems(zItem, dir);
  637. int allowed = maxCount;
  638. if (allowPushStack(targetSlot, dir, zItem, allowed))
  639. count += MIN(maxCount, allowed);
  640. }
  641. }
  642. return count;
  643. }
  644. bool Inventory::isAllAvailable(Framework::RCArray<RecipieInput>& inputs,
  645. const Framework::Text& slotName) const
  646. {
  647. int* used = new int[pullSlotsOrder->getEintragAnzahl()];
  648. memset(used, 0, sizeof(int) * pullSlotsOrder->getEintragAnzahl());
  649. for (RecipieInput* input : inputs)
  650. {
  651. int found = 0;
  652. for (int i = 0; i < pullSlotsOrder->getEintragAnzahl(); i++)
  653. {
  654. ItemSlot* slot = pullSlotsOrder->get(i);
  655. if (slot && slot->zStack() && slot->zStack()->zItem()
  656. && slot->getNumberOfItems() > used[i]
  657. && slot->getName().istGleich(slotName)
  658. && input->zFilter()->matchItem(slot->zStack()->zItem()))
  659. {
  660. int usable = slot->getNumberOfItems() - used[i];
  661. if (found + usable >= input->getAmount())
  662. {
  663. used[i] += input->getAmount() - found;
  664. found = input->getAmount();
  665. break;
  666. }
  667. else
  668. {
  669. used[i] += usable;
  670. found += usable;
  671. }
  672. }
  673. }
  674. if (found < input->getAmount())
  675. {
  676. delete[] used;
  677. return 0;
  678. }
  679. }
  680. delete[] used;
  681. return 1;
  682. }
  683. void Inventory::consume(
  684. Framework::RCArray<RecipieInput>& inputs, const Framework::Text& slotName)
  685. {
  686. for (RecipieInput* input : inputs)
  687. {
  688. int consumed = 0;
  689. for (int i = 0; i < pullSlotsOrder->getEintragAnzahl(); i++)
  690. {
  691. ItemSlot* slot = pullSlotsOrder->get(i);
  692. if (slot && slot->zStack() && slot->zStack()->zItem()
  693. && slot->getName().istGleich(slotName)
  694. && input->zFilter()->matchItem(slot->zStack()->zItem()))
  695. {
  696. if (consumed + slot->getNumberOfItems() >= input->getAmount())
  697. {
  698. takeItemsOut(
  699. slot, input->getAmount() - consumed, NO_DIRECTION)
  700. ->release();
  701. consumed = input->getAmount();
  702. break;
  703. }
  704. else
  705. {
  706. consumed += slot->getNumberOfItems();
  707. takeItemsOut(slot, slot->getNumberOfItems(), NO_DIRECTION)
  708. ->release();
  709. }
  710. }
  711. }
  712. }
  713. }
  714. Framework::ArrayIterator<ItemSlot*> Inventory::begin()
  715. {
  716. return pullSlotsOrder->begin();
  717. }
  718. Framework::ArrayIterator<ItemSlot*> Inventory::end()
  719. {
  720. return pullSlotsOrder->end();
  721. }
  722. void Inventory::inventoryApi(Framework::StreamReader* zRequest,
  723. NetworkMessage* zResponse,
  724. Entity* zSource)
  725. {
  726. char type;
  727. zRequest->lese(&type, 1);
  728. switch (type)
  729. {
  730. case 0: // request inventory
  731. {
  732. char idLen;
  733. zRequest->lese(&idLen, 1);
  734. char* id = new char[idLen + 1];
  735. zRequest->lese(id, idLen);
  736. id[(int)idLen] = 0;
  737. int processor;
  738. zRequest->lese((char*)&processor, 4);
  739. zResponse->addressUIElement(id, processor);
  740. observable.addObserver(zSource, id, processor);
  741. delete[] id;
  742. char filterLen;
  743. zRequest->lese(&filterLen, 1);
  744. char* filter = new char[filterLen + 1];
  745. if (filterLen) zRequest->lese(filter, filterLen);
  746. filter[(int)filterLen] = 0;
  747. InMemoryBuffer buffer;
  748. int count = 0;
  749. for (ItemSlot* slot : *this)
  750. {
  751. if (filterLen == 0 || slot->getName().istGleich(filter))
  752. {
  753. count++;
  754. int id = slot->getId();
  755. buffer.schreibe((char*)&id, 4);
  756. int itemCount = slot->getNumberOfItems();
  757. buffer.schreibe((char*)&itemCount, 4);
  758. if (itemCount > 0)
  759. {
  760. float f = slot->zStack()->zItem()->getHp();
  761. buffer.schreibe((char*)&f, 4);
  762. f = slot->zStack()->zItem()->getMaxHp();
  763. buffer.schreibe((char*)&f, 4);
  764. f = slot->zStack()->zItem()->getDurability();
  765. buffer.schreibe((char*)&f, 4);
  766. f = slot->zStack()->zItem()->getMaxDurability();
  767. buffer.schreibe((char*)&f, 4);
  768. int id = slot->zStack()->zItem()->zItemType()->getId();
  769. buffer.schreibe((char*)&id, 4);
  770. char len = (char)slot->zStack()
  771. ->zItem()
  772. ->getName()
  773. .getLength();
  774. buffer.schreibe((char*)&len, 1);
  775. buffer.schreibe(
  776. slot->zStack()->zItem()->getName().getText(),
  777. slot->zStack()->zItem()->getName().getLength());
  778. }
  779. }
  780. }
  781. delete[] filter;
  782. char* msg = new char[5 + buffer.getSize()];
  783. msg[0] = 0;
  784. *(int*)(msg + 1) = count;
  785. buffer.lese(msg + 5, (int)buffer.getSize());
  786. zResponse->setMessage(msg, 5 + (int)buffer.getSize());
  787. break;
  788. }
  789. case 1: // remove Observer
  790. {
  791. char idLen;
  792. zRequest->lese(&idLen, 1);
  793. char* id = new char[idLen + 1];
  794. zRequest->lese(id, idLen);
  795. id[(int)idLen] = 0;
  796. int processor;
  797. zRequest->lese((char*)&processor, 4);
  798. observable.removeObserver(zSource, id, processor);
  799. delete[] id;
  800. break;
  801. }
  802. case 2: // request item tooltip
  803. {
  804. char idLen;
  805. zRequest->lese(&idLen, 1);
  806. char* id = new char[idLen + 1];
  807. zRequest->lese(id, idLen);
  808. id[(int)idLen] = 0;
  809. int processor;
  810. zRequest->lese((char*)&processor, 4);
  811. zResponse->addressUIElement(id, processor);
  812. delete[] id;
  813. int slotId;
  814. zRequest->lese((char*)&slotId, 4);
  815. Text uiml;
  816. for (ItemSlot* slot : *pullSlotsOrder)
  817. {
  818. if (slot->getId() == slotId)
  819. {
  820. if (slot->zStack() && slot->zStack()->zItem())
  821. {
  822. Framework::XML::Element* element
  823. = slot->zStack()
  824. ->zItem()
  825. ->getTooltipUIML()
  826. ->build();
  827. uiml = element->toString();
  828. element->release();
  829. }
  830. }
  831. }
  832. short len = (short)uiml.getLength();
  833. char* buffer = new char[uiml.getLength() + 7];
  834. buffer[0] = 3;
  835. *(int*)(buffer + 1) = slotId;
  836. *(short*)(buffer + 5) = len;
  837. memcpy(buffer + 7, uiml, len);
  838. zResponse->setMessage(buffer, len + 7);
  839. break;
  840. }
  841. }
  842. }
  843. void Inventory::registerAfterPullStackCall(std::function<void(
  844. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  845. {
  846. afterPullStackCalls.add(call);
  847. }
  848. void Inventory::registerAfterPushStackCall(std::function<void(
  849. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  850. {
  851. afterPushStackCalls.add(call);
  852. }
  853. void Inventory::registerObserverAddedCall(
  854. std::function<void(Entity* zSource, Framework::Text id, int processor)>
  855. call)
  856. {
  857. observable.registerOnObserverAddedCall(call);
  858. }
  859. int Inventory::getDimensionId() const
  860. {
  861. return dimensionId;
  862. }
  863. Framework::Vec3<float> Inventory::getLocation() const
  864. {
  865. return location;
  866. }
  867. bool Inventory::unsafeMove(Inventory* zSource,
  868. Inventory* zTarget,
  869. ArrayIterator<ItemSlot*>& sourceSlot,
  870. ArrayIterator<ItemSlot*>& targetSlot,
  871. Direction outDir,
  872. Direction inDir,
  873. int& count)
  874. {
  875. if (targetSlot->zStack())
  876. {
  877. if (sourceSlot->zStack()->zItem()->canBeStackedWith(
  878. targetSlot->zStack()->zItem()))
  879. {
  880. int number = MIN(targetSlot->numberOfAddableItems(
  881. sourceSlot->zStack()->zItem(), outDir),
  882. count);
  883. int tmp = number;
  884. if (number > 0 && zSource->allowPullStack(sourceSlot, outDir)
  885. && zTarget->allowPushStack(
  886. targetSlot, inDir, sourceSlot->zStack()->zItem(), tmp))
  887. {
  888. number = MIN(number, tmp);
  889. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  890. if (stack)
  891. {
  892. targetSlot->addItems(stack, inDir);
  893. zSource->updateCache(sourceSlot,
  894. targetSlot->zStack()->zItem()->zItemType()->getId());
  895. zSource->afterPullStack(sourceSlot,
  896. outDir,
  897. targetSlot->zStack()->zItem(),
  898. number);
  899. zTarget->afterPushStack(targetSlot,
  900. inDir,
  901. targetSlot->zStack()->zItem(),
  902. number);
  903. if (stack->getSize()) throw stack;
  904. stack->release();
  905. count -= number;
  906. return 1;
  907. }
  908. else
  909. targetSlot++;
  910. }
  911. else
  912. targetSlot++;
  913. }
  914. else
  915. targetSlot++;
  916. }
  917. else
  918. {
  919. int number = MIN(targetSlot->numberOfAddableItems(
  920. sourceSlot->zStack()->zItem(), outDir),
  921. count);
  922. int tmp = number;
  923. if (number > 0 && zSource->allowPullStack(sourceSlot, outDir)
  924. && zTarget->allowPushStack(
  925. targetSlot, inDir, sourceSlot->zStack()->zItem(), tmp))
  926. {
  927. number = MIN(number, tmp);
  928. if (number > 0)
  929. {
  930. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  931. if (stack)
  932. {
  933. targetSlot->addItems(stack, inDir);
  934. zSource->updateCache(sourceSlot,
  935. targetSlot->zStack()->zItem()->zItemType()->getId());
  936. zTarget->updateCache(targetSlot, -1);
  937. zSource->afterPullStack(sourceSlot,
  938. outDir,
  939. targetSlot->zStack()->zItem(),
  940. number);
  941. zTarget->afterPushStack(targetSlot,
  942. inDir,
  943. targetSlot->zStack()->zItem(),
  944. number);
  945. if (stack->getSize()) throw stack;
  946. stack->release();
  947. count -= number;
  948. return 1;
  949. }
  950. else
  951. targetSlot++;
  952. }
  953. else
  954. targetSlot++;
  955. }
  956. else
  957. targetSlot++;
  958. }
  959. return 0;
  960. }