ResourceSet.cpp 4.1 KB

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