ResourceSet.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include "../global.h"
  3. #include <climits>
  4. typedef si32 TResource;
  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_EXPORT ResourceSet();
  18. #define scalarOperator(OPSIGN) \
  19. DLL_EXPORT 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_EXPORT 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_EXPORT 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_EXPORT int operator/(const ResourceSet &rhs)
  57. {
  58. int ret = INT_MAX;
  59. for(int i = 0; i < size(); i++)
  60. if(rhs[i])
  61. amin(ret, at(i) / rhs[i]);
  62. return ret;
  63. }
  64. // WARNING: comparison operators are used for "can afford" relation: a <= b means that foreach i a[i] <= b[i]
  65. // 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
  66. // bool operator<(const ResourceSet &rhs)
  67. // {
  68. // for(int i = 0; i < size(); i++)
  69. // if(at(i) >= rhs[i])
  70. // return false;
  71. //
  72. // return true;
  73. // }
  74. template <typename Handler> void serialize(Handler &h, const int version)
  75. {
  76. h & static_cast<std::vector<int>&>(*this);
  77. }
  78. DLL_EXPORT void amax(const TResource &val); //performs amax on each element
  79. DLL_EXPORT bool nonZero() const; //returns true if at least one value is non-zero;
  80. DLL_EXPORT bool canAfford(const ResourceSet &price) const;
  81. DLL_EXPORT bool canBeAfforded(const ResourceSet &res) const;
  82. //special iterator of iterating over non-zero resources in set
  83. class DLL_EXPORT nziterator
  84. {
  85. struct ResEntry
  86. {
  87. TResource resType, resVal;
  88. } cur;
  89. const ResourceSet &rs;
  90. void advance();
  91. public:
  92. nziterator(const ResourceSet &RS);
  93. bool valid();
  94. nziterator operator++();
  95. nziterator operator++(int);
  96. const ResEntry& operator*() const;
  97. const ResEntry* operator->() const;
  98. };
  99. };
  100. }
  101. typedef Res::ResourceSet TResources;