ResourceSet.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #include "GameConstants.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. using TResource = int32_t;
  14. using TResourceCap = int64_t; //to avoid overflow when adding integers. Signed values are easier to control.
  15. class JsonNode;
  16. class JsonSerializeFormat;
  17. class ResourceSet;
  18. //class to be representing a vector of resource
  19. class ResourceSet
  20. {
  21. private:
  22. std::array<TResource, GameConstants::RESOURCE_QUANTITY> container = {};
  23. public:
  24. // read resources set from json. Format example: { "gold": 500, "wood":5 }
  25. DLL_LINKAGE ResourceSet(const JsonNode & node);
  26. DLL_LINKAGE ResourceSet();
  27. #define scalarOperator(OPSIGN) \
  28. ResourceSet& operator OPSIGN ## =(const TResource &rhs) \
  29. { \
  30. for(auto i = 0; i < container.size(); i++) \
  31. container.at(i) OPSIGN ## = rhs; \
  32. \
  33. return *this; \
  34. }
  35. #define vectorOperator(OPSIGN) \
  36. ResourceSet& operator OPSIGN ## =(const ResourceSet &rhs) \
  37. { \
  38. for(auto i = 0; i < container.size(); i++) \
  39. container.at(i) OPSIGN ## = rhs[i]; \
  40. \
  41. return *this; \
  42. }
  43. #define twoOperands(OPSIGN, RHS_TYPE) \
  44. friend ResourceSet operator OPSIGN(ResourceSet lhs, const RHS_TYPE &rhs) \
  45. { \
  46. lhs OPSIGN ## = rhs; \
  47. return lhs; \
  48. }
  49. scalarOperator(+)
  50. scalarOperator(-)
  51. scalarOperator(*)
  52. scalarOperator(/)
  53. vectorOperator(+)
  54. vectorOperator(-)
  55. twoOperands(+, TResource)
  56. twoOperands(-, TResource)
  57. twoOperands(*, TResource)
  58. twoOperands(/, TResource)
  59. twoOperands(+, ResourceSet)
  60. twoOperands(-, ResourceSet)
  61. #undef scalarOperator
  62. #undef vectorOperator
  63. #undef twoOperands
  64. using const_reference = decltype(container)::const_reference;
  65. using value_type = decltype(container)::value_type;
  66. using const_iterator = decltype(container)::const_iterator;
  67. using iterator = decltype(container)::iterator;
  68. // Array-like interface
  69. TResource & operator[](GameResID index)
  70. {
  71. return operator[](index.getNum());
  72. }
  73. const TResource & operator[](GameResID index) const
  74. {
  75. return operator[](index.getNum());
  76. }
  77. TResource & operator[](size_t index)
  78. {
  79. return container.at(index);
  80. }
  81. const TResource & operator[](size_t index) const
  82. {
  83. return container.at(index);
  84. }
  85. bool empty () const
  86. {
  87. for(const auto & res : *this)
  88. if(res)
  89. return false;
  90. return true;
  91. }
  92. // C++ range-based for support
  93. auto begin () -> decltype (container.begin())
  94. {
  95. return container.begin();
  96. }
  97. auto end () -> decltype (container.end())
  98. {
  99. return container.end();
  100. }
  101. auto begin () const -> decltype (container.cbegin())
  102. {
  103. return container.cbegin();
  104. }
  105. auto end () const -> decltype (container.cend())
  106. {
  107. return container.cend();
  108. }
  109. auto size () const -> decltype (container.size())
  110. {
  111. return container.size();
  112. }
  113. //to be used for calculations of type "how many units of sth can I afford?"
  114. int operator/(const ResourceSet &rhs)
  115. {
  116. int ret = INT_MAX;
  117. for(int i = 0; i < container.size(); i++)
  118. if(rhs[i])
  119. vstd::amin(ret, container.at(i) / rhs[i]);
  120. return ret;
  121. }
  122. //Returns how many items of "this" we can afford with provided income
  123. int maxPurchasableCount(const ResourceSet& income) {
  124. int ret = 0; // Initialize to 0 because we want the maximum number of accumulations
  125. for (size_t i = 0; i < container.size(); ++i) {
  126. if (container.at(i) > 0) { // We only care about fulfilling positive needs
  127. if (income[i] == 0) {
  128. // If income is 0 and we need a positive amount, it's impossible to fulfill
  129. return INT_MAX;
  130. }
  131. else {
  132. // Calculate the number of times we need to accumulate income to fulfill the need
  133. int ceiledResult = vstd::divideAndCeil(container.at(i), income[i]);
  134. ret = std::max(ret, ceiledResult);
  135. }
  136. }
  137. }
  138. return ret;
  139. }
  140. ResourceSet & operator=(const TResource &rhs)
  141. {
  142. for(int & i : container)
  143. i = rhs;
  144. return *this;
  145. }
  146. ResourceSet operator-() const
  147. {
  148. ResourceSet ret;
  149. for(int i = 0; i < container.size(); i++)
  150. ret[i] = -container.at(i);
  151. return ret;
  152. }
  153. bool operator==(const ResourceSet &rhs) const
  154. {
  155. return this->container == rhs.container;
  156. }
  157. template <typename Handler> void serialize(Handler &h)
  158. {
  159. h & container;
  160. }
  161. DLL_LINKAGE void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName);
  162. DLL_LINKAGE void amax(const TResourceCap &val); //performs vstd::amax on each element
  163. DLL_LINKAGE void amin(const TResourceCap &val); //performs vstd::amin on each element
  164. DLL_LINKAGE void positive(); //values below 0 are set to 0 - upgrade cost can't be negative, for example
  165. DLL_LINKAGE void applyHandicap(int percentage);
  166. DLL_LINKAGE bool nonZero() const; //returns true if at least one value is non-zero;
  167. DLL_LINKAGE bool canAfford(const ResourceSet &price) const;
  168. DLL_LINKAGE bool canBeAfforded(const ResourceSet &res) const;
  169. DLL_LINKAGE TResourceCap marketValue() const;
  170. DLL_LINKAGE std::string toString() const;
  171. //special iterator of iterating over non-zero resources in set
  172. class DLL_LINKAGE nziterator
  173. {
  174. struct ResEntry
  175. {
  176. GameResID resType;
  177. TResourceCap resVal;
  178. } cur;
  179. const ResourceSet &rs;
  180. void advance();
  181. public:
  182. nziterator(const ResourceSet &RS);
  183. bool valid() const;
  184. nziterator operator++();
  185. nziterator operator++(int);
  186. const ResEntry& operator*() const;
  187. const ResEntry* operator->() const;
  188. };
  189. };
  190. using TResources = ResourceSet;
  191. VCMI_LIB_NAMESPACE_END