CGameHandler.h 3.8 KB

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