CGameHandler.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 checkLossVictory(ui8 player);
  80. void winLoseHandle(ui8 players=255); //players: bit field - colours of players to be checked; default: all
  81. void getLossVicMessage(ui8 player, bool standard, bool victory, InfoWindow &out) const;
  82. ////used only in endBattle - don't touch elsewhere
  83. boost::function<void(BattleResult*)> * battleEndCallback;
  84. const CArmedInstance * bEndArmy1, * bEndArmy2;
  85. //
  86. void endBattle(int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2); //ends battle
  87. void prepareAttack(BattleAttack &bat, const CStack *att, const CStack *def, int distance); //distance - number of hexes travelled before attacking
  88. void prepareAttacked(BattleStackAttacked &bsa, const CStack *def);
  89. void checkForBattleEnd( std::vector<CStack*> &stacks );
  90. void setupBattle( BattleInfo * curB, int3 tile, const CCreatureSet &army1, const CCreatureSet &army2, const CGHeroInstance * hero1, const CGHeroInstance * hero2, bool creatureBank, const CGTownInstance *town);
  91. CGameHandler(void);
  92. ~CGameHandler(void);
  93. //////////////////////////////////////////////////////////////////////////
  94. //from IGameCallback
  95. //get info
  96. int getCurrentPlayer();
  97. int getSelectedHero();
  98. //do sth
  99. void changeSpells(int hid, bool give, const std::set<ui32> &spells);
  100. bool removeObject(int objid);
  101. void setBlockVis(int objid, bool bv);
  102. void setOwner(int objid, ui8 owner);
  103. void setHoverName(int objid, MetaString * name);
  104. void setObjProperty(int objid, int prop, si64 val);
  105. void changePrimSkill(int ID, int which, si64 val, bool abs=false);
  106. void changeSecSkill(int ID, int which, int val, bool abs=false);
  107. void showInfoDialog(InfoWindow *iw);
  108. void showBlockingDialog(BlockingDialog *iw, const CFunctionList<void(ui32)> &callback);
  109. ui32 showBlockingDialog(BlockingDialog *iw); //synchronous version of above
  110. void showGarrisonDialog(int upobj, int hid, bool removableUnits, const boost::function<void()> &cb);
  111. void giveResource(int player, int which, int val);
  112. void giveCreatures (int objid, const CGHeroInstance * h, CCreatureSet creatures);
  113. void showCompInfo(ShowInInfobox * comp);
  114. void heroVisitCastle(int obj, int heroID);
  115. void vistiCastleObjects (const CGTownInstance *t, const CGHeroInstance *h);
  116. void stopHeroVisitCastle(int obj, int heroID);
  117. void giveHeroArtifact(int artid, int hid, int position); //pos==-1 - first free slot in backpack; pos==-2 - default if available or backpack
  118. void moveArtifact(int hid, int oldPosition, int destPos);
  119. void removeArtifact(int hid, int pos);
  120. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank = false, boost::function<void(BattleResult*)> cb = 0, const CGTownInstance *town = NULL); //use hero=NULL for no hero
  121. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false); //if any of armies is hero, hero will be used
  122. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false); //if any of armies is hero, hero will be used, visitable tile of second obj is place of battle//void startBattleI(int heroID, CCreatureSet army, int3 tile, boost::function<void(BattleResult*)> cb); //for hero<=>neutral army
  123. void setAmount(int objid, ui32 val);
  124. bool moveHero(si32 hid, int3 dst, ui8 instant, ui8 asker = 255);
  125. void giveHeroBonus(GiveBonus * bonus);
  126. void setMovePoints(SetMovePoints * smp);
  127. void setManaPoints(int hid, int val);
  128. void giveHero(int id, int player);
  129. void changeObjPos(int objid, int3 newPos, ui8 flags);
  130. void heroExchange(si32 hero1, si32 hero2);
  131. //////////////////////////////////////////////////////////////////////////
  132. void init(StartInfo *si, int Seed);
  133. void handleConnection(std::set<int> players, CConnection &c);
  134. int getPlayerAt(CConnection *c) const;
  135. void playerMessage( ui8 player, const std::string &message);
  136. bool makeBattleAction(BattleAction &ba);
  137. bool makeCustomAction(BattleAction &ba);
  138. bool queryReply( ui32 qid, ui32 answer );
  139. bool hireHero( ui32 tid, ui8 hid );
  140. bool buildBoat( ui32 objid );
  141. bool setFormation( si32 hid, ui8 formation );
  142. bool tradeResources( ui32 val, ui8 player, ui32 id1, ui32 id2 );
  143. bool buyArtifact( ui32 hid, si32 aid );
  144. bool swapArtifacts(si32 srcHeroID, si32 destHeroID, ui16 srcSlot, ui16 destSlot);
  145. bool setArtifact(si32 heroID, ui16 slot, int artID);
  146. bool garrisonSwap(si32 tid);
  147. bool upgradeCreature( ui32 objid, ui8 pos, ui32 upgID );
  148. bool recruitCreatures(si32 objid, ui32 crid, ui32 cram);
  149. bool buildStructure(si32 tid, si32 bid);
  150. bool razeStructure(si32 tid, si32 bid);
  151. bool disbandCreature( si32 id, ui8 pos );
  152. bool arrangeStacks( si32 id1, si32 id2, ui8 what, ui8 p1, ui8 p2, si32 val );
  153. void save(const std::string &fname);
  154. void close();
  155. void handleTimeEvents();
  156. bool complain(const std::string &problem); //sends message to all clients, prints on the logs and return true
  157. void objectVisited( const CGObjectInstance * obj, const CGHeroInstance * h );
  158. void engageIntoBattle( ui8 player );
  159. template <typename Handler> void serialize(Handler &h, const int version)
  160. {
  161. h & QID & states;
  162. }
  163. ui32 getQueryResult(ui8 player, int queryID);
  164. void sendMessageToAll(const std::string &message);
  165. void sendMessageTo(CConnection &c, const std::string &message);
  166. void applyAndAsk(Query * sel, ui8 player, boost::function<void(ui32)> &callback);
  167. void ask(Query * sel, ui8 player, const CFunctionList<void(ui32)> &callback);
  168. void sendToAllClients(CPackForClient * info);
  169. void sendAndApply(CPackForClient * info);
  170. void run(bool resume);
  171. void newTurn();
  172. friend class CVCMIServer;
  173. friend class CScriptCallback;
  174. };
  175. #endif // __CGAMEHANDLER_H__