CCallback.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 InfoAboutHero
  39. {
  40. struct Details
  41. {
  42. std::vector<int> primskills;
  43. int mana, luck, morale;
  44. } *details;
  45. char owner;
  46. const CHeroClass *hclass;
  47. std::string name;
  48. int portrait;
  49. CCreatureSet army; //numbers of creatures are exact numbers if detailed else they are quantity ids (0 - a few, 1 - several and so on)
  50. InfoAboutHero();
  51. ~InfoAboutHero();
  52. void initFromHero(const CGHeroInstance *h, bool detailed);
  53. };
  54. struct InfoAboutTown
  55. {
  56. struct Details
  57. {
  58. int hallLevel, goldIncome;
  59. bool customRes;
  60. bool garrisonedHero;
  61. } *details;
  62. char fortLevel; //0 - none
  63. char owner;
  64. std::string name;
  65. CTown *tType;
  66. bool built;
  67. CCreatureSet army; //numbers of creatures are valid only if details
  68. InfoAboutTown();
  69. ~InfoAboutTown();
  70. void initFromTown(const CGTownInstance *t, bool detailed);
  71. };
  72. class ICallback
  73. {
  74. public:
  75. virtual bool moveHero(const CGHeroInstance *h, int3 dst) const =0; //dst must be free, neighbouring tile (this function can move hero only by one tile)
  76. virtual void selectionMade(int selection, int asker) =0;
  77. 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!
  78. virtual int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)=0;//joins first stack tothe second (creatures must be same type)
  79. virtual int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)=0;//split creatures from the first stack
  80. virtual bool dismissHero(const CGHeroInstance * hero)=0; //dismisses diven hero; true - successfuly, false - not successfuly
  81. virtual bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)=0; //swaps artifacts between two given heroes
  82. virtual void recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount)=0;
  83. virtual bool dismissCreature(const CArmedInstance *obj, int stackPos)=0;
  84. virtual bool upgradeCreature(const CArmedInstance *obj, int stackPos, int newID=-1)=0; //if newID==-1 then best possible upgrade will be made
  85. virtual void endTurn()=0;
  86. virtual void swapGarrisonHero(const CGTownInstance *town)=0;
  87. 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)
  88. virtual void trade(int mode, int id1, int id2, int val1)=0; //mode==0: sell val1 units of id1 resource for id2 resiurce
  89. virtual void setFormation(const CGHeroInstance * hero, bool tight)=0;
  90. virtual void setSelection(const CArmedInstance * obj)=0;
  91. virtual void recruitHero(const CGTownInstance *town, const CGHeroInstance *hero)=0;
  92. virtual void save(const std::string &fname) = 0;
  93. virtual void sendMessage(const std::string &mess) = 0;
  94. virtual void buildBoat(const IShipyard *obj) = 0;
  95. //get info
  96. virtual bool verifyPath(CPath * path, bool blockSea)const =0;
  97. 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
  98. virtual std::vector< std::vector< std::vector<unsigned char> > > & getVisibilityMap()const =0; //returns visibility map (TODO: make it const)
  99. virtual const CGHeroInstance * getHeroInfo(int val, int mode=2)const =0; //mode = 0 -> val = serial; mode = 1 -> val = ID
  100. virtual int getResourceAmount(int type)const =0;
  101. virtual int howManyHeroes(bool includeGarrisoned = true)const =0;
  102. virtual const CGTownInstance * getTownInfo(int val, bool mode)const =0; //mode = 0 -> val = serial; mode = 1 -> val = ID
  103. virtual int howManyTowns()const =0;
  104. virtual std::vector < std::string > getObjDescriptions(int3 pos)const =0; //returns descriptions of objects at pos in order from the lowest to the highest
  105. virtual std::vector < const CGHeroInstance *> getHeroesInfo(bool onlyOur=true)const =0;
  106. virtual bool isVisible(int3 pos)const =0;
  107. virtual int getMyColor()const =0;
  108. virtual int getMySerial()const =0;
  109. virtual int getHeroSerial(const CGHeroInstance * hero)const =0;
  110. virtual const CCreatureSet* getGarrison(const CGObjectInstance *obj)const =0;
  111. virtual UpgradeInfo getUpgradeInfo(const CArmedInstance *obj, int stackPos)const =0;
  112. virtual const StartInfo * getStartInfo()const =0;
  113. virtual std::vector < const CGObjectInstance * > getBlockingObjs(int3 pos)const =0;
  114. virtual std::vector < const CGObjectInstance * > getVisitableObjs(int3 pos)const =0;
  115. 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)
  116. virtual std::vector < const CGObjectInstance * > getFlaggableObjects(int3 pos) const =0;
  117. virtual int3 getMapSize() const =0; //returns size of map - z is 1 for one - level map and 2 for two level map
  118. virtual std::vector<const CGHeroInstance *> getAvailableHeroes(const CGTownInstance * town) const =0; //heroes that can be recruited
  119. virtual const TerrainTile * getTileInfo(int3 tile) const = 0;
  120. 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
  121. virtual bool getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)=0;
  122. virtual bool getHeroInfo(const CGObjectInstance *hero, InfoAboutHero &dest) const = 0;
  123. virtual bool getTownInfo(const CGObjectInstance *town, InfoAboutTown &dest) const = 0;
  124. //battle
  125. 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
  126. virtual int battleGetObstaclesAtTile(int tile)=0; //returns bitfield
  127. virtual std::vector<CObstacleInstance> battleGetAllObstacles()=0; //returns all obstacles on the battlefield
  128. virtual int battleGetStack(int pos)=0; //returns ID of stack on the tile
  129. virtual CStack * battleGetStackByID(int ID)=0; //returns stack info by given ID
  130. virtual CStack * battleGetStackByPos(int pos)=0; //returns stack info by given pos
  131. virtual int battleGetPos(int stack)=0; //returns position (tile ID) of stack
  132. virtual int battleMakeAction(BattleAction* action)=0;//for casting spells by hero - DO NOT use it for moving active stack
  133. virtual std::map<int, CStack> battleGetStacks()=0; //returns stacks on battlefield
  134. virtual std::vector<CStack> battleGetStackQueue()=0; //returns vector of stack in order of their move sequence
  135. virtual CCreature battleGetCreature(int number)=0; //returns type of creature by given number of stack
  136. //virtual bool battleMoveCreature(int ID, int dest)=0; //moves creature with id ID to dest if possible
  137. virtual std::vector<int> battleGetAvailableHexes(int ID, bool addOccupiable)=0; //reutrns numbers of hexes reachable by creature with id ID
  138. virtual bool battleIsStackMine(int ID)=0; //returns true if stack with id ID belongs to caller
  139. virtual bool battleCanShoot(int ID, int dest)=0; //returns true if unit with id ID can shoot to dest
  140. virtual bool battleCanCastSpell()=0; //returns true, if caller can cast a spell
  141. };
  142. struct HeroMoveDetails
  143. {
  144. HeroMoveDetails(){};
  145. HeroMoveDetails(int3 Src, int3 Dst, CGHeroInstance*Ho);
  146. int3 src, dst; //source and destination points
  147. CGHeroInstance * ho; //object instance of this hero
  148. int owner, style; //style: 0 - normal move, 1 - teleport, 2 - instant jump
  149. bool successful;
  150. };
  151. class CCallback : public ICallback
  152. {
  153. private:
  154. CCallback(CGameState * GS, int Player, CClient *C):gs(GS), cl(C), player(Player){};
  155. CGameState * gs;
  156. CClient *cl;
  157. bool isVisible(int3 pos, int Player) const;
  158. bool isVisible(const CGObjectInstance *obj, int Player) const;
  159. protected:
  160. int player;
  161. public:
  162. //commands
  163. bool moveHero(const CGHeroInstance *h, int3 dst) const; //dst must be free, neighbouring tile (this function can move hero only by one tile)
  164. void selectionMade(int selection, int asker);
  165. int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2);
  166. int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2); //first goes to the second
  167. int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val);
  168. bool dismissHero(const CGHeroInstance * hero);
  169. bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2);
  170. bool buildBuilding(const CGTownInstance *town, si32 buildingID);
  171. void recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount);
  172. bool dismissCreature(const CArmedInstance *obj, int stackPos);
  173. bool upgradeCreature(const CArmedInstance *obj, int stackPos, int newID=-1);
  174. void endTurn();
  175. void swapGarrisonHero(const CGTownInstance *town);
  176. void buyArtifact(const CGHeroInstance *hero, int aid);
  177. void trade(int mode, int id1, int id2, int val1);
  178. void setFormation(const CGHeroInstance * hero, bool tight);
  179. void setSelection(const CArmedInstance * obj);
  180. void recruitHero(const CGTownInstance *town, const CGHeroInstance *hero);
  181. void save(const std::string &fname);
  182. void sendMessage(const std::string &mess);
  183. void buildBoat(const IShipyard *obj);
  184. //get info
  185. bool verifyPath(CPath * path, bool blockSea) const;
  186. 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
  187. std::vector< std::vector< std::vector<unsigned char> > > & getVisibilityMap() const; //returns visibility map (TODO: make it const)
  188. 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)
  189. int getResourceAmount(int type) const;
  190. std::vector<si32> getResourceAmount() const;
  191. int howManyHeroes(bool includeGarrisoned = true) const;
  192. const CGTownInstance * getTownInfo(int val, bool mode) const; //mode = 0 -> val = serial; mode = 1 -> val = ID
  193. std::vector < const CGTownInstance *> getTownsInfo(bool onlyOur=true) const;
  194. int howManyTowns()const;
  195. std::vector < std::string > getObjDescriptions(int3 pos) const; //returns descriptions of objects at pos in order from the lowest to the highest
  196. std::vector < const CGHeroInstance *> getHeroesInfo(bool onlyOur=true) const;
  197. bool isVisible(int3 pos) const;
  198. int getMyColor() const;
  199. int getHeroSerial(const CGHeroInstance * hero) const;
  200. int getMySerial() const;
  201. const CCreatureSet* getGarrison(const CGObjectInstance *obj) const;
  202. UpgradeInfo getUpgradeInfo(const CArmedInstance *obj, int stackPos) const;
  203. const StartInfo * getStartInfo() const;
  204. std::vector < const CGObjectInstance * > getBlockingObjs(int3 pos) const;
  205. std::vector < const CGObjectInstance * > getVisitableObjs(int3 pos) const;
  206. 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)
  207. std::vector < const CGObjectInstance * > getFlaggableObjects(int3 pos) const;
  208. int3 getMapSize() const; //returns size of map - z is 1 for one - level map and 2 for two level map
  209. std::vector<const CGHeroInstance *> getAvailableHeroes(const CGTownInstance * town) const; //heroes that can be recruited
  210. const TerrainTile * getTileInfo(int3 tile) const;
  211. 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
  212. bool getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret);
  213. bool getHeroInfo(const CGObjectInstance *hero, InfoAboutHero &dest) const;
  214. bool getTownInfo(const CGObjectInstance *town, InfoAboutTown &dest) const;
  215. //battle
  216. 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
  217. int battleGetObstaclesAtTile(int tile); //returns bitfield
  218. std::vector<CObstacleInstance> battleGetAllObstacles(); //returns all obstacles on the battlefield
  219. int battleGetStack(int pos); //returns ID of stack on the tile
  220. CStack * battleGetStackByID(int ID); //returns stack info by given ID
  221. CStack * battleGetStackByPos(int pos); //returns stack info by given pos
  222. int battleGetPos(int stack); //returns position (tile ID) of stack
  223. int battleMakeAction(BattleAction* action);//for casting spells by hero - DO NOT use it for moving active stack
  224. std::map<int, CStack> battleGetStacks(); //returns stacks on battlefield
  225. std::vector<CStack> battleGetStackQueue(); //returns vector of stack in order of their move sequence
  226. CCreature battleGetCreature(int number); //returns type of creature by given number of stack
  227. std::vector<int> battleGetAvailableHexes(int ID, bool addOccupiable); //reutrns numbers of hexes reachable by creature with id ID
  228. bool battleIsStackMine(int ID); //returns true if stack with id ID belongs to caller
  229. bool battleCanShoot(int ID, int dest); //returns true if unit with id ID can shoot to dest
  230. bool battleCanCastSpell(); //returns true, if caller can cast a spell
  231. //XXX hmmm _tmain on _GNUC_ wtf?
  232. //friends
  233. friend class CClient;
  234. #ifndef __GNUC__
  235. friend int _tmain(int argc, _TCHAR* argv[]);
  236. #else
  237. friend int main(int argc, char** argv);
  238. #endif
  239. };
  240. #endif // __CCALLBACK_H__