IGameCallback.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #pragma once
  2. #include "BattleHex.h"
  3. #include "ResourceSet.h"
  4. #include "int3.h"
  5. #include "GameConstants.h"
  6. #include "CBattleCallback.h"
  7. /*
  8. * IGameCallback.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. struct SetMovePoints;
  17. struct GiveBonus;
  18. class CGObjectInstance;
  19. class CGTownInstance;
  20. class CGHeroInstance;
  21. struct BlockingDialog;
  22. struct InfoWindow;
  23. struct MetaString;
  24. struct ShowInInfobox;
  25. struct BattleResult;
  26. struct Component;
  27. class CGameState;
  28. struct PlayerSettings;
  29. struct CPackForClient;
  30. class CArtHandler;
  31. class CArtifact;
  32. class CArmedInstance;
  33. struct TerrainTile;
  34. struct PlayerState;
  35. class CTown;
  36. struct StackLocation;
  37. struct ArtifactLocation;
  38. class CArtifactInstance;
  39. struct StartInfo;
  40. struct InfoAboutTown;
  41. struct UpgradeInfo;
  42. struct SThievesGuildInfo;
  43. struct CPath;
  44. class CGDwelling;
  45. struct InfoAboutHero;
  46. class CMapHeader;
  47. struct BattleAction;
  48. class CStack;
  49. class CSpell;
  50. class CCreatureSet;
  51. class CCreature;
  52. class CStackBasicDescriptor;
  53. struct TeamState;
  54. struct QuestInfo;
  55. class CGCreature;
  56. class CSaveFile;
  57. class CLoadFile;
  58. class DLL_LINKAGE CGameInfoCallback : public virtual CCallbackBase
  59. {
  60. protected:
  61. CGameInfoCallback();
  62. CGameInfoCallback(CGameState *GS, boost::optional<PlayerColor> Player);
  63. bool hasAccess(boost::optional<PlayerColor> playerId) const;
  64. bool isVisible(int3 pos, boost::optional<PlayerColor> Player) const;
  65. bool isVisible(const CGObjectInstance *obj, boost::optional<PlayerColor> Player) const;
  66. bool isVisible(const CGObjectInstance *obj) const;
  67. bool canGetFullInfo(const CGObjectInstance *obj) const; //true we player owns obj or ally owns obj or privileged mode
  68. bool isOwnedOrVisited(const CGObjectInstance *obj) const;
  69. public:
  70. //various
  71. int getDate(Date::EDateType mode=Date::DAY)const; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
  72. const StartInfo * getStartInfo(bool beforeRandomization = false)const;
  73. bool isAllowed(int type, int id); //type: 0 - spell; 1- artifact; 2 - secondary skill
  74. //player
  75. const PlayerState * getPlayer(PlayerColor color, bool verbose = true) const;
  76. int getResource(PlayerColor Player, Res::ERes which) const;
  77. bool isVisible(int3 pos) const;
  78. PlayerRelations::PlayerRelations getPlayerRelations(PlayerColor color1, PlayerColor color2) const;
  79. void getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj); //get thieves' guild info obtainable while visiting given object
  80. EPlayerStatus::EStatus getPlayerStatus(PlayerColor player, bool verbose = true) const; //-1 if no such player
  81. PlayerColor getCurrentPlayer() const; //player that currently makes move // TODO synchronous turns
  82. virtual PlayerColor getLocalPlayer() const; //player that is currently owning given client (if not a client, then returns current player)
  83. const PlayerSettings * getPlayerSettings(PlayerColor color) const;
  84. //armed object
  85. void getUpgradeInfo(const CArmedInstance *obj, SlotID stackPos, UpgradeInfo &out)const;
  86. //hero
  87. const CGHeroInstance* getHero(ObjectInstanceID objid) const;
  88. const CGHeroInstance* getHeroWithSubid(int subid) const;
  89. int getHeroCount(PlayerColor player, bool includeGarrisoned) const;
  90. bool getHeroInfo(const CGObjectInstance *hero, InfoAboutHero &dest) const;
  91. int getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const; //when called during battle, takes into account creatures' spell cost reduction
  92. int estimateSpellDamage(const CSpell * sp, const CGHeroInstance * hero) const; //estimates damage of given spell; returns 0 if spell causes no dmg
  93. const CGHeroInstance* getSelectedHero(PlayerColor player) const; //nullptr if no hero is selected
  94. const CGHeroInstance* getSelectedHero() const; //of current (active) player
  95. const CArtifactInstance * getArtInstance(ArtifactInstanceID aid) const;
  96. const CGObjectInstance * getObjInstance(ObjectInstanceID oid) const;
  97. //objects
  98. const CGObjectInstance* getObj(ObjectInstanceID objid, bool verbose = true) const;
  99. std::vector <const CGObjectInstance * > getBlockingObjs(int3 pos)const;
  100. std::vector <const CGObjectInstance * > getVisitableObjs(int3 pos, bool verbose = true)const;
  101. std::vector <const CGObjectInstance * > getFlaggableObjects(int3 pos) const;
  102. const CGObjectInstance * getTopObj (int3 pos) const;
  103. std::vector <std::string > getObjDescriptions(int3 pos)const; //returns descriptions of objects at pos in order from the lowest to the highest
  104. PlayerColor getOwner(ObjectInstanceID heroID) const;
  105. const CGObjectInstance *getObjByQuestIdentifier(int identifier) const; //nullptr if object has been removed (eg. killed)
  106. //map
  107. int3 guardingCreaturePosition (int3 pos) const;
  108. std::vector<const CGObjectInstance*> getGuardingCreatures (int3 pos) const;
  109. const CMapHeader * getMapHeader()const;
  110. int3 getMapSize() const; //returns size of map - z is 1 for one - level map and 2 for two level map
  111. const TerrainTile * getTile(int3 tile, bool verbose = true) const;
  112. bool isInTheMap(const int3 &pos) const;
  113. //town
  114. const CGTownInstance* getTown(ObjectInstanceID objid) const;
  115. int howManyTowns(PlayerColor Player) const;
  116. const CGTownInstance * getTownInfo(int val, bool mode)const; //mode = 0 -> val = player town serial; mode = 1 -> val = object id (serial)
  117. std::vector<const CGHeroInstance *> getAvailableHeroes(const CGObjectInstance * townOrTavern) const; //heroes that can be recruited
  118. std::string getTavernGossip(const CGObjectInstance * townOrTavern) const;
  119. EBuildingState::EBuildingState canBuildStructure(const CGTownInstance *t, BuildingID ID);//// 0 - no more than one capitol, 1 - lack of water, 2 - forbidden, 3 - Add another level to Mage Guild, 4 - already built, 5 - cannot build, 6 - cannot afford, 7 - build, 8 - lack of requirements
  120. virtual bool getTownInfo(const CGObjectInstance *town, InfoAboutTown &dest) const;
  121. const CTown *getNativeTown(PlayerColor color) const;
  122. //from gs
  123. const TeamState *getTeam(TeamID teamID) const;
  124. const TeamState *getPlayerTeam(PlayerColor color) const;
  125. EBuildingState::EBuildingState canBuildStructure(const CGTownInstance *t, BuildingID ID) const;// 0 - no more than one capitol, 1 - lack of water, 2 - forbidden, 3 - Add another level to Mage Guild, 4 - already built, 5 - cannot build, 6 - cannot afford, 7 - build, 8 - lack of requirements
  126. };
  127. class DLL_LINKAGE CPlayerSpecificInfoCallback : public CGameInfoCallback
  128. {
  129. public:
  130. int howManyTowns() const;
  131. int howManyHeroes(bool includeGarrisoned = true) const;
  132. int3 getGrailPos(double &outKnownRatio);
  133. boost::optional<PlayerColor> getMyColor() const;
  134. std::vector <const CGTownInstance *> getTownsInfo(bool onlyOur = true) const; //true -> only owned; false -> all visible
  135. int getHeroSerial(const CGHeroInstance * hero, bool includeGarrisoned=true) const;
  136. const CGTownInstance* getTownBySerial(int serialId) const; // serial id is [0, number of towns)
  137. const CGHeroInstance* getHeroBySerial(int serialId, bool includeGarrisoned=true) const; // serial id is [0, number of heroes)
  138. std::vector <const CGHeroInstance *> getHeroesInfo(bool onlyOur = true) const; //true -> only owned; false -> all visible
  139. std::vector <const CGDwelling *> getMyDwellings() const; //returns all dwellings that belong to player
  140. std::vector <const CGObjectInstance * > getMyObjects() const; //returns all objects flagged by belonging player
  141. std::vector <QuestInfo> getMyQuests() const;
  142. int getResourceAmount(Res::ERes type) const;
  143. TResources getResourceAmount() const;
  144. const std::vector< std::vector< std::vector<ui8> > > & getVisibilityMap()const; //returns visibility map
  145. const PlayerSettings * getPlayerSettings(PlayerColor color) const;
  146. };
  147. class DLL_LINKAGE CPrivilagedInfoCallback : public CGameInfoCallback
  148. {
  149. public:
  150. CGameState * gameState ();
  151. void getFreeTiles (std::vector<int3> &tiles) const; //used for random spawns
  152. void getTilesInRange(std::unordered_set<int3, ShashInt3> &tiles, int3 pos, int radious, boost::optional<PlayerColor> player = boost::optional<PlayerColor>(), int mode=0) const; //mode 1 - only unrevealed tiles; mode 0 - all, mode -1 - only unrevealed
  153. void getAllTiles (std::unordered_set<int3, ShashInt3> &tiles, boost::optional<PlayerColor> player = boost::optional<PlayerColor>(), int level=-1, int surface=0) const; //returns all tiles on given level (-1 - both levels, otherwise number of level); surface: 0 - land and water, 1 - only land, 2 - only water
  154. ArtifactID getRandomArt (int flags);
  155. ArtifactID getArtSync (ui32 rand, int flags, bool erasePicked); //synchronous
  156. void pickAllowedArtsSet(std::vector<const CArtifact*> &out); //gives 3 treasures, 3 minors, 1 major -> used by Black Market and Artifact Merchant
  157. void getAllowedSpells(std::vector<SpellID> &out, ui16 level);
  158. template<typename Saver>
  159. void saveCommonState(Saver &out) const; //stores GS and VLC
  160. template<typename Loader>
  161. void loadCommonState(Loader &in); //loads GS and VLC
  162. };
  163. class DLL_LINKAGE CNonConstInfoCallback : public CPrivilagedInfoCallback
  164. {
  165. public:
  166. PlayerState *getPlayer(PlayerColor color, bool verbose = true);
  167. TeamState *getTeam(TeamID teamID);//get team by team ID
  168. TeamState *getPlayerTeam(PlayerColor color);// get team by player color
  169. CGHeroInstance *getHero(ObjectInstanceID objid);
  170. CGTownInstance *getTown(ObjectInstanceID objid);
  171. TerrainTile * getTile(int3 pos);
  172. CArtifactInstance * getArtInstance(ArtifactInstanceID aid);
  173. CGObjectInstance * getObjInstance(ObjectInstanceID oid);
  174. };
  175. class DLL_LINKAGE IGameEventRealizer
  176. {
  177. public:
  178. virtual void commitPackage(CPackForClient *pack) = 0;
  179. virtual void showInfoDialog(InfoWindow *iw);
  180. virtual void setObjProperty(ObjectInstanceID objid, int prop, si64 val);
  181. virtual void showInfoDialog(const std::string &msg, PlayerColor player);
  182. };
  183. class DLL_LINKAGE IGameEventCallback : public IGameEventRealizer
  184. {
  185. public:
  186. virtual void changeSpells(const CGHeroInstance * hero, bool give, const std::set<SpellID> &spells)=0;
  187. virtual bool removeObject(const CGObjectInstance * obj)=0;
  188. virtual void setBlockVis(ObjectInstanceID objid, bool bv)=0;
  189. virtual void setOwner(const CGObjectInstance * objid, PlayerColor owner)=0;
  190. virtual void setHoverName(const CGObjectInstance * obj, MetaString * name)=0;
  191. virtual void changePrimSkill(const CGHeroInstance * hero, PrimarySkill::PrimarySkill which, si64 val, bool abs=false)=0;
  192. virtual void changeSecSkill(const CGHeroInstance * hero, SecondarySkill which, int val, bool abs=false)=0;
  193. virtual void showBlockingDialog(BlockingDialog *iw) =0;
  194. virtual void showGarrisonDialog(ObjectInstanceID upobj, ObjectInstanceID hid, bool removableUnits) =0; //cb will be called when player closes garrison window
  195. virtual void showThievesGuildWindow(PlayerColor player, ObjectInstanceID requestingObjId) =0;
  196. virtual void giveResource(PlayerColor player, Res::ERes which, int val)=0;
  197. virtual void giveResources(PlayerColor player, TResources resources)=0;
  198. virtual void giveCreatures(const CArmedInstance *objid, const CGHeroInstance * h, const CCreatureSet &creatures, bool remove) =0;
  199. virtual void takeCreatures(ObjectInstanceID objid, const std::vector<CStackBasicDescriptor> &creatures) =0;
  200. virtual bool changeStackCount(const StackLocation &sl, TQuantity count, bool absoluteValue = false) =0;
  201. virtual bool changeStackType(const StackLocation &sl, CCreature *c) =0;
  202. virtual bool insertNewStack(const StackLocation &sl, const CCreature *c, TQuantity count = -1) =0; //count -1 => moves whole stack
  203. virtual bool eraseStack(const StackLocation &sl, bool forceRemoval = false) =0;
  204. virtual bool swapStacks(const StackLocation &sl1, const StackLocation &sl2) =0;
  205. virtual bool addToSlot(const StackLocation &sl, const CCreature *c, TQuantity count) =0; //makes new stack or increases count of already existing
  206. virtual void tryJoiningArmy(const CArmedInstance *src, const CArmedInstance *dst, bool removeObjWhenFinished, bool allowMerging) =0; //merges army from src do dst or opens a garrison window
  207. virtual bool moveStack(const StackLocation &src, const StackLocation &dst, TQuantity count) = 0;
  208. virtual void removeAfterVisit(const CGObjectInstance *object) = 0; //object will be destroyed when interaction is over. Do not call when interaction is not ongoing!
  209. virtual void giveHeroNewArtifact(const CGHeroInstance *h, const CArtifact *artType, ArtifactPosition pos) = 0;
  210. virtual void giveHeroArtifact(const CGHeroInstance *h, const CArtifactInstance *a, ArtifactPosition pos) = 0; //pos==-1 - first free slot in backpack=0; pos==-2 - default if available or backpack
  211. virtual void putArtifact(const ArtifactLocation &al, const CArtifactInstance *a) = 0;
  212. virtual void removeArtifact(const ArtifactLocation &al) = 0;
  213. virtual bool moveArtifact(const ArtifactLocation &al1, const ArtifactLocation &al2) = 0;
  214. virtual void synchronizeArtifactHandlerLists() = 0;
  215. virtual void showCompInfo(ShowInInfobox * comp)=0;
  216. virtual void heroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero)=0;
  217. virtual void stopHeroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero)=0;
  218. virtual void startBattlePrimary(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank = false, const CGTownInstance *town = nullptr)=0; //use hero=nullptr for no hero
  219. virtual void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, bool creatureBank = false)=0; //if any of armies is hero, hero will be used
  220. virtual void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, bool creatureBank = false)=0; //if any of armies is hero, hero will be used, visitable tile of second obj is place of battle
  221. virtual void setAmount(ObjectInstanceID objid, ui32 val)=0;
  222. virtual bool moveHero(ObjectInstanceID hid, int3 dst, ui8 teleporting, PlayerColor asker = PlayerColor::NEUTRAL)=0;
  223. virtual void giveHeroBonus(GiveBonus * bonus)=0;
  224. virtual void setMovePoints(SetMovePoints * smp)=0;
  225. virtual void setManaPoints(ObjectInstanceID hid, int val)=0;
  226. virtual void giveHero(ObjectInstanceID id, PlayerColor player)=0;
  227. virtual void changeObjPos(ObjectInstanceID objid, int3 newPos, ui8 flags)=0;
  228. virtual void sendAndApply(CPackForClient * info)=0;
  229. virtual void heroExchange(ObjectInstanceID hero1, ObjectInstanceID hero2)=0; //when two heroes meet on adventure map
  230. virtual void addQuest(int player, QuestInfo & quest){};
  231. };
  232. /// Interface class for handling general game logic and actions
  233. class DLL_LINKAGE IGameCallback : public CPrivilagedInfoCallback, public IGameEventCallback
  234. {
  235. public:
  236. virtual ~IGameCallback(){};
  237. //do sth
  238. const CGObjectInstance *putNewObject(Obj ID, int subID, int3 pos);
  239. const CGCreature *putNewMonster(CreatureID creID, int count, int3 pos);
  240. //get info
  241. virtual bool isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero);
  242. friend struct CPack;
  243. friend struct CPackForClient;
  244. friend struct CPackForServer;
  245. };