CGameHandler.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #pragma once
  2. #include "../global.h"
  3. #include <set>
  4. #include "../CGameState.h"
  5. #include "../lib/Connection.h"
  6. #ifndef _MSC_VER
  7. #include <boost/thread.hpp>
  8. #endif
  9. class CVCMIServer;
  10. class CGameState;
  11. //class CConnection;
  12. struct StartInfo;
  13. class CCPPObjectScript;
  14. class CScriptCallback;
  15. template <typename T> struct CPack;
  16. class CGHeroInstance;
  17. class CGameHandler
  18. {
  19. CGameState *gs;
  20. std::set<CCPPObjectScript *> cppscripts; //C++ scripts
  21. //std::map<int, std::map<std::string, CObjectScript*> > objscr; //non-C++ scripts
  22. CVCMIServer *s;
  23. std::map<int,CConnection*> connections; //player color -> connection to clinet with interface of that player
  24. std::map<int,int> states; //player color -> player state
  25. std::set<CConnection*> conns;
  26. void handleCPPObjS(std::map<int,CCPPObjectScript*> * mapa, CCPPObjectScript * script);
  27. void changePrimSkill(int ID, int which, int val, bool abs=false);
  28. void moveStack(int stack, int dest);
  29. void startBattle(CCreatureSet army1, CCreatureSet army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2); //use hero=NULL for no hero
  30. public:
  31. CGameHandler(void);
  32. ~CGameHandler(void);
  33. void init(StartInfo *si, int Seed);
  34. void handleConnection(std::set<int> players, CConnection &c);
  35. template <typename T>void sendToAllClients(CPack<T> * info)
  36. {
  37. for(std::set<CConnection*>::iterator i=conns.begin(); i!=conns.end();i++)
  38. {
  39. (*i)->wmx->lock();
  40. **i << info->getType() << *info->This();
  41. (*i)->wmx->unlock();
  42. }
  43. }
  44. template <typename T>void sendAndApply(CPack<T> * info)
  45. {
  46. gs->apply(info);
  47. sendToAllClients(info);
  48. }
  49. void run();
  50. void newTurn();
  51. friend class CVCMIServer;
  52. friend class CScriptCallback;
  53. };