Inventory.cpp 19 KB

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