CGameState.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. CStack * getStack(int stackID);
  71. CStack * getStackT(int tileID);
  72. void getAccessibilityMap(bool *accessibility, int stackToOmmit=-1); //send pointer to at least 187 allocated bytes
  73. void getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide, int stackToOmmit=-1); //send pointer to at least 187 allocated bytes
  74. 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
  75. std::vector<int> getPath(int start, int dest, bool*accessibility);
  76. std::vector<int> getAccessibility(int stackID); //returns vector of accessible tiles (taking into account the creature range)
  77. bool isStackBlocked(int ID); //returns true if there is neighbouring enemy stack
  78. 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)
  79. static std::vector<int> neighbouringTiles(int hex);
  80. 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
  81. void calculateCasualties(std::set<std::pair<ui32,si32> > *casualties);
  82. };
  83. class DLL_EXPORT CStack
  84. {
  85. public:
  86. ui32 ID; //unique ID of stack
  87. CCreature * creature;
  88. ui32 amount, baseAmount;
  89. ui32 firstHPleft; //HP of first creature in stack
  90. ui8 owner, slot; //owner - player colour (255 for neutrals), slot - position in garrison (may be 255 for neutrals/called creatures)
  91. ui8 attackerOwned; //if true, this stack is owned by attakcer (this one from left hand side of battle)
  92. ui16 position; //position on battlefield
  93. ui8 counterAttacks; //how many counter attacks can be performed more in this turn (by default set at the beginning of the round to 1)
  94. si16 shots; //how many shots left
  95. std::set<EAbilities> abilities;
  96. std::set<ECombatInfo> state;
  97. struct StackEffect
  98. {
  99. ui16 id; //spell id
  100. ui8 level; //skill level
  101. ui16 turnsRemain;
  102. template <typename Handler> void serialize(Handler &h, const int version)
  103. {
  104. h & id & level & turnsRemain;
  105. }
  106. };
  107. std::vector<StackEffect> effects;
  108. CStack(CCreature * C, int A, int O, int I, bool AO, int S);
  109. CStack() : creature(NULL),amount(-1),owner(255), position(-1), ID(-1), attackerOwned(true), firstHPleft(-1), slot(255), baseAmount(-1), counterAttacks(1), effects(), state(), abilities(){}
  110. const StackEffect * getEffect(ui16 id) const; //effect id (SP)
  111. ui32 speed() const;
  112. template <typename Handler> void save(Handler &h, const int version)
  113. {
  114. h & creature->idNumber;
  115. }
  116. template <typename Handler> void load(Handler &h, const int version)
  117. {
  118. ui32 id;
  119. h & id;
  120. creature = &VLC->creh->creatures[id];
  121. abilities = creature->abilities;
  122. shots = creature->shots;
  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. if(h.saving)
  128. save(h,version);
  129. else
  130. load(h,version);
  131. }
  132. bool alive() const
  133. {
  134. return vstd::contains(state,ALIVE);
  135. }
  136. };
  137. struct UpgradeInfo
  138. {
  139. int oldID; //creature to be upgraded
  140. std::vector<int> newID; //possible upgrades
  141. std::vector<std::set<std::pair<int,int> > > cost; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>
  142. UpgradeInfo(){oldID = -1;};
  143. };
  144. class DLL_EXPORT CGameState
  145. {
  146. private:
  147. StartInfo* scenarioOps;
  148. ui32 seed;
  149. ui8 currentPlayer; //ID of player currently having turn
  150. BattleInfo *curB; //current battle
  151. ui32 day; //total number of days in game
  152. Mapa * map;
  153. std::map<ui8,PlayerState> players; //ID <-> player state
  154. std::map<int, CGDefInfo*> villages, forts, capitols; //def-info for town graphics
  155. std::vector<ui32> resVals;
  156. struct DLL_EXPORT HeroesPool
  157. {
  158. std::map<ui32,CGHeroInstance *> heroesPool; //[subID] - heroes available to buy; NULL if not available
  159. std::map<ui32,ui8> pavailable; // [subid] -> which players can recruit hero
  160. CGHeroInstance * pickHeroFor(bool native, int player, const CTown *town, int notThatOne=-1);
  161. } hpool; //we have here all heroes available on this map that are not hired
  162. boost::shared_mutex *mx;
  163. CGameState();
  164. ~CGameState();
  165. void init(StartInfo * si, Mapa * map, int Seed);
  166. void loadTownDInfos();
  167. void applyNL(IPack * pack);
  168. void apply(IPack * pack);
  169. void randomizeObject(CGObjectInstance *cur);
  170. std::pair<int,int> pickObject(CGObjectInstance *obj);
  171. int pickHero(int owner);
  172. CGHeroInstance *getHero(int objid);
  173. CGTownInstance *getTown(int objid);
  174. bool battleMoveCreatureStack(int ID, int dest);
  175. bool battleAttackCreatureStack(int ID, int dest);
  176. bool battleShootCreatureStack(int ID, int dest);
  177. int battleGetStack(int pos); //returns ID of stack at given tile
  178. UpgradeInfo getUpgradeInfo(CArmedInstance *obj, int stackPos);
  179. float getMarketEfficiency(int player, int mode=0);
  180. std::set<int3> tilesToReveal(int3 pos, int radious, int player); //if player==-1 => adds all tiles in radious
  181. public:
  182. 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
  183. template <typename Handler> void serialize(Handler &h, const int version)
  184. {
  185. h & scenarioOps & seed & currentPlayer & day & map & players & resVals;
  186. if(!h.saving)
  187. {
  188. loadTownDInfos();
  189. }
  190. //TODO: hero pool
  191. }
  192. friend class CCallback;
  193. friend class CPathfinder;;
  194. friend class CLuaCallback;
  195. friend class CClient;
  196. friend void initGameState(Mapa * map, CGameInfo * cgi);
  197. friend class CScriptCallback;
  198. friend class CMapHandler;
  199. friend class CGameHandler;
  200. };
  201. #endif //CGAMESTATE_H