Inventory.cpp 27 KB

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