ResourceSet.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #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. if(index >= container.size())
  84. logGlobal->error("Try to access resource which is not existing! Maybe new resources in mod not marked as modType=Resources?");
  85. return container.at(index);
  86. }
  87. bool empty () const
  88. {
  89. for(const auto & res : *this)
  90. if(res)
  91. return false;
  92. return true;
  93. }
  94. // C++ range-based for support
  95. auto begin () -> decltype (container.begin())
  96. {
  97. return container.begin();
  98. }
  99. auto end () -> decltype (container.end())
  100. {
  101. return container.end();
  102. }
  103. auto begin () const -> decltype (container.cbegin())
  104. {
  105. return container.cbegin();
  106. }
  107. auto end () const -> decltype (container.cend())
  108. {
  109. return container.cend();
  110. }
  111. auto size () const -> decltype (container.size())
  112. {
  113. return container.size();
  114. }
  115. //to be used for calculations of type "how many units of sth can I afford?"
  116. int operator/(const ResourceSet &rhs)
  117. {
  118. int ret = INT_MAX;
  119. for(int i = 0; i < container.size(); i++)
  120. if(rhs[i])
  121. vstd::amin(ret, container.at(i) / rhs[i]);
  122. return ret;
  123. }
  124. //Returns how many items of "this" we can afford with provided funds
  125. int maxPurchasableCount(const ResourceSet& availableFunds) {
  126. int ret = 0; // Initialize to 0 because we want the maximum number of accumulations
  127. for (size_t i = 0; i < container.size(); ++i) {
  128. if (container.at(i) > 0) { // We only care about fulfilling positive needs
  129. if (availableFunds[i] == 0) {
  130. // If income is 0 and we need a positive amount, it's impossible to fulfill
  131. return INT_MAX;
  132. }
  133. else {
  134. // Calculate the number of times we need to accumulate income to fulfill the need
  135. int ceiledResult = vstd::divideAndCeil(container.at(i), availableFunds[i]);
  136. ret = std::max(ret, ceiledResult);
  137. }
  138. }
  139. }
  140. return ret;
  141. }
  142. ResourceSet & operator=(const TResource &rhs)
  143. {
  144. for(int & i : container)
  145. i = rhs;
  146. return *this;
  147. }
  148. ResourceSet operator-() const
  149. {
  150. ResourceSet ret;
  151. for(int i = 0; i < container.size(); i++)
  152. ret[i] = -container.at(i);
  153. return ret;
  154. }
  155. bool operator==(const ResourceSet &rhs) const
  156. {
  157. return this->container == rhs.container;
  158. }
  159. template <typename Handler> void serialize(Handler &h)
  160. {
  161. h & container;
  162. }
  163. DLL_LINKAGE void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName);
  164. DLL_LINKAGE void amax(const TResourceCap &val); //performs vstd::amax on each element
  165. DLL_LINKAGE void amin(const TResourceCap &val); //performs vstd::amin on each element
  166. DLL_LINKAGE void positive(); //values below 0 are set to 0 - upgrade cost can't be negative, for example
  167. DLL_LINKAGE void applyHandicap(int percentage);
  168. DLL_LINKAGE bool nonZero() const; //returns true if at least one value is non-zero;
  169. DLL_LINKAGE bool canAfford(const ResourceSet &price) const;
  170. DLL_LINKAGE bool canBeAfforded(const ResourceSet &res) const;
  171. DLL_LINKAGE TResourceCap marketValue() const;
  172. DLL_LINKAGE std::string toString() const;
  173. //special iterator of iterating over non-zero resources in set
  174. class DLL_LINKAGE nziterator
  175. {
  176. struct ResEntry
  177. {
  178. GameResID resType;
  179. TResourceCap resVal;
  180. } cur;
  181. const ResourceSet &rs;
  182. void advance();
  183. public:
  184. nziterator(const ResourceSet &RS);
  185. bool valid() const;
  186. nziterator operator++();
  187. nziterator operator++(int);
  188. const ResEntry& operator*() const;
  189. const ResEntry* operator->() const;
  190. };
  191. };
  192. using TResources = ResourceSet;
  193. VCMI_LIB_NAMESPACE_END