CGameState.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "IGameCallback.h"
  13. namespace boost
  14. {
  15. class shared_mutex;
  16. }
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. class EVictoryLossCheckResult;
  19. class Services;
  20. class IMapService;
  21. class CMap;
  22. struct CPack;
  23. class CHeroClass;
  24. struct EventCondition;
  25. struct CampaignTravel;
  26. class CStackInstance;
  27. class CGameStateCampaign;
  28. template<typename T> class CApplier;
  29. class CBaseForGSApply;
  30. struct DLL_LINKAGE SThievesGuildInfo
  31. {
  32. std::vector<PlayerColor> playerColors; //colors of players that are in-game
  33. std::vector< std::vector< PlayerColor > > numOfTowns, numOfHeroes, gold, woodOre, mercSulfCrystGems, obelisks, artifacts, army, income; // [place] -> [colours of players]
  34. std::map<PlayerColor, InfoAboutHero> colorToBestHero; //maps player's color to his best heros'
  35. std::map<PlayerColor, EAiTactic::EAiTactic> personality; // color to personality // ai tactic
  36. std::map<PlayerColor, si32> bestCreature; // color to ID // id or -1 if not known
  37. // template <typename Handler> void serialize(Handler &h, const int version)
  38. // {
  39. // h & playerColors;
  40. // h & numOfTowns;
  41. // h & numOfHeroes;
  42. // h & gold;
  43. // h & woodOre;
  44. // h & mercSulfCrystGems;
  45. // h & obelisks;
  46. // h & artifacts;
  47. // h & army;
  48. // h & income;
  49. // h & colorToBestHero;
  50. // h & personality;
  51. // h & bestCreature;
  52. // }
  53. };
  54. struct DLL_LINKAGE RumorState
  55. {
  56. enum ERumorType : ui8
  57. {
  58. TYPE_NONE = 0, TYPE_RAND, TYPE_SPECIAL, TYPE_MAP
  59. };
  60. enum ERumorTypeSpecial : ui8
  61. {
  62. RUMOR_OBELISKS = 208,
  63. RUMOR_ARTIFACTS = 209,
  64. RUMOR_ARMY = 210,
  65. RUMOR_INCOME = 211,
  66. RUMOR_GRAIL = 212
  67. };
  68. ERumorType type;
  69. std::map<ERumorType, std::pair<int, int>> last;
  70. RumorState(){type = TYPE_NONE;};
  71. bool update(int id, int extra);
  72. template <typename Handler> void serialize(Handler &h, const int version)
  73. {
  74. h & type;
  75. h & last;
  76. }
  77. };
  78. struct UpgradeInfo
  79. {
  80. CreatureID oldID; //creature to be upgraded
  81. std::vector<CreatureID> newID; //possible upgrades
  82. std::vector<ResourceSet> cost; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>; cost is for single unit (not entire stack)
  83. UpgradeInfo(){oldID = CreatureID::NONE;};
  84. };
  85. class BattleInfo;
  86. DLL_LINKAGE std::ostream & operator<<(std::ostream & os, const EVictoryLossCheckResult & victoryLossCheckResult);
  87. class DLL_LINKAGE CGameState : public CNonConstInfoCallback
  88. {
  89. friend class CGameStateCampaign;
  90. public:
  91. struct DLL_LINKAGE HeroesPool
  92. {
  93. std::map<ui32, ConstTransitivePtr<CGHeroInstance> > heroesPool; //[subID] - heroes available to buy; nullptr if not available
  94. std::map<ui32,ui8> pavailable; // [subid] -> which players can recruit hero (binary flags)
  95. CGHeroInstance * pickHeroFor(bool native,
  96. const PlayerColor & player,
  97. const CTown * town,
  98. std::map<ui32, ConstTransitivePtr<CGHeroInstance>> & available,
  99. CRandomGenerator & rand,
  100. const CHeroClass * bannedClass = nullptr) const;
  101. template <typename Handler> void serialize(Handler &h, const int version)
  102. {
  103. h & heroesPool;
  104. h & pavailable;
  105. }
  106. } hpool; //we have here all heroes available on this map that are not hired
  107. CGameState();
  108. virtual ~CGameState();
  109. void preInit(Services * services);
  110. void init(const IMapService * mapService, StartInfo * si, bool allowSavingRandomMap = false);
  111. void updateOnLoad(StartInfo * si);
  112. ConstTransitivePtr<StartInfo> scenarioOps, initialOpts; //second one is a copy of settings received from pregame (not randomized)
  113. PlayerColor currentPlayer; //ID of player currently having turn
  114. ConstTransitivePtr<BattleInfo> curB; //current battle
  115. ui32 day; //total number of days in game
  116. ConstTransitivePtr<CMap> map;
  117. std::map<PlayerColor, PlayerState> players;
  118. std::map<TeamID, TeamState> teams;
  119. CBonusSystemNode globalEffects;
  120. RumorState rumor;
  121. static boost::shared_mutex mutex;
  122. void updateEntity(Metatype metatype, int32_t index, const JsonNode & data) override;
  123. bool giveHeroArtifact(CGHeroInstance * h, const ArtifactID & aid);
  124. void apply(CPack *pack);
  125. BattleField battleGetBattlefieldType(int3 tile, CRandomGenerator & rand);
  126. void fillUpgradeInfo(const CArmedInstance *obj, SlotID stackPos, UpgradeInfo &out) const override;
  127. PlayerRelations::PlayerRelations getPlayerRelations(PlayerColor color1, PlayerColor color2) const override;
  128. bool checkForVisitableDir(const int3 & src, const int3 & dst) const; //check if src tile is visitable from dst tile
  129. void calculatePaths(const CGHeroInstance *hero, CPathsInfo &out) override; //calculates possible paths for hero, by default uses current hero position and movement left; returns pointer to newly allocated CPath or nullptr if path does not exists
  130. void calculatePaths(const std::shared_ptr<PathfinderConfig> & config) override;
  131. int3 guardingCreaturePosition (int3 pos) const override;
  132. std::vector<CGObjectInstance*> guardingCreatures (int3 pos) const;
  133. void updateRumor();
  134. // ----- victory, loss condition checks -----
  135. EVictoryLossCheckResult checkForVictoryAndLoss(const PlayerColor & player) const;
  136. bool checkForVictory(const PlayerColor & player, const EventCondition & condition) const; //checks if given player is winner
  137. PlayerColor checkForStandardWin() const; //returns color of player that accomplished standard victory conditions or 255 (NEUTRAL) if no winner
  138. bool checkForStandardLoss(const PlayerColor & player) const; //checks if given player lost the game
  139. void obtainPlayersStats(SThievesGuildInfo & tgi, int level); //fills tgi with info about other players that is available at given level of thieves' guild
  140. std::map<ui32, ConstTransitivePtr<CGHeroInstance> > unusedHeroesFromPool(); //heroes pool without heroes that are available in taverns
  141. bool isVisible(int3 pos, const std::optional<PlayerColor> & player) const override;
  142. bool isVisible(const CGObjectInstance * obj, const std::optional<PlayerColor> & player) const override;
  143. int getDate(Date::EDateType mode=Date::DAY) const override; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
  144. // ----- getters, setters -----
  145. /// This RNG should only be used inside GS or CPackForClient-derived applyGs
  146. /// If this doesn't work for your code that mean you need a new netpack
  147. ///
  148. /// Client-side must use CRandomGenerator::getDefault which is not serialized
  149. ///
  150. /// CGameHandler have it's own getter for CRandomGenerator::getDefault
  151. /// Any server-side code outside of GH must use CRandomGenerator::getDefault
  152. CRandomGenerator & getRandomGenerator();
  153. template <typename Handler> void serialize(Handler &h, const int version)
  154. {
  155. h & scenarioOps;
  156. h & initialOpts;
  157. h & currentPlayer;
  158. h & day;
  159. h & map;
  160. h & players;
  161. h & teams;
  162. h & hpool;
  163. h & globalEffects;
  164. h & rand;
  165. h & rumor;
  166. h & campaign;
  167. BONUS_TREE_DESERIALIZATION_FIX
  168. }
  169. private:
  170. // ----- initialization -----
  171. void preInitAuto();
  172. void initNewGame(const IMapService * mapService, bool allowSavingRandomMap);
  173. void checkMapChecksum();
  174. void initGlobalBonuses();
  175. void initGrailPosition();
  176. void initRandomFactionsForPlayers();
  177. void randomizeMapObjects();
  178. void randomizeObject(CGObjectInstance *cur);
  179. void initPlayerStates();
  180. void placeStartingHeroes();
  181. void placeStartingHero(const PlayerColor & playerColor, const HeroTypeID & heroTypeId, int3 townPos);
  182. void removeHeroPlaceholders();
  183. void initStartingResources();
  184. void initHeroes();
  185. void placeHeroesInTowns();
  186. void initFogOfWar();
  187. void initStartingBonus();
  188. void initTowns();
  189. void initMapObjects();
  190. void initVisitingAndGarrisonedHeroes();
  191. void initCampaign();
  192. // ----- bonus system handling -----
  193. void buildBonusSystemTree();
  194. void attachArmedObjects();
  195. void buildGlobalTeamPlayerTree();
  196. void deserializationFix();
  197. // ---- misc helpers -----
  198. CGHeroInstance * getUsedHero(const HeroTypeID & hid) const;
  199. bool isUsedHero(const HeroTypeID & hid) const; //looks in heroes and prisons
  200. std::set<HeroTypeID> getUnusedAllowedHeroes(bool alsoIncludeNotAllowed = false) const;
  201. std::pair<Obj,int> pickObject(CGObjectInstance *obj); //chooses type of object to be randomized, returns <type, subtype>
  202. int pickUnusedHeroTypeRandomly(const PlayerColor & owner); // picks a unused hero type randomly
  203. int pickNextHeroType(const PlayerColor & owner); // picks next free hero type of the H3 hero init sequence -> chosen starting hero, then unused hero type randomly
  204. UpgradeInfo fillUpgradeInfo(const CStackInstance &stack) const;
  205. // ---- data -----
  206. std::shared_ptr<CApplier<CBaseForGSApply>> applier;
  207. CRandomGenerator rand;
  208. Services * services;
  209. /// Ponter to campaign state manager. Nullptr for single scenarios
  210. std::unique_ptr<CGameStateCampaign> campaign;
  211. friend class IGameCallback;
  212. friend class CMapHandler;
  213. friend class CGameHandler;
  214. };
  215. VCMI_LIB_NAMESPACE_END