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 "mapObjects/CGDwelling.h"
  13. #include "mapObjects/CGTownInstance.h"
  14. #include "mapObjects/CGHeroInstance.h"
  15. #include "gameState/QuestInfo.h"
  16. #include "texts/CGeneralTextHandler.h"
  17. #include "VCMI_Lib.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. PlayerState::PlayerState()
  20. : color(-1), human(false), cheated(false), enteredWinningCheatCode(false),
  21. enteredLosingCheatCode(false), status(EPlayerStatus::INGAME)
  22. {
  23. setNodeType(PLAYER);
  24. }
  25. PlayerState::~PlayerState() = default;
  26. std::string PlayerState::nodeName() const
  27. {
  28. return "Player " + color.toString();
  29. }
  30. PlayerColor PlayerState::getId() const
  31. {
  32. return color;
  33. }
  34. int32_t PlayerState::getIndex() const
  35. {
  36. return color.getNum();
  37. }
  38. int32_t PlayerState::getIconIndex() const
  39. {
  40. return color.getNum();
  41. }
  42. std::string PlayerState::getJsonKey() const
  43. {
  44. return color.toString();
  45. }
  46. std::string PlayerState::getModScope() const
  47. {
  48. return "core";
  49. }
  50. std::string PlayerState::getNameTranslated() const
  51. {
  52. return VLC->generaltexth->translate(getNameTextID());
  53. }
  54. std::string PlayerState::getNameTextID() const
  55. {
  56. return TextIdentifier("core.plcolors", color.getNum()).get();
  57. }
  58. void PlayerState::registerIcons(const IconRegistar & cb) const
  59. {
  60. //We cannot register new icons for players
  61. }
  62. TeamID PlayerState::getTeam() const
  63. {
  64. return team;
  65. }
  66. bool PlayerState::isHuman() const
  67. {
  68. return human;
  69. }
  70. const IBonusBearer * PlayerState::getBonusBearer() const
  71. {
  72. return this;
  73. }
  74. int PlayerState::getResourceAmount(int type) const
  75. {
  76. return vstd::atOrDefault(resources, static_cast<size_t>(type), 0);
  77. }
  78. std::vector<const CGDwelling* > PlayerState::getDwellings() const
  79. {
  80. std::vector<const CGDwelling* > result;
  81. for (auto const & object : ownedObjects)
  82. {
  83. auto dwelling = dynamic_cast<const CGDwelling *>(object);
  84. auto town = dynamic_cast<const CGTownInstance *>(object);
  85. if (dwelling && !town)
  86. result.push_back(dwelling);
  87. }
  88. return result;
  89. }
  90. template<typename T>
  91. std::vector<T> PlayerState::getObjectsOfType() const
  92. {
  93. std::vector<T> result;
  94. for (auto const & object : ownedObjects)
  95. {
  96. auto casted = dynamic_cast<T>(object);
  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 {ownedObjects.begin(), ownedObjects.end()};
  121. }
  122. void PlayerState::addOwnedObject(CGObjectInstance * object)
  123. {
  124. ownedObjects.push_back(object);
  125. }
  126. void PlayerState::removeOwnedObject(CGObjectInstance * object)
  127. {
  128. vstd::erase(ownedObjects, object);
  129. }
  130. VCMI_LIB_NAMESPACE_END