base.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * base.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. // CI build fails without this
  12. #include <utility>
  13. #include "Global.h"
  14. #include "battle/CPlayerBattleCallback.h"
  15. #include "callback/CBattleCallback.h"
  16. #include "callback/CBattleGameInterface.h"
  17. #include "schema/base.h"
  18. namespace MMAI::BAI
  19. {
  20. class Base : public CBattleGameInterface
  21. {
  22. public:
  23. // Factory method for versioned derived BAI (e.g. BAI::V1)
  24. static std::shared_ptr<Base>
  25. Create(Schema::IModel * model, const std::shared_ptr<Environment> & env, const std::shared_ptr<CBattleCallback> & cb, bool enableSpellsUsage);
  26. Base() = delete;
  27. Base(Schema::IModel * model, int version, const std::shared_ptr<Environment> & env, const std::shared_ptr<CBattleCallback> & cb);
  28. /*
  29. * These methods MUST be overridden by derived BAI (e.g. BAI::V1)
  30. * Their base implementation is is for logging purposes only.
  31. */
  32. virtual Schema::Action getNonRenderAction() = 0;
  33. void activeStack(const BattleID & bid, const CStack * stack) override;
  34. void yourTacticPhase(const BattleID & bid, int distance) override;
  35. /*
  36. * These methods MAY be overriden by derived BAI (e.g. BAI::V1)
  37. * Their base implementation is for logging purposes only.
  38. */
  39. virtual void init(bool enableSpellsUsage); // called shortly after object construction
  40. void actionFinished(const BattleID & bid, const BattleAction & action) override;
  41. void actionStarted(const BattleID & bid, const BattleAction & action) override;
  42. void battleAttack(const BattleID & bid, const BattleAttack * ba) override;
  43. void battleCatapultAttacked(const BattleID & bid, const CatapultAttack & ca) override;
  44. void battleEnd(const BattleID & bid, const BattleResult * br, QueryID queryID) override;
  45. void battleGateStateChanged(const BattleID & bid, EGateState state) override;
  46. void battleLogMessage(const BattleID & bid, const std::vector<MetaString> & lines) override;
  47. void battleNewRound(const BattleID & bid) override;
  48. void battleNewRoundFirst(const BattleID & bid) override;
  49. void battleObstaclesChanged(const BattleID & bid, const std::vector<ObstacleChanges> & obstacles) override;
  50. void battleSpellCast(const BattleID & bid, const BattleSpellCast * sc) override;
  51. void battleStackMoved(const BattleID & bid, const CStack * stack, const BattleHexArray & dest, int distance, bool teleport) override;
  52. void battleStacksAttacked(const BattleID & bid, const std::vector<BattleStackAttacked> & bsa, bool ranged) override;
  53. void battleStacksEffectsSet(const BattleID & bid, const SetStackEffect & sse) override;
  54. void battleStart(
  55. const BattleID & bid,
  56. const CCreatureSet * army1,
  57. const CCreatureSet * army2,
  58. int3 tile,
  59. const CGHeroInstance * hero1,
  60. const CGHeroInstance * hero2,
  61. BattleSide side,
  62. bool replayAllowed
  63. ) override;
  64. void battleTriggerEffect(const BattleID & bid, const BattleTriggerEffect & bte) override;
  65. void battleUnitsChanged(const BattleID & bid, const std::vector<UnitChanges> & changes) override;
  66. /*
  67. * These methods MUST NOT be called.
  68. * Their base implementation throws a runtime error
  69. * (whistleblower for developer mistakes)
  70. */
  71. void initBattleInterface(std::shared_ptr<Environment> _1, std::shared_ptr<CBattleCallback> _2) override
  72. {
  73. throw std::runtime_error("BAI (base class) received initBattleInterface call");
  74. }
  75. void initBattleInterface(std::shared_ptr<Environment> _1, std::shared_ptr<CBattleCallback> _2, AutocombatPreferences _3) override
  76. {
  77. throw std::runtime_error("BAI (base class) received initBattleInterface call");
  78. }
  79. Schema::IModel * model;
  80. const int version;
  81. const std::string name = "BAI"; // used in logging
  82. const std::string colorname;
  83. const std::shared_ptr<Environment> env;
  84. const std::shared_ptr<CBattleCallback> cb;
  85. std::string addrstr = "?";
  86. // Set via VCMI_BAI_VERBOSE env var ("1" to enable)
  87. bool verbose = false;
  88. bool enableSpellsUsage = false;
  89. /*
  90. * Templates defined in the header
  91. * Needed to prevent linker errors for calls from derived classes
  92. */
  93. template<typename... Args>
  94. void _log(const ELogLevel::ELogLevel level, const std::string & format, Args... args) const
  95. {
  96. logAi->log(level, "%s-%s [%s] " + format, name, addrstr, colorname, std::move(args)...);
  97. }
  98. template<typename... Args>
  99. void error(const std::string & format, Args... args) const
  100. {
  101. log(ELogLevel::ERROR, format, args...);
  102. }
  103. template<typename... Args>
  104. void warn(const std::string & format, Args... args) const
  105. {
  106. log(ELogLevel::WARN, format, args...);
  107. }
  108. template<typename... Args>
  109. void info(const std::string & format, Args... args) const
  110. {
  111. log(ELogLevel::INFO, format, args...);
  112. }
  113. template<typename... Args>
  114. void debug(const std::string & format, Args... args) const
  115. {
  116. log(ELogLevel::DEBUG, format, args...);
  117. }
  118. template<typename... Args>
  119. void trace(const std::string & format, Args... args) const
  120. {
  121. log(ELogLevel::DEBUG, format, args...);
  122. }
  123. template<typename... Args>
  124. void log(ELogLevel::ELogLevel level, const std::string & format, Args... args) const
  125. {
  126. if(logAi->getEffectiveLevel() <= level)
  127. _log(level, format, args...);
  128. }
  129. void error(const std::string & text) const
  130. {
  131. log(ELogLevel::ERROR, text);
  132. }
  133. void warn(const std::string & text) const
  134. {
  135. log(ELogLevel::WARN, text);
  136. }
  137. void info(const std::string & text) const
  138. {
  139. log(ELogLevel::INFO, text);
  140. }
  141. void debug(const std::string & text) const
  142. {
  143. log(ELogLevel::DEBUG, text);
  144. }
  145. void trace(const std::string & text) const
  146. {
  147. log(ELogLevel::TRACE, text);
  148. }
  149. void log(ELogLevel::ELogLevel level, const std::string & text) const
  150. {
  151. if(logAi->getEffectiveLevel() <= level)
  152. _log(level, "%s", text);
  153. }
  154. void error(const std::function<std::string()> & f) const
  155. {
  156. log(ELogLevel::ERROR, f);
  157. }
  158. void warn(const std::function<std::string()> & f) const
  159. {
  160. log(ELogLevel::WARN, f);
  161. }
  162. void info(const std::function<std::string()> & f) const
  163. {
  164. log(ELogLevel::INFO, f);
  165. }
  166. void debug(const std::function<std::string()> & f) const
  167. {
  168. log(ELogLevel::DEBUG, f);
  169. }
  170. void trace(const std::function<std::string()> & f) const
  171. {
  172. log(ELogLevel::TRACE, f);
  173. }
  174. template<typename F>
  175. void log(ELogLevel::ELogLevel level, F & f) const
  176. requires(std::is_invocable_r_v<std::string, F &>)
  177. {
  178. if(logAi->getEffectiveLevel() <= level)
  179. _log(level, "%s", f());
  180. }
  181. };
  182. }