CGameState.h 6.8 KB

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