CCallback.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #pragma once
  2. #include "lib/IGameCallback2.h"
  3. #include "int3.h" // for int3
  4. /*
  5. * CCallback.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  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 IBattleEventsReceiver;
  28. class IGameEventsReceiver;
  29. //my
  30. struct ArtifactLocation;
  31. class IBattleCallback
  32. {
  33. public:
  34. bool waitTillRealize; //if true, request functions will return after they are realized by server
  35. 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!
  36. //battle
  37. virtual int battleMakeAction(BattleAction* action)=0;//for casting spells by hero - DO NOT use it for moving active stack
  38. virtual bool battleMakeTacticAction(BattleAction * action) =0; // performs tactic phase actions
  39. };
  40. class IGameActionCallback
  41. {
  42. public:
  43. //hero
  44. virtual bool moveHero(const CGHeroInstance *h, int3 dst) =0; //dst must be free, neighbouring tile (this function can move hero only by one tile)
  45. virtual bool dismissHero(const CGHeroInstance * hero)=0; //dismisses given hero; true - successfuly, false - not successfuly
  46. virtual void dig(const CGObjectInstance *hero)=0;
  47. virtual void castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos = int3(-1, -1, -1))=0; //cast adventure map spell
  48. //town
  49. virtual void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)=0;
  50. virtual bool buildBuilding(const CGTownInstance *town, BuildingID buildingID)=0;
  51. virtual void recruitCreatures(const CGObjectInstance *obj, CreatureID ID, ui32 amount, si32 level=-1)=0;
  52. virtual bool upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID=CreatureID::NONE)=0; //if newID==-1 then best possible upgrade will be made
  53. virtual void swapGarrisonHero(const CGTownInstance *town)=0;
  54. virtual void trade(const CGObjectInstance *market, EMarketMode::EMarketMode mode, int id1, int id2, int val1, const CGHeroInstance *hero = nullptr)=0; //mode==0: sell val1 units of id1 resource for id2 resiurce
  55. virtual int selectionMade(int selection, QueryID queryID) =0;
  56. virtual int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)=0;//swaps creatures between two possibly different garrisons // TODO: AI-unsafe code - fix it!
  57. virtual int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)=0;//joins first stack to the second (creatures must be same type)
  58. virtual int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) =0; //first goes to the second
  59. virtual int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val)=0;//split creatures from the first stack
  60. //virtual bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)=0; //swaps artifacts between two given heroes
  61. virtual bool swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)=0;
  62. virtual bool assembleArtifacts(const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)=0;
  63. virtual bool dismissCreature(const CArmedInstance *obj, SlotID stackPos)=0;
  64. virtual void endTurn()=0;
  65. virtual void buyArtifact(const CGHeroInstance *hero, ArtifactID aid)=0; //used to buy artifacts in towns (including spell book in the guild and war machines in blacksmith)
  66. virtual void setFormation(const CGHeroInstance * hero, bool tight)=0;
  67. virtual void setSelection(const CArmedInstance * obj)=0;
  68. virtual void save(const std::string &fname) = 0;
  69. virtual void sendMessage(const std::string &mess) = 0;
  70. virtual void buildBoat(const IShipyard *obj) = 0;
  71. };
  72. struct CPack;
  73. class CBattleCallback : public IBattleCallback, public CPlayerBattleCallback
  74. {
  75. protected:
  76. int sendRequest(const CPack *request); //returns requestID (that'll be matched to requestID in PackageApplied)
  77. CClient *cl;
  78. //virtual bool hasAccess(int playerId) const;
  79. public:
  80. CBattleCallback(CGameState *GS, boost::optional<PlayerColor> Player, CClient *C);
  81. int battleMakeAction(BattleAction* action) override;//for casting spells by hero - DO NOT use it for moving active stack
  82. bool battleMakeTacticAction(BattleAction * action) override; // performs tactic phase actions
  83. friend class CCallback;
  84. friend class CClient;
  85. };
  86. class CCallback : public CPlayerSpecificInfoCallback, public IGameActionCallback, public CBattleCallback
  87. {
  88. private:
  89. void validatePaths(); //recalcualte paths if necessary
  90. public:
  91. CCallback(CGameState * GS, boost::optional<PlayerColor> Player, CClient *C);
  92. virtual ~CCallback();
  93. //client-specific functionalities (pathfinding)
  94. virtual const CGPathNode *getPathInfo(int3 tile); //uses main, client pathfinder info
  95. virtual int getDistance(int3 tile);
  96. virtual bool getPath2(int3 dest, CGPath &ret); //uses main, client pathfinder info
  97. virtual bool canMoveBetween(const int3 &a, const int3 &b);
  98. virtual int getMovementCost(const CGHeroInstance * hero, int3 dest);
  99. virtual int3 getGuardingCreaturePosition(int3 tile); //uses main, client pathfinder info
  100. virtual void calculatePaths(const CGHeroInstance *hero, CPathsInfo &out, int3 src = int3(-1,-1,-1), int movement = -1);
  101. virtual void recalculatePaths(); //updates main, client pathfinder info (should be called when moving hero is over)
  102. //Set of metrhods that allows adding more interfaces for this player that'll receive game event call-ins.
  103. void registerGameInterface(shared_ptr<IGameEventsReceiver> gameEvents);
  104. void registerBattleInterface(shared_ptr<IBattleEventsReceiver> battleEvents);
  105. void unregisterGameInterface(shared_ptr<IGameEventsReceiver> gameEvents);
  106. void unregisterBattleInterface(shared_ptr<IBattleEventsReceiver> battleEvents);
  107. void unregisterAllInterfaces(); //stops delivering information about game events to player interfaces -> can be called ONLY after victory/loss
  108. //commands
  109. bool moveHero(const CGHeroInstance *h, int3 dst); //dst must be free, neighbouring tile (this function can move hero only by one tile)
  110. bool teleportHero(const CGHeroInstance *who, const CGTownInstance *where);
  111. int selectionMade(int selection, QueryID queryID);
  112. int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2);
  113. int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2); //first goes to the second
  114. int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2); //first goes to the second
  115. int splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val);
  116. bool dismissHero(const CGHeroInstance * hero);
  117. //bool swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2);
  118. bool swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2);
  119. //bool moveArtifact(const CGHeroInstance * hero, ui16 src, const CStackInstance * stack, ui16 dest); // TODO: unify classes
  120. //bool moveArtifact(const CStackInstance * stack, ui16 src , const CGHeroInstance * hero, ui16 dest); // TODO: unify classes
  121. bool assembleArtifacts(const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo);
  122. bool buildBuilding(const CGTownInstance *town, BuildingID buildingID) override;
  123. void recruitCreatures(const CGObjectInstance *obj, CreatureID ID, ui32 amount, si32 level=-1);
  124. bool dismissCreature(const CArmedInstance *obj, SlotID stackPos);
  125. bool upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID=CreatureID::NONE) override;
  126. void endTurn();
  127. void swapGarrisonHero(const CGTownInstance *town);
  128. void buyArtifact(const CGHeroInstance *hero, ArtifactID aid) override;
  129. void trade(const CGObjectInstance *market, EMarketMode::EMarketMode mode, int id1, int id2, int val1, const CGHeroInstance *hero = nullptr);
  130. void setFormation(const CGHeroInstance * hero, bool tight);
  131. void setSelection(const CArmedInstance * obj);
  132. void recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero);
  133. void save(const std::string &fname);
  134. void sendMessage(const std::string &mess);
  135. void buildBoat(const IShipyard *obj);
  136. void dig(const CGObjectInstance *hero);
  137. void castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos = int3(-1, -1, -1));
  138. //friends
  139. friend class CClient;
  140. };