2
0

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