ResourceSet.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. typedef si32 TResource;
  14. typedef si64 TResourceCap; //to avoid overflow when adding integers. Signed values are easier to control.
  15. class JsonNode;
  16. class JsonSerializeFormat;
  17. namespace Res
  18. {
  19. class ResourceSet;
  20. bool canAfford(const ResourceSet &res, const ResourceSet &price); //can a be used to pay price b
  21. enum ERes
  22. {
  23. WOOD = 0, MERCURY, ORE, SULFUR, CRYSTAL, GEMS, GOLD, MITHRIL,
  24. WOOD_AND_ORE = 127, // special case for town bonus resource
  25. INVALID = -1
  26. };
  27. //class to be representing a vector of resource
  28. class ResourceSet : public std::array<int, GameConstants::RESOURCE_QUANTITY>
  29. {
  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 < size(); i++) \
  39. at(i) OPSIGN ## = rhs; \
  40. \
  41. return *this; \
  42. }
  43. #define vectorOperator(OPSIGN) \
  44. ResourceSet& operator OPSIGN ## =(const ResourceSet &rhs) \
  45. { \
  46. for(int i = 0; i < (int)size(); i++) \
  47. 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. //to be used for calculations of type "how many units of sth can I afford?"
  73. int operator/(const ResourceSet &rhs)
  74. {
  75. int ret = INT_MAX;
  76. for(int i = 0; i < (int)size(); i++)
  77. if(rhs[i])
  78. vstd::amin(ret, at(i) / rhs[i]);
  79. return ret;
  80. }
  81. ResourceSet & operator=(const TResource &rhs)
  82. {
  83. for(int i = 0; i < (int)size(); i++)
  84. at(i) = rhs;
  85. return *this;
  86. }
  87. ResourceSet operator-() const
  88. {
  89. ResourceSet ret;
  90. for(int i = 0; i < (int)size(); i++)
  91. ret[i] = -at(i);
  92. return ret;
  93. }
  94. // WARNING: comparison operators are used for "can afford" relation: a <= b means that foreach i a[i] <= b[i]
  95. // 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
  96. // bool operator<(const ResourceSet &rhs)
  97. // {
  98. // for(int i = 0; i < size(); i++)
  99. // if(at(i) >= rhs[i])
  100. // return false;
  101. //
  102. // return true;
  103. // }
  104. template <typename Handler> void serialize(Handler &h, const int version)
  105. {
  106. h & static_cast<std::array<int, GameConstants::RESOURCE_QUANTITY>&>(*this);
  107. }
  108. DLL_LINKAGE void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName);
  109. DLL_LINKAGE void amax(const TResourceCap &val); //performs vstd::amax on each element
  110. DLL_LINKAGE void amin(const TResourceCap &val); //performs vstd::amin on each element
  111. DLL_LINKAGE void positive(); //values below 0 are set to 0 - upgrade cost can't be negative, for example
  112. DLL_LINKAGE bool nonZero() const; //returns true if at least one value is non-zero;
  113. DLL_LINKAGE bool canAfford(const ResourceSet &price) const;
  114. DLL_LINKAGE bool canBeAfforded(const ResourceSet &res) const;
  115. DLL_LINKAGE TResourceCap marketValue() const;
  116. DLL_LINKAGE std::string toString() const;
  117. //special iterator of iterating over non-zero resources in set
  118. class DLL_LINKAGE nziterator
  119. {
  120. struct ResEntry
  121. {
  122. Res::ERes resType;
  123. TResourceCap resVal;
  124. } cur;
  125. const ResourceSet &rs;
  126. void advance();
  127. public:
  128. nziterator(const ResourceSet &RS);
  129. bool valid() const;
  130. nziterator operator++();
  131. nziterator operator++(int);
  132. const ResEntry& operator*() const;
  133. const ResEntry* operator->() const;
  134. };
  135. };
  136. }
  137. using TResources = Res::ResourceSet;
  138. VCMI_LIB_NAMESPACE_END