ResourceSet.h 6.3 KB

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