Inventory.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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 "ItemSlot.h"
  9. #include "ItemStack.h"
  10. #include "NetworkMessage.h"
  11. using namespace Framework;
  12. InventoryInteraction::InventoryInteraction(
  13. Inventory* current, Inventory* other, Direction dir)
  14. : current(current),
  15. other(other),
  16. dir(dir)
  17. {
  18. lock();
  19. }
  20. InventoryInteraction::InventoryInteraction(
  21. const InventoryInteraction& interaction)
  22. : InventoryInteraction(
  23. interaction.current, interaction.other, interaction.dir)
  24. {}
  25. InventoryInteraction::~InventoryInteraction()
  26. {
  27. unlock();
  28. }
  29. void InventoryInteraction::lock()
  30. {
  31. if (!current || !other) return;
  32. if (current->location.x < other->location.x)
  33. {
  34. current->cs.lock();
  35. other->cs.lock();
  36. return;
  37. }
  38. else if (current->location.x == other->location.x)
  39. {
  40. if (current->location.y < other->location.y)
  41. {
  42. current->cs.lock();
  43. other->cs.lock();
  44. return;
  45. }
  46. else if (current->location.y == other->location.y)
  47. {
  48. if (current->location.z < other->location.z)
  49. {
  50. current->cs.lock();
  51. other->cs.lock();
  52. return;
  53. }
  54. }
  55. }
  56. other->cs.lock();
  57. current->cs.lock();
  58. }
  59. void InventoryInteraction::unlock()
  60. {
  61. if (!current || !other) return;
  62. if (current->location.x < other->location.x)
  63. {
  64. current->cs.unlock();
  65. other->cs.unlock();
  66. return;
  67. }
  68. else if (current->location.x == other->location.x)
  69. {
  70. if (current->location.y < other->location.y)
  71. {
  72. current->cs.unlock();
  73. other->cs.unlock();
  74. return;
  75. }
  76. else if (current->location.y == other->location.y)
  77. {
  78. if (current->location.z < other->location.z)
  79. {
  80. current->cs.unlock();
  81. other->cs.unlock();
  82. return;
  83. }
  84. }
  85. }
  86. other->cs.unlock();
  87. current->cs.unlock();
  88. }
  89. void InventoryInteraction::transaction(Inventory* zSource,
  90. Inventory* zTarget,
  91. ItemFilter* zFilter,
  92. Direction sourceView,
  93. Direction targetView,
  94. int count)
  95. {
  96. for (auto sourceSlot = zSource->pullSlotsOrder->begin(); sourceSlot;)
  97. {
  98. while (sourceSlot
  99. && (sourceSlot->getNumberOfItems() == 0
  100. || (zFilter && !zFilter->matchSourceSlot(sourceSlot))))
  101. sourceSlot++;
  102. if (!sourceSlot) break;
  103. // TODO: use target cache ot get list of slots that already contains the
  104. // source item
  105. bool needNext = 1;
  106. for (auto targetSlot = zTarget->pushSlotsOrder->begin(); targetSlot;)
  107. {
  108. while (targetSlot
  109. && (targetSlot->isFull()
  110. || (zFilter && !zFilter->matchTargetSlot(targetSlot))))
  111. targetSlot++;
  112. if (!targetSlot) break;
  113. needNext &= !Inventory::unsafeMove(zSource,
  114. zTarget,
  115. sourceSlot,
  116. targetSlot,
  117. sourceView,
  118. targetView,
  119. count);
  120. if (count == 0) return;
  121. if (sourceSlot->getNumberOfItems() == 0) break;
  122. }
  123. if (needNext) sourceSlot++;
  124. }
  125. }
  126. InventoryInteraction& InventoryInteraction::operator=(
  127. const InventoryInteraction& data)
  128. {
  129. if (&data == this) return *this;
  130. unlock();
  131. current = data.current;
  132. other = data.other;
  133. dir = data.dir;
  134. lock();
  135. return *this;
  136. }
  137. void InventoryInteraction::endInteraction()
  138. {
  139. unlock();
  140. current = 0;
  141. other = 0;
  142. }
  143. void InventoryInteraction::pullItems(int count, ItemFilter* zFilter)
  144. {
  145. if (!current || !other) return;
  146. transaction(other, current, zFilter, getOppositeDirection(dir), dir, count);
  147. }
  148. void InventoryInteraction::pushItems(int count, ItemFilter* zFilter)
  149. {
  150. if (!current || !other) return;
  151. transaction(current, other, zFilter, dir, getOppositeDirection(dir), count);
  152. }
  153. MultipleInventoryLock::MultipleInventoryLock(Inventory** inventories, int count)
  154. : inventories(new Inventory*[count]),
  155. count(count),
  156. locked(0)
  157. {
  158. // sort given inventories in locking order
  159. bool* used = new bool[count];
  160. memset(used, 0, count);
  161. // TODO: use a performant sorting algorithm
  162. for (int i = 0; i < count; i++)
  163. {
  164. Inventory* min = 0;
  165. int minJ = 0;
  166. for (int j = 0; j < count; j++)
  167. {
  168. if (!used[j])
  169. {
  170. if (!min)
  171. {
  172. min = inventories[j];
  173. minJ = j;
  174. continue;
  175. }
  176. if (inventories[j]->location.x < min->location.x)
  177. {
  178. min = inventories[j];
  179. minJ = j;
  180. continue;
  181. }
  182. if (inventories[j]->location.x == min->location.x)
  183. {
  184. if (inventories[j]->location.y < min->location.y)
  185. {
  186. min = inventories[j];
  187. minJ = j;
  188. continue;
  189. }
  190. if (inventories[j]->location.y == min->location.y)
  191. {
  192. if (inventories[j]->location.z < min->location.z)
  193. {
  194. min = inventories[j];
  195. minJ = j;
  196. continue;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. this->inventories[i] = min;
  203. used[minJ] = 1;
  204. }
  205. lock();
  206. delete[] used;
  207. }
  208. MultipleInventoryLock::~MultipleInventoryLock()
  209. {
  210. unlock();
  211. delete[] inventories;
  212. }
  213. void MultipleInventoryLock::unlock()
  214. {
  215. if (locked)
  216. {
  217. locked = 0;
  218. for (int i = count - 1; i >= 0; i--)
  219. {
  220. inventories[i]->cs.unlock();
  221. }
  222. }
  223. }
  224. void MultipleInventoryLock::lock()
  225. {
  226. if (!locked)
  227. {
  228. locked = 1;
  229. for (int i = 0; i < count; i++)
  230. {
  231. inventories[i]->cs.lock();
  232. }
  233. }
  234. }
  235. Inventory::Inventory(
  236. const Framework::Vec3<float> location, int dimensionId, bool hasInventory)
  237. : ReferenceCounter(),
  238. nextSlotId(1),
  239. dimensionId(dimensionId),
  240. location(location)
  241. {
  242. if (hasInventory)
  243. {
  244. pullSlotsOrder = new Framework::RCArray<ItemSlot>();
  245. pushSlotsOrder = new Framework::RCArray<ItemSlot>();
  246. itemCache = new Framework::HashMap<int, Framework::Array<ItemSlot*>*>(
  247. ITEM_CACHE_SIZE, [](int key) { return key; });
  248. }
  249. else
  250. {
  251. pullSlotsOrder = 0;
  252. pushSlotsOrder = 0;
  253. itemCache = 0;
  254. }
  255. }
  256. Inventory::~Inventory()
  257. {
  258. if (pullSlotsOrder) pullSlotsOrder->release();
  259. if (pushSlotsOrder) pushSlotsOrder->release();
  260. if (itemCache) itemCache->release();
  261. }
  262. void Inventory::updateCache(ItemSlot* zSlot, int beforeKey)
  263. {
  264. if (!itemCache) return;
  265. int key
  266. = zSlot->zStack() ? zSlot->zStack()->zItem()->zItemType()->getId() : -1;
  267. if (key == beforeKey) return;
  268. if (beforeKey >= 0)
  269. {
  270. auto tmp = itemCache->safeGet(key, 0);
  271. if (tmp) tmp->removeValue(zSlot);
  272. }
  273. if (zSlot->zStack())
  274. {
  275. auto tmp = itemCache->safeGet(key, 0);
  276. if (!tmp)
  277. {
  278. tmp = new Array<ItemSlot*>();
  279. itemCache->put(key, tmp);
  280. }
  281. tmp->add(zSlot, 0);
  282. }
  283. }
  284. void Inventory::addSlot(ItemSlot* slot)
  285. {
  286. cs.lock();
  287. ((ItemSlotIDSetter*)slot)->setId(nextSlotId++);
  288. int pullPrio = slot->getPullPriority();
  289. int pushPrio = slot->getPushPriority();
  290. int index = 0;
  291. for (auto stack : *pullSlotsOrder)
  292. {
  293. if (stack->getPullPriority() > pullPrio) break;
  294. index++;
  295. }
  296. pullSlotsOrder->add(dynamic_cast<ItemSlot*>(slot->getThis()), index);
  297. index = 0;
  298. for (auto stack : *pushSlotsOrder)
  299. {
  300. if (stack->getPushPriority() > pushPrio) break;
  301. index++;
  302. }
  303. pushSlotsOrder->add(slot, index);
  304. updateCache(slot, -1);
  305. cs.unlock();
  306. }
  307. bool Inventory::allowPullStack(ItemSlot* zSlot, Direction dir) const
  308. {
  309. return pullSlotsOrder;
  310. }
  311. bool Inventory::allowPushStack(
  312. ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const
  313. {
  314. return pushSlotsOrder;
  315. }
  316. void Inventory::afterPullStack(
  317. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  318. {
  319. NetworkMessage* msg = new NetworkMessage();
  320. char* message = new char[9];
  321. message[0] = 1; // set count of items
  322. *(int*)(message + 1) = zSlot->getId();
  323. *(int*)(message + 5) = zSlot->getNumberOfItems();
  324. msg->setMessage(message, 9);
  325. notifyObservers(msg);
  326. for (auto call : afterPullStackCalls)
  327. call(zSlot, dir, zItem, count);
  328. }
  329. void Inventory::afterPushStack(
  330. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  331. {
  332. updateSlot(zSlot);
  333. for (auto call : afterPushStackCalls)
  334. call(zSlot, dir, zItem, count);
  335. }
  336. void Inventory::updateSlot(ItemSlot* zSlot)
  337. {
  338. NetworkMessage* msg = new NetworkMessage();
  339. char* message = new char[9];
  340. message[0] = 1; // set count of items
  341. *(int*)(message + 1) = zSlot->getId();
  342. *(int*)(message + 5) = 0;
  343. msg->setMessage(message, 9);
  344. notifyObservers(msg);
  345. if (zSlot->getNumberOfItems() > 0)
  346. {
  347. const Item* zItem = zSlot->zStack()->zItem();
  348. NetworkMessage* msg = new NetworkMessage();
  349. char* message = new char[30 + zItem->getName().getLength()];
  350. message[0] = 2; // add new stack
  351. *(int*)(message + 1) = zSlot->getId();
  352. *(int*)(message + 5) = zSlot->getNumberOfItems();
  353. *(float*)(message + 9) = zItem->getHp();
  354. *(float*)(message + 13) = zItem->getMaxHp();
  355. *(float*)(message + 17) = zItem->getDurability();
  356. *(float*)(message + 21) = zItem->getMaxDurability();
  357. *(int*)(message + 25) = zItem->zItemType()->getId();
  358. *(message + 29) = (char)zItem->getName().getLength();
  359. memcpy(message + 30,
  360. zItem->getName().getText(),
  361. zItem->getName().getLength());
  362. msg->setMessage(message, 30 + zItem->getName().getLength());
  363. notifyObservers(msg);
  364. }
  365. }
  366. void Inventory::loadInventory(Framework::StreamReader* zReader)
  367. {
  368. if (itemCache)
  369. {
  370. for (auto stack : *pushSlotsOrder)
  371. {
  372. int size = 0;
  373. zReader->lese((char*)&size, 4);
  374. if (size != 0)
  375. {
  376. int id = 0;
  377. zReader->lese((char*)&id, 4);
  378. Item* item = Game::INSTANCE->zItemType(id)->loadItem(zReader);
  379. stack->addItems(new ItemStack(item, size), NO_DIRECTION);
  380. }
  381. }
  382. }
  383. }
  384. void Inventory::saveInventory(Framework::StreamWriter* zWriter)
  385. {
  386. if (itemCache)
  387. {
  388. for (auto slot : *pushSlotsOrder)
  389. {
  390. const ItemStack* stack = slot->zStack();
  391. int value = 0;
  392. if (!stack || !stack->zItem())
  393. {
  394. zWriter->schreibe((char*)&value, 4);
  395. }
  396. else
  397. {
  398. value = stack->getSize();
  399. zWriter->schreibe((char*)&value, 4);
  400. value = stack->zItem()->zItemType()->getId();
  401. zWriter->schreibe((char*)&value, 4);
  402. stack->zItem()->zItemType()->saveItem(stack->zItem(), zWriter);
  403. }
  404. }
  405. }
  406. }
  407. void Inventory::notifyObservers(NetworkMessage* msg)
  408. {
  409. observable.notifyObservers(msg);
  410. }
  411. void Inventory::lock()
  412. {
  413. cs.lock();
  414. }
  415. void Inventory::unlock()
  416. {
  417. cs.unlock();
  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) return slot;
  426. }
  427. }
  428. return 0;
  429. }
  430. void Inventory::localTransaction(Array<ItemSlot*>* zSourceSlots,
  431. Array<ItemSlot*>* zTargetSlots,
  432. ItemFilter* zFilter,
  433. int count,
  434. Direction outDir,
  435. Direction inDir)
  436. {
  437. if (itemCache)
  438. {
  439. cs.lock();
  440. auto sourceSlot
  441. = zSourceSlots ? zSourceSlots->begin() : pullSlotsOrder->begin();
  442. while (true)
  443. {
  444. while (sourceSlot
  445. && (sourceSlot->getNumberOfItems() == 0
  446. || (zFilter && !zFilter->matchSourceSlot(sourceSlot))))
  447. sourceSlot++;
  448. if (!sourceSlot)
  449. {
  450. cs.unlock();
  451. return;
  452. }
  453. bool needNext = 1;
  454. for (auto targetSlot = zTargetSlots->begin(); targetSlot;)
  455. {
  456. while (
  457. targetSlot
  458. && (targetSlot->isFull()
  459. || (zFilter && !zFilter->matchTargetSlot(targetSlot))))
  460. targetSlot++;
  461. if (!targetSlot) break;
  462. needNext &= !Inventory::unsafeMove(
  463. this, this, sourceSlot, targetSlot, outDir, inDir, count);
  464. if (count == 0)
  465. {
  466. cs.unlock();
  467. return;
  468. }
  469. if (sourceSlot->getNumberOfItems() == 0) break;
  470. }
  471. if (needNext) sourceSlot++;
  472. }
  473. cs.unlock();
  474. }
  475. }
  476. void Inventory::addItems(ItemStack* zItems, Direction dir, ItemFilter* zFilter)
  477. {
  478. if (itemCache && zItems && zItems->getSize() > 0)
  479. {
  480. cs.lock();
  481. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot;
  482. targetSlot++)
  483. {
  484. if (!targetSlot->isFull()
  485. && (!zFilter || zFilter->matchTargetSlot(targetSlot)))
  486. {
  487. if (targetSlot->zStack())
  488. {
  489. if (targetSlot->zStack()->zItem()->canBeStackedWith(
  490. zItems->zItem()))
  491. {
  492. int number = MIN(targetSlot->numberOfAddableItems(
  493. zItems->zItem(), dir),
  494. zItems->getSize());
  495. int tmp = number;
  496. if (number > 0
  497. && allowPushStack(
  498. targetSlot, dir, zItems->zItem(), tmp))
  499. {
  500. number = MIN(number, tmp);
  501. ItemStack* stack = zItems->split(number);
  502. if (stack)
  503. {
  504. targetSlot->addItems(stack, dir);
  505. afterPushStack(targetSlot,
  506. dir,
  507. targetSlot->zStack()->zItem(),
  508. number);
  509. if (stack->getSize()) throw stack;
  510. stack->release();
  511. if (!zItems->getSize()) break;
  512. }
  513. }
  514. }
  515. }
  516. else
  517. {
  518. int number = MIN(
  519. targetSlot->numberOfAddableItems(zItems->zItem(), dir),
  520. zItems->getSize());
  521. int tmp = number;
  522. if (number > 0
  523. && allowPushStack(
  524. targetSlot, dir, zItems->zItem(), tmp))
  525. {
  526. number = MIN(number, tmp);
  527. ItemStack* stack = zItems->split(number);
  528. if (stack)
  529. {
  530. targetSlot->addItems(stack, dir);
  531. updateCache(targetSlot, -1);
  532. afterPushStack(targetSlot,
  533. dir,
  534. targetSlot->zStack()->zItem(),
  535. number);
  536. if (stack->getSize()) throw stack;
  537. stack->release();
  538. if (!zItems->getSize()) break;
  539. }
  540. }
  541. }
  542. }
  543. }
  544. cs.unlock();
  545. }
  546. }
  547. void Inventory::addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir)
  548. {
  549. if (zSlot->zStack()
  550. && !zSlot->zStack()->zItem()->canBeStackedWith(zItems->zItem()))
  551. return;
  552. bool needUpdate = !zSlot->zStack();
  553. int number = MIN(
  554. zSlot->numberOfAddableItems(zItems->zItem(), dir), zItems->getSize());
  555. int tmp = number;
  556. if (number > 0 && allowPushStack(zSlot, dir, zItems->zItem(), tmp))
  557. {
  558. number = MIN(number, tmp);
  559. ItemStack* stack = zItems->split(number);
  560. if (stack)
  561. {
  562. zSlot->addItems(stack, dir);
  563. if (needUpdate) updateCache(zSlot, -1);
  564. afterPushStack(zSlot, dir, zSlot->zStack()->zItem(), number);
  565. if (stack->getSize()) throw stack;
  566. stack->release();
  567. }
  568. }
  569. }
  570. ItemStack* Inventory::takeItemsOut(ItemSlot* zSlot, int count, Direction dir)
  571. {
  572. if (allowPullStack(zSlot, dir))
  573. {
  574. ItemStack* stack = zSlot->takeItemsOut(count, dir);
  575. if (stack)
  576. {
  577. updateCache(zSlot, stack->zItem()->zItemType()->getId());
  578. if (stack->getSize() > 0)
  579. afterPullStack(zSlot, dir, stack->zItem(), stack->getSize());
  580. }
  581. return stack;
  582. }
  583. return 0;
  584. }
  585. InventoryInteraction Inventory::interactWith(
  586. Inventory* zInventory, Direction dir)
  587. {
  588. return InventoryInteraction(this, zInventory, dir);
  589. }
  590. void Inventory::unsaveAddItem(
  591. ItemStack* zStack, Direction dir, ItemFilter* zFilter)
  592. {
  593. addItems(zStack, dir, zFilter);
  594. }
  595. int Inventory::numberOfAddableItems(const Item* zItem, Direction dir) const
  596. {
  597. int count = 0;
  598. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
  599. {
  600. int maxCount = targetSlot->numberOfAddableItems(zItem, dir);
  601. int allowed = maxCount;
  602. if (allowPushStack(targetSlot, dir, zItem, allowed))
  603. count += MIN(maxCount, allowed);
  604. }
  605. return count;
  606. }
  607. int Inventory::numberOfAddableItems(
  608. const Item* zItem, Direction dir, const Framework::Text& slotName) const
  609. {
  610. int count = 0;
  611. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
  612. {
  613. if (targetSlot->getName().istGleich(slotName))
  614. {
  615. int maxCount = targetSlot->numberOfAddableItems(zItem, dir);
  616. int allowed = maxCount;
  617. if (allowPushStack(targetSlot, dir, zItem, allowed))
  618. count += MIN(maxCount, allowed);
  619. }
  620. }
  621. return count;
  622. }
  623. bool Inventory::isAllAvailable(Framework::RCArray<RecipieInput>& inputs,
  624. const Framework::Text& slotName) const
  625. {
  626. int* used = new int[pullSlotsOrder->getEintragAnzahl()];
  627. memset(used, 0, sizeof(int) * pullSlotsOrder->getEintragAnzahl());
  628. for (RecipieInput* input : inputs)
  629. {
  630. int found = 0;
  631. for (int i = 0; i < pullSlotsOrder->getEintragAnzahl(); i++)
  632. {
  633. ItemSlot* slot = pullSlotsOrder->get(i);
  634. if (slot && slot->zStack() && slot->zStack()->zItem()
  635. && slot->getNumberOfItems() > used[i]
  636. && slot->getName().istGleich(slotName)
  637. && input->zFilter()->matchItem(slot->zStack()->zItem()))
  638. {
  639. int usable = slot->getNumberOfItems() - used[i];
  640. if (found + usable >= input->getAmount())
  641. {
  642. used[i] += input->getAmount() - found;
  643. found = input->getAmount();
  644. break;
  645. }
  646. else
  647. {
  648. used[i] += usable;
  649. found += usable;
  650. }
  651. }
  652. }
  653. if (found < input->getAmount())
  654. {
  655. delete[] used;
  656. return 0;
  657. }
  658. }
  659. delete[] used;
  660. return 1;
  661. }
  662. void Inventory::consume(
  663. Framework::RCArray<RecipieInput>& inputs, const Framework::Text& slotName)
  664. {
  665. for (RecipieInput* input : inputs)
  666. {
  667. int consumed = 0;
  668. for (int i = 0; i < pullSlotsOrder->getEintragAnzahl(); i++)
  669. {
  670. ItemSlot* slot = pullSlotsOrder->get(i);
  671. if (slot && slot->zStack() && slot->zStack()->zItem()
  672. && slot->getName().istGleich(slotName)
  673. && input->zFilter()->matchItem(slot->zStack()->zItem()))
  674. {
  675. if (consumed + slot->getNumberOfItems() >= input->getAmount())
  676. {
  677. takeItemsOut(
  678. slot, input->getAmount() - consumed, NO_DIRECTION)
  679. ->release();
  680. consumed = input->getAmount();
  681. break;
  682. }
  683. else
  684. {
  685. consumed += slot->getNumberOfItems();
  686. takeItemsOut(slot, slot->getNumberOfItems(), NO_DIRECTION)
  687. ->release();
  688. }
  689. }
  690. }
  691. }
  692. }
  693. Framework::ArrayIterator<ItemSlot*> Inventory::begin()
  694. {
  695. return pullSlotsOrder->begin();
  696. }
  697. Framework::ArrayIterator<ItemSlot*> Inventory::end()
  698. {
  699. return pullSlotsOrder->end();
  700. }
  701. void Inventory::inventoryApi(Framework::StreamReader* zRequest,
  702. NetworkMessage* zResponse,
  703. Entity* zSource)
  704. {
  705. char type;
  706. zRequest->lese(&type, 1);
  707. switch (type)
  708. {
  709. case 0: // request inventory
  710. {
  711. char idLen;
  712. zRequest->lese(&idLen, 1);
  713. char* id = new char[idLen + 1];
  714. zRequest->lese(id, idLen);
  715. id[(int)idLen] = 0;
  716. int processor;
  717. zRequest->lese((char*)&processor, 4);
  718. zResponse->addressUIElement(id, processor);
  719. observable.addObserver(zSource, id, processor);
  720. delete[] id;
  721. char filterLen;
  722. zRequest->lese(&filterLen, 1);
  723. char* filter = new char[filterLen + 1];
  724. if (filterLen) zRequest->lese(filter, filterLen);
  725. filter[(int)filterLen] = 0;
  726. InMemoryBuffer buffer;
  727. int count = 0;
  728. for (ItemSlot* slot : *this)
  729. {
  730. if (filterLen == 0 || slot->getName().istGleich(filter))
  731. {
  732. count++;
  733. int id = slot->getId();
  734. buffer.schreibe((char*)&id, 4);
  735. int itemCount = slot->getNumberOfItems();
  736. buffer.schreibe((char*)&itemCount, 4);
  737. if (itemCount > 0)
  738. {
  739. float f = slot->zStack()->zItem()->getHp();
  740. buffer.schreibe((char*)&f, 4);
  741. f = slot->zStack()->zItem()->getMaxHp();
  742. buffer.schreibe((char*)&f, 4);
  743. f = slot->zStack()->zItem()->getDurability();
  744. buffer.schreibe((char*)&f, 4);
  745. f = slot->zStack()->zItem()->getMaxDurability();
  746. buffer.schreibe((char*)&f, 4);
  747. int id = slot->zStack()->zItem()->zItemType()->getId();
  748. buffer.schreibe((char*)&id, 4);
  749. char len = (char)slot->zStack()
  750. ->zItem()
  751. ->getName()
  752. .getLength();
  753. buffer.schreibe((char*)&len, 1);
  754. buffer.schreibe(
  755. slot->zStack()->zItem()->getName().getText(),
  756. slot->zStack()->zItem()->getName().getLength());
  757. }
  758. }
  759. }
  760. delete[] filter;
  761. char* msg = new char[5 + buffer.getSize()];
  762. msg[0] = 0;
  763. *(int*)(msg + 1) = count;
  764. buffer.lese(msg + 5, (int)buffer.getSize());
  765. zResponse->setMessage(msg, 5 + (int)buffer.getSize());
  766. break;
  767. }
  768. case 1: // remove Observer
  769. {
  770. char idLen;
  771. zRequest->lese(&idLen, 1);
  772. char* id = new char[idLen + 1];
  773. zRequest->lese(id, idLen);
  774. id[(int)idLen] = 0;
  775. int processor;
  776. zRequest->lese((char*)&processor, 4);
  777. observable.removeObserver(zSource, id, processor);
  778. delete[] id;
  779. break;
  780. }
  781. case 2: // request item tooltip
  782. {
  783. char idLen;
  784. zRequest->lese(&idLen, 1);
  785. char* id = new char[idLen + 1];
  786. zRequest->lese(id, idLen);
  787. id[(int)idLen] = 0;
  788. int processor;
  789. zRequest->lese((char*)&processor, 4);
  790. zResponse->addressUIElement(id, processor);
  791. delete[] id;
  792. int slotId;
  793. zRequest->lese((char*)&slotId, 4);
  794. Text uiml;
  795. for (ItemSlot* slot : *pullSlotsOrder)
  796. {
  797. if (slot->getId() == slotId)
  798. {
  799. if (slot->zStack() && slot->zStack()->zItem())
  800. uiml = slot->zStack()->zItem()->getTooltipUIML();
  801. }
  802. }
  803. short len = (short)uiml.getLength();
  804. char* buffer = new char[uiml.getLength() + 7];
  805. buffer[0] = 3;
  806. *(int*)(buffer + 1) = slotId;
  807. *(short*)(buffer + 5) = len;
  808. memcpy(buffer + 7, uiml, len);
  809. zResponse->setMessage(buffer, len + 7);
  810. break;
  811. }
  812. }
  813. }
  814. void Inventory::registerAfterPullStackCall(std::function<void(
  815. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  816. {
  817. afterPullStackCalls.add(call);
  818. }
  819. void Inventory::registerAfterPushStackCall(std::function<void(
  820. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call)
  821. {
  822. afterPushStackCalls.add(call);
  823. }
  824. void Inventory::registerObserverAddedCall(
  825. std::function<void(Entity* zSource, Framework::Text id, int processor)>
  826. call)
  827. {
  828. observable.registerOnObserverAddedCall(call);
  829. }
  830. int Inventory::getDimensionId() const
  831. {
  832. return dimensionId;
  833. }
  834. Framework::Vec3<float> Inventory::getLocation() const
  835. {
  836. return location;
  837. }
  838. bool Inventory::unsafeMove(Inventory* zSource,
  839. Inventory* zTarget,
  840. ArrayIterator<ItemSlot*>& sourceSlot,
  841. ArrayIterator<ItemSlot*>& targetSlot,
  842. Direction outDir,
  843. Direction inDir,
  844. int& count)
  845. {
  846. if (targetSlot->zStack())
  847. {
  848. if (sourceSlot->zStack()->zItem()->canBeStackedWith(
  849. targetSlot->zStack()->zItem()))
  850. {
  851. int number = MIN(targetSlot->numberOfAddableItems(
  852. sourceSlot->zStack()->zItem(), outDir),
  853. count);
  854. int tmp = number;
  855. if (number > 0 && zSource->allowPullStack(sourceSlot, outDir)
  856. && zTarget->allowPushStack(
  857. targetSlot, inDir, sourceSlot->zStack()->zItem(), tmp))
  858. {
  859. number = MIN(number, tmp);
  860. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  861. if (stack)
  862. {
  863. targetSlot->addItems(stack, inDir);
  864. zSource->updateCache(sourceSlot,
  865. targetSlot->zStack()->zItem()->zItemType()->getId());
  866. zSource->afterPullStack(sourceSlot,
  867. outDir,
  868. targetSlot->zStack()->zItem(),
  869. number);
  870. zTarget->afterPushStack(targetSlot,
  871. inDir,
  872. targetSlot->zStack()->zItem(),
  873. number);
  874. if (stack->getSize()) throw stack;
  875. stack->release();
  876. count -= number;
  877. return 1;
  878. }
  879. else
  880. targetSlot++;
  881. }
  882. else
  883. targetSlot++;
  884. }
  885. else
  886. targetSlot++;
  887. }
  888. else
  889. {
  890. int number = MIN(targetSlot->numberOfAddableItems(
  891. sourceSlot->zStack()->zItem(), outDir),
  892. count);
  893. int tmp = number;
  894. if (number > 0 && zSource->allowPullStack(sourceSlot, outDir)
  895. && zTarget->allowPushStack(
  896. targetSlot, inDir, sourceSlot->zStack()->zItem(), tmp))
  897. {
  898. number = MIN(number, tmp);
  899. if (number > 0)
  900. {
  901. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  902. if (stack)
  903. {
  904. targetSlot->addItems(stack, inDir);
  905. zSource->updateCache(sourceSlot,
  906. targetSlot->zStack()->zItem()->zItemType()->getId());
  907. zTarget->updateCache(targetSlot, -1);
  908. zSource->afterPullStack(sourceSlot,
  909. outDir,
  910. targetSlot->zStack()->zItem(),
  911. number);
  912. zTarget->afterPushStack(targetSlot,
  913. inDir,
  914. targetSlot->zStack()->zItem(),
  915. number);
  916. if (stack->getSize()) throw stack;
  917. stack->release();
  918. count -= number;
  919. return 1;
  920. }
  921. else
  922. targetSlot++;
  923. }
  924. else
  925. targetSlot++;
  926. }
  927. else
  928. targetSlot++;
  929. }
  930. return 0;
  931. }