CGameState.h 7.8 KB

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