Inventory.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. : ReferenceCounter(),
  188. location( location )
  189. {
  190. pullSlotsOrder = new Framework::RCArray<ItemSlot>();
  191. pushSlotsOrder = new Framework::RCArray<ItemSlot>();
  192. itemCache = new Framework::HashMap<int, Framework::Array<ItemSlot *> *>( ITEM_CACHE_SIZE, std::_Identity<int>() );
  193. }
  194. Inventory::~Inventory()
  195. {
  196. pullSlotsOrder->release();
  197. pushSlotsOrder->release();
  198. itemCache->release();
  199. }
  200. void Inventory::updateCache( ItemSlot *zSlot, int beforeKey )
  201. {
  202. int key = zSlot->zStack()->zItem()->zItemType()->getId();
  203. if( key == beforeKey )
  204. return;
  205. if( beforeKey >= 0 )
  206. {
  207. auto tmp = itemCache->safeGet( key, 0 );
  208. if( tmp )
  209. tmp->removeValue( zSlot );
  210. }
  211. if( zSlot->zStack() )
  212. {
  213. auto tmp = itemCache->safeGet( key, 0 );
  214. if( !tmp )
  215. {
  216. tmp = new Array<ItemSlot *>();
  217. itemCache->put( key, tmp );
  218. }
  219. tmp->add( zSlot, 0 );
  220. }
  221. }
  222. void Inventory::addSlot( ItemSlot *slot )
  223. {
  224. cs.Enter();
  225. int pullPrio = slot->getPullPriority();
  226. int pushPrio = slot->getPushPriority();
  227. int index = 0;
  228. for( auto iterator = pullSlotsOrder->getIterator(); iterator; iterator++, index++ )
  229. {
  230. if( iterator->getPullPriority() > pullPrio )
  231. break;
  232. }
  233. pullSlotsOrder->add( dynamic_cast<ItemSlot *>(slot->getThis()), index );
  234. index = 0;
  235. for( auto iterator = pushSlotsOrder->getIterator(); iterator; iterator++, index++ )
  236. {
  237. if( iterator->getPushPriority() > pushPrio )
  238. break;
  239. }
  240. pullSlotsOrder->add( slot, index );
  241. updateCache( slot, -1 );
  242. cs.Leave();
  243. }
  244. bool Inventory::allowPullStack( ItemSlot *zSlot, Direction dir )
  245. {
  246. return true;
  247. }
  248. bool Inventory::allowPushStack( ItemSlot *zSlot, Direction dir, const Item *zItem, int &count )
  249. {
  250. return true;
  251. }
  252. void Inventory::afterPullStack( ItemSlot *zSlot, Direction dir, const Item *zItem, int count )
  253. {}
  254. void Inventory::afterPushStack( ItemSlot *zSlot, Direction dir, const Item *zItem, int count )
  255. {}
  256. void Inventory::loadInventory( Framework::StreamReader *zReader )
  257. {
  258. for( auto iterator = pushSlotsOrder->getIterator(); iterator; iterator++ )
  259. {
  260. int size = 0;
  261. zReader->lese( (char *)&size, 4 );
  262. if( size != 0 )
  263. {
  264. int id = 0;
  265. zReader->lese( (char *)&id, 4 );
  266. Item *item = StaticRegistry<ItemType>::INSTANCE.zElement( id )->loadItem( zReader );
  267. iterator->addItems( new ItemStack( item, size ), NO_DIRECTION );
  268. }
  269. }
  270. }
  271. void Inventory::saveInventory( Framework::StreamWriter *zWriter )
  272. {
  273. for( auto iterator = pushSlotsOrder->getIterator(); iterator; iterator++ )
  274. {
  275. const ItemStack *stack = iterator->zStack();
  276. int value = 0;
  277. if( !stack || !stack->zItem() )
  278. {
  279. zWriter->schreibe( (char *)&value, 4 );
  280. }
  281. else
  282. {
  283. value = stack->getSize();
  284. zWriter->schreibe( (char *)&value, 4 );
  285. value = stack->zItem()->zItemType()->getId();
  286. zWriter->schreibe( (char *)&value, 4 );
  287. stack->zItem()->zItemType()->saveItem( stack->zItem(), zWriter );
  288. }
  289. }
  290. }
  291. void Inventory::localTransaction( Array< ItemSlot * > *zSourceSlots, Array< ItemSlot * > *zTargetSlots, ItemFilter *zFilter, int count )
  292. {
  293. cs.Enter();
  294. auto sourceSlot = zSourceSlots->getIterator();
  295. for( auto targetSlot = zTargetSlots->getIterator(); targetSlot; )
  296. {
  297. int amount = count;
  298. if( !targetSlot->isFull() )
  299. {
  300. if( targetSlot->zStack() )
  301. {
  302. Array<ItemSlot *> *zSurceListe = itemCache->safeGet( targetSlot->zStack()->zItem()->zItemType()->getId(), 0 );
  303. if( zSurceListe )
  304. {
  305. Array<int> toDelete;
  306. int index = 0;
  307. for( auto sourceSlot = zSurceListe->getIterator(); sourceSlot; sourceSlot++, index++ )
  308. {
  309. if( zSourceSlots->getWertIndex( sourceSlot ) < 0 )
  310. continue;
  311. if( zFilter && !zFilter->matchItem( sourceSlot->zStack()->zItem() ) )
  312. continue;
  313. int number = MIN( targetSlot->numberOfAddableItems( sourceSlot->zStack(), NO_DIRECTION ), count );
  314. if( number > 0 )
  315. {
  316. ItemStack *stack = sourceSlot->takeItemsOut( number, NO_DIRECTION );
  317. if( stack )
  318. {
  319. if( !sourceSlot->zStack() )
  320. toDelete.add( index, 0 );
  321. targetSlot->addItems( stack, NO_DIRECTION );
  322. if( stack->getSize() )
  323. {
  324. cs.Leave();
  325. throw stack;
  326. }
  327. stack->release();
  328. count -= number;
  329. if( count == 0 )
  330. break;
  331. }
  332. }
  333. }
  334. for( auto indexToDelete = toDelete.getIterator(); indexToDelete; indexToDelete++ )
  335. zSurceListe->remove( indexToDelete );
  336. if( count == 0 )
  337. {
  338. cs.Leave();
  339. return;
  340. }
  341. }
  342. }
  343. else
  344. {
  345. while( sourceSlot && (!sourceSlot->zStack() || (zFilter && !zFilter->matchItem( sourceSlot->zStack()->zItem() ))) )
  346. sourceSlot++;
  347. if( !sourceSlot )
  348. {
  349. cs.Leave();
  350. return;
  351. }
  352. int number = MIN( targetSlot->numberOfAddableItems( sourceSlot->zStack(), NO_DIRECTION ), count );
  353. if( number > 0 )
  354. {
  355. ItemStack *stack = sourceSlot->takeItemsOut( number, NO_DIRECTION );
  356. if( stack )
  357. {
  358. updateCache( sourceSlot, targetSlot->zStack()->zItem()->zItemType()->getId() );
  359. targetSlot->addItems( stack, NO_DIRECTION );
  360. updateCache( targetSlot, targetSlot->zStack()->zItem()->zItemType()->getId() );
  361. if( stack->getSize() )
  362. {
  363. cs.Leave();
  364. throw stack;
  365. }
  366. stack->release();
  367. count -= number;
  368. if( count == 0 )
  369. {
  370. cs.Leave();
  371. return;
  372. }
  373. }
  374. }
  375. }
  376. }
  377. if( amount == count || targetSlot->isFull() )
  378. targetSlot++;
  379. }
  380. cs.Leave();
  381. }
  382. InventoryInteraction Inventory::interactWith( Inventory *zInventory, Direction dir )
  383. {
  384. return InventoryInteraction( this, zInventory, dir );
  385. }