CGameState.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #pragma once
  2. //#ifndef _MSC_VER
  3. #include "CCreatureHandler.h"
  4. #include "VCMI_Lib.h"
  5. #include "Mapping/CMap.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. #include "CObjectHandler.h"
  14. #include "IGameCallback.h"
  15. /*
  16. * CGameState.h, part of VCMI engine
  17. *
  18. * Authors: listed in file AUTHORS in main folder
  19. *
  20. * License: GNU General Public License v2.0 or later
  21. * Full text of license available in license.txt file, in main folder
  22. *
  23. */
  24. class CTown;
  25. class CScriptCallback;
  26. class CCallback;
  27. class IGameCallback;
  28. class CLuaCallback;
  29. class CCPPObjectScript;
  30. class CCreatureSet;
  31. class CStack;
  32. class CQuest;
  33. class CGHeroInstance;
  34. class CGTownInstance;
  35. class CArmedInstance;
  36. class CGDwelling;
  37. class CObjectScript;
  38. class CGObjectInstance;
  39. class CCreature;
  40. class CMap;
  41. struct StartInfo;
  42. struct SDL_Surface;
  43. class CMapHandler;
  44. class CPathfinder;
  45. struct SetObjectProperty;
  46. struct MetaString;
  47. struct CPack;
  48. class CSpell;
  49. struct TerrainTile;
  50. class CHeroClass;
  51. class CCampaign;
  52. class CCampaignState;
  53. class IModableArt;
  54. class CGGarrison;
  55. class CGameInfo;
  56. struct QuestInfo;
  57. class CQuest;
  58. namespace boost
  59. {
  60. class shared_mutex;
  61. }
  62. //numbers of creatures are exact numbers if detailed else they are quantity ids (0 - a few, 1 - several and so on; additionally -1 - unknown)
  63. struct ArmyDescriptor : public std::map<TSlot, CStackBasicDescriptor>
  64. {
  65. bool isDetailed;
  66. DLL_LINKAGE ArmyDescriptor(const CArmedInstance *army, bool detailed); //not detailed -> quantity ids as count
  67. DLL_LINKAGE ArmyDescriptor();
  68. DLL_LINKAGE int getStrength() const;
  69. };
  70. struct DLL_LINKAGE InfoAboutArmy
  71. {
  72. ui8 owner;
  73. std::string name;
  74. ArmyDescriptor army;
  75. InfoAboutArmy();
  76. InfoAboutArmy(const CArmedInstance *Army, bool detailed);
  77. void initFromArmy(const CArmedInstance *Army, bool detailed);
  78. };
  79. struct DLL_LINKAGE InfoAboutHero : public InfoAboutArmy
  80. {
  81. private:
  82. void assign(const InfoAboutHero & iah);
  83. public:
  84. struct DLL_LINKAGE Details
  85. {
  86. std::vector<si32> primskills;
  87. si32 mana, luck, morale;
  88. } *details;
  89. const CHeroClass *hclass;
  90. int portrait;
  91. InfoAboutHero();
  92. InfoAboutHero(const InfoAboutHero & iah);
  93. InfoAboutHero(const CGHeroInstance *h, bool detailed);
  94. ~InfoAboutHero();
  95. InfoAboutHero & operator=(const InfoAboutHero & iah);
  96. void initFromHero(const CGHeroInstance *h, bool detailed);
  97. };
  98. /// Struct which holds a int information about a town
  99. struct DLL_LINKAGE InfoAboutTown : public InfoAboutArmy
  100. {
  101. struct DLL_LINKAGE Details
  102. {
  103. si32 hallLevel, goldIncome;
  104. bool customRes;
  105. bool garrisonedHero;
  106. } *details;
  107. CTown *tType;
  108. si32 built;
  109. si32 fortLevel; //0 - none
  110. InfoAboutTown();
  111. InfoAboutTown(const CGTownInstance *t, bool detailed);
  112. ~InfoAboutTown();
  113. void initFromTown(const CGTownInstance *t, bool detailed);
  114. };
  115. // typedef si32 TResourceUnit;
  116. // typedef std::vector<si32> TResourceVector;
  117. // typedef std::set<si32> TResourceSet;
  118. struct DLL_LINKAGE SThievesGuildInfo
  119. {
  120. std::vector<TPlayerColor> playerColors; //colors of players that are in-game
  121. std::vector< std::vector< TPlayerColor > > numOfTowns, numOfHeroes, gold, woodOre, mercSulfCrystGems, obelisks, artifacts, army, income; // [place] -> [colours of players]
  122. std::map<TPlayerColor, InfoAboutHero> colorToBestHero; //maps player's color to his best heros'
  123. std::map<TPlayerColor, EAiTactic::EAiTactic> personality; // color to personality // ai tactic
  124. std::map<TPlayerColor, si32> bestCreature; // color to ID // id or -1 if not known
  125. // template <typename Handler> void serialize(Handler &h, const int version)
  126. // {
  127. // h & playerColors & numOfTowns & numOfHeroes & gold & woodOre & mercSulfCrystGems & obelisks & artifacts & army & income;
  128. // h & colorToBestHero & personality & bestCreature;
  129. // }
  130. };
  131. struct DLL_LINKAGE PlayerState : public CBonusSystemNode
  132. {
  133. public:
  134. enum EStatus {WRONG = -1, INGAME, LOSER, WINNER};
  135. TPlayerColor color;
  136. ui8 human; //true if human controlled player, false for AI
  137. ui32 currentSelection; //id of hero/town, 0xffffffff if none
  138. ui8 team;
  139. TResources resources;
  140. std::vector<ConstTransitivePtr<CGHeroInstance> > heroes;
  141. std::vector<ConstTransitivePtr<CGTownInstance> > towns;
  142. std::vector<ConstTransitivePtr<CGHeroInstance> > availableHeroes; //heroes available in taverns
  143. std::vector<ConstTransitivePtr<CGDwelling> > dwellings; //used for town growth
  144. std::vector<QuestInfo> quests; //store info about all received quests
  145. ui8 enteredWinningCheatCode, enteredLosingCheatCode; //if true, this player has entered cheat codes for loss / victory
  146. EStatus status;
  147. ui8 daysWithoutCastle;
  148. PlayerState();
  149. std::string nodeName() const OVERRIDE;
  150. //override
  151. //void getParents(TCNodes &out, const CBonusSystemNode *root = NULL) const;
  152. //void getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root = NULL) const;
  153. template <typename Handler> void serialize(Handler &h, const int version)
  154. {
  155. h & color & human & currentSelection & team & resources & status;
  156. h & heroes & towns & availableHeroes & dwellings;
  157. h & getBonusList(); //FIXME FIXME FIXME
  158. h & status & daysWithoutCastle;
  159. h & enteredLosingCheatCode & enteredWinningCheatCode;
  160. h & static_cast<CBonusSystemNode&>(*this);
  161. }
  162. };
  163. struct DLL_LINKAGE TeamState : public CBonusSystemNode
  164. {
  165. public:
  166. ui8 id; //position in gameState::teams
  167. std::set<TPlayerColor> players; // members of this team
  168. std::vector<std::vector<std::vector<ui8> > > fogOfWarMap; //true - visible, false - hidden
  169. TeamState();
  170. template <typename Handler> void serialize(Handler &h, const int version)
  171. {
  172. h & id & players & fogOfWarMap;
  173. h & static_cast<CBonusSystemNode&>(*this);
  174. }
  175. };
  176. struct UpgradeInfo
  177. {
  178. int oldID; //creature to be upgraded
  179. std::vector<int> newID; //possible upgrades
  180. std::vector<TResources> cost; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>; cost is for single unit (not entire stack)
  181. UpgradeInfo(){oldID = -1;};
  182. };
  183. struct CPathNode
  184. {
  185. bool accessible; //true if a hero can be on this node
  186. int dist; //distance from the first node of searching; -1 is infinity
  187. CPathNode * theNodeBefore;
  188. int3 coord; //coordiantes
  189. bool visited;
  190. };
  191. struct DLL_LINKAGE CGPathNode
  192. {
  193. enum EAccessibility
  194. {
  195. ACCESSIBLE=1, //tile can be entered and passed
  196. VISITABLE, //tile can be entered as the last tile in path
  197. BLOCKVIS, //visitable from neighbouring tile but not passable
  198. BLOCKED //tile can't be entered nor visited
  199. };
  200. ui8 accessible; //the enum above
  201. ui8 land;
  202. ui8 turns;
  203. ui32 moveRemains;
  204. CGPathNode * theNodeBefore;
  205. int3 coord; //coordinates
  206. CGPathNode();
  207. bool reachable() const;
  208. };
  209. struct DLL_LINKAGE CPath
  210. {
  211. std::vector<CPathNode> nodes; //just get node by node
  212. int3 startPos() const; // start point
  213. int3 endPos() const; //destination point
  214. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  215. };
  216. struct DLL_LINKAGE CGPath
  217. {
  218. std::vector<CGPathNode> nodes; //just get node by node
  219. int3 startPos() const; // start point
  220. int3 endPos() const; //destination point
  221. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  222. };
  223. struct DLL_LINKAGE CPathsInfo
  224. {
  225. bool isValid;
  226. const CGHeroInstance *hero;
  227. int3 hpos;
  228. int3 sizes;
  229. CGPathNode ***nodes; //[w][h][level]
  230. bool getPath(const int3 &dst, CGPath &out);
  231. CPathsInfo(const int3 &Sizes);
  232. ~CPathsInfo();
  233. };
  234. struct DLL_EXPORT DuelParameters
  235. {
  236. ETerrainType::ETerrainType terType;
  237. BFieldType::BFieldType bfieldType;
  238. struct DLL_EXPORT SideSettings
  239. {
  240. struct DLL_EXPORT StackSettings
  241. {
  242. si32 type;
  243. si32 count;
  244. template <typename Handler> void serialize(Handler &h, const int version)
  245. {
  246. h & type & count;
  247. }
  248. StackSettings();
  249. StackSettings(si32 Type, si32 Count);
  250. } stacks[GameConstants::ARMY_SIZE];
  251. si32 heroId; //-1 if none
  252. std::vector<si32> heroPrimSkills; //may be empty
  253. std::map<si32, CArtifactInstance*> artifacts;
  254. std::vector<std::pair<si32, si8> > heroSecSkills; //may be empty; pairs <id, level>, level [0-3]
  255. std::set<si32> spells;
  256. SideSettings();
  257. template <typename Handler> void serialize(Handler &h, const int version)
  258. {
  259. h & stacks & heroId & heroPrimSkills & artifacts & heroSecSkills & spells;
  260. }
  261. } sides[2];
  262. std::vector<shared_ptr<CObstacleInstance> > obstacles;
  263. static DuelParameters fromJSON(const std::string &fname);
  264. struct CusomCreature
  265. {
  266. int id;
  267. int attack, defense, dmg, HP, speed, shoots;
  268. CusomCreature()
  269. {
  270. id = attack = defense = dmg = HP = speed = shoots = -1;
  271. }
  272. template <typename Handler> void serialize(Handler &h, const int version)
  273. {
  274. h & id & attack & defense & dmg & HP & speed & shoots;
  275. }
  276. };
  277. std::vector<CusomCreature> creatures;
  278. DuelParameters();
  279. template <typename Handler> void serialize(Handler &h, const int version)
  280. {
  281. h & terType & bfieldType & sides & obstacles & creatures;
  282. }
  283. };
  284. class CPathfinder : private CGameInfoCallback
  285. {
  286. private:
  287. bool useSubterraneanGates;
  288. bool allowEmbarkAndDisembark;
  289. CPathsInfo &out;
  290. const CGHeroInstance *hero;
  291. const std::vector<std::vector<std::vector<ui8> > > &FoW;
  292. std::list<CGPathNode*> mq; //BFS queue -> nodes to be checked
  293. int3 curPos;
  294. CGPathNode *cp; //current (source) path node -> we took it from the queue
  295. CGPathNode *dp; //destination node -> it's a neighbour of cp that we consider
  296. const TerrainTile *ct, *dt; //tile info for both nodes
  297. ui8 useEmbarkCost; //0 - usual movement; 1 - embark; 2 - disembark
  298. int destTopVisObjID;
  299. CGPathNode *getNode(const int3 &coord);
  300. void initializeGraph();
  301. 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
  302. CGPathNode::EAccessibility evaluateAccessibility(const TerrainTile *tinfo) const;
  303. 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)
  304. public:
  305. CPathfinder(CPathsInfo &_out, CGameState *_gs, const CGHeroInstance *_hero);
  306. 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
  307. };
  308. struct BattleInfo;
  309. class DLL_LINKAGE CGameState : public CNonConstInfoCallback
  310. {
  311. public:
  312. ConstTransitivePtr<StartInfo> scenarioOps, initialOpts; //second one is a copy of settings received from pregame (not randomized)
  313. ui8 currentPlayer; //ID of player currently having turn
  314. ConstTransitivePtr<BattleInfo> curB; //current battle
  315. ui32 day; //total number of days in game
  316. ConstTransitivePtr<CMap> map;
  317. bmap<TPlayerColor, PlayerState> players;
  318. bmap<TPlayerColor, TeamState> teams;
  319. CBonusSystemNode globalEffects;
  320. bmap<const CGHeroInstance*, const CGObjectInstance*> ongoingVisits;
  321. struct DLL_LINKAGE HeroesPool
  322. {
  323. bmap<ui32, ConstTransitivePtr<CGHeroInstance> > heroesPool; //[subID] - heroes available to buy; NULL if not available
  324. bmap<ui32,ui8> pavailable; // [subid] -> which players can recruit hero (binary flags)
  325. CGHeroInstance * pickHeroFor(bool native, TPlayerColor player, const CTown *town, bmap<ui32, ConstTransitivePtr<CGHeroInstance> > &available, const CHeroClass *bannedClass = NULL) const;
  326. template <typename Handler> void serialize(Handler &h, const int version)
  327. {
  328. h & heroesPool & pavailable;
  329. }
  330. } hpool; //we have here all heroes available on this map that are not hired
  331. boost::shared_mutex *mx;
  332. void init(StartInfo * si);
  333. void initDuel();
  334. void randomizeObject(CGObjectInstance *cur);
  335. std::pair<int,int> pickObject(CGObjectInstance *obj); //chooses type of object to be randomized, returns <type, subtype>
  336. int pickHero(int owner);
  337. void giveHeroArtifact(CGHeroInstance *h, int aid);
  338. void apply(CPack *pack);
  339. BFieldType::BFieldType battleGetBattlefieldType(int3 tile) const;
  340. UpgradeInfo getUpgradeInfo(const CStackInstance &stack);
  341. PlayerRelations::PlayerRelations getPlayerRelations(TPlayerColor color1, TPlayerColor color2);
  342. bool checkForVisitableDir(const int3 & src, const int3 & dst) const; //check if src tile is visitable from dst tile
  343. bool checkForVisitableDir(const int3 & src, const TerrainTile *pom, const int3 & dst) const; //check if src tile is visitable from dst tile
  344. 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
  345. 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
  346. int3 guardingCreaturePosition (int3 pos) const;
  347. std::vector<CGObjectInstance*> guardingCreatures (int3 pos) const;
  348. int victoryCheck(TPlayerColor player) const; //checks if given player is winner; -1 if std victory, 1 if special victory, 0 else
  349. int lossCheck(TPlayerColor player) const; //checks if given player is loser; -1 if std loss, 1 if special, 0 else
  350. ui8 checkForStandardWin() const; //returns color of player that accomplished standard victory conditions or 255 if no winner
  351. bool checkForStandardLoss(TPlayerColor player) const; //checks if given player lost the game
  352. void obtainPlayersStats(SThievesGuildInfo & tgi, int level); //fills tgi with info about other players that is available at given level of thieves' guild
  353. bmap<ui32, ConstTransitivePtr<CGHeroInstance> > unusedHeroesFromPool(); //heroes pool without heroes that are available in taverns
  354. BattleInfo * setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance *town);
  355. void buildBonusSystemTree();
  356. void attachArmedObjects();
  357. void buildGlobalTeamPlayerTree();
  358. void deserializationFix();
  359. bool isVisible(int3 pos, int player);
  360. bool isVisible(const CGObjectInstance *obj, int player);
  361. CGameState(); //c-tor
  362. virtual ~CGameState(); //d-tor
  363. void getNeighbours(const TerrainTile &srct, int3 tile, std::vector<int3> &vec, const boost::logic::tribool &onLand, bool limitCoastSailing);
  364. int getMovementCost(const CGHeroInstance *h, const int3 &src, const int3 &dest, int remainingMovePoints=-1, bool checkLast=true);
  365. 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
  366. template <typename Handler> void serialize(Handler &h, const int version)
  367. {
  368. h & scenarioOps & initialOpts & currentPlayer & day & map & players & teams & hpool & globalEffects;
  369. BONUS_TREE_DESERIALIZATION_FIX
  370. }
  371. friend class CCallback;
  372. friend class CLuaCallback;
  373. friend class CClient;
  374. friend void initGameState(CMap * map, CGameInfo * cgi);
  375. friend class IGameCallback;
  376. friend class CMapHandler;
  377. friend class CGameHandler;
  378. };
  379. struct DLL_LINKAGE QuestInfo //universal interface for human and AI
  380. {
  381. const CQuest * quest;
  382. const CGObjectInstance * obj; //related object, most likely Seer Hut
  383. int3 tile;
  384. QuestInfo(){};
  385. QuestInfo (const CQuest * Quest, const CGObjectInstance * Obj, int3 Tile) :
  386. quest (Quest), obj (Obj), tile (Tile){};
  387. bool operator= (const QuestInfo &qi)
  388. {
  389. quest = qi.quest;
  390. obj = qi.obj;
  391. tile = qi.tile;
  392. return true;
  393. }
  394. bool operator== (const QuestInfo & qi) const
  395. {
  396. return (quest == qi.quest && obj == qi.obj);
  397. }
  398. //std::vector<std::string> > texts //allow additional info for quest log?
  399. template <typename Handler> void serialize(Handler &h, const int version)
  400. {
  401. h & quest & obj & tile;
  402. }
  403. };