CGameHandler.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #ifndef __CGAMEHANDLER_H__
  2. #define __CGAMEHANDLER_H__
  3. #include "../global.h"
  4. #include <set>
  5. #include "../client/FunctionList.h"
  6. #include "../lib/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. int moveStack(int stack, int dest); //returned value - travelled distance
  78. void startBattle(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank, boost::function<void(BattleResult*)> cb, const CGTownInstance *town = NULL); //use hero=NULL for no hero
  79. void prepareAttack(BattleAttack &bat, CStack *att, CStack *def, int distance); //distance - number of hexes travelled before attacking
  80. void prepareAttacked(BattleStackAttacked &bsa, CStack *def);
  81. void checkForBattleEnd( std::vector<CStack*> &stacks );
  82. void setupBattle( BattleInfo * curB, int3 tile, const CCreatureSet &army1, const CCreatureSet &army2, const CGHeroInstance * hero1, const CGHeroInstance * hero2, bool creatureBank, const CGTownInstance *town);
  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. bool 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, si64 val);
  97. void changePrimSkill(int ID, int which, si64 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 giveCreatures (int objid, const CGHeroInstance * h, CCreatureSet *creatures);
  105. void showCompInfo(ShowInInfobox * comp);
  106. void heroVisitCastle(int obj, int heroID);
  107. void stopHeroVisitCastle(int obj, int heroID);
  108. void giveHeroArtifact(int artid, int hid, int position); //pos==-1 - first free slot in backpack; pos==-2 - default if available or backpack
  109. void moveArtifact(int hid, int oldPosition, int destPos);
  110. void removeArtifact(int hid, int pos);
  111. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank, boost::function<void(BattleResult*)> cb, const CGTownInstance *town = NULL); //use hero=NULL for no hero
  112. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, bool creatureBank, boost::function<void(BattleResult*)> cb);
  113. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, bool creatureBank, boost::function<void(BattleResult*)> cb = 0); //if any of armies is hero, hero will be used, visitable tile of second obj is place of battle
  114. //void startBattleI(int heroID, CCreatureSet army, int3 tile, boost::function<void(BattleResult*)> cb); //for hero<=>neutral army
  115. void setAmount(int objid, ui32 val);
  116. bool moveHero(si32 hid, int3 dst, ui8 instant, ui8 asker = 255);
  117. void giveHeroBonus(GiveBonus * bonus);
  118. void setMovePoints(SetMovePoints * smp);
  119. void setManaPoints(int hid, int val);
  120. void giveHero(int id, int player);
  121. void changeObjPos(int objid, int3 newPos, ui8 flags);
  122. void heroExchange(si32 hero1, si32 hero2);
  123. //////////////////////////////////////////////////////////////////////////
  124. void init(StartInfo *si, int Seed);
  125. void handleConnection(std::set<int> players, CConnection &c);
  126. int getPlayerAt(CConnection *c) const;
  127. void playerMessage( ui8 player, const std::string &message);
  128. bool makeBattleAction(BattleAction &ba);
  129. bool makeCustomAction(BattleAction &ba);
  130. bool queryReply( ui32 qid, ui32 answer );
  131. bool hireHero( ui32 tid, ui8 hid );
  132. bool buildBoat( ui32 objid );
  133. bool setFormation( si32 hid, ui8 formation );
  134. bool tradeResources( ui32 val, ui8 player, ui32 id1, ui32 id2 );
  135. bool buyArtifact( ui32 hid, si32 aid );
  136. bool swapArtifacts( si32 hid1, si32 hid2, ui16 slot1, ui16 slot2 );
  137. bool garrisonSwap(si32 tid);
  138. bool upgradeCreature( ui32 objid, ui8 pos, ui32 upgID );
  139. bool recruitCreatures(si32 objid, ui32 crid, ui32 cram);
  140. bool buildStructure(si32 tid, si32 bid);
  141. bool disbandCreature( si32 id, ui8 pos );
  142. bool arrangeStacks( si32 id1, si32 id2, ui8 what, ui8 p1, ui8 p2, si32 val );
  143. void save(const std::string &fname);
  144. void close();
  145. void handleTimeEvents();
  146. bool complain(const std::string &problem); //sends message to all clients, prints on the logs and return true
  147. void objectVisited( const CGObjectInstance * obj, const CGHeroInstance * h );
  148. void engageIntoBattle( ui8 player );
  149. template <typename Handler> void serialize(Handler &h, const int version)
  150. {
  151. h & QID & states;
  152. }
  153. ui32 getQueryResult(ui8 player, int queryID);
  154. void sendMessageToAll(const std::string &message);
  155. void sendMessageTo(CConnection &c, const std::string &message);
  156. void applyAndAsk(Query * sel, ui8 player, boost::function<void(ui32)> &callback);
  157. void ask(Query * sel, ui8 player, const CFunctionList<void(ui32)> &callback);
  158. void sendToAllClients(CPackForClient * info);
  159. void sendAndApply(CPackForClient * info);
  160. void run(bool resume);
  161. void newTurn();
  162. friend class CVCMIServer;
  163. friend class CScriptCallback;
  164. };
  165. #endif // __CGAMEHANDLER_H__