CGameState.h 13 KB

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