CPlayerState.cpp 3.2 KB

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