ResourceSet.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. enum class EGameResID : int8_t
  19. {
  20. WOOD = 0, MERCURY, ORE, SULFUR, CRYSTAL, GEMS, GOLD, MITHRIL,
  21. WOOD_AND_ORE = 127, // special case for town bonus resource
  22. INVALID = -1
  23. };
  24. using GameResID = Identifier<EGameResID>;
  25. //class to be representing a vector of resource
  26. class ResourceSet
  27. {
  28. private:
  29. std::array<TResource, GameConstants::RESOURCE_QUANTITY> container;
  30. public:
  31. // read resources set from json. Format example: { "gold": 500, "wood":5 }
  32. DLL_LINKAGE ResourceSet(const JsonNode & node);
  33. DLL_LINKAGE ResourceSet(TResource wood = 0, TResource mercury = 0, TResource ore = 0, TResource sulfur = 0, TResource crystal = 0,
  34. TResource gems = 0, TResource gold = 0, TResource mithril = 0);
  35. #define scalarOperator(OPSIGN) \
  36. ResourceSet& operator OPSIGN ## =(const TResource &rhs) \
  37. { \
  38. for(auto i = 0; i < container.size(); i++) \
  39. container.at(i) OPSIGN ## = rhs; \
  40. \
  41. return *this; \
  42. }
  43. #define vectorOperator(OPSIGN) \
  44. ResourceSet& operator OPSIGN ## =(const ResourceSet &rhs) \
  45. { \
  46. for(auto i = 0; i < container.size(); i++) \
  47. container.at(i) OPSIGN ## = rhs[i]; \
  48. \
  49. return *this; \
  50. }
  51. #define twoOperands(OPSIGN, RHS_TYPE) \
  52. friend ResourceSet operator OPSIGN(ResourceSet lhs, const RHS_TYPE &rhs) \
  53. { \
  54. lhs OPSIGN ## = rhs; \
  55. return lhs; \
  56. }
  57. scalarOperator(+)
  58. scalarOperator(-)
  59. scalarOperator(*)
  60. scalarOperator(/)
  61. vectorOperator(+)
  62. vectorOperator(-)
  63. twoOperands(+, TResource)
  64. twoOperands(-, TResource)
  65. twoOperands(*, TResource)
  66. twoOperands(/, TResource)
  67. twoOperands(+, ResourceSet)
  68. twoOperands(-, ResourceSet)
  69. #undef scalarOperator
  70. #undef vectorOperator
  71. #undef twoOperands
  72. using const_reference = decltype(container)::const_reference;
  73. using value_type = decltype(container)::value_type;
  74. using const_iterator = decltype(container)::const_iterator;
  75. using iterator = decltype(container)::iterator;
  76. // Array-like interface
  77. TResource & operator[](GameResID index)
  78. {
  79. return operator[](index.getNum());
  80. }
  81. const TResource & operator[](GameResID index) const
  82. {
  83. return operator[](index.getNum());
  84. }
  85. TResource & operator[](size_t index)
  86. {
  87. return container.at(index);
  88. }
  89. const TResource & operator[](size_t index) const
  90. {
  91. return container.at(index);
  92. }
  93. bool empty () const
  94. {
  95. for(const auto & res : *this)
  96. if(res)
  97. return false;
  98. return true;
  99. }
  100. // C++ range-based for support
  101. auto begin () -> decltype (container.begin())
  102. {
  103. return container.begin();
  104. }
  105. auto end () -> decltype (container.end())
  106. {
  107. return container.end();
  108. }
  109. auto begin () const -> decltype (container.cbegin())
  110. {
  111. return container.cbegin();
  112. }
  113. auto end () const -> decltype (container.cend())
  114. {
  115. return container.cend();
  116. }
  117. auto size () const -> decltype (container.size())
  118. {
  119. return container.size();
  120. }
  121. //to be used for calculations of type "how many units of sth can I afford?"
  122. int operator/(const ResourceSet &rhs)
  123. {
  124. int ret = INT_MAX;
  125. for(int i = 0; i < container.size(); i++)
  126. if(rhs[i])
  127. vstd::amin(ret, container.at(i) / rhs[i]);
  128. return ret;
  129. }
  130. ResourceSet & operator=(const TResource &rhs)
  131. {
  132. for(int i = 0; i < container.size(); i++)
  133. container.at(i) = rhs;
  134. return *this;
  135. }
  136. ResourceSet operator-() const
  137. {
  138. ResourceSet ret;
  139. for(int i = 0; i < container.size(); i++)
  140. ret[i] = -container.at(i);
  141. return ret;
  142. }
  143. bool operator==(const ResourceSet &rhs) const
  144. {
  145. return this->container == rhs.container;
  146. }
  147. // WARNING: comparison operators are used for "can afford" relation: a <= b means that foreach i a[i] <= b[i]
  148. // 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
  149. // bool operator<(const ResourceSet &rhs)
  150. // {
  151. // for(int i = 0; i < size(); i++)
  152. // if(at(i) >= rhs[i])
  153. // return false;
  154. //
  155. // return true;
  156. // }
  157. template <typename Handler> void serialize(Handler &h, const int version)
  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 bool nonZero() const; //returns true if at least one value is non-zero;
  166. DLL_LINKAGE bool canAfford(const ResourceSet &price) const;
  167. DLL_LINKAGE bool canBeAfforded(const ResourceSet &res) const;
  168. DLL_LINKAGE TResourceCap marketValue() const;
  169. DLL_LINKAGE std::string toString() const;
  170. //special iterator of iterating over non-zero resources in set
  171. class DLL_LINKAGE nziterator
  172. {
  173. struct ResEntry
  174. {
  175. GameResID resType;
  176. TResourceCap resVal;
  177. } cur;
  178. const ResourceSet &rs;
  179. void advance();
  180. public:
  181. nziterator(const ResourceSet &RS);
  182. bool valid() const;
  183. nziterator operator++();
  184. nziterator operator++(int);
  185. const ResEntry& operator*() const;
  186. const ResEntry* operator->() const;
  187. };
  188. };
  189. using TResources = ResourceSet;
  190. VCMI_LIB_NAMESPACE_END