ResourceSet.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * ResourceSet.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ResourceSet.h"
  12. #include "StringConstants.h"
  13. #include "JsonNode.h"
  14. #include "serializer/JsonSerializeFormat.h"
  15. Res::ResourceSet::ResourceSet()
  16. {
  17. resize(GameConstants::RESOURCE_QUANTITY, 0);
  18. }
  19. Res::ResourceSet::ResourceSet(const JsonNode & node)
  20. {
  21. reserve(GameConstants::RESOURCE_QUANTITY);
  22. for(std::string name : GameConstants::RESOURCE_NAMES)
  23. push_back(node[name].Float());
  24. }
  25. void Res::ResourceSet::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  26. {
  27. if(!handler.saving)
  28. resize(GameConstants::RESOURCE_QUANTITY, 0);
  29. if(handler.saving && !nonZero())
  30. return;
  31. auto s = handler.enterStruct(fieldName);
  32. //TODO: add proper support for mithril to map format
  33. for(int idx = 0; idx < GameConstants::RESOURCE_QUANTITY - 1; idx ++)
  34. handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], this->operator[](idx), 0);
  35. }
  36. bool Res::ResourceSet::nonZero() const
  37. {
  38. for(auto & elem : *this)
  39. if(elem)
  40. return true;
  41. return false;
  42. }
  43. void Res::ResourceSet::amax(const TResourceCap &val)
  44. {
  45. for(auto & elem : *this)
  46. ::vstd::amax(elem, val);
  47. }
  48. void Res::ResourceSet::amin(const TResourceCap &val)
  49. {
  50. for(auto & elem : *this)
  51. ::vstd::amin(elem, val);
  52. }
  53. void Res::ResourceSet::positive()
  54. {
  55. for(auto & elem : *this)
  56. ::vstd::amax(elem, 0);
  57. }
  58. bool Res::ResourceSet::canBeAfforded(const ResourceSet &res) const
  59. {
  60. return Res::canAfford(res, *this);
  61. }
  62. bool Res::ResourceSet::canAfford(const ResourceSet &price) const
  63. {
  64. return Res::canAfford(*this, price);
  65. }
  66. bool Res::canAfford(const ResourceSet &res, const ResourceSet &price)
  67. {
  68. assert(res.size() == price.size() && price.size() == GameConstants::RESOURCE_QUANTITY);
  69. for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  70. if(price[i] > res[i])
  71. return false;
  72. return true;
  73. }
  74. std::string Res::ResourceSet::toString() const
  75. {
  76. std::ostringstream out;
  77. out << "[";
  78. for(auto it = begin(); it != end(); ++it)
  79. {
  80. out << *it;
  81. if(std::prev(end()) != it) out << ", ";
  82. }
  83. out << "]";
  84. return out.str();
  85. }
  86. bool Res::ResourceSet::nziterator::valid()
  87. {
  88. return cur.resType < GameConstants::RESOURCE_QUANTITY && cur.resVal;
  89. }
  90. Res::ResourceSet::nziterator Res::ResourceSet::nziterator::operator++()
  91. {
  92. advance();
  93. return *this;
  94. }
  95. Res::ResourceSet::nziterator Res::ResourceSet::nziterator::operator++(int)
  96. {
  97. nziterator ret = *this;
  98. advance();
  99. return ret;
  100. }
  101. const Res::ResourceSet::nziterator::ResEntry& Res::ResourceSet::nziterator::operator*() const
  102. {
  103. return cur;
  104. }
  105. const Res::ResourceSet::nziterator::ResEntry * Res::ResourceSet::nziterator::operator->() const
  106. {
  107. return &cur;
  108. }
  109. void Res::ResourceSet::nziterator::advance()
  110. {
  111. do
  112. {
  113. vstd::advance(cur.resType, +1);
  114. } while(cur.resType < GameConstants::RESOURCE_QUANTITY && !(cur.resVal=rs[cur.resType]));
  115. if(cur.resType >= GameConstants::RESOURCE_QUANTITY)
  116. cur.resVal = -1;
  117. }
  118. Res::ResourceSet::nziterator::nziterator(const ResourceSet &RS)
  119. : rs(RS)
  120. {
  121. cur.resType = WOOD;
  122. cur.resVal = rs[WOOD];
  123. if(!valid())
  124. advance();
  125. }