CGameState.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * CGameState.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 "../bonuses/CBonusSystemNode.h"
  12. #include "../callback/CNonConstInfoCallback.h"
  13. #include "../callback/GameCallbackHolder.h"
  14. #include "../entities/artifact/EArtifactClass.h"
  15. #include "../LoadProgress.h"
  16. #include "RumorState.h"
  17. #include "GameStatistics.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. class EVictoryLossCheckResult;
  20. class Services;
  21. class IGameRandomizer;
  22. class IMapService;
  23. class CMap;
  24. class CSaveFile;
  25. class CLoadFile;
  26. struct CPackForClient;
  27. class CHeroClass;
  28. struct EventCondition;
  29. struct CampaignTravel;
  30. class CStackInstance;
  31. class CGameStateCampaign;
  32. class TavernHeroesPool;
  33. struct SThievesGuildInfo;
  34. class CRandomGenerator;
  35. class GameSettings;
  36. class BattleInfo;
  37. class UpgradeInfo;
  38. DLL_LINKAGE std::ostream & operator<<(std::ostream & os, const EVictoryLossCheckResult & victoryLossCheckResult);
  39. class DLL_LINKAGE CGameState : public CNonConstInfoCallback, public Serializeable
  40. {
  41. friend class CGameStateCampaign;
  42. std::shared_ptr<StartInfo> initialOpts; //copy of settings received from pregame (not randomized)
  43. std::shared_ptr<StartInfo> scenarioOps;
  44. std::unique_ptr<CMap> map;
  45. void saveCompatibilityRegisterMissingArtifacts();
  46. public:
  47. ArtifactInstanceID saveCompatibilityLastAllocatedArtifactID;
  48. std::vector<std::shared_ptr<CArtifactInstance>> saveCompatibilityUnregisteredArtifacts;
  49. /// List of currently ongoing battles
  50. std::vector<std::unique_ptr<BattleInfo>> currentBattles;
  51. /// ID that can be allocated to next battle
  52. BattleID nextBattleID = BattleID(0);
  53. //we have here all heroes available on this map that are not hired
  54. std::unique_ptr<TavernHeroesPool> heroesPool;
  55. /// list of players currently making turn. Usually - just one, except for simturns
  56. std::set<PlayerColor> actingPlayers;
  57. CGameState();
  58. virtual ~CGameState();
  59. CGameState & gameState() final { return *this; }
  60. const CGameState & gameState() const final { return *this; }
  61. void preInit(Services * services);
  62. void init(const IMapService * mapService, StartInfo * si, IGameRandomizer & gameRandomizer, Load::ProgressAccumulator &, bool allowSavingRandomMap = true);
  63. void updateOnLoad(const StartInfo & si);
  64. ui32 day; //total number of days in game
  65. std::map<PlayerColor, PlayerState> players;
  66. std::map<TeamID, TeamState> teams;
  67. CBonusSystemNode globalEffects;
  68. RumorState currentRumor;
  69. // NOTE: effectively AI mutex, only used by adventure map AI
  70. static std::shared_mutex mutex;
  71. void updateEntity(Metatype metatype, int32_t index, const JsonNode & data) override;
  72. bool giveHeroArtifact(CGHeroInstance * h, const ArtifactID & aid);
  73. /// picks next free hero type of the H3 hero init sequence -> chosen starting hero, then unused hero type randomly
  74. HeroTypeID pickNextHeroType(vstd::RNG & randomGenerator, const PlayerColor & owner);
  75. void apply(CPackForClient & pack);
  76. BattleField battleGetBattlefieldType(int3 tile, vstd::RNG & randomGenerator) const;
  77. PlayerRelations getPlayerRelations(PlayerColor color1, PlayerColor color2) const override;
  78. void calculatePaths(const std::shared_ptr<PathfinderConfig> & config) const override;
  79. std::vector<const CGObjectInstance*> guardingCreatures (int3 pos) const;
  80. /// Creates instance of spell scroll artifact with provided spell
  81. CArtifactInstance * createScroll(const SpellID & spellId);
  82. /// Creates instance of requested artifact
  83. /// For combined artifact this method will also create alll required components
  84. /// For scrolls this method will also initialize its spell
  85. CArtifactInstance * createArtifact(const ArtifactID & artId, const SpellID & spellId = SpellID::NONE);
  86. /// Returns battle in which selected player is engaged, or nullptr if none.
  87. /// Can NOT be used with neutral player, use battle by ID instead
  88. const BattleInfo * getBattle(const PlayerColor & player) const;
  89. /// Returns battle by its unique identifier, or nullptr if not found
  90. const BattleInfo * getBattle(const BattleID & battle) const;
  91. BattleInfo * getBattle(const BattleID & battle);
  92. // ----- victory, loss condition checks -----
  93. EVictoryLossCheckResult checkForVictoryAndLoss(const PlayerColor & player) const;
  94. bool checkForVictory(const PlayerColor & player, const EventCondition & condition) const; //checks if given player is winner
  95. PlayerColor checkForStandardWin() const; //returns color of player that accomplished standard victory conditions or 255 (NEUTRAL) if no winner
  96. bool checkForStandardLoss(const PlayerColor & player) const; //checks if given player lost the game
  97. //fills tgi with info about other players that is available at given level of thieves' guild
  98. void obtainPlayersStats(SThievesGuildInfo & tgi, int level) const;
  99. const IGameSettings & getSettings() const override;
  100. StartInfo * getStartInfo()
  101. {
  102. return scenarioOps.get();
  103. }
  104. const StartInfo * getStartInfo() const final
  105. {
  106. return scenarioOps.get();
  107. }
  108. const StartInfo * getInitialStartInfo() const
  109. {
  110. return initialOpts.get();
  111. }
  112. CMap & getMap()
  113. {
  114. return *map;
  115. }
  116. const CMap & getMap() const
  117. {
  118. return *map;
  119. }
  120. bool isVisibleFor(int3 pos, const PlayerColor player) const override;
  121. bool isVisibleFor(const CGObjectInstance * obj, const PlayerColor player) const override;
  122. static int getDate(int day, Date mode);
  123. int getDate(Date mode=Date::DAY) const override; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
  124. #if SCRIPTING_ENABLED
  125. scripting::Pool * getGlobalContextPool() const override;
  126. #endif
  127. void saveGame(CSaveFile & file) const;
  128. void loadGame(CLoadFile & file);
  129. template <typename Handler> void serialize(Handler &h)
  130. {
  131. h & scenarioOps;
  132. h & initialOpts;
  133. h & actingPlayers;
  134. h & day;
  135. h & map;
  136. if (!h.hasFeature(Handler::Version::NO_RAW_POINTERS_IN_SERIALIZER))
  137. saveCompatibilityRegisterMissingArtifacts();
  138. h & players;
  139. h & teams;
  140. if (h.hasFeature(Handler::Version::NO_RAW_POINTERS_IN_SERIALIZER))
  141. h & *heroesPool;
  142. else
  143. h & heroesPool;
  144. h & globalEffects;
  145. h & currentRumor;
  146. h & campaign;
  147. if (!h.hasFeature(Handler::Version::RANDOMIZATION_REWORK))
  148. {
  149. std::map<ArtifactID, int> allocatedArtifactsUnused;
  150. h & allocatedArtifactsUnused;
  151. }
  152. if (!h.hasFeature(Handler::Version::SERVER_STATISTICS))
  153. {
  154. StatisticDataSet statistic;
  155. h & statistic;
  156. }
  157. if(!h.saving && h.loadingGamestate)
  158. restoreBonusSystemTree();
  159. }
  160. private:
  161. // ----- initialization -----
  162. void initNewGame(const IMapService * mapService, vstd::RNG & randomGenerator, bool allowSavingRandomMap, Load::ProgressAccumulator & progressTracking);
  163. void initGlobalBonuses();
  164. void initGrailPosition(vstd::RNG & randomGenerator);
  165. void initRandomFactionsForPlayers(vstd::RNG & randomGenerator);
  166. void initOwnedObjects();
  167. void randomizeMapObjects(IGameRandomizer & gameRandomizer);
  168. void initPlayerStates();
  169. void placeStartingHeroes(vstd::RNG & randomGenerator);
  170. void placeStartingHero(const PlayerColor & playerColor, const HeroTypeID & heroTypeId, int3 townPos);
  171. void removeHeroPlaceholders();
  172. void initDifficulty();
  173. void initHeroes(IGameRandomizer & gameRandomizer);
  174. void placeHeroesInTowns();
  175. void initFogOfWar();
  176. void initStartingBonus(IGameRandomizer & gameRandomizer);
  177. void initTowns(vstd::RNG & randomGenerator);
  178. void initTownNames(vstd::RNG & randomGenerator);
  179. void initMapObjects(IGameRandomizer & gameRandomizer);
  180. void initVisitingAndGarrisonedHeroes();
  181. void initCampaign();
  182. // ----- bonus system handling -----
  183. void buildBonusSystemTree();
  184. void buildGlobalTeamPlayerTree();
  185. void restoreBonusSystemTree();
  186. // ---- misc helpers -----
  187. CGHeroInstance * getUsedHero(const HeroTypeID & hid) const;
  188. bool isUsedHero(const HeroTypeID & hid) const; //looks in heroes and prisons
  189. std::set<HeroTypeID> getUnusedAllowedHeroes(bool alsoIncludeNotAllowed = false) const;
  190. HeroTypeID pickUnusedHeroTypeRandomly(vstd::RNG & randomGenerator, const PlayerColor & owner); // picks a unused hero type randomly
  191. // ---- data -----
  192. Services * services;
  193. /// Pointer to campaign state manager. Nullptr for single scenarios
  194. std::unique_ptr<CGameStateCampaign> campaign;
  195. friend class IGameInfoCallback;
  196. friend class CMapHandler;
  197. friend class CGameHandler;
  198. };
  199. VCMI_LIB_NAMESPACE_END