CCallback.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * CCallback.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "lib/CGameInfoCallback.h"
  12. #include "lib/battle/CPlayerBattleCallback.h"
  13. #include "lib/int3.h" // for int3
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CGHeroInstance;
  16. class CGameState;
  17. struct CPath;
  18. class CGObjectInstance;
  19. class CArmedInstance;
  20. class BattleAction;
  21. class CGTownInstance;
  22. class IShipyard;
  23. struct CGPathNode;
  24. struct CGPath;
  25. struct CPathsInfo;
  26. class PathfinderConfig;
  27. struct CPack;
  28. struct CPackForServer;
  29. class IBattleEventsReceiver;
  30. class IGameEventsReceiver;
  31. struct ArtifactLocation;
  32. class BattleStateInfoForRetreat;
  33. class IMarket;
  34. VCMI_LIB_NAMESPACE_END
  35. // in static AI build this file gets included into libvcmi
  36. #ifdef STATIC_AI
  37. VCMI_LIB_USING_NAMESPACE
  38. #endif
  39. class CClient;
  40. struct lua_State;
  41. class IBattleCallback
  42. {
  43. public:
  44. virtual ~IBattleCallback() = default;
  45. bool waitTillRealize = false; //if true, request functions will return after they are realized by server
  46. bool unlockGsWhenWaiting = false;//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!
  47. //battle
  48. virtual void battleMakeSpellAction(const BattleID & battleID, const BattleAction & action) = 0;
  49. virtual void battleMakeUnitAction(const BattleID & battleID, const BattleAction & action) = 0;
  50. virtual void battleMakeTacticAction(const BattleID & battleID, const BattleAction & action) = 0;
  51. virtual std::optional<BattleAction> makeSurrenderRetreatDecision(const BattleID & battleID, const BattleStateInfoForRetreat & battleState) = 0;
  52. virtual std::shared_ptr<CPlayerBattleCallback> getBattle(const BattleID & battleID) = 0;
  53. virtual std::optional<PlayerColor> getPlayerID() const = 0;
  54. };
  55. class IGameActionCallback
  56. {
  57. public:
  58. //hero
  59. virtual bool moveHero(const CGHeroInstance *h, int3 dst, bool transit) =0; //dst must be free, neighbouring tile (this function can move hero only by one tile)
  60. virtual bool dismissHero(const CGHeroInstance * hero)=0; //dismisses given hero; true - successfuly, false - not successfuly
  61. virtual void dig(const CGObjectInstance *hero)=0;
  62. virtual void castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos = int3(-1, -1, -1))=0; //cast adventure map spell
  63. //town
  64. virtual void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)=0;
  65. virtual bool buildBuilding(const CGTownInstance *town, BuildingID buildingID)=0;
  66. virtual void recruitCreatures(const CGDwelling *obj, const CArmedInstance * dst, CreatureID ID, ui32 amount, si32 level=-1)=0;
  67. virtual bool upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID=CreatureID::NONE)=0; //if newID==-1 then best possible upgrade will be made
  68. virtual void swapGarrisonHero(const CGTownInstance *town)=0;
  69. virtual void trade(const IMarket * market, EMarketMode mode, ui32 id1, ui32 id2, ui32 val1, const CGHeroInstance * hero = nullptr)=0; //mode==0: sell val1 units of id1 resource for id2 resiurce
  70. virtual void trade(const IMarket * market, EMarketMode mode, const std::vector<ui32> & id1, const std::vector<ui32> & id2, const std::vector<ui32> & val1, const CGHeroInstance * hero = nullptr)=0;
  71. virtual int selectionMade(int selection, QueryID queryID) =0;
  72. virtual int sendQueryReply(std::optional<int32_t> reply, QueryID queryID) =0;
  73. 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!
  74. 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)
  75. virtual int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) =0; //first goes to the second
  76. virtual int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val)=0;//split creatures from the first stack
  77. //virtual bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)=0; //swaps artifacts between two given heroes
  78. virtual bool swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)=0;
  79. virtual void assembleArtifacts(const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)=0;
  80. virtual void eraseArtifactByClient(const ArtifactLocation & al)=0;
  81. virtual bool dismissCreature(const CArmedInstance *obj, SlotID stackPos)=0;
  82. virtual void endTurn()=0;
  83. 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)
  84. virtual void setFormation(const CGHeroInstance * hero, bool tight)=0;
  85. virtual void save(const std::string &fname) = 0;
  86. virtual void sendMessage(const std::string &mess, const CGObjectInstance * currentObject = nullptr) = 0;
  87. virtual void buildBoat(const IShipyard *obj) = 0;
  88. // To implement high-level army management bulk actions
  89. virtual int bulkMoveArmy(ObjectInstanceID srcArmy, ObjectInstanceID destArmy, SlotID srcSlot) = 0;
  90. virtual int bulkSplitStack(ObjectInstanceID armyId, SlotID srcSlot, int howMany = 1) = 0;
  91. virtual int bulkSmartSplitStack(ObjectInstanceID armyId, SlotID srcSlot) = 0;
  92. virtual int bulkMergeStacks(ObjectInstanceID armyId, SlotID srcSlot) = 0;
  93. // Moves all artifacts from one hero to another
  94. virtual void bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero, bool swap, bool equipped, bool backpack) = 0;
  95. };
  96. class CBattleCallback : public IBattleCallback
  97. {
  98. std::map<BattleID, std::shared_ptr<CPlayerBattleCallback>> activeBattles;
  99. std::optional<PlayerColor> player;
  100. protected:
  101. int sendRequest(const CPackForServer * request); //returns requestID (that'll be matched to requestID in PackageApplied)
  102. CClient *cl;
  103. public:
  104. CBattleCallback(std::optional<PlayerColor> player, CClient * C);
  105. void battleMakeSpellAction(const BattleID & battleID, const BattleAction & action) override;//for casting spells by hero - DO NOT use it for moving active stack
  106. void battleMakeUnitAction(const BattleID & battleID, const BattleAction & action) override;
  107. void battleMakeTacticAction(const BattleID & battleID, const BattleAction & action) override; // performs tactic phase actions
  108. std::optional<BattleAction> makeSurrenderRetreatDecision(const BattleID & battleID, const BattleStateInfoForRetreat & battleState) override;
  109. std::shared_ptr<CPlayerBattleCallback> getBattle(const BattleID & battleID) override;
  110. std::optional<PlayerColor> getPlayerID() const override;
  111. void onBattleStarted(const IBattleInfo * info);
  112. void onBattleEnded(const BattleID & battleID);
  113. friend class CCallback;
  114. friend class CClient;
  115. };
  116. class CCallback : public CPlayerSpecificInfoCallback, public CBattleCallback, public IGameActionCallback
  117. {
  118. public:
  119. CCallback(CGameState * GS, std::optional<PlayerColor> Player, CClient * C);
  120. virtual ~CCallback();
  121. //client-specific functionalities (pathfinding)
  122. virtual bool canMoveBetween(const int3 &a, const int3 &b);
  123. virtual int3 getGuardingCreaturePosition(int3 tile);
  124. virtual std::shared_ptr<const CPathsInfo> getPathsInfo(const CGHeroInstance * h);
  125. std::optional<PlayerColor> getPlayerID() const override;
  126. //Set of metrhods that allows adding more interfaces for this player that'll receive game event call-ins.
  127. void registerBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents);
  128. void unregisterBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents);
  129. //commands
  130. bool moveHero(const CGHeroInstance *h, int3 dst, bool transit = false) override; //dst must be free, neighbouring tile (this function can move hero only by one tile)
  131. bool teleportHero(const CGHeroInstance *who, const CGTownInstance *where);
  132. int selectionMade(int selection, QueryID queryID) override;
  133. int sendQueryReply(std::optional<int32_t> reply, QueryID queryID) override;
  134. int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) override;
  135. int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) override; //first goes to the second
  136. int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) override; //first goes to the second
  137. int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val) override;
  138. int bulkMoveArmy(ObjectInstanceID srcArmy, ObjectInstanceID destArmy, SlotID srcSlot) override;
  139. int bulkSplitStack(ObjectInstanceID armyId, SlotID srcSlot, int howMany = 1) override;
  140. int bulkSmartSplitStack(ObjectInstanceID armyId, SlotID srcSlot) override;
  141. int bulkMergeStacks(ObjectInstanceID armyId, SlotID srcSlot) override;
  142. bool dismissHero(const CGHeroInstance * hero) override;
  143. bool swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2) override;
  144. void assembleArtifacts(const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo) override;
  145. void bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero, bool swap, bool equipped = true, bool backpack = true) override;
  146. void eraseArtifactByClient(const ArtifactLocation & al) override;
  147. bool buildBuilding(const CGTownInstance *town, BuildingID buildingID) override;
  148. void recruitCreatures(const CGDwelling * obj, const CArmedInstance * dst, CreatureID ID, ui32 amount, si32 level=-1) override;
  149. bool dismissCreature(const CArmedInstance *obj, SlotID stackPos) override;
  150. bool upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID=CreatureID::NONE) override;
  151. void endTurn() override;
  152. void swapGarrisonHero(const CGTownInstance *town) override;
  153. void buyArtifact(const CGHeroInstance *hero, ArtifactID aid) override;
  154. void trade(const IMarket * market, EMarketMode mode, ui32 id1, ui32 id2, ui32 val1, const CGHeroInstance * hero = nullptr) override;
  155. void trade(const IMarket * market, EMarketMode mode, const std::vector<ui32> & id1, const std::vector<ui32> & id2, const std::vector<ui32> & val1, const CGHeroInstance * hero = nullptr) override;
  156. void setFormation(const CGHeroInstance * hero, bool tight) override;
  157. void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero) override;
  158. void save(const std::string &fname) override;
  159. void sendMessage(const std::string &mess, const CGObjectInstance * currentObject = nullptr) override;
  160. void buildBoat(const IShipyard *obj) override;
  161. void dig(const CGObjectInstance *hero) override;
  162. void castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos = int3(-1, -1, -1)) override;
  163. //friends
  164. friend class CClient;
  165. };