ResourceSet.h 3.4 KB

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