Guest User

Untitled

a guest
Feb 6th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. bool Game::removeMoney(Cylinder* cylinder, uint64_t money, uint32_t flags /*= 0*/)
  2. {
  3. if (cylinder == nullptr) {
  4. return false;
  5. }
  6.  
  7. if (money <= 0) {
  8. return true;
  9. }
  10.  
  11. std::vector<Container*> containers;
  12.  
  13. typedef std::multimap<uint64_t, Item*, std::less<uint64_t>> MoneyMap;
  14. typedef MoneyMap::value_type moneymap_pair;
  15. MoneyMap moneyMap;
  16. uint64_t moneyCount = 0;
  17.  
  18. for (int32_t i = cylinder->getFirstIndex(), j = cylinder->getLastIndex(); i < j; ++i) {
  19. Thing* thing = cylinder->getThing(i);
  20. if (!thing) {
  21. continue;
  22. }
  23.  
  24. Item* item = thing->getItem();
  25. if (!item) {
  26. continue;
  27. }
  28.  
  29. Container* container = item->getContainer();
  30. if (container) {
  31. containers.push_back(container);
  32. } else if (item->getWorth() != 0) {
  33. moneyCount += item->getWorth();
  34. moneyMap.insert(moneymap_pair(item->getWorth(), item));
  35. }
  36. }
  37.  
  38. size_t i = 0;
  39. while (i < containers.size()) {
  40. Container* container = containers[i++];
  41. for (Item* item : container->getItemList()) {
  42. Container* tmpContainer = item->getContainer();
  43. if (tmpContainer) {
  44. containers.push_back(tmpContainer);
  45. } else if (item->getWorth() != 0) {
  46. moneyCount += item->getWorth();
  47. moneyMap.insert(moneymap_pair(item->getWorth(), item));
  48. }
  49. }
  50. }
  51.  
  52. /*not enough money*/
  53. if (moneyCount < money) {
  54. return false;
  55. }
  56.  
  57. for (MoneyMap::const_iterator mit = moneyMap.begin(), mend = moneyMap.end(); mit != mend && money > 0; ++mit) {
  58. Item* item = mit->second;
  59. internalRemoveItem(item);
  60.  
  61. if (mit->first > money) {
  62. /* Remove a monetary value from an item*/
  63. uint64_t remaind = item->getWorth() - money;
  64. addMoney(cylinder, remaind, flags);
  65. money = 0;
  66. } else {
  67. money -= mit->first;
  68. }
  69. }
  70.  
  71. return money == 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment