Inventory.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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, 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()->zItem()->zItemType()->getId();
  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.Enter();
  239. int pullPrio = slot->getPullPriority();
  240. int pushPrio = slot->getPushPriority();
  241. int index = 0;
  242. for( auto iterator = pullSlotsOrder->getIterator(); iterator; iterator++, index++ )
  243. {
  244. if( iterator->getPullPriority() > pullPrio )
  245. break;
  246. }
  247. pullSlotsOrder->add( dynamic_cast<ItemSlot *>(slot->getThis()), index );
  248. index = 0;
  249. for( auto iterator = pushSlotsOrder->getIterator(); iterator; iterator++, index++ )
  250. {
  251. if( iterator->getPushPriority() > pushPrio )
  252. break;
  253. }
  254. pullSlotsOrder->add( slot, index );
  255. updateCache( slot, -1 );
  256. cs.Leave();
  257. }
  258. bool Inventory::allowPullStack( ItemSlot *zSlot, Direction dir )
  259. {
  260. return pullSlotsOrder;
  261. }
  262. bool Inventory::allowPushStack( ItemSlot *zSlot, Direction dir, const Item *zItem, int &count )
  263. {
  264. return pushSlotsOrder;
  265. }
  266. void Inventory::afterPullStack( ItemSlot *zSlot, Direction dir, const Item *zItem, int count )
  267. {}
  268. void Inventory::afterPushStack( ItemSlot *zSlot, Direction dir, const Item *zItem, int count )
  269. {}
  270. void Inventory::loadInventory( Framework::StreamReader *zReader )
  271. {
  272. if( itemCache )
  273. {
  274. for( auto iterator = pushSlotsOrder->getIterator(); iterator; iterator++ )
  275. {
  276. int size = 0;
  277. zReader->lese( (char *)&size, 4 );
  278. if( size != 0 )
  279. {
  280. int id = 0;
  281. zReader->lese( (char *)&id, 4 );
  282. Item *item = StaticRegistry<ItemType>::INSTANCE.zElement( id )->loadItem( zReader );
  283. iterator->addItems( new ItemStack( item, size ), NO_DIRECTION );
  284. }
  285. }
  286. }
  287. }
  288. void Inventory::saveInventory( Framework::StreamWriter *zWriter )
  289. {
  290. if( itemCache )
  291. {
  292. for( auto iterator = pushSlotsOrder->getIterator(); iterator; iterator++ )
  293. {
  294. const ItemStack *stack = iterator->zStack();
  295. int value = 0;
  296. if( !stack || !stack->zItem() )
  297. {
  298. zWriter->schreibe( (char *)&value, 4 );
  299. }
  300. else
  301. {
  302. value = stack->getSize();
  303. zWriter->schreibe( (char *)&value, 4 );
  304. value = stack->zItem()->zItemType()->getId();
  305. zWriter->schreibe( (char *)&value, 4 );
  306. stack->zItem()->zItemType()->saveItem( stack->zItem(), zWriter );
  307. }
  308. }
  309. }
  310. }
  311. void Inventory::localTransaction( Array< ItemSlot * > *zSourceSlots, Array< ItemSlot * > *zTargetSlots, ItemFilter *zFilter, int count )
  312. {
  313. if( itemCache )
  314. {
  315. cs.Enter();
  316. auto sourceSlot = zSourceSlots->getIterator();
  317. for( auto targetSlot = zTargetSlots->getIterator(); targetSlot; )
  318. {
  319. int amount = count;
  320. if( !targetSlot->isFull() )
  321. {
  322. if( targetSlot->zStack() )
  323. {
  324. Array<ItemSlot *> *zSurceListe = itemCache->safeGet( targetSlot->zStack()->zItem()->zItemType()->getId(), 0 );
  325. if( zSurceListe )
  326. {
  327. Array<int> toDelete;
  328. int index = 0;
  329. for( auto sourceSlot = zSurceListe->getIterator(); sourceSlot; sourceSlot++, index++ )
  330. {
  331. if( zSourceSlots->getWertIndex( sourceSlot ) < 0 )
  332. continue;
  333. if( zFilter && !zFilter->matchItem( sourceSlot->zStack()->zItem() ) )
  334. continue;
  335. int number = MIN( targetSlot->numberOfAddableItems( sourceSlot->zStack(), NO_DIRECTION ), count );
  336. if( number > 0 )
  337. {
  338. ItemStack *stack = sourceSlot->takeItemsOut( number, NO_DIRECTION );
  339. if( stack )
  340. {
  341. if( !sourceSlot->zStack() )
  342. toDelete.add( index, 0 );
  343. targetSlot->addItems( stack, NO_DIRECTION );
  344. if( stack->getSize() )
  345. {
  346. cs.Leave();
  347. throw stack;
  348. }
  349. stack->release();
  350. count -= number;
  351. if( count == 0 )
  352. break;
  353. }
  354. }
  355. }
  356. for( auto indexToDelete = toDelete.getIterator(); indexToDelete; indexToDelete++ )
  357. zSurceListe->remove( indexToDelete );
  358. if( count == 0 )
  359. {
  360. cs.Leave();
  361. return;
  362. }
  363. }
  364. }
  365. else
  366. {
  367. while( sourceSlot && (!sourceSlot->zStack() || (zFilter && !zFilter->matchItem( sourceSlot->zStack()->zItem() ))) )
  368. sourceSlot++;
  369. if( !sourceSlot )
  370. {
  371. cs.Leave();
  372. return;
  373. }
  374. int number = MIN( targetSlot->numberOfAddableItems( sourceSlot->zStack(), NO_DIRECTION ), count );
  375. if( number > 0 )
  376. {
  377. ItemStack *stack = sourceSlot->takeItemsOut( number, NO_DIRECTION );
  378. if( stack )
  379. {
  380. updateCache( sourceSlot, targetSlot->zStack()->zItem()->zItemType()->getId() );
  381. targetSlot->addItems( stack, NO_DIRECTION );
  382. updateCache( targetSlot, targetSlot->zStack()->zItem()->zItemType()->getId() );
  383. if( stack->getSize() )
  384. {
  385. cs.Leave();
  386. throw stack;
  387. }
  388. stack->release();
  389. count -= number;
  390. if( count == 0 )
  391. {
  392. cs.Leave();
  393. return;
  394. }
  395. }
  396. }
  397. }
  398. }
  399. if( amount == count || targetSlot->isFull() )
  400. targetSlot++;
  401. }
  402. cs.Leave();
  403. }
  404. }
  405. InventoryInteraction Inventory::interactWith( Inventory *zInventory, Direction dir )
  406. {
  407. return InventoryInteraction( this, zInventory, dir );
  408. }