ResourceSet.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "StdInc.h"
  2. #include "ResourceSet.h"
  3. #include "GameConstants.h"
  4. #include "JsonNode.h"
  5. Res::ResourceSet::ResourceSet()
  6. {
  7. resize(GameConstants::RESOURCE_QUANTITY, 0);
  8. }
  9. Res::ResourceSet::ResourceSet(const JsonNode & node)
  10. {
  11. resize(GameConstants::RESOURCE_QUANTITY, 0);
  12. at(0) = node["wood"].Float();
  13. at(1) = node["mercury"].Float();
  14. at(2) = node["ore"].Float();
  15. at(3) = node["sulfur"].Float();
  16. at(4) = node["crystal"].Float();
  17. at(5) = node["gems"].Float();
  18. at(6) = node["gold"].Float();
  19. at(7) = node["mithril"].Float();
  20. }
  21. bool Res::ResourceSet::nonZero() const
  22. {
  23. for(int i = 0; i < size(); i++)
  24. if(at(i))
  25. return true;
  26. return false;
  27. }
  28. void Res::ResourceSet::amax(const TResourceCap &val)
  29. {
  30. for(int i = 0; i < size(); i++)
  31. ::vstd::amax(at(i), val);
  32. }
  33. bool Res::ResourceSet::canBeAfforded(const ResourceSet &res) const
  34. {
  35. return Res::canAfford(res, *this);
  36. }
  37. bool Res::ResourceSet::canAfford(const ResourceSet &price) const
  38. {
  39. return Res::canAfford(*this, price);
  40. }
  41. bool Res::canAfford(const ResourceSet &res, const ResourceSet &price)
  42. {
  43. assert(res.size() == price.size() && price.size() == GameConstants::RESOURCE_QUANTITY);
  44. for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  45. if(price[i] > res[i])
  46. return false;
  47. return true;
  48. }
  49. bool Res::ResourceSet::nziterator::valid()
  50. {
  51. return cur.resType < GameConstants::RESOURCE_QUANTITY && cur.resVal;
  52. }
  53. Res::ResourceSet::nziterator Res::ResourceSet::nziterator::operator++()
  54. {
  55. advance();
  56. return *this;
  57. }
  58. Res::ResourceSet::nziterator Res::ResourceSet::nziterator::operator++(int)
  59. {
  60. nziterator ret = *this;
  61. advance();
  62. return ret;
  63. }
  64. const Res::ResourceSet::nziterator::ResEntry& Res::ResourceSet::nziterator::operator*() const
  65. {
  66. return cur;
  67. }
  68. const Res::ResourceSet::nziterator::ResEntry * Res::ResourceSet::nziterator::operator->() const
  69. {
  70. return &cur;
  71. }
  72. void Res::ResourceSet::nziterator::advance()
  73. {
  74. do
  75. {
  76. cur.resType++;
  77. } while(cur.resType < GameConstants::RESOURCE_QUANTITY && !(cur.resVal=rs[cur.resType]));
  78. if(cur.resType >= GameConstants::RESOURCE_QUANTITY)
  79. cur.resVal = -1;
  80. }
  81. Res::ResourceSet::nziterator::nziterator(const ResourceSet &RS)
  82. : rs(RS)
  83. {
  84. cur.resType = 0;
  85. cur.resVal = rs[0];
  86. if(!valid())
  87. advance();
  88. }