Inventory.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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
  341. = 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)
  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. {
  526. if (targetSlot->zStack())
  527. {
  528. if (targetSlot->zStack()->zItem()->canBeStackedWith(
  529. zItems->zItem()))
  530. {
  531. int number
  532. = MIN(targetSlot->numberOfAddableItems(zItems, dir),
  533. zItems->getSize());
  534. int tmp = number;
  535. if (number > 0
  536. && allowPushStack(
  537. targetSlot, dir, zItems->zItem(), tmp))
  538. {
  539. number = MIN(number, tmp);
  540. ItemStack* stack = zItems->split(number);
  541. if (stack)
  542. {
  543. targetSlot->addItems(stack, dir);
  544. afterPushStack(targetSlot,
  545. dir,
  546. targetSlot->zStack()->zItem(),
  547. number);
  548. if (stack->getSize()) throw stack;
  549. stack->release();
  550. if (!zItems->getSize()) break;
  551. }
  552. }
  553. }
  554. }
  555. else
  556. {
  557. int number
  558. = MIN(targetSlot->numberOfAddableItems(zItems, dir),
  559. zItems->getSize());
  560. int tmp = number;
  561. if (number > 0
  562. && allowPushStack(
  563. targetSlot, dir, zItems->zItem(), tmp))
  564. {
  565. number = MIN(number, tmp);
  566. ItemStack* stack = zItems->split(number);
  567. if (stack)
  568. {
  569. targetSlot->addItems(stack, dir);
  570. updateCache(targetSlot, -1);
  571. afterPushStack(targetSlot,
  572. dir,
  573. targetSlot->zStack()->zItem(),
  574. number);
  575. if (stack->getSize()) throw stack;
  576. stack->release();
  577. if (!zItems->getSize()) break;
  578. }
  579. }
  580. }
  581. }
  582. }
  583. cs.unlock();
  584. }
  585. }
  586. void Inventory::addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir)
  587. {
  588. if (zSlot->zStack()
  589. && !zSlot->zStack()->zItem()->canBeStackedWith(zItems->zItem()))
  590. return;
  591. bool needUpdate = !zSlot->zStack();
  592. int number
  593. = MIN(zSlot->numberOfAddableItems(zItems, dir), zItems->getSize());
  594. int tmp = number;
  595. if (number > 0 && allowPushStack(zSlot, dir, zItems->zItem(), tmp))
  596. {
  597. number = MIN(number, tmp);
  598. ItemStack* stack = zItems->split(number);
  599. if (stack)
  600. {
  601. zSlot->addItems(stack, dir);
  602. if (needUpdate) updateCache(zSlot, -1);
  603. afterPushStack(zSlot, dir, zSlot->zStack()->zItem(), number);
  604. if (stack->getSize()) throw stack;
  605. stack->release();
  606. }
  607. }
  608. }
  609. ItemStack* Inventory::takeItemsOut(ItemSlot* zSlot, int count, Direction dir)
  610. {
  611. if (allowPullStack(zSlot, dir))
  612. {
  613. ItemStack* stack = zSlot->takeItemsOut(count, dir);
  614. if (stack)
  615. {
  616. updateCache(zSlot, stack->zItem()->zItemType()->getId());
  617. if (stack->getSize() > 0)
  618. afterPullStack(zSlot, dir, stack->zItem(), stack->getSize());
  619. }
  620. return stack;
  621. }
  622. return 0;
  623. }
  624. InventoryInteraction Inventory::interactWith(
  625. Inventory* zInventory, Direction dir)
  626. {
  627. return InventoryInteraction(this, zInventory, dir);
  628. }
  629. void Inventory::unsaveAddItem(ItemStack* zStack, Direction dir)
  630. {
  631. addItems(zStack, dir);
  632. }
  633. int Inventory::numberOfAddableItems(
  634. const ItemStack* zStack, Direction dir) const
  635. {
  636. int count = 0;
  637. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
  638. {
  639. int maxCount = targetSlot->numberOfAddableItems(zStack, dir);
  640. int allowed = maxCount;
  641. if (allowPushStack(targetSlot, dir, zStack->zItem(), allowed))
  642. count += MIN(maxCount, allowed);
  643. }
  644. return count;
  645. }
  646. Framework::Iterator<ItemSlot*> Inventory::begin()
  647. {
  648. return pullSlotsOrder->begin();
  649. }
  650. Framework::Iterator<ItemSlot*> Inventory::end()
  651. {
  652. return pullSlotsOrder->end();
  653. }
  654. void Inventory::inventoryApi(Framework::StreamReader* zRequest,
  655. NetworkMessage* zResponse,
  656. Entity* zSource)
  657. {
  658. char type;
  659. zRequest->lese(&type, 1);
  660. switch (type)
  661. {
  662. case 0: // request inventory
  663. {
  664. char idLen;
  665. zRequest->lese(&idLen, 1);
  666. char* id = new char[idLen + 1];
  667. zRequest->lese(id, idLen);
  668. id[(int)idLen] = 0;
  669. zResponse->addressGui(id);
  670. addObserver(zSource, id);
  671. delete[] id;
  672. char filterLen;
  673. zRequest->lese(&filterLen, 1);
  674. char* filter = new char[filterLen + 1];
  675. zRequest->lese(filter, filterLen);
  676. filter[(int)filterLen] = 0;
  677. InMemoryBuffer buffer;
  678. int count = 0;
  679. for (ItemSlot* slot : *this)
  680. {
  681. if (slot->getName().istGleich(filter))
  682. {
  683. count++;
  684. int id = slot->getId();
  685. buffer.schreibe((char*)&id, 4);
  686. int itemCount = slot->getNumberOfItems();
  687. buffer.schreibe((char*)&itemCount, 4);
  688. if (itemCount > 0)
  689. {
  690. float f = slot->zStack()->zItem()->getHp();
  691. buffer.schreibe((char*)&f, 4);
  692. f = slot->zStack()->zItem()->getMaxHp();
  693. buffer.schreibe((char*)&f, 4);
  694. f = slot->zStack()->zItem()->getDurability();
  695. buffer.schreibe((char*)&f, 4);
  696. f = slot->zStack()->zItem()->getMaxDurability();
  697. buffer.schreibe((char*)&f, 4);
  698. int id = slot->zStack()->zItem()->zItemType()->getId();
  699. buffer.schreibe((char*)&id, 4);
  700. char len = (char)slot->zStack()
  701. ->zItem()
  702. ->getName()
  703. .getLength();
  704. buffer.schreibe((char*)&len, 1);
  705. buffer.schreibe(
  706. slot->zStack()->zItem()->getName().getText(),
  707. slot->zStack()->zItem()->getName().getLength());
  708. }
  709. }
  710. }
  711. delete[] filter;
  712. char* msg = new char[5 + buffer.getSize()];
  713. msg[0] = 0;
  714. *(int*)(msg + 1) = count;
  715. buffer.lese(msg + 5, (int)buffer.getSize());
  716. zResponse->setMessage(msg, 5 + (int)buffer.getSize());
  717. break;
  718. }
  719. case 1: // remove Observer
  720. {
  721. char idLen;
  722. zRequest->lese(&idLen, 1);
  723. char* id = new char[idLen + 1];
  724. zRequest->lese(id, idLen);
  725. id[(int)idLen] = 0;
  726. removeObserver(zSource, id);
  727. delete[] id;
  728. break;
  729. }
  730. case 2: // request item tooltip
  731. {
  732. char idLen;
  733. zRequest->lese(&idLen, 1);
  734. char* id = new char[idLen + 1];
  735. zRequest->lese(id, idLen);
  736. id[(int)idLen] = 0;
  737. zResponse->addressGui(id);
  738. delete[] id;
  739. int slotId;
  740. zRequest->lese((char*)&slotId, 4);
  741. Text uiml;
  742. for (ItemSlot* slot : *pullSlotsOrder)
  743. {
  744. if (slot->getId() == slotId)
  745. {
  746. if (slot->zStack() && slot->zStack()->zItem())
  747. uiml = slot->zStack()->zItem()->getTooltipUIML();
  748. }
  749. }
  750. short len = (short)uiml.getLength();
  751. char* buffer = new char[uiml.getLength() + 7];
  752. buffer[0] = 3;
  753. *(int*)(buffer + 1) = slotId;
  754. *(short*)(buffer + 5) = len;
  755. memcpy(buffer + 7, uiml, len);
  756. zResponse->setMessage(buffer, len + 7);
  757. break;
  758. }
  759. }
  760. }
  761. void Inventory::registerAfterPullStackCall(std::function<void(
  762. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  763. {
  764. afterPullStackCalls.add(call);
  765. }
  766. void Inventory::registerAfterPushStackCall(std::function<void(
  767. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  768. {
  769. afterPushStackCalls.add(call);
  770. }
  771. void Inventory::registerObserverAddedCall(
  772. std::function<void(Entity* zSource, Framework::Text id)> call)
  773. {
  774. observerAddedCalls.add(call);
  775. }
  776. bool Inventory::unsafeMove(Inventory* zSource,
  777. Inventory* zTarget,
  778. Iterator<ItemSlot*>& sourceSlot,
  779. Iterator<ItemSlot*>& targetSlot,
  780. Direction outDir,
  781. Direction inDir,
  782. int& count)
  783. {
  784. if (targetSlot->zStack())
  785. {
  786. if (sourceSlot->zStack()->zItem()->canBeStackedWith(
  787. targetSlot->zStack()->zItem()))
  788. {
  789. int number = MIN(
  790. targetSlot->numberOfAddableItems(sourceSlot->zStack(), outDir),
  791. count);
  792. int tmp = number;
  793. if (number > 0 && zSource->allowPullStack(sourceSlot, outDir)
  794. && zTarget->allowPushStack(
  795. targetSlot, inDir, sourceSlot->zStack()->zItem(), tmp))
  796. {
  797. number = MIN(number, tmp);
  798. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  799. if (stack)
  800. {
  801. targetSlot->addItems(stack, inDir);
  802. zSource->updateCache(sourceSlot,
  803. targetSlot->zStack()->zItem()->zItemType()->getId());
  804. zSource->afterPullStack(sourceSlot,
  805. outDir,
  806. targetSlot->zStack()->zItem(),
  807. number);
  808. zTarget->afterPushStack(targetSlot,
  809. inDir,
  810. targetSlot->zStack()->zItem(),
  811. number);
  812. if (stack->getSize()) throw stack;
  813. stack->release();
  814. count -= number;
  815. return 1;
  816. }
  817. else
  818. targetSlot++;
  819. }
  820. else
  821. targetSlot++;
  822. }
  823. else
  824. targetSlot++;
  825. }
  826. else
  827. {
  828. int number = MIN(
  829. targetSlot->numberOfAddableItems(sourceSlot->zStack(), outDir),
  830. count);
  831. int tmp = number;
  832. if (number > 0 && zSource->allowPullStack(sourceSlot, outDir)
  833. && zTarget->allowPushStack(
  834. targetSlot, inDir, sourceSlot->zStack()->zItem(), tmp))
  835. {
  836. number = MIN(number, tmp);
  837. if (number > 0)
  838. {
  839. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  840. if (stack)
  841. {
  842. targetSlot->addItems(stack, inDir);
  843. zSource->updateCache(sourceSlot,
  844. targetSlot->zStack()->zItem()->zItemType()->getId());
  845. zTarget->updateCache(targetSlot, -1);
  846. zSource->afterPullStack(sourceSlot,
  847. outDir,
  848. targetSlot->zStack()->zItem(),
  849. number);
  850. zTarget->afterPushStack(targetSlot,
  851. inDir,
  852. targetSlot->zStack()->zItem(),
  853. number);
  854. if (stack->getSize()) throw stack;
  855. stack->release();
  856. count -= number;
  857. return 1;
  858. }
  859. else
  860. targetSlot++;
  861. }
  862. else
  863. targetSlot++;
  864. }
  865. else
  866. targetSlot++;
  867. }
  868. return 0;
  869. }