Inventory.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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.Enter();
  26. other->cs.Enter();
  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.Enter();
  34. other->cs.Enter();
  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.Enter();
  42. other->cs.Enter();
  43. return;
  44. }
  45. }
  46. }
  47. other->cs.Enter();
  48. current->cs.Enter();
  49. }
  50. void InventoryInteraction::unlock()
  51. {
  52. if( !current || !other ) return;
  53. if( current->location.x < other->location.x )
  54. {
  55. current->cs.Leave();
  56. other->cs.Leave();
  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.Leave();
  64. other->cs.Leave();
  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.Leave();
  72. other->cs.Leave();
  73. return;
  74. }
  75. }
  76. }
  77. other->cs.Leave();
  78. current->cs.Leave();
  79. }
  80. void InventoryInteraction::transaction( Inventory *zSource, Inventory *zTarget, ItemFilter *zFilter, Direction sourceView, Direction targetView, int count )
  81. {
  82. auto sourceSlot = zSource->pullSlotsOrder->getIterator();
  83. for( auto targetSlot = zTarget->pushSlotsOrder->getIterator(); 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->getIterator(); 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.getIterator(); 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, opposite( dir ), dir, count );
  180. }
  181. void InventoryInteraction::pushItems( int count, ItemFilter *zFilter )
  182. {
  183. if( !current || !other ) return;
  184. transaction( current, other, zFilter, dir, opposite( dir ), count );
  185. }
  186. Inventory::Inventory( const Framework::Vec3<float> location )
  187. {
  188. pullSlotsOrder = new Framework::RCArray<ItemSlot>();
  189. pushSlotsOrder = new Framework::RCArray<ItemSlot>();
  190. itemCache = new Framework::HashMap<int, Framework::Array<ItemSlot *> *>( ITEM_CACHE_SIZE, std::_Identity<int>() );
  191. }
  192. Inventory::~Inventory()
  193. {
  194. pullSlotsOrder->release();
  195. pushSlotsOrder->release();
  196. itemCache->release();
  197. }
  198. void Inventory::updateCache( ItemSlot *zSlot, int beforeKey )
  199. {
  200. int key = zSlot->zStack()->zItem()->zItemType()->getId();
  201. if( key == beforeKey )
  202. return;
  203. if( beforeKey >= 0 )
  204. {
  205. auto tmp = itemCache->safeGet( key, 0 );
  206. if( tmp )
  207. tmp->removeValue( zSlot );
  208. }
  209. if( zSlot->zStack() )
  210. {
  211. auto tmp = itemCache->safeGet( key, 0 );
  212. if( !tmp )
  213. {
  214. tmp = new Array<ItemSlot *>();
  215. itemCache->put( key, tmp );
  216. }
  217. tmp->add( zSlot, 0 );
  218. }
  219. }
  220. void Inventory::addSlot( ItemSlot *slot )
  221. {
  222. cs.Enter();
  223. int pullPrio = slot->getPullPriority();
  224. int pushPrio = slot->getPushPriority();
  225. int index = 0;
  226. for( auto iterator = pullSlotsOrder->getIterator(); iterator; iterator++, index++ )
  227. {
  228. if( iterator->getPullPriority() > pullPrio )
  229. break;
  230. }
  231. pullSlotsOrder->add( dynamic_cast<ItemSlot *>( slot->getThis() ), index );
  232. index = 0;
  233. for( auto iterator = pushSlotsOrder->getIterator(); iterator; iterator++, index++ )
  234. {
  235. if( iterator->getPushPriority() > pushPrio )
  236. break;
  237. }
  238. pullSlotsOrder->add( slot, index );
  239. updateCache( slot, -1 );
  240. cs.Leave();
  241. }
  242. bool Inventory::allowPullStack( ItemSlot *zSlot, Direction dir )
  243. {
  244. return true;
  245. }
  246. bool Inventory::allowPushStack( ItemSlot *zSlot, Direction dir, const Item *zItem, int &count )
  247. {
  248. return true;
  249. }
  250. void Inventory::afterPullStack( ItemSlot *zSlot, Direction dir, const Item *zItem, int count )
  251. {}
  252. void Inventory::afterPushStack( ItemSlot *zSlot, Direction dir, const Item *zItem, int count )
  253. {}
  254. void Inventory::localTransaction( Array< ItemSlot * > *zSourceSlots, Array< ItemSlot * > *zTargetSlots, ItemFilter *zFilter, int count )
  255. {
  256. cs.Enter();
  257. auto sourceSlot = zSourceSlots->getIterator();
  258. for( auto targetSlot = zTargetSlots->getIterator(); targetSlot; )
  259. {
  260. int amount = count;
  261. if( !targetSlot->isFull() )
  262. {
  263. if( targetSlot->zStack() )
  264. {
  265. Array<ItemSlot *> *zSurceListe = itemCache->safeGet( targetSlot->zStack()->zItem()->zItemType()->getId(), 0 );
  266. if( zSurceListe )
  267. {
  268. Array<int> toDelete;
  269. int index = 0;
  270. for( auto sourceSlot = zSurceListe->getIterator(); sourceSlot; sourceSlot++, index++ )
  271. {
  272. if( zSourceSlots->getWertIndex( sourceSlot ) < 0 )
  273. continue;
  274. if( zFilter && !zFilter->matchItem( sourceSlot->zStack()->zItem() ) )
  275. continue;
  276. int number = MIN( targetSlot->numberOfAddableItems( sourceSlot->zStack(), NO_DIRECTION ), count );
  277. if( number > 0 )
  278. {
  279. ItemStack *stack = sourceSlot->takeItemsOut( number, NO_DIRECTION );
  280. if( stack )
  281. {
  282. if( !sourceSlot->zStack() )
  283. toDelete.add( index, 0 );
  284. targetSlot->addItems( stack, NO_DIRECTION );
  285. if( stack->getSize() )
  286. {
  287. cs.Leave();
  288. throw stack;
  289. }
  290. stack->release();
  291. count -= number;
  292. if( count == 0 )
  293. break;
  294. }
  295. }
  296. }
  297. for( auto indexToDelete = toDelete.getIterator(); indexToDelete; indexToDelete++ )
  298. zSurceListe->remove( indexToDelete );
  299. if( count == 0 )
  300. {
  301. cs.Leave();
  302. return;
  303. }
  304. }
  305. }
  306. else
  307. {
  308. while( sourceSlot && ( !sourceSlot->zStack() || ( zFilter && !zFilter->matchItem( sourceSlot->zStack()->zItem() ) ) ) )
  309. sourceSlot++;
  310. if( !sourceSlot )
  311. {
  312. cs.Leave();
  313. return;
  314. }
  315. int number = MIN( targetSlot->numberOfAddableItems( sourceSlot->zStack(), NO_DIRECTION ), count );
  316. if( number > 0 )
  317. {
  318. ItemStack *stack = sourceSlot->takeItemsOut( number, NO_DIRECTION );
  319. if( stack )
  320. {
  321. updateCache( sourceSlot, targetSlot->zStack()->zItem()->zItemType()->getId() );
  322. targetSlot->addItems( stack, NO_DIRECTION );
  323. updateCache( targetSlot, targetSlot->zStack()->zItem()->zItemType()->getId() );
  324. if( stack->getSize() )
  325. {
  326. cs.Leave();
  327. throw stack;
  328. }
  329. stack->release();
  330. count -= number;
  331. if( count == 0 )
  332. {
  333. cs.Leave();
  334. return;
  335. }
  336. }
  337. }
  338. }
  339. }
  340. if( amount == count || targetSlot->isFull() )
  341. targetSlot++;
  342. }
  343. cs.Leave();
  344. }
  345. InventoryInteraction Inventory::interactWith( Inventory *zInventory, Direction dir )
  346. {
  347. return InventoryInteraction( this, zInventory, dir );
  348. }