ResourceSet.h 5.8 KB

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