CGameState.h 8.1 KB

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