CGameState.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 UpgradeInfo
  73. {
  74. CreatureID oldID; //creature to be upgraded
  75. std::vector<CreatureID> newID; //possible upgrades
  76. std::vector<TResources> cost; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>; cost is for single unit (not entire stack)
  77. UpgradeInfo(){oldID = CreatureID::NONE;};
  78. };
  79. struct DLL_EXPORT DuelParameters
  80. {
  81. ETerrainType terType;
  82. BFieldType bfieldType;
  83. struct DLL_EXPORT SideSettings
  84. {
  85. struct DLL_EXPORT StackSettings
  86. {
  87. CreatureID type;
  88. si32 count;
  89. template <typename Handler> void serialize(Handler &h, const int version)
  90. {
  91. h & type & count;
  92. }
  93. StackSettings();
  94. StackSettings(CreatureID Type, si32 Count);
  95. } stacks[GameConstants::ARMY_SIZE];
  96. si32 heroId; //-1 if none
  97. std::vector<si32> heroPrimSkills; //may be empty
  98. std::map<si32, CArtifactInstance*> artifacts;
  99. std::vector<std::pair<si32, si8> > heroSecSkills; //may be empty; pairs <id, level>, level [0-3]
  100. std::set<SpellID> spells;
  101. SideSettings();
  102. template <typename Handler> void serialize(Handler &h, const int version)
  103. {
  104. h & stacks & heroId & heroPrimSkills & artifacts & heroSecSkills & spells;
  105. }
  106. } sides[2];
  107. std::vector<shared_ptr<CObstacleInstance> > obstacles;
  108. static DuelParameters fromJSON(const std::string &fname);
  109. struct CusomCreature
  110. {
  111. int id;
  112. int attack, defense, dmg, HP, speed, shoots;
  113. CusomCreature()
  114. {
  115. id = attack = defense = dmg = HP = speed = shoots = -1;
  116. }
  117. template <typename Handler> void serialize(Handler &h, const int version)
  118. {
  119. h & id & attack & defense & dmg & HP & speed & shoots;
  120. }
  121. };
  122. std::vector<CusomCreature> creatures;
  123. DuelParameters();
  124. template <typename Handler> void serialize(Handler &h, const int version)
  125. {
  126. h & terType & bfieldType & sides & obstacles & creatures;
  127. }
  128. };
  129. struct BattleInfo;
  130. DLL_LINKAGE std::ostream & operator<<(std::ostream & os, const EVictoryLossCheckResult & victoryLossCheckResult);
  131. class DLL_LINKAGE CGameState : public CNonConstInfoCallback
  132. {
  133. public:
  134. struct DLL_LINKAGE HeroesPool
  135. {
  136. std::map<ui32, ConstTransitivePtr<CGHeroInstance> > heroesPool; //[subID] - heroes available to buy; nullptr if not available
  137. std::map<ui32,ui8> pavailable; // [subid] -> which players can recruit hero (binary flags)
  138. CGHeroInstance * pickHeroFor(bool native, PlayerColor player, const CTown *town,
  139. std::map<ui32, ConstTransitivePtr<CGHeroInstance> > &available, CRandomGenerator & rand, const CHeroClass *bannedClass = nullptr) const;
  140. template <typename Handler> void serialize(Handler &h, const int version)
  141. {
  142. h & heroesPool & pavailable;
  143. }
  144. } hpool; //we have here all heroes available on this map that are not hired
  145. CGameState();
  146. virtual ~CGameState();
  147. void init(StartInfo * si);
  148. ConstTransitivePtr<StartInfo> scenarioOps, initialOpts; //second one is a copy of settings received from pregame (not randomized)
  149. PlayerColor currentPlayer; //ID of player currently having turn
  150. ConstTransitivePtr<BattleInfo> curB; //current battle
  151. ui32 day; //total number of days in game
  152. ConstTransitivePtr<CMap> map;
  153. std::map<PlayerColor, PlayerState> players;
  154. std::map<TeamID, TeamState> teams;
  155. CBonusSystemNode globalEffects;
  156. boost::shared_mutex *mx;
  157. void giveHeroArtifact(CGHeroInstance *h, ArtifactID aid);
  158. void apply(CPack *pack);
  159. BFieldType battleGetBattlefieldType(int3 tile);
  160. UpgradeInfo getUpgradeInfo(const CStackInstance &stack);
  161. PlayerRelations::PlayerRelations getPlayerRelations(PlayerColor color1, PlayerColor color2);
  162. bool checkForVisitableDir(const int3 & src, const int3 & dst) const; //check if src tile is visitable from dst tile
  163. 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
  164. int3 guardingCreaturePosition (int3 pos) const;
  165. std::vector<CGObjectInstance*> guardingCreatures (int3 pos) const;
  166. // ----- victory, loss condition checks -----
  167. EVictoryLossCheckResult checkForVictoryAndLoss(PlayerColor player) const;
  168. bool checkForVictory(PlayerColor player, const EventCondition & condition) const; //checks if given player is winner
  169. PlayerColor checkForStandardWin() const; //returns color of player that accomplished standard victory conditions or 255 (NEUTRAL) if no winner
  170. bool checkForStandardLoss(PlayerColor player) const; //checks if given player lost the game
  171. void obtainPlayersStats(SThievesGuildInfo & tgi, int level); //fills tgi with info about other players that is available at given level of thieves' guild
  172. std::map<ui32, ConstTransitivePtr<CGHeroInstance> > unusedHeroesFromPool(); //heroes pool without heroes that are available in taverns
  173. BattleInfo * setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance *town);
  174. bool isVisible(int3 pos, PlayerColor player);
  175. bool isVisible(const CGObjectInstance *obj, boost::optional<PlayerColor> player);
  176. 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
  177. // ----- getters, setters -----
  178. CRandomGenerator & getRandomGenerator();
  179. template <typename Handler> void serialize(Handler &h, const int version)
  180. {
  181. h & scenarioOps & initialOpts & currentPlayer & day & map & players & teams & hpool & globalEffects & rand;
  182. BONUS_TREE_DESERIALIZATION_FIX
  183. }
  184. private:
  185. struct CrossoverHeroesList
  186. {
  187. std::vector<CGHeroInstance *> heroesFromPreviousScenario, heroesFromAnyPreviousScenarios;
  188. void addHeroToBothLists(CGHeroInstance * hero);
  189. void removeHeroFromBothLists(CGHeroInstance * hero);
  190. };
  191. struct CampaignHeroReplacement
  192. {
  193. CampaignHeroReplacement(CGHeroInstance * hero, ObjectInstanceID heroPlaceholderId);
  194. CGHeroInstance * hero;
  195. ObjectInstanceID heroPlaceholderId;
  196. };
  197. // ----- initialization -----
  198. void initNewGame();
  199. void initCampaign();
  200. void initDuel();
  201. void checkMapChecksum();
  202. void initGrailPosition();
  203. void initRandomFactionsForPlayers();
  204. void randomizeMapObjects();
  205. void randomizeObject(CGObjectInstance *cur);
  206. void initPlayerStates();
  207. void placeCampaignHeroes();
  208. CrossoverHeroesList getCrossoverHeroesFromPreviousScenarios() const;
  209. /// returns heroes and placeholders in where heroes will be put
  210. std::vector<CampaignHeroReplacement> generateCampaignHeroesToReplace(CrossoverHeroesList & crossoverHeroes);
  211. /// gets prepared and copied hero instances with crossover heroes from prev. scenario and travel options from current scenario
  212. void prepareCrossoverHeroes(std::vector<CampaignHeroReplacement> & campaignHeroReplacements, const CScenarioTravel & travelOptions) const;
  213. void replaceHeroesPlaceholders(const std::vector<CampaignHeroReplacement> & campaignHeroReplacements);
  214. void placeStartingHeroes();
  215. void placeStartingHero(PlayerColor playerColor, HeroTypeID heroTypeId, int3 townPos);
  216. void initStartingResources();
  217. void initHeroes();
  218. void giveCampaignBonusToHero(CGHeroInstance * hero);
  219. void initFogOfWar();
  220. void initStartingBonus();
  221. void initTowns();
  222. void initMapObjects();
  223. void initVisitingAndGarrisonedHeroes();
  224. // ----- bonus system handling -----
  225. void buildBonusSystemTree();
  226. void attachArmedObjects();
  227. void buildGlobalTeamPlayerTree();
  228. void deserializationFix();
  229. // ---- misc helpers -----
  230. CGHeroInstance * getUsedHero(HeroTypeID hid) const;
  231. bool isUsedHero(HeroTypeID hid) const; //looks in heroes and prisons
  232. std::set<HeroTypeID> getUnusedAllowedHeroes(bool alsoIncludeNotAllowed = false) const;
  233. std::pair<Obj,int> pickObject(CGObjectInstance *obj); //chooses type of object to be randomized, returns <type, subtype>
  234. int pickUnusedHeroTypeRandomly(PlayerColor owner); // picks a unused hero type randomly
  235. int pickNextHeroType(PlayerColor owner); // picks next free hero type of the H3 hero init sequence -> chosen starting hero, then unused hero type randomly
  236. // ---- data -----
  237. CRandomGenerator rand;
  238. friend class CCallback;
  239. friend class CClient;
  240. friend class IGameCallback;
  241. friend class CMapHandler;
  242. friend class CGameHandler;
  243. };