CPlayerState.h 4.6 KB

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