CGameHandler.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. template <typename T> struct CPack;
  16. template <typename T> struct Query;
  17. class CGHeroInstance;
  18. extern std::map<ui32, CFunctionList<void(ui32)> > callbacks; //question id => callback functions - for selection dialogs
  19. extern boost::mutex gsm;
  20. struct PlayerStatus
  21. {
  22. bool makingTurn, engagedIntoBattle;
  23. std::set<ui32> queries;
  24. PlayerStatus():makingTurn(false),engagedIntoBattle(false){};
  25. };
  26. class PlayerStatuses
  27. {
  28. public:
  29. std::map<ui8,PlayerStatus> players;
  30. boost::mutex mx;
  31. boost::condition_variable cv; //notifies when any changes are made
  32. void addPlayer(ui8 player);
  33. PlayerStatus operator[](ui8 player);
  34. bool hasQueries(ui8 player);
  35. bool checkFlag(ui8 player, bool PlayerStatus::*flag);
  36. void setFlag(ui8 player, bool PlayerStatus::*flag, bool val);
  37. void addQuery(ui8 player, ui32 id);
  38. void removeQuery(ui8 player, ui32 id);
  39. };
  40. class CGameHandler
  41. {
  42. static ui32 QID;
  43. CGameState *gs;
  44. std::set<CCPPObjectScript *> cppscripts; //C++ scripts
  45. //std::map<int, std::map<std::string, CObjectScript*> > objscr; //non-C++ scripts
  46. CVCMIServer *s;
  47. std::map<int,CConnection*> connections; //player color -> connection to clinet with interface of that player
  48. PlayerStatuses states; //player color -> player state
  49. std::set<CConnection*> conns;
  50. void handleCPPObjS(std::map<int,CCPPObjectScript*> * mapa, CCPPObjectScript * script);
  51. void changePrimSkill(int ID, int which, int val, bool abs=false);
  52. void changeSecSkill(int ID, ui16 which, int val, bool abs=false);
  53. void giveSpells(const CGTownInstance *t, const CGHeroInstance *h);
  54. void moveStack(int stack, int dest);
  55. void startBattle(CCreatureSet army1, CCreatureSet army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, boost::function<void(BattleResult*)> cb); //use hero=NULL for no hero
  56. void checkForBattleEnd( std::vector<CStack*> &stacks );
  57. void setupBattle( BattleInfo * curB, int3 tile, CCreatureSet &army1, CCreatureSet &army2, CGHeroInstance * hero1, CGHeroInstance * hero2 );
  58. public:
  59. CGameHandler(void);
  60. ~CGameHandler(void);
  61. void init(StartInfo *si, int Seed);
  62. void handleConnection(std::set<int> players, CConnection &c);
  63. template <typename T> void applyAndAsk(Query<T> * sel, ui8 player, boost::function<void(ui32)> &callback)
  64. {
  65. gsm.lock();
  66. sel->id = QID;
  67. callbacks[QID] = callback;
  68. states.addQuery(player,QID);
  69. QID++;
  70. sendAndApply(sel);
  71. gsm.unlock();
  72. }
  73. template <typename T> void ask(Query<T> * sel, ui8 player, const CFunctionList<void(ui32)> &callback)
  74. {
  75. gsm.lock();
  76. sel->id = QID;
  77. callbacks[QID] = callback;
  78. states.addQuery(player,QID);
  79. sendToAllClients(sel);
  80. QID++;
  81. gsm.unlock();
  82. }
  83. template <typename T>void sendDataToClients(const T & data)
  84. {
  85. for(std::set<CConnection*>::iterator i=conns.begin(); i!=conns.end();i++)
  86. {
  87. (*i)->wmx->lock();
  88. **i << data;
  89. (*i)->wmx->unlock();
  90. }
  91. }
  92. template <typename T>void sendToAllClients(CPack<T> * info)
  93. {
  94. for(std::set<CConnection*>::iterator i=conns.begin(); i!=conns.end();i++)
  95. {
  96. (*i)->wmx->lock();
  97. **i << info->getType() << *info->This();
  98. (*i)->wmx->unlock();
  99. }
  100. }
  101. template <typename T>void sendAndApply(CPack<T> * info)
  102. {
  103. gs->apply(info);
  104. sendToAllClients(info);
  105. }
  106. void run();
  107. void newTurn();
  108. friend class CVCMIServer;
  109. friend class CScriptCallback;
  110. };