CGameState.h 7.5 KB

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