ResourceSet.h 3.1 KB

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