Inventory.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. #include "Inventory.h"
  2. #include "ItemFilter.h"
  3. #include "Constants.h"
  4. #include "Area.h"
  5. using namespace Framework;
  6. InventoryInteraction::InventoryInteraction(Inventory* current, Inventory* other, Direction dir)
  7. : current(current),
  8. other(other),
  9. dir(dir)
  10. {
  11. lock();
  12. }
  13. InventoryInteraction::InventoryInteraction(const InventoryInteraction& interaction)
  14. : InventoryInteraction(interaction.current, interaction.other, interaction.dir)
  15. {}
  16. InventoryInteraction::~InventoryInteraction()
  17. {
  18. unlock();
  19. }
  20. void InventoryInteraction::lock()
  21. {
  22. if (!current || !other) return;
  23. if (current->location.x < other->location.x)
  24. {
  25. current->cs.lock();
  26. other->cs.lock();
  27. return;
  28. }
  29. else if (current->location.x == other->location.x)
  30. {
  31. if (current->location.y < other->location.y)
  32. {
  33. current->cs.lock();
  34. other->cs.lock();
  35. return;
  36. }
  37. else if (current->location.y == other->location.y)
  38. {
  39. if (current->location.z < other->location.z)
  40. {
  41. current->cs.lock();
  42. other->cs.lock();
  43. return;
  44. }
  45. }
  46. }
  47. other->cs.lock();
  48. current->cs.lock();
  49. }
  50. void InventoryInteraction::unlock()
  51. {
  52. if (!current || !other) return;
  53. if (current->location.x < other->location.x)
  54. {
  55. current->cs.unlock();
  56. other->cs.unlock();
  57. return;
  58. }
  59. else if (current->location.x == other->location.x)
  60. {
  61. if (current->location.y < other->location.y)
  62. {
  63. current->cs.unlock();
  64. other->cs.unlock();
  65. return;
  66. }
  67. else if (current->location.y == other->location.y)
  68. {
  69. if (current->location.z < other->location.z)
  70. {
  71. current->cs.unlock();
  72. other->cs.unlock();
  73. return;
  74. }
  75. }
  76. }
  77. other->cs.unlock();
  78. current->cs.unlock();
  79. }
  80. void InventoryInteraction::transaction(Inventory* zSource, Inventory* zTarget, ItemFilter* zFilter, Direction sourceView, Direction targetView, int count)
  81. {
  82. // TODO: rewrite this such that for each source slot all target slots are looped trough
  83. auto sourceSlot = zSource->pullSlotsOrder->begin();
  84. for (auto targetSlot = zTarget->pushSlotsOrder->begin(); targetSlot; )
  85. {
  86. int amount = count;
  87. if (!targetSlot->isFull())
  88. {
  89. if (targetSlot->zStack())
  90. {
  91. Array<ItemSlot*>* zSurceListe = zSource->itemCache->safeGet(targetSlot->zStack()->zItem()->zItemType()->getId(), 0);
  92. if (zSurceListe)
  93. {
  94. Array<int> toDelete;
  95. int index = 0;
  96. for (auto sourceSlot = zSurceListe->begin(); sourceSlot; sourceSlot++, index++)
  97. {
  98. if (zFilter && !zFilter->matchItem(sourceSlot->zStack()->zItem()))
  99. continue;
  100. int number = MIN(targetSlot->numberOfAddableItems(sourceSlot->zStack(), sourceView), count);
  101. int tmp = number;
  102. if (number > 0 && zSource->allowPullStack(sourceSlot, sourceView) && zTarget->allowPushStack(targetSlot, targetView, sourceSlot->zStack()->zItem(), tmp))
  103. {
  104. number = MIN(number, tmp);
  105. ItemStack* stack = sourceSlot->takeItemsOut(number, dir);
  106. if (stack)
  107. {
  108. if (!sourceSlot->zStack())
  109. toDelete.add(index, 0);
  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. break;
  120. }
  121. }
  122. }
  123. for (auto indexToDelete = toDelete.begin(); indexToDelete; indexToDelete++)
  124. zSurceListe->remove(indexToDelete);
  125. if (count == 0)
  126. return;
  127. }
  128. }
  129. else
  130. {
  131. while (sourceSlot && (!sourceSlot->zStack() || (zFilter && !zFilter->matchItem(sourceSlot->zStack()->zItem()))))
  132. sourceSlot++;
  133. if (!sourceSlot)
  134. return;
  135. int number = MIN(targetSlot->numberOfAddableItems(sourceSlot->zStack(), sourceView), count);
  136. int tmp = number;
  137. if (number > 0 && zSource->allowPullStack(sourceSlot, sourceView) && zTarget->allowPushStack(targetSlot, targetView, sourceSlot->zStack()->zItem(), tmp))
  138. {
  139. number = MIN(number, tmp);
  140. if (number > 0)
  141. {
  142. ItemStack* stack = sourceSlot->takeItemsOut(number, dir);
  143. if (stack)
  144. {
  145. targetSlot->addItems(stack, dir);
  146. zSource->updateCache(sourceSlot, targetSlot->zStack()->zItem()->zItemType()->getId());
  147. zTarget->updateCache(targetSlot, -1);
  148. zSource->afterPullStack(sourceSlot, sourceView, targetSlot->zStack()->zItem(), number);
  149. zTarget->afterPushStack(targetSlot, targetView, targetSlot->zStack()->zItem(), number);
  150. if (stack->getSize())
  151. throw stack;
  152. stack->release();
  153. count -= number;
  154. if (count == 0)
  155. return;
  156. }
  157. }
  158. }
  159. }
  160. }
  161. if (amount == count || targetSlot->isFull())
  162. targetSlot++;
  163. }
  164. }
  165. InventoryInteraction& InventoryInteraction::operator=(const InventoryInteraction& data)
  166. {
  167. if (&data == this) return *this;
  168. unlock();
  169. current = data.current;
  170. other = data.other;
  171. dir = data.dir;
  172. lock();
  173. return *this;
  174. }
  175. void InventoryInteraction::endInteraction()
  176. {
  177. unlock();
  178. current = 0;
  179. other = 0;
  180. }
  181. void InventoryInteraction::pullItems(int count, ItemFilter* zFilter)
  182. {
  183. if (!current || !other) return;
  184. transaction(other, current, zFilter, getOppositeDirection(dir), dir, count);
  185. }
  186. void InventoryInteraction::pushItems(int count, ItemFilter* zFilter)
  187. {
  188. if (!current || !other) return;
  189. transaction(current, other, zFilter, dir, getOppositeDirection(dir), count);
  190. }
  191. Inventory::Inventory(const Framework::Vec3<float> location, bool hasInventory)
  192. : ReferenceCounter(),
  193. nextSlotId(1),
  194. location(location)
  195. {
  196. if (hasInventory)
  197. {
  198. pullSlotsOrder = new Framework::RCArray<ItemSlot>();
  199. pushSlotsOrder = new Framework::RCArray<ItemSlot>();
  200. itemCache = new Framework::HashMap<int, Framework::Array<ItemSlot*>*>(ITEM_CACHE_SIZE, std::_Identity<int>());
  201. }
  202. else
  203. {
  204. pullSlotsOrder = 0;
  205. pushSlotsOrder = 0;
  206. itemCache = 0;
  207. }
  208. }
  209. Inventory::~Inventory()
  210. {
  211. if (pullSlotsOrder)
  212. pullSlotsOrder->release();
  213. if (pushSlotsOrder)
  214. pushSlotsOrder->release();
  215. if (itemCache)
  216. itemCache->release();
  217. }
  218. void Inventory::updateCache(ItemSlot* zSlot, int beforeKey)
  219. {
  220. if (!itemCache)
  221. return;
  222. int key = zSlot->zStack() ? zSlot->zStack()->zItem()->zItemType()->getId() : -1;
  223. if (key == beforeKey)
  224. return;
  225. if (beforeKey >= 0)
  226. {
  227. auto tmp = itemCache->safeGet(key, 0);
  228. if (tmp)
  229. tmp->removeValue(zSlot);
  230. }
  231. if (zSlot->zStack())
  232. {
  233. auto tmp = itemCache->safeGet(key, 0);
  234. if (!tmp)
  235. {
  236. tmp = new Array<ItemSlot*>();
  237. itemCache->put(key, tmp);
  238. }
  239. tmp->add(zSlot, 0);
  240. }
  241. }
  242. void Inventory::addSlot(ItemSlot* slot)
  243. {
  244. cs.lock();
  245. ((ItemSlotIDSetter*)slot)->setId(nextSlotId++);
  246. int pullPrio = slot->getPullPriority();
  247. int pushPrio = slot->getPushPriority();
  248. int index = 0;
  249. for (auto stack : *pullSlotsOrder)
  250. {
  251. if (stack->getPullPriority() > pullPrio)
  252. break;
  253. index++;
  254. }
  255. pullSlotsOrder->add(dynamic_cast<ItemSlot*>(slot->getThis()), index);
  256. index = 0;
  257. for (auto stack : *pushSlotsOrder)
  258. {
  259. if (stack->getPushPriority() > pushPrio)
  260. break;
  261. index++;
  262. }
  263. pushSlotsOrder->add(slot, index);
  264. updateCache(slot, -1);
  265. cs.unlock();
  266. }
  267. bool Inventory::allowPullStack(ItemSlot* zSlot, Direction dir) const
  268. {
  269. return pullSlotsOrder;
  270. }
  271. bool Inventory::allowPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const
  272. {
  273. return pushSlotsOrder;
  274. }
  275. void Inventory::afterPullStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  276. {}
  277. void Inventory::afterPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)
  278. {}
  279. void Inventory::loadInventory(Framework::StreamReader* zReader)
  280. {
  281. if (itemCache)
  282. {
  283. for (auto stack : *pushSlotsOrder)
  284. {
  285. int size = 0;
  286. zReader->lese((char*)&size, 4);
  287. if (size != 0)
  288. {
  289. int id = 0;
  290. zReader->lese((char*)&id, 4);
  291. Item* item = StaticRegistry<ItemType>::INSTANCE.zElement(id)->loadItem(zReader);
  292. stack->addItems(new ItemStack(item, size), NO_DIRECTION);
  293. }
  294. }
  295. }
  296. }
  297. void Inventory::saveInventory(Framework::StreamWriter* zWriter)
  298. {
  299. if (itemCache)
  300. {
  301. for (auto slot : *pushSlotsOrder)
  302. {
  303. const ItemStack* stack = slot->zStack();
  304. int value = 0;
  305. if (!stack || !stack->zItem())
  306. {
  307. zWriter->schreibe((char*)&value, 4);
  308. }
  309. else
  310. {
  311. value = stack->getSize();
  312. zWriter->schreibe((char*)&value, 4);
  313. value = stack->zItem()->zItemType()->getId();
  314. zWriter->schreibe((char*)&value, 4);
  315. stack->zItem()->zItemType()->saveItem(stack->zItem(), zWriter);
  316. }
  317. }
  318. }
  319. }
  320. void Inventory::localTransaction(Array< ItemSlot* >* zSourceSlots, Array< ItemSlot* >* zTargetSlots, ItemFilter* zFilter, int count, Direction outDir, Direction inDir)
  321. {
  322. if (itemCache)
  323. {
  324. cs.lock();
  325. auto sourceSlot = zSourceSlots ? zSourceSlots->begin() : pullSlotsOrder->begin();
  326. for (auto targetSlot = zTargetSlots->begin(); targetSlot; )
  327. {
  328. int amount = count;
  329. if (!targetSlot->isFull())
  330. {
  331. if (targetSlot->zStack())
  332. {
  333. Array<ItemSlot*>* zSurceListe = itemCache->safeGet(targetSlot->zStack()->zItem()->zItemType()->getId(), 0);
  334. if (zSurceListe)
  335. {
  336. Array<int> toDelete;
  337. int index = 0;
  338. for (auto sourceSlot = zSurceListe->begin(); sourceSlot; sourceSlot++, index++)
  339. {
  340. if (zSourceSlots && zSourceSlots->getWertIndex(sourceSlot) < 0)
  341. continue;
  342. if (zFilter && !zFilter->matchItem(sourceSlot->zStack()->zItem()))
  343. continue;
  344. int number = MIN(targetSlot->numberOfAddableItems(sourceSlot->zStack(), inDir), count);
  345. if (number > 0)
  346. {
  347. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  348. if (stack)
  349. {
  350. if (!sourceSlot->zStack())
  351. toDelete.add(index, 0);
  352. targetSlot->addItems(stack, inDir);
  353. updateCache(sourceSlot, targetSlot->zStack()->zItem()->zItemType()->getId());
  354. if (stack->getSize())
  355. {
  356. cs.unlock();
  357. throw stack;
  358. }
  359. stack->release();
  360. count -= number;
  361. if (count == 0)
  362. break;
  363. }
  364. }
  365. }
  366. for (auto indexToDelete = toDelete.begin(); indexToDelete; indexToDelete++)
  367. zSurceListe->remove(indexToDelete);
  368. if (count == 0)
  369. {
  370. cs.unlock();
  371. return;
  372. }
  373. }
  374. }
  375. else
  376. {
  377. while (sourceSlot && (!sourceSlot->zStack() || (zFilter && !zFilter->matchItem(sourceSlot->zStack()->zItem()))))
  378. sourceSlot++;
  379. if (!sourceSlot)
  380. {
  381. cs.unlock();
  382. return;
  383. }
  384. int number = MIN(targetSlot->numberOfAddableItems(sourceSlot->zStack(), inDir), count);
  385. if (number > 0)
  386. {
  387. ItemStack* stack = sourceSlot->takeItemsOut(number, outDir);
  388. if (stack)
  389. {
  390. targetSlot->addItems(stack, inDir);
  391. updateCache(sourceSlot, targetSlot->zStack()->zItem()->zItemType()->getId());
  392. updateCache(targetSlot, -1);
  393. if (stack->getSize())
  394. {
  395. cs.unlock();
  396. throw stack;
  397. }
  398. stack->release();
  399. count -= number;
  400. if (count == 0)
  401. {
  402. cs.unlock();
  403. return;
  404. }
  405. }
  406. }
  407. }
  408. }
  409. if (amount == count || targetSlot->isFull())
  410. targetSlot++;
  411. }
  412. cs.unlock();
  413. }
  414. }
  415. void Inventory::addItems(ItemStack* items, Direction dir)
  416. {
  417. if (itemCache && items && items->getSize() > 0)
  418. {
  419. cs.lock();
  420. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
  421. {
  422. if (!targetSlot->isFull())
  423. {
  424. if (targetSlot->zStack())
  425. {
  426. int number = MIN(targetSlot->numberOfAddableItems(items, dir), items->getSize());
  427. int tmp = number;
  428. if (number > 0 && allowPushStack(targetSlot, dir, items->zItem(), tmp))
  429. {
  430. number = MIN(number, tmp);
  431. ItemStack* stack = items->split(number);
  432. if (stack)
  433. {
  434. targetSlot->addItems(stack, dir);
  435. afterPushStack(targetSlot, dir, targetSlot->zStack()->zItem(), number);
  436. if (stack->getSize())
  437. throw stack;
  438. stack->release();
  439. if (!items->getSize())
  440. break;
  441. }
  442. }
  443. }
  444. else
  445. {
  446. int number = MIN(targetSlot->numberOfAddableItems(items, dir), items->getSize());
  447. int tmp = number;
  448. if (number > 0 && allowPushStack(targetSlot, dir, items->zItem(), tmp))
  449. {
  450. number = MIN(number, tmp);
  451. ItemStack* stack = items->split(number);
  452. if (stack)
  453. {
  454. targetSlot->addItems(stack, dir);
  455. updateCache(targetSlot, -1);
  456. afterPushStack(targetSlot, dir, targetSlot->zStack()->zItem(), number);
  457. if (stack->getSize())
  458. throw stack;
  459. stack->release();
  460. if (!items->getSize())
  461. break;
  462. }
  463. }
  464. }
  465. }
  466. }
  467. cs.unlock();
  468. }
  469. }
  470. ItemStack* Inventory::takeItemsOut(ItemSlot* zSlot, int count, Direction dir)
  471. {
  472. if (allowPullStack(zSlot, dir))
  473. {
  474. ItemStack* stack = zSlot->takeItemsOut(count, dir);
  475. if (stack)
  476. updateCache(zSlot, stack->zItem()->zItemType()->getId());
  477. return stack;
  478. }
  479. return 0;
  480. }
  481. InventoryInteraction Inventory::interactWith(Inventory* zInventory, Direction dir)
  482. {
  483. return InventoryInteraction(this, zInventory, dir);
  484. }
  485. void Inventory::unsaveAddItem(ItemStack* zStack, Direction dir)
  486. {
  487. addItems(zStack, dir);
  488. }
  489. int Inventory::numberOfAddableItems(const ItemStack* zStack, Direction dir) const
  490. {
  491. int count = 0;
  492. for (auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++)
  493. {
  494. int maxCount = targetSlot->numberOfAddableItems(zStack, dir);
  495. int allowed = maxCount;
  496. if (allowPushStack(targetSlot, dir, zStack->zItem(), allowed))
  497. count += MIN(maxCount, allowed);
  498. }
  499. return count;
  500. }
  501. Framework::Iterator<ItemSlot*> Inventory::begin()
  502. {
  503. return pullSlotsOrder->begin();
  504. }
  505. Framework::Iterator<ItemSlot*> Inventory::end()
  506. {
  507. return pullSlotsOrder->end();
  508. }