CGameState.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #pragma once
  2. #include "CCreatureHandler.h"
  3. #include "VCMI_Lib.h"
  4. #include "HeroBonus.h"
  5. #include "CCreatureSet.h"
  6. #include "ConstTransitivePtr.h"
  7. #include "IGameCallback.h"
  8. #include "ResourceSet.h"
  9. #include "int3.h"
  10. #include "CRandomGenerator.h"
  11. #include "CGameStateFwd.h"
  12. #include "CPathfinder.h"
  13. /*
  14. * CGameState.h, part of VCMI engine
  15. *
  16. * Authors: listed in file AUTHORS in main folder
  17. *
  18. * License: GNU General Public License v2.0 or later
  19. * Full text of license available in license.txt file, in main folder
  20. *
  21. */
  22. class CTown;
  23. class CCallback;
  24. class IGameCallback;
  25. class CCreatureSet;
  26. class CStack;
  27. class CQuest;
  28. class CGHeroInstance;
  29. class CGTownInstance;
  30. class CArmedInstance;
  31. class CGDwelling;
  32. class CObjectScript;
  33. class CGObjectInstance;
  34. class CCreature;
  35. class CMap;
  36. struct StartInfo;
  37. struct SDL_Surface;
  38. class CMapHandler;
  39. struct SetObjectProperty;
  40. struct MetaString;
  41. struct CPack;
  42. class CSpell;
  43. struct TerrainTile;
  44. class CHeroClass;
  45. class CCampaign;
  46. class CCampaignState;
  47. class IModableArt;
  48. class CGGarrison;
  49. class CGameInfo;
  50. struct QuestInfo;
  51. class CQuest;
  52. class CCampaignScenario;
  53. struct EventCondition;
  54. class CScenarioTravel;
  55. namespace boost
  56. {
  57. class shared_mutex;
  58. }
  59. struct DLL_LINKAGE SThievesGuildInfo
  60. {
  61. std::vector<PlayerColor> playerColors; //colors of players that are in-game
  62. std::vector< std::vector< PlayerColor > > numOfTowns, numOfHeroes, gold, woodOre, mercSulfCrystGems, obelisks, artifacts, army, income; // [place] -> [colours of players]
  63. std::map<PlayerColor, InfoAboutHero> colorToBestHero; //maps player's color to his best heros'
  64. std::map<PlayerColor, EAiTactic::EAiTactic> personality; // color to personality // ai tactic
  65. std::map<PlayerColor, si32> bestCreature; // color to ID // id or -1 if not known
  66. // template <typename Handler> void serialize(Handler &h, const int version)
  67. // {
  68. // h & playerColors & numOfTowns & numOfHeroes & gold & woodOre & mercSulfCrystGems & obelisks & artifacts & army & income;
  69. // h & colorToBestHero & personality & bestCreature;
  70. // }
  71. };
  72. struct DLL_LINKAGE RumorState
  73. {
  74. enum ERumorType : ui8
  75. {
  76. TYPE_NONE = 0, TYPE_RAND, TYPE_SPECIAL, TYPE_MAP
  77. };
  78. enum ERumorTypeSpecial : ui8
  79. {
  80. RUMOR_OBELISKS = 208,
  81. RUMOR_ARTIFACTS = 209,
  82. RUMOR_ARMY = 210,
  83. RUMOR_INCOME = 211,
  84. RUMOR_GRAIL = 212
  85. };
  86. ERumorType type;
  87. std::map<ERumorType, std::pair<int, int>> last;
  88. RumorState(){type = TYPE_NONE; last = {};};
  89. bool update(int id, int extra);
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. h & type & last;
  93. }
  94. };
  95. struct UpgradeInfo
  96. {
  97. CreatureID oldID; //creature to be upgraded
  98. std::vector<CreatureID> newID; //possible upgrades
  99. std::vector<TResources> cost; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>; cost is for single unit (not entire stack)
  100. UpgradeInfo(){oldID = CreatureID::NONE;};
  101. };
  102. struct DLL_EXPORT DuelParameters
  103. {
  104. ETerrainType terType;
  105. BFieldType bfieldType;
  106. struct DLL_EXPORT SideSettings
  107. {
  108. struct DLL_EXPORT StackSettings
  109. {
  110. CreatureID type;
  111. si32 count;
  112. template <typename Handler> void serialize(Handler &h, const int version)
  113. {
  114. h & type & count;
  115. }
  116. StackSettings();
  117. StackSettings(CreatureID Type, si32 Count);
  118. } stacks[GameConstants::ARMY_SIZE];
  119. si32 heroId; //-1 if none
  120. std::vector<si32> heroPrimSkills; //may be empty
  121. std::map<si32, CArtifactInstance*> artifacts;
  122. std::vector<std::pair<si32, si8> > heroSecSkills; //may be empty; pairs <id, level>, level [0-3]
  123. std::set<SpellID> spells;
  124. SideSettings();
  125. template <typename Handler> void serialize(Handler &h, const int version)
  126. {
  127. h & stacks & heroId & heroPrimSkills & artifacts & heroSecSkills & spells;
  128. }
  129. } sides[2];
  130. std::vector<shared_ptr<CObstacleInstance> > obstacles;
  131. static DuelParameters fromJSON(const std::string &fname);
  132. struct CusomCreature
  133. {
  134. int id;
  135. int attack, defense, dmg, HP, speed, shoots;
  136. CusomCreature()
  137. {
  138. id = attack = defense = dmg = HP = speed = shoots = -1;
  139. }
  140. template <typename Handler> void serialize(Handler &h, const int version)
  141. {
  142. h & id & attack & defense & dmg & HP & speed & shoots;
  143. }
  144. };
  145. std::vector<CusomCreature> creatures;
  146. DuelParameters();
  147. template <typename Handler> void serialize(Handler &h, const int version)
  148. {
  149. h & terType & bfieldType & sides & obstacles & creatures;
  150. }
  151. };
  152. struct BattleInfo;
  153. DLL_LINKAGE std::ostream & operator<<(std::ostream & os, const EVictoryLossCheckResult & victoryLossCheckResult);
  154. class DLL_LINKAGE CGameState : public CNonConstInfoCallback
  155. {
  156. public:
  157. struct DLL_LINKAGE HeroesPool
  158. {
  159. std::map<ui32, ConstTransitivePtr<CGHeroInstance> > heroesPool; //[subID] - heroes available to buy; nullptr if not available
  160. std::map<ui32,ui8> pavailable; // [subid] -> which players can recruit hero (binary flags)
  161. CGHeroInstance * pickHeroFor(bool native, PlayerColor player, const CTown *town,
  162. std::map<ui32, ConstTransitivePtr<CGHeroInstance> > &available, CRandomGenerator & rand, const CHeroClass *bannedClass = nullptr) const;
  163. template <typename Handler> void serialize(Handler &h, const int version)
  164. {
  165. h & heroesPool & pavailable;
  166. }
  167. } hpool; //we have here all heroes available on this map that are not hired
  168. CGameState();
  169. virtual ~CGameState();
  170. void init(StartInfo * si);
  171. ConstTransitivePtr<StartInfo> scenarioOps, initialOpts; //second one is a copy of settings received from pregame (not randomized)
  172. PlayerColor currentPlayer; //ID of player currently having turn
  173. ConstTransitivePtr<BattleInfo> curB; //current battle
  174. ui32 day; //total number of days in game
  175. ConstTransitivePtr<CMap> map;
  176. std::map<PlayerColor, PlayerState> players;
  177. std::map<TeamID, TeamState> teams;
  178. CBonusSystemNode globalEffects;
  179. RumorState rumor;
  180. boost::shared_mutex *mx;
  181. void giveHeroArtifact(CGHeroInstance *h, ArtifactID aid);
  182. void apply(CPack *pack);
  183. BFieldType battleGetBattlefieldType(int3 tile);
  184. UpgradeInfo getUpgradeInfo(const CStackInstance &stack);
  185. PlayerRelations::PlayerRelations getPlayerRelations(PlayerColor color1, PlayerColor color2);
  186. bool checkForVisitableDir(const int3 & src, const int3 & dst) const; //check if src tile is visitable from dst tile
  187. void calculatePaths(const CGHeroInstance *hero, CPathsInfo &out); //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
  188. int3 guardingCreaturePosition (int3 pos) const;
  189. std::vector<CGObjectInstance*> guardingCreatures (int3 pos) const;
  190. void updateRumor();
  191. // ----- victory, loss condition checks -----
  192. EVictoryLossCheckResult checkForVictoryAndLoss(PlayerColor player) const;
  193. bool checkForVictory(PlayerColor player, const EventCondition & condition) const; //checks if given player is winner
  194. PlayerColor checkForStandardWin() const; //returns color of player that accomplished standard victory conditions or 255 (NEUTRAL) if no winner
  195. bool checkForStandardLoss(PlayerColor player) const; //checks if given player lost the game
  196. void obtainPlayersStats(SThievesGuildInfo & tgi, int level); //fills tgi with info about other players that is available at given level of thieves' guild
  197. std::map<ui32, ConstTransitivePtr<CGHeroInstance> > unusedHeroesFromPool(); //heroes pool without heroes that are available in taverns
  198. BattleInfo * setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance *town);
  199. bool isVisible(int3 pos, PlayerColor player);
  200. bool isVisible(const CGObjectInstance *obj, boost::optional<PlayerColor> player);
  201. int getDate(Date::EDateType mode=Date::DAY) const; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
  202. // ----- getters, setters -----
  203. CRandomGenerator & getRandomGenerator();
  204. template <typename Handler> void serialize(Handler &h, const int version)
  205. {
  206. h & scenarioOps & initialOpts & currentPlayer & day & map & players & teams & hpool & globalEffects & rand;
  207. if(version >= 755)
  208. {
  209. h & rumor;
  210. }
  211. else if(!h.saving)
  212. rumor = RumorState();
  213. BONUS_TREE_DESERIALIZATION_FIX
  214. }
  215. private:
  216. struct CrossoverHeroesList
  217. {
  218. std::vector<CGHeroInstance *> heroesFromPreviousScenario, heroesFromAnyPreviousScenarios;
  219. void addHeroToBothLists(CGHeroInstance * hero);
  220. void removeHeroFromBothLists(CGHeroInstance * hero);
  221. };
  222. struct CampaignHeroReplacement
  223. {
  224. CampaignHeroReplacement(CGHeroInstance * hero, ObjectInstanceID heroPlaceholderId);
  225. CGHeroInstance * hero;
  226. ObjectInstanceID heroPlaceholderId;
  227. };
  228. // ----- initialization -----
  229. void initNewGame();
  230. void initCampaign();
  231. void initDuel();
  232. void checkMapChecksum();
  233. void initGrailPosition();
  234. void initRandomFactionsForPlayers();
  235. void randomizeMapObjects();
  236. void randomizeObject(CGObjectInstance *cur);
  237. void initPlayerStates();
  238. void placeCampaignHeroes();
  239. CrossoverHeroesList getCrossoverHeroesFromPreviousScenarios() const;
  240. /// returns heroes and placeholders in where heroes will be put
  241. std::vector<CampaignHeroReplacement> generateCampaignHeroesToReplace(CrossoverHeroesList & crossoverHeroes);
  242. /// gets prepared and copied hero instances with crossover heroes from prev. scenario and travel options from current scenario
  243. void prepareCrossoverHeroes(std::vector<CampaignHeroReplacement> & campaignHeroReplacements, const CScenarioTravel & travelOptions) const;
  244. void replaceHeroesPlaceholders(const std::vector<CampaignHeroReplacement> & campaignHeroReplacements);
  245. void placeStartingHeroes();
  246. void placeStartingHero(PlayerColor playerColor, HeroTypeID heroTypeId, int3 townPos);
  247. void initStartingResources();
  248. void initHeroes();
  249. void giveCampaignBonusToHero(CGHeroInstance * hero);
  250. void initFogOfWar();
  251. void initStartingBonus();
  252. void initTowns();
  253. void initMapObjects();
  254. void initVisitingAndGarrisonedHeroes();
  255. // ----- bonus system handling -----
  256. void buildBonusSystemTree();
  257. void attachArmedObjects();
  258. void buildGlobalTeamPlayerTree();
  259. void deserializationFix();
  260. // ---- misc helpers -----
  261. CGHeroInstance * getUsedHero(HeroTypeID hid) const;
  262. bool isUsedHero(HeroTypeID hid) const; //looks in heroes and prisons
  263. std::set<HeroTypeID> getUnusedAllowedHeroes(bool alsoIncludeNotAllowed = false) const;
  264. std::pair<Obj,int> pickObject(CGObjectInstance *obj); //chooses type of object to be randomized, returns <type, subtype>
  265. int pickUnusedHeroTypeRandomly(PlayerColor owner); // picks a unused hero type randomly
  266. int pickNextHeroType(PlayerColor owner); // picks next free hero type of the H3 hero init sequence -> chosen starting hero, then unused hero type randomly
  267. // ---- data -----
  268. CRandomGenerator rand;
  269. friend class CCallback;
  270. friend class CClient;
  271. friend class IGameCallback;
  272. friend class CMapHandler;
  273. friend class CGameHandler;
  274. };