CGameHandler.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #pragma once
  2. #include "../global.h"
  3. #include <set>
  4. #include "../client/FunctionList.h"
  5. #include "../CGameState.h"
  6. #include "../lib/Connection.h"
  7. #include <boost/function.hpp>
  8. #include <boost/thread.hpp>
  9. class CVCMIServer;
  10. class CGameState;
  11. struct StartInfo;
  12. class CCPPObjectScript;
  13. class CScriptCallback;
  14. struct BattleResult;
  15. struct BattleAttack;
  16. struct BattleStackAttacked;
  17. template <typename T> struct CPack;
  18. template <typename T> struct Query;
  19. class CGHeroInstance;
  20. extern std::map<ui32, CFunctionList<void(ui32)> > callbacks; //question id => callback functions - for selection dialogs
  21. extern boost::mutex gsm;
  22. struct PlayerStatus
  23. {
  24. bool makingTurn, engagedIntoBattle;
  25. std::set<ui32> queries;
  26. PlayerStatus():makingTurn(false),engagedIntoBattle(false){};
  27. template <typename Handler> void serialize(Handler &h, const int version)
  28. {
  29. h & makingTurn & engagedIntoBattle & queries;
  30. }
  31. };
  32. class PlayerStatuses
  33. {
  34. public:
  35. std::map<ui8,PlayerStatus> players;
  36. boost::mutex mx;
  37. boost::condition_variable cv; //notifies when any changes are made
  38. void addPlayer(ui8 player);
  39. PlayerStatus operator[](ui8 player);
  40. bool hasQueries(ui8 player);
  41. bool checkFlag(ui8 player, bool PlayerStatus::*flag);
  42. void setFlag(ui8 player, bool PlayerStatus::*flag, bool val);
  43. void addQuery(ui8 player, ui32 id);
  44. void removeQuery(ui8 player, ui32 id);
  45. template <typename Handler> void serialize(Handler &h, const int version)
  46. {
  47. h & players;
  48. }
  49. };
  50. class CGameHandler
  51. {
  52. static ui32 QID;
  53. CGameState *gs;
  54. std::set<CCPPObjectScript *> cppscripts; //C++ scripts
  55. //std::map<int, std::map<std::string, CObjectScript*> > objscr; //non-C++ scripts
  56. CVCMIServer *s;
  57. std::map<int,CConnection*> connections; //player color -> connection to clinet with interface of that player
  58. PlayerStatuses states; //player color -> player state
  59. std::set<CConnection*> conns;
  60. void handleCPPObjS(std::map<int,CCPPObjectScript*> * mapa, CCPPObjectScript * script);
  61. void changePrimSkill(int ID, int which, int val, bool abs=false);
  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. void init(StartInfo *si, int Seed);
  74. void handleConnection(std::set<int> players, CConnection &c);
  75. template <typename Handler> void serialize(Handler &h, const int version)
  76. {
  77. h & QID & gs & cppscripts & states;
  78. }
  79. template <typename T> void applyAndAsk(Query<T> * sel, ui8 player, boost::function<void(ui32)> &callback)
  80. {
  81. gsm.lock();
  82. sel->id = QID;
  83. callbacks[QID] = callback;
  84. states.addQuery(player,QID);
  85. QID++;
  86. sendAndApply(sel);
  87. gsm.unlock();
  88. }
  89. template <typename T> void ask(Query<T> * sel, ui8 player, const CFunctionList<void(ui32)> &callback)
  90. {
  91. gsm.lock();
  92. sel->id = QID;
  93. callbacks[QID] = callback;
  94. states.addQuery(player,QID);
  95. sendToAllClients(sel);
  96. QID++;
  97. gsm.unlock();
  98. }
  99. template <typename T>void sendDataToClients(const T & data)
  100. {
  101. for(std::set<CConnection*>::iterator i=conns.begin(); i!=conns.end();i++)
  102. {
  103. (*i)->wmx->lock();
  104. **i << data;
  105. (*i)->wmx->unlock();
  106. }
  107. }
  108. template <typename T>void sendToAllClients(CPack<T> * info)
  109. {
  110. for(std::set<CConnection*>::iterator i=conns.begin(); i!=conns.end();i++)
  111. {
  112. (*i)->wmx->lock();
  113. **i << info->getType() << *info->This();
  114. (*i)->wmx->unlock();
  115. }
  116. }
  117. template <typename T>void sendAndApply(CPack<T> * info)
  118. {
  119. gs->apply(info);
  120. sendToAllClients(info);
  121. }
  122. void run();
  123. void newTurn();
  124. friend class CVCMIServer;
  125. friend class CScriptCallback;
  126. };