Inventory.cpp 27 KB

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