CGameState.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #ifndef __CGAMESTATE_H__
  2. #define __CGAMESTATE_H__
  3. #include "global.h"
  4. #ifndef _MSC_VER
  5. #include "hch/CCreatureHandler.h"
  6. #include "lib/VCMI_Lib.h"
  7. #include "map.h"
  8. #endif
  9. #include <set>
  10. #include <vector>
  11. #ifdef _WIN32
  12. #include <tchar.h>
  13. #else
  14. #include "tchar_amigaos4.h"
  15. #endif
  16. /*
  17. * CGameState.h, part of VCMI engine
  18. *
  19. * Authors: listed in file AUTHORS in main folder
  20. *
  21. * License: GNU General Public License v2.0 or later
  22. * Full text of license available in license.txt file, in main folder
  23. *
  24. */
  25. class CTown;
  26. class CScriptCallback;
  27. class CCallback;
  28. class CLuaCallback;
  29. class CCPPObjectScript;
  30. class CCreatureSet;
  31. class CStack;
  32. class CGHeroInstance;
  33. class CGTownInstance;
  34. class CArmedInstance;
  35. class CGDefInfo;
  36. class CObjectScript;
  37. class CGObjectInstance;
  38. class CCreature;
  39. struct Mapa;
  40. struct StartInfo;
  41. struct SDL_Surface;
  42. class CMapHandler;
  43. class CPathfinder;
  44. struct SetObjectProperty;
  45. struct MetaString;
  46. struct CPack;
  47. std::string DLL_EXPORT toString(MetaString &ms);
  48. namespace boost
  49. {
  50. class shared_mutex;
  51. }
  52. struct DLL_EXPORT PlayerState
  53. {
  54. public:
  55. ui8 color, serial;
  56. ui8 human; //true if human controlled player, false for AI
  57. ui32 currentSelection; //id of hero/town, 0xffffffff if none
  58. std::vector<std::vector<std::vector<ui8> > > fogOfWarMap; //true - visible, false - hidden
  59. std::vector<si32> resources;
  60. std::vector<CGHeroInstance *> heroes;
  61. std::vector<CGTownInstance *> towns;
  62. std::vector<CGHeroInstance *> availableHeroes; //heroes available in taverns
  63. PlayerState():color(-1),currentSelection(0xffffffff){};
  64. template <typename Handler> void serialize(Handler &h, const int version)
  65. {
  66. h & color & serial & human & currentSelection & fogOfWarMap & resources;
  67. ui32 size;
  68. if(h.saving) //write subids of available heroes
  69. {
  70. size = availableHeroes.size();
  71. h & size;
  72. for(size_t i=0; i < size; i++)
  73. h & availableHeroes[i]->subID;
  74. }
  75. else
  76. {
  77. ui32 hid;
  78. h & size;
  79. for(size_t i=0; i < size; i++)
  80. {
  81. //fill availableHeroes with dummy hero instances, holding subids
  82. h & hid;
  83. availableHeroes.push_back(new CGHeroInstance);
  84. availableHeroes[availableHeroes.size()-1]->subID = hid;
  85. }
  86. }
  87. }
  88. };
  89. struct DLL_EXPORT CObstacleInstance
  90. {
  91. int ID; //ID of obstacle
  92. int pos; //position on battlefield
  93. template <typename Handler> void serialize(Handler &h, const int version)
  94. {
  95. h & ID & pos;
  96. }
  97. };
  98. struct DLL_EXPORT BattleInfo
  99. {
  100. ui8 side1, side2; //side1 - attacker, side2 - defender
  101. si32 round, activeStack;
  102. ui8 siege; // = 0 ordinary battle = 1 a siege with a Fort = 2 a siege with a Citadel = 3 a siege with a Castle
  103. int3 tile; //for background and bonuses
  104. si32 hero1, hero2;
  105. CCreatureSet army1, army2;
  106. std::vector<CStack*> stacks;
  107. std::vector<CObstacleInstance> obstacles;
  108. ui8 castedSpells[2]; //[0] - attacker, [1] - defender
  109. template <typename Handler> void serialize(Handler &h, const int version)
  110. {
  111. h & side1 & side2 & round & activeStack & siege & tile & stacks & army1 & army2 & hero1 & hero2 & obstacles
  112. & castedSpells;
  113. }
  114. CStack * getNextStack(); //which stack will have turn after current one
  115. std::vector<CStack> getStackQueue(); //returns stack in order of their movement action
  116. CStack * getStack(int stackID);
  117. CStack * getStackT(int tileID);
  118. void getAccessibilityMap(bool *accessibility, int stackToOmmit=-1); //send pointer to at least 187 allocated bytes
  119. void getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide, int stackToOmmit=-1, bool addOccupiable = false); //send pointer to at least 187 allocated bytes
  120. void makeBFS(int start, bool*accessibility, int *predecessor, int *dists); //*accessibility must be prepared bool[187] array; last two pointers must point to the at least 187-elements int arrays - there is written result
  121. std::pair< std::vector<int>, int > getPath(int start, int dest, bool*accessibility, bool flyingCreature); //returned value: pair<path, length>; length may be different than number of elements in path since flying vreatures jump between distant hexes
  122. std::vector<int> getAccessibility(int stackID, bool addOccupiable); //returns vector of accessible tiles (taking into account the creature range)
  123. bool isStackBlocked(int ID); //returns true if there is neighbouring enemy stack
  124. static signed char mutualPosition(int hex1, int hex2); //returns info about mutual position of given hexes (-1 - they're distant, 0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left)
  125. static std::vector<int> neighbouringTiles(int hex);
  126. static int calculateDmg(const CStack* attacker, const CStack* defender, const CGHeroInstance * attackerHero, const CGHeroInstance * defendingHero, bool shooting); //TODO: add additional conditions and require necessary data
  127. void calculateCasualties(std::set<std::pair<ui32,si32> > *casualties);
  128. };
  129. class DLL_EXPORT CStack
  130. {
  131. public:
  132. ui32 ID; //unique ID of stack
  133. CCreature * creature;
  134. ui32 amount, baseAmount;
  135. ui32 firstHPleft; //HP of first creature in stack
  136. ui8 owner, slot; //owner - player colour (255 for neutrals), slot - position in garrison (may be 255 for neutrals/called creatures)
  137. ui8 attackerOwned; //if true, this stack is owned by attakcer (this one from left hand side of battle)
  138. ui16 position; //position on battlefield
  139. ui8 counterAttacks; //how many counter attacks can be performed more in this turn (by default set at the beginning of the round to 1)
  140. si16 shots; //how many shots left
  141. ui8 speed;
  142. si8 morale, luck; //base stack luck/morale
  143. std::set<EAbilities> abilities;
  144. std::set<ECombatInfo> state;
  145. struct StackEffect
  146. {
  147. ui16 id; //spell id
  148. ui8 level; //skill level
  149. ui16 turnsRemain;
  150. template <typename Handler> void serialize(Handler &h, const int version)
  151. {
  152. h & id & level & turnsRemain;
  153. }
  154. };
  155. std::vector<StackEffect> effects;
  156. CStack(CCreature * C, int A, int O, int I, bool AO, int S);
  157. CStack() : ID(-1), creature(NULL), amount(-1), baseAmount(-1), firstHPleft(-1), owner(255), slot(255), attackerOwned(true), position(-1), counterAttacks(1), abilities(), state(), effects() {}
  158. const StackEffect * getEffect(ui16 id) const; //effect id (SP)
  159. ui8 howManyEffectsSet(ui16 id) const; //returns amount of effects with given id set for this stack
  160. bool willMove(); //if stack has remaining move this turn
  161. ui32 Speed() const;
  162. si8 Morale() const;
  163. si8 Luck() const;
  164. template <typename Handler> void save(Handler &h, const int version)
  165. {
  166. h & creature->idNumber;
  167. }
  168. template <typename Handler> void load(Handler &h, const int version)
  169. {
  170. ui32 id;
  171. h & id;
  172. creature = &VLC->creh->creatures[id];
  173. abilities = creature->abilities;
  174. }
  175. template <typename Handler> void serialize(Handler &h, const int version)
  176. {
  177. h & ID & amount & baseAmount & firstHPleft & owner & slot & attackerOwned & position & state & counterAttacks
  178. & shots & morale & luck & speed;
  179. if(h.saving)
  180. save(h,version);
  181. else
  182. load(h,version);
  183. }
  184. bool alive() const
  185. {
  186. return vstd::contains(state,ALIVE);
  187. }
  188. };
  189. struct UpgradeInfo
  190. {
  191. int oldID; //creature to be upgraded
  192. std::vector<int> newID; //possible upgrades
  193. std::vector<std::set<std::pair<int,int> > > cost; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>
  194. UpgradeInfo(){oldID = -1;};
  195. };
  196. struct CPathNode
  197. {
  198. bool accesible; //true if a hero can be on this node
  199. int dist; //distance from the first node of searching; -1 is infinity
  200. CPathNode * theNodeBefore;
  201. int3 coord; //coordiantes
  202. bool visited;
  203. };
  204. struct DLL_EXPORT CPath
  205. {
  206. std::vector<CPathNode> nodes; //just get node by node
  207. int3 startPos() const; // start point
  208. int3 endPos() const; //destination point
  209. void convert(ui8 mode); //mode=0 -> from 'manifest' to 'object'
  210. };
  211. class DLL_EXPORT CGameState
  212. {
  213. public:
  214. StartInfo* scenarioOps;
  215. ui32 seed;
  216. ui8 currentPlayer; //ID of player currently having turn
  217. BattleInfo *curB; //current battle
  218. ui32 day; //total number of days in game
  219. Mapa * map;
  220. std::map<ui8,PlayerState> players; //ID <-> player state
  221. std::map<int, CGDefInfo*> villages, forts, capitols; //def-info for town graphics
  222. std::vector<ui32> resVals;
  223. struct DLL_EXPORT HeroesPool
  224. {
  225. std::map<ui32,CGHeroInstance *> heroesPool; //[subID] - heroes available to buy; NULL if not available
  226. std::map<ui32,ui8> pavailable; // [subid] -> which players can recruit hero
  227. CGHeroInstance * pickHeroFor(bool native, int player, const CTown *town, int notThatOne=-1);
  228. template <typename Handler> void serialize(Handler &h, const int version)
  229. {
  230. h & heroesPool & pavailable;
  231. }
  232. } hpool; //we have here all heroes available on this map that are not hired
  233. boost::shared_mutex *mx;
  234. PlayerState *getPlayer(ui8 color);
  235. void init(StartInfo * si, Mapa * map, int Seed);
  236. void loadTownDInfos();
  237. void randomizeObject(CGObjectInstance *cur);
  238. std::pair<int,int> pickObject(CGObjectInstance *obj);
  239. int pickHero(int owner);
  240. void apply(CPack *pack);
  241. CGHeroInstance *getHero(int objid);
  242. CGTownInstance *getTown(int objid);
  243. bool battleMoveCreatureStack(int ID, int dest);
  244. bool battleAttackCreatureStack(int ID, int dest);
  245. bool battleShootCreatureStack(int ID, int dest);
  246. int battleGetStack(int pos); //returns ID of stack at given tile
  247. 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
  248. UpgradeInfo getUpgradeInfo(CArmedInstance *obj, int stackPos);
  249. float getMarketEfficiency(int player, int mode=0);
  250. 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
  251. bool checkForVisitableDir(const int3 & src, const int3 & dst) const; //check if dst tile is visitable from dst tile
  252. CPath * getPath(int3 src, int3 dest, const CGHeroInstance * hero); //calculates path between src and dest; returns pointer to newly allocated CPath or NULL if path does not exists
  253. CGameState();
  254. ~CGameState();
  255. void getNeighbours(int3 tile, std::vector<int3> &vec, const boost::logic::tribool &onLand);
  256. int getMovementCost(const CGHeroInstance *h, int3 src, int3 dest, int remainingMovePoints=-1, bool checkLast=true);
  257. 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
  258. template <typename Handler> void serialize(Handler &h, const int version)
  259. {
  260. h & scenarioOps & seed & currentPlayer & day & map & players & resVals & hpool;
  261. if(!h.saving)
  262. {
  263. loadTownDInfos();
  264. //recreating towns/heroes vectors in players entries
  265. for(int i=0; i<map->towns.size(); i++)
  266. if(map->towns[i]->tempOwner < PLAYER_LIMIT)
  267. getPlayer(map->towns[i]->tempOwner)->towns.push_back(map->towns[i]);
  268. for(int i=0; i<map->heroes.size(); i++)
  269. if(map->heroes[i]->tempOwner < PLAYER_LIMIT)
  270. getPlayer(map->heroes[i]->tempOwner)->heroes.push_back(map->heroes[i]);
  271. //recreating available heroes
  272. for(std::map<ui8,PlayerState>::iterator i=players.begin(); i!=players.end(); i++)
  273. {
  274. for(size_t j=0; j < i->second.availableHeroes.size(); j++)
  275. {
  276. ui32 hlp = i->second.availableHeroes[j]->subID;
  277. delete i->second.availableHeroes[j];
  278. i->second.availableHeroes[j] = hpool.heroesPool[hlp];
  279. }
  280. }
  281. }
  282. }
  283. friend class CCallback;
  284. friend class CLuaCallback;
  285. friend class CClient;
  286. friend void initGameState(Mapa * map, CGameInfo * cgi);
  287. friend class IGameCallback;
  288. friend class CMapHandler;
  289. friend class CGameHandler;
  290. };
  291. #endif // __CGAMESTATE_H__