CGameHandler.h 8.4 KB

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