CCallback.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/IGameCallback.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. struct BattleAction;
  27. class CGTownInstance;
  28. struct lua_State;
  29. class CClient;
  30. class IShipyard;
  31. struct CGPathNode;
  32. struct CGPath;
  33. struct CPathsInfo;
  34. class IBattleCallback
  35. {
  36. public:
  37. bool waitTillRealize; //if true, request functions will return after they are realized by server
  38. bool unlockGsWhenWaiting;//if true after sending each request, gs mutex will be unlocked so the changes can be applied; NOTICE caller must have gs mx locked prior to any call to actiob callback!
  39. //battle
  40. virtual int battleMakeAction(BattleAction* action)=0;//for casting spells by hero - DO NOT use it for moving active stack
  41. virtual bool battleMakeTacticAction(BattleAction * action) =0; // performs tactic phase actions
  42. };
  43. class IGameActionCallback
  44. {
  45. public:
  46. //hero
  47. virtual bool moveHero(const CGHeroInstance *h, int3 dst) =0; //dst must be free, neighbouring tile (this function can move hero only by one tile)
  48. virtual bool dismissHero(const CGHeroInstance * hero)=0; //dismisses given hero; true - successfuly, false - not successfuly
  49. virtual void dig(const CGObjectInstance *hero)=0;
  50. virtual void castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos = int3(-1, -1, -1))=0; //cast adventure map spell
  51. //town
  52. virtual void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)=0;
  53. virtual bool buildBuilding(const CGTownInstance *town, si32 buildingID)=0;
  54. virtual void recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount, si32 level=-1)=0;
  55. virtual bool upgradeCreature(const CArmedInstance *obj, int stackPos, int newID=-1)=0; //if newID==-1 then best possible upgrade will be made
  56. virtual void swapGarrisonHero(const CGTownInstance *town)=0;
  57. virtual void trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero = NULL)=0; //mode==0: sell val1 units of id1 resource for id2 resiurce
  58. virtual void selectionMade(int selection, int asker) =0;
  59. virtual int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)=0;//swaps creatures between two possibly different garrisons // TODO: AI-unsafe code - fix it!
  60. virtual int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)=0;//joins first stack to the second (creatures must be same type)
  61. virtual int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)=0;//split creatures from the first stack
  62. virtual bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)=0; //swaps artifacts between two given heroes
  63. virtual bool assembleArtifacts(const CGHeroInstance * hero, ui16 artifactSlot, bool assemble, ui32 assembleTo)=0;
  64. virtual bool dismissCreature(const CArmedInstance *obj, int stackPos)=0;
  65. virtual void endTurn()=0;
  66. 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)
  67. virtual void setFormation(const CGHeroInstance * hero, bool tight)=0;
  68. virtual void setSelection(const CArmedInstance * obj)=0;
  69. virtual void save(const std::string &fname) = 0;
  70. virtual void sendMessage(const std::string &mess) = 0;
  71. virtual void buildBoat(const IShipyard *obj) = 0;
  72. };
  73. class CBattleCallback : public IBattleCallback, public CBattleInfoCallback
  74. {
  75. private:
  76. CBattleCallback(CGameState *GS, int Player, CClient *C);
  77. protected:
  78. template <typename T> void sendRequest(const T*request);
  79. CClient *cl;
  80. //virtual bool hasAccess(int playerId) const;
  81. public:
  82. int battleMakeAction(BattleAction* action) OVERRIDE;//for casting spells by hero - DO NOT use it for moving active stack
  83. bool battleMakeTacticAction(BattleAction * action) OVERRIDE; // performs tactic phase actions
  84. friend class CCallback;
  85. friend class CClient;
  86. };
  87. class CCallback : public CPlayerSpecificInfoCallback, public IGameActionCallback, public CBattleCallback
  88. {
  89. private:
  90. CCallback(CGameState * GS, int Player, CClient *C);
  91. public:
  92. //client-specific functionalities (pathfinding)
  93. virtual bool getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret); //DEPRACATED!!!
  94. virtual const CGPathNode *getPathInfo(int3 tile); //uses main, client pathfinder info
  95. virtual bool getPath2(int3 dest, CGPath &ret); //uses main, client pathfinder info
  96. virtual void calculatePaths(const CGHeroInstance *hero, CPathsInfo &out, int3 src = int3(-1,-1,-1), int movement = -1);
  97. virtual void recalculatePaths(); //updates main, client pathfinder info (should be called when moving hero is over)
  98. void unregisterMyInterface(); //stops delivering information about game events to that player's interface -> can be called ONLY after victory/loss
  99. //commands
  100. bool moveHero(const CGHeroInstance *h, int3 dst); //dst must be free, neighbouring tile (this function can move hero only by one tile)
  101. bool teleportHero(const CGHeroInstance *who, const CGTownInstance *where);
  102. void selectionMade(int selection, int asker);
  103. int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2);
  104. int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2); //first goes to the second
  105. int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val);
  106. bool dismissHero(const CGHeroInstance * hero);
  107. bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2);
  108. bool assembleArtifacts(const CGHeroInstance * hero, ui16 artifactSlot, bool assemble, ui32 assembleTo);
  109. bool buildBuilding(const CGTownInstance *town, si32 buildingID);
  110. void recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount, si32 level=-1);
  111. bool dismissCreature(const CArmedInstance *obj, int stackPos);
  112. bool upgradeCreature(const CArmedInstance *obj, int stackPos, int newID=-1);
  113. void endTurn();
  114. void swapGarrisonHero(const CGTownInstance *town);
  115. void buyArtifact(const CGHeroInstance *hero, int aid);
  116. void trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero = NULL);
  117. void setFormation(const CGHeroInstance * hero, bool tight);
  118. void setSelection(const CArmedInstance * obj);
  119. void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero);
  120. void save(const std::string &fname);
  121. void sendMessage(const std::string &mess);
  122. void buildBoat(const IShipyard *obj);
  123. void dig(const CGObjectInstance *hero);
  124. void castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos = int3(-1, -1, -1));
  125. //friends
  126. friend class CClient;
  127. };
  128. #endif // __CCALLBACK_H__