CGameState.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #ifndef __CGAMESTATE_H__
  2. #define __CGAMESTATE_H__
  3. #include "../global.h"
  4. #include <cassert>
  5. #ifndef _MSC_VER
  6. #include "CCreatureHandler.h"
  7. #include "VCMI_Lib.h"
  8. #include "map.h"
  9. #endif
  10. #include <set>
  11. #include <vector>
  12. #include <list>
  13. #include "HeroBonus.h"
  14. #include "CCreatureSet.h"
  15. #ifdef _WIN32
  16. #include <tchar.h>
  17. #else
  18. #include "../tchar_amigaos4.h"
  19. #endif
  20. #include "ConstTransitivePtr.h"
  21. /*
  22. * CGameState.h, part of VCMI engine
  23. *
  24. * Authors: listed in file AUTHORS in main folder
  25. *
  26. * License: GNU General Public License v2.0 or later
  27. * Full text of license available in license.txt file, in main folder
  28. *
  29. */
  30. class CTown;
  31. class CScriptCallback;
  32. class CCallback;
  33. class IGameCallback;
  34. class CLuaCallback;
  35. class CCPPObjectScript;
  36. class CCreatureSet;
  37. class CStack;
  38. class CGHeroInstance;
  39. class CGTownInstance;
  40. class CArmedInstance;
  41. class CGDwelling;
  42. class CGDefInfo;
  43. class CObjectScript;
  44. class CGObjectInstance;
  45. class CCreature;
  46. struct Mapa;
  47. struct StartInfo;
  48. struct SDL_Surface;
  49. class CMapHandler;
  50. class CPathfinder;
  51. struct SetObjectProperty;
  52. struct MetaString;
  53. struct CPack;
  54. class CSpell;
  55. struct TerrainTile;
  56. class CHeroClass;
  57. class CCampaign;
  58. class CCampaignState;
  59. class IModableArt;
  60. namespace boost
  61. {
  62. class shared_mutex;
  63. }
  64. //numbers of creatures are exact numbers if detailed else they are quantity ids (0 - a few, 1 - several and so on; additionaly -1 - unknown)
  65. struct DLL_EXPORT ArmyDescriptor : public std::map<TSlot, CStackBasicDescriptor>
  66. {
  67. ArmyDescriptor(const CArmedInstance *army, bool detailed); //not detailed -> quantity ids as count
  68. ArmyDescriptor();
  69. };
  70. struct DLL_EXPORT InfoAboutHero
  71. {
  72. private:
  73. void assign(const InfoAboutHero & iah);
  74. public:
  75. struct DLL_EXPORT Details
  76. {
  77. std::vector<int> primskills;
  78. int mana, luck, morale;
  79. } *details;
  80. char owner;
  81. const CHeroClass *hclass;
  82. std::string name;
  83. int portrait;
  84. ArmyDescriptor army;
  85. InfoAboutHero();
  86. InfoAboutHero(const InfoAboutHero & iah);
  87. InfoAboutHero & operator=(const InfoAboutHero & iah);
  88. ~InfoAboutHero();
  89. void initFromHero(const CGHeroInstance *h, bool detailed);
  90. };
  91. // typedef si32 TResourceUnit;
  92. // typedef std::vector<si32> TResourceVector;
  93. // typedef std::set<si32> TResourceSet;
  94. struct DLL_EXPORT SThievesGuildInfo
  95. {
  96. std::vector<ui8> playerColors; //colors of players that are in-game
  97. std::vector< std::list< ui8 > > numOfTowns, numOfHeroes, gold, woodOre, mercSulfCrystGems, obelisks, artifacts, army, income; // [place] -> [colours of players]
  98. std::map<ui8, InfoAboutHero> colorToBestHero; //maps player's color to his best heros'
  99. std::map<ui8, si8> personality; // color to personality // -1 - human, AI -> (00 - random, 01 - warrior, 02 - builder, 03 - explorer)
  100. std::map<ui8, si32> bestCreature; // color to ID // id or -1 if not known
  101. // template <typename Handler> void serialize(Handler &h, const int version)
  102. // {
  103. // h & playerColors & numOfTowns & numOfHeroes & gold & woodOre & mercSulfCrystGems & obelisks & artifacts & army & income;
  104. // h & colorToBestHero & personality & bestCreature;
  105. // }
  106. };
  107. struct DLL_EXPORT PlayerState : public CBonusSystemNode
  108. {
  109. public:
  110. enum EStatus {INGAME, LOSER, WINNER};
  111. ui8 color;
  112. ui8 human; //true if human controlled player, false for AI
  113. ui32 currentSelection; //id of hero/town, 0xffffffff if none
  114. ui8 team;
  115. std::vector<si32> resources;
  116. std::vector<ConstTransitivePtr<CGHeroInstance> > heroes;
  117. std::vector<ConstTransitivePtr<CGTownInstance> > towns;
  118. std::vector<ConstTransitivePtr<CGHeroInstance> > availableHeroes; //heroes available in taverns
  119. std::vector<ConstTransitivePtr<CGDwelling> > dwellings; //used for town growth
  120. ui8 enteredWinningCheatCode, enteredLosingCheatCode; //if true, this player has entered cheat codes for loss / victory
  121. ui8 status; //0 - in game, 1 - loser, 2 - winner <- uses EStatus enum
  122. ui8 daysWithoutCastle;
  123. PlayerState();
  124. std::string nodeName() const OVERRIDE;
  125. //override
  126. //void getParents(TCNodes &out, const CBonusSystemNode *root = NULL) const;
  127. //void getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root = NULL) const;
  128. template <typename Handler> void serialize(Handler &h, const int version)
  129. {
  130. h & color & human & currentSelection & team & resources & status;
  131. h & heroes & towns & availableHeroes & dwellings & bonuses;
  132. h & status & daysWithoutCastle;
  133. h & enteredLosingCheatCode & enteredWinningCheatCode;
  134. h & static_cast<CBonusSystemNode&>(*this);
  135. }
  136. };
  137. struct DLL_EXPORT TeamState : public CBonusSystemNode
  138. {
  139. public:
  140. ui8 id; //position in gameState::teams
  141. std::set<ui8> players; // members of this team
  142. std::vector<std::vector<std::vector<ui8> > > fogOfWarMap; //true - visible, false - hidden
  143. //TeamState();
  144. template <typename Handler> void serialize(Handler &h, const int version)
  145. {
  146. h & id & players & fogOfWarMap;
  147. h & static_cast<CBonusSystemNode&>(*this);
  148. }
  149. };
  150. struct UpgradeInfo
  151. {
  152. int oldID; //creature to be upgraded
  153. std::vector<int> newID; //possible upgrades
  154. std::vector<std::set<std::pair<int,int> > > cost; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>
  155. UpgradeInfo(){oldID = -1;};
  156. };
  157. struct CPathNode
  158. {
  159. bool accessible; //true if a hero can be on this node
  160. int dist; //distance from the first node of searching; -1 is infinity
  161. CPathNode * theNodeBefore;
  162. int3 coord; //coordiantes
  163. bool visited;
  164. };
  165. struct CGPathNode
  166. {
  167. enum
  168. {
  169. ACCESSIBLE=1, //tile can be entered and passed
  170. VISITABLE, //tile can be entered as the last tile in path
  171. BLOCKVIS, //visitable from neighbouring tile but not passable
  172. BLOCKED, //tile can't be entered nor visited
  173. FLYABLE //if hero flies, he can pass this tile
  174. };
  175. ui8 accessible; //the enum above
  176. ui8 land;
  177. ui8 turns;
  178. ui32 moveRemains;
  179. CGPathNode * theNodeBefore;
  180. int3 coord; //coordinates
  181. CGPathNode();
  182. };
  183. struct DLL_EXPORT CPath
  184. {
  185. std::vector<CPathNode> nodes; //just get node by node
  186. int3 startPos() const; // start point
  187. int3 endPos() const; //destination point
  188. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  189. };
  190. struct DLL_EXPORT CGPath
  191. {
  192. std::vector<CGPathNode> nodes; //just get node by node
  193. int3 startPos() const; // start point
  194. int3 endPos() const; //destination point
  195. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  196. };
  197. struct DLL_EXPORT CPathsInfo
  198. {
  199. const CGHeroInstance *hero;
  200. int3 hpos;
  201. int3 sizes;
  202. CGPathNode ***nodes; //[w][h][level]
  203. bool getPath(const int3 &dst, CGPath &out);
  204. CPathsInfo(const int3 &Sizes);
  205. ~CPathsInfo();
  206. };
  207. class BattleInfo;
  208. class DLL_EXPORT CGameState
  209. {
  210. public:
  211. ConstTransitivePtr<StartInfo> scenarioOps, initialOpts; //second one is a copy of settings received from pregame (not randomized)
  212. ConstTransitivePtr<CCampaignState> campaign;
  213. ui32 seed;
  214. ui8 currentPlayer; //ID of player currently having turn
  215. ConstTransitivePtr<BattleInfo> curB; //current battle
  216. ui32 day; //total number of days in game
  217. ConstTransitivePtr<Mapa> map;
  218. bmap<ui8, PlayerState> players; //ID <-> player state
  219. bmap<ui8, TeamState> teams; //ID <-> team state
  220. bmap<int, ConstTransitivePtr<CGDefInfo> > villages, forts, capitols; //def-info for town graphics
  221. CBonusSystemNode globalEffects;
  222. struct DLL_EXPORT HeroesPool
  223. {
  224. bmap<ui32, ConstTransitivePtr<CGHeroInstance> > heroesPool; //[subID] - heroes available to buy; NULL if not available
  225. bmap<ui32,ui8> pavailable; // [subid] -> which players can recruit hero (binary flags)
  226. CGHeroInstance * pickHeroFor(bool native, int player, const CTown *town, bmap<ui32, ConstTransitivePtr<CGHeroInstance> > &available, const CHeroClass *bannedClass = NULL) const;
  227. template <typename Handler> void serialize(Handler &h, const int version)
  228. {
  229. h & heroesPool & pavailable;
  230. }
  231. } hpool; //we have here all heroes available on this map that are not hired
  232. boost::shared_mutex *mx;
  233. PlayerState *getPlayer(ui8 color, bool verbose = true);
  234. TeamState *getTeam(ui8 teamID);//get team by team ID
  235. TeamState *getPlayerTeam(ui8 color);// get team by player color
  236. const PlayerState *getPlayer(ui8 color, bool verbose = true) const;
  237. const TeamState *getTeam(ui8 teamID) const;
  238. const TeamState *getPlayerTeam(ui8 color) const;
  239. void init(StartInfo * si, ui32 checksum, int Seed);
  240. void buildGameLogicTree();
  241. void loadTownDInfos();
  242. void randomizeObject(CGObjectInstance *cur);
  243. std::pair<int,int> pickObject(CGObjectInstance *obj); //chooses type of object to be randomized, returns <type, subtype>
  244. int pickHero(int owner);
  245. void apply(CPack *pack);
  246. CGHeroInstance *getHero(int objid);
  247. CGTownInstance *getTown(int objid);
  248. const CGHeroInstance *getHero(int objid) const;
  249. const CGTownInstance *getTown(int objid) const;
  250. bool battleCanFlee(int player); //returns true if player can flee from the battle
  251. int battleGetStack(int pos, bool onlyAlive); //returns ID of stack at given tile
  252. 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
  253. const CGHeroInstance * battleGetOwner(int stackID); //returns hero that owns given stack; NULL if none
  254. si8 battleMaxSpellLevel(); //calculates maximum spell level possible to be cast on battlefield - takes into account artifacts of both heroes; if no effects are set, SPELL_LEVELS is returned
  255. bool battleCanShoot(int ID, int dest); //determines if stack with given ID shoot at the selected destination
  256. UpgradeInfo getUpgradeInfo(const CStackInstance &stack);
  257. int getPlayerRelations(ui8 color1, ui8 color2);// 0 = enemy, 1 = ally, 2 = same player
  258. //float getMarketEfficiency(int player, int mode=0);
  259. std::set<int> getBuildingRequiments(const CGTownInstance *t, int ID);
  260. int canBuildStructure(const CGTownInstance *t, int ID);// 0 - no more than one capitol, 1 - lack of water, 2 - forbidden, 3 - Add another level to Mage Guild, 4 - already built, 5 - cannot build, 6 - cannot afford, 7 - build, 8 - lack of requirements
  261. bool checkForVisitableDir(const int3 & src, const int3 & dst) const; //check if src tile is visitable from dst tile
  262. bool checkForVisitableDir(const int3 & src, const TerrainTile *pom, const int3 & dst) const; //check if src tile is visitable from dst tile
  263. 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
  264. 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
  265. int3 guardingCreaturePosition (int3 pos) const;
  266. int victoryCheck(ui8 player) const; //checks if given player is winner; -1 if std victory, 1 if special victory, 0 else
  267. int lossCheck(ui8 player) const; //checks if given player is loser; -1 if std loss, 1 if special, 0 else
  268. ui8 checkForStandardWin() const; //returns color of player that accomplished standard victory conditions or 255 if no winner
  269. bool checkForStandardLoss(ui8 player) const; //checks if given player lost the game
  270. void obtainPlayersStats(SThievesGuildInfo & tgi, int level); //fills tgi with info about other players that is available at given level of thieves' guild
  271. bmap<ui32, ConstTransitivePtr<CGHeroInstance> > unusedHeroesFromPool(); //heroes pool without heroes that are available in taverns
  272. BattleInfo * setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance *town);
  273. bool isVisible(int3 pos, int player);
  274. bool isVisible(const CGObjectInstance *obj, int player);
  275. CGameState(); //c-tor
  276. ~CGameState(); //d-tor
  277. void getNeighbours(const TerrainTile &srct, int3 tile, std::vector<int3> &vec, const boost::logic::tribool &onLand, bool limitCoastSailing);
  278. int getMovementCost(const CGHeroInstance *h, const int3 &src, const int3 &dest, int remainingMovePoints=-1, bool checkLast=true);
  279. 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
  280. template <typename Handler> void serialize(Handler &h, const int version)
  281. {
  282. h & scenarioOps & seed & currentPlayer & day & map & players & teams & hpool & globalEffects & campaign;
  283. h & villages & forts & capitols;
  284. if(!h.saving)
  285. {
  286. loadTownDInfos();
  287. }
  288. }
  289. friend class CCallback;
  290. friend class CLuaCallback;
  291. friend class CClient;
  292. friend void initGameState(Mapa * map, CGameInfo * cgi);
  293. friend class IGameCallback;
  294. friend class CMapHandler;
  295. friend class CGameHandler;
  296. };
  297. #endif // __CGAMESTATE_H__