CPlayerState.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * CPlayerState.h, 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. #pragma once
  11. #include <vcmi/Player.h>
  12. #include <vcmi/Team.h>
  13. #include "bonuses/Bonus.h"
  14. #include "bonuses/CBonusSystemNode.h"
  15. #include "ResourceSet.h"
  16. #include "TurnTimerInfo.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. class CGObjectInstance;
  19. class CGHeroInstance;
  20. class CGTownInstance;
  21. class CGDwelling;
  22. struct QuestInfo;
  23. class DLL_LINKAGE PlayerState : public CBonusSystemNode, public Player
  24. {
  25. struct VisitedObjectGlobal
  26. {
  27. MapObjectID id;
  28. MapObjectSubID subID;
  29. bool operator < (const VisitedObjectGlobal & other) const
  30. {
  31. if (id != other.id)
  32. return id < other.id;
  33. else
  34. return subID < other.subID;
  35. }
  36. template <typename Handler> void serialize(Handler &h)
  37. {
  38. h & id;
  39. subID.serializeIdentifier(h, id);
  40. }
  41. };
  42. std::vector<CGObjectInstance*> ownedObjects;
  43. std::vector<const CGTownInstance*> constOwnedTowns; //not serialized
  44. std::vector<const CGHeroInstance*> constOwnedHeroes; //not serialized
  45. std::vector<CGTownInstance*> ownedTowns; //not serialized
  46. std::vector<CGHeroInstance*> ownedHeroes; //not serialized
  47. template<typename T>
  48. std::vector<T> getObjectsOfType() const;
  49. public:
  50. PlayerColor color;
  51. bool human; //true if human controlled player, false for AI
  52. TeamID team;
  53. TResources resources;
  54. /// list of objects that were "destroyed" by player, either via simple pick-up (e.g. resources) or defeated heroes or wandering monsters
  55. std::set<ObjectInstanceID> destroyedObjects;
  56. std::set<ObjectInstanceID> visitedObjects; // as a std::set, since most accesses here will be from visited status checks
  57. std::set<VisitedObjectGlobal> visitedObjectsGlobal;
  58. std::vector<QuestInfo> quests; //store info about all received quests
  59. std::vector<Bonus> battleBonuses; //additional bonuses to be added during battle with neutrals
  60. std::map<uint32_t, std::map<ArtifactPosition, ArtifactID>> costumesArtifacts;
  61. std::unique_ptr<JsonNode> playerLocalSettings; // Json with client-defined data, such as order of heroes or current hero paths. Not used by client/lib
  62. bool cheated;
  63. bool enteredWinningCheatCode, enteredLosingCheatCode; //if true, this player has entered cheat codes for loss / victory
  64. EPlayerStatus status;
  65. std::optional<ui8> daysWithoutCastle;
  66. TurnTimerInfo turnTimer;
  67. PlayerState();
  68. ~PlayerState();
  69. std::string nodeName() const override;
  70. PlayerColor getId() const override;
  71. TeamID getTeam() const override;
  72. bool isHuman() const override;
  73. const IBonusBearer * getBonusBearer() const override;
  74. int getResourceAmount(int type) const override;
  75. int32_t getIndex() const override;
  76. int32_t getIconIndex() const override;
  77. std::string getJsonKey() const override;
  78. std::string getModScope() const override;
  79. std::string getNameTranslated() const override;
  80. std::string getNameTextID() const override;
  81. void registerIcons(const IconRegistar & cb) const override;
  82. const std::vector<const CGHeroInstance* > & getHeroes() const;
  83. const std::vector<const CGTownInstance* > & getTowns() const;
  84. const std::vector<CGHeroInstance* > & getHeroes();
  85. const std::vector<CGTownInstance* > & getTowns();
  86. std::vector<const CGObjectInstance* > getOwnedObjects() const;
  87. void addOwnedObject(CGObjectInstance * object);
  88. void removeOwnedObject(CGObjectInstance * object);
  89. void postDeserialize();
  90. bool checkVanquished() const
  91. {
  92. return getHeroes().empty() && getTowns().empty();
  93. }
  94. template <typename Handler> void serialize(Handler &h)
  95. {
  96. h & color;
  97. h & human;
  98. h & team;
  99. h & resources;
  100. h & status;
  101. h & turnTimer;
  102. h & *playerLocalSettings;
  103. h & ownedObjects;
  104. h & quests;
  105. h & visitedObjects;
  106. h & visitedObjectsGlobal;
  107. h & status;
  108. h & daysWithoutCastle;
  109. h & cheated;
  110. h & battleBonuses;
  111. h & costumesArtifacts;
  112. h & enteredLosingCheatCode;
  113. h & enteredWinningCheatCode;
  114. h & static_cast<CBonusSystemNode&>(*this);
  115. h & destroyedObjects;
  116. if (!h.saving)
  117. postDeserialize();
  118. }
  119. };
  120. struct DLL_LINKAGE TeamState : public CBonusSystemNode
  121. {
  122. public:
  123. TeamID id; //position in gameState::teams
  124. std::set<PlayerColor> players; // members of this team
  125. //TODO: boost::array, bool if possible
  126. boost::multi_array<ui8, 3> fogOfWarMap; //[z][x][y] true - visible, false - hidden
  127. std::set<ObjectInstanceID> scoutedObjects;
  128. TeamState();
  129. template <typename Handler> void serialize(Handler &h)
  130. {
  131. h & id;
  132. h & players;
  133. h & fogOfWarMap;
  134. h & static_cast<CBonusSystemNode&>(*this);
  135. h & scoutedObjects;
  136. }
  137. };
  138. VCMI_LIB_NAMESPACE_END