CGameState.h 13 KB

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