Inventory.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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->afterPullStack( sourceSlot, sourceView, targetSlot->zStack()->zItem(), number );
  112. zTarget->afterPushStack( targetSlot, targetView, targetSlot->zStack()->zItem(), number );
  113. if( stack->getSize() )
  114. throw stack;
  115. stack->release();
  116. count -= number;
  117. if( count == 0 )
  118. break;
  119. }
  120. }
  121. }
  122. for( auto indexToDelete = toDelete.begin(); indexToDelete; indexToDelete++ )
  123. zSurceListe->remove( indexToDelete );
  124. if( count == 0 )
  125. return;
  126. }
  127. }
  128. else
  129. {
  130. while( sourceSlot && (!sourceSlot->zStack() || (zFilter && !zFilter->matchItem( sourceSlot->zStack()->zItem() ))) )
  131. sourceSlot++;
  132. if( !sourceSlot )
  133. return;
  134. int number = MIN( targetSlot->numberOfAddableItems( sourceSlot->zStack(), sourceView ), count );
  135. int tmp = number;
  136. if( number > 0 && zSource->allowPullStack( sourceSlot, sourceView ) && zTarget->allowPushStack( targetSlot, targetView, sourceSlot->zStack()->zItem(), tmp ) )
  137. {
  138. number = MIN( number, tmp );
  139. if( number > 0 )
  140. {
  141. ItemStack* stack = sourceSlot->takeItemsOut( number, dir );
  142. if( stack )
  143. {
  144. targetSlot->addItems( stack, dir );
  145. zSource->updateCache( sourceSlot, targetSlot->zStack()->zItem()->zItemType()->getId() );
  146. zTarget->updateCache( targetSlot, targetSlot->zStack()->zItem()->zItemType()->getId() );
  147. zSource->afterPullStack( sourceSlot, sourceView, targetSlot->zStack()->zItem(), number );
  148. zTarget->afterPushStack( targetSlot, targetView, targetSlot->zStack()->zItem(), number );
  149. if( stack->getSize() )
  150. throw stack;
  151. stack->release();
  152. count -= number;
  153. if( count == 0 )
  154. return;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. if( amount == count || targetSlot->isFull() )
  161. targetSlot++;
  162. }
  163. }
  164. InventoryInteraction& InventoryInteraction::operator=( const InventoryInteraction& data )
  165. {
  166. if( &data == this ) return *this;
  167. unlock();
  168. current = data.current;
  169. other = data.other;
  170. dir = data.dir;
  171. lock();
  172. return *this;
  173. }
  174. void InventoryInteraction::endInteraction()
  175. {
  176. unlock();
  177. current = 0;
  178. other = 0;
  179. }
  180. void InventoryInteraction::pullItems( int count, ItemFilter* zFilter )
  181. {
  182. if( !current || !other ) return;
  183. transaction( other, current, zFilter, getOppositeDirection( dir ), dir, count );
  184. }
  185. void InventoryInteraction::pushItems( int count, ItemFilter* zFilter )
  186. {
  187. if( !current || !other ) return;
  188. transaction( current, other, zFilter, dir, getOppositeDirection( dir ), count );
  189. }
  190. Inventory::Inventory( const Framework::Vec3<float> location, bool hasInventory )
  191. : ReferenceCounter(),
  192. location( location )
  193. {
  194. if( hasInventory )
  195. {
  196. pullSlotsOrder = new Framework::RCArray<ItemSlot>();
  197. pushSlotsOrder = new Framework::RCArray<ItemSlot>();
  198. itemCache = new Framework::HashMap<int, Framework::Array<ItemSlot*>*>( ITEM_CACHE_SIZE, std::_Identity<int>() );
  199. }
  200. else
  201. {
  202. pullSlotsOrder = 0;
  203. pushSlotsOrder = 0;
  204. itemCache = 0;
  205. }
  206. }
  207. Inventory::~Inventory()
  208. {
  209. if( pullSlotsOrder )
  210. pullSlotsOrder->release();
  211. if( pushSlotsOrder )
  212. pushSlotsOrder->release();
  213. if( itemCache )
  214. itemCache->release();
  215. }
  216. void Inventory::updateCache( ItemSlot* zSlot, int beforeKey )
  217. {
  218. if( !itemCache )
  219. return;
  220. int key = zSlot->zStack() ? zSlot->zStack()->zItem()->zItemType()->getId() : -1;
  221. if( key == beforeKey )
  222. return;
  223. if( beforeKey >= 0 )
  224. {
  225. auto tmp = itemCache->safeGet( key, 0 );
  226. if( tmp )
  227. tmp->removeValue( zSlot );
  228. }
  229. if( zSlot->zStack() )
  230. {
  231. auto tmp = itemCache->safeGet( key, 0 );
  232. if( !tmp )
  233. {
  234. tmp = new Array<ItemSlot*>();
  235. itemCache->put( key, tmp );
  236. }
  237. tmp->add( zSlot, 0 );
  238. }
  239. }
  240. void Inventory::addSlot( ItemSlot* slot )
  241. {
  242. cs.lock();
  243. int pullPrio = slot->getPullPriority();
  244. int pushPrio = slot->getPushPriority();
  245. int index = 0;
  246. for( auto stack : *pullSlotsOrder )
  247. {
  248. if( stack->getPullPriority() > pullPrio )
  249. break;
  250. index++;
  251. }
  252. pullSlotsOrder->add( dynamic_cast<ItemSlot*>(slot->getThis()), index );
  253. index = 0;
  254. for( auto stack : *pushSlotsOrder )
  255. {
  256. if( stack->getPushPriority() > pushPrio )
  257. break;
  258. index++;
  259. }
  260. pushSlotsOrder->add( slot, index );
  261. updateCache( slot, -1 );
  262. cs.unlock();
  263. }
  264. bool Inventory::allowPullStack( ItemSlot* zSlot, Direction dir ) const
  265. {
  266. return pullSlotsOrder;
  267. }
  268. bool Inventory::allowPushStack( ItemSlot* zSlot, Direction dir, const Item* zItem, int& count ) const
  269. {
  270. return pushSlotsOrder;
  271. }
  272. void Inventory::afterPullStack( ItemSlot* zSlot, Direction dir, const Item* zItem, int count )
  273. {}
  274. void Inventory::afterPushStack( ItemSlot* zSlot, Direction dir, const Item* zItem, int count )
  275. {}
  276. void Inventory::loadInventory( Framework::StreamReader* zReader )
  277. {
  278. if( itemCache )
  279. {
  280. for( auto stack : *pushSlotsOrder )
  281. {
  282. int size = 0;
  283. zReader->lese( (char*)&size, 4 );
  284. if( size != 0 )
  285. {
  286. int id = 0;
  287. zReader->lese( (char*)&id, 4 );
  288. Item* item = StaticRegistry<ItemType>::INSTANCE.zElement( id )->loadItem( zReader );
  289. stack->addItems( new ItemStack( item, size ), NO_DIRECTION );
  290. }
  291. }
  292. }
  293. }
  294. void Inventory::saveInventory( Framework::StreamWriter* zWriter )
  295. {
  296. if( itemCache )
  297. {
  298. for( auto slot : *pushSlotsOrder )
  299. {
  300. const ItemStack* stack = slot->zStack();
  301. int value = 0;
  302. if( !stack || !stack->zItem() )
  303. {
  304. zWriter->schreibe( (char*)&value, 4 );
  305. }
  306. else
  307. {
  308. value = stack->getSize();
  309. zWriter->schreibe( (char*)&value, 4 );
  310. value = stack->zItem()->zItemType()->getId();
  311. zWriter->schreibe( (char*)&value, 4 );
  312. stack->zItem()->zItemType()->saveItem( stack->zItem(), zWriter );
  313. }
  314. }
  315. }
  316. }
  317. void Inventory::localTransaction( Array< ItemSlot* >* zSourceSlots, Array< ItemSlot* >* zTargetSlots, ItemFilter* zFilter, int count )
  318. {
  319. if( itemCache )
  320. {
  321. cs.lock();
  322. auto sourceSlot = zSourceSlots->begin();
  323. for( auto targetSlot = zTargetSlots->begin(); targetSlot; )
  324. {
  325. int amount = count;
  326. if( !targetSlot->isFull() )
  327. {
  328. if( targetSlot->zStack() )
  329. {
  330. Array<ItemSlot*>* zSurceListe = itemCache->safeGet( targetSlot->zStack()->zItem()->zItemType()->getId(), 0 );
  331. if( zSurceListe )
  332. {
  333. Array<int> toDelete;
  334. int index = 0;
  335. for( auto sourceSlot = zSurceListe->begin(); sourceSlot; sourceSlot++, index++ )
  336. {
  337. if( zSourceSlots->getWertIndex( sourceSlot ) < 0 )
  338. continue;
  339. if( zFilter && !zFilter->matchItem( sourceSlot->zStack()->zItem() ) )
  340. continue;
  341. int number = MIN( targetSlot->numberOfAddableItems( sourceSlot->zStack(), NO_DIRECTION ), count );
  342. if( number > 0 )
  343. {
  344. ItemStack* stack = sourceSlot->takeItemsOut( number, NO_DIRECTION );
  345. if( stack )
  346. {
  347. if( !sourceSlot->zStack() )
  348. toDelete.add( index, 0 );
  349. targetSlot->addItems( stack, NO_DIRECTION );
  350. if( stack->getSize() )
  351. {
  352. cs.unlock();
  353. throw stack;
  354. }
  355. stack->release();
  356. count -= number;
  357. if( count == 0 )
  358. break;
  359. }
  360. }
  361. }
  362. for( auto indexToDelete = toDelete.begin(); indexToDelete; indexToDelete++ )
  363. zSurceListe->remove( indexToDelete );
  364. if( count == 0 )
  365. {
  366. cs.unlock();
  367. return;
  368. }
  369. }
  370. }
  371. else
  372. {
  373. while( sourceSlot && (!sourceSlot->zStack() || (zFilter && !zFilter->matchItem( sourceSlot->zStack()->zItem() ))) )
  374. sourceSlot++;
  375. if( !sourceSlot )
  376. {
  377. cs.unlock();
  378. return;
  379. }
  380. int number = MIN( targetSlot->numberOfAddableItems( sourceSlot->zStack(), NO_DIRECTION ), count );
  381. if( number > 0 )
  382. {
  383. ItemStack* stack = sourceSlot->takeItemsOut( number, NO_DIRECTION );
  384. if( stack )
  385. {
  386. targetSlot->addItems( stack, NO_DIRECTION );
  387. updateCache( sourceSlot, targetSlot->zStack()->zItem()->zItemType()->getId() );
  388. updateCache( targetSlot, targetSlot->zStack()->zItem()->zItemType()->getId() );
  389. if( stack->getSize() )
  390. {
  391. cs.unlock();
  392. throw stack;
  393. }
  394. stack->release();
  395. count -= number;
  396. if( count == 0 )
  397. {
  398. cs.unlock();
  399. return;
  400. }
  401. }
  402. }
  403. }
  404. }
  405. if( amount == count || targetSlot->isFull() )
  406. targetSlot++;
  407. }
  408. cs.unlock();
  409. }
  410. }
  411. void Inventory::addItems( ItemStack* items, Direction dir )
  412. {
  413. if( itemCache && items && items->getSize() > 0 )
  414. {
  415. cs.lock();
  416. for( auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++ )
  417. {
  418. if( !targetSlot->isFull() )
  419. {
  420. if( targetSlot->zStack() )
  421. {
  422. int number = MIN( targetSlot->numberOfAddableItems( items, dir ), items->getSize() );
  423. int tmp = number;
  424. if( number > 0 && allowPushStack( targetSlot, dir, items->zItem(), tmp ) )
  425. {
  426. number = MIN( number, tmp );
  427. ItemStack* stack = items->split( number );
  428. if( stack )
  429. {
  430. targetSlot->addItems( stack, dir );
  431. afterPushStack( targetSlot, dir, targetSlot->zStack()->zItem(), number );
  432. if( stack->getSize() )
  433. throw stack;
  434. stack->release();
  435. if( !items->getSize() )
  436. break;
  437. }
  438. }
  439. }
  440. else
  441. {
  442. int number = MIN( targetSlot->numberOfAddableItems( items, dir ), items->getSize() );
  443. int tmp = number;
  444. if( number > 0 && allowPushStack( targetSlot, dir, items->zItem(), tmp ) )
  445. {
  446. number = MIN( number, tmp );
  447. ItemStack* stack = items->split( number );
  448. if( stack )
  449. {
  450. targetSlot->addItems( stack, dir );
  451. updateCache( targetSlot, targetSlot->zStack()->zItem()->zItemType()->getId() );
  452. afterPushStack( targetSlot, dir, targetSlot->zStack()->zItem(), number );
  453. if( stack->getSize() )
  454. throw stack;
  455. stack->release();
  456. if( !items->getSize() )
  457. break;
  458. }
  459. }
  460. }
  461. }
  462. }
  463. cs.unlock();
  464. }
  465. }
  466. InventoryInteraction Inventory::interactWith( Inventory* zInventory, Direction dir )
  467. {
  468. return InventoryInteraction( this, zInventory, dir );
  469. }
  470. void Inventory::unsaveAddItem( ItemStack* zStack, Direction dir )
  471. {
  472. addItems( zStack, dir );
  473. }
  474. int Inventory::numberOfAddableItems( const ItemStack* zStack, Direction dir ) const
  475. {
  476. int count = 0;
  477. for( auto targetSlot = pushSlotsOrder->begin(); targetSlot; targetSlot++ )
  478. {
  479. int maxCount = targetSlot->numberOfAddableItems( zStack, dir );
  480. int allowed = maxCount;
  481. if( allowPushStack( targetSlot, dir, zStack->zItem(), allowed ) )
  482. count += MIN( maxCount, allowed );
  483. }
  484. return count;
  485. }