ResourceSet.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * ResourceSet.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. typedef si32 TResource;
  12. typedef si64 TResourceCap; //to avoid overflow when adding integers. Signed values are easier to control.
  13. class JsonNode;
  14. namespace Res
  15. {
  16. class ResourceSet;
  17. bool canAfford(const ResourceSet &res, const ResourceSet &price); //can a be used to pay price b
  18. enum ERes
  19. {
  20. WOOD = 0, MERCURY, ORE, SULFUR, CRYSTAL, GEMS, GOLD, MITHRIL,
  21. WOOD_AND_ORE = 127 // special case for town bonus resource
  22. };
  23. //class to be representing a vector of resource
  24. class ResourceSet : public std::vector<int>
  25. {
  26. public:
  27. DLL_LINKAGE ResourceSet();
  28. // read resources set from json. Format example: { "gold": 500, "wood":5 }
  29. DLL_LINKAGE ResourceSet(const JsonNode & node);
  30. #define scalarOperator(OPSIGN) \
  31. ResourceSet operator OPSIGN(const TResource &rhs) const \
  32. { \
  33. ResourceSet ret = *this; \
  34. for(int i = 0; i < size(); i++) \
  35. ret[i] = at(i) OPSIGN rhs; \
  36. \
  37. return ret; \
  38. }
  39. #define vectorOperator(OPSIGN) \
  40. ResourceSet operator OPSIGN(const ResourceSet &rhs) const \
  41. { \
  42. ResourceSet ret = *this; \
  43. for(int i = 0; i < size(); i++) \
  44. ret[i] = at(i) OPSIGN rhs[i]; \
  45. \
  46. return ret; \
  47. }
  48. #define opEqOperator(OPSIGN, RHS_TYPE) \
  49. ResourceSet& operator OPSIGN ## =(const RHS_TYPE &rhs) \
  50. { \
  51. return *this = *this OPSIGN rhs; \
  52. }
  53. scalarOperator(+)
  54. scalarOperator(-)
  55. scalarOperator(*)
  56. scalarOperator(/)
  57. opEqOperator(+, TResource)
  58. opEqOperator(-, TResource)
  59. opEqOperator(*, TResource)
  60. vectorOperator(+)
  61. vectorOperator(-)
  62. opEqOperator(+, ResourceSet)
  63. opEqOperator(-, ResourceSet)
  64. #undef scalarOperator
  65. #undef vectorOperator
  66. #undef opEqOperator
  67. //to be used for calculations of type "how many units of sth can I afford?"
  68. int operator/(const ResourceSet &rhs)
  69. {
  70. int ret = INT_MAX;
  71. for(int i = 0; i < size(); i++)
  72. if(rhs[i])
  73. vstd::amin(ret, at(i) / rhs[i]);
  74. return ret;
  75. }
  76. ResourceSet & operator=(const TResource &rhs)
  77. {
  78. for(int i = 0; i < size(); i++)
  79. at(i) = rhs;
  80. return *this;
  81. }
  82. ResourceSet operator-() const
  83. {
  84. ResourceSet ret;
  85. for(int i = 0; i < size(); i++)
  86. ret[i] = -at(i);
  87. return ret;
  88. }
  89. // WARNING: comparison operators are used for "can afford" relation: a <= b means that foreach i a[i] <= b[i]
  90. // that doesn't work the other way: a > b doesn't mean that a cannot be afforded with b, it's still b can afford a
  91. // bool operator<(const ResourceSet &rhs)
  92. // {
  93. // for(int i = 0; i < size(); i++)
  94. // if(at(i) >= rhs[i])
  95. // return false;
  96. //
  97. // return true;
  98. // }
  99. template <typename Handler> void serialize(Handler &h, const int version)
  100. {
  101. h & static_cast<std::vector<int>&>(*this);
  102. }
  103. DLL_LINKAGE void amax(const TResourceCap &val); //performs vstd::amax on each element
  104. DLL_LINKAGE bool nonZero() const; //returns true if at least one value is non-zero;
  105. DLL_LINKAGE bool canAfford(const ResourceSet &price) const;
  106. DLL_LINKAGE bool canBeAfforded(const ResourceSet &res) const;
  107. //special iterator of iterating over non-zero resources in set
  108. class DLL_LINKAGE nziterator
  109. {
  110. struct ResEntry
  111. {
  112. Res::ERes resType;
  113. TResourceCap resVal;
  114. } cur;
  115. const ResourceSet &rs;
  116. void advance();
  117. public:
  118. nziterator(const ResourceSet &RS);
  119. bool valid();
  120. nziterator operator++();
  121. nziterator operator++(int);
  122. const ResEntry& operator*() const;
  123. const ResEntry* operator->() const;
  124. };
  125. };
  126. }
  127. typedef Res::ResourceSet TResources;