CGameState.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. class CTown;
  17. class CScriptCallback;
  18. class CCallback;
  19. class CLuaCallback;
  20. class CCPPObjectScript;
  21. class CCreatureSet;
  22. class CStack;
  23. class CGHeroInstance;
  24. class CGTownInstance;
  25. class CArmedInstance;
  26. class CGDefInfo;
  27. class CObjectScript;
  28. class CGObjectInstance;
  29. class CCreature;
  30. struct Mapa;
  31. struct StartInfo;
  32. struct SDL_Surface;
  33. class CMapHandler;
  34. class CPathfinder;
  35. struct IPack;
  36. struct SetObjectProperty;
  37. struct MetaString;
  38. std::string DLL_EXPORT toString(MetaString &ms);
  39. namespace boost
  40. {
  41. class shared_mutex;
  42. }
  43. struct DLL_EXPORT PlayerState
  44. {
  45. public:
  46. ui8 color, serial;
  47. ui32 currentSelection; //id of hero/town, 0xffffffff if none
  48. std::vector<std::vector<std::vector<ui8> > > fogOfWarMap; //true - visible, false - hidden
  49. std::vector<si32> resources;
  50. std::vector<CGHeroInstance *> heroes;
  51. std::vector<CGTownInstance *> towns;
  52. std::vector<CGHeroInstance *> availableHeroes; //heroes available in taverns
  53. PlayerState():color(-1),currentSelection(0xffffffff){};
  54. template <typename Handler> void serialize(Handler &h, const int version)
  55. {
  56. h & color & serial & currentSelection & fogOfWarMap & resources;
  57. ui32 size;
  58. if(h.saving) //write subids of available heroes
  59. {
  60. size = availableHeroes.size();
  61. h & size;
  62. for(size_t i=0; i < size; i++)
  63. h & availableHeroes[i]->subID;
  64. }
  65. else
  66. {
  67. ui32 hid;
  68. h & size;
  69. for(size_t i=0; i < size; i++)
  70. {
  71. //fill availableHeroes with dummy hero instances, holding subids
  72. h & hid;
  73. availableHeroes.push_back(new CGHeroInstance);
  74. availableHeroes[availableHeroes.size()-1]->subID = hid;
  75. }
  76. }
  77. }
  78. };
  79. struct DLL_EXPORT BattleInfo
  80. {
  81. ui8 side1, side2;
  82. si32 round, activeStack;
  83. ui8 siege; // = 0 ordinary battle = 1 a siege with a Fort = 2 a siege with a Citadel = 3 a siege with a Castle
  84. int3 tile; //for background and bonuses
  85. si32 hero1, hero2;
  86. CCreatureSet army1, army2;
  87. std::vector<CStack*> stacks;
  88. template <typename Handler> void serialize(Handler &h, const int version)
  89. {
  90. h & side1 & side2 & round & activeStack & siege & tile & stacks & army1 & army2 & hero1 & hero2;
  91. }
  92. CStack * getNextStack(); //which stack will have turn after current one
  93. std::vector<CStack> getStackQueue(); //returns stack in order of their movement action
  94. CStack * getStack(int stackID);
  95. CStack * getStackT(int tileID);
  96. void getAccessibilityMap(bool *accessibility, int stackToOmmit=-1); //send pointer to at least 187 allocated bytes
  97. void getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide, int stackToOmmit=-1); //send pointer to at least 187 allocated bytes
  98. 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
  99. std::vector<int> getPath(int start, int dest, bool*accessibility);
  100. std::vector<int> getAccessibility(int stackID); //returns vector of accessible tiles (taking into account the creature range)
  101. bool isStackBlocked(int ID); //returns true if there is neighbouring enemy stack
  102. 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)
  103. static std::vector<int> neighbouringTiles(int hex);
  104. 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
  105. void calculateCasualties(std::set<std::pair<ui32,si32> > *casualties);
  106. };
  107. class DLL_EXPORT CStack
  108. {
  109. public:
  110. ui32 ID; //unique ID of stack
  111. CCreature * creature;
  112. ui32 amount, baseAmount;
  113. ui32 firstHPleft; //HP of first creature in stack
  114. ui8 owner, slot; //owner - player colour (255 for neutrals), slot - position in garrison (may be 255 for neutrals/called creatures)
  115. ui8 attackerOwned; //if true, this stack is owned by attakcer (this one from left hand side of battle)
  116. ui16 position; //position on battlefield
  117. ui8 counterAttacks; //how many counter attacks can be performed more in this turn (by default set at the beginning of the round to 1)
  118. si16 shots; //how many shots left
  119. std::set<EAbilities> abilities;
  120. std::set<ECombatInfo> state;
  121. struct StackEffect
  122. {
  123. ui16 id; //spell id
  124. ui8 level; //skill level
  125. ui16 turnsRemain;
  126. template <typename Handler> void serialize(Handler &h, const int version)
  127. {
  128. h & id & level & turnsRemain;
  129. }
  130. };
  131. std::vector<StackEffect> effects;
  132. CStack(CCreature * C, int A, int O, int I, bool AO, int S);
  133. CStack() : creature(NULL),amount(-1),owner(255), position(-1), ID(-1), attackerOwned(true), firstHPleft(-1), slot(255), baseAmount(-1), counterAttacks(1), effects(), state(), abilities(){}
  134. const StackEffect * getEffect(ui16 id) const; //effect id (SP)
  135. ui32 speed() const;
  136. template <typename Handler> void save(Handler &h, const int version)
  137. {
  138. h & creature->idNumber;
  139. }
  140. template <typename Handler> void load(Handler &h, const int version)
  141. {
  142. ui32 id;
  143. h & id;
  144. creature = &VLC->creh->creatures[id];
  145. abilities = creature->abilities;
  146. }
  147. template <typename Handler> void serialize(Handler &h, const int version)
  148. {
  149. h & ID & amount & baseAmount & firstHPleft & owner & slot & attackerOwned & position & state & counterAttacks
  150. & shots;
  151. if(h.saving)
  152. save(h,version);
  153. else
  154. load(h,version);
  155. }
  156. bool alive() const
  157. {
  158. return vstd::contains(state,ALIVE);
  159. }
  160. };
  161. struct UpgradeInfo
  162. {
  163. int oldID; //creature to be upgraded
  164. std::vector<int> newID; //possible upgrades
  165. std::vector<std::set<std::pair<int,int> > > cost; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>
  166. UpgradeInfo(){oldID = -1;};
  167. };
  168. class DLL_EXPORT CGameState
  169. {
  170. private:
  171. StartInfo* scenarioOps;
  172. ui32 seed;
  173. ui8 currentPlayer; //ID of player currently having turn
  174. BattleInfo *curB; //current battle
  175. ui32 day; //total number of days in game
  176. Mapa * map;
  177. std::map<ui8,PlayerState> players; //ID <-> player state
  178. std::map<int, CGDefInfo*> villages, forts, capitols; //def-info for town graphics
  179. std::vector<ui32> resVals;
  180. struct DLL_EXPORT HeroesPool
  181. {
  182. std::map<ui32,CGHeroInstance *> heroesPool; //[subID] - heroes available to buy; NULL if not available
  183. std::map<ui32,ui8> pavailable; // [subid] -> which players can recruit hero
  184. CGHeroInstance * pickHeroFor(bool native, int player, const CTown *town, int notThatOne=-1);
  185. template <typename Handler> void serialize(Handler &h, const int version)
  186. {
  187. h & heroesPool & pavailable;
  188. }
  189. } hpool; //we have here all heroes available on this map that are not hired
  190. boost::shared_mutex *mx;
  191. void init(StartInfo * si, Mapa * map, int Seed);
  192. void loadTownDInfos();
  193. void applyNL(IPack * pack);
  194. void setObjProperty( SetObjectProperty * p );
  195. void apply(IPack * pack);
  196. void randomizeObject(CGObjectInstance *cur);
  197. std::pair<int,int> pickObject(CGObjectInstance *obj);
  198. int pickHero(int owner);
  199. CGHeroInstance *getHero(int objid);
  200. CGTownInstance *getTown(int objid);
  201. bool battleMoveCreatureStack(int ID, int dest);
  202. bool battleAttackCreatureStack(int ID, int dest);
  203. bool battleShootCreatureStack(int ID, int dest);
  204. int battleGetStack(int pos); //returns ID of stack at given tile
  205. UpgradeInfo getUpgradeInfo(CArmedInstance *obj, int stackPos);
  206. float getMarketEfficiency(int player, int mode=0);
  207. std::set<int3> tilesToReveal(int3 pos, int radious, int player) const; //if player==-1 => adds all tiles in radious
  208. public:
  209. CGameState();
  210. ~CGameState();
  211. 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
  212. template <typename Handler> void serialize(Handler &h, const int version)
  213. {
  214. h & scenarioOps & seed & currentPlayer & day & map & players & resVals & hpool;
  215. if(!h.saving)
  216. {
  217. loadTownDInfos();
  218. //recreating towns/heroes vectors in players entries
  219. for(int i=0; i<map->towns.size(); i++)
  220. if(map->towns[i]->tempOwner < PLAYER_LIMIT)
  221. players[map->towns[i]->tempOwner].towns.push_back(map->towns[i]);
  222. for(int i=0; i<map->heroes.size(); i++)
  223. if(map->heroes[i]->tempOwner < PLAYER_LIMIT)
  224. players[map->heroes[i]->tempOwner].heroes.push_back(map->heroes[i]);
  225. //recreating available heroes
  226. for(std::map<ui8,PlayerState>::iterator i=players.begin(); i!=players.end(); i++)
  227. {
  228. for(size_t j=0; j < i->second.availableHeroes.size(); j++)
  229. {
  230. ui32 hlp = i->second.availableHeroes[j]->subID;
  231. delete i->second.availableHeroes[j];
  232. i->second.availableHeroes[j] = hpool.heroesPool[hlp];
  233. }
  234. }
  235. }
  236. }
  237. friend class CCallback;
  238. friend class CPathfinder;;
  239. friend class CLuaCallback;
  240. friend class CClient;
  241. friend void initGameState(Mapa * map, CGameInfo * cgi);
  242. friend class IGameCallback;
  243. friend class CMapHandler;
  244. friend class CGameHandler;
  245. };
  246. #endif // __CGAMESTATE_H__