CGameState.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. class CGGarrison;
  61. namespace boost
  62. {
  63. class shared_mutex;
  64. }
  65. //numbers of creatures are exact numbers if detailed else they are quantity ids (0 - a few, 1 - several and so on; additionaly -1 - unknown)
  66. struct DLL_EXPORT ArmyDescriptor : public std::map<TSlot, CStackBasicDescriptor>
  67. {
  68. ArmyDescriptor(const CArmedInstance *army, bool detailed); //not detailed -> quantity ids as count
  69. ArmyDescriptor();
  70. };
  71. struct DLL_EXPORT InfoAboutHero
  72. {
  73. private:
  74. void assign(const InfoAboutHero & iah);
  75. public:
  76. struct DLL_EXPORT Details
  77. {
  78. std::vector<int> primskills;
  79. int mana, luck, morale;
  80. } *details;
  81. char owner;
  82. const CHeroClass *hclass;
  83. std::string name;
  84. int portrait;
  85. ArmyDescriptor army;
  86. InfoAboutHero();
  87. InfoAboutHero(const InfoAboutHero & iah);
  88. InfoAboutHero & operator=(const InfoAboutHero & iah);
  89. ~InfoAboutHero();
  90. void initFromHero(const CGHeroInstance *h, bool detailed);
  91. };
  92. /// Struct which holds a short information about a town
  93. struct DLL_EXPORT InfoAboutTown
  94. {
  95. struct Details
  96. {
  97. int hallLevel, goldIncome;
  98. bool customRes;
  99. bool garrisonedHero;
  100. } *details;
  101. const CArmedInstance * obj;
  102. char fortLevel; //0 - none
  103. char owner;
  104. std::string name;
  105. CTown *tType;
  106. bool built;
  107. ArmyDescriptor army; //numbers of creatures are valid only if details
  108. InfoAboutTown();
  109. ~InfoAboutTown();
  110. void initFromTown(const CGTownInstance *t, bool detailed);
  111. void initFromGarrison(const CGGarrison *garr, bool detailed);
  112. };
  113. // typedef si32 TResourceUnit;
  114. // typedef std::vector<si32> TResourceVector;
  115. // typedef std::set<si32> TResourceSet;
  116. struct DLL_EXPORT SThievesGuildInfo
  117. {
  118. std::vector<ui8> playerColors; //colors of players that are in-game
  119. std::vector< std::list< ui8 > > numOfTowns, numOfHeroes, gold, woodOre, mercSulfCrystGems, obelisks, artifacts, army, income; // [place] -> [colours of players]
  120. std::map<ui8, InfoAboutHero> colorToBestHero; //maps player's color to his best heros'
  121. std::map<ui8, si8> personality; // color to personality // -1 - human, AI -> (00 - random, 01 - warrior, 02 - builder, 03 - explorer)
  122. std::map<ui8, si32> bestCreature; // color to ID // id or -1 if not known
  123. // template <typename Handler> void serialize(Handler &h, const int version)
  124. // {
  125. // h & playerColors & numOfTowns & numOfHeroes & gold & woodOre & mercSulfCrystGems & obelisks & artifacts & army & income;
  126. // h & colorToBestHero & personality & bestCreature;
  127. // }
  128. };
  129. struct DLL_EXPORT PlayerState : public CBonusSystemNode
  130. {
  131. public:
  132. enum EStatus {INGAME, LOSER, WINNER};
  133. ui8 color;
  134. ui8 human; //true if human controlled player, false for AI
  135. ui32 currentSelection; //id of hero/town, 0xffffffff if none
  136. ui8 team;
  137. std::vector<si32> resources;
  138. std::vector<ConstTransitivePtr<CGHeroInstance> > heroes;
  139. std::vector<ConstTransitivePtr<CGTownInstance> > towns;
  140. std::vector<ConstTransitivePtr<CGHeroInstance> > availableHeroes; //heroes available in taverns
  141. std::vector<ConstTransitivePtr<CGDwelling> > dwellings; //used for town growth
  142. ui8 enteredWinningCheatCode, enteredLosingCheatCode; //if true, this player has entered cheat codes for loss / victory
  143. ui8 status; //0 - in game, 1 - loser, 2 - winner <- uses EStatus enum
  144. ui8 daysWithoutCastle;
  145. PlayerState();
  146. std::string nodeName() const OVERRIDE;
  147. //override
  148. //void getParents(TCNodes &out, const CBonusSystemNode *root = NULL) const;
  149. //void getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root = NULL) const;
  150. template <typename Handler> void serialize(Handler &h, const int version)
  151. {
  152. h & color & human & currentSelection & team & resources & status;
  153. h & heroes & towns & availableHeroes & dwellings & bonuses;
  154. h & status & daysWithoutCastle;
  155. h & enteredLosingCheatCode & enteredWinningCheatCode;
  156. h & static_cast<CBonusSystemNode&>(*this);
  157. }
  158. };
  159. struct DLL_EXPORT TeamState : public CBonusSystemNode
  160. {
  161. public:
  162. ui8 id; //position in gameState::teams
  163. std::set<ui8> players; // members of this team
  164. std::vector<std::vector<std::vector<ui8> > > fogOfWarMap; //true - visible, false - hidden
  165. TeamState();
  166. template <typename Handler> void serialize(Handler &h, const int version)
  167. {
  168. h & id & players & fogOfWarMap;
  169. h & static_cast<CBonusSystemNode&>(*this);
  170. }
  171. };
  172. struct UpgradeInfo
  173. {
  174. int oldID; //creature to be upgraded
  175. std::vector<int> newID; //possible upgrades
  176. std::vector<std::set<std::pair<int,int> > > cost; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>
  177. UpgradeInfo(){oldID = -1;};
  178. };
  179. struct CPathNode
  180. {
  181. bool accessible; //true if a hero can be on this node
  182. int dist; //distance from the first node of searching; -1 is infinity
  183. CPathNode * theNodeBefore;
  184. int3 coord; //coordiantes
  185. bool visited;
  186. };
  187. struct CGPathNode
  188. {
  189. enum
  190. {
  191. ACCESSIBLE=1, //tile can be entered and passed
  192. VISITABLE, //tile can be entered as the last tile in path
  193. BLOCKVIS, //visitable from neighbouring tile but not passable
  194. BLOCKED, //tile can't be entered nor visited
  195. FLYABLE //if hero flies, he can pass this tile
  196. };
  197. ui8 accessible; //the enum above
  198. ui8 land;
  199. ui8 turns;
  200. ui32 moveRemains;
  201. CGPathNode * theNodeBefore;
  202. int3 coord; //coordinates
  203. CGPathNode();
  204. };
  205. struct DLL_EXPORT CPath
  206. {
  207. std::vector<CPathNode> nodes; //just get node by node
  208. int3 startPos() const; // start point
  209. int3 endPos() const; //destination point
  210. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  211. };
  212. struct DLL_EXPORT CGPath
  213. {
  214. std::vector<CGPathNode> nodes; //just get node by node
  215. int3 startPos() const; // start point
  216. int3 endPos() const; //destination point
  217. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  218. };
  219. struct DLL_EXPORT CPathsInfo
  220. {
  221. const CGHeroInstance *hero;
  222. int3 hpos;
  223. int3 sizes;
  224. CGPathNode ***nodes; //[w][h][level]
  225. bool getPath(const int3 &dst, CGPath &out);
  226. CPathsInfo(const int3 &Sizes);
  227. ~CPathsInfo();
  228. };
  229. struct DLL_EXPORT DuelParameters
  230. {
  231. si32 terType, bfieldType;
  232. struct SideSettings
  233. {
  234. struct StackSettings
  235. {
  236. si32 type;
  237. si32 count;
  238. template <typename Handler> void serialize(Handler &h, const int version)
  239. {
  240. h & type & count;
  241. }
  242. StackSettings();
  243. StackSettings(si32 Type, si32 Count);
  244. } stacks[ARMY_SIZE];
  245. si32 heroId; //-1 if none
  246. std::set<si32> spells;
  247. SideSettings();
  248. template <typename Handler> void serialize(Handler &h, const int version)
  249. {
  250. h & stacks & heroId & spells;
  251. }
  252. } sides[2];
  253. DuelParameters();
  254. template <typename Handler> void serialize(Handler &h, const int version)
  255. {
  256. h & terType & bfieldType & sides;
  257. }
  258. };
  259. class BattleInfo;
  260. class DLL_EXPORT CGameState
  261. {
  262. public:
  263. ConstTransitivePtr<StartInfo> scenarioOps, initialOpts; //second one is a copy of settings received from pregame (not randomized)
  264. ConstTransitivePtr<CCampaignState> campaign;
  265. ui32 seed;
  266. ui8 currentPlayer; //ID of player currently having turn
  267. ConstTransitivePtr<BattleInfo> curB; //current battle
  268. ui32 day; //total number of days in game
  269. ConstTransitivePtr<Mapa> map;
  270. bmap<ui8, PlayerState> players; //ID <-> player state
  271. bmap<ui8, TeamState> teams; //ID <-> team state
  272. bmap<int, ConstTransitivePtr<CGDefInfo> > villages, forts, capitols; //def-info for town graphics
  273. CBonusSystemNode globalEffects;
  274. struct DLL_EXPORT HeroesPool
  275. {
  276. bmap<ui32, ConstTransitivePtr<CGHeroInstance> > heroesPool; //[subID] - heroes available to buy; NULL if not available
  277. bmap<ui32,ui8> pavailable; // [subid] -> which players can recruit hero (binary flags)
  278. CGHeroInstance * pickHeroFor(bool native, int player, const CTown *town, bmap<ui32, ConstTransitivePtr<CGHeroInstance> > &available, const CHeroClass *bannedClass = NULL) const;
  279. template <typename Handler> void serialize(Handler &h, const int version)
  280. {
  281. h & heroesPool & pavailable;
  282. }
  283. } hpool; //we have here all heroes available on this map that are not hired
  284. boost::shared_mutex *mx;
  285. PlayerState *getPlayer(ui8 color, bool verbose = true);
  286. TeamState *getTeam(ui8 teamID);//get team by team ID
  287. TeamState *getPlayerTeam(ui8 color);// get team by player color
  288. const PlayerState *getPlayer(ui8 color, bool verbose = true) const;
  289. const TeamState *getTeam(ui8 teamID) const;
  290. const TeamState *getPlayerTeam(ui8 color) const;
  291. void init(StartInfo * si, ui32 checksum, int Seed);
  292. void loadTownDInfos();
  293. void randomizeObject(CGObjectInstance *cur);
  294. std::pair<int,int> pickObject(CGObjectInstance *obj); //chooses type of object to be randomized, returns <type, subtype>
  295. int pickHero(int owner);
  296. void apply(CPack *pack);
  297. CGHeroInstance *getHero(int objid);
  298. CGTownInstance *getTown(int objid);
  299. const CGHeroInstance *getHero(int objid) const;
  300. const CGTownInstance *getTown(int objid) const;
  301. 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
  302. UpgradeInfo getUpgradeInfo(const CStackInstance &stack);
  303. int getPlayerRelations(ui8 color1, ui8 color2);// 0 = enemy, 1 = ally, 2 = same player
  304. //float getMarketEfficiency(int player, int mode=0);
  305. std::set<int> getBuildingRequiments(const CGTownInstance *t, int ID);
  306. 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
  307. bool checkForVisitableDir(const int3 & src, const int3 & dst) const; //check if src tile is visitable from dst tile
  308. bool checkForVisitableDir(const int3 & src, const TerrainTile *pom, const int3 & dst) const; //check if src tile is visitable from dst tile
  309. 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
  310. 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
  311. int3 guardingCreaturePosition (int3 pos) const;
  312. int victoryCheck(ui8 player) const; //checks if given player is winner; -1 if std victory, 1 if special victory, 0 else
  313. int lossCheck(ui8 player) const; //checks if given player is loser; -1 if std loss, 1 if special, 0 else
  314. ui8 checkForStandardWin() const; //returns color of player that accomplished standard victory conditions or 255 if no winner
  315. bool checkForStandardLoss(ui8 player) const; //checks if given player lost the game
  316. void obtainPlayersStats(SThievesGuildInfo & tgi, int level); //fills tgi with info about other players that is available at given level of thieves' guild
  317. bmap<ui32, ConstTransitivePtr<CGHeroInstance> > unusedHeroesFromPool(); //heroes pool without heroes that are available in taverns
  318. BattleInfo * setupBattle(int3 tile, const CArmedInstance *armies[2], const CGHeroInstance * heroes[2], bool creatureBank, const CGTownInstance *town);
  319. void buildBonusSystemTree();
  320. void attachArmedObjects();
  321. void buildGlobalTeamPlayerTree();
  322. void deserializationFix();
  323. bool isVisible(int3 pos, int player);
  324. bool isVisible(const CGObjectInstance *obj, int player);
  325. CGameState(); //c-tor
  326. ~CGameState(); //d-tor
  327. void getNeighbours(const TerrainTile &srct, int3 tile, std::vector<int3> &vec, const boost::logic::tribool &onLand, bool limitCoastSailing);
  328. int getMovementCost(const CGHeroInstance *h, const int3 &src, const int3 &dest, int remainingMovePoints=-1, bool checkLast=true);
  329. 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
  330. template <typename Handler> void serialize(Handler &h, const int version)
  331. {
  332. h & scenarioOps & initialOpts & seed & currentPlayer & day & map & players & teams & hpool & globalEffects & campaign;
  333. h & villages & forts & capitols;
  334. if(!h.saving)
  335. {
  336. loadTownDInfos();
  337. }
  338. BONUS_TREE_DESERIALIZATION_FIX
  339. }
  340. friend class CCallback;
  341. friend class CLuaCallback;
  342. friend class CClient;
  343. friend void initGameState(Mapa * map, CGameInfo * cgi);
  344. friend class IGameCallback;
  345. friend class CMapHandler;
  346. friend class CGameHandler;
  347. };
  348. #endif // __CGAMESTATE_H__