Inventory.cpp 17 KB

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