CGameHandler.h 3.9 KB

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