CGameInterface.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/BattleAction.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. struct BattleResult;
  33. struct BattleAttack;
  34. struct BattleStackAttacked;
  35. struct BattleSpellCast;
  36. struct SetStackEffect;
  37. struct Bonus;
  38. struct PackageApplied;
  39. struct SetObjectProperty;
  40. struct CatapultAttack;
  41. struct StackLocation;
  42. class CStackInstance;
  43. class CCommanderInstance;
  44. class CStack;
  45. class CCreature;
  46. class CLoadFile;
  47. class CSaveFile;
  48. class BinaryDeserializer;
  49. class BinarySerializer;
  50. class BattleStateInfo;
  51. struct ArtifactLocation;
  52. class BattleStateInfoForRetreat;
  53. #if SCRIPTING_ENABLED
  54. namespace scripting
  55. {
  56. class Module;
  57. }
  58. #endif
  59. using TTeleportExitsList = std::vector<std::pair<ObjectInstanceID, int3>>;
  60. class DLL_LINKAGE CBattleGameInterface : public IBattleEventsReceiver
  61. {
  62. public:
  63. bool human;
  64. PlayerColor playerID;
  65. std::string dllName;
  66. virtual ~CBattleGameInterface() {};
  67. virtual void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB){};
  68. //battle call-ins
  69. virtual void activeStack(const CStack * stack)=0; //called when it's turn of that stack
  70. virtual void yourTacticPhase(int distance)=0; //called when interface has opportunity to use Tactics skill -> use cb->battleMakeTacticAction from this function
  71. };
  72. /// Central class for managing human player / AI interface logic
  73. class DLL_LINKAGE CGameInterface : public CBattleGameInterface, public IGameEventsReceiver
  74. {
  75. public:
  76. virtual ~CGameInterface() = default;
  77. virtual void initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB){};
  78. virtual void yourTurn(){}; //called AFTER playerStartsTurn(player)
  79. //pskill is gained primary skill, interface has to choose one of given skills and call callback with selection id
  80. virtual void heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID)=0;
  81. virtual void commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID)=0;
  82. // Show a dialog, player must take decision. If selection then he has to choose between one of given components,
  83. // if cancel he is allowed to not choose. After making choice, CCallback::selectionMade should be called
  84. // with number of selected component (1 - n) or 0 for cancel (if allowed) and askID.
  85. virtual void showBlockingDialog(const std::string &text, const std::vector<Component> &components, QueryID askID, const int soundID, bool selection, bool cancel) = 0;
  86. // all stacks operations between these objects become allowed, interface has to call onEnd when done
  87. virtual void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, QueryID queryID) = 0;
  88. virtual void showTeleportDialog(TeleportChannelID channel, TTeleportExitsList exits, bool impassable, QueryID askID) = 0;
  89. virtual void showMapObjectSelectDialog(QueryID askID, const Component & icon, const MetaString & title, const MetaString & description, const std::vector<ObjectInstanceID> & objects) = 0;
  90. virtual void finish(){}; //if for some reason we want to end
  91. virtual void showWorldViewEx(const std::vector<ObjectPosInfo> & objectPositions, bool showTerrain){};
  92. virtual std::optional<BattleAction> makeSurrenderRetreatDecision(const BattleStateInfoForRetreat & battleState)
  93. {
  94. return std::nullopt;
  95. }
  96. virtual void saveGame(BinarySerializer & h, const int version) = 0;
  97. virtual void loadGame(BinaryDeserializer & h, const int version) = 0;
  98. };
  99. class DLL_LINKAGE CDynLibHandler
  100. {
  101. public:
  102. static std::shared_ptr<CGlobalAI> getNewAI(const std::string & dllname);
  103. static std::shared_ptr<CBattleGameInterface> getNewBattleAI(const std::string & dllname);
  104. #if SCRIPTING_ENABLED
  105. static std::shared_ptr<scripting::Module> getNewScriptingModule(const boost::filesystem::path & dllname);
  106. #endif
  107. };
  108. class DLL_LINKAGE CGlobalAI : public CGameInterface // AI class (to derivate)
  109. {
  110. public:
  111. std::shared_ptr<Environment> env;
  112. CGlobalAI();
  113. };
  114. //class to be inherited by adventure-only AIs, it cedes battle actions to given battle-AI
  115. class DLL_LINKAGE CAdventureAI : public CGlobalAI
  116. {
  117. public:
  118. CAdventureAI() = default;
  119. std::shared_ptr<CBattleGameInterface> battleAI;
  120. std::shared_ptr<CBattleCallback> cbc;
  121. virtual std::string getBattleAIName() const = 0; //has to return name of the battle AI to be used
  122. //battle interface
  123. virtual void activeStack(const CStack * stack) override;
  124. virtual void yourTacticPhase(int distance) override;
  125. virtual void battleNewRound(int round) override;
  126. virtual void battleCatapultAttacked(const CatapultAttack & ca) override;
  127. virtual void battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side) override;
  128. virtual void battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa, bool ranged) override;
  129. virtual void actionStarted(const BattleAction &action) override;
  130. virtual void battleNewRoundFirst(int round) override;
  131. virtual void actionFinished(const BattleAction &action) override;
  132. virtual void battleStacksEffectsSet(const SetStackEffect & sse) override;
  133. virtual void battleObstaclesChanged(const std::vector<ObstacleChanges> & obstacles) override;
  134. virtual void battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance, bool teleport) override;
  135. virtual void battleAttack(const BattleAttack *ba) override;
  136. virtual void battleSpellCast(const BattleSpellCast *sc) override;
  137. virtual void battleEnd(const BattleResult *br, QueryID queryID) override;
  138. virtual void battleUnitsChanged(const std::vector<UnitChanges> & units) override;
  139. virtual void saveGame(BinarySerializer & h, const int version) override;
  140. virtual void loadGame(BinaryDeserializer & h, const int version) override;
  141. };
  142. VCMI_LIB_NAMESPACE_END