ResourceSet.cpp 4.0 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 "StringConstants.h"
  14. #include "JsonNode.h"
  15. #include "serializer/JsonSerializeFormat.h"
  16. #include "VCMI_Lib.h"
  17. #include "mapObjects/CObjectHandler.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. Res::ResourceSet::ResourceSet(const JsonNode & node)
  20. {
  21. for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  22. this[i] = static_cast<int>(node[GameConstants::RESOURCE_NAMES[i]].Float());
  23. }
  24. Res::ResourceSet::ResourceSet(TResource wood, TResource mercury, TResource ore, TResource sulfur, TResource crystal,
  25. TResource gems, TResource gold, TResource mithril)
  26. {
  27. this[Res::WOOD] = wood;
  28. this[Res::MERCURY] = mercury;
  29. this[Res::ORE] = ore;
  30. this[Res::SULFUR] = sulfur;
  31. this[Res::CRYSTAL] = crystal;
  32. this[Res::GEMS] = gems;
  33. this[Res::GOLD] = gold;
  34. this[Res::MITHRIL] = mithril;
  35. }
  36. void Res::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 Res::ResourceSet::nonZero() const
  46. {
  47. for(const auto & elem : *this)
  48. if(elem)
  49. return true;
  50. return false;
  51. }
  52. void Res::ResourceSet::amax(const TResourceCap &val)
  53. {
  54. for(auto & elem : *this)
  55. vstd::amax(elem, val);
  56. }
  57. void Res::ResourceSet::amin(const TResourceCap &val)
  58. {
  59. for(auto & elem : *this)
  60. vstd::amin(elem, val);
  61. }
  62. void Res::ResourceSet::positive()
  63. {
  64. for(auto & elem : *this)
  65. vstd::amax(elem, 0);
  66. }
  67. bool Res::ResourceSet::canBeAfforded(const ResourceSet &res) const
  68. {
  69. return Res::canAfford(res, *this);
  70. }
  71. bool Res::ResourceSet::canAfford(const ResourceSet &price) const
  72. {
  73. return Res::canAfford(*this, price);
  74. }
  75. bool Res::canAfford(const ResourceSet &res, const ResourceSet &price)
  76. {
  77. assert(res.size() == price.size() && price.size() == GameConstants::RESOURCE_QUANTITY);
  78. for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
  79. if(price[i] > res[i])
  80. return false;
  81. return true;
  82. }
  83. TResourceCap Res::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 Res::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 Res::ResourceSet::nziterator::valid() const
  103. {
  104. return cur.resType < GameConstants::RESOURCE_QUANTITY && cur.resVal;
  105. }
  106. Res::ResourceSet::nziterator Res::ResourceSet::nziterator::operator++()
  107. {
  108. advance();
  109. return *this;
  110. }
  111. Res::ResourceSet::nziterator Res::ResourceSet::nziterator::operator++(int)
  112. {
  113. nziterator ret = *this;
  114. advance();
  115. return ret;
  116. }
  117. const Res::ResourceSet::nziterator::ResEntry& Res::ResourceSet::nziterator::operator*() const
  118. {
  119. return cur;
  120. }
  121. const Res::ResourceSet::nziterator::ResEntry * Res::ResourceSet::nziterator::operator->() const
  122. {
  123. return &cur;
  124. }
  125. void Res::ResourceSet::nziterator::advance()
  126. {
  127. do
  128. {
  129. vstd::advance(cur.resType, +1);
  130. } while(cur.resType < GameConstants::RESOURCE_QUANTITY && !(cur.resVal=rs[cur.resType]));
  131. if(cur.resType >= GameConstants::RESOURCE_QUANTITY)
  132. cur.resVal = -1;
  133. }
  134. Res::ResourceSet::nziterator::nziterator(const ResourceSet &RS)
  135. : rs(RS)
  136. {
  137. cur.resType = WOOD;
  138. cur.resVal = rs[WOOD];
  139. if(!valid())
  140. advance();
  141. }
  142. VCMI_LIB_NAMESPACE_END