CPlayerState.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. if (h.version >= Handler::Version::LOCAL_PLAYER_STATE_DATA)
  103. h & *playerLocalSettings;
  104. if (h.version >= Handler::Version::PLAYER_STATE_OWNED_OBJECTS)
  105. {
  106. h & ownedObjects;
  107. }
  108. else
  109. {
  110. std::vector<const CGObjectInstance* > heroes;
  111. std::vector<const CGObjectInstance* > towns;
  112. std::vector<const CGObjectInstance* > dwellings;
  113. h & heroes;
  114. h & towns;
  115. h & dwellings;
  116. }
  117. h & quests;
  118. h & visitedObjects;
  119. h & visitedObjectsGlobal;
  120. h & status;
  121. h & daysWithoutCastle;
  122. h & cheated;
  123. h & battleBonuses;
  124. h & costumesArtifacts;
  125. h & enteredLosingCheatCode;
  126. h & enteredWinningCheatCode;
  127. h & static_cast<CBonusSystemNode&>(*this);
  128. h & destroyedObjects;
  129. if (!h.saving)
  130. postDeserialize();
  131. }
  132. };
  133. struct DLL_LINKAGE TeamState : public CBonusSystemNode
  134. {
  135. public:
  136. TeamID id; //position in gameState::teams
  137. std::set<PlayerColor> players; // members of this team
  138. //TODO: boost::array, bool if possible
  139. boost::multi_array<ui8, 3> fogOfWarMap; //[z][x][y] true - visible, false - hidden
  140. std::set<ObjectInstanceID> scoutedObjects;
  141. TeamState();
  142. template <typename Handler> void serialize(Handler &h)
  143. {
  144. h & id;
  145. h & players;
  146. if (h.version < Handler::Version::REMOVE_FOG_OF_WAR_POINTER)
  147. {
  148. struct Helper : public Serializeable
  149. {
  150. void serialize(Handler &h) const
  151. {}
  152. };
  153. Helper helper;
  154. auto ptrHelper = &helper;
  155. h & ptrHelper;
  156. }
  157. h & fogOfWarMap;
  158. h & static_cast<CBonusSystemNode&>(*this);
  159. if (h.version >= Handler::Version::REWARDABLE_BANKS)
  160. h & scoutedObjects;
  161. }
  162. };
  163. VCMI_LIB_NAMESPACE_END