CGameHandler.h 6.2 KB

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