CCallback.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #pragma once
  2. #include "lib/IGameCallback.h"
  3. /*
  4. * CCallback.h, part of VCMI engine
  5. *
  6. * Authors: listed in file AUTHORS in main folder
  7. *
  8. * License: GNU General Public License v2.0 or later
  9. * Full text of license available in license.txt file, in main folder
  10. *
  11. */
  12. class CGHeroInstance;
  13. class CGameState;
  14. struct CPath;
  15. class CGObjectInstance;
  16. class CArmedInstance;
  17. struct BattleAction;
  18. class CGTownInstance;
  19. struct lua_State;
  20. class CClient;
  21. class IShipyard;
  22. struct CGPathNode;
  23. struct CGPath;
  24. struct CPathsInfo;
  25. struct CPack;
  26. class IBattleEventsReceiver;
  27. class IGameEventsReceiver;
  28. class CAutomationModule;
  29. struct ObjectsCeded;
  30. class IBattleCallback
  31. {
  32. public:
  33. bool waitTillRealize; //if true, request functions will return after they are realized by server
  34. bool unlockGsWhenWaiting;//if true after sending each request, gs mutex will be unlocked so the changes can be applied; NOTICE caller must have gs mx locked prior to any call to actiob callback!
  35. //battle
  36. virtual int battleMakeAction(BattleAction* action)=0;//for casting spells by hero - DO NOT use it for moving active stack
  37. virtual bool battleMakeTacticAction(BattleAction * action) =0; // performs tactic phase actions
  38. };
  39. class IGameActionCallback
  40. {
  41. public:
  42. //hero
  43. virtual bool moveHero(const CGHeroInstance *h, int3 dst) =0; //dst must be free, neighbouring tile (this function can move hero only by one tile)
  44. virtual bool dismissHero(const CGHeroInstance * hero)=0; //dismisses given hero; true - successfuly, false - not successfuly
  45. virtual void dig(const CGObjectInstance *hero)=0;
  46. virtual void castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos = int3(-1, -1, -1))=0; //cast adventure map spell
  47. //town
  48. virtual void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)=0;
  49. virtual bool buildBuilding(const CGTownInstance *town, BuildingID buildingID)=0;
  50. virtual void recruitCreatures(const CGObjectInstance *obj, CreatureID ID, ui32 amount, si32 level=-1)=0;
  51. virtual bool upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID=CreatureID::NONE)=0; //if newID==-1 then best possible upgrade will be made
  52. virtual void swapGarrisonHero(const CGTownInstance *town)=0;
  53. virtual void trade(const CGObjectInstance *market, EMarketMode::EMarketMode mode, int id1, int id2, int val1, const CGHeroInstance *hero = NULL)=0; //mode==0: sell val1 units of id1 resource for id2 resiurce
  54. virtual int selectionMade(int selection, QueryID queryID) =0;
  55. virtual int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)=0;//swaps creatures between two possibly different garrisons // TODO: AI-unsafe code - fix it!
  56. virtual int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)=0;//joins first stack to the second (creatures must be same type)
  57. virtual int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) =0; //first goes to the second
  58. virtual int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val)=0;//split creatures from the first stack
  59. //virtual bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)=0; //swaps artifacts between two given heroes
  60. virtual bool swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)=0;
  61. virtual bool assembleArtifacts(const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)=0;
  62. virtual bool dismissCreature(const CArmedInstance *obj, SlotID stackPos)=0;
  63. virtual void endTurn()=0;
  64. virtual void buyArtifact(const CGHeroInstance *hero, ArtifactID aid)=0; //used to buy artifacts in towns (including spell book in the guild and war machines in blacksmith)
  65. virtual void setFormation(const CGHeroInstance * hero, bool tight)=0;
  66. virtual void setSelection(const CArmedInstance * obj)=0;
  67. virtual void save(const std::string &fname) = 0;
  68. virtual void sendMessage(const std::string &mess) = 0;
  69. virtual void buildBoat(const IShipyard *obj) = 0;
  70. };
  71. struct CPack;
  72. class CBattleCallback : public IBattleCallback, public CPlayerBattleCallback
  73. {
  74. protected:
  75. int sendRequest(const CPack *request); //returns requestID (that'll be matched to requestID in PackageApplied)
  76. CClient *cl;
  77. //virtual bool hasAccess(int playerId) const;
  78. public:
  79. CBattleCallback(CGameState *GS, boost::optional<PlayerColor> Player, CClient *C);
  80. int battleMakeAction(BattleAction* action) OVERRIDE;//for casting spells by hero - DO NOT use it for moving active stack
  81. bool battleMakeTacticAction(BattleAction * action) OVERRIDE; // performs tactic phase actions
  82. friend class CCallback;
  83. friend class CClient;
  84. };
  85. class CCallback : public CPlayerSpecificInfoCallback, public IGameActionCallback, public CBattleCallback
  86. {
  87. private:
  88. void validatePaths(); //recalcualte paths if necessary
  89. public:
  90. CCallback(CGameState * GS, boost::optional<PlayerColor> Player, CClient *C);
  91. virtual ~CCallback();
  92. //client-specific functionalities (pathfinding)
  93. virtual const CGPathNode *getPathInfo(int3 tile); //uses main, client pathfinder info
  94. virtual bool getPath2(int3 dest, CGPath &ret); //uses main, client pathfinder info
  95. virtual void calculatePaths(const CGHeroInstance *hero, CPathsInfo &out, int3 src = int3(-1,-1,-1), int movement = -1);
  96. virtual void recalculatePaths(); //updates main, client pathfinder info (should be called when moving hero is over)
  97. //Set of metrhods that allows adding more interfaces for this player that'll receive game event call-ins.
  98. void registerGameInterface(shared_ptr<IGameEventsReceiver> gameEvents);
  99. void registerBattleInterface(shared_ptr<IBattleEventsReceiver> battleEvents);
  100. void unregisterGameInterface(shared_ptr<IGameEventsReceiver> gameEvents);
  101. void unregisterBattleInterface(shared_ptr<IBattleEventsReceiver> battleEvents);
  102. virtual shared_ptr<CAutomationModule> createAutomationModule(const std::string &moduleName, const ObjectsCeded &objects, const boost::any &initialMessage = boost::none);
  103. virtual void installAutomationModule(shared_ptr<CAutomationModule> module, const ObjectsCeded &objects);
  104. virtual void removeAutomationModule(shared_ptr<CAutomationModule> module);
  105. virtual void giveObjectToModule(const CGObjectInstance *obj, shared_ptr<CAutomationModule> module);
  106. virtual void takeObjectFromModule(const CGObjectInstance *obj, shared_ptr<CAutomationModule> module);
  107. virtual void sendMessageToModule(shared_ptr<CAutomationModule> module, const boost::any &message);
  108. virtual std::vector<shared_ptr<CAutomationModule>> getAutomationInterfaces();
  109. virtual void executeAutomation();
  110. void unregisterMyInterface(); //stops delivering information about game events to that player's interface -> can be called ONLY after victory/loss
  111. //commands
  112. bool moveHero(const CGHeroInstance *h, int3 dst); //dst must be free, neighbouring tile (this function can move hero only by one tile)
  113. bool teleportHero(const CGHeroInstance *who, const CGTownInstance *where);
  114. int selectionMade(int selection, QueryID queryID);
  115. int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2);
  116. int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2); //first goes to the second
  117. int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2); //first goes to the second
  118. int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val);
  119. bool dismissHero(const CGHeroInstance * hero);
  120. //bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2);
  121. bool swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2);
  122. //bool moveArtifact(const CGHeroInstance * hero, ui16 src, const CStackInstance * stack, ui16 dest); // TODO: unify classes
  123. //bool moveArtifact(const CStackInstance * stack, ui16 src , const CGHeroInstance * hero, ui16 dest); // TODO: unify classes
  124. bool assembleArtifacts(const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo);
  125. bool buildBuilding(const CGTownInstance *town, BuildingID buildingID) OVERRIDE;
  126. void recruitCreatures(const CGObjectInstance *obj, CreatureID ID, ui32 amount, si32 level=-1);
  127. bool dismissCreature(const CArmedInstance *obj, SlotID stackPos);
  128. bool upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID=CreatureID::NONE) OVERRIDE;
  129. void endTurn();
  130. void swapGarrisonHero(const CGTownInstance *town);
  131. void buyArtifact(const CGHeroInstance *hero, ArtifactID aid) OVERRIDE;
  132. void trade(const CGObjectInstance *market, EMarketMode::EMarketMode mode, int id1, int id2, int val1, const CGHeroInstance *hero = NULL);
  133. void setFormation(const CGHeroInstance * hero, bool tight);
  134. void setSelection(const CArmedInstance * obj);
  135. void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero);
  136. void save(const std::string &fname);
  137. void sendMessage(const std::string &mess);
  138. void buildBoat(const IShipyard *obj);
  139. void dig(const CGObjectInstance *hero);
  140. void castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos = int3(-1, -1, -1));
  141. //friends
  142. friend class CClient;
  143. };
  144. class CAutomationCallback : public CCallback
  145. {
  146. std::weak_ptr<CAutomationModule> automationModule;
  147. shared_ptr<CAutomationModule> getModule();
  148. bool canAccessHero(const CGHeroInstance *h);
  149. bool canRecruitAt(const CGDwelling *dwelling);
  150. public:
  151. CAutomationCallback(CGameState * GS, boost::optional<PlayerColor> Player, CClient *C, shared_ptr<CAutomationModule> module);
  152. //overloads
  153. /*
  154. virtual bool moveHero(const CGHeroInstance *h, int3 dst);
  155. virtual bool dismissHero(const CGHeroInstance * hero);
  156. virtual void dig(const CGObjectInstance *hero);
  157. virtual void castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos = int3(-1, -1, -1) );
  158. virtual void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero);
  159. virtual bool buildBuilding(const CGTownInstance *town, BuildingID buildingID);
  160. virtual void recruitCreatures(const CGObjectInstance *obj, CreatureID ID, ui32 amount, si32 level=-1);
  161. virtual bool upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID=CreatureID::NONE);
  162. virtual void swapGarrisonHero(const CGTownInstance *town);
  163. virtual void trade(const CGObjectInstance *market, EMarketMode::EMarketMode mode, int id1, int id2, int val1, const CGHeroInstance *hero = NULL);
  164. virtual int selectionMade(int selection, QueryID queryID);
  165. virtual int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2);
  166. virtual int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2);
  167. virtual int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2);
  168. virtual int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val);
  169. virtual bool swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2);
  170. virtual bool assembleArtifacts(const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo);
  171. virtual bool dismissCreature(const CArmedInstance *obj, SlotID stackPos);
  172. virtual void endTurn();
  173. virtual void buyArtifact(const CGHeroInstance *hero, ArtifactID aid);
  174. virtual void setFormation(const CGHeroInstance * hero, bool tight);
  175. virtual void setSelection(const CArmedInstance * obj);
  176. virtual void buildBoat(const IShipyard *obj);*/
  177. //new, automation specific
  178. virtual void sendMessage(const boost::any &msg);
  179. };