Inventory.cpp 27 KB

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