CGameState.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 CScriptCallback;
  16. class CCallback;
  17. class CLuaCallback;
  18. class CCPPObjectScript;
  19. class CCreatureSet;
  20. class CStack;
  21. class CGHeroInstance;
  22. class CGTownInstance;
  23. class CArmedInstance;
  24. class CGDefInfo;
  25. class CObjectScript;
  26. class CGObjectInstance;
  27. class CCreature;
  28. struct Mapa;
  29. struct StartInfo;
  30. struct SDL_Surface;
  31. class CMapHandler;
  32. class CPathfinder;
  33. struct IPack;
  34. namespace boost
  35. {
  36. class shared_mutex;
  37. }
  38. struct DLL_EXPORT PlayerState
  39. {
  40. public:
  41. ui8 color, serial;
  42. std::vector<std::vector<std::vector<ui8> > > fogOfWarMap; //true - visible, false - hidden
  43. std::vector<si32> resources;
  44. std::vector<CGHeroInstance *> heroes;
  45. std::vector<CGTownInstance *> towns;
  46. PlayerState():color(-1){};
  47. };
  48. struct DLL_EXPORT BattleInfo
  49. {
  50. ui8 side1, side2;
  51. si32 round, activeStack;
  52. ui8 siege; // = 0 ordinary battle = 1 a siege with a Fort = 2 a siege with a Citadel = 3 a siege with a Castle
  53. int3 tile; //for background and bonuses
  54. si32 hero1, hero2;
  55. CCreatureSet army1, army2;
  56. std::vector<CStack*> stacks;
  57. template <typename Handler> void serialize(Handler &h, const int version)
  58. {
  59. h & side1 & side2 & round & activeStack & siege & tile & stacks & army1 & army2 & hero1 & hero2;
  60. }
  61. CStack * getStack(int stackID);
  62. CStack * getStackT(int tileID);
  63. void getAccessibilityMap(bool *accessibility, int stackToOmmit=-1); //send pointer to at least 187 allocated bytes
  64. void getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide, int stackToOmmit=-1); //send pointer to at least 187 allocated bytes
  65. 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
  66. std::vector<int> getPath(int start, int dest, bool*accessibility);
  67. std::vector<int> getAccessibility(int stackID); //returns vector of accessible tiles (taking into account the creature range)
  68. 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)
  69. static std::vector<int> neighbouringTiles(int hex);
  70. static int calculateDmg(const CStack* attacker, const CStack* defender); //TODO: add additional conditions and require necessary data
  71. };
  72. class DLL_EXPORT CStack
  73. {
  74. public:
  75. ui32 ID; //unique ID of stack
  76. CCreature * creature;
  77. ui32 amount;
  78. ui32 firstHPleft; //HP of first creature in stack
  79. ui8 owner;
  80. ui8 attackerOwned; //if true, this stack is owned by attakcer (this one from left hand side of battle)
  81. ui16 position; //position on battlefield
  82. ui8 alive; //true if it is alive
  83. CStack(CCreature * C, int A, int O, int I, bool AO);
  84. CStack() : creature(NULL),amount(-1),owner(255), alive(true), position(-1), ID(-1), attackerOwned(true), firstHPleft(-1){};
  85. template <typename Handler> void save(Handler &h, const int version)
  86. {
  87. h & creature->idNumber;
  88. }
  89. template <typename Handler> void load(Handler &h, const int version)
  90. {
  91. ui32 id;
  92. h & id;
  93. creature = &VLC->creh->creatures[id];
  94. }
  95. template <typename Handler> void serialize(Handler &h, const int version)
  96. {
  97. h & ID & amount & firstHPleft & owner & attackerOwned & position & alive;
  98. if(h.saving)
  99. save(h,version);
  100. else
  101. load(h,version);
  102. }
  103. };
  104. struct UpgradeInfo
  105. {
  106. int oldID; //creature to be upgraded
  107. std::vector<int> newID; //possible upgrades
  108. std::vector<std::set<std::pair<int,int> > > cost; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>
  109. UpgradeInfo(){oldID = -1;};
  110. };
  111. class DLL_EXPORT CGameState
  112. {
  113. private:
  114. StartInfo* scenarioOps;
  115. ui32 seed;
  116. ui8 currentPlayer; //ID of player currently having turn
  117. BattleInfo *curB; //current battle
  118. ui32 day; //total number of days in game
  119. Mapa * map;
  120. std::map<ui8,PlayerState> players; //ID <-> playerstate
  121. std::map<int, CGDefInfo*> villages, forts, capitols; //def-info for town graphics
  122. boost::shared_mutex *mx;
  123. CGameState();
  124. ~CGameState();
  125. void init(StartInfo * si, Mapa * map, int Seed);
  126. void applyNL(IPack * pack);
  127. void apply(IPack * pack);
  128. void randomizeObject(CGObjectInstance *cur);
  129. std::pair<int,int> pickObject(CGObjectInstance *obj);
  130. int pickHero(int owner);
  131. CGHeroInstance *getHero(int objid);
  132. CGTownInstance *getTown(int objid);
  133. bool battleMoveCreatureStack(int ID, int dest);
  134. bool battleAttackCreatureStack(int ID, int dest);
  135. bool battleShootCreatureStack(int ID, int dest);
  136. int battleGetStack(int pos); //returns ID of stack at given tile
  137. UpgradeInfo getUpgradeInfo(CArmedInstance *obj, int stackPos);
  138. public:
  139. 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
  140. friend class CCallback;
  141. friend class CPathfinder;;
  142. friend class CLuaCallback;
  143. friend class CClient;
  144. friend void initGameState(Mapa * map, CGameInfo * cgi);
  145. friend class CScriptCallback;
  146. friend class CMapHandler;
  147. friend class CGameHandler;
  148. };
  149. #endif //CGAMESTATE_H