CGameHandler.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #ifndef __CGAMEHANDLER_H__
  2. #define __CGAMEHANDLER_H__
  3. #include "../global.h"
  4. #include <set>
  5. #include "../client/FunctionList.h"
  6. #include "../CGameState.h"
  7. #include "../lib/Connection.h"
  8. #include "../lib/IGameCallback.h"
  9. #include "../lib/BattleAction.h"
  10. #include <boost/function.hpp>
  11. #include <boost/thread.hpp>
  12. class CVCMIServer;
  13. class CGameState;
  14. struct StartInfo;
  15. class CCPPObjectScript;
  16. class CScriptCallback;
  17. struct BattleResult;
  18. struct BattleAttack;
  19. struct BattleStackAttacked;
  20. struct CPack;
  21. struct Query;
  22. class CGHeroInstance;
  23. extern std::map<ui32, CFunctionList<void(ui32)> > callbacks; //question id => callback functions - for selection dialogs
  24. extern boost::mutex gsm;
  25. struct PlayerStatus
  26. {
  27. bool makingTurn, engagedIntoBattle;
  28. std::set<ui32> queries;
  29. PlayerStatus():makingTurn(false),engagedIntoBattle(false){};
  30. template <typename Handler> void serialize(Handler &h, const int version)
  31. {
  32. h & makingTurn & engagedIntoBattle & queries;
  33. }
  34. };
  35. class PlayerStatuses
  36. {
  37. public:
  38. std::map<ui8,PlayerStatus> players;
  39. boost::mutex mx;
  40. boost::condition_variable cv; //notifies when any changes are made
  41. void addPlayer(ui8 player);
  42. PlayerStatus operator[](ui8 player);
  43. bool hasQueries(ui8 player);
  44. bool checkFlag(ui8 player, bool PlayerStatus::*flag);
  45. void setFlag(ui8 player, bool PlayerStatus::*flag, bool val);
  46. void addQuery(ui8 player, ui32 id);
  47. void removeQuery(ui8 player, ui32 id);
  48. template <typename Handler> void serialize(Handler &h, const int version)
  49. {
  50. h & players;
  51. }
  52. };
  53. class CGameHandler : public IGameCallback
  54. {
  55. public:
  56. CVCMIServer *s;
  57. std::map<int,CConnection*> connections; //player color -> connection to client with interface of that player
  58. PlayerStatuses states; //player color -> player state
  59. std::set<CConnection*> conns;
  60. //queries stuff
  61. boost::recursive_mutex gsm;
  62. ui32 QID;
  63. std::map<ui32, CFunctionList<void(ui32)> > callbacks; //query id => callback function - for selection and yes/no dialogs
  64. std::map<ui32, boost::function<void()> > garrisonCallbacks; //query id => callback - for garrison dialogs
  65. std::map<ui32, std::pair<si32,si32> > allowedExchanges;
  66. bool isAllowedExchange(int id1, int id2);
  67. void giveSpells(const CGTownInstance *t, const CGHeroInstance *h);
  68. void moveStack(int stack, int dest);
  69. void startBattle(CCreatureSet army1, CCreatureSet army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, boost::function<void(BattleResult*)> cb); //use hero=NULL for no hero
  70. void prepareAttack(BattleAttack &bat, CStack *att, CStack *def); //if last parameter is true, attack is by shooting, if false it's a melee attack
  71. void prepareAttacked(BattleStackAttacked &bsa, CStack *def);
  72. void checkForBattleEnd( std::vector<CStack*> &stacks );
  73. void setupBattle( BattleInfo * curB, int3 tile, CCreatureSet &army1, CCreatureSet &army2, CGHeroInstance * hero1, CGHeroInstance * hero2 );
  74. CGameHandler(void);
  75. ~CGameHandler(void);
  76. //////////////////////////////////////////////////////////////////////////
  77. //from IGameCallback
  78. //get info
  79. int getCurrentPlayer();
  80. int getSelectedHero();
  81. //do sth
  82. void changeSpells(int hid, bool give, const std::set<ui32> &spells);
  83. void removeObject(int objid);
  84. void setBlockVis(int objid, bool bv);
  85. void setOwner(int objid, ui8 owner);
  86. void setHoverName(int objid, MetaString * name);
  87. void setObjProperty(int objid, int prop, int val);
  88. void changePrimSkill(int ID, int which, int val, bool abs=false);
  89. void changeSecSkill(int ID, int which, int val, bool abs=false);
  90. void showInfoDialog(InfoWindow *iw);
  91. void showBlockingDialog(BlockingDialog *iw, const CFunctionList<void(ui32)> &callback);
  92. ui32 showBlockingDialog(BlockingDialog *iw); //synchronous version of above
  93. void showGarrisonDialog(int upobj, int hid, const boost::function<void()> &cb);
  94. void giveResource(int player, int which, int val);
  95. void showCompInfo(ShowInInfobox * comp);
  96. void heroVisitCastle(int obj, int heroID);
  97. void stopHeroVisitCastle(int obj, int heroID);
  98. void giveHeroArtifact(int artid, int hid, int position); //pos==-1 - first free slot in backpack; pos==-2 - default if available or backpack
  99. void moveArtifact(int hid, int oldPosition, int destPos);
  100. void removeArtifact(int hid, int pos);
  101. void startBattleI(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, boost::function<void(BattleResult*)> cb); //use hero=NULL for no hero
  102. void startBattleI(int heroID, CCreatureSet army, int3 tile, boost::function<void(BattleResult*)> cb); //for hero<=>neutral army
  103. void setAmount(int objid, ui32 val);
  104. void moveHero(si32 hid, int3 dst, ui8 instant, ui8 asker = 255);
  105. void giveHeroBonus(GiveBonus * bonus);
  106. void setMovePoints(SetMovePoints * smp);
  107. void setManaPoints(int hid, int val);
  108. void giveHero(int id, int player);
  109. void changeObjPos(int objid, int3 newPos, ui8 flags);
  110. //////////////////////////////////////////////////////////////////////////
  111. void init(StartInfo *si, int Seed);
  112. void handleConnection(std::set<int> players, CConnection &c);
  113. int getPlayerAt(CConnection *c) const;
  114. void playerMessage( ui8 player, const std::string &message);
  115. void makeBattleAction(BattleAction &ba);
  116. void makeCustomAction(BattleAction &ba);
  117. void queryReply( ui32 qid, ui32 answer );
  118. void hireHero( ui32 tid, ui8 hid );
  119. void setFormation( si32 hid, ui8 formation );
  120. void tradeResources( ui32 val, ui8 player, ui32 id1, ui32 id2 );
  121. void buyArtifact( ui32 hid, si32 aid );
  122. void swapArtifacts( si32 hid1, si32 hid2, ui16 slot1, ui16 slot2 );
  123. void garrisonSwap(si32 tid);
  124. void upgradeCreature( ui32 objid, ui8 pos, ui32 upgID );
  125. void recruitCreatures(si32 objid, ui32 crid, ui32 cram);
  126. void buildStructure(si32 tid, si32 bid);
  127. void disbandCreature( si32 id, ui8 pos );
  128. void arrangeStacks( si32 id1, si32 id2, ui8 what, ui8 p1, ui8 p2, si32 val );
  129. void save(const std::string &fname);
  130. void close();
  131. void handleTimeEvents();
  132. bool complain(const std::string &problem); //sends message to all clients, prints on the logs and return true
  133. template <typename Handler> void serialize(Handler &h, const int version)
  134. {
  135. h & QID & states;
  136. }
  137. ui32 getQueryResult(ui8 player, int queryID);
  138. void sendMessageToAll(const std::string &message);
  139. void sendMessageTo(CConnection &c, const std::string &message);
  140. void applyAndAsk(Query * sel, ui8 player, boost::function<void(ui32)> &callback);
  141. void ask(Query * sel, ui8 player, const CFunctionList<void(ui32)> &callback);
  142. void sendToAllClients(CPackForClient * info);
  143. void sendAndApply(CPackForClient * info);
  144. void run(bool resume);
  145. void newTurn();
  146. friend class CVCMIServer;
  147. friend class CScriptCallback;
  148. };
  149. #endif // __CGAMEHANDLER_H__