ResourceSet.h 3.7 KB

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