ResourceSet.cpp 3.5 KB

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