CCallback.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #ifndef CCALLBACK_H
  2. #define CCALLBACK_H
  3. #include "mapHandler.h"
  4. #ifdef _WIN32
  5. #include "tchar.h"
  6. #else
  7. #include "tchar_amigaos4.h"
  8. #endif
  9. #include "CGameState.h"
  10. class CGameState;
  11. struct CPath;
  12. class CGObjectInstance;
  13. class SComponent;
  14. class IChosen;
  15. class CSelectableComponent;
  16. struct BattleAction;
  17. typedef struct lua_State lua_State;
  18. //structure gathering info about upgrade possibilites
  19. struct UpgradeInfo
  20. {
  21. int oldID; //creature to be upgraded
  22. std::vector<int> newID; //possible upgrades
  23. std::vector<std::set<std::pair<int,int> > > cost; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>
  24. UpgradeInfo(){oldID = -1;};
  25. };
  26. class ICallback
  27. {
  28. public:
  29. virtual bool moveHero(int ID, CPath * path, int idtype, int pathType=0)=0;//idtype: 0 - position in vector of heroes (of that player); 1 - ID of hero
  30. //pathType: 0 - nodes are manifestation pos, 1 - nodes are object pos
  31. virtual int swapCreatures(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)=0;//swaps creatures between two posiibly different garrisons // TODO: AI-unsafe code - fix it!
  32. virtual int mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)=0;//joins first stack tothe second (creatures must be same type)
  33. virtual int splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val)=0;//split creatures from the first stack
  34. virtual bool dismissHero(const CGHeroInstance * hero)=0; //dismisses diven hero; true - successfuly, false - not successfuly
  35. virtual bool swapArifacts(const CGHeroInstance * hero1, bool worn1, int pos1, const CGHeroInstance * hero2, bool worn2, int pos2)=0; //swaps artifacts between two given heroes
  36. virtual void recruitCreatures(const CGObjectInstance *obj, int ID, int amount)=0;
  37. virtual bool dismissCreature(const CArmedInstance *obj, int stackPos)=0;
  38. virtual bool upgradeCreature(const CArmedInstance *obj, int stackPos, int newID=-1)=0; //if newID==-1 then best possible upgrade will be made
  39. //get info
  40. virtual bool verifyPath(CPath * path, bool blockSea)=0;
  41. virtual int getDate(int mode=0)=0; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
  42. virtual PseudoV< PseudoV< PseudoV<unsigned char> > > & getVisibilityMap()=0; //returns visibility map (TODO: make it const)
  43. virtual const CGHeroInstance * getHeroInfo(int player, int val, bool mode)=0; //mode = 0 -> val = serial; mode = 1 -> val = ID
  44. virtual int getResourceAmount(int type)=0;
  45. virtual int howManyHeroes()=0;
  46. virtual const CGTownInstance * getTownInfo(int val, bool mode)=0; //mode = 0 -> val = serial; mode = 1 -> val = ID
  47. virtual int howManyTowns()=0;
  48. virtual std::vector < std::string > getObjDescriptions(int3 pos)=0; //returns descriptions of objects at pos in order from the lowest to the highest
  49. virtual std::vector < const CGHeroInstance *> getHeroesInfo(bool onlyOur=true)=0;
  50. virtual bool isVisible(int3 pos)=0;
  51. virtual int getMyColor()=0;
  52. virtual int getMySerial()=0;
  53. virtual int getHeroSerial(const CGHeroInstance * hero)=0;
  54. virtual const CCreatureSet* getGarrison(const CGObjectInstance *obj)=0;
  55. virtual UpgradeInfo getUpgradeInfo(const CArmedInstance *obj, int stackPos)=0;
  56. //battle
  57. 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
  58. virtual int battleGetObstaclesAtTile(int tile)=0; //returns bitfield
  59. virtual int battleGetStack(int pos)=0; //returns ID of stack on the tile
  60. virtual CStack battleGetStackByID(int ID)=0; //returns stack info by given ID
  61. virtual CStack battleGetStackByPos(int pos)=0; //returns stack info by given pos
  62. virtual int battleGetPos(int stack)=0; //returns position (tile ID) of stack
  63. //virtual int battleMakeAction(BattleAction* action)=0;//perform action with an active stack (or custom action)
  64. virtual std::map<int, CStack> battleGetStacks()=0; //returns stacks on battlefield
  65. virtual CCreature battleGetCreature(int number)=0; //returns type of creature by given number of stack
  66. //virtual bool battleMoveCreature(int ID, int dest)=0; //moves creature with id ID to dest if possible
  67. virtual std::vector<int> battleGetAvailableHexes(int ID)=0; //reutrns numbers of hexes reachable by creature with id ID
  68. virtual bool battleIsStackMine(int ID)=0; //returns true if stack with id ID belongs to caller
  69. virtual bool battleCanShoot(int ID, int dest)=0; //returns true if unit with id ID can shoot to dest
  70. };
  71. struct HeroMoveDetails
  72. {
  73. int3 src, dst; //source and destination points
  74. CGHeroInstance * ho; //object instance of this hero
  75. int owner;
  76. bool successful;
  77. };
  78. class CCallback : public ICallback
  79. {
  80. private:
  81. void newTurn();
  82. CCallback(CGameState * GS, int Player):gs(GS),player(Player){};
  83. CGameState * gs;
  84. int lowestSpeed(CGHeroInstance * chi); //speed of the slowest stack
  85. int valMovePoints(CGHeroInstance * chi);
  86. bool isVisible(int3 pos, int Player);
  87. protected:
  88. int player;
  89. public:
  90. //commands
  91. bool moveHero(int ID, CPath * path, int idtype, int pathType=0);//idtype: 0 - position in vector of heroes (of that player); 1 - ID of hero
  92. //pathType: 0 - nodes are manifestation pos, 1 - nodes are object pos
  93. void selectionMade(int selection, int asker);
  94. int swapCreatures(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2);
  95. int mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2); //first goes to the second
  96. int splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val);
  97. bool dismissHero(const CGHeroInstance * hero);
  98. bool swapArifacts(const CGHeroInstance * hero1, bool worn1, int pos1, const CGHeroInstance * hero2, bool worn2, int pos2);
  99. bool buildBuilding(const CGTownInstance *town, int buildingID);
  100. void recruitCreatures(const CGObjectInstance *obj, int ID, int amount);
  101. bool dismissCreature(const CArmedInstance *obj, int stackPos);
  102. bool upgradeCreature(const CArmedInstance *obj, int stackPos, int newID=-1);
  103. //get info
  104. bool verifyPath(CPath * path, bool blockSea);
  105. int getDate(int mode=0); //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
  106. PseudoV< PseudoV< PseudoV<unsigned char> > > & getVisibilityMap(); //returns visibility map (TODO: make it const)
  107. const CGHeroInstance * getHeroInfo(int player, int val, bool mode); //mode = 0 -> val = serial; mode = 1 -> val = ID
  108. int getResourceAmount(int type);
  109. std::vector<int> getResourceAmount();
  110. int howManyHeroes();
  111. const CGTownInstance * getTownInfo(int val, bool mode); //mode = 0 -> val = serial; mode = 1 -> val = ID
  112. std::vector < const CGTownInstance *> getTownsInfo(bool onlyOur=true);
  113. int howManyTowns();
  114. std::vector < std::string > getObjDescriptions(int3 pos); //returns descriptions of objects at pos in order from the lowest to the highest
  115. std::vector < const CGHeroInstance *> getHeroesInfo(bool onlyOur=true);
  116. bool isVisible(int3 pos);
  117. int getMyColor();
  118. int getHeroSerial(const CGHeroInstance * hero);
  119. int getMySerial();
  120. const CCreatureSet* getGarrison(const CGObjectInstance *obj);
  121. UpgradeInfo getUpgradeInfo(const CArmedInstance *obj, int stackPos);
  122. //battle
  123. 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
  124. int battleGetObstaclesAtTile(int tile); //returns bitfield
  125. int battleGetStack(int pos); //returns ID of stack on the tile
  126. CStack battleGetStackByID(int ID); //returns stack info by given ID
  127. CStack battleGetStackByPos(int pos); //returns stack info by given pos
  128. int battleGetPos(int stack); //returns position (tile ID) of stack
  129. //int battleMakeAction(BattleAction* action);//perform action with an active stack (or custom action)
  130. std::map<int, CStack> battleGetStacks(); //returns stacks on battlefield
  131. CCreature battleGetCreature(int number); //returns type of creature by given number of stack
  132. //bool battleMoveCreature(int ID, int dest); //moves creature with id ID to dest if possible
  133. std::vector<int> battleGetAvailableHexes(int ID); //reutrns numbers of hexes reachable by creature with id ID
  134. bool battleIsStackMine(int ID); //returns true if stack with id ID belongs to caller
  135. bool battleCanShoot(int ID, int dest); //returns true if unit with id ID can shoot to dest
  136. //friends
  137. #ifndef __GNUC__
  138. friend int _tmain(int argc, _TCHAR* argv[]);
  139. #else
  140. friend int main(int argc, _TCHAR* argv[]);
  141. #endif
  142. };
  143. class CScriptCallback
  144. {
  145. public:
  146. CGameState * gs;
  147. //get info
  148. static int3 getPos(CGObjectInstance * ob);
  149. int getHeroOwner(int heroID);
  150. int getSelectedHero();
  151. int getDate(int mode=0);
  152. //do sth
  153. static void changePrimSkill(int ID, int which, int val);
  154. void showInfoDialog(int player, std::string text, std::vector<SComponent*> * components); //TODO: obslugiwac nulle
  155. void showSelDialog(int player, std::string text, std::vector<CSelectableComponent*>*components, IChosen * asker);
  156. void giveResource(int player, int which, int val);
  157. void showCompInfo(int player, SComponent * comp);
  158. void heroVisitCastle(CGObjectInstance * ob, int heroID);
  159. void stopHeroVisitCastle(CGObjectInstance * ob, int heroID);
  160. void giveHeroArtifact(int artid, int hid, int position); //pos==-1 - first free slot in backpack
  161. void startBattle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2); //use hero=NULL for no hero
  162. void startBattle(int heroID, CCreatureSet * army, int3 tile); //for hero<=>neutral army
  163. //friends
  164. friend void initGameState(CGameInfo * cgi);
  165. };
  166. class CLuaCallback : public CScriptCallback
  167. {
  168. private:
  169. static void registerFuncs(lua_State * L);
  170. static int getPos(lua_State * L);//(CGObjectInstance * object);
  171. static int changePrimSkill(lua_State * L);//(int ID, int which, int val);
  172. static int getGnrlText(lua_State * L);//(int ID, int which, int val);
  173. static int getSelectedHero(lua_State * L);//()
  174. friend void initGameState(CGameInfo * cgi);
  175. };
  176. #endif //CCALLBACK_H