CPlayerState.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. , playerLocalSettings(std::make_unique<JsonNode>())
  23. , human(false)
  24. , cheated(false)
  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. std::vector<const CGHeroInstance *> PlayerState::getHeroes() const
  97. {
  98. return getObjectsOfType<const CGHeroInstance *>();
  99. }
  100. std::vector<const CGTownInstance *> PlayerState::getTowns() const
  101. {
  102. return getObjectsOfType<const CGTownInstance *>();
  103. }
  104. std::vector<CGHeroInstance *> PlayerState::getHeroes()
  105. {
  106. return getObjectsOfType<CGHeroInstance *>();
  107. }
  108. std::vector<CGTownInstance *> PlayerState::getTowns()
  109. {
  110. return getObjectsOfType<CGTownInstance *>();
  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. }
  121. void PlayerState::removeOwnedObject(CGObjectInstance * object)
  122. {
  123. vstd::erase(ownedObjects, object);
  124. }
  125. VCMI_LIB_NAMESPACE_END