Inventory.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. MultipleInventoryLock::MultipleInventoryLock(Inventory** inventories, int count)
  152. : inventories(new Inventory*[count]),
  153. count(count),
  154. locked(0)
  155. {
  156. // sort given inventories in locking order
  157. bool used[count];
  158. memset(&used, 0, count);
  159. // TODO: use a performant sorting algorithm
  160. for (int i = 0; i < count; i++)
  161. {
  162. Inventory* min = 0;
  163. int minJ = 0;
  164. for (int j = 0; j < count; j++)
  165. {
  166. if (!used[j])
  167. {
  168. if (!min)
  169. {
  170. min = inventories[j];
  171. minJ = j;
  172. continue;
  173. }
  174. if (inventories[j]->location.x < min->location.x)
  175. {
  176. min = inventories[j];
  177. minJ = j;
  178. continue;
  179. }
  180. if (inventories[j]->location.x == min->location.x)
  181. {
  182. if (inventories[j]->location.y < min->location.y)
  183. {
  184. min = inventories[j];
  185. minJ = j;
  186. continue;
  187. }
  188. if (inventories[j]->location.y == min->location.y)
  189. {
  190. if (inventories[j]->location.z < min->location.z)
  191. {
  192. min = inventories[j];
  193. minJ = j;
  194. continue;
  195. }
  196. }
  197. }
  198. }
  199. }
  200. this->inventories[i] = min;
  201. used[minJ] = 1;
  202. }
  203. lock();
  204. }
  205. MultipleInventoryLock::~MultipleInventoryLock()
  206. {
  207. unlock();
  208. delete[] inventories;
  209. }
  210. void MultipleInventoryLock::unlock()
  211. {
  212. if (locked)
  213. {
  214. locked = 0;
  215. for (int i = count - 1; i >= 0; i--)
  216. {
  217. inventories[i]->cs.unlock();
  218. }
  219. }
  220. }
  221. void MultipleInventoryLock::lock()
  222. {
  223. if (!locked)
  224. {
  225. locked = 1;
  226. for (int i = 0; i < count; i++)
  227. {
  228. inventories[i]->cs.lock();
  229. }
  230. }
  231. }
  232. Inventory::Inventory(const Framework::Vec3<float> location, bool hasInventory)
  233. : ReferenceCounter(),
  234. nextSlotId(1),
  235. location(location)
  236. {
  237. if (hasInventory)
  238. {
  239. pullSlotsOrder = new Framework::RCArray<ItemSlot>();
  240. pushSlotsOrder = new Framework::RCArray<ItemSlot>();
  241. itemCache = new Framework::HashMap<int, Framework::Array<ItemSlot*>*>(
  242. ITEM_CACHE_SIZE, std::_Identity<int>());
  243. }
  244. else
  245. {
  246. pullSlotsOrder = 0;
  247. pushSlotsOrder = 0;
  248. itemCache = 0;
  249. }
  250. }
  251. Inventory::~Inventory()
  252. {
  253. if (pullSlotsOrder) pullSlotsOrder->release();
  254. if (pushSlotsOrder) pushSlotsOrder->release();
  255. if (itemCache) itemCache->release();
  256. }
  257. void Inventory::updateCache(ItemSlot* zSlot, int beforeKey)
  258. {
  259. if (!itemCache) return;
  260. int key
  261. = zSlot->zStack() ? zSlot->zStack()->zItem()->zItemType()->getId() : -1;
  262. if (key == beforeKey) return;
  263. if (beforeKey >= 0)
  264. {
  265. auto tmp = itemCache->safeGet(key, 0);
  266. if (tmp) tmp->removeValue(zSlot);
  267. }
  268. if (zSlot->zStack())
  269. {
  270. auto tmp = itemCache->safeGet(key, 0);
  271. if (!tmp)
  272. {
  273. tmp = new Array<ItemSlot*>();
  274. itemCache->put(key, tmp);
  275. }
  276. tmp->add(zSlot, 0);
  277. }
  278. }
  279. void Inventory::addSlot(ItemSlot* slot)
  280. {
  281. cs.lock();
  282. ((ItemSlotIDSetter*)slot)->setId(nextSlotId++);
  283. int pullPrio = slot->getPullPriority();
  284. int pushPrio = slot->getPushPriority();
  285. int index = 0;
  286. for (auto stack : *pullSlotsOrder)
  287. {
  288. if (stack->getPullPriority() > pullPrio) break;
  289. index++;
  290. }
  291. pullSlotsOrder->add(dynamic_cast<ItemSlot*>(slot->getThis()), index);
  292. index = 0;
  293. for (auto stack : *pushSlotsOrder)
  294. {
  295. if (stack->getPushPriority() > pushPrio) break;
  296. index++;
  297. }
  298. pushSlotsOrder->add(slot, index);
  299. updateCache(slot, -1);
  300. cs.unlock();
  301. }
  302. bool Inventory::allowPullStack(ItemSlot* zSlot, Direction dir) const
  303. {
  304. return pullSlotsOrder;
  305. }
  306. bool Inventory::allowPushStack(
  307. ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const
  308. {
  309. return pushSlotsOrder;
  310. }
  311. void Inventory::afterPullStack(
  312. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  313. {
  314. NetworkMessage* msg = new NetworkMessage();
  315. char* message = new char[9];
  316. message[0] = 1; // set count of items
  317. *(int*)(message + 1) = zSlot->getId();
  318. *(int*)(message + 5) = zSlot->getNumberOfItems();
  319. msg->setMessage(message, 9);
  320. notifyObservers(msg);
  321. for (auto call : afterPullStackCalls)
  322. call(zSlot, dir, zItem, count);
  323. }
  324. void Inventory::afterPushStack(
  325. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  326. {
  327. if (zSlot->getNumberOfItems() > count)
  328. {
  329. NetworkMessage* msg = new NetworkMessage();
  330. char* message = new char[9];
  331. message[0] = 1; // set count of items
  332. *(int*)(message + 1) = zSlot->getId();
  333. *(int*)(message + 5) = zSlot->getNumberOfItems();
  334. msg->setMessage(message, 9);
  335. notifyObservers(msg);
  336. }
  337. else
  338. {
  339. NetworkMessage* msg = new NetworkMessage();
  340. char* message = new char[29];
  341. message[0] = 2; // add new stack
  342. *(int*)(message + 1) = zSlot->getId();
  343. *(int*)(message + 5) = zSlot->getNumberOfItems();
  344. const Item* zItem = zSlot->zStack()->zItem();
  345. *(float*)(message + 9) = zItem->getHp();
  346. *(float*)(message + 13) = zItem->getMaxHp();
  347. *(float*)(message + 17) = zItem->getDurability();
  348. *(float*)(message + 21) = zItem->getMaxDurability();
  349. *(int*)(message + 25) = zItem->zItemType()->getId();
  350. msg->setMessage(message, 29);
  351. notifyObservers(msg);
  352. }
  353. for (auto call : afterPushStackCalls)
  354. call(zSlot, dir, zItem, count);
  355. }
  356. void Inventory::loadInventory(Framework::StreamReader* zReader)
  357. {
  358. if (itemCache)
  359. {
  360. for (auto stack : *pushSlotsOrder)
  361. {
  362. int size = 0;
  363. zReader->lese((char*)&size, 4);
  364. if (size != 0)
  365. {
  366. int id = 0;
  367. zReader->lese((char*)&id, 4);
  368. Item* item
  369. = StaticRegistry<ItemType>::INSTANCE.zElement(id)->loadItem(
  370. zReader);
  371. stack->addItems(new ItemStack(item, size), NO_DIRECTION);
  372. }
  373. }
  374. }
  375. }
  376. void Inventory::saveInventory(Framework::StreamWriter* zWriter)
  377. {
  378. if (itemCache)
  379. {
  380. for (auto slot : *pushSlotsOrder)
  381. {
  382. const ItemStack* stack = slot->zStack();
  383. int value = 0;
  384. if (!stack || !stack->zItem())
  385. {
  386. zWriter->schreibe((char*)&value, 4);
  387. }
  388. else
  389. {
  390. value = stack->getSize();
  391. zWriter->schreibe((char*)&value, 4);
  392. value = stack->zItem()->zItemType()->getId();
  393. zWriter->schreibe((char*)&value, 4);
  394. stack->zItem()->zItemType()->saveItem(stack->zItem(), zWriter);
  395. }
  396. }
  397. }
  398. }
  399. void Inventory::notifyObservers(NetworkMessage* msg)
  400. {
  401. cs.lock();
  402. int index = 0;
  403. Array<int> toDelete;
  404. for (auto observer : observers)
  405. {
  406. Entity* e = Game::INSTANCE->zEntity(observer.getFirst());
  407. if (e)
  408. {
  409. msg->addressGui(observer.getSecond());
  410. Game::INSTANCE->sendMessage(msg->clone(), e);
  411. }
  412. else
  413. toDelete.add(index, 0);
  414. index++;
  415. }
  416. for (int i : toDelete)
  417. observers.remove(i);
  418. cs.unlock();
  419. msg->release();
  420. }
  421. void Inventory::removeObserver(Entity* zSource, Framework::Text id)
  422. {
  423. cs.lock();
  424. int index = 0;
  425. for (auto observer : observers)
  426. {
  427. if (observer.getFirst() == zSource->getId()
  428. && observer.getSecond().istGleich(id))
  429. {
  430. observers.remove(index);
  431. break;
  432. }
  433. index++;
  434. }
  435. cs.unlock();
  436. }
  437. void Inventory::addObserver(Entity* zSource, Framework::Text id)
  438. {
  439. cs.lock();
  440. for (auto observer : observers)
  441. {
  442. if (observer.getFirst() == zSource->getId()
  443. && observer.getSecond().istGleich(id))
  444. {
  445. cs.unlock();
  446. return;
  447. }
  448. }
  449. observers.add(ImmutablePair<int, Text>(zSource->getId(), id));
  450. cs.unlock();
  451. for (auto call : observerAddedCalls)
  452. call(zSource, id);
  453. }
  454. const ItemSlot* Inventory::zSlot(int id) const
  455. {
  456. if (itemCache)
  457. {
  458. for (auto slot : *pushSlotsOrder)
  459. {
  460. if (slot->getId() == id) return slot;
  461. }
  462. }
  463. return 0;
  464. }
  465. void Inventory::localTransaction(Array<ItemSlot*>* zSourceSlots,
  466. Array<ItemSlot*>* zTargetSlots,
  467. ItemFilter* zFilter,
  468. int count,
  469. Direction outDir,
  470. Direction inDir)
  471. {
  472. if (itemCache)
  473. {
  474. cs.lock();
  475. auto sourceSlot
  476. = zSourceSlots ? zSourceSlots->begin() : pullSlotsOrder->begin();
  477. while (true)
  478. {
  479. while (sourceSlot
  480. && (sourceSlot->getNumberOfItems() == 0
  481. || (zFilter && !zFilter->matchSourceSlot(sourceSlot))))
  482. sourceSlot++;
  483. if (!sourceSlot)
  484. {
  485. cs.unlock();
  486. return;
  487. }
  488. bool needNext = 1;
  489. for (auto targetSlot = zTargetSlots->begin(); targetSlot;)
  490. {
  491. while (
  492. targetSlot
  493. && (targetSlot->isFull()
  494. || (zFilter && !zFilter->matchTargetSlot(targetSlot))))
  495. targetSlot++;
  496. if (!targetSlot) break;
  497. needNext &= !Inventory::unsafeMove(
  498. this, this, sourceSlot, targetSlot, outDir, inDir, count);
  499. if (count == 0)
  500. {
  501. cs.unlock();
  502. return;
  503. }
  504. if (sourceSlot->getNumberOfItems() == 0) break;
  505. }
  506. if (needNext) sourceSlot++;
  507. }
  508. cs.unlock();
  509. }
  510. }
  511. void Inventory::addItems(ItemStack* zItems, Direction dir)
  512. {
  513. if (itemCache && zItems && zItems->getSize() > 0)
  514. {
  515. cs.lock();
  516. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot;
  517. targetSlot++)
  518. {
  519. if (!targetSlot->isFull())
  520. {
  521. if (targetSlot->zStack())
  522. {
  523. if (targetSlot->zStack()->zItem()->canBeStackedWith(
  524. zItems->zItem()))
  525. {
  526. int number
  527. = MIN(targetSlot->numberOfAddableItems(zItems, dir),
  528. zItems->getSize());
  529. int tmp = number;
  530. if (number > 0
  531. && allowPushStack(
  532. targetSlot, dir, zItems->zItem(), tmp))
  533. {
  534. number = MIN(number, tmp);
  535. ItemStack* stack = zItems->split(number);
  536. if (stack)
  537. {
  538. targetSlot->addItems(stack, dir);
  539. afterPushStack(targetSlot,
  540. dir,
  541. targetSlot->zStack()->zItem(),
  542. number);
  543. if (stack->getSize()) throw stack;
  544. stack->release();
  545. if (!zItems->getSize()) break;
  546. }
  547. }
  548. }
  549. }
  550. else
  551. {
  552. int number
  553. = MIN(targetSlot->numberOfAddableItems(zItems, dir),
  554. zItems->getSize());
  555. int tmp = number;
  556. if (number > 0
  557. && allowPushStack(
  558. targetSlot, dir, zItems->zItem(), tmp))
  559. {
  560. number = MIN(number, tmp);
  561. ItemStack* stack = zItems->split(number);
  562. if (stack)
  563. {
  564. targetSlot->addItems(stack, dir);
  565. updateCache(targetSlot, -1);
  566. afterPushStack(targetSlot,
  567. dir,
  568. targetSlot->zStack()->zItem(),
  569. number);
  570. if (stack->getSize()) throw stack;
  571. stack->release();
  572. if (!zItems->getSize()) break;
  573. }
  574. }
  575. }
  576. }
  577. }
  578. cs.unlock();
  579. }
  580. }
  581. void Inventory::addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir)
  582. {
  583. if (zSlot->zStack()
  584. && !zSlot->zStack()->zItem()->canBeStackedWith(zItems->zItem()))
  585. return;
  586. bool needUpdate = !zSlot->zStack();
  587. int number
  588. = MIN(zSlot->numberOfAddableItems(zItems, dir), zItems->getSize());
  589. int tmp = number;
  590. if (number > 0 && allowPushStack(zSlot, dir, zItems->zItem(), tmp))
  591. {
  592. number = MIN(number, tmp);
  593. ItemStack* stack = zItems->split(number);
  594. if (stack)
  595. {
  596. zSlot->addItems(stack, dir);
  597. if (needUpdate) updateCache(zSlot, -1);
  598. afterPushStack(zSlot, dir, zSlot->zStack()->zItem(), number);
  599. if (stack->getSize()) throw stack;
  600. stack->release();
  601. }
  602. }
  603. }
  604. ItemStack* Inventory::takeItemsOut(ItemSlot* zSlot, int count, Direction dir)
  605. {
  606. if (allowPullStack(zSlot, dir))
  607. {
  608. ItemStack* stack = zSlot->takeItemsOut(count, dir);
  609. if (stack)
  610. {
  611. updateCache(zSlot, stack->zItem()->zItemType()->getId());
  612. if (stack->getSize() > 0)
  613. afterPullStack(zSlot, dir, stack->zItem(), stack->getSize());
  614. }
  615. return stack;
  616. }
  617. return 0;
  618. }
  619. InventoryInteraction Inventory::interactWith(
  620. Inventory* zInventory, Direction dir)
  621. {
  622. return InventoryInteraction(this, zInventory, dir);
  623. }
  624. void Inventory::unsaveAddItem(ItemStack* zStack, Direction dir)
  625. {
  626. addItems(zStack, dir);
  627. }
  628. int Inventory::numberOfAddableItems(
  629. const ItemStack* zStack, Direction dir) const
  630. {
  631. int count = 0;
  632. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
  633. {
  634. int maxCount = targetSlot->numberOfAddableItems(zStack, dir);
  635. int allowed = maxCount;
  636. if (allowPushStack(targetSlot, dir, zStack->zItem(), allowed))
  637. count += MIN(maxCount, allowed);
  638. }
  639. return count;
  640. }
  641. Framework::Iterator<ItemSlot*> Inventory::begin()
  642. {
  643. return pullSlotsOrder->begin();
  644. }
  645. Framework::Iterator<ItemSlot*> Inventory::end()
  646. {
  647. return pullSlotsOrder->end();
  648. }
  649. void Inventory::inventoryApi(Framework::StreamReader* zRequest,
  650. NetworkMessage* zResponse,
  651. Entity* zSource)
  652. {
  653. char type;
  654. zRequest->lese(&type, 1);
  655. switch (type)
  656. {
  657. case 0: // request inventory
  658. {
  659. char idLen;
  660. zRequest->lese(&idLen, 1);
  661. char* id = new char[idLen + 1];
  662. zRequest->lese(id, idLen);
  663. id[(int)idLen] = 0;
  664. zResponse->addressGui(id);
  665. addObserver(zSource, id);
  666. delete[] id;
  667. char filterLen;
  668. zRequest->lese(&filterLen, 1);
  669. char* filter = new char[filterLen + 1];
  670. zRequest->lese(filter, filterLen);
  671. filter[(int)filterLen] = 0;
  672. InMemoryBuffer buffer;
  673. int count = 0;
  674. for (ItemSlot* slot : *this)
  675. {
  676. if (slot->getName().istGleich(filter))
  677. {
  678. count++;
  679. int id = slot->getId();
  680. buffer.schreibe((char*)&id, 4);
  681. int itemCount = slot->getNumberOfItems();
  682. buffer.schreibe((char*)&itemCount, 4);
  683. if (itemCount > 0)
  684. {
  685. float f = slot->zStack()->zItem()->getHp();
  686. buffer.schreibe((char*)&f, 4);
  687. f = slot->zStack()->zItem()->getMaxHp();
  688. buffer.schreibe((char*)&f, 4);
  689. f = slot->zStack()->zItem()->getDurability();
  690. buffer.schreibe((char*)&f, 4);
  691. f = slot->zStack()->zItem()->getMaxDurability();
  692. buffer.schreibe((char*)&f, 4);
  693. int id = slot->zStack()->zItem()->zItemType()->getId();
  694. buffer.schreibe((char*)&id, 4);
  695. }
  696. }
  697. }
  698. delete[] filter;
  699. char* msg = new char[5 + buffer.getSize()];
  700. msg[0] = 0;
  701. *(int*)(msg + 1) = count;
  702. buffer.lese(msg + 5, (int)buffer.getSize());
  703. zResponse->setMessage(msg, 5 + (int)buffer.getSize());
  704. break;
  705. }
  706. case 1: // remove Observer
  707. {
  708. char idLen;
  709. zRequest->lese(&idLen, 1);
  710. char* id = new char[idLen + 1];
  711. zRequest->lese(id, idLen);
  712. id[(int)idLen] = 0;
  713. removeObserver(zSource, id);
  714. delete[] id;
  715. break;
  716. }
  717. case 2: // request item tooltip
  718. {
  719. char idLen;
  720. zRequest->lese(&idLen, 1);
  721. char* id = new char[idLen + 1];
  722. zRequest->lese(id, idLen);
  723. id[(int)idLen] = 0;
  724. zResponse->addressGui(id);
  725. delete[] id;
  726. int slotId;
  727. zRequest->lese((char*)&slotId, 4);
  728. Text uiml;
  729. for (ItemSlot* slot : *pullSlotsOrder)
  730. {
  731. if (slot->getId() == slotId)
  732. {
  733. if (slot->zStack() && slot->zStack()->zItem())
  734. uiml = slot->zStack()->zItem()->getTooltipUIML();
  735. }
  736. }
  737. short len = (short)uiml.getLength();
  738. char* buffer = new char[uiml.getLength() + 7];
  739. buffer[0] = 3;
  740. *(int*)(buffer + 1) = slotId;
  741. *(short*)(buffer + 5) = len;
  742. memcpy(buffer + 7, uiml, len);
  743. zResponse->setMessage(buffer, len + 7);
  744. break;
  745. }
  746. }
  747. }
  748. void Inventory::registerAfterPullStackCall(std::function<void(
  749. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  750. {
  751. afterPullStackCalls.add(call);
  752. }
  753. void Inventory::registerAfterPushStackCall(std::function<void(
  754. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  755. {
  756. afterPushStackCalls.add(call);
  757. }
  758. void Inventory::registerObserverAddedCall(
  759. std::function<void(Entity* zSource, Framework::Text id)> call)
  760. {
  761. observerAddedCalls.add(call);
  762. }
  763. bool Inventory::unsafeMove(Inventory* zSource,
  764. Inventory* zTarget,
  765. Iterator<ItemSlot*>& sourceSlot,
  766. Iterator<ItemSlot*>& targetSlot,
  767. Direction outDir,
  768. Direction inDir,
  769. int& count)
  770. {
  771. if (targetSlot->zStack())
  772. {
  773. if (sourceSlot->zStack()->zItem()->canBeStackedWith(
  774. targetSlot->zStack()->zItem()))
  775. {
  776. int number = MIN(
  777. targetSlot->numberOfAddableItems(sourceSlot->zStack(), outDir),
  778. count);
  779. int tmp = number;
  780. if (number > 0 && zSource->allowPullStack(sourceSlot, outDir)
  781. && zTarget->allowPushStack(
  782. targetSlot, inDir, sourceSlot->zStack()->zItem(), tmp))
  783. {
  784. number = MIN(number, tmp);
  785. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  786. if (stack)
  787. {
  788. targetSlot->addItems(stack, inDir);
  789. zSource->updateCache(sourceSlot,
  790. targetSlot->zStack()->zItem()->zItemType()->getId());
  791. zSource->afterPullStack(sourceSlot,
  792. outDir,
  793. targetSlot->zStack()->zItem(),
  794. number);
  795. zTarget->afterPushStack(targetSlot,
  796. inDir,
  797. targetSlot->zStack()->zItem(),
  798. number);
  799. if (stack->getSize()) throw stack;
  800. stack->release();
  801. count -= number;
  802. return 1;
  803. }
  804. else
  805. targetSlot++;
  806. }
  807. else
  808. targetSlot++;
  809. }
  810. else
  811. targetSlot++;
  812. }
  813. else
  814. {
  815. int number = MIN(
  816. targetSlot->numberOfAddableItems(sourceSlot->zStack(), outDir),
  817. count);
  818. int tmp = number;
  819. if (number > 0 && zSource->allowPullStack(sourceSlot, outDir)
  820. && zTarget->allowPushStack(
  821. targetSlot, inDir, sourceSlot->zStack()->zItem(), tmp))
  822. {
  823. number = MIN(number, tmp);
  824. if (number > 0)
  825. {
  826. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  827. if (stack)
  828. {
  829. targetSlot->addItems(stack, inDir);
  830. zSource->updateCache(sourceSlot,
  831. targetSlot->zStack()->zItem()->zItemType()->getId());
  832. zTarget->updateCache(targetSlot, -1);
  833. zSource->afterPullStack(sourceSlot,
  834. outDir,
  835. targetSlot->zStack()->zItem(),
  836. number);
  837. zTarget->afterPushStack(targetSlot,
  838. inDir,
  839. targetSlot->zStack()->zItem(),
  840. number);
  841. if (stack->getSize()) throw stack;
  842. stack->release();
  843. count -= number;
  844. return 1;
  845. }
  846. else
  847. targetSlot++;
  848. }
  849. else
  850. targetSlot++;
  851. }
  852. else
  853. targetSlot++;
  854. }
  855. return 0;
  856. }