ResourceSet.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "entities/ResourceTypeHandler.h"
  16. #include "GameLibrary.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. ResourceSet::ResourceSet() = default;
  19. ResourceSet::ResourceSet(const JsonNode & node)
  20. {
  21. for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
  22. container[i] = static_cast<int>(node[i.toResource()->getJsonKey()].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. for(auto & idx : LIBRARY->resourceTypeHandler->getAllObjects())
  30. {
  31. //TODO: add proper support for mithril to map format
  32. if(idx == EGameResID::MITHRIL)
  33. continue;
  34. handler.serializeInt(idx.toResource()->getJsonKey(), this->operator[](idx), 0);
  35. }
  36. }
  37. bool ResourceSet::nonZero() const
  38. {
  39. for(const auto & elem : *this)
  40. if(elem.second)
  41. return true;
  42. return false;
  43. }
  44. void ResourceSet::amax(const TResourceCap &val)
  45. {
  46. for(auto & elem : *this)
  47. vstd::amax(elem.second, val);
  48. }
  49. void ResourceSet::amin(const TResourceCap &val)
  50. {
  51. for(auto & elem : *this)
  52. vstd::amin(elem.second, val);
  53. }
  54. void ResourceSet::positive()
  55. {
  56. for(auto & elem : *this)
  57. vstd::amax(elem.second, 0);
  58. }
  59. void ResourceSet::applyHandicap(int percentage)
  60. {
  61. for(auto & elem : *this)
  62. {
  63. int64_t newAmount = vstd::divideAndCeil(static_cast<int64_t>(elem.second) * percentage, 100);
  64. int64_t cap = GameConstants::PLAYER_RESOURCES_CAP;
  65. elem.second = std::min(cap, newAmount);
  66. }
  67. }
  68. static bool canAfford(const ResourceSet &res, const ResourceSet &price)
  69. {
  70. for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
  71. if(price[i] > res[i])
  72. return false;
  73. return true;
  74. }
  75. bool ResourceSet::canBeAfforded(const ResourceSet &res) const
  76. {
  77. return VCMI_LIB_WRAP_NAMESPACE(canAfford(res, *this));
  78. }
  79. bool ResourceSet::canAfford(const ResourceSet &price) const
  80. {
  81. return VCMI_LIB_WRAP_NAMESPACE(canAfford(*this, price));
  82. }
  83. TResourceCap ResourceSet::marketValue() const
  84. {
  85. TResourceCap total = 0;
  86. for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
  87. total += static_cast<TResourceCap>(i.toResource()->getPrice()) * static_cast<TResourceCap>(operator[](i));
  88. return total;
  89. }
  90. std::string ResourceSet::toString() const
  91. {
  92. std::ostringstream out;
  93. out << "[";
  94. for(auto it = begin(); it != end(); ++it)
  95. {
  96. out << (*it).second;
  97. if(std::prev(end()) != it) out << ", ";
  98. }
  99. out << "]";
  100. return out.str();
  101. }
  102. bool ResourceSet::nziterator::valid() const
  103. {
  104. return cur.resType < GameResID::COUNT && cur.resVal;
  105. }
  106. ResourceSet::nziterator ResourceSet::nziterator::operator++()
  107. {
  108. advance();
  109. return *this;
  110. }
  111. ResourceSet::nziterator ResourceSet::nziterator::operator++(int)
  112. {
  113. nziterator ret = *this;
  114. advance();
  115. return ret;
  116. }
  117. const ResourceSet::nziterator::ResEntry& ResourceSet::nziterator::operator*() const
  118. {
  119. return cur;
  120. }
  121. const ResourceSet::nziterator::ResEntry * ResourceSet::nziterator::operator->() const
  122. {
  123. return &cur;
  124. }
  125. void ResourceSet::nziterator::advance()
  126. {
  127. do
  128. {
  129. ++cur.resType;
  130. } while(cur.resType < GameResID::COUNT && !(cur.resVal=rs[cur.resType]));
  131. if(cur.resType >= GameResID::COUNT)
  132. cur.resVal = -1;
  133. }
  134. ResourceSet::nziterator::nziterator(const ResourceSet &RS)
  135. : rs(RS)
  136. {
  137. cur.resType = EGameResID::WOOD;
  138. cur.resVal = rs[EGameResID::WOOD];
  139. if(!valid())
  140. advance();
  141. }
  142. VCMI_LIB_NAMESPACE_END