Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. ReturnValue Game::internalMoveItem(Creature* actor, Cylinder* fromCylinder, Cylinder* toCylinder,
  2. int32_t index, Item* item, uint32_t count, Item** _moveItem, uint32_t flags /*= 0*/)
  3. {
  4. if(!toCylinder)
  5. return RET_NOTPOSSIBLE;
  6.  
  7. if(!item->getParent())
  8. {
  9. assert(fromCylinder == item->getParent());
  10. return internalAddItem(actor, toCylinder, item, INDEX_WHEREEVER, FLAG_NOLIMIT);
  11. }
  12.  
  13. Item* toItem = NULL;
  14. Cylinder* subCylinder = NULL;
  15.  
  16. int32_t floor = 0;
  17. while((subCylinder = toCylinder->__queryDestination(index, item, &toItem, flags)) != toCylinder)
  18. {
  19. toCylinder = subCylinder;
  20. flags = 0;
  21. //to prevent infinite loop
  22. if(++floor >= MAP_MAX_LAYERS)
  23. break;
  24. }
  25.  
  26. //destination is the same as the source?
  27. if(item == toItem)
  28. return RET_NOERROR; //silently ignore move
  29.  
  30. //check if we can add this item
  31. ReturnValue ret = toCylinder->__queryAdd(index, item, count, flags);
  32. if(ret == RET_NEEDEXCHANGE)
  33. {
  34. //check if we can add it to source cylinder
  35. int32_t fromIndex = fromCylinder->__getIndexOfThing(item);
  36.  
  37. ret = fromCylinder->__queryAdd(fromIndex, toItem, toItem->getItemCount(), 0);
  38. if(ret == RET_NOERROR)
  39. {
  40. //check how much we can move
  41. uint32_t maxExchangeQueryCount = 0;
  42. ReturnValue retExchangeMaxCount = fromCylinder->__queryMaxCount(-1, toItem, toItem->getItemCount(), maxExchangeQueryCount, 0);
  43.  
  44. if(retExchangeMaxCount != RET_NOERROR && maxExchangeQueryCount == 0)
  45. return retExchangeMaxCount;
  46.  
  47. if((toCylinder->__queryRemove(toItem, toItem->getItemCount(), flags) == RET_NOERROR) && ret == RET_NOERROR)
  48. {
  49. int32_t oldToItemIndex = toCylinder->__getIndexOfThing(toItem);
  50. toCylinder->__removeThing(toItem, toItem->getItemCount());
  51.  
  52. fromCylinder->__addThing(actor, toItem);
  53. if(oldToItemIndex != -1)
  54. toCylinder->postRemoveNotification(actor, toItem, fromCylinder, oldToItemIndex, true);
  55.  
  56. int32_t newToItemIndex = fromCylinder->__getIndexOfThing(toItem);
  57. if(newToItemIndex != -1)
  58. fromCylinder->postAddNotification(actor, toItem, toCylinder, newToItemIndex);
  59.  
  60. ret = toCylinder->__queryAdd(index, item, count, flags);
  61. toItem = NULL;
  62. }
  63. }
  64. }
  65.  
  66. if(ret != RET_NOERROR)
  67. return ret;
  68.  
  69. //check how much we can move
  70. uint32_t maxQueryCount = 0;
  71. ReturnValue retMaxCount = toCylinder->__queryMaxCount(index, item, count, maxQueryCount, flags);
  72. if(retMaxCount != RET_NOERROR && !maxQueryCount)
  73. return retMaxCount;
  74.  
  75. uint32_t m = maxQueryCount, n = 0;
  76. if(item->isStackable())
  77. m = std::min((uint32_t)count, m);
  78.  
  79. Item* moveItem = item;
  80. //check if we can remove this item
  81. ret = fromCylinder->__queryRemove(item, m, flags);
  82. if(ret != RET_NOERROR)
  83. return ret;
  84.  
  85. //remove the item
  86. int32_t itemIndex = fromCylinder->__getIndexOfThing(item);
  87. fromCylinder->__removeThing(item, m);
  88.  
  89. bool isCompleteRemoval = item->isRemoved();
  90. Item* updateItem = NULL;
  91. //update item(s)
  92. if(item->isStackable())
  93. {
  94. if(toItem && toItem->getID() == item->getID())
  95. {
  96. if (toItem->getActionId() != item->getActionId())
  97. {
  98. return internalAddItem(actor, toCylinder, item, INDEX_WHEREEVER, FLAG_NOLIMIT);
  99. }
  100.  
  101. n = std::min((uint32_t)100 - toItem->getItemCount(), m);
  102. toCylinder->__updateThing(toItem, toItem->getID(), toItem->getItemCount() + n);
  103. updateItem = toItem;
  104. }
  105.  
  106. if(m - n > 0)
  107. moveItem = Item::CreateItem(item->getID(), m - n);
  108. else
  109. moveItem = NULL;
  110.  
  111. if(item->isRemoved())
  112. freeThing(item);
  113. }
  114.  
  115. //add item
  116. if(moveItem /*m - n > 0*/)
  117. toCylinder->__addThing(actor, index, moveItem);
  118.  
  119. if(itemIndex != -1)
  120. fromCylinder->postRemoveNotification(actor, item, toCylinder, itemIndex, isCompleteRemoval);
  121.  
  122. if(moveItem)
  123. {
  124. int32_t moveItemIndex = toCylinder->__getIndexOfThing(moveItem);
  125. if(moveItemIndex != -1)
  126. toCylinder->postAddNotification(actor, moveItem, fromCylinder, moveItemIndex);
  127. }
  128.  
  129. if(updateItem)
  130. {
  131. int32_t updateItemIndex = toCylinder->__getIndexOfThing(updateItem);
  132. if(updateItemIndex != -1)
  133. toCylinder->postAddNotification(actor, updateItem, fromCylinder, updateItemIndex);
  134. }
  135.  
  136. if(_moveItem)
  137. {
  138. if(moveItem)
  139. *_moveItem = moveItem;
  140. else
  141. *_moveItem = item;
  142. }
  143.  
  144. //we could not move all, inform the player
  145. if(item->isStackable() && maxQueryCount < count)
  146. return retMaxCount;
  147.  
  148. return ret;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement