CPlayerState.cpp 3.1 KB

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