CCallback.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #ifndef __CCALLBACK_H__
  2. #define __CCALLBACK_H__
  3. #include "global.h"
  4. #ifdef _WIN32
  5. #include "tchar.h"
  6. #else
  7. #include "tchar_amigaos4.h" //XXX this is mingw header are we need this for something? for 'true'
  8. //support of unicode we should use ICU or some boost wraper areound it
  9. //(boost using this lib during compilation i dont know what for exactly)
  10. #endif
  11. #include "lib/CGameState.h"
  12. /*
  13. * CCallback.h, part of VCMI engine
  14. *
  15. * Authors: listed in file AUTHORS in main folder
  16. *
  17. * License: GNU General Public License v2.0 or later
  18. * Full text of license available in license.txt file, in main folder
  19. *
  20. */
  21. class CGHeroInstance;
  22. class CGameState;
  23. struct CPath;
  24. class CGObjectInstance;
  25. class CArmedInstance;
  26. class SComponent;
  27. class IChosen;
  28. class CSelectableComponent;
  29. struct BattleAction;
  30. class CGTownInstance;
  31. struct StartInfo;
  32. class CStack;
  33. struct lua_State;
  34. class CClient;
  35. struct TerrainTile;
  36. class CHeroClass;
  37. class IShipyard;
  38. struct CPackForServer;
  39. class CMapHeader;
  40. struct CGPathNode;
  41. struct CGPath;
  42. struct InfoAboutHero
  43. {
  44. struct Details
  45. {
  46. std::vector<int> primskills;
  47. int mana, luck, morale;
  48. } *details;
  49. char owner;
  50. const CHeroClass *hclass;
  51. std::string name;
  52. int portrait;
  53. CCreatureSet army; //numbers of creatures are exact numbers if detailed else they are quantity ids (0 - a few, 1 - several and so on)
  54. InfoAboutHero();
  55. ~InfoAboutHero();
  56. void initFromHero(const CGHeroInstance *h, bool detailed);
  57. };
  58. struct InfoAboutTown
  59. {
  60. struct Details
  61. {
  62. int hallLevel, goldIncome;
  63. bool customRes;
  64. bool garrisonedHero;
  65. } *details;
  66. const CArmedInstance * obj;
  67. char fortLevel; //0 - none
  68. char owner;
  69. std::string name;
  70. CTown *tType;
  71. bool built;
  72. CCreatureSet army; //numbers of creatures are valid only if details
  73. InfoAboutTown();
  74. ~InfoAboutTown();
  75. void initFromTown(const CGTownInstance *t, bool detailed);
  76. };
  77. class ICallback
  78. {
  79. public:
  80. bool waitTillRealize; //if true, request functions will return after they are realized by server
  81. //hero
  82. virtual bool moveHero(const CGHeroInstance *h, int3 dst) =0; //dst must be free, neighbouring tile (this function can move hero only by one tile)
  83. virtual bool dismissHero(const CGHeroInstance * hero)=0; //dismisses given hero; true - successfuly, false - not successfuly
  84. //town
  85. virtual void recruitHero(const CGTownInstance *town, const CGHeroInstance *hero)=0;
  86. virtual bool buildBuilding(const CGTownInstance *town, si32 buildingID)=0;
  87. virtual void recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount)=0;
  88. virtual bool upgradeCreature(const CArmedInstance *obj, int stackPos, int newID=-1)=0; //if newID==-1 then best possible upgrade will be made
  89. virtual void swapGarrisonHero(const CGTownInstance *town)=0;
  90. virtual void trade(int mode, int id1, int id2, int val1)=0; //mode==0: sell val1 units of id1 resource for id2 resiurce
  91. virtual void selectionMade(int selection, int asker) =0;
  92. virtual int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)=0;//swaps creatures between two posiibly different garrisons // TODO: AI-unsafe code - fix it!
  93. virtual int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)=0;//joins first stack tothe second (creatures must be same type)
  94. virtual int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)=0;//split creatures from the first stack
  95. virtual bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)=0; //swaps artifacts between two given heroes
  96. virtual bool dismissCreature(const CArmedInstance *obj, int stackPos)=0;
  97. virtual void endTurn()=0;
  98. virtual void buyArtifact(const CGHeroInstance *hero, int aid)=0; //used to buy artifacts in towns (including spell book in the guild and war machines in blacksmith)
  99. virtual void setFormation(const CGHeroInstance * hero, bool tight)=0;
  100. virtual void setSelection(const CArmedInstance * obj)=0;
  101. virtual void save(const std::string &fname) = 0;
  102. virtual void sendMessage(const std::string &mess) = 0;
  103. virtual void buildBoat(const IShipyard *obj) = 0;
  104. //get info
  105. virtual bool verifyPath(CPath * path, bool blockSea)const =0;
  106. virtual int getDate(int mode=0)const =0; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
  107. virtual std::vector< std::vector< std::vector<unsigned char> > > & getVisibilityMap()const =0; //returns visibility map (TODO: make it const)
  108. virtual int getResourceAmount(int type)const =0;
  109. virtual bool isVisible(int3 pos)const =0;
  110. virtual int getMyColor()const =0;
  111. virtual int getMySerial()const =0;
  112. virtual int getHeroSerial(const CGHeroInstance * hero)const =0;
  113. virtual const StartInfo * getStartInfo()const =0;
  114. virtual const CMapHeader * getMapHeader()const =0;
  115. virtual int getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const =0; //when called during battle, takes into account creatures' spell cost reduction
  116. virtual int estimateSpellDamage(const CSpell * sp) const =0; //estimates damage of given spell; returns 0 if spell causes no dmg
  117. //hero
  118. virtual int howManyHeroes(bool includeGarrisoned = true)const =0;
  119. virtual const CGHeroInstance * getHeroInfo(int val, int mode=2)const =0; //mode = 0 -> val = serial; mode = 1 -> val = ID
  120. virtual std::vector < const CGHeroInstance *> getHeroesInfo(bool onlyOur=true)const =0;
  121. virtual bool getHeroInfo(const CGObjectInstance *hero, InfoAboutHero &dest) const = 0;
  122. virtual bool getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)=0;
  123. virtual const CGPathNode *getPathInfo(int3 tile)=0; //uses main, client pathfinder info
  124. virtual bool getPath2(int3 dest, CGPath &ret)=0; //uses main, client pathfinder info
  125. virtual void calculatePaths(const CGHeroInstance *hero, CPathsInfo &out, int3 src = int3(-1,-1,-1), int movement = -1) =0;
  126. virtual void recalculatePaths()=0; //updates main, client pathfinder info (should be called when moving hero is over)
  127. //map
  128. virtual std::vector < const CGObjectInstance * > getBlockingObjs(int3 pos)const =0;
  129. virtual std::vector < const CGObjectInstance * > getVisitableObjs(int3 pos)const =0;
  130. virtual std::vector < const CGObjectInstance * > getFlaggableObjects(int3 pos) const =0;
  131. virtual std::vector < std::string > getObjDescriptions(int3 pos)const =0; //returns descriptions of objects at pos in order from the lowest to the highest
  132. //town
  133. virtual int howManyTowns()const =0;
  134. virtual const CGTownInstance * getTownInfo(int val, bool mode)const =0; //mode = 0 -> val = serial; mode = 1 -> val = ID
  135. virtual std::vector < const CGTownInstance *> getTownsInfo(bool onlyOur=true) const=0;
  136. virtual std::vector<const CGHeroInstance *> getAvailableHeroes(const CGTownInstance * town) const =0; //heroes that can be recruited
  137. virtual int canBuildStructure(const CGTownInstance *t, int ID) =0;//// 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
  138. virtual void getMarketOffer(int t1, int t2, int &give, int &rec, int mode=0)const =0; //t1 - type of given resource, t2 - type of received resource; give is the amount of resource t1 that can be traded for amount rec of resource t2 (one of them is 1)
  139. virtual bool getTownInfo(const CGObjectInstance *town, InfoAboutTown &dest) const = 0;
  140. virtual UpgradeInfo getUpgradeInfo(const CArmedInstance *obj, int stackPos)const =0;
  141. virtual const CCreatureSet* getGarrison(const CGObjectInstance *obj)const =0;
  142. virtual int3 getMapSize() const =0; //returns size of map - z is 1 for one - level map and 2 for two level map
  143. virtual const TerrainTile * getTileInfo(int3 tile) const = 0;
  144. //battle
  145. virtual int battleGetBattlefieldType()=0; // 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
  146. virtual int battleGetObstaclesAtTile(int tile)=0; //returns bitfield
  147. virtual std::vector<CObstacleInstance> battleGetAllObstacles()=0; //returns all obstacles on the battlefield
  148. virtual int battleGetStack(int pos, bool onlyAlive)=0; //returns ID of stack on the tile
  149. virtual const CStack * battleGetStackByID(int ID, bool onlyAlive = true)=0; //returns stack info by given ID
  150. virtual const CStack * battleGetStackByPos(int pos, bool onlyAlive = true)=0; //returns stack info by given pos
  151. virtual int battleGetPos(int stack)=0; //returns position (tile ID) of stack
  152. virtual int battleMakeAction(BattleAction* action)=0;//for casting spells by hero - DO NOT use it for moving active stack
  153. virtual std::map<int, CStack> battleGetStacks()=0; //returns stacks on battlefield
  154. virtual void getStackQueue( std::vector<const CStack *> &out, int howMany )=0; //returns vector of stack in order of their move sequence
  155. virtual CCreature battleGetCreature(int number)=0; //returns type of creature by given number of stack
  156. //virtual bool battleMoveCreature(int ID, int dest)=0; //moves creature with id ID to dest if possible
  157. virtual std::vector<int> battleGetAvailableHexes(int ID, bool addOccupiable)=0; //reutrns numbers of hexes reachable by creature with id ID
  158. virtual bool battleIsStackMine(int ID)=0; //returns true if stack with id ID belongs to caller
  159. virtual bool battleCanShoot(int ID, int dest)=0; //returns true if unit with id ID can shoot to dest
  160. virtual bool battleCanCastSpell()=0; //returns true, if caller can cast a spell
  161. virtual bool battleCanFlee()=0; //returns true if caller can flee from the battle
  162. virtual const CGTownInstance * battleGetDefendedTown()=0; //returns defended town if current battle is a siege, NULL instead
  163. virtual ui8 battleGetWallState(int partOfWall)=0; //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
  164. virtual int battleGetWallUnderHex(int hex)=0; //returns part of destructible wall / gate / keep under given hex or -1 if not found
  165. virtual std::pair<ui32, ui32> battleEstimateDamage(int attackerID, int defenderID)=0; //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>
  166. virtual ui8 battleGetSiegeLevel()=0; //returns 0 when there is no siege, 1 if fort, 2 is citadel, 3 is castle
  167. virtual const CGHeroInstance * battleGetFightingHero(ui8 side) const =0; //returns hero corresponding ot given side (0 - attacker, 1 - defender)
  168. };
  169. struct HeroMoveDetails
  170. {
  171. HeroMoveDetails(){};
  172. HeroMoveDetails(int3 Src, int3 Dst, CGHeroInstance*Ho);
  173. int3 src, dst; //source and destination points
  174. CGHeroInstance * ho; //object instance of this hero
  175. int owner, style; //style: 0 - normal move, 1 - teleport, 2 - instant jump
  176. bool successful;
  177. };
  178. class CCallback : public ICallback
  179. {
  180. private:
  181. CCallback(CGameState * GS, int Player, CClient *C);;
  182. CGameState * gs;
  183. CClient *cl;
  184. bool isVisible(int3 pos, int Player) const;
  185. bool isVisible(const CGObjectInstance *obj, int Player) const;
  186. template <typename T> void sendRequest(const T*request);
  187. protected:
  188. int player;
  189. public:
  190. //commands
  191. bool moveHero(const CGHeroInstance *h, int3 dst); //dst must be free, neighbouring tile (this function can move hero only by one tile)
  192. void selectionMade(int selection, int asker);
  193. int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2);
  194. int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2); //first goes to the second
  195. int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val);
  196. bool dismissHero(const CGHeroInstance * hero);
  197. bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2);
  198. bool buildBuilding(const CGTownInstance *town, si32 buildingID);
  199. void recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount);
  200. bool dismissCreature(const CArmedInstance *obj, int stackPos);
  201. bool upgradeCreature(const CArmedInstance *obj, int stackPos, int newID=-1);
  202. void endTurn();
  203. void swapGarrisonHero(const CGTownInstance *town);
  204. void buyArtifact(const CGHeroInstance *hero, int aid);
  205. void trade(int mode, int id1, int id2, int val1);
  206. void setFormation(const CGHeroInstance * hero, bool tight);
  207. void setSelection(const CArmedInstance * obj);
  208. void recruitHero(const CGTownInstance *town, const CGHeroInstance *hero);
  209. void save(const std::string &fname);
  210. void sendMessage(const std::string &mess);
  211. void buildBoat(const IShipyard *obj);
  212. //get info
  213. bool verifyPath(CPath * path, bool blockSea) const;
  214. 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
  215. std::vector< std::vector< std::vector<unsigned char> > > & getVisibilityMap() const; //returns visibility map (TODO: make it const)
  216. const CGHeroInstance * getHeroInfo(int val, int mode=2) const; //mode = 0 -> val = serial; mode = 1 -> val = hero type id (subID); mode = 2 -> val = global object serial id (id)
  217. const CGObjectInstance * getObjectInfo(int ID) const; //global object serial id (ID)
  218. int getResourceAmount(int type) const;
  219. std::vector<si32> getResourceAmount() const;
  220. int howManyHeroes(bool includeGarrisoned = true) const;
  221. const CGTownInstance * getTownInfo(int val, bool mode) const; //mode = 0 -> val = serial; mode = 1 -> val = ID
  222. std::vector < const CGTownInstance *> getTownsInfo(bool onlyOur=true) const;
  223. int howManyTowns()const;
  224. std::vector < std::string > getObjDescriptions(int3 pos) const; //returns descriptions of objects at pos in order from the lowest to the highest
  225. std::vector < const CGHeroInstance *> getHeroesInfo(bool onlyOur=true) const;
  226. bool isVisible(int3 pos) const;
  227. int getMyColor() const;
  228. int getHeroSerial(const CGHeroInstance * hero) const;
  229. int getMySerial() const;
  230. const CCreatureSet* getGarrison(const CGObjectInstance *obj) const;
  231. UpgradeInfo getUpgradeInfo(const CArmedInstance *obj, int stackPos) const;
  232. const StartInfo * getStartInfo() const;
  233. const CMapHeader * getMapHeader()const ;
  234. int getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const; //when called during battle, takes into account creatures' spell cost reduction
  235. int estimateSpellDamage(const CSpell * sp) const; //estimates damage of given spell; returns 0 if spell causes no dmg
  236. std::vector < const CGObjectInstance * > getBlockingObjs(int3 pos) const;
  237. std::vector < const CGObjectInstance * > getVisitableObjs(int3 pos) const;
  238. void getMarketOffer(int t1, int t2, int &give, int &rec, int mode=0) const; //t1 - type of given resource, t2 - type of received resource; give is the amount of resource t1 that can be traded for amount rec of resource t2 (one of them is 1)
  239. std::vector < const CGObjectInstance * > getFlaggableObjects(int3 pos) const;
  240. int3 getMapSize() const; //returns size of map - z is 1 for one - level map and 2 for two level map
  241. std::vector<const CGHeroInstance *> getAvailableHeroes(const CGTownInstance * town) const; //heroes that can be recruited
  242. const TerrainTile * getTileInfo(int3 tile) const;
  243. int canBuildStructure(const CGTownInstance *t, int 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
  244. bool getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret);
  245. const CGPathNode *getPathInfo(int3 tile);
  246. bool getPath2(int3 dest, CGPath &ret);
  247. void calculatePaths(const CGHeroInstance *hero, CPathsInfo &out, int3 src = int3(-1,-1,-1), int movement = -1); //calculates possible paths for hero, by default uses current hero position and movement left;
  248. void recalculatePaths(); //updates pathfinder info (should be called when moving hero is over)
  249. bool getHeroInfo(const CGObjectInstance *hero, InfoAboutHero &dest) const;
  250. bool getTownInfo(const CGObjectInstance *town, InfoAboutTown &dest) const;
  251. //battle
  252. int battleGetBattlefieldType(); // 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
  253. int battleGetObstaclesAtTile(int tile); //returns bitfield
  254. std::vector<CObstacleInstance> battleGetAllObstacles(); //returns all obstacles on the battlefield
  255. int battleGetStack(int pos, bool onlyAlive = true); //returns ID of stack on the tile
  256. const CStack * battleGetStackByID(int ID, bool onlyAlive = true); //returns stack info by given ID
  257. const CStack * battleGetStackByPos(int pos, bool onlyAlive = true); //returns stack info by given pos
  258. int battleGetPos(int stack); //returns position (tile ID) of stack
  259. int battleMakeAction(BattleAction* action);//for casting spells by hero - DO NOT use it for moving active stack
  260. std::map<int, CStack> battleGetStacks(); //returns stacks on battlefield
  261. void getStackQueue( std::vector<const CStack *> &out, int howMany ); //returns vector of stack in order of their move sequence
  262. CCreature battleGetCreature(int number); //returns type of creature by given number of stack
  263. std::vector<int> battleGetAvailableHexes(int ID, bool addOccupiable); //reutrns numbers of hexes reachable by creature with id ID
  264. bool battleIsStackMine(int ID); //returns true if stack with id ID belongs to caller
  265. bool battleCanShoot(int ID, int dest); //returns true if unit with id ID can shoot to dest
  266. bool battleCanCastSpell(); //returns true, if caller can cast a spell
  267. bool battleCanFlee(); //returns true if caller can flee from the battle
  268. const CGTownInstance * battleGetDefendedTown(); //returns defended town if current battle is a siege, NULL instead
  269. ui8 battleGetWallState(int partOfWall); //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
  270. int battleGetWallUnderHex(int hex); //returns part of destructible wall / gate / keep under given hex or -1 if not found
  271. std::pair<ui32, ui32> battleEstimateDamage(int attackerID, int defenderID); //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>
  272. ui8 battleGetSiegeLevel(); //returns 0 when there is no siege, 1 if fort, 2 is citadel, 3 is castle
  273. const CGHeroInstance * battleGetFightingHero(ui8 side) const; //returns hero corresponding ot given side (0 - attacker, 1 - defender)
  274. //XXX hmmm _tmain on _GNUC_ wtf?
  275. //friends
  276. friend class CClient;
  277. #ifndef __GNUC__
  278. friend int _tmain(int argc, _TCHAR* argv[]);
  279. #else
  280. friend int main(int argc, char** argv);
  281. #endif
  282. };
  283. #endif // __CCALLBACK_H__