CCallback.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #include "lib/IGameCallback.h"
  3. /*
  4. * CCallback.h, part of VCMI engine
  5. *
  6. * Authors: listed in file AUTHORS in main folder
  7. *
  8. * License: GNU General Public License v2.0 or later
  9. * Full text of license available in license.txt file, in main folder
  10. *
  11. */
  12. class IArtifactSetBase;
  13. class CGHeroInstance;
  14. class CGameState;
  15. struct CPath;
  16. class CGObjectInstance;
  17. class CArmedInstance;
  18. struct BattleAction;
  19. class CGTownInstance;
  20. struct lua_State;
  21. class CClient;
  22. class IShipyard;
  23. struct CGPathNode;
  24. struct CGPath;
  25. struct CPathsInfo;
  26. struct CPack;
  27. class IBattleCallback
  28. {
  29. public:
  30. bool waitTillRealize; //if true, request functions will return after they are realized by server
  31. 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!
  32. //battle
  33. virtual int battleMakeAction(BattleAction* action)=0;//for casting spells by hero - DO NOT use it for moving active stack
  34. virtual bool battleMakeTacticAction(BattleAction * action) =0; // performs tactic phase actions
  35. };
  36. class IGameActionCallback
  37. {
  38. public:
  39. //hero
  40. virtual bool moveHero(const CGHeroInstance *h, int3 dst) =0; //dst must be free, neighbouring tile (this function can move hero only by one tile)
  41. virtual bool dismissHero(const CGHeroInstance * hero)=0; //dismisses given hero; true - successfuly, false - not successfuly
  42. virtual void dig(const CGObjectInstance *hero)=0;
  43. virtual void castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos = int3(-1, -1, -1))=0; //cast adventure map spell
  44. //town
  45. virtual void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)=0;
  46. virtual bool buildBuilding(const CGTownInstance *town, si32 buildingID)=0;
  47. virtual void recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount, si32 level=-1)=0;
  48. virtual bool upgradeCreature(const CArmedInstance *obj, int stackPos, int newID=-1)=0; //if newID==-1 then best possible upgrade will be made
  49. virtual void swapGarrisonHero(const CGTownInstance *town)=0;
  50. 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
  51. virtual void selectionMade(int selection, int asker) =0;
  52. 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!
  53. 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)
  54. virtual int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2) =0; //first goes to the second
  55. virtual int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)=0;//split creatures from the first stack
  56. //virtual bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)=0; //swaps artifacts between two given heroes
  57. virtual bool swapArtifacts(const IArtifactSetBase * src, ui16 pos1, const IArtifactSetBase * dest, ui16 pos2)=0;
  58. virtual bool assembleArtifacts(const CGHeroInstance * hero, ui16 artifactSlot, bool assemble, ui32 assembleTo)=0;
  59. virtual bool dismissCreature(const CArmedInstance *obj, int stackPos)=0;
  60. virtual void endTurn()=0;
  61. 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)
  62. virtual void setFormation(const CGHeroInstance * hero, bool tight)=0;
  63. virtual void setSelection(const CArmedInstance * obj)=0;
  64. virtual void save(const std::string &fname) = 0;
  65. virtual void sendMessage(const std::string &mess) = 0;
  66. virtual void buildBoat(const IShipyard *obj) = 0;
  67. };
  68. struct CPack;
  69. class CBattleCallback : public IBattleCallback, public CBattleInfoCallback
  70. {
  71. private:
  72. CBattleCallback(CGameState *GS, int Player, CClient *C);
  73. protected:
  74. void sendRequest(const CPack *request);
  75. CClient *cl;
  76. //virtual bool hasAccess(int playerId) const;
  77. public:
  78. int battleMakeAction(BattleAction* action) OVERRIDE;//for casting spells by hero - DO NOT use it for moving active stack
  79. bool battleMakeTacticAction(BattleAction * action) OVERRIDE; // performs tactic phase actions
  80. friend class CCallback;
  81. friend class CClient;
  82. };
  83. class CCallback : public CPlayerSpecificInfoCallback, public IGameActionCallback, public CBattleCallback
  84. {
  85. private:
  86. void validatePaths(); //recalcualte paths if necessary
  87. public:
  88. CCallback(CGameState * GS, int Player, CClient *C);
  89. //client-specific functionalities (pathfinding)
  90. virtual bool getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret); //DEPRACATED!!!
  91. virtual const CGPathNode *getPathInfo(int3 tile); //uses main, client pathfinder info
  92. virtual bool getPath2(int3 dest, CGPath &ret); //uses main, client pathfinder info
  93. virtual void calculatePaths(const CGHeroInstance *hero, CPathsInfo &out, int3 src = int3(-1,-1,-1), int movement = -1);
  94. virtual void recalculatePaths(); //updates main, client pathfinder info (should be called when moving hero is over)
  95. void unregisterMyInterface(); //stops delivering information about game events to that player's interface -> can be called ONLY after victory/loss
  96. //commands
  97. bool moveHero(const CGHeroInstance *h, int3 dst); //dst must be free, neighbouring tile (this function can move hero only by one tile)
  98. bool teleportHero(const CGHeroInstance *who, const CGTownInstance *where);
  99. void selectionMade(int selection, int asker);
  100. int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2);
  101. int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2); //first goes to the second
  102. int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2); //first goes to the second
  103. int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val);
  104. bool dismissHero(const CGHeroInstance * hero);
  105. //bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2);
  106. bool swapArtifacts(const IArtifactSetBase * src, ui16 pos1, const IArtifactSetBase * dest, ui16 pos2);
  107. //bool moveArtifact(const CGHeroInstance * hero, ui16 src, const CStackInstance * stack, ui16 dest); // TODO: unify classes
  108. //bool moveArtifact(const CStackInstance * stack, ui16 src , const CGHeroInstance * hero, ui16 dest); // TODO: unify classes
  109. bool assembleArtifacts(const CGHeroInstance * hero, ui16 artifactSlot, bool assemble, ui32 assembleTo);
  110. bool buildBuilding(const CGTownInstance *town, si32 buildingID);
  111. void recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount, si32 level=-1);
  112. bool dismissCreature(const CArmedInstance *obj, int stackPos);
  113. bool upgradeCreature(const CArmedInstance *obj, int stackPos, int newID=-1);
  114. void endTurn();
  115. void swapGarrisonHero(const CGTownInstance *town);
  116. void buyArtifact(const CGHeroInstance *hero, int aid);
  117. void trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero = NULL);
  118. void setFormation(const CGHeroInstance * hero, bool tight);
  119. void setSelection(const CArmedInstance * obj);
  120. void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero);
  121. void save(const std::string &fname);
  122. void sendMessage(const std::string &mess);
  123. void buildBoat(const IShipyard *obj);
  124. void dig(const CGObjectInstance *hero);
  125. void castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos = int3(-1, -1, -1));
  126. //friends
  127. friend class CClient;
  128. };