CGameHandler.h 6.9 KB

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