CGameState.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #pragma once
  2. #ifndef _MSC_VER
  3. #include "CCreatureHandler.h"
  4. #include "VCMI_Lib.h"
  5. #include "map.h"
  6. #endif
  7. #include "HeroBonus.h"
  8. #include "CCreatureSet.h"
  9. #include "ConstTransitivePtr.h"
  10. #include "IGameCallback.h"
  11. #include "ResourceSet.h"
  12. #include "int3.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 CScriptCallback;
  24. class CCallback;
  25. class IGameCallback;
  26. class CLuaCallback;
  27. class CCPPObjectScript;
  28. class CCreatureSet;
  29. class CStack;
  30. class CGHeroInstance;
  31. class CGTownInstance;
  32. class CArmedInstance;
  33. class CGDwelling;
  34. class CGDefInfo;
  35. class CObjectScript;
  36. class CGObjectInstance;
  37. class CCreature;
  38. struct Mapa;
  39. struct StartInfo;
  40. struct SDL_Surface;
  41. class CMapHandler;
  42. class CPathfinder;
  43. struct SetObjectProperty;
  44. struct MetaString;
  45. struct CPack;
  46. class CSpell;
  47. struct TerrainTile;
  48. class CHeroClass;
  49. class CCampaign;
  50. class CCampaignState;
  51. class IModableArt;
  52. class CGGarrison;
  53. class CGameInfo;
  54. namespace boost
  55. {
  56. class shared_mutex;
  57. }
  58. //numbers of creatures are exact numbers if detailed else they are quantity ids (0 - a few, 1 - several and so on; additionally -1 - unknown)
  59. struct ArmyDescriptor : public std::map<TSlot, CStackBasicDescriptor>
  60. {
  61. bool isDetailed;
  62. DLL_LINKAGE ArmyDescriptor(const CArmedInstance *army, bool detailed); //not detailed -> quantity ids as count
  63. DLL_LINKAGE ArmyDescriptor();
  64. DLL_LINKAGE int getStrength() const;
  65. };
  66. struct DLL_LINKAGE InfoAboutHero
  67. {
  68. private:
  69. void assign(const InfoAboutHero & iah);
  70. public:
  71. struct DLL_LINKAGE Details
  72. {
  73. std::vector<int> primskills;
  74. int mana, luck, morale;
  75. } *details;
  76. char owner;
  77. const CHeroClass *hclass;
  78. std::string name;
  79. int portrait;
  80. ArmyDescriptor army;
  81. InfoAboutHero();
  82. InfoAboutHero(const InfoAboutHero & iah);
  83. InfoAboutHero & operator=(const InfoAboutHero & iah);
  84. ~InfoAboutHero();
  85. void initFromHero(const CGHeroInstance *h, bool detailed);
  86. };
  87. /// Struct which holds a int information about a town
  88. struct DLL_LINKAGE InfoAboutTown
  89. {
  90. struct Details
  91. {
  92. int hallLevel, goldIncome;
  93. bool customRes;
  94. bool garrisonedHero;
  95. } *details;
  96. const CArmedInstance * obj;
  97. char fortLevel; //0 - none
  98. char owner;
  99. std::string name;
  100. CTown *tType;
  101. bool built;
  102. ArmyDescriptor army; //numbers of creatures are valid only if details
  103. InfoAboutTown();
  104. ~InfoAboutTown();
  105. void initFromTown(const CGTownInstance *t, bool detailed);
  106. void initFromGarrison(const CGGarrison *garr, bool detailed);
  107. };
  108. // typedef si32 TResourceUnit;
  109. // typedef std::vector<si32> TResourceVector;
  110. // typedef std::set<si32> TResourceSet;
  111. struct DLL_LINKAGE SThievesGuildInfo
  112. {
  113. std::vector<ui8> playerColors; //colors of players that are in-game
  114. std::vector< std::list< ui8 > > numOfTowns, numOfHeroes, gold, woodOre, mercSulfCrystGems, obelisks, artifacts, army, income; // [place] -> [colours of players]
  115. std::map<ui8, InfoAboutHero> colorToBestHero; //maps player's color to his best heros'
  116. std::map<ui8, si8> personality; // color to personality // -1 - human, AI -> (00 - random, 01 - warrior, 02 - builder, 03 - explorer)
  117. std::map<ui8, si32> bestCreature; // color to ID // id or -1 if not known
  118. // template <typename Handler> void serialize(Handler &h, const int version)
  119. // {
  120. // h & playerColors & numOfTowns & numOfHeroes & gold & woodOre & mercSulfCrystGems & obelisks & artifacts & army & income;
  121. // h & colorToBestHero & personality & bestCreature;
  122. // }
  123. };
  124. struct DLL_LINKAGE PlayerState : public CBonusSystemNode
  125. {
  126. public:
  127. enum EStatus {INGAME, LOSER, WINNER};
  128. ui8 color;
  129. ui8 human; //true if human controlled player, false for AI
  130. ui32 currentSelection; //id of hero/town, 0xffffffff if none
  131. ui8 team;
  132. TResources resources;
  133. std::vector<ConstTransitivePtr<CGHeroInstance> > heroes;
  134. std::vector<ConstTransitivePtr<CGTownInstance> > towns;
  135. std::vector<ConstTransitivePtr<CGHeroInstance> > availableHeroes; //heroes available in taverns
  136. std::vector<ConstTransitivePtr<CGDwelling> > dwellings; //used for town growth
  137. ui8 enteredWinningCheatCode, enteredLosingCheatCode; //if true, this player has entered cheat codes for loss / victory
  138. ui8 status; //0 - in game, 1 - loser, 2 - winner <- uses EStatus enum
  139. ui8 daysWithoutCastle;
  140. PlayerState();
  141. std::string nodeName() const OVERRIDE;
  142. //override
  143. //void getParents(TCNodes &out, const CBonusSystemNode *root = NULL) const;
  144. //void getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root = NULL) const;
  145. template <typename Handler> void serialize(Handler &h, const int version)
  146. {
  147. h & color & human & currentSelection & team & resources & status;
  148. h & heroes & towns & availableHeroes & dwellings & getBonusList();
  149. h & status & daysWithoutCastle;
  150. h & enteredLosingCheatCode & enteredWinningCheatCode;
  151. h & static_cast<CBonusSystemNode&>(*this);
  152. }
  153. };
  154. struct DLL_LINKAGE TeamState : public CBonusSystemNode
  155. {
  156. public:
  157. ui8 id; //position in gameState::teams
  158. std::set<ui8> players; // members of this team
  159. std::vector<std::vector<std::vector<ui8> > > fogOfWarMap; //true - visible, false - hidden
  160. TeamState();
  161. template <typename Handler> void serialize(Handler &h, const int version)
  162. {
  163. h & id & players & fogOfWarMap;
  164. h & static_cast<CBonusSystemNode&>(*this);
  165. }
  166. };
  167. struct UpgradeInfo
  168. {
  169. int oldID; //creature to be upgraded
  170. std::vector<int> newID; //possible upgrades
  171. std::vector<TResources> cost; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>; cost is for single unit (not entire stack)
  172. UpgradeInfo(){oldID = -1;};
  173. };
  174. struct CPathNode
  175. {
  176. bool accessible; //true if a hero can be on this node
  177. int dist; //distance from the first node of searching; -1 is infinity
  178. CPathNode * theNodeBefore;
  179. int3 coord; //coordiantes
  180. bool visited;
  181. };
  182. struct DLL_LINKAGE CGPathNode
  183. {
  184. enum EAccessibility
  185. {
  186. ACCESSIBLE=1, //tile can be entered and passed
  187. VISITABLE, //tile can be entered as the last tile in path
  188. BLOCKVIS, //visitable from neighbouring tile but not passable
  189. BLOCKED //tile can't be entered nor visited
  190. };
  191. ui8 accessible; //the enum above
  192. ui8 land;
  193. ui8 turns;
  194. ui32 moveRemains;
  195. CGPathNode * theNodeBefore;
  196. int3 coord; //coordinates
  197. CGPathNode();
  198. bool reachable() const;
  199. };
  200. struct DLL_LINKAGE CPath
  201. {
  202. std::vector<CPathNode> nodes; //just get node by node
  203. int3 startPos() const; // start point
  204. int3 endPos() const; //destination point
  205. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  206. };
  207. struct DLL_LINKAGE CGPath
  208. {
  209. std::vector<CGPathNode> nodes; //just get node by node
  210. int3 startPos() const; // start point
  211. int3 endPos() const; //destination point
  212. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  213. };
  214. struct DLL_LINKAGE CPathsInfo
  215. {
  216. bool isValid;
  217. const CGHeroInstance *hero;
  218. int3 hpos;
  219. int3 sizes;
  220. CGPathNode ***nodes; //[w][h][level]
  221. bool getPath(const int3 &dst, CGPath &out);
  222. CPathsInfo(const int3 &Sizes);
  223. ~CPathsInfo();
  224. };
  225. struct DLL_LINKAGE DuelParameters
  226. {
  227. si32 terType, bfieldType;
  228. struct SideSettings
  229. {
  230. struct StackSettings
  231. {
  232. si32 type;
  233. si32 count;
  234. template <typename Handler> void serialize(Handler &h, const int version)
  235. {
  236. h & type & count;
  237. }
  238. StackSettings();
  239. StackSettings(si32 Type, si32 Count);
  240. };
  241. StackSettings stacks[GameConstants::ARMY_SIZE];
  242. si32 heroId; //-1 if none
  243. std::set<si32> spells;
  244. SideSettings();
  245. template <typename Handler> void serialize(Handler &h, const int version)
  246. {
  247. h & stacks & heroId & spells;
  248. }
  249. } sides[2];
  250. DuelParameters();
  251. template <typename Handler> void serialize(Handler &h, const int version)
  252. {
  253. h & terType & bfieldType & sides;
  254. }
  255. };
  256. class CPathfinder : private CGameInfoCallback
  257. {
  258. private:
  259. bool useSubterraneanGates;
  260. bool allowEmbarkAndDisembark;
  261. CPathsInfo &out;
  262. const CGHeroInstance *hero;
  263. const std::vector<std::vector<std::vector<ui8> > > &FoW;
  264. std::list<CGPathNode*> mq; //BFS queue -> nodes to be checked
  265. int3 curPos;
  266. CGPathNode *cp; //current (source) path node -> we took it from the queue
  267. CGPathNode *dp; //destination node -> it's a neighbour of cp that we consider
  268. const TerrainTile *ct, *dt; //tile info for both nodes
  269. ui8 useEmbarkCost; //0 - usual movement; 1 - embark; 2 - disembark
  270. int destTopVisObjID;
  271. CGPathNode *getNode(const int3 &coord);
  272. void initializeGraph();
  273. bool goodForLandSeaTransition(); //checks if current move will be between sea<->land. If so, checks it legality (returns false if movement is not possible) and sets useEmbarkCost
  274. CGPathNode::EAccessibility evaluateAccessibility(const TerrainTile *tinfo) const;
  275. bool canMoveBetween(const int3 &a, const int3 &b) const; //checks only for visitable objects that may make moving between tiles impossible, not other conditions (like tiles itself accessibility)
  276. bool canStepOntoDst() const;
  277. public:
  278. CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero);
  279. void calculatePaths(int3 src = int3(-1,-1,-1), int movement = -1); //calculates possible paths for hero, by default uses current hero position and movement left; returns pointer to newly allocated CPath or NULL if path does not exists
  280. };
  281. struct BattleInfo;
  282. class DLL_LINKAGE CGameState : public CNonConstInfoCallback
  283. {
  284. public:
  285. ConstTransitivePtr<StartInfo> scenarioOps, initialOpts; //second one is a copy of settings received from pregame (not randomized)
  286. ConstTransitivePtr<CCampaignState> campaign;
  287. ui32 seed;
  288. ui8 currentPlayer; //ID of player currently having turn
  289. ConstTransitivePtr<BattleInfo> curB; //current battle
  290. ui32 day; //total number of days in game
  291. ConstTransitivePtr<Mapa> map;
  292. bmap<ui8, PlayerState> players; //ID <-> player state
  293. bmap<ui8, TeamState> teams; //ID <-> team state
  294. bmap<int, ConstTransitivePtr<CGDefInfo> > villages, forts, capitols; //def-info for town graphics
  295. CBonusSystemNode globalEffects;
  296. bmap<const CGHeroInstance*, const CGObjectInstance*> ongoingVisits;
  297. struct DLL_LINKAGE HeroesPool
  298. {
  299. bmap<ui32, ConstTransitivePtr<CGHeroInstance> > heroesPool; //[subID] - heroes available to buy; NULL if not available
  300. bmap<ui32,ui8> pavailable; // [subid] -> which players can recruit hero (binary flags)
  301. CGHeroInstance * pickHeroFor(bool native, int player, const CTown *town, bmap<ui32, ConstTransitivePtr<CGHeroInstance> > &available, const CHeroClass *bannedClass = NULL) const;
  302. template <typename Handler> void serialize(Handler &h, const int version)
  303. {
  304. h & heroesPool & pavailable;
  305. }
  306. } hpool; //we have here all heroes available on this map that are not hired
  307. boost::shared_mutex *mx;
  308. void init(StartInfo * si, ui32 checksum, int Seed, int expectedPostInitSeed = -1);
  309. void loadTownDInfos();
  310. void randomizeObject(CGObjectInstance *cur);
  311. std::pair<int,int> pickObject(CGObjectInstance *obj); //chooses type of object to be randomized, returns <type, subtype>
  312. int pickHero(int owner);
  313. void giveHeroArtifact(CGHeroInstance *h, int aid);
  314. void apply(CPack *pack);
  315. int battleGetBattlefieldType(int3 tile = int3());// 1. sand/shore 2. sand/mesas 3. dirt/birches 4. dirt/hills 5. dirt/pines 6. grass/hills 7. grass/pines 8. lava 9. magic plains 10. snow/mountains 11. snow/trees 12. subterranean 13. swamp/trees 14. fiery fields 15. rock lands 16. magic clouds 17. lucid pools 18. holy ground 19. clover field 20. evil fog 21. "favourable winds" text on magic plains background 22. cursed ground 23. rough 24. ship to ship 25. ship
  316. UpgradeInfo getUpgradeInfo(const CStackInstance &stack);
  317. int getPlayerRelations(ui8 color1, ui8 color2);// 0 = enemy, 1 = ally, 2 = same player
  318. bool checkForVisitableDir(const int3 & src, const int3 & dst) const; //check if src tile is visitable from dst tile
  319. bool checkForVisitableDir(const int3 & src, const TerrainTile *pom, const int3 & dst) const; //check if src tile is visitable from dst tile
  320. bool getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret); //calculates path between src and dest; returns pointer to newly allocated CPath or NULL if path does not exists
  321. void calculatePaths(const CGHeroInstance *hero, CPathsInfo &out, int3 src = int3(-1,-1,-1), int movement = -1); //calculates possible paths for hero, by default uses current hero position and movement left; returns pointer to newly allocated CPath or NULL if path does not exists
  322. int3 guardingCreaturePosition (int3 pos) const;
  323. int victoryCheck(ui8 player) const; //checks if given player is winner; -1 if std victory, 1 if special victory, 0 else
  324. int lossCheck(ui8 player) const; //checks if given player is loser; -1 if std loss, 1 if special, 0 else
  325. ui8 checkForStandardWin() const; //returns color of player that accomplished standard victory conditions or 255 if no winner
  326. bool checkForStandardLoss(ui8 player) const; //checks if given player lost the game
  327. void obtainPlayersStats(SThievesGuildInfo & tgi, int level); //fills tgi with info about other players that is available at given level of thieves' guild
  328. bmap<ui32, ConstTransitivePtr<CGHeroInstance> > unusedHeroesFromPool(); //heroes pool without heroes that are available in taverns
  329. BattleInfo * setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance *town);
  330. void buildBonusSystemTree();
  331. void attachArmedObjects();
  332. void buildGlobalTeamPlayerTree();
  333. void deserializationFix();
  334. bool isVisible(int3 pos, int player);
  335. bool isVisible(const CGObjectInstance *obj, int player);
  336. CGameState(); //c-tor
  337. ~CGameState(); //d-tor
  338. void getNeighbours(const TerrainTile &srct, int3 tile, std::vector<int3> &vec, const boost::logic::tribool &onLand, bool limitCoastSailing);
  339. int getMovementCost(const CGHeroInstance *h, const int3 &src, const int3 &dest, int remainingMovePoints=-1, bool checkLast=true);
  340. int getDate(int mode=0) const; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
  341. template <typename Handler> void serialize(Handler &h, const int version)
  342. {
  343. h & scenarioOps & initialOpts & seed & currentPlayer & day & map & players & teams & hpool & globalEffects & campaign;
  344. h & villages & forts & capitols;
  345. if(!h.saving)
  346. {
  347. loadTownDInfos();
  348. }
  349. BONUS_TREE_DESERIALIZATION_FIX
  350. }
  351. friend class CCallback;
  352. friend class CLuaCallback;
  353. friend class CClient;
  354. friend void initGameState(Mapa * map, CGameInfo * cgi);
  355. friend class IGameCallback;
  356. friend class CMapHandler;
  357. friend class CGameHandler;
  358. };