CGameInterface.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * CGameInterface.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "battle/AutocombatPreferences.h"
  12. #include "IGameEventsReceiver.h"
  13. #include "spells/ViewSpellInt.h"
  14. class CBattleCallback;
  15. class CCallback;
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. using boost::logic::tribool;
  18. class Environment;
  19. class ICallback;
  20. class CGlobalAI;
  21. struct Component;
  22. struct TryMoveHero;
  23. class CGHeroInstance;
  24. class CGTownInstance;
  25. class CGObjectInstance;
  26. class CGBlackMarket;
  27. class CGDwelling;
  28. class CCreatureSet;
  29. class CArmedInstance;
  30. class IShipyard;
  31. class IMarket;
  32. class BattleAction;
  33. struct BattleResult;
  34. struct BattleAttack;
  35. struct BattleStackAttacked;
  36. struct BattleSpellCast;
  37. struct SetStackEffect;
  38. struct Bonus;
  39. struct PackageApplied;
  40. struct SetObjectProperty;
  41. struct CatapultAttack;
  42. struct StackLocation;
  43. class CStackInstance;
  44. class CCommanderInstance;
  45. class CStack;
  46. class CCreature;
  47. class BattleStateInfo;
  48. struct ArtifactLocation;
  49. class BattleStateInfoForRetreat;
  50. struct CPathsInfo;
  51. #if SCRIPTING_ENABLED
  52. namespace scripting
  53. {
  54. class Module;
  55. }
  56. #endif
  57. using TTeleportExitsList = std::vector<std::pair<ObjectInstanceID, int3>>;
  58. class DLL_LINKAGE CBattleGameInterface : public IBattleEventsReceiver
  59. {
  60. public:
  61. bool human = false;
  62. PlayerColor playerID;
  63. std::string dllName;
  64. virtual ~CBattleGameInterface() {};
  65. virtual void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB){};
  66. virtual void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AutocombatPreferences autocombatPreferences){};
  67. //battle call-ins
  68. virtual void activeStack(const BattleID & battleID, const CStack * stack)=0; //called when it's turn of that stack
  69. virtual void yourTacticPhase(const BattleID & battleID, int distance)=0; //called when interface has opportunity to use Tactics skill -> use cb->battleMakeTacticAction from this function
  70. };
  71. /// Central class for managing human player / AI interface logic
  72. class DLL_LINKAGE CGameInterface : public CBattleGameInterface, public IGameEventsReceiver
  73. {
  74. public:
  75. virtual ~CGameInterface() = default;
  76. virtual void initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB){};
  77. virtual void yourTurn(QueryID askID){}; //called AFTER playerStartsTurn(player)
  78. //pskill is gained primary skill, interface has to choose one of given skills and call callback with selection id
  79. virtual void heroGotLevel(const CGHeroInstance *hero, PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID)=0;
  80. virtual void commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID)=0;
  81. // Show a dialog, player must take decision. If selection then he has to choose between one of given components,
  82. // if cancel he is allowed to not choose. After making choice, CCallback::selectionMade should be called
  83. // with number of selected component (1 - n) or 0 for cancel (if allowed) and askID.
  84. virtual void showBlockingDialog(const std::string &text, const std::vector<Component> &components, QueryID askID, const int soundID, bool selection, bool cancel, bool safeToAutoaccept) = 0;
  85. // all stacks operations between these objects become allowed, interface has to call onEnd when done
  86. virtual void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, QueryID queryID) = 0;
  87. virtual void showTeleportDialog(const CGHeroInstance * hero, TeleportChannelID channel, TTeleportExitsList exits, bool impassable, QueryID askID) = 0;
  88. virtual void showMapObjectSelectDialog(QueryID askID, const Component & icon, const MetaString & title, const MetaString & description, const std::vector<ObjectInstanceID> & objects) = 0;
  89. virtual void finish(){}; //if for some reason we want to end
  90. virtual void showWorldViewEx(const std::vector<ObjectPosInfo> & objectPositions, bool showTerrain){};
  91. virtual std::optional<BattleAction> makeSurrenderRetreatDecision(const BattleID & battleID, const BattleStateInfoForRetreat & battleState) = 0;
  92. /// Invalidates and destroys all paths for all heroes
  93. virtual void invalidatePaths(){};
  94. };
  95. class DLL_LINKAGE CDynLibHandler
  96. {
  97. public:
  98. static std::shared_ptr<CGlobalAI> getNewAI(const std::string & dllname);
  99. static std::shared_ptr<CBattleGameInterface> getNewBattleAI(const std::string & dllname);
  100. #if SCRIPTING_ENABLED
  101. static std::shared_ptr<scripting::Module> getNewScriptingModule(const boost::filesystem::path & dllname);
  102. #endif
  103. };
  104. class DLL_LINKAGE CGlobalAI : public CGameInterface // AI class (to derivate)
  105. {
  106. public:
  107. std::shared_ptr<Environment> env;
  108. CGlobalAI();
  109. };
  110. //class to be inherited by adventure-only AIs, it cedes battle actions to given battle-AI
  111. class DLL_LINKAGE CAdventureAI : public CGlobalAI
  112. {
  113. public:
  114. CAdventureAI() = default;
  115. std::shared_ptr<CBattleGameInterface> battleAI;
  116. std::shared_ptr<CBattleCallback> cbc;
  117. virtual std::string getBattleAIName() const = 0; //has to return name of the battle AI to be used
  118. //battle interface
  119. void activeStack(const BattleID & battleID, const CStack * stack) override;
  120. void yourTacticPhase(const BattleID & battleID, int distance) override;
  121. void battleNewRound(const BattleID & battleID) override;
  122. void battleCatapultAttacked(const BattleID & battleID, const CatapultAttack & ca) override;
  123. void battleStart(const BattleID & battleID, const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, BattleSide side, bool replayAllowed) override;
  124. void battleStacksAttacked(const BattleID & battleID, const std::vector<BattleStackAttacked> & bsa, bool ranged) override;
  125. void actionStarted(const BattleID & battleID, const BattleAction &action) override;
  126. void battleNewRoundFirst(const BattleID & battleID) override;
  127. void actionFinished(const BattleID & battleID, const BattleAction &action) override;
  128. void battleStacksEffectsSet(const BattleID & battleID, const SetStackEffect & sse) override;
  129. void battleObstaclesChanged(const BattleID & battleID, const std::vector<ObstacleChanges> & obstacles) override;
  130. void battleStackMoved(const BattleID & battleID, const CStack * stack, const BattleHexArray & dest, int distance, bool teleport) override;
  131. void battleAttack(const BattleID & battleID, const BattleAttack *ba) override;
  132. void battleSpellCast(const BattleID & battleID, const BattleSpellCast *sc) override;
  133. void battleEnd(const BattleID & battleID, const BattleResult *br, QueryID queryID) override;
  134. void battleUnitsChanged(const BattleID & battleID, const std::vector<UnitChanges> & units) override;
  135. };
  136. VCMI_LIB_NAMESPACE_END