CGameHandler.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #ifndef __CGAMEHANDLER_H__
  2. #define __CGAMEHANDLER_H__
  3. #include "../global.h"
  4. #include <set>
  5. #include "../client/FunctionList.h"
  6. #include "../CGameState.h"
  7. #include "../lib/Connection.h"
  8. #include "../lib/IGameCallback.h"
  9. #include <boost/function.hpp>
  10. #include <boost/thread.hpp>
  11. class CVCMIServer;
  12. class CGameState;
  13. struct StartInfo;
  14. class CCPPObjectScript;
  15. class CScriptCallback;
  16. struct BattleResult;
  17. struct BattleAttack;
  18. struct BattleStackAttacked;
  19. template <typename T> struct CPack;
  20. template <typename T> struct Query;
  21. class CGHeroInstance;
  22. extern std::map<ui32, CFunctionList<void(ui32)> > callbacks; //question id => callback functions - for selection dialogs
  23. extern boost::mutex gsm;
  24. struct PlayerStatus
  25. {
  26. bool makingTurn, engagedIntoBattle;
  27. std::set<ui32> queries;
  28. PlayerStatus():makingTurn(false),engagedIntoBattle(false){};
  29. template <typename Handler> void serialize(Handler &h, const int version)
  30. {
  31. h & makingTurn & engagedIntoBattle & queries;
  32. }
  33. };
  34. class PlayerStatuses
  35. {
  36. public:
  37. std::map<ui8,PlayerStatus> players;
  38. boost::mutex mx;
  39. boost::condition_variable cv; //notifies when any changes are made
  40. void addPlayer(ui8 player);
  41. PlayerStatus operator[](ui8 player);
  42. bool hasQueries(ui8 player);
  43. bool checkFlag(ui8 player, bool PlayerStatus::*flag);
  44. void setFlag(ui8 player, bool PlayerStatus::*flag, bool val);
  45. void addQuery(ui8 player, ui32 id);
  46. void removeQuery(ui8 player, ui32 id);
  47. template <typename Handler> void serialize(Handler &h, const int version)
  48. {
  49. h & players;
  50. }
  51. };
  52. class CGameHandler : public IGameCallback
  53. {
  54. static ui32 QID;
  55. CGameState *gs;
  56. //std::set<CCPPObjectScript *> cppscripts; //C++ scripts
  57. //std::map<int, std::map<std::string, CObjectScript*> > objscr; //non-C++ scripts
  58. CVCMIServer *s;
  59. std::map<int,CConnection*> connections; //player color -> connection to clinet with interface of that player
  60. PlayerStatuses states; //player color -> player state
  61. std::set<CConnection*> conns;
  62. void changeSecSkill(int ID, ui16 which, int val, bool abs=false);
  63. void giveSpells(const CGTownInstance *t, const CGHeroInstance *h);
  64. void moveStack(int stack, int dest);
  65. void startBattle(CCreatureSet army1, CCreatureSet army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, boost::function<void(BattleResult*)> cb); //use hero=NULL for no hero
  66. void prepareAttack(BattleAttack &bat, CStack *att, CStack *def); //if last parameter is true, attack is by shooting, if false it's a melee attack
  67. void prepareAttacked(BattleStackAttacked &bsa, CStack *def);
  68. void checkForBattleEnd( std::vector<CStack*> &stacks );
  69. void setupBattle( BattleInfo * curB, int3 tile, CCreatureSet &army1, CCreatureSet &army2, CGHeroInstance * hero1, CGHeroInstance * hero2 );
  70. public:
  71. CGameHandler(void);
  72. ~CGameHandler(void);
  73. //////////////////////////////////////////////////////////////////////////
  74. //from IGameCallback
  75. //get info
  76. int getOwner(int heroID);
  77. int getResource(int player, int which);
  78. int getSelectedHero();
  79. int getDate(int mode=0);
  80. const CGObjectInstance* getObj(int objid);
  81. const CGHeroInstance* getHero(int objid);
  82. const CGTownInstance* getTown(int objid);
  83. const CGHeroInstance* getSelectedHero(int player); //NULL if no hero is selected
  84. int getCurrentPlayer();
  85. //do sth
  86. void changeSpells(int hid, bool give, const std::set<ui32> &spells);
  87. void removeObject(int objid);
  88. void setBlockVis(int objid, bool bv);
  89. void setOwner(int objid, ui8 owner);
  90. void setHoverName(int objid, MetaString * name);
  91. void changePrimSkill(int ID, int which, int val, bool abs=false);
  92. void showInfoDialog(InfoWindow *iw);
  93. void showYesNoDialog(YesNoDialog *iw, const CFunctionList<void(ui32)> &callback);
  94. void showSelectionDialog(SelectionDialog *iw, const CFunctionList<void(ui32)> &callback); //returns question id
  95. void giveResource(int player, int which, int val);
  96. void showCompInfo(ShowInInfobox * comp);
  97. void heroVisitCastle(int obj, int heroID);
  98. void stopHeroVisitCastle(int obj, int heroID);
  99. void giveHeroArtifact(int artid, int hid, int position); //pos==-1 - first free slot in backpack; pos==-2 - default if available or backpack
  100. void startBattleI(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, boost::function<void(BattleResult*)> cb); //use hero=NULL for no hero
  101. void startBattleI(int heroID, CCreatureSet army, int3 tile, boost::function<void(BattleResult*)> cb); //for hero<=>neutral army
  102. void setAmount(int objid, ui32 val);
  103. void moveHero(int hid, int3 pos, bool instant);
  104. //////////////////////////////////////////////////////////////////////////
  105. void init(StartInfo *si, int Seed);
  106. void handleConnection(std::set<int> players, CConnection &c);
  107. template <typename Handler> void serialize(Handler &h, const int version)
  108. {
  109. h & QID & gs & states;
  110. }
  111. template <typename T> void applyAndAsk(Query<T> * sel, ui8 player, boost::function<void(ui32)> &callback)
  112. {
  113. gsm.lock();
  114. sel->id = QID;
  115. callbacks[QID] = callback;
  116. states.addQuery(player,QID);
  117. QID++;
  118. sendAndApply(sel);
  119. gsm.unlock();
  120. }
  121. template <typename T> void ask(Query<T> * sel, ui8 player, const CFunctionList<void(ui32)> &callback)
  122. {
  123. gsm.lock();
  124. sel->id = QID;
  125. callbacks[QID] = callback;
  126. states.addQuery(player,QID);
  127. sendToAllClients(sel);
  128. QID++;
  129. gsm.unlock();
  130. }
  131. template <typename T>void sendDataToClients(const T & data)
  132. {
  133. for(std::set<CConnection*>::iterator i=conns.begin(); i!=conns.end();i++)
  134. {
  135. (*i)->wmx->lock();
  136. **i << data;
  137. (*i)->wmx->unlock();
  138. }
  139. }
  140. template <typename T>void sendToAllClients(CPack<T> * info)
  141. {
  142. for(std::set<CConnection*>::iterator i=conns.begin(); i!=conns.end();i++)
  143. {
  144. (*i)->wmx->lock();
  145. **i << info->getType() << *info->This();
  146. (*i)->wmx->unlock();
  147. }
  148. }
  149. template <typename T>void sendAndApply(CPack<T> * info)
  150. {
  151. gs->apply(info);
  152. sendToAllClients(info);
  153. }
  154. void run();
  155. void newTurn();
  156. friend class CVCMIServer;
  157. friend class CScriptCallback;
  158. };
  159. #endif // __CGAMEHANDLER_H__