CPlayerState.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. template<typename T>
  44. std::vector<T> getObjectsOfType() const;
  45. public:
  46. PlayerColor color;
  47. bool human; //true if human controlled player, false for AI
  48. TeamID team;
  49. TResources resources;
  50. /// list of objects that were "destroyed" by player, either via simple pick-up (e.g. resources) or defeated heroes or wandering monsters
  51. std::set<ObjectInstanceID> destroyedObjects;
  52. std::set<ObjectInstanceID> visitedObjects; // as a std::set, since most accesses here will be from visited status checks
  53. std::set<VisitedObjectGlobal> visitedObjectsGlobal;
  54. std::vector<QuestInfo> quests; //store info about all received quests
  55. std::vector<Bonus> battleBonuses; //additional bonuses to be added during battle with neutrals
  56. std::map<uint32_t, std::map<ArtifactPosition, ArtifactID>> costumesArtifacts;
  57. std::unique_ptr<JsonNode> playerLocalSettings; // Json with client-defined data, such as order of heroes or current hero paths. Not used by client/lib
  58. bool cheated;
  59. bool enteredWinningCheatCode, enteredLosingCheatCode; //if true, this player has entered cheat codes for loss / victory
  60. EPlayerStatus status;
  61. std::optional<ui8> daysWithoutCastle;
  62. TurnTimerInfo turnTimer;
  63. PlayerState();
  64. ~PlayerState();
  65. std::string nodeName() const override;
  66. PlayerColor getId() const override;
  67. TeamID getTeam() const override;
  68. bool isHuman() const override;
  69. const IBonusBearer * getBonusBearer() const override;
  70. int getResourceAmount(int type) const override;
  71. int32_t getIndex() const override;
  72. int32_t getIconIndex() const override;
  73. std::string getJsonKey() const override;
  74. std::string getModScope() const override;
  75. std::string getNameTranslated() const override;
  76. std::string getNameTextID() const override;
  77. void registerIcons(const IconRegistar & cb) const override;
  78. std::vector<const CGHeroInstance* > getHeroes() const;
  79. std::vector<const CGTownInstance* > getTowns() const;
  80. std::vector<CGHeroInstance* > getHeroes();
  81. std::vector<CGTownInstance* > getTowns();
  82. std::vector<const CGObjectInstance* > getOwnedObjects() const;
  83. void addOwnedObject(CGObjectInstance * object);
  84. void removeOwnedObject(CGObjectInstance * object);
  85. bool checkVanquished() const
  86. {
  87. return getHeroes().empty() && getTowns().empty();
  88. }
  89. template <typename Handler> void serialize(Handler &h)
  90. {
  91. h & color;
  92. h & human;
  93. h & team;
  94. h & resources;
  95. h & status;
  96. h & turnTimer;
  97. if (h.version >= Handler::Version::LOCAL_PLAYER_STATE_DATA)
  98. h & *playerLocalSettings;
  99. if (h.version >= Handler::Version::PLAYER_STATE_OWNED_OBJECTS)
  100. {
  101. h & ownedObjects;
  102. }
  103. else
  104. {
  105. std::vector<const CGObjectInstance* > heroes;
  106. std::vector<const CGObjectInstance* > towns;
  107. std::vector<const CGObjectInstance* > dwellings;
  108. h & heroes;
  109. h & towns;
  110. h & dwellings;
  111. }
  112. h & quests;
  113. h & visitedObjects;
  114. h & visitedObjectsGlobal;
  115. h & status;
  116. h & daysWithoutCastle;
  117. h & cheated;
  118. h & battleBonuses;
  119. h & costumesArtifacts;
  120. h & enteredLosingCheatCode;
  121. h & enteredWinningCheatCode;
  122. h & static_cast<CBonusSystemNode&>(*this);
  123. h & destroyedObjects;
  124. }
  125. };
  126. struct DLL_LINKAGE TeamState : public CBonusSystemNode
  127. {
  128. public:
  129. TeamID id; //position in gameState::teams
  130. std::set<PlayerColor> players; // members of this team
  131. //TODO: boost::array, bool if possible
  132. boost::multi_array<ui8, 3> fogOfWarMap; //[z][x][y] true - visible, false - hidden
  133. std::set<ObjectInstanceID> scoutedObjects;
  134. TeamState();
  135. template <typename Handler> void serialize(Handler &h)
  136. {
  137. h & id;
  138. h & players;
  139. if (h.version < Handler::Version::REMOVE_FOG_OF_WAR_POINTER)
  140. {
  141. struct Helper : public Serializeable
  142. {
  143. void serialize(Handler &h) const
  144. {}
  145. };
  146. Helper helper;
  147. auto ptrHelper = &helper;
  148. h & ptrHelper;
  149. }
  150. h & fogOfWarMap;
  151. h & static_cast<CBonusSystemNode&>(*this);
  152. if (h.version >= Handler::Version::REWARDABLE_BANKS)
  153. h & scoutedObjects;
  154. }
  155. };
  156. VCMI_LIB_NAMESPACE_END