IGameCallback.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #ifndef __IGAMECALLBACK_H__
  2. #define __IGAMECALLBACK_H__
  3. #include "../global.h"
  4. #include <vector>
  5. #include <set>
  6. #include "../client/FunctionList.h"
  7. #include "CObstacleInstance.h"
  8. #include "ResourceSet.h"
  9. /*
  10. * IGameCallback.h, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. struct SetMovePoints;
  19. struct GiveBonus;
  20. class CGObjectInstance;
  21. class CGTownInstance;
  22. class CGHeroInstance;
  23. struct BlockingDialog;
  24. struct InfoWindow;
  25. struct MetaString;
  26. struct ShowInInfobox;
  27. struct BattleResult;
  28. struct Component;
  29. class CGameState;
  30. struct PlayerSettings;
  31. struct CPackForClient;
  32. class CArtHandler;
  33. class CArtifact;
  34. class CArmedInstance;
  35. struct TerrainTile;
  36. struct PlayerState;
  37. class CTown;
  38. struct StackLocation;
  39. struct ArtifactLocation;
  40. class CArtifactInstance;
  41. struct StartInfo;
  42. struct InfoAboutTown;
  43. struct UpgradeInfo;
  44. struct SThievesGuildInfo;
  45. struct CPath;
  46. class CGDwelling;
  47. struct InfoAboutHero;
  48. class CMapHeader;
  49. struct BattleAction;
  50. class CStack;
  51. class CSpell;
  52. class CCreatureSet;
  53. class CCreature;
  54. class CStackBasicDescriptor;
  55. struct TeamState;
  56. class CGCreature;
  57. typedef std::vector<const CStack*> TStacks;
  58. namespace boost
  59. {class shared_mutex;}
  60. class DLL_EXPORT CCallbackBase
  61. {
  62. protected:
  63. CGameState *gs;
  64. int player; // -1 gives access to all information, otherwise limited to knowledge of given player
  65. CCallbackBase(CGameState *GS, int Player)
  66. : gs(GS), player(Player)
  67. {}
  68. CCallbackBase()
  69. : gs(NULL), player(-1)
  70. {}
  71. public:
  72. boost::shared_mutex &getGsMutex(); //just return a reference to mutex, does not lock nor anything
  73. };
  74. class DLL_EXPORT CBattleInfoCallback : public virtual CCallbackBase
  75. {
  76. public:
  77. enum EStackOwnership
  78. {
  79. ONLY_MINE, ONLY_ENEMY, MINE_AND_ENEMY
  80. };
  81. /// Return value: 1. sand/shore 2. sand/mesas 3. dirt/birches 4. dirt/hills 5. dirt/pines 6. grass/hills 7. grass/pines 8. lava 9. magic plains 10. snow/mountains 11. snow/trees 12. subterranean 13. swamp/trees 14. fiery fields 15. rock lands 16. magic clouds 17. lucid pools 18. holy ground 19. clover field 20. evil fog 21. "favourable winds" text on magic plains background 22. cursed ground 23. rough 24. ship to ship 25. ship
  82. int battleGetBattlefieldType();
  83. /// returns 1 if there is an obstacle or 0 otherwise
  84. int battleGetObstaclesAtTile(THex tile);
  85. /// returns all obstacles on the battlefield
  86. std::vector<CObstacleInstance> battleGetAllObstacles();
  87. /// returns stack info by given ID
  88. const CStack * battleGetStackByID(int ID, bool onlyAlive = true);
  89. /// returns stack info by given pos
  90. const CStack * battleGetStackByPos(THex pos, bool onlyAlive = true);
  91. /// returns position (tile ID) of stack
  92. THex battleGetPos(int stack);
  93. /// returns stacks on battlefield
  94. TStacks battleGetStacks(EStackOwnership whose = MINE_AND_ENEMY, bool onlyAlive = true);
  95. /// returns vector of stack in order of their move sequence
  96. void getStackQueue( std::vector<const CStack *> &out, int howMany );
  97. /// returns hexes which when in front of a stack cause us to move the amount box back
  98. void battleGetStackCountOutsideHexes(bool *ac);
  99. /// returns numbers of hexes reachable by creature with id ID
  100. std::vector<THex> battleGetAvailableHexes(const CStack * stack, bool addOccupiable, std::vector<THex> * attackable = NULL);
  101. /// returns vector of distances to [dest hex number]; WARNING: second argument is ignored
  102. std::vector<int> battleGetDistances(const CStack * stack, THex hex = THex::INVALID, THex * predecessors = NULL);
  103. /// returns vector of distances to [dest hex number]
  104. std::vector<int> battleGetDistancesFromHex(const CStack * stack, THex hex = THex::INVALID, THex * predecessors = NULL);
  105. std::set<THex> battleGetAttackedHexes(const CStack* attacker, THex destinationTile, THex attackerPos = THex::INVALID);
  106. /// returns true if unit with id ID can shoot to dest
  107. bool battleCanShoot(const CStack * stack, THex dest);
  108. /// returns true, if caller can cast a spell
  109. bool battleCanCastSpell();
  110. /// determines if given spell can be casted (and returns problem description)
  111. SpellCasting::ESpellCastProblem battleCanCastThisSpell(const CSpell * spell);
  112. ///estimates how much damage a spell will inflict upon a destination stack. If it's NULL a base damage value will be returned. If spell is not a plain offensive spell, 0 will be returned. The function doesn't check, if hero can cast given spell.
  113. int battleEstimateSpellDamage(const CGHeroInstance *caster, const CSpell * spell, const CStack *destination = NULL);
  114. /// returns true if caller can flee from the battle
  115. bool battleCanFlee();
  116. /// returns cost of surrendering battle, -1 if surrendering is not possible
  117. int battleGetSurrenderCost();
  118. /// returns defended town if current battle is a siege, NULL instead
  119. const CGTownInstance * battleGetDefendedTown();
  120. /// for determining state of a part of the wall; format: parameter [0] - keep, [1] - bottom tower, [2] - bottom wall, [3] - below gate, [4] - over gate, [5] - upper wall, [6] - uppert tower, [7] - gate; returned value: 1 - intact, 2 - damaged, 3 - destroyed; 0 - no battle
  121. ui8 battleGetWallState(int partOfWall);
  122. /// returns part of destructible wall / gate / keep under given hex or -1 if not found
  123. int battleGetWallUnderHex(THex hex);
  124. /// estimates damage dealt by attacker to defender; it may be not precise especially when stack has randomly working bonuses; returns pair <min dmg, max dmg>
  125. TDmgRange battleEstimateDamage(const CStack * attacker, const CStack * defender, TDmgRange * retaliationDmg = NULL);
  126. /// returns 0 when there is no siege, 1 if fort, 2 is citadel, 3 is castle
  127. ui8 battleGetSiegeLevel();
  128. /// returns hero corresponding to given side (0 - attacker, 1 - defender)
  129. const CGHeroInstance * battleGetFightingHero(ui8 side) const;
  130. /// checks if given stack has distance penalty
  131. si8 battleHasDistancePenalty(const CStack * stack, THex destHex);
  132. /// checks if given stack has wall penalty
  133. si8 battleHasWallPenalty(const CStack * stack, THex destHex);
  134. /// checks if teleportation of given stack to given position can take place
  135. si8 battleCanTeleportTo(const CStack * stack, THex destHex, int telportLevel);
  136. /// returns tactic distance for calling player or 0 if player is not in tactic phase
  137. si8 battleGetTacticDist();
  138. /// return side of player in battle (attacker/defender)
  139. ui8 battleGetMySide();
  140. //convenience methods using the ones above
  141. /// returns all stacks, alive or dead or undead or mechanical :)
  142. TStacks battleGetAllStacks()
  143. {
  144. return battleGetStacks(MINE_AND_ENEMY, false);
  145. }
  146. };
  147. class DLL_EXPORT CGameInfoCallback : public virtual CCallbackBase
  148. {
  149. protected:
  150. CGameInfoCallback();
  151. CGameInfoCallback(CGameState *GS, int Player);
  152. bool hasAccess(int playerId) const;
  153. bool isVisible(int3 pos, int Player) const;
  154. bool isVisible(const CGObjectInstance *obj, int Player) const;
  155. bool isVisible(const CGObjectInstance *obj) const;
  156. bool canGetFullInfo(const CGObjectInstance *obj) const; //true we player owns obj or ally owns obj or privileged mode
  157. bool isOwnedOrVisited(const CGObjectInstance *obj) const;
  158. public:
  159. //various
  160. int getDate(int mode=0)const; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
  161. const StartInfo * getStartInfo()const;
  162. bool isAllowed(int type, int id); //type: 0 - spell; 1- artifact; 2 - secondary skill
  163. //player
  164. const PlayerState * getPlayer(int color, bool verbose = true) const;
  165. int getResource(int Player, int which) const;
  166. bool isVisible(int3 pos) const;
  167. int getPlayerRelations(ui8 color1, ui8 color2) const;// 0 = enemy, 1 = ally, 2 = same player
  168. void getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj); //get thieves' guild info obtainable while visiting given object
  169. int getPlayerStatus(int player) const; //-1 if no such player
  170. int getCurrentPlayer() const; //player that currently makes move // TODO synchronous turns
  171. virtual int getLocalPlayer() const; //player that is currently owning given client (if not a client, then returns current player)
  172. const PlayerSettings * getPlayerSettings(int color) const;
  173. //armed object
  174. void getUpgradeInfo(const CArmedInstance *obj, int stackPos, UpgradeInfo &out)const;
  175. //hero
  176. const CGHeroInstance* getHero(int objid) const;
  177. const CGHeroInstance* getHeroWithSubid(int subid) const;
  178. int getHeroCount(int player, bool includeGarrisoned) const;
  179. bool getHeroInfo(const CGObjectInstance *hero, InfoAboutHero &dest) const;
  180. int getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const; //when called during battle, takes into account creatures' spell cost reduction
  181. int estimateSpellDamage(const CSpell * sp, const CGHeroInstance * hero) const; //estimates damage of given spell; returns 0 if spell causes no dmg
  182. bool verifyPath(CPath * path, bool blockSea)const;
  183. const CGHeroInstance* getSelectedHero(int player) const; //NULL if no hero is selected
  184. const CGHeroInstance* getSelectedHero() const; //of current (active) player
  185. //objects
  186. const CGObjectInstance* getObj(int objid, bool verbose = true) const;
  187. std::vector <const CGObjectInstance * > getBlockingObjs(int3 pos)const;
  188. std::vector <const CGObjectInstance * > getVisitableObjs(int3 pos)const;
  189. std::vector <const CGObjectInstance * > getFlaggableObjects(int3 pos) const;
  190. std::vector <std::string > getObjDescriptions(int3 pos)const; //returns descriptions of objects at pos in order from the lowest to the highest
  191. int getOwner(int heroID) const;
  192. const CGObjectInstance *getObjByQuestIdentifier(int identifier) const; //NULL if object has been removed (eg. killed)
  193. //map
  194. int3 guardingCreaturePosition (int3 pos) const;
  195. const CMapHeader * getMapHeader()const;
  196. int3 getMapSize() const; //returns size of map - z is 1 for one - level map and 2 for two level map
  197. const TerrainTile * getTile(int3 tile, bool verbose = true) const;
  198. //town
  199. const CGTownInstance* getTown(int objid) const;
  200. int howManyTowns(int Player) const;
  201. const CGTownInstance * getTownInfo(int val, bool mode)const; //mode = 0 -> val = player town serial; mode = 1 -> val = object id (serial)
  202. std::vector<const CGHeroInstance *> getAvailableHeroes(const CGObjectInstance * townOrTavern) const; //heroes that can be recruited
  203. std::string getTavernGossip(const CGObjectInstance * townOrTavern) const;
  204. /// 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
  205. int canBuildStructure(const CGTownInstance *t, int ID);
  206. std::set<int> getBuildingRequiments(const CGTownInstance *t, int ID);
  207. virtual bool getTownInfo(const CGObjectInstance *town, InfoAboutTown &dest) const;
  208. const CTown *getNativeTown(int color) const;
  209. //from gs
  210. const TeamState *getTeam(ui8 teamID) const;
  211. const TeamState *getPlayerTeam(ui8 color) const;
  212. std::set<int> getBuildingRequiments(const CGTownInstance *t, int ID) const;
  213. int canBuildStructure(const CGTownInstance *t, int 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
  214. };
  215. class DLL_EXPORT CPlayerSpecificInfoCallback : public CGameInfoCallback
  216. {
  217. public:
  218. int howManyTowns() const;
  219. int howManyHeroes(bool includeGarrisoned = true) const;
  220. int3 getGrailPos(float &outKnownRatio);
  221. int getMyColor() const;
  222. std::vector <const CGTownInstance *> getTownsInfo(bool onlyOur = true) const; //true -> only owned; false -> all visible
  223. int getHeroSerial(const CGHeroInstance * hero)const;
  224. const CGTownInstance* getTownBySerial(int serialId) const; // serial id is [0, number of towns)
  225. const CGHeroInstance* getHeroBySerial(int serialId, bool includeGarrisoned=true) const; // serial id is [0, number of heroes)
  226. std::vector <const CGHeroInstance *> getHeroesInfo(bool onlyOur = true) const; //true -> only owned; false -> all visible
  227. std::vector <const CGDwelling *> getMyDwellings() const; //returns all dwellings that belong to player
  228. std::vector <const CGObjectInstance * > getMyObjects() const; //returns all objects flagged by belonging player
  229. int getResourceAmount(int type)const;
  230. TResources getResourceAmount() const;
  231. const std::vector< std::vector< std::vector<unsigned char> > > & getVisibilityMap()const; //returns visibility map
  232. const PlayerSettings * getPlayerSettings(int color) const;
  233. };
  234. class DLL_EXPORT CPrivilagedInfoCallback : public CGameInfoCallback
  235. {
  236. public:
  237. CGameState *const gameState ();
  238. void getFreeTiles (std::vector<int3> &tiles) const; //used for random spawns
  239. void getTilesInRange(boost::unordered_set<int3, ShashInt3> &tiles, int3 pos, int radious, int player=-1, int mode=0) const; //mode 1 - only unrevealed tiles; mode 0 - all, mode -1 - only unrevealed
  240. void getAllTiles (boost::unordered_set<int3, ShashInt3> &tiles, int player=-1, 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
  241. ui16 getRandomArt (int flags);
  242. ui16 getArtSync (ui32 rand, int flags); //synchronous
  243. void pickAllowedArtsSet(std::vector<const CArtifact*> &out); //gives 3 treasures, 3 minors, 1 major -> used by Black Market and Artifact Merchant
  244. void erasePickedArt (si32 id);
  245. void getAllowedSpells(std::vector<ui16> &out, ui16 level);
  246. };
  247. class DLL_EXPORT CNonConstInfoCallback : public CPrivilagedInfoCallback
  248. {
  249. public:
  250. PlayerState *getPlayer(ui8 color, bool verbose = true);
  251. TeamState *getTeam(ui8 teamID);//get team by team ID
  252. TeamState *getPlayerTeam(ui8 color);// get team by player color
  253. CGHeroInstance *getHero(int objid);
  254. CGTownInstance *getTown(int objid);
  255. TerrainTile * getTile(int3 pos);
  256. };
  257. class DLL_EXPORT IGameEventRealizer
  258. {
  259. public:
  260. virtual void commitPackage(CPackForClient *pack) = 0;
  261. virtual void showInfoDialog(InfoWindow *iw);
  262. virtual void setObjProperty(int objid, int prop, si64 val);
  263. virtual void showInfoDialog(const std::string &msg, int player);
  264. };
  265. class DLL_EXPORT IGameEventCallback : public IGameEventRealizer
  266. {
  267. public:
  268. virtual void changeSpells(int hid, bool give, const std::set<ui32> &spells)=0;
  269. virtual bool removeObject(int objid)=0;
  270. virtual void setBlockVis(int objid, bool bv)=0;
  271. virtual void setOwner(int objid, ui8 owner)=0;
  272. virtual void setHoverName(int objid, MetaString * name)=0;
  273. virtual void changePrimSkill(int ID, int which, si64 val, bool abs=false)=0;
  274. virtual void changeSecSkill(int ID, int which, int val, bool abs=false)=0;
  275. virtual void showBlockingDialog(BlockingDialog *iw, const CFunctionList<void(ui32)> &callback)=0;
  276. virtual ui32 showBlockingDialog(BlockingDialog *iw) =0; //synchronous version of above //TODO:
  277. virtual void showGarrisonDialog(int upobj, int hid, bool removableUnits, const boost::function<void()> &cb) =0; //cb will be called when player closes garrison window
  278. virtual void showThievesGuildWindow(int requestingObjId) =0;
  279. virtual void giveResource(int player, int which, int val)=0;
  280. virtual void giveCreatures(const CArmedInstance *objid, const CGHeroInstance * h, const CCreatureSet &creatures, bool remove) =0;
  281. virtual void takeCreatures(int objid, const std::vector<CStackBasicDescriptor> &creatures) =0;
  282. virtual bool changeStackCount(const StackLocation &sl, TQuantity count, bool absoluteValue = false) =0;
  283. virtual bool changeStackType(const StackLocation &sl, CCreature *c) =0;
  284. virtual bool insertNewStack(const StackLocation &sl, const CCreature *c, TQuantity count = -1) =0; //count -1 => moves whole stack
  285. virtual bool eraseStack(const StackLocation &sl, bool forceRemoval = false) =0;
  286. virtual bool swapStacks(const StackLocation &sl1, const StackLocation &sl2) =0;
  287. virtual bool addToSlot(const StackLocation &sl, const CCreature *c, TQuantity count) =0; //makes new stack or increases count of already existing
  288. 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
  289. virtual bool moveStack(const StackLocation &src, const StackLocation &dst, TQuantity count) = 0;
  290. virtual void giveHeroNewArtifact(const CGHeroInstance *h, const CArtifact *artType, int pos) = 0;
  291. virtual void giveHeroArtifact(const CGHeroInstance *h, const CArtifactInstance *a, int pos) = 0; //pos==-1 - first free slot in backpack=0; pos==-2 - default if available or backpack
  292. virtual void putArtifact(const ArtifactLocation &al, const CArtifactInstance *a) = 0;
  293. virtual void removeArtifact(const ArtifactLocation &al) = 0;
  294. virtual void moveArtifact(const ArtifactLocation &al1, const ArtifactLocation &al2) = 0;
  295. virtual void showCompInfo(ShowInInfobox * comp)=0;
  296. virtual void heroVisitCastle(int obj, int heroID)=0;
  297. virtual void stopHeroVisitCastle(int obj, int heroID)=0;
  298. virtual void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank = false, boost::function<void(BattleResult*)> cb = 0, const CGTownInstance *town = NULL)=0; //use hero=NULL for no hero
  299. virtual void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false)=0; //if any of armies is hero, hero will be used
  300. virtual void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false)=0; //if any of armies is hero, hero will be used, visitable tile of second obj is place of battle
  301. virtual void setAmount(int objid, ui32 val)=0;
  302. virtual bool moveHero(si32 hid, int3 dst, ui8 instant, ui8 asker = 255)=0;
  303. virtual void giveHeroBonus(GiveBonus * bonus)=0;
  304. virtual void setMovePoints(SetMovePoints * smp)=0;
  305. virtual void setManaPoints(int hid, int val)=0;
  306. virtual void giveHero(int id, int player)=0;
  307. virtual void changeObjPos(int objid, int3 newPos, ui8 flags)=0;
  308. virtual void sendAndApply(CPackForClient * info)=0;
  309. virtual void heroExchange(si32 hero1, si32 hero2)=0; //when two heroes meet on adventure map
  310. };
  311. /// Interface class for handling general game logic and actions
  312. class DLL_EXPORT IGameCallback : public CPrivilagedInfoCallback, public IGameEventCallback
  313. {
  314. public:
  315. virtual ~IGameCallback(){};
  316. //do sth
  317. const CGObjectInstance *putNewObject(int ID, int subID, int3 pos);
  318. const CGCreature *putNewMonster(int creID, int count, int3 pos);
  319. friend struct CPack;
  320. friend struct CPackForClient;
  321. friend struct CPackForServer;
  322. };
  323. #endif // __IGAMECALLBACK_H__