ResourceSet.h 4.5 KB

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