Inventory.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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[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. }
  205. MultipleInventoryLock::~MultipleInventoryLock()
  206. {
  207. unlock();
  208. delete[] inventories;
  209. }
  210. void MultipleInventoryLock::unlock()
  211. {
  212. if (locked)
  213. {
  214. locked = 0;
  215. for (int i = count - 1; i >= 0; i--)
  216. {
  217. inventories[i]->cs.unlock();
  218. }
  219. }
  220. }
  221. void MultipleInventoryLock::lock()
  222. {
  223. if (!locked)
  224. {
  225. locked = 1;
  226. for (int i = 0; i < count; i++)
  227. {
  228. inventories[i]->cs.lock();
  229. }
  230. }
  231. }
  232. Inventory::Inventory(const Framework::Vec3<float> location, bool hasInventory)
  233. : ReferenceCounter(),
  234. nextSlotId(1),
  235. location(location)
  236. {
  237. if (hasInventory)
  238. {
  239. pullSlotsOrder = new Framework::RCArray<ItemSlot>();
  240. pushSlotsOrder = new Framework::RCArray<ItemSlot>();
  241. itemCache = new Framework::HashMap<int, Framework::Array<ItemSlot*>*>(
  242. ITEM_CACHE_SIZE, std::_Identity<int>());
  243. }
  244. else
  245. {
  246. pullSlotsOrder = 0;
  247. pushSlotsOrder = 0;
  248. itemCache = 0;
  249. }
  250. }
  251. Inventory::~Inventory()
  252. {
  253. if (pullSlotsOrder) pullSlotsOrder->release();
  254. if (pushSlotsOrder) pushSlotsOrder->release();
  255. if (itemCache) itemCache->release();
  256. }
  257. void Inventory::updateCache(ItemSlot* zSlot, int beforeKey)
  258. {
  259. if (!itemCache) return;
  260. int key
  261. = zSlot->zStack() ? zSlot->zStack()->zItem()->zItemType()->getId() : -1;
  262. if (key == beforeKey) return;
  263. if (beforeKey >= 0)
  264. {
  265. auto tmp = itemCache->safeGet(key, 0);
  266. if (tmp) tmp->removeValue(zSlot);
  267. }
  268. if (zSlot->zStack())
  269. {
  270. auto tmp = itemCache->safeGet(key, 0);
  271. if (!tmp)
  272. {
  273. tmp = new Array<ItemSlot*>();
  274. itemCache->put(key, tmp);
  275. }
  276. tmp->add(zSlot, 0);
  277. }
  278. }
  279. void Inventory::addSlot(ItemSlot* slot)
  280. {
  281. cs.lock();
  282. ((ItemSlotIDSetter*)slot)->setId(nextSlotId++);
  283. int pullPrio = slot->getPullPriority();
  284. int pushPrio = slot->getPushPriority();
  285. int index = 0;
  286. for (auto stack : *pullSlotsOrder)
  287. {
  288. if (stack->getPullPriority() > pullPrio) break;
  289. index++;
  290. }
  291. pullSlotsOrder->add(dynamic_cast<ItemSlot*>(slot->getThis()), index);
  292. index = 0;
  293. for (auto stack : *pushSlotsOrder)
  294. {
  295. if (stack->getPushPriority() > pushPrio) break;
  296. index++;
  297. }
  298. pushSlotsOrder->add(slot, index);
  299. updateCache(slot, -1);
  300. cs.unlock();
  301. }
  302. bool Inventory::allowPullStack(ItemSlot* zSlot, Direction dir) const
  303. {
  304. return pullSlotsOrder;
  305. }
  306. bool Inventory::allowPushStack(
  307. ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const
  308. {
  309. return pushSlotsOrder;
  310. }
  311. void Inventory::afterPullStack(
  312. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  313. {
  314. NetworkMessage* msg = new NetworkMessage();
  315. char* message = new char[9];
  316. message[0] = 1; // set count of items
  317. *(int*)(message + 1) = zSlot->getId();
  318. *(int*)(message + 5) = zSlot->getNumberOfItems();
  319. msg->setMessage(message, 9);
  320. notifyObservers(msg);
  321. for (auto call : afterPullStackCalls)
  322. call(zSlot, dir, zItem, count);
  323. }
  324. void Inventory::afterPushStack(
  325. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  326. {
  327. if (zSlot->getNumberOfItems() > count)
  328. {
  329. NetworkMessage* msg = new NetworkMessage();
  330. char* message = new char[9];
  331. message[0] = 1; // set count of items
  332. *(int*)(message + 1) = zSlot->getId();
  333. *(int*)(message + 5) = zSlot->getNumberOfItems();
  334. msg->setMessage(message, 9);
  335. notifyObservers(msg);
  336. }
  337. else
  338. {
  339. NetworkMessage* msg = new NetworkMessage();
  340. char* message
  341. = 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. const ItemSlot* Inventory::zSlot(int id) const
  460. {
  461. if (itemCache)
  462. {
  463. for (auto slot : *pushSlotsOrder)
  464. {
  465. if (slot->getId() == id) return slot;
  466. }
  467. }
  468. return 0;
  469. }
  470. void Inventory::localTransaction(Array<ItemSlot*>* zSourceSlots,
  471. Array<ItemSlot*>* zTargetSlots,
  472. ItemFilter* zFilter,
  473. int count,
  474. Direction outDir,
  475. Direction inDir)
  476. {
  477. if (itemCache)
  478. {
  479. cs.lock();
  480. auto sourceSlot
  481. = zSourceSlots ? zSourceSlots->begin() : pullSlotsOrder->begin();
  482. while (true)
  483. {
  484. while (sourceSlot
  485. && (sourceSlot->getNumberOfItems() == 0
  486. || (zFilter && !zFilter->matchSourceSlot(sourceSlot))))
  487. sourceSlot++;
  488. if (!sourceSlot)
  489. {
  490. cs.unlock();
  491. return;
  492. }
  493. bool needNext = 1;
  494. for (auto targetSlot = zTargetSlots->begin(); targetSlot;)
  495. {
  496. while (
  497. targetSlot
  498. && (targetSlot->isFull()
  499. || (zFilter && !zFilter->matchTargetSlot(targetSlot))))
  500. targetSlot++;
  501. if (!targetSlot) break;
  502. needNext &= !Inventory::unsafeMove(
  503. this, this, sourceSlot, targetSlot, outDir, inDir, count);
  504. if (count == 0)
  505. {
  506. cs.unlock();
  507. return;
  508. }
  509. if (sourceSlot->getNumberOfItems() == 0) break;
  510. }
  511. if (needNext) sourceSlot++;
  512. }
  513. cs.unlock();
  514. }
  515. }
  516. void Inventory::addItems(ItemStack* zItems, Direction dir, ItemFilter* zFilter)
  517. {
  518. if (itemCache && zItems && zItems->getSize() > 0)
  519. {
  520. cs.lock();
  521. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot;
  522. targetSlot++)
  523. {
  524. if (!targetSlot->isFull() && (!zFilter || zFilter->matchTargetSlot(targetSlot)))
  525. {
  526. if (targetSlot->zStack())
  527. {
  528. if (targetSlot->zStack()->zItem()->canBeStackedWith(
  529. zItems->zItem()))
  530. {
  531. int number
  532. = MIN(targetSlot->numberOfAddableItems(zItems, dir),
  533. zItems->getSize());
  534. int tmp = number;
  535. if (number > 0
  536. && allowPushStack(
  537. targetSlot, dir, zItems->zItem(), tmp))
  538. {
  539. number = MIN(number, tmp);
  540. ItemStack* stack = zItems->split(number);
  541. if (stack)
  542. {
  543. targetSlot->addItems(stack, dir);
  544. afterPushStack(targetSlot,
  545. dir,
  546. targetSlot->zStack()->zItem(),
  547. number);
  548. if (stack->getSize()) throw stack;
  549. stack->release();
  550. if (!zItems->getSize()) break;
  551. }
  552. }
  553. }
  554. }
  555. else
  556. {
  557. int number
  558. = MIN(targetSlot->numberOfAddableItems(zItems, dir),
  559. zItems->getSize());
  560. int tmp = number;
  561. if (number > 0
  562. && allowPushStack(
  563. targetSlot, dir, zItems->zItem(), tmp))
  564. {
  565. number = MIN(number, tmp);
  566. ItemStack* stack = zItems->split(number);
  567. if (stack)
  568. {
  569. targetSlot->addItems(stack, dir);
  570. updateCache(targetSlot, -1);
  571. afterPushStack(targetSlot,
  572. dir,
  573. targetSlot->zStack()->zItem(),
  574. number);
  575. if (stack->getSize()) throw stack;
  576. stack->release();
  577. if (!zItems->getSize()) break;
  578. }
  579. }
  580. }
  581. }
  582. }
  583. cs.unlock();
  584. }
  585. }
  586. void Inventory::addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir)
  587. {
  588. if (zSlot->zStack()
  589. && !zSlot->zStack()->zItem()->canBeStackedWith(zItems->zItem()))
  590. return;
  591. bool needUpdate = !zSlot->zStack();
  592. int number
  593. = 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) updateCache(zSlot, -1);
  603. afterPushStack(zSlot, dir, zSlot->zStack()->zItem(), number);
  604. if (stack->getSize()) throw stack;
  605. stack->release();
  606. }
  607. }
  608. }
  609. ItemStack* Inventory::takeItemsOut(ItemSlot* zSlot, int count, Direction dir)
  610. {
  611. if (allowPullStack(zSlot, dir))
  612. {
  613. ItemStack* stack = zSlot->takeItemsOut(count, dir);
  614. if (stack)
  615. {
  616. updateCache(zSlot, stack->zItem()->zItemType()->getId());
  617. if (stack->getSize() > 0)
  618. afterPullStack(zSlot, dir, stack->zItem(), stack->getSize());
  619. }
  620. return stack;
  621. }
  622. return 0;
  623. }
  624. InventoryInteraction Inventory::interactWith(
  625. Inventory* zInventory, Direction dir)
  626. {
  627. return InventoryInteraction(this, zInventory, dir);
  628. }
  629. void Inventory::unsaveAddItem(
  630. ItemStack* zStack, Direction dir, ItemFilter* zFilter)
  631. {
  632. addItems(zStack, dir, zFilter);
  633. }
  634. int Inventory::numberOfAddableItems(
  635. const ItemStack* zStack, Direction dir) const
  636. {
  637. int count = 0;
  638. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
  639. {
  640. int maxCount = targetSlot->numberOfAddableItems(zStack, dir);
  641. int allowed = maxCount;
  642. if (allowPushStack(targetSlot, dir, zStack->zItem(), allowed))
  643. count += MIN(maxCount, allowed);
  644. }
  645. return count;
  646. }
  647. Framework::Iterator<ItemSlot*> Inventory::begin()
  648. {
  649. return pullSlotsOrder->begin();
  650. }
  651. Framework::Iterator<ItemSlot*> Inventory::end()
  652. {
  653. return pullSlotsOrder->end();
  654. }
  655. void Inventory::inventoryApi(Framework::StreamReader* zRequest,
  656. NetworkMessage* zResponse,
  657. Entity* zSource)
  658. {
  659. char type;
  660. zRequest->lese(&type, 1);
  661. switch (type)
  662. {
  663. case 0: // request inventory
  664. {
  665. char idLen;
  666. zRequest->lese(&idLen, 1);
  667. char* id = new char[idLen + 1];
  668. zRequest->lese(id, idLen);
  669. id[(int)idLen] = 0;
  670. zResponse->addressGui(id);
  671. addObserver(zSource, id);
  672. delete[] id;
  673. char filterLen;
  674. zRequest->lese(&filterLen, 1);
  675. char* filter = new char[filterLen + 1];
  676. zRequest->lese(filter, filterLen);
  677. filter[(int)filterLen] = 0;
  678. InMemoryBuffer buffer;
  679. int count = 0;
  680. for (ItemSlot* slot : *this)
  681. {
  682. if (slot->getName().istGleich(filter))
  683. {
  684. count++;
  685. int id = slot->getId();
  686. buffer.schreibe((char*)&id, 4);
  687. int itemCount = slot->getNumberOfItems();
  688. buffer.schreibe((char*)&itemCount, 4);
  689. if (itemCount > 0)
  690. {
  691. float f = slot->zStack()->zItem()->getHp();
  692. buffer.schreibe((char*)&f, 4);
  693. f = slot->zStack()->zItem()->getMaxHp();
  694. buffer.schreibe((char*)&f, 4);
  695. f = slot->zStack()->zItem()->getDurability();
  696. buffer.schreibe((char*)&f, 4);
  697. f = slot->zStack()->zItem()->getMaxDurability();
  698. buffer.schreibe((char*)&f, 4);
  699. int id = slot->zStack()->zItem()->zItemType()->getId();
  700. buffer.schreibe((char*)&id, 4);
  701. char len = (char)slot->zStack()
  702. ->zItem()
  703. ->getName()
  704. .getLength();
  705. buffer.schreibe((char*)&len, 1);
  706. buffer.schreibe(
  707. slot->zStack()->zItem()->getName().getText(),
  708. slot->zStack()->zItem()->getName().getLength());
  709. }
  710. }
  711. }
  712. delete[] filter;
  713. char* msg = new char[5 + buffer.getSize()];
  714. msg[0] = 0;
  715. *(int*)(msg + 1) = count;
  716. buffer.lese(msg + 5, (int)buffer.getSize());
  717. zResponse->setMessage(msg, 5 + (int)buffer.getSize());
  718. break;
  719. }
  720. case 1: // remove Observer
  721. {
  722. char idLen;
  723. zRequest->lese(&idLen, 1);
  724. char* id = new char[idLen + 1];
  725. zRequest->lese(id, idLen);
  726. id[(int)idLen] = 0;
  727. removeObserver(zSource, id);
  728. delete[] id;
  729. break;
  730. }
  731. case 2: // request item tooltip
  732. {
  733. char idLen;
  734. zRequest->lese(&idLen, 1);
  735. char* id = new char[idLen + 1];
  736. zRequest->lese(id, idLen);
  737. id[(int)idLen] = 0;
  738. zResponse->addressGui(id);
  739. delete[] id;
  740. int slotId;
  741. zRequest->lese((char*)&slotId, 4);
  742. Text uiml;
  743. for (ItemSlot* slot : *pullSlotsOrder)
  744. {
  745. if (slot->getId() == slotId)
  746. {
  747. if (slot->zStack() && slot->zStack()->zItem())
  748. uiml = slot->zStack()->zItem()->getTooltipUIML();
  749. }
  750. }
  751. short len = (short)uiml.getLength();
  752. char* buffer = new char[uiml.getLength() + 7];
  753. buffer[0] = 3;
  754. *(int*)(buffer + 1) = slotId;
  755. *(short*)(buffer + 5) = len;
  756. memcpy(buffer + 7, uiml, len);
  757. zResponse->setMessage(buffer, len + 7);
  758. break;
  759. }
  760. }
  761. }
  762. void Inventory::registerAfterPullStackCall(std::function<void(
  763. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  764. {
  765. afterPullStackCalls.add(call);
  766. }
  767. void Inventory::registerAfterPushStackCall(std::function<void(
  768. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  769. {
  770. afterPushStackCalls.add(call);
  771. }
  772. void Inventory::registerObserverAddedCall(
  773. std::function<void(Entity* zSource, Framework::Text id)> call)
  774. {
  775. observerAddedCalls.add(call);
  776. }
  777. bool Inventory::unsafeMove(Inventory* zSource,
  778. Inventory* zTarget,
  779. Iterator<ItemSlot*>& sourceSlot,
  780. Iterator<ItemSlot*>& targetSlot,
  781. Direction outDir,
  782. Direction inDir,
  783. int& count)
  784. {
  785. if (targetSlot->zStack())
  786. {
  787. if (sourceSlot->zStack()->zItem()->canBeStackedWith(
  788. targetSlot->zStack()->zItem()))
  789. {
  790. int number = MIN(
  791. targetSlot->numberOfAddableItems(sourceSlot->zStack(), outDir),
  792. count);
  793. int tmp = number;
  794. if (number > 0 && zSource->allowPullStack(sourceSlot, outDir)
  795. && zTarget->allowPushStack(
  796. targetSlot, inDir, sourceSlot->zStack()->zItem(), tmp))
  797. {
  798. number = MIN(number, tmp);
  799. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  800. if (stack)
  801. {
  802. targetSlot->addItems(stack, inDir);
  803. zSource->updateCache(sourceSlot,
  804. targetSlot->zStack()->zItem()->zItemType()->getId());
  805. zSource->afterPullStack(sourceSlot,
  806. outDir,
  807. targetSlot->zStack()->zItem(),
  808. number);
  809. zTarget->afterPushStack(targetSlot,
  810. inDir,
  811. targetSlot->zStack()->zItem(),
  812. number);
  813. if (stack->getSize()) throw stack;
  814. stack->release();
  815. count -= number;
  816. return 1;
  817. }
  818. else
  819. targetSlot++;
  820. }
  821. else
  822. targetSlot++;
  823. }
  824. else
  825. targetSlot++;
  826. }
  827. else
  828. {
  829. int number = MIN(
  830. targetSlot->numberOfAddableItems(sourceSlot->zStack(), outDir),
  831. count);
  832. int tmp = number;
  833. if (number > 0 && zSource->allowPullStack(sourceSlot, outDir)
  834. && zTarget->allowPushStack(
  835. targetSlot, inDir, sourceSlot->zStack()->zItem(), tmp))
  836. {
  837. number = MIN(number, tmp);
  838. if (number > 0)
  839. {
  840. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  841. if (stack)
  842. {
  843. targetSlot->addItems(stack, inDir);
  844. zSource->updateCache(sourceSlot,
  845. targetSlot->zStack()->zItem()->zItemType()->getId());
  846. zTarget->updateCache(targetSlot, -1);
  847. zSource->afterPullStack(sourceSlot,
  848. outDir,
  849. targetSlot->zStack()->zItem(),
  850. number);
  851. zTarget->afterPushStack(targetSlot,
  852. inDir,
  853. targetSlot->zStack()->zItem(),
  854. number);
  855. if (stack->getSize()) throw stack;
  856. stack->release();
  857. count -= number;
  858. return 1;
  859. }
  860. else
  861. targetSlot++;
  862. }
  863. else
  864. targetSlot++;
  865. }
  866. else
  867. targetSlot++;
  868. }
  869. return 0;
  870. }