Inventory.cpp 27 KB

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