2
0

ResourceSet.cpp 3.6 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 = 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. void ResourceSet::applyHandicap(int percentage)
  56. {
  57. for(auto & elem : *this)
  58. {
  59. int64_t newAmount = vstd::divideAndCeil(static_cast<int64_t>(elem) * percentage, 100);
  60. int64_t cap = GameConstants::PLAYER_RESOURCES_CAP;
  61. elem = std::min(cap, newAmount);
  62. }
  63. }
  64. static bool canAfford(const ResourceSet &res, const ResourceSet &price)
  65. {
  66. assert(res.size() == price.size() && price.size() == GameConstants::RESOURCE_QUANTITY);
  67. for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  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(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  84. total += static_cast<TResourceCap>(LIBRARY->resourceTypeHandler->getById(i)->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;
  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