ResourceSet.h 3.5 KB

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