CPlayerState.h 4.5 KB

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