CGameHandler.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. CVCMIServer *s;
  56. std::map<int,CConnection*> connections; //player color -> connection to clinet with interface of that player
  57. PlayerStatuses states; //player color -> player state
  58. std::set<CConnection*> conns;
  59. void sendMessageTo(CConnection &c, std::string message);
  60. void giveSpells(const CGTownInstance *t, const CGHeroInstance *h);
  61. void moveStack(int stack, int dest);
  62. void startBattle(CCreatureSet army1, CCreatureSet army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, boost::function<void(BattleResult*)> cb); //use hero=NULL for no hero
  63. void prepareAttack(BattleAttack &bat, CStack *att, CStack *def); //if last parameter is true, attack is by shooting, if false it's a melee attack
  64. void prepareAttacked(BattleStackAttacked &bsa, CStack *def);
  65. void checkForBattleEnd( std::vector<CStack*> &stacks );
  66. void setupBattle( BattleInfo * curB, int3 tile, CCreatureSet &army1, CCreatureSet &army2, CGHeroInstance * hero1, CGHeroInstance * hero2 );
  67. public:
  68. CGameHandler(void);
  69. ~CGameHandler(void);
  70. //////////////////////////////////////////////////////////////////////////
  71. //from IGameCallback
  72. //get info
  73. int getCurrentPlayer();
  74. int getSelectedHero();
  75. //do sth
  76. void changeSpells(int hid, bool give, const std::set<ui32> &spells);
  77. void removeObject(int objid);
  78. void setBlockVis(int objid, bool bv);
  79. void setOwner(int objid, ui8 owner);
  80. void setHoverName(int objid, MetaString * name);
  81. void setObjProperty(int objid, int prop, int val);
  82. void changePrimSkill(int ID, int which, int val, bool abs=false);
  83. void changeSecSkill(int ID, int which, int val, bool abs=false);
  84. void showInfoDialog(InfoWindow *iw);
  85. void showYesNoDialog(YesNoDialog *iw, const CFunctionList<void(ui32)> &callback);
  86. void showSelectionDialog(SelectionDialog *iw, const CFunctionList<void(ui32)> &callback); //returns question id
  87. void giveResource(int player, int which, int val);
  88. void showCompInfo(ShowInInfobox * comp);
  89. void heroVisitCastle(int obj, int heroID);
  90. void stopHeroVisitCastle(int obj, int heroID);
  91. void giveHeroArtifact(int artid, int hid, int position); //pos==-1 - first free slot in backpack; pos==-2 - default if available or backpack
  92. 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
  93. void startBattleI(int heroID, CCreatureSet army, int3 tile, boost::function<void(BattleResult*)> cb); //for hero<=>neutral army
  94. void setAmount(int objid, ui32 val);
  95. void moveHero(int hid, int3 pos, bool instant);
  96. void giveHeroBonus(GiveBonus * bonus);
  97. void setMovePoints(SetMovePoints * smp);
  98. void setManaPoints(int hid, int val);
  99. //////////////////////////////////////////////////////////////////////////
  100. void init(StartInfo *si, int Seed);
  101. void handleConnection(std::set<int> players, CConnection &c);
  102. template <typename Handler> void serialize(Handler &h, const int version)
  103. {
  104. h & QID & states;
  105. }
  106. template <typename T> void applyAndAsk(Query<T> * sel, ui8 player, boost::function<void(ui32)> &callback)
  107. {
  108. gsm.lock();
  109. sel->id = QID;
  110. callbacks[QID] = callback;
  111. states.addQuery(player,QID);
  112. QID++;
  113. sendAndApply(sel);
  114. gsm.unlock();
  115. }
  116. template <typename T> void ask(Query<T> * sel, ui8 player, const CFunctionList<void(ui32)> &callback)
  117. {
  118. gsm.lock();
  119. sel->id = QID;
  120. callbacks[QID] = callback;
  121. states.addQuery(player,QID);
  122. sendToAllClients(sel);
  123. QID++;
  124. gsm.unlock();
  125. }
  126. template <typename T>void sendDataToClients(const T & data)
  127. {
  128. for(std::set<CConnection*>::iterator i=conns.begin(); i!=conns.end();i++)
  129. {
  130. (*i)->wmx->lock();
  131. **i << data;
  132. (*i)->wmx->unlock();
  133. }
  134. }
  135. template <typename T>void sendToAllClients(CPack<T> * info)
  136. {
  137. for(std::set<CConnection*>::iterator i=conns.begin(); i!=conns.end();i++)
  138. {
  139. (*i)->wmx->lock();
  140. **i << info->getType() << *info->This();
  141. (*i)->wmx->unlock();
  142. }
  143. }
  144. template <typename T>void sendAndApply(CPack<T> * info)
  145. {
  146. gs->apply(info);
  147. sendToAllClients(info);
  148. }
  149. void run(bool resume);
  150. void newTurn();
  151. friend class CVCMIServer;
  152. friend class CScriptCallback;
  153. };
  154. #endif // __CGAMEHANDLER_H__