ResourceSet.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. VCMI_LIB_NAMESPACE_BEGIN
  12. typedef si32 TResource;
  13. typedef si64 TResourceCap; //to avoid overflow when adding integers. Signed values are easier to control.
  14. class JsonNode;
  15. class JsonSerializeFormat;
  16. namespace Res
  17. {
  18. class ResourceSet;
  19. bool canAfford(const ResourceSet &res, const ResourceSet &price); //can a be used to pay price b
  20. enum ERes
  21. {
  22. WOOD = 0, MERCURY, ORE, SULFUR, CRYSTAL, GEMS, GOLD, MITHRIL,
  23. WOOD_AND_ORE = 127, // special case for town bonus resource
  24. INVALID = -1
  25. };
  26. //class to be representing a vector of resource
  27. class ResourceSet : public std::vector<int>
  28. {
  29. public:
  30. DLL_LINKAGE ResourceSet();
  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, TResource mercury, TResource ore, TResource sulfur, TResource crystal,
  34. TResource gems, TResource gold, TResource mithril = 0);
  35. #define scalarOperator(OPSIGN) \
  36. ResourceSet operator OPSIGN(const TResource &rhs) const \
  37. { \
  38. ResourceSet ret = *this; \
  39. for(int i = 0; i < (int)size(); i++) \
  40. ret[i] = at(i) OPSIGN rhs; \
  41. \
  42. return ret; \
  43. }
  44. #define vectorOperator(OPSIGN) \
  45. ResourceSet operator OPSIGN(const ResourceSet &rhs) const \
  46. { \
  47. ResourceSet ret = *this; \
  48. for(int i = 0; i < (int)size(); i++) \
  49. ret[i] = at(i) OPSIGN rhs[i]; \
  50. \
  51. return ret; \
  52. }
  53. #define opEqOperator(OPSIGN, RHS_TYPE) \
  54. ResourceSet& operator OPSIGN ## =(const RHS_TYPE &rhs) \
  55. { \
  56. return *this = *this OPSIGN rhs; \
  57. }
  58. scalarOperator(+)
  59. scalarOperator(-)
  60. scalarOperator(*)
  61. scalarOperator(/)
  62. opEqOperator(+, TResource)
  63. opEqOperator(-, TResource)
  64. opEqOperator(*, TResource)
  65. vectorOperator(+)
  66. vectorOperator(-)
  67. opEqOperator(+, ResourceSet)
  68. opEqOperator(-, ResourceSet)
  69. #undef scalarOperator
  70. #undef vectorOperator
  71. #undef opEqOperator
  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::vector<int>&>(*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. typedef Res::ResourceSet TResources;
  138. VCMI_LIB_NAMESPACE_END