ResourceSet.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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()
  19. {
  20. resizeContainer();
  21. };
  22. ResourceSet::ResourceSet(const ResourceSet& rhs)
  23. : container(rhs.container) // vector copy constructor
  24. {
  25. resizeContainer();
  26. }
  27. void ResourceSet::resizeContainer()
  28. {
  29. container.resize(LIBRARY->resourceTypeHandler->getAllObjects().size());
  30. }
  31. void ResourceSet::resolveFromJson(const JsonNode & node)
  32. {
  33. for(auto & n : node.Struct())
  34. LIBRARY->identifiers()->requestIdentifier(n.second.getModScope(), "resource", n.first, [n, this](int32_t identifier)
  35. {
  36. (*this)[identifier] = static_cast<int>(n.second.Float());
  37. });
  38. }
  39. void ResourceSet::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  40. {
  41. if(handler.saving && !nonZero())
  42. return;
  43. auto s = handler.enterStruct(fieldName);
  44. for(auto & idx : LIBRARY->resourceTypeHandler->getAllObjects())
  45. handler.serializeInt(idx.toResource()->getJsonKey(), this->operator[](idx), 0);
  46. }
  47. bool ResourceSet::nonZero() const
  48. {
  49. for(const auto & elem : *this)
  50. if(elem)
  51. return true;
  52. return false;
  53. }
  54. void ResourceSet::amax(const TResourceCap &val)
  55. {
  56. for(auto & elem : *this)
  57. vstd::amax(elem, val);
  58. }
  59. void ResourceSet::amin(const TResourceCap &val)
  60. {
  61. for(auto & elem : *this)
  62. vstd::amin(elem, val);
  63. }
  64. void ResourceSet::positive()
  65. {
  66. for(auto & elem : *this)
  67. vstd::amax(elem, 0);
  68. }
  69. void ResourceSet::applyHandicap(int percentage)
  70. {
  71. for(auto & elem : *this)
  72. {
  73. int64_t newAmount = vstd::divideAndCeil(static_cast<int64_t>(elem) * percentage, 100);
  74. int64_t cap = GameConstants::PLAYER_RESOURCES_CAP;
  75. elem = std::min(cap, newAmount);
  76. }
  77. }
  78. static bool canAfford(const ResourceSet &res, const ResourceSet &price)
  79. {
  80. for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
  81. if(price[i] > res[i])
  82. return false;
  83. return true;
  84. }
  85. bool ResourceSet::canBeAfforded(const ResourceSet &res) const
  86. {
  87. return VCMI_LIB_WRAP_NAMESPACE(canAfford(res, *this));
  88. }
  89. bool ResourceSet::canAfford(const ResourceSet &price) const
  90. {
  91. return VCMI_LIB_WRAP_NAMESPACE(canAfford(*this, price));
  92. }
  93. TResourceCap ResourceSet::marketValue() const
  94. {
  95. TResourceCap total = 0;
  96. for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
  97. total += static_cast<TResourceCap>(i.toResource()->getPrice()) * static_cast<TResourceCap>(operator[](i));
  98. return total;
  99. }
  100. std::string ResourceSet::toString() const
  101. {
  102. std::ostringstream out;
  103. out << "[";
  104. for(auto it = begin(); it != end(); ++it)
  105. {
  106. out << *it;
  107. if(std::prev(end()) != it) out << ", ";
  108. }
  109. out << "]";
  110. return out.str();
  111. }
  112. bool ResourceSet::nziterator::valid() const
  113. {
  114. return static_cast<int>(cur.resType) < LIBRARY->resourceTypeHandler->getAllObjects().size() && cur.resVal;
  115. }
  116. ResourceSet::nziterator ResourceSet::nziterator::operator++()
  117. {
  118. advance();
  119. return *this;
  120. }
  121. ResourceSet::nziterator ResourceSet::nziterator::operator++(int)
  122. {
  123. nziterator ret = *this;
  124. advance();
  125. return ret;
  126. }
  127. const ResourceSet::nziterator::ResEntry& ResourceSet::nziterator::operator*() const
  128. {
  129. return cur;
  130. }
  131. const ResourceSet::nziterator::ResEntry * ResourceSet::nziterator::operator->() const
  132. {
  133. return &cur;
  134. }
  135. void ResourceSet::nziterator::advance()
  136. {
  137. do
  138. {
  139. ++cur.resType;
  140. } while(static_cast<int>(cur.resType) < LIBRARY->resourceTypeHandler->getAllObjects().size() && !(cur.resVal=rs[cur.resType]));
  141. if(static_cast<int>(cur.resType) >= LIBRARY->resourceTypeHandler->getAllObjects().size())
  142. cur.resVal = -1;
  143. }
  144. ResourceSet::nziterator::nziterator(const ResourceSet &RS)
  145. : rs(RS)
  146. {
  147. cur.resType = EGameResID::WOOD;
  148. cur.resVal = rs[EGameResID::WOOD];
  149. if(!valid())
  150. advance();
  151. }
  152. VCMI_LIB_NAMESPACE_END