CGameHandler.h 3.2 KB

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