CGameHandler.h 5.6 KB

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