ResourceSet.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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(TResource wood = 0, TResource mercury = 0, TResource ore = 0, TResource sulfur = 0, TResource crystal = 0,
  27. TResource gems = 0, TResource gold = 0, TResource mithril = 0);
  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. return container.at(index);
  85. }
  86. bool empty () const
  87. {
  88. for(const auto & res : *this)
  89. if(res)
  90. return false;
  91. return true;
  92. }
  93. // C++ range-based for support
  94. auto begin () -> decltype (container.begin())
  95. {
  96. return container.begin();
  97. }
  98. auto end () -> decltype (container.end())
  99. {
  100. return container.end();
  101. }
  102. auto begin () const -> decltype (container.cbegin())
  103. {
  104. return container.cbegin();
  105. }
  106. auto end () const -> decltype (container.cend())
  107. {
  108. return container.cend();
  109. }
  110. auto size () const -> decltype (container.size())
  111. {
  112. return container.size();
  113. }
  114. //to be used for calculations of type "how many units of sth can I afford?"
  115. int operator/(const ResourceSet &rhs)
  116. {
  117. int ret = INT_MAX;
  118. for(int i = 0; i < container.size(); i++)
  119. if(rhs[i])
  120. vstd::amin(ret, container.at(i) / rhs[i]);
  121. return ret;
  122. }
  123. ResourceSet & operator=(const TResource &rhs)
  124. {
  125. for(int & i : container)
  126. i = rhs;
  127. return *this;
  128. }
  129. ResourceSet operator-() const
  130. {
  131. ResourceSet ret;
  132. for(int i = 0; i < container.size(); i++)
  133. ret[i] = -container.at(i);
  134. return ret;
  135. }
  136. bool operator==(const ResourceSet &rhs) const
  137. {
  138. return this->container == rhs.container;
  139. }
  140. // WARNING: comparison operators are used for "can afford" relation: a <= b means that foreach i a[i] <= b[i]
  141. // 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
  142. // bool operator<(const ResourceSet &rhs)
  143. // {
  144. // for(int i = 0; i < size(); i++)
  145. // if(at(i) >= rhs[i])
  146. // return false;
  147. //
  148. // return true;
  149. // }
  150. template <typename Handler> void serialize(Handler &h, const int version)
  151. {
  152. h & container;
  153. }
  154. DLL_LINKAGE void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName);
  155. DLL_LINKAGE void amax(const TResourceCap &val); //performs vstd::amax on each element
  156. DLL_LINKAGE void amin(const TResourceCap &val); //performs vstd::amin on each element
  157. DLL_LINKAGE void positive(); //values below 0 are set to 0 - upgrade cost can't be negative, for example
  158. DLL_LINKAGE bool nonZero() const; //returns true if at least one value is non-zero;
  159. DLL_LINKAGE bool canAfford(const ResourceSet &price) const;
  160. DLL_LINKAGE bool canBeAfforded(const ResourceSet &res) const;
  161. DLL_LINKAGE TResourceCap marketValue() const;
  162. DLL_LINKAGE std::string toString() const;
  163. //special iterator of iterating over non-zero resources in set
  164. class DLL_LINKAGE nziterator
  165. {
  166. struct ResEntry
  167. {
  168. GameResID resType;
  169. TResourceCap resVal;
  170. } cur;
  171. const ResourceSet &rs;
  172. void advance();
  173. public:
  174. nziterator(const ResourceSet &RS);
  175. bool valid() const;
  176. nziterator operator++();
  177. nziterator operator++(int);
  178. const ResEntry& operator*() const;
  179. const ResEntry* operator->() const;
  180. };
  181. };
  182. using TResources = ResourceSet;
  183. VCMI_LIB_NAMESPACE_END