ResourceSet.h 4.3 KB

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