ResourceSet.cpp 3.4 KB

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