Inventory.cpp 18 KB

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