CCallback.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. class CGHeroInstance;
  15. class CGameState;
  16. struct CPath;
  17. class CGObjectInstance;
  18. class CArmedInstance;
  19. class BattleAction;
  20. class CGTownInstance;
  21. struct lua_State;
  22. class CClient;
  23. class IShipyard;
  24. struct CGPathNode;
  25. struct CGPath;
  26. struct CPathsInfo;
  27. class PathfinderConfig;
  28. struct CPack;
  29. class IBattleEventsReceiver;
  30. class IGameEventsReceiver;
  31. struct ArtifactLocation;
  32. class IBattleCallback
  33. {
  34. public:
  35. bool waitTillRealize; //if true, request functions will return after they are realized by server
  36. 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!
  37. //battle
  38. virtual int battleMakeAction(const BattleAction * action) = 0;//for casting spells by hero - DO NOT use it for moving active stack
  39. virtual bool battleMakeTacticAction(BattleAction * action) = 0; // performs tactic phase actions
  40. };
  41. class IGameActionCallback
  42. {
  43. public:
  44. //hero
  45. 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)
  46. virtual bool dismissHero(const CGHeroInstance * hero)=0; //dismisses given hero; true - successfuly, false - not successfuly
  47. virtual void dig(const CGObjectInstance *hero)=0;
  48. virtual void castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos = int3(-1, -1, -1))=0; //cast adventure map spell
  49. //town
  50. virtual void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)=0;
  51. virtual bool buildBuilding(const CGTownInstance *town, BuildingID buildingID)=0;
  52. virtual void recruitCreatures(const CGDwelling *obj, const CArmedInstance * dst, CreatureID ID, ui32 amount, si32 level=-1)=0;
  53. virtual bool upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID=CreatureID::NONE)=0; //if newID==-1 then best possible upgrade will be made
  54. virtual void swapGarrisonHero(const CGTownInstance *town)=0;
  55. virtual void trade(const CGObjectInstance * market, EMarketMode::EMarketMode mode, ui32 id1, ui32 id2, ui32 val1, const CGHeroInstance * hero = nullptr)=0; //mode==0: sell val1 units of id1 resource for id2 resiurce
  56. virtual void trade(const CGObjectInstance * market, EMarketMode::EMarketMode mode, const std::vector<ui32> & id1, const std::vector<ui32> & id2, const std::vector<ui32> & val1, const CGHeroInstance * hero = nullptr)=0;
  57. virtual int selectionMade(int selection, QueryID queryID) =0;
  58. virtual int sendQueryReply(const JsonNode & reply, QueryID queryID) =0;
  59. 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!
  60. 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)
  61. virtual int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) =0; //first goes to the second
  62. virtual int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val)=0;//split creatures from the first stack
  63. //virtual bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)=0; //swaps artifacts between two given heroes
  64. virtual bool swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)=0;
  65. virtual bool assembleArtifacts(const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)=0;
  66. virtual bool dismissCreature(const CArmedInstance *obj, SlotID stackPos)=0;
  67. virtual void endTurn()=0;
  68. 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)
  69. virtual void setFormation(const CGHeroInstance * hero, bool tight)=0;
  70. virtual void save(const std::string &fname) = 0;
  71. virtual void sendMessage(const std::string &mess, const CGObjectInstance * currentObject = nullptr) = 0;
  72. virtual void buildBoat(const IShipyard *obj) = 0;
  73. // To implement high-level army management bulk actions
  74. virtual int bulkMoveArmy(ObjectInstanceID srcArmy, ObjectInstanceID destArmy, SlotID srcSlot) = 0;
  75. virtual int bulkSplitStack(ObjectInstanceID armyId, SlotID srcSlot, int howMany = 1) = 0;
  76. virtual int bulkSmartSplitStack(ObjectInstanceID armyId, SlotID srcSlot) = 0;
  77. virtual int bulkMergeStacks(ObjectInstanceID armyId, SlotID srcSlot) = 0;
  78. };
  79. struct CPackForServer;
  80. class CBattleCallback : public IBattleCallback, public CPlayerBattleCallback
  81. {
  82. protected:
  83. int sendRequest(const CPackForServer * request); //returns requestID (that'll be matched to requestID in PackageApplied)
  84. CClient *cl;
  85. public:
  86. CBattleCallback(boost::optional<PlayerColor> Player, CClient *C);
  87. int battleMakeAction(const BattleAction * action) override;//for casting spells by hero - DO NOT use it for moving active stack
  88. bool battleMakeTacticAction(BattleAction * action) override; // performs tactic phase actions
  89. #if SCRIPTING_ENABLED
  90. scripting::Pool * getContextPool() const override;
  91. #endif
  92. friend class CCallback;
  93. friend class CClient;
  94. };
  95. class CCallback : public CPlayerSpecificInfoCallback,
  96. public IGameActionCallback,
  97. public CBattleCallback
  98. {
  99. public:
  100. CCallback(CGameState * GS, boost::optional<PlayerColor> Player, CClient *C);
  101. virtual ~CCallback();
  102. //client-specific functionalities (pathfinding)
  103. virtual bool canMoveBetween(const int3 &a, const int3 &b);
  104. virtual int3 getGuardingCreaturePosition(int3 tile);
  105. virtual std::shared_ptr<const CPathsInfo> getPathsInfo(const CGHeroInstance * h);
  106. virtual void calculatePaths(const CGHeroInstance *hero, CPathsInfo &out);
  107. //Set of metrhods that allows adding more interfaces for this player that'll receive game event call-ins.
  108. void registerBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents);
  109. void unregisterBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents);
  110. //commands
  111. 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)
  112. bool teleportHero(const CGHeroInstance *who, const CGTownInstance *where);
  113. int selectionMade(int selection, QueryID queryID) override;
  114. int sendQueryReply(const JsonNode & reply, QueryID queryID) override;
  115. int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) override;
  116. int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) override; //first goes to the second
  117. int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) override; //first goes to the second
  118. int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val) override;
  119. int bulkMoveArmy(ObjectInstanceID srcArmy, ObjectInstanceID destArmy, SlotID srcSlot) override;
  120. int bulkSplitStack(ObjectInstanceID armyId, SlotID srcSlot, int howMany = 1) override;
  121. int bulkSmartSplitStack(ObjectInstanceID armyId, SlotID srcSlot) override;
  122. int bulkMergeStacks(ObjectInstanceID armyId, SlotID srcSlot) override;
  123. bool dismissHero(const CGHeroInstance * hero) override;
  124. bool swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2) override;
  125. bool assembleArtifacts(const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo) override;
  126. bool buildBuilding(const CGTownInstance *town, BuildingID buildingID) override;
  127. void recruitCreatures(const CGDwelling * obj, const CArmedInstance * dst, CreatureID ID, ui32 amount, si32 level=-1) override;
  128. bool dismissCreature(const CArmedInstance *obj, SlotID stackPos) override;
  129. bool upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID=CreatureID::NONE) override;
  130. void endTurn() override;
  131. void swapGarrisonHero(const CGTownInstance *town) override;
  132. void buyArtifact(const CGHeroInstance *hero, ArtifactID aid) override;
  133. void trade(const CGObjectInstance * market, EMarketMode::EMarketMode mode, ui32 id1, ui32 id2, ui32 val1, const CGHeroInstance * hero = nullptr) override;
  134. void trade(const CGObjectInstance * market, EMarketMode::EMarketMode mode, const std::vector<ui32> & id1, const std::vector<ui32> & id2, const std::vector<ui32> & val1, const CGHeroInstance * hero = nullptr) override;
  135. void setFormation(const CGHeroInstance * hero, bool tight) override;
  136. void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero) override;
  137. void save(const std::string &fname) override;
  138. void sendMessage(const std::string &mess, const CGObjectInstance * currentObject = nullptr) override;
  139. void buildBoat(const IShipyard *obj) override;
  140. void dig(const CGObjectInstance *hero) override;
  141. void castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos = int3(-1, -1, -1)) override;
  142. //friends
  143. friend class CClient;
  144. };