CGameHandler.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. struct SetGarrisons;
  32. struct SetResource;
  33. struct SetResources;
  34. struct NewStructures;
  35. class CGHeroInstance;
  36. extern std::map<ui32, CFunctionList<void(ui32)> > callbacks; //question id => callback functions - for selection dialogs
  37. extern boost::mutex gsm;
  38. struct PlayerStatus
  39. {
  40. bool makingTurn, engagedIntoBattle;
  41. std::set<ui32> queries;
  42. PlayerStatus():makingTurn(false),engagedIntoBattle(false){};
  43. template <typename Handler> void serialize(Handler &h, const int version)
  44. {
  45. h & makingTurn & engagedIntoBattle & queries;
  46. }
  47. };
  48. class PlayerStatuses
  49. {
  50. public:
  51. std::map<ui8,PlayerStatus> players;
  52. boost::mutex mx;
  53. boost::condition_variable cv; //notifies when any changes are made
  54. void addPlayer(ui8 player);
  55. PlayerStatus operator[](ui8 player);
  56. bool hasQueries(ui8 player);
  57. bool checkFlag(ui8 player, bool PlayerStatus::*flag);
  58. void setFlag(ui8 player, bool PlayerStatus::*flag, bool val);
  59. void addQuery(ui8 player, ui32 id);
  60. void removeQuery(ui8 player, ui32 id);
  61. template <typename Handler> void serialize(Handler &h, const int version)
  62. {
  63. h & players;
  64. }
  65. };
  66. class CGameHandler : public IGameCallback
  67. {
  68. public:
  69. CVCMIServer *s;
  70. std::map<int,CConnection*> connections; //player color -> connection to client with interface of that player
  71. PlayerStatuses states; //player color -> player state
  72. std::set<CConnection*> conns;
  73. //queries stuff
  74. boost::recursive_mutex gsm;
  75. ui32 QID;
  76. std::map<ui32, CFunctionList<void(ui32)> > callbacks; //query id => callback function - for selection and yes/no dialogs
  77. std::map<ui32, boost::function<void()> > garrisonCallbacks; //query id => callback - for garrison dialogs
  78. std::map<ui32, std::pair<si32,si32> > allowedExchanges;
  79. bool isAllowedExchange(int id1, int id2);
  80. void giveSpells(const CGTownInstance *t, const CGHeroInstance *h);
  81. int moveStack(int stack, int dest); //returned value - travelled distance
  82. 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
  83. void checkLossVictory(ui8 player);
  84. void winLoseHandle(ui8 players=255); //players: bit field - colours of players to be checked; default: all
  85. void getLossVicMessage(ui8 player, ui8 standard, bool victory, InfoWindow &out) const;
  86. ////used only in endBattle - don't touch elsewhere
  87. boost::function<void(BattleResult*)> * battleEndCallback;
  88. const CArmedInstance * bEndArmy1, * bEndArmy2;
  89. //
  90. void endBattle(int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2); //ends battle
  91. void prepareAttack(BattleAttack &bat, const CStack *att, const CStack *def, int distance); //distance - number of hexes travelled before attacking
  92. void prepareAttacked(BattleStackAttacked &bsa, const CStack *def);
  93. void checkForBattleEnd( std::vector<CStack*> &stacks );
  94. void setupBattle( BattleInfo * curB, int3 tile, const CCreatureSet &army1, const CCreatureSet &army2, const CGHeroInstance * hero1, const CGHeroInstance * hero2, bool creatureBank, const CGTownInstance *town);
  95. CGameHandler(void);
  96. ~CGameHandler(void);
  97. //////////////////////////////////////////////////////////////////////////
  98. //from IGameCallback
  99. //get info
  100. int getCurrentPlayer();
  101. int getSelectedHero();
  102. //do sth
  103. void changeSpells(int hid, bool give, const std::set<ui32> &spells);
  104. bool removeObject(int objid);
  105. void setBlockVis(int objid, bool bv);
  106. void setOwner(int objid, ui8 owner);
  107. void setHoverName(int objid, MetaString * name);
  108. void setObjProperty(int objid, int prop, si64 val);
  109. void changePrimSkill(int ID, int which, si64 val, bool abs=false);
  110. void changeSecSkill(int ID, int which, int val, bool abs=false);
  111. void showInfoDialog(InfoWindow *iw);
  112. void showBlockingDialog(BlockingDialog *iw, const CFunctionList<void(ui32)> &callback);
  113. ui32 showBlockingDialog(BlockingDialog *iw); //synchronous version of above
  114. void showGarrisonDialog(int upobj, int hid, bool removableUnits, const boost::function<void()> &cb);
  115. void showThievesGuildWindow(int requestingObjId); //TODO: make something more general?
  116. void giveResource(int player, int which, int val);
  117. void giveCreatures (int objid, const CGHeroInstance * h, CCreatureSet creatures);
  118. void takeCreatures (int objid, TSlots creatures);
  119. void showCompInfo(ShowInInfobox * comp);
  120. void heroVisitCastle(int obj, int heroID);
  121. void vistiCastleObjects (const CGTownInstance *t, const CGHeroInstance *h);
  122. void stopHeroVisitCastle(int obj, int heroID);
  123. void giveHeroArtifact(int artid, int hid, int position); //pos==-1 - first free slot in backpack; pos==-2 - default if available or backpack
  124. void moveArtifact(int hid, int oldPosition, int destPos);
  125. void removeArtifact(int artid, int hid);
  126. 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
  127. 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
  128. 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
  129. void setAmount(int objid, ui32 val);
  130. bool moveHero(si32 hid, int3 dst, ui8 instant, ui8 asker = 255);
  131. void giveHeroBonus(GiveBonus * bonus);
  132. void setMovePoints(SetMovePoints * smp);
  133. void setManaPoints(int hid, int val);
  134. void giveHero(int id, int player);
  135. void changeObjPos(int objid, int3 newPos, ui8 flags);
  136. void useScholarSkill(si32 hero1, si32 hero2);
  137. void heroExchange(si32 hero1, si32 hero2);
  138. //////////////////////////////////////////////////////////////////////////
  139. void init(StartInfo *si, int Seed);
  140. void handleConnection(std::set<int> players, CConnection &c);
  141. int getPlayerAt(CConnection *c) const;
  142. void playerMessage( ui8 player, const std::string &message);
  143. bool makeBattleAction(BattleAction &ba);
  144. void handleSpellCasting(int spellID, int spellLvl, int destination, ui8 casterSide, ui8 casterColor, const CGHeroInstance * caster, const CGHeroInstance * secHero, int usedSpellPower);
  145. bool makeCustomAction(BattleAction &ba);
  146. bool queryReply( ui32 qid, ui32 answer );
  147. bool hireHero( ui32 tid, ui8 hid );
  148. bool buildBoat( ui32 objid );
  149. bool setFormation( si32 hid, ui8 formation );
  150. bool tradeResources( ui32 val, ui8 player, ui32 id1, ui32 id2 );
  151. bool assembleArtifacts (si32 heroID, ui16 artifactSlot, bool assemble, ui32 assembleTo);
  152. bool buyArtifact( ui32 hid, si32 aid );
  153. bool swapArtifacts(si32 srcHeroID, si32 destHeroID, ui16 srcSlot, ui16 destSlot);
  154. bool garrisonSwap(si32 tid);
  155. bool upgradeCreature( ui32 objid, ui8 pos, ui32 upgID );
  156. bool recruitCreatures(si32 objid, ui32 crid, ui32 cram);
  157. bool buildStructure(si32 tid, si32 bid);
  158. bool razeStructure(si32 tid, si32 bid);
  159. bool disbandCreature( si32 id, ui8 pos );
  160. bool arrangeStacks( si32 id1, si32 id2, ui8 what, ui8 p1, ui8 p2, si32 val );
  161. void save(const std::string &fname);
  162. void close();
  163. void handleTimeEvents();
  164. bool complain(const std::string &problem); //sends message to all clients, prints on the logs and return true
  165. void objectVisited( const CGObjectInstance * obj, const CGHeroInstance * h );
  166. void engageIntoBattle( ui8 player );
  167. bool dig(const CGHeroInstance *h);
  168. template <typename Handler> void serialize(Handler &h, const int version)
  169. {
  170. h & QID & states;
  171. }
  172. ui32 getQueryResult(ui8 player, int queryID);
  173. void sendMessageToAll(const std::string &message);
  174. void sendMessageTo(CConnection &c, const std::string &message);
  175. void applyAndAsk(Query * sel, ui8 player, boost::function<void(ui32)> &callback);
  176. void ask(Query * sel, ui8 player, const CFunctionList<void(ui32)> &callback);
  177. void sendToAllClients(CPackForClient * info);
  178. void sendAndApply(CPackForClient * info);
  179. void sendAndApply(SetGarrisons * info);
  180. void sendAndApply(SetResource * info);
  181. void sendAndApply(SetResources * info);
  182. void sendAndApply(NewStructures * info);
  183. void run(bool resume);
  184. void newTurn();
  185. void handleAfterAttackCasting( const BattleAttack & bat );
  186. friend class CVCMIServer;
  187. friend class CScriptCallback;
  188. };
  189. #endif // __CGAMEHANDLER_H__