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 "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
  377. = StaticRegistry<ItemType>::INSTANCE.zElement(id)->loadItem(
  378. 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::Iterator<ItemSlot*> Inventory::begin()
  660. {
  661. return pullSlotsOrder->begin();
  662. }
  663. Framework::Iterator<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. Iterator<ItemSlot*>& sourceSlot,
  800. Iterator<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. }