Inventory.cpp 27 KB

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