ResourceSet.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "JsonNode.h"
  15. #include "serializer/JsonSerializeFormat.h"
  16. #include "mapObjects/CObjectHandler.h"
  17. #include "VCMI_Lib.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  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. ResourceSet::ResourceSet(TResource wood, TResource mercury, TResource ore, TResource sulfur, TResource crystal,
  25. TResource gems, TResource gold, TResource mithril)
  26. {
  27. container[GameResID(EGameResID::WOOD)] = wood;
  28. container[GameResID(EGameResID::MERCURY)] = mercury;
  29. container[GameResID(EGameResID::ORE)] = ore;
  30. container[GameResID(EGameResID::SULFUR)] = sulfur;
  31. container[GameResID(EGameResID::CRYSTAL)] = crystal;
  32. container[GameResID(EGameResID::GEMS)] = gems;
  33. container[GameResID(EGameResID::GOLD)] = gold;
  34. container[GameResID(EGameResID::MITHRIL)] = mithril;
  35. }
  36. void ResourceSet::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
  37. {
  38. if(handler.saving && !nonZero())
  39. return;
  40. auto s = handler.enterStruct(fieldName);
  41. //TODO: add proper support for mithril to map format
  42. for(int idx = 0; idx < GameConstants::RESOURCE_QUANTITY - 1; idx ++)
  43. handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], this->operator[](idx), 0);
  44. }
  45. bool ResourceSet::nonZero() const
  46. {
  47. for(const auto & elem : *this)
  48. if(elem)
  49. return true;
  50. return false;
  51. }
  52. void ResourceSet::amax(const TResourceCap &val)
  53. {
  54. for(auto & elem : *this)
  55. vstd::amax(elem, val);
  56. }
  57. void ResourceSet::amin(const TResourceCap &val)
  58. {
  59. for(auto & elem : *this)
  60. vstd::amin(elem, val);
  61. }
  62. void ResourceSet::positive()
  63. {
  64. for(auto & elem : *this)
  65. vstd::amax(elem, 0);
  66. }
  67. static bool canAfford(const ResourceSet &res, const ResourceSet &price)
  68. {
  69. assert(res.size() == price.size() && price.size() == GameConstants::RESOURCE_QUANTITY);
  70. for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  71. if(price[i] > res[i])
  72. return false;
  73. return true;
  74. }
  75. bool ResourceSet::canBeAfforded(const ResourceSet &res) const
  76. {
  77. return VCMI_LIB_WRAP_NAMESPACE(canAfford(res, *this));
  78. }
  79. bool ResourceSet::canAfford(const ResourceSet &price) const
  80. {
  81. return VCMI_LIB_WRAP_NAMESPACE(canAfford(*this, price));
  82. }
  83. TResourceCap ResourceSet::marketValue() const
  84. {
  85. TResourceCap total = 0;
  86. for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  87. total += static_cast<TResourceCap>(VLC->objh->resVals[i]) * static_cast<TResourceCap>(operator[](i));
  88. return total;
  89. }
  90. std::string ResourceSet::toString() const
  91. {
  92. std::ostringstream out;
  93. out << "[";
  94. for(auto it = begin(); it != end(); ++it)
  95. {
  96. out << *it;
  97. if(std::prev(end()) != it) out << ", ";
  98. }
  99. out << "]";
  100. return out.str();
  101. }
  102. bool ResourceSet::nziterator::valid() const
  103. {
  104. return cur.resType < GameResID::COUNT && cur.resVal;
  105. }
  106. ResourceSet::nziterator ResourceSet::nziterator::operator++()
  107. {
  108. advance();
  109. return *this;
  110. }
  111. ResourceSet::nziterator ResourceSet::nziterator::operator++(int)
  112. {
  113. nziterator ret = *this;
  114. advance();
  115. return ret;
  116. }
  117. const ResourceSet::nziterator::ResEntry& ResourceSet::nziterator::operator*() const
  118. {
  119. return cur;
  120. }
  121. const ResourceSet::nziterator::ResEntry * ResourceSet::nziterator::operator->() const
  122. {
  123. return &cur;
  124. }
  125. void ResourceSet::nziterator::advance()
  126. {
  127. do
  128. {
  129. ++cur.resType;
  130. } while(cur.resType < GameResID::COUNT && !(cur.resVal=rs[cur.resType]));
  131. if(cur.resType >= GameResID::COUNT)
  132. cur.resVal = -1;
  133. }
  134. ResourceSet::nziterator::nziterator(const ResourceSet &RS)
  135. : rs(RS)
  136. {
  137. cur.resType = EGameResID::WOOD;
  138. cur.resVal = rs[EGameResID::WOOD];
  139. if(!valid())
  140. advance();
  141. }
  142. VCMI_LIB_NAMESPACE_END