Inventory.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 "NetworkMessage.h"
  9. using namespace Framework;
  10. InventoryInteraction::InventoryInteraction(
  11. Inventory* current, Inventory* other, Direction dir)
  12. : current(current),
  13. other(other),
  14. dir(dir)
  15. {
  16. lock();
  17. }
  18. InventoryInteraction::InventoryInteraction(
  19. const InventoryInteraction& interaction)
  20. : InventoryInteraction(
  21. interaction.current, interaction.other, interaction.dir)
  22. {}
  23. InventoryInteraction::~InventoryInteraction()
  24. {
  25. unlock();
  26. }
  27. void InventoryInteraction::lock()
  28. {
  29. if (!current || !other) return;
  30. if (current->location.x < other->location.x)
  31. {
  32. current->cs.lock();
  33. other->cs.lock();
  34. return;
  35. }
  36. else if (current->location.x == other->location.x)
  37. {
  38. if (current->location.y < other->location.y)
  39. {
  40. current->cs.lock();
  41. other->cs.lock();
  42. return;
  43. }
  44. else if (current->location.y == other->location.y)
  45. {
  46. if (current->location.z < other->location.z)
  47. {
  48. current->cs.lock();
  49. other->cs.lock();
  50. return;
  51. }
  52. }
  53. }
  54. other->cs.lock();
  55. current->cs.lock();
  56. }
  57. void InventoryInteraction::unlock()
  58. {
  59. if (!current || !other) return;
  60. if (current->location.x < other->location.x)
  61. {
  62. current->cs.unlock();
  63. other->cs.unlock();
  64. return;
  65. }
  66. else if (current->location.x == other->location.x)
  67. {
  68. if (current->location.y < other->location.y)
  69. {
  70. current->cs.unlock();
  71. other->cs.unlock();
  72. return;
  73. }
  74. else if (current->location.y == other->location.y)
  75. {
  76. if (current->location.z < other->location.z)
  77. {
  78. current->cs.unlock();
  79. other->cs.unlock();
  80. return;
  81. }
  82. }
  83. }
  84. other->cs.unlock();
  85. current->cs.unlock();
  86. }
  87. void InventoryInteraction::transaction(Inventory* zSource,
  88. Inventory* zTarget,
  89. ItemFilter* zFilter,
  90. Direction sourceView,
  91. Direction targetView,
  92. int count)
  93. {
  94. for (auto sourceSlot = zSource->pullSlotsOrder->begin(); sourceSlot;)
  95. {
  96. while (sourceSlot
  97. && (sourceSlot->getNumberOfItems() == 0
  98. || (zFilter && !zFilter->matchSourceSlot(sourceSlot))))
  99. sourceSlot++;
  100. if (!sourceSlot) break;
  101. // TODO: use target cache ot get list of slots that already contains the
  102. // source item
  103. bool needNext = 1;
  104. for (auto targetSlot = zTarget->pushSlotsOrder->begin(); targetSlot;)
  105. {
  106. while (targetSlot
  107. && (targetSlot->isFull()
  108. || (zFilter && !zFilter->matchTargetSlot(targetSlot))))
  109. targetSlot++;
  110. if (!targetSlot) break;
  111. needNext &= !Inventory::unsafeMove(zSource,
  112. zTarget,
  113. sourceSlot,
  114. targetSlot,
  115. sourceView,
  116. targetView,
  117. count);
  118. if (count == 0) return;
  119. if (sourceSlot->getNumberOfItems() == 0) break;
  120. }
  121. if (needNext) sourceSlot++;
  122. }
  123. }
  124. InventoryInteraction& InventoryInteraction::operator=(
  125. const InventoryInteraction& data)
  126. {
  127. if (&data == this) return *this;
  128. unlock();
  129. current = data.current;
  130. other = data.other;
  131. dir = data.dir;
  132. lock();
  133. return *this;
  134. }
  135. void InventoryInteraction::endInteraction()
  136. {
  137. unlock();
  138. current = 0;
  139. other = 0;
  140. }
  141. void InventoryInteraction::pullItems(int count, ItemFilter* zFilter)
  142. {
  143. if (!current || !other) return;
  144. transaction(other, current, zFilter, getOppositeDirection(dir), dir, count);
  145. }
  146. void InventoryInteraction::pushItems(int count, ItemFilter* zFilter)
  147. {
  148. if (!current || !other) return;
  149. transaction(current, other, zFilter, dir, getOppositeDirection(dir), count);
  150. }
  151. Inventory::Inventory(const Framework::Vec3<float> location, bool hasInventory)
  152. : ReferenceCounter(),
  153. nextSlotId(1),
  154. location(location)
  155. {
  156. if (hasInventory)
  157. {
  158. pullSlotsOrder = new Framework::RCArray<ItemSlot>();
  159. pushSlotsOrder = new Framework::RCArray<ItemSlot>();
  160. itemCache = new Framework::HashMap<int, Framework::Array<ItemSlot*>*>(
  161. ITEM_CACHE_SIZE, std::_Identity<int>());
  162. }
  163. else
  164. {
  165. pullSlotsOrder = 0;
  166. pushSlotsOrder = 0;
  167. itemCache = 0;
  168. }
  169. }
  170. Inventory::~Inventory()
  171. {
  172. if (pullSlotsOrder) pullSlotsOrder->release();
  173. if (pushSlotsOrder) pushSlotsOrder->release();
  174. if (itemCache) itemCache->release();
  175. }
  176. void Inventory::updateCache(ItemSlot* zSlot, int beforeKey)
  177. {
  178. if (!itemCache) return;
  179. int key
  180. = zSlot->zStack() ? zSlot->zStack()->zItem()->zItemType()->getId() : -1;
  181. if (key == beforeKey) return;
  182. if (beforeKey >= 0)
  183. {
  184. auto tmp = itemCache->safeGet(key, 0);
  185. if (tmp) tmp->removeValue(zSlot);
  186. }
  187. if (zSlot->zStack())
  188. {
  189. auto tmp = itemCache->safeGet(key, 0);
  190. if (!tmp)
  191. {
  192. tmp = new Array<ItemSlot*>();
  193. itemCache->put(key, tmp);
  194. }
  195. tmp->add(zSlot, 0);
  196. }
  197. }
  198. void Inventory::addSlot(ItemSlot* slot)
  199. {
  200. cs.lock();
  201. ((ItemSlotIDSetter*)slot)->setId(nextSlotId++);
  202. int pullPrio = slot->getPullPriority();
  203. int pushPrio = slot->getPushPriority();
  204. int index = 0;
  205. for (auto stack : *pullSlotsOrder)
  206. {
  207. if (stack->getPullPriority() > pullPrio) break;
  208. index++;
  209. }
  210. pullSlotsOrder->add(dynamic_cast<ItemSlot*>(slot->getThis()), index);
  211. index = 0;
  212. for (auto stack : *pushSlotsOrder)
  213. {
  214. if (stack->getPushPriority() > pushPrio) break;
  215. index++;
  216. }
  217. pushSlotsOrder->add(slot, index);
  218. updateCache(slot, -1);
  219. cs.unlock();
  220. }
  221. bool Inventory::allowPullStack(ItemSlot* zSlot, Direction dir) const
  222. {
  223. return pullSlotsOrder;
  224. }
  225. bool Inventory::allowPushStack(
  226. ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const
  227. {
  228. return pushSlotsOrder;
  229. }
  230. void Inventory::afterPullStack(
  231. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  232. {
  233. NetworkMessage* msg = new NetworkMessage();
  234. char* message = new char[9];
  235. message[0] = 1; // set count of items
  236. *(int*)(message + 1) = zSlot->getId();
  237. *(int*)(message + 5) = zSlot->getNumberOfItems();
  238. msg->setMessage(message, 9);
  239. notifyObservers(msg);
  240. for (auto call : afterPullStackCalls)
  241. call(zSlot, dir, zItem, count);
  242. }
  243. void Inventory::afterPushStack(
  244. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  245. {
  246. if (zSlot->getNumberOfItems() > count)
  247. {
  248. NetworkMessage* msg = new NetworkMessage();
  249. char* message = new char[9];
  250. message[0] = 1; // set count of items
  251. *(int*)(message + 1) = zSlot->getId();
  252. *(int*)(message + 5) = zSlot->getNumberOfItems();
  253. msg->setMessage(message, 9);
  254. notifyObservers(msg);
  255. }
  256. else
  257. {
  258. NetworkMessage* msg = new NetworkMessage();
  259. char* message = new char[29];
  260. message[0] = 2; // add new stack
  261. *(int*)(message + 1) = zSlot->getId();
  262. *(int*)(message + 5) = zSlot->getNumberOfItems();
  263. const Item* zItem = zSlot->zStack()->zItem();
  264. *(float*)(message + 9) = zItem->getHp();
  265. *(float*)(message + 13) = zItem->getMaxHp();
  266. *(float*)(message + 17) = zItem->getDurability();
  267. *(float*)(message + 21) = zItem->getMaxDurability();
  268. *(int*)(message + 25) = zItem->zItemType()->getId();
  269. msg->setMessage(message, 29);
  270. notifyObservers(msg);
  271. }
  272. for (auto call : afterPushStackCalls)
  273. call(zSlot, dir, zItem, count);
  274. }
  275. void Inventory::loadInventory(Framework::StreamReader* zReader)
  276. {
  277. if (itemCache)
  278. {
  279. for (auto stack : *pushSlotsOrder)
  280. {
  281. int size = 0;
  282. zReader->lese((char*)&size, 4);
  283. if (size != 0)
  284. {
  285. int id = 0;
  286. zReader->lese((char*)&id, 4);
  287. Item* item
  288. = StaticRegistry<ItemType>::INSTANCE.zElement(id)->loadItem(
  289. zReader);
  290. stack->addItems(new ItemStack(item, size), NO_DIRECTION);
  291. }
  292. }
  293. }
  294. }
  295. void Inventory::saveInventory(Framework::StreamWriter* zWriter)
  296. {
  297. if (itemCache)
  298. {
  299. for (auto slot : *pushSlotsOrder)
  300. {
  301. const ItemStack* stack = slot->zStack();
  302. int value = 0;
  303. if (!stack || !stack->zItem())
  304. {
  305. zWriter->schreibe((char*)&value, 4);
  306. }
  307. else
  308. {
  309. value = stack->getSize();
  310. zWriter->schreibe((char*)&value, 4);
  311. value = stack->zItem()->zItemType()->getId();
  312. zWriter->schreibe((char*)&value, 4);
  313. stack->zItem()->zItemType()->saveItem(stack->zItem(), zWriter);
  314. }
  315. }
  316. }
  317. }
  318. void Inventory::notifyObservers(NetworkMessage* msg)
  319. {
  320. cs.lock();
  321. int index = 0;
  322. Array<int> toDelete;
  323. for (auto observer : observers)
  324. {
  325. Entity* e = Game::INSTANCE->zEntity(observer.getFirst());
  326. if (e)
  327. {
  328. msg->addressGui(observer.getSecond());
  329. Game::INSTANCE->sendMessage(msg->clone(), e);
  330. }
  331. else
  332. toDelete.add(index, 0);
  333. index++;
  334. }
  335. for (int i : toDelete)
  336. observers.remove(i);
  337. cs.unlock();
  338. msg->release();
  339. }
  340. void Inventory::removeObserver(Entity* zSource, Framework::Text id)
  341. {
  342. cs.lock();
  343. int index = 0;
  344. for (auto observer : observers)
  345. {
  346. if (observer.getFirst() == zSource->getId()
  347. && observer.getSecond().istGleich(id))
  348. {
  349. observers.remove(index);
  350. break;
  351. }
  352. index++;
  353. }
  354. cs.unlock();
  355. }
  356. void Inventory::addObserver(Entity* zSource, Framework::Text id)
  357. {
  358. cs.lock();
  359. for (auto observer : observers)
  360. {
  361. if (observer.getFirst() == zSource->getId()
  362. && observer.getSecond().istGleich(id))
  363. {
  364. cs.unlock();
  365. return;
  366. }
  367. }
  368. observers.add(ImmutablePair<int, Text>(zSource->getId(), id));
  369. cs.unlock();
  370. for (auto call : observerAddedCalls)
  371. call(zSource, id);
  372. }
  373. const ItemSlot* Inventory::zSlot(int id) const
  374. {
  375. if (itemCache)
  376. {
  377. for (auto slot : *pushSlotsOrder)
  378. {
  379. if (slot->getId() == id) return slot;
  380. }
  381. }
  382. return 0;
  383. }
  384. void Inventory::localTransaction(Array<ItemSlot*>* zSourceSlots,
  385. Array<ItemSlot*>* zTargetSlots,
  386. ItemFilter* zFilter,
  387. int count,
  388. Direction outDir,
  389. Direction inDir)
  390. {
  391. if (itemCache)
  392. {
  393. cs.lock();
  394. auto sourceSlot
  395. = zSourceSlots ? zSourceSlots->begin() : pullSlotsOrder->begin();
  396. while (true)
  397. {
  398. while (sourceSlot
  399. && (sourceSlot->getNumberOfItems() == 0
  400. || (zFilter && !zFilter->matchSourceSlot(sourceSlot))))
  401. sourceSlot++;
  402. if (!sourceSlot)
  403. {
  404. cs.unlock();
  405. return;
  406. }
  407. bool needNext = 1;
  408. for (auto targetSlot = zTargetSlots->begin(); targetSlot;)
  409. {
  410. while (
  411. targetSlot
  412. && (targetSlot->isFull()
  413. || (zFilter && !zFilter->matchTargetSlot(targetSlot))))
  414. targetSlot++;
  415. if (!targetSlot) break;
  416. needNext &= !Inventory::unsafeMove(
  417. this, this, sourceSlot, targetSlot, outDir, inDir, count);
  418. if (count == 0)
  419. {
  420. cs.unlock();
  421. return;
  422. }
  423. if (sourceSlot->getNumberOfItems() == 0) break;
  424. }
  425. if (needNext) sourceSlot++;
  426. }
  427. cs.unlock();
  428. }
  429. }
  430. void Inventory::addItems(ItemStack* zItems, Direction dir)
  431. {
  432. if (itemCache && zItems && zItems->getSize() > 0)
  433. {
  434. cs.lock();
  435. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot;
  436. targetSlot++)
  437. {
  438. if (!targetSlot->isFull())
  439. {
  440. if (targetSlot->zStack())
  441. {
  442. if (targetSlot->zStack()->zItem()->canBeStackedWith(
  443. zItems->zItem()))
  444. {
  445. int number
  446. = MIN(targetSlot->numberOfAddableItems(zItems, dir),
  447. zItems->getSize());
  448. int tmp = number;
  449. if (number > 0
  450. && allowPushStack(
  451. targetSlot, dir, zItems->zItem(), tmp))
  452. {
  453. number = MIN(number, tmp);
  454. ItemStack* stack = zItems->split(number);
  455. if (stack)
  456. {
  457. targetSlot->addItems(stack, dir);
  458. afterPushStack(targetSlot,
  459. dir,
  460. targetSlot->zStack()->zItem(),
  461. number);
  462. if (stack->getSize()) throw stack;
  463. stack->release();
  464. if (!zItems->getSize()) break;
  465. }
  466. }
  467. }
  468. }
  469. else
  470. {
  471. int number
  472. = MIN(targetSlot->numberOfAddableItems(zItems, dir),
  473. zItems->getSize());
  474. int tmp = number;
  475. if (number > 0
  476. && allowPushStack(
  477. targetSlot, dir, zItems->zItem(), tmp))
  478. {
  479. number = MIN(number, tmp);
  480. ItemStack* stack = zItems->split(number);
  481. if (stack)
  482. {
  483. targetSlot->addItems(stack, dir);
  484. updateCache(targetSlot, -1);
  485. afterPushStack(targetSlot,
  486. dir,
  487. targetSlot->zStack()->zItem(),
  488. number);
  489. if (stack->getSize()) throw stack;
  490. stack->release();
  491. if (!zItems->getSize()) break;
  492. }
  493. }
  494. }
  495. }
  496. }
  497. cs.unlock();
  498. }
  499. }
  500. void Inventory::addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir)
  501. {
  502. if (zSlot->zStack()
  503. && !zSlot->zStack()->zItem()->canBeStackedWith(zItems->zItem()))
  504. return;
  505. bool needUpdate = !zSlot->zStack();
  506. int number
  507. = MIN(zSlot->numberOfAddableItems(zItems, dir), zItems->getSize());
  508. int tmp = number;
  509. if (number > 0 && allowPushStack(zSlot, dir, zItems->zItem(), tmp))
  510. {
  511. number = MIN(number, tmp);
  512. ItemStack* stack = zItems->split(number);
  513. if (stack)
  514. {
  515. zSlot->addItems(stack, dir);
  516. if (needUpdate) updateCache(zSlot, -1);
  517. afterPushStack(zSlot, dir, zSlot->zStack()->zItem(), number);
  518. if (stack->getSize()) throw stack;
  519. stack->release();
  520. }
  521. }
  522. }
  523. ItemStack* Inventory::takeItemsOut(ItemSlot* zSlot, int count, Direction dir)
  524. {
  525. if (allowPullStack(zSlot, dir))
  526. {
  527. ItemStack* stack = zSlot->takeItemsOut(count, dir);
  528. if (stack)
  529. {
  530. updateCache(zSlot, stack->zItem()->zItemType()->getId());
  531. if (stack->getSize() > 0)
  532. afterPullStack(zSlot, dir, stack->zItem(), stack->getSize());
  533. }
  534. return stack;
  535. }
  536. return 0;
  537. }
  538. InventoryInteraction Inventory::interactWith(
  539. Inventory* zInventory, Direction dir)
  540. {
  541. return InventoryInteraction(this, zInventory, dir);
  542. }
  543. void Inventory::unsaveAddItem(ItemStack* zStack, Direction dir)
  544. {
  545. addItems(zStack, dir);
  546. }
  547. int Inventory::numberOfAddableItems(
  548. const ItemStack* zStack, Direction dir) const
  549. {
  550. int count = 0;
  551. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
  552. {
  553. int maxCount = targetSlot->numberOfAddableItems(zStack, dir);
  554. int allowed = maxCount;
  555. if (allowPushStack(targetSlot, dir, zStack->zItem(), allowed))
  556. count += MIN(maxCount, allowed);
  557. }
  558. return count;
  559. }
  560. Framework::Iterator<ItemSlot*> Inventory::begin()
  561. {
  562. return pullSlotsOrder->begin();
  563. }
  564. Framework::Iterator<ItemSlot*> Inventory::end()
  565. {
  566. return pullSlotsOrder->end();
  567. }
  568. void Inventory::inventoryApi(Framework::StreamReader* zRequest,
  569. NetworkMessage* zResponse,
  570. Entity* zSource)
  571. {
  572. char type;
  573. zRequest->lese(&type, 1);
  574. switch (type)
  575. {
  576. case 0: // request inventory
  577. {
  578. char idLen;
  579. zRequest->lese(&idLen, 1);
  580. char* id = new char[idLen + 1];
  581. zRequest->lese(id, idLen);
  582. id[(int)idLen] = 0;
  583. zResponse->addressGui(id);
  584. addObserver(zSource, id);
  585. delete[] id;
  586. char filterLen;
  587. zRequest->lese(&filterLen, 1);
  588. char* filter = new char[filterLen + 1];
  589. zRequest->lese(filter, filterLen);
  590. filter[(int)filterLen] = 0;
  591. InMemoryBuffer buffer;
  592. int count = 0;
  593. for (ItemSlot* slot : *this)
  594. {
  595. if (slot->getName().istGleich(filter))
  596. {
  597. count++;
  598. int id = slot->getId();
  599. buffer.schreibe((char*)&id, 4);
  600. int itemCount = slot->getNumberOfItems();
  601. buffer.schreibe((char*)&itemCount, 4);
  602. if (itemCount > 0)
  603. {
  604. float f = slot->zStack()->zItem()->getHp();
  605. buffer.schreibe((char*)&f, 4);
  606. f = slot->zStack()->zItem()->getMaxHp();
  607. buffer.schreibe((char*)&f, 4);
  608. f = slot->zStack()->zItem()->getDurability();
  609. buffer.schreibe((char*)&f, 4);
  610. f = slot->zStack()->zItem()->getMaxDurability();
  611. buffer.schreibe((char*)&f, 4);
  612. int id = slot->zStack()->zItem()->zItemType()->getId();
  613. buffer.schreibe((char*)&id, 4);
  614. }
  615. }
  616. }
  617. delete[] filter;
  618. char* msg = new char[5 + buffer.getSize()];
  619. msg[0] = 0;
  620. *(int*)(msg + 1) = count;
  621. buffer.lese(msg + 5, (int)buffer.getSize());
  622. zResponse->setMessage(msg, 5 + (int)buffer.getSize());
  623. break;
  624. }
  625. case 1: // remove Observer
  626. {
  627. char idLen;
  628. zRequest->lese(&idLen, 1);
  629. char* id = new char[idLen + 1];
  630. zRequest->lese(id, idLen);
  631. id[(int)idLen] = 0;
  632. removeObserver(zSource, id);
  633. delete[] id;
  634. break;
  635. }
  636. case 2: // request item tooltip
  637. {
  638. char idLen;
  639. zRequest->lese(&idLen, 1);
  640. char* id = new char[idLen + 1];
  641. zRequest->lese(id, idLen);
  642. id[(int)idLen] = 0;
  643. zResponse->addressGui(id);
  644. delete[] id;
  645. int slotId;
  646. zRequest->lese((char*)&slotId, 4);
  647. Text uiml;
  648. for (ItemSlot* slot : *pullSlotsOrder)
  649. {
  650. if (slot->getId() == slotId)
  651. {
  652. if (slot->zStack() && slot->zStack()->zItem())
  653. uiml = slot->zStack()->zItem()->getTooltipUIML();
  654. }
  655. }
  656. short len = (short)uiml.getLength();
  657. char* buffer = new char[uiml.getLength() + 7];
  658. buffer[0] = 3;
  659. *(int*)(buffer + 1) = slotId;
  660. *(short*)(buffer + 5) = len;
  661. memcpy(buffer + 7, uiml, len);
  662. zResponse->setMessage(buffer, len + 7);
  663. break;
  664. }
  665. }
  666. }
  667. void Inventory::registerAfterPullStackCall(std::function<void(
  668. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  669. {
  670. afterPullStackCalls.add(call);
  671. }
  672. void Inventory::registerAfterPushStackCall(std::function<void(
  673. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  674. {
  675. afterPushStackCalls.add(call);
  676. }
  677. void Inventory::registerObserverAddedCall(
  678. std::function<void(Entity* zSource, Framework::Text id)> call)
  679. {
  680. observerAddedCalls.add(call);
  681. }
  682. bool Inventory::unsafeMove(Inventory* zSource,
  683. Inventory* zTarget,
  684. Iterator<ItemSlot*>& sourceSlot,
  685. Iterator<ItemSlot*>& targetSlot,
  686. Direction outDir,
  687. Direction inDir,
  688. int& count)
  689. {
  690. if (targetSlot->zStack())
  691. {
  692. if (sourceSlot->zStack()->zItem()->canBeStackedWith(
  693. targetSlot->zStack()->zItem()))
  694. {
  695. int number = MIN(
  696. targetSlot->numberOfAddableItems(sourceSlot->zStack(), outDir),
  697. count);
  698. int tmp = number;
  699. if (number > 0 && zSource->allowPullStack(sourceSlot, outDir)
  700. && zTarget->allowPushStack(
  701. targetSlot, inDir, sourceSlot->zStack()->zItem(), tmp))
  702. {
  703. number = MIN(number, tmp);
  704. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  705. if (stack)
  706. {
  707. targetSlot->addItems(stack, inDir);
  708. zSource->updateCache(sourceSlot,
  709. targetSlot->zStack()->zItem()->zItemType()->getId());
  710. zSource->afterPullStack(sourceSlot,
  711. outDir,
  712. targetSlot->zStack()->zItem(),
  713. number);
  714. zTarget->afterPushStack(targetSlot,
  715. inDir,
  716. targetSlot->zStack()->zItem(),
  717. number);
  718. if (stack->getSize()) throw stack;
  719. stack->release();
  720. count -= number;
  721. return 1;
  722. }
  723. else
  724. targetSlot++;
  725. }
  726. else
  727. targetSlot++;
  728. }
  729. else
  730. targetSlot++;
  731. }
  732. else
  733. {
  734. int number = MIN(
  735. targetSlot->numberOfAddableItems(sourceSlot->zStack(), outDir),
  736. count);
  737. int tmp = number;
  738. if (number > 0 && zSource->allowPullStack(sourceSlot, outDir)
  739. && zTarget->allowPushStack(
  740. targetSlot, inDir, sourceSlot->zStack()->zItem(), tmp))
  741. {
  742. number = MIN(number, tmp);
  743. if (number > 0)
  744. {
  745. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  746. if (stack)
  747. {
  748. targetSlot->addItems(stack, inDir);
  749. zSource->updateCache(sourceSlot,
  750. targetSlot->zStack()->zItem()->zItemType()->getId());
  751. zTarget->updateCache(targetSlot, -1);
  752. zSource->afterPullStack(sourceSlot,
  753. outDir,
  754. targetSlot->zStack()->zItem(),
  755. number);
  756. zTarget->afterPushStack(targetSlot,
  757. inDir,
  758. targetSlot->zStack()->zItem(),
  759. number);
  760. if (stack->getSize()) throw stack;
  761. stack->release();
  762. count -= number;
  763. return 1;
  764. }
  765. else
  766. targetSlot++;
  767. }
  768. else
  769. targetSlot++;
  770. }
  771. else
  772. targetSlot++;
  773. }
  774. return 0;
  775. }