CPlayerState.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include "ConstTransitivePtr.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. class CGObjectInstance;
  20. class CGHeroInstance;
  21. class CGTownInstance;
  22. class CGDwelling;
  23. struct QuestInfo;
  24. class DLL_LINKAGE PlayerState : public CBonusSystemNode, public Player
  25. {
  26. struct VisitedObjectGlobal
  27. {
  28. MapObjectID id;
  29. MapObjectSubID subID;
  30. bool operator < (const VisitedObjectGlobal & other) const
  31. {
  32. if (id != other.id)
  33. return id < other.id;
  34. else
  35. return subID < other.subID;
  36. }
  37. template <typename Handler> void serialize(Handler &h)
  38. {
  39. h & id;
  40. subID.serializeIdentifier(h, id);
  41. }
  42. };
  43. std::vector<CGObjectInstance*> ownedObjects;
  44. template<typename T>
  45. std::vector<T> getObjectsOfType() const;
  46. public:
  47. PlayerColor color;
  48. bool human; //true if human controlled player, false for AI
  49. TeamID team;
  50. TResources resources;
  51. /// list of objects that were "destroyed" by player, either via simple pick-up (e.g. resources) or defeated heroes or wandering monsters
  52. std::set<ObjectInstanceID> destroyedObjects;
  53. std::set<ObjectInstanceID> visitedObjects; // as a std::set, since most accesses here will be from visited status checks
  54. std::set<VisitedObjectGlobal> visitedObjectsGlobal;
  55. std::vector<QuestInfo> quests; //store info about all received quests
  56. std::vector<Bonus> battleBonuses; //additional bonuses to be added during battle with neutrals
  57. std::map<uint32_t, std::map<ArtifactPosition, ArtifactID>> costumesArtifacts;
  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::PLAYER_STATE_OWNED_OBJECTS)
  98. {
  99. h & ownedObjects;
  100. }
  101. else
  102. {
  103. std::vector<const CGObjectInstance* > heroes;
  104. std::vector<const CGObjectInstance* > towns;
  105. std::vector<const CGObjectInstance* > dwellings;
  106. h & heroes;
  107. h & towns;
  108. h & dwellings;
  109. }
  110. h & quests;
  111. h & visitedObjects;
  112. h & visitedObjectsGlobal;
  113. h & status;
  114. h & daysWithoutCastle;
  115. h & cheated;
  116. h & battleBonuses;
  117. if (h.version >= Handler::Version::ARTIFACT_COSTUMES)
  118. h & costumesArtifacts;
  119. h & enteredLosingCheatCode;
  120. h & enteredWinningCheatCode;
  121. h & static_cast<CBonusSystemNode&>(*this);
  122. if (h.version >= Handler::Version::DESTROYED_OBJECTS)
  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. TeamState();
  134. template <typename Handler> void serialize(Handler &h)
  135. {
  136. h & id;
  137. h & players;
  138. if (h.version < Handler::Version::REMOVE_FOG_OF_WAR_POINTER)
  139. {
  140. struct Helper : public Serializeable
  141. {
  142. void serialize(Handler &h) const
  143. {}
  144. };
  145. Helper helper;
  146. auto ptrHelper = &helper;
  147. h & ptrHelper;
  148. }
  149. h & fogOfWarMap;
  150. h & static_cast<CBonusSystemNode&>(*this);
  151. }
  152. };
  153. VCMI_LIB_NAMESPACE_END