CPlayerState.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * CPlayerState.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 "CPlayerState.h"
  12. #include "json/JsonNode.h"
  13. #include "mapObjects/CGDwelling.h"
  14. #include "mapObjects/CGTownInstance.h"
  15. #include "mapObjects/CGHeroInstance.h"
  16. #include "gameState/QuestInfo.h"
  17. #include "texts/CGeneralTextHandler.h"
  18. #include "VCMI_Lib.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. PlayerState::PlayerState()
  21. : color(-1)
  22. , human(false)
  23. , cheated(false)
  24. , playerLocalSettings(std::make_unique<JsonNode>())
  25. , enteredWinningCheatCode(false)
  26. , enteredLosingCheatCode(false)
  27. , status(EPlayerStatus::INGAME)
  28. {
  29. setNodeType(PLAYER);
  30. }
  31. PlayerState::~PlayerState() = default;
  32. std::string PlayerState::nodeName() const
  33. {
  34. return "Player " + color.toString();
  35. }
  36. PlayerColor PlayerState::getId() const
  37. {
  38. return color;
  39. }
  40. int32_t PlayerState::getIndex() const
  41. {
  42. return color.getNum();
  43. }
  44. int32_t PlayerState::getIconIndex() const
  45. {
  46. return color.getNum();
  47. }
  48. std::string PlayerState::getJsonKey() const
  49. {
  50. return color.toString();
  51. }
  52. std::string PlayerState::getModScope() const
  53. {
  54. return "core";
  55. }
  56. std::string PlayerState::getNameTranslated() const
  57. {
  58. return VLC->generaltexth->translate(getNameTextID());
  59. }
  60. std::string PlayerState::getNameTextID() const
  61. {
  62. return TextIdentifier("core.plcolors", color.getNum()).get();
  63. }
  64. void PlayerState::registerIcons(const IconRegistar & cb) const
  65. {
  66. //We cannot register new icons for players
  67. }
  68. TeamID PlayerState::getTeam() const
  69. {
  70. return team;
  71. }
  72. bool PlayerState::isHuman() const
  73. {
  74. return human;
  75. }
  76. const IBonusBearer * PlayerState::getBonusBearer() const
  77. {
  78. return this;
  79. }
  80. int PlayerState::getResourceAmount(int type) const
  81. {
  82. return vstd::atOrDefault(resources, static_cast<size_t>(type), 0);
  83. }
  84. template<typename T>
  85. std::vector<T> PlayerState::getObjectsOfType() const
  86. {
  87. std::vector<T> result;
  88. for (auto const & object : ownedObjects)
  89. {
  90. auto casted = dynamic_cast<T>(object);
  91. if (casted)
  92. result.push_back(casted);
  93. }
  94. return result;
  95. }
  96. const std::vector<const CGHeroInstance *> & PlayerState::getHeroes() const
  97. {
  98. return constOwnedHeroes;
  99. }
  100. const std::vector<const CGTownInstance *> & PlayerState::getTowns() const
  101. {
  102. return constOwnedTowns;
  103. }
  104. const std::vector<CGHeroInstance *> & PlayerState::getHeroes()
  105. {
  106. return ownedHeroes;
  107. }
  108. const std::vector<CGTownInstance *> & PlayerState::getTowns()
  109. {
  110. return ownedTowns;
  111. }
  112. std::vector<const CGObjectInstance *> PlayerState::getOwnedObjects() const
  113. {
  114. return {ownedObjects.begin(), ownedObjects.end()};
  115. }
  116. void PlayerState::addOwnedObject(CGObjectInstance * object)
  117. {
  118. assert(object->asOwnable() != nullptr);
  119. ownedObjects.push_back(object);
  120. auto * town = dynamic_cast<CGTownInstance*>(object);
  121. auto * hero = dynamic_cast<CGHeroInstance*>(object);
  122. if (town)
  123. {
  124. ownedTowns.push_back(town);
  125. constOwnedTowns.push_back(town);
  126. }
  127. if (hero)
  128. {
  129. ownedHeroes.push_back(hero);
  130. constOwnedHeroes.push_back(hero);
  131. }
  132. }
  133. void PlayerState::postDeserialize()
  134. {
  135. for (const auto& object : ownedObjects)
  136. {
  137. auto* town = dynamic_cast<CGTownInstance*>(object);
  138. auto* hero = dynamic_cast<CGHeroInstance*>(object);
  139. if (town)
  140. {
  141. ownedTowns.push_back(town);
  142. constOwnedTowns.push_back(town);
  143. }
  144. if (hero)
  145. {
  146. ownedHeroes.push_back(hero);
  147. constOwnedHeroes.push_back(hero);
  148. }
  149. }
  150. }
  151. void PlayerState::removeOwnedObject(CGObjectInstance * object)
  152. {
  153. vstd::erase(ownedObjects, object);
  154. vstd::erase(ownedTowns, object);
  155. vstd::erase(constOwnedTowns, object);
  156. vstd::erase(ownedHeroes, object);
  157. vstd::erase(constOwnedHeroes, object);
  158. }
  159. VCMI_LIB_NAMESPACE_END