ResourceSet.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "GameConstants.h"
  12. #include "ResourceSet.h"
  13. #include "constants/StringConstants.h"
  14. #include "JsonNode.h"
  15. #include "serializer/JsonSerializeFormat.h"
  16. #include "mapObjects/CObjectHandler.h"
  17. #include "VCMI_Lib.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. ResourceSet::ResourceSet() = default;
  20. ResourceSet::ResourceSet(const JsonNode & node)
  21. {
  22. for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  23. container[i] = static_cast<int>(node[GameConstants::RESOURCE_NAMES[i]].Float());
  24. }
  25. void ResourceSet::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  26. {
  27. if(handler.saving && !nonZero())
  28. return;
  29. auto s = handler.enterStruct(fieldName);
  30. //TODO: add proper support for mithril to map format
  31. for(int idx = 0; idx < GameConstants::RESOURCE_QUANTITY - 1; idx ++)
  32. handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], this->operator[](idx), 0);
  33. }
  34. bool ResourceSet::nonZero() const
  35. {
  36. for(const auto & elem : *this)
  37. if(elem)
  38. return true;
  39. return false;
  40. }
  41. void ResourceSet::amax(const TResourceCap &val)
  42. {
  43. for(auto & elem : *this)
  44. vstd::amax(elem, val);
  45. }
  46. void ResourceSet::amin(const TResourceCap &val)
  47. {
  48. for(auto & elem : *this)
  49. vstd::amin(elem, val);
  50. }
  51. void ResourceSet::positive()
  52. {
  53. for(auto & elem : *this)
  54. vstd::amax(elem, 0);
  55. }
  56. static bool canAfford(const ResourceSet &res, const ResourceSet &price)
  57. {
  58. assert(res.size() == price.size() && price.size() == GameConstants::RESOURCE_QUANTITY);
  59. for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  60. if(price[i] > res[i])
  61. return false;
  62. return true;
  63. }
  64. bool ResourceSet::canBeAfforded(const ResourceSet &res) const
  65. {
  66. return VCMI_LIB_WRAP_NAMESPACE(canAfford(res, *this));
  67. }
  68. bool ResourceSet::canAfford(const ResourceSet &price) const
  69. {
  70. return VCMI_LIB_WRAP_NAMESPACE(canAfford(*this, price));
  71. }
  72. TResourceCap ResourceSet::marketValue() const
  73. {
  74. TResourceCap total = 0;
  75. for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  76. total += static_cast<TResourceCap>(VLC->objh->resVals[i]) * static_cast<TResourceCap>(operator[](i));
  77. return total;
  78. }
  79. std::string ResourceSet::toString() const
  80. {
  81. std::ostringstream out;
  82. out << "[";
  83. for(auto it = begin(); it != end(); ++it)
  84. {
  85. out << *it;
  86. if(std::prev(end()) != it) out << ", ";
  87. }
  88. out << "]";
  89. return out.str();
  90. }
  91. bool ResourceSet::nziterator::valid() const
  92. {
  93. return cur.resType < GameResID::COUNT && cur.resVal;
  94. }
  95. ResourceSet::nziterator ResourceSet::nziterator::operator++()
  96. {
  97. advance();
  98. return *this;
  99. }
  100. ResourceSet::nziterator ResourceSet::nziterator::operator++(int)
  101. {
  102. nziterator ret = *this;
  103. advance();
  104. return ret;
  105. }
  106. const ResourceSet::nziterator::ResEntry& ResourceSet::nziterator::operator*() const
  107. {
  108. return cur;
  109. }
  110. const ResourceSet::nziterator::ResEntry * ResourceSet::nziterator::operator->() const
  111. {
  112. return &cur;
  113. }
  114. void ResourceSet::nziterator::advance()
  115. {
  116. do
  117. {
  118. ++cur.resType;
  119. } while(cur.resType < GameResID::COUNT && !(cur.resVal=rs[cur.resType]));
  120. if(cur.resType >= GameResID::COUNT)
  121. cur.resVal = -1;
  122. }
  123. ResourceSet::nziterator::nziterator(const ResourceSet &RS)
  124. : rs(RS)
  125. {
  126. cur.resType = EGameResID::WOOD;
  127. cur.resVal = rs[EGameResID::WOOD];
  128. if(!valid())
  129. advance();
  130. }
  131. VCMI_LIB_NAMESPACE_END