CGameState.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef CGAMESTATE_H
  2. #define CGAMESTATE_H
  3. #include "global.h"
  4. #include "../hch/CCreatureHandler.h"
  5. #include "lib/VCMI_Lib.h"
  6. #include <set>
  7. #include <vector>
  8. #ifdef _WIN32
  9. #include <tchar.h>
  10. #else
  11. #include "tchar_amigaos4.h"
  12. #endif
  13. class CScriptCallback;
  14. class CCallback;
  15. class CLuaCallback;
  16. class CCPPObjectScript;
  17. class CCreatureSet;
  18. class CStack;
  19. class CGHeroInstance;
  20. class CGTownInstance;
  21. class CArmedInstance;
  22. class CGDefInfo;
  23. class CObjectScript;
  24. class CGObjectInstance;
  25. class CCreature;
  26. struct Mapa;
  27. struct StartInfo;
  28. struct SDL_Surface;
  29. class CMapHandler;
  30. class CPathfinder;
  31. struct IPack;
  32. namespace boost
  33. {
  34. class shared_mutex;
  35. }
  36. struct DLL_EXPORT PlayerState
  37. {
  38. public:
  39. ui8 color, serial;
  40. std::vector<std::vector<std::vector<ui8> > > fogOfWarMap; //true - visible, false - hidden
  41. std::vector<si32> resources;
  42. std::vector<CGHeroInstance *> heroes;
  43. std::vector<CGTownInstance *> towns;
  44. PlayerState():color(-1){};
  45. };
  46. struct DLL_EXPORT BattleInfo
  47. {
  48. ui8 side1, side2;
  49. si32 round, activeStack;
  50. ui8 siege; // = 0 ordinary battle = 1 a siege with a Fort = 2 a siege with a Citadel = 3 a siege with a Castle
  51. int3 tile; //for background and bonuses
  52. si32 hero1, hero2;
  53. CCreatureSet army1, army2;
  54. std::vector<CStack*> stacks;
  55. template <typename Handler> void serialize(Handler &h, const int version)
  56. {
  57. h & side1 & side2 & round & activeStack & siege & tile & stacks & army1 & army2 & hero1 & hero2;
  58. }
  59. };
  60. class DLL_EXPORT CStack
  61. {
  62. public:
  63. ui32 ID; //unique ID of stack
  64. CCreature * creature;
  65. ui32 amount;
  66. ui32 firstHPleft; //HP of first creature in stack
  67. ui8 owner;
  68. ui8 attackerOwned; //if true, this stack is owned by attakcer (this one from left hand side of battle)
  69. ui16 position; //position on battlefield
  70. ui8 alive; //true if it is alive
  71. CStack(CCreature * C, int A, int O, int I, bool AO);
  72. CStack() : creature(NULL),amount(-1),owner(255), alive(true), position(-1), ID(-1), attackerOwned(true), firstHPleft(-1){};
  73. template <typename Handler> void save(Handler &h, const int version)
  74. {
  75. h & creature->idNumber;
  76. }
  77. template <typename Handler> void load(Handler &h, const int version)
  78. {
  79. ui32 id;
  80. h & id;
  81. creature = &VLC->creh->creatures[id];
  82. }
  83. template <typename Handler> void serialize(Handler &h, const int version)
  84. {
  85. h & ID & amount & firstHPleft & owner & attackerOwned & position & alive;
  86. if(h.saving)
  87. save(h,version);
  88. else
  89. load(h,version);
  90. }
  91. };
  92. class DLL_EXPORT CGameState
  93. {
  94. private:
  95. StartInfo* scenarioOps;
  96. ui32 seed;
  97. ui8 currentPlayer; //ID of player currently having turn
  98. BattleInfo *curB; //current battle
  99. ui32 day; //total number of days in game
  100. Mapa * map;
  101. std::map<ui8,PlayerState> players; //ID <-> playerstate
  102. std::map<int, CGDefInfo*> villages, forts, capitols; //def-info for town graphics
  103. boost::shared_mutex *mx;
  104. CGameState();
  105. ~CGameState();
  106. void init(StartInfo * si, Mapa * map, int Seed);
  107. void apply(IPack * pack);
  108. void randomizeObject(CGObjectInstance *cur);
  109. std::pair<int,int> pickObject(CGObjectInstance *obj);
  110. int pickHero(int owner);
  111. CGHeroInstance *getHero(int objid);
  112. void battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CArmedInstance *hero1, CArmedInstance *hero2);
  113. bool battleMoveCreatureStack(int ID, int dest);
  114. bool battleAttackCreatureStack(int ID, int dest);
  115. bool battleShootCreatureStack(int ID, int dest);
  116. int battleGetStack(int pos); //returns ID of stack at given tile
  117. static int calculateDmg(const CStack* attacker, const CStack* defender); //TODO: add additional conditions and require necessary data
  118. std::vector<int> battleGetRange(int ID); //called by std::vector<int> CCallback::battleGetAvailableHexes(int ID);
  119. public:
  120. 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
  121. friend class CCallback;
  122. friend class CPathfinder;;
  123. friend class CLuaCallback;
  124. friend class CClient;
  125. friend void initGameState(Mapa * map, CGameInfo * cgi);
  126. friend class CScriptCallback;
  127. friend class CMapHandler;
  128. friend class CGameHandler;
  129. };
  130. #endif //CGAMESTATE_H