Inventory.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 = new NetworkMessage();
  285. char* message = new char[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);
  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 = new NetworkMessage();
  299. char* message = new char[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);
  304. notyObservers(msg);
  305. }
  306. else
  307. {
  308. NetworkMessage* msg = new NetworkMessage();
  309. char* message = new char[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->getHp();
  315. *(float*)(message + 13) = zItem->getMaxHp();
  316. *(float*)(message + 17) = zItem->getDurability();
  317. *(float*)(message + 21) = zItem->getMaxDurability();
  318. *(int*)(message + 25) = zItem->zItemType()->getId();
  319. msg->setMessage(message, 29);
  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->clone(), 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. msg->release();
  387. }
  388. void Inventory::removeObserver(Entity* zSource, Framework::Text id)
  389. {
  390. cs.lock();
  391. int index = 0;
  392. for (auto observer : observers)
  393. {
  394. if (observer.getFirst() == zSource->getId() && observer.getSecond().istGleich(id))
  395. {
  396. observers.remove(index);
  397. break;
  398. }
  399. index++;
  400. }
  401. cs.unlock();
  402. }
  403. void Inventory::addObserver(Entity* zSource, Framework::Text id)
  404. {
  405. cs.lock();
  406. for (auto observer : observers)
  407. {
  408. if (observer.getFirst() == zSource->getId() && observer.getSecond().istGleich(id))
  409. {
  410. cs.unlock();
  411. return;
  412. }
  413. }
  414. observers.add(ImmutablePair<int, Text>(zSource->getId(), id));
  415. cs.unlock();
  416. for (auto call : observerAddedCalls)
  417. call(zSource, id);
  418. }
  419. const ItemSlot* Inventory::zSlot(int id) const
  420. {
  421. if (itemCache)
  422. {
  423. for (auto slot : *pushSlotsOrder)
  424. {
  425. if (slot->getId() == id)
  426. return slot;
  427. }
  428. }
  429. return 0;
  430. }
  431. void Inventory::localTransaction(Array< ItemSlot* >* zSourceSlots, Array< ItemSlot* >* zTargetSlots, ItemFilter* zFilter, int count, Direction outDir, Direction inDir)
  432. {
  433. if (itemCache)
  434. {
  435. cs.lock();
  436. auto sourceSlot = zSourceSlots ? zSourceSlots->begin() : pullSlotsOrder->begin();
  437. for (auto targetSlot = zTargetSlots->begin(); targetSlot; )
  438. {
  439. int amount = count;
  440. if (!targetSlot->isFull())
  441. {
  442. if (targetSlot->zStack())
  443. {
  444. Array<ItemSlot*>* zSurceListe = itemCache->safeGet(targetSlot->zStack()->zItem()->zItemType()->getId(), 0);
  445. if (zSurceListe)
  446. {
  447. Array<int> toDelete;
  448. int index = 0;
  449. for (auto sourceSlot = zSurceListe->begin(); sourceSlot; sourceSlot++, index++)
  450. {
  451. if (zSourceSlots && zSourceSlots->getWertIndex(sourceSlot) < 0)
  452. continue;
  453. if (zFilter && !zFilter->matchItem(sourceSlot->zStack()->zItem()))
  454. continue;
  455. int number = MIN(targetSlot->numberOfAddableItems(sourceSlot->zStack(), inDir), count);
  456. if (number > 0)
  457. {
  458. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  459. if (stack)
  460. {
  461. if (!sourceSlot->zStack())
  462. toDelete.add(index, 0);
  463. targetSlot->addItems(stack, inDir);
  464. updateCache(sourceSlot, targetSlot->zStack()->zItem()->zItemType()->getId());
  465. afterPullStack(sourceSlot, outDir, targetSlot->zStack()->zItem(), number);
  466. afterPushStack(targetSlot, inDir, targetSlot->zStack()->zItem(), number);
  467. if (stack->getSize())
  468. {
  469. cs.unlock();
  470. throw stack;
  471. }
  472. stack->release();
  473. count -= number;
  474. if (count == 0)
  475. break;
  476. }
  477. }
  478. }
  479. for (auto indexToDelete = toDelete.begin(); indexToDelete; indexToDelete++)
  480. zSurceListe->remove(indexToDelete);
  481. if (count == 0)
  482. {
  483. cs.unlock();
  484. return;
  485. }
  486. }
  487. }
  488. else
  489. {
  490. while (sourceSlot && (!sourceSlot->zStack() || (zFilter && !zFilter->matchItem(sourceSlot->zStack()->zItem()))))
  491. sourceSlot++;
  492. if (!sourceSlot)
  493. {
  494. cs.unlock();
  495. return;
  496. }
  497. int number = MIN(targetSlot->numberOfAddableItems(sourceSlot->zStack(), inDir), count);
  498. if (number > 0)
  499. {
  500. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  501. if (stack)
  502. {
  503. targetSlot->addItems(stack, inDir);
  504. updateCache(sourceSlot, targetSlot->zStack()->zItem()->zItemType()->getId());
  505. updateCache(targetSlot, -1);
  506. afterPullStack(sourceSlot, outDir, targetSlot->zStack()->zItem(), number);
  507. afterPushStack(targetSlot, inDir, targetSlot->zStack()->zItem(), number);
  508. if (stack->getSize())
  509. {
  510. cs.unlock();
  511. throw stack;
  512. }
  513. stack->release();
  514. count -= number;
  515. if (count == 0)
  516. {
  517. cs.unlock();
  518. return;
  519. }
  520. }
  521. }
  522. }
  523. }
  524. if (amount == count || targetSlot->isFull())
  525. targetSlot++;
  526. }
  527. cs.unlock();
  528. }
  529. }
  530. void Inventory::addItems(ItemStack* zItems, Direction dir)
  531. {
  532. if (itemCache && zItems && zItems->getSize() > 0)
  533. {
  534. cs.lock();
  535. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
  536. {
  537. if (!targetSlot->isFull())
  538. {
  539. if (targetSlot->zStack())
  540. {
  541. if (targetSlot->zStack()->zItem()->canBeStackedWith(zItems->zItem()))
  542. {
  543. int number = MIN(targetSlot->numberOfAddableItems(zItems, dir), zItems->getSize());
  544. int tmp = number;
  545. if (number > 0 && allowPushStack(targetSlot, dir, zItems->zItem(), tmp))
  546. {
  547. number = MIN(number, tmp);
  548. ItemStack* stack = zItems->split(number);
  549. if (stack)
  550. {
  551. targetSlot->addItems(stack, dir);
  552. afterPushStack(targetSlot, dir, targetSlot->zStack()->zItem(), number);
  553. if (stack->getSize())
  554. throw stack;
  555. stack->release();
  556. if (!zItems->getSize())
  557. break;
  558. }
  559. }
  560. }
  561. }
  562. else
  563. {
  564. int number = MIN(targetSlot->numberOfAddableItems(zItems, dir), zItems->getSize());
  565. int tmp = number;
  566. if (number > 0 && allowPushStack(targetSlot, dir, zItems->zItem(), tmp))
  567. {
  568. number = MIN(number, tmp);
  569. ItemStack* stack = zItems->split(number);
  570. if (stack)
  571. {
  572. targetSlot->addItems(stack, dir);
  573. updateCache(targetSlot, -1);
  574. afterPushStack(targetSlot, dir, targetSlot->zStack()->zItem(), number);
  575. if (stack->getSize())
  576. throw stack;
  577. stack->release();
  578. if (!zItems->getSize())
  579. break;
  580. }
  581. }
  582. }
  583. }
  584. }
  585. cs.unlock();
  586. }
  587. }
  588. void Inventory::addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir)
  589. {
  590. if (zSlot->zStack() && !zSlot->zStack()->zItem()->canBeStackedWith(zItems->zItem()))
  591. return;
  592. bool needUpdate = !zSlot->zStack();
  593. int number = MIN(zSlot->numberOfAddableItems(zItems, dir), zItems->getSize());
  594. int tmp = number;
  595. if (number > 0 && allowPushStack(zSlot, dir, zItems->zItem(), tmp))
  596. {
  597. number = MIN(number, tmp);
  598. ItemStack* stack = zItems->split(number);
  599. if (stack)
  600. {
  601. zSlot->addItems(stack, dir);
  602. if (needUpdate)
  603. updateCache(zSlot, -1);
  604. afterPushStack(zSlot, dir, zSlot->zStack()->zItem(), number);
  605. if (stack->getSize())
  606. throw stack;
  607. stack->release();
  608. }
  609. }
  610. }
  611. ItemStack* Inventory::takeItemsOut(ItemSlot* zSlot, int count, Direction dir)
  612. {
  613. if (allowPullStack(zSlot, dir))
  614. {
  615. ItemStack* stack = zSlot->takeItemsOut(count, dir);
  616. if (stack)
  617. {
  618. updateCache(zSlot, stack->zItem()->zItemType()->getId());
  619. if (stack->getSize() > 0)
  620. afterPullStack(zSlot, dir, stack->zItem(), stack->getSize());
  621. }
  622. return stack;
  623. }
  624. return 0;
  625. }
  626. InventoryInteraction Inventory::interactWith(Inventory* zInventory, Direction dir)
  627. {
  628. return InventoryInteraction(this, zInventory, dir);
  629. }
  630. void Inventory::unsaveAddItem(ItemStack* zStack, Direction dir)
  631. {
  632. addItems(zStack, dir);
  633. }
  634. int Inventory::numberOfAddableItems(const ItemStack* zStack, Direction dir) const
  635. {
  636. int count = 0;
  637. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
  638. {
  639. int maxCount = targetSlot->numberOfAddableItems(zStack, dir);
  640. int allowed = maxCount;
  641. if (allowPushStack(targetSlot, dir, zStack->zItem(), allowed))
  642. count += MIN(maxCount, allowed);
  643. }
  644. return count;
  645. }
  646. Framework::Iterator<ItemSlot*> Inventory::begin()
  647. {
  648. return pullSlotsOrder->begin();
  649. }
  650. Framework::Iterator<ItemSlot*> Inventory::end()
  651. {
  652. return pullSlotsOrder->end();
  653. }
  654. void Inventory::inventoryApi(Framework::StreamReader* zRequest, NetworkMessage* zResponse, Entity* zSource)
  655. {
  656. char type;
  657. zRequest->lese(&type, 1);
  658. switch (type)
  659. {
  660. case 0: // request inventory
  661. {
  662. char idLen;
  663. zRequest->lese(&idLen, 1);
  664. char* id = new char[idLen + 1];
  665. zRequest->lese(id, idLen);
  666. id[(int)idLen] = 0;
  667. zResponse->addressGui(id);
  668. addObserver(zSource, id);
  669. delete[] id;
  670. char filterLen;
  671. zRequest->lese(&filterLen, 1);
  672. char* filter = new char[filterLen + 1];
  673. zRequest->lese(filter, filterLen);
  674. filter[(int)filterLen] = 0;
  675. InMemoryBuffer buffer;
  676. int count = 0;
  677. for (ItemSlot* slot : *this)
  678. {
  679. if (slot->getName().istGleich(filter))
  680. {
  681. count++;
  682. int id = slot->getId();
  683. buffer.schreibe((char*)&id, 4);
  684. int itemCount = slot->getNumberOfItems();
  685. buffer.schreibe((char*)&itemCount, 4);
  686. if (itemCount > 0)
  687. {
  688. float f = slot->zStack()->zItem()->getHp();
  689. buffer.schreibe((char*)&f, 4);
  690. f = slot->zStack()->zItem()->getMaxHp();
  691. buffer.schreibe((char*)&f, 4);
  692. f = slot->zStack()->zItem()->getDurability();
  693. buffer.schreibe((char*)&f, 4);
  694. f = slot->zStack()->zItem()->getMaxDurability();
  695. buffer.schreibe((char*)&f, 4);
  696. int id = slot->zStack()->zItem()->zItemType()->getId();
  697. buffer.schreibe((char*)&id, 4);
  698. }
  699. }
  700. }
  701. delete[] filter;
  702. char* msg = new char[5 + buffer.getSize()];
  703. msg[0] = 0;
  704. *(int*)(msg + 1) = count;
  705. buffer.lese(msg + 5, (int)buffer.getSize());
  706. zResponse->setMessage(msg, 5 + (int)buffer.getSize());
  707. break;
  708. }
  709. case 1: // remove Observer
  710. char idLen;
  711. zRequest->lese(&idLen, 1);
  712. char* id = new char[idLen + 1];
  713. zRequest->lese(id, idLen);
  714. id[(int)idLen] = 0;
  715. removeObserver(zSource, id);
  716. delete[] id;
  717. break;
  718. }
  719. }
  720. void Inventory::registerAfterPullStackCall(std::function<void(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  721. {
  722. afterPullStackCalls.add(call);
  723. }
  724. void Inventory::registerAfterPushStackCall(std::function<void(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  725. {
  726. afterPushStackCalls.add(call);
  727. }
  728. void Inventory::registerObserverAddedCall(std::function<void(Entity* zSource, Framework::Text id)> call)
  729. {
  730. observerAddedCalls.add(call);
  731. }