Inventory.cpp 14 KB

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