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