ResourceSet.cpp 3.9 KB

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