CGameState.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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); //send pointer to at least 187 allocated bytes
  64. void getAccessibilityMapForTwoHex(bool *accessibility, bool atackerSide); //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. };
  69. class DLL_EXPORT CStack
  70. {
  71. public:
  72. ui32 ID; //unique ID of stack
  73. CCreature * creature;
  74. ui32 amount;
  75. ui32 firstHPleft; //HP of first creature in stack
  76. ui8 owner;
  77. ui8 attackerOwned; //if true, this stack is owned by attakcer (this one from left hand side of battle)
  78. ui16 position; //position on battlefield
  79. ui8 alive; //true if it is alive
  80. CStack(CCreature * C, int A, int O, int I, bool AO);
  81. CStack() : creature(NULL),amount(-1),owner(255), alive(true), position(-1), ID(-1), attackerOwned(true), firstHPleft(-1){};
  82. template <typename Handler> void save(Handler &h, const int version)
  83. {
  84. h & creature->idNumber;
  85. }
  86. template <typename Handler> void load(Handler &h, const int version)
  87. {
  88. ui32 id;
  89. h & id;
  90. creature = &VLC->creh->creatures[id];
  91. }
  92. template <typename Handler> void serialize(Handler &h, const int version)
  93. {
  94. h & ID & amount & firstHPleft & owner & attackerOwned & position & alive;
  95. if(h.saving)
  96. save(h,version);
  97. else
  98. load(h,version);
  99. }
  100. };
  101. class DLL_EXPORT CGameState
  102. {
  103. private:
  104. StartInfo* scenarioOps;
  105. ui32 seed;
  106. ui8 currentPlayer; //ID of player currently having turn
  107. BattleInfo *curB; //current battle
  108. ui32 day; //total number of days in game
  109. Mapa * map;
  110. std::map<ui8,PlayerState> players; //ID <-> playerstate
  111. std::map<int, CGDefInfo*> villages, forts, capitols; //def-info for town graphics
  112. boost::shared_mutex *mx;
  113. CGameState();
  114. ~CGameState();
  115. void init(StartInfo * si, Mapa * map, int Seed);
  116. void apply(IPack * pack);
  117. void randomizeObject(CGObjectInstance *cur);
  118. std::pair<int,int> pickObject(CGObjectInstance *obj);
  119. int pickHero(int owner);
  120. CGHeroInstance *getHero(int objid);
  121. bool battleMoveCreatureStack(int ID, int dest);
  122. bool battleAttackCreatureStack(int ID, int dest);
  123. bool battleShootCreatureStack(int ID, int dest);
  124. int battleGetStack(int pos); //returns ID of stack at given tile
  125. static int calculateDmg(const CStack* attacker, const CStack* defender); //TODO: add additional conditions and require necessary data
  126. std::vector<int> battleGetRange(int ID); //called by std::vector<int> CCallback::battleGetAvailableHexes(int ID);
  127. public:
  128. 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
  129. friend class CCallback;
  130. friend class CPathfinder;;
  131. friend class CLuaCallback;
  132. friend class CClient;
  133. friend void initGameState(Mapa * map, CGameInfo * cgi);
  134. friend class CScriptCallback;
  135. friend class CMapHandler;
  136. friend class CGameHandler;
  137. };
  138. #endif //CGAMESTATE_H