CGameState.h 4.1 KB

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