ISpellMechanics.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * ISpellMechanics.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 <vcmi/spells/Magic.h>
  12. #include <vcmi/ServerCallback.h>
  13. #include "../battle/Destination.h"
  14. #include "../int3.h"
  15. #include "../GameConstants.h"
  16. #include "../bonuses/Bonus.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. struct Query;
  19. class IBattleState;
  20. class CRandomGenerator;
  21. class CreatureService;
  22. class CMap;
  23. class CGameInfoCallback;
  24. class CBattleInfoCallback;
  25. class IGameInfoCallback;
  26. class JsonNode;
  27. class CStack;
  28. class CGObjectInstance;
  29. class CGHeroInstance;
  30. namespace spells
  31. {
  32. class Service;
  33. }
  34. namespace vstd
  35. {
  36. class RNG;
  37. }
  38. #if SCRIPTING_ENABLED
  39. namespace scripting
  40. {
  41. class Service;
  42. }
  43. #endif
  44. ///callback to be provided by server
  45. class DLL_LINKAGE SpellCastEnvironment : public ServerCallback
  46. {
  47. public:
  48. virtual ~SpellCastEnvironment() = default;
  49. virtual const CMap * getMap() const = 0;
  50. virtual const CGameInfoCallback * getCb() const = 0;
  51. virtual bool moveHero(ObjectInstanceID hid, int3 dst, bool teleporting) = 0; //TODO: remove
  52. virtual void genericQuery(Query * request, PlayerColor color, std::function<void(std::optional<int32_t>)> callback) = 0;//TODO: type safety on query, use generic query packet when implemented
  53. };
  54. namespace spells
  55. {
  56. class DLL_LINKAGE IBattleCast
  57. {
  58. public:
  59. using Value = int32_t;
  60. using Value64 = int64_t;
  61. using OptionalValue = std::optional<Value>;
  62. using OptionalValue64 = std::optional<Value64>;
  63. virtual const CSpell * getSpell() const = 0;
  64. virtual Mode getMode() const = 0;
  65. virtual const Caster * getCaster() const = 0;
  66. virtual const CBattleInfoCallback * getBattle() const = 0;
  67. virtual const IGameInfoCallback * getGame() const = 0;
  68. virtual OptionalValue getSpellLevel() const = 0;
  69. virtual OptionalValue getEffectPower() const = 0;
  70. virtual OptionalValue getEffectDuration() const = 0;
  71. virtual OptionalValue64 getEffectValue() const = 0;
  72. virtual boost::logic::tribool isSmart() const = 0;
  73. virtual boost::logic::tribool isMassive() const = 0;
  74. };
  75. ///all parameters of particular cast event
  76. class DLL_LINKAGE BattleCast : public IBattleCast
  77. {
  78. public:
  79. boost::logic::tribool smart;
  80. boost::logic::tribool massive;
  81. //normal constructor
  82. BattleCast(const CBattleInfoCallback * cb_, const Caster * caster_, const Mode mode_, const CSpell * spell_);
  83. //magic mirror constructor
  84. BattleCast(const BattleCast & orig, const Caster * caster_);
  85. virtual ~BattleCast();
  86. ///IBattleCast
  87. const CSpell * getSpell() const override;
  88. Mode getMode() const override;
  89. const Caster * getCaster() const override;
  90. const CBattleInfoCallback * getBattle() const override;
  91. const IGameInfoCallback * getGame() const override;
  92. OptionalValue getSpellLevel() const override;
  93. OptionalValue getEffectPower() const override;
  94. OptionalValue getEffectDuration() const override;
  95. OptionalValue64 getEffectValue() const override;
  96. boost::logic::tribool isSmart() const override;
  97. boost::logic::tribool isMassive() const override;
  98. void setSpellLevel(Value value);
  99. void setEffectPower(Value value);
  100. void setEffectDuration(Value value);
  101. void setEffectValue(Value64 value);
  102. ///only apply effects to specified targets
  103. void applyEffects(ServerCallback * server, const Target & target, bool indirect = false, bool ignoreImmunity = false) const;
  104. ///normal cast
  105. void cast(ServerCallback * server, Target target);
  106. ///cast evaluation
  107. void castEval(ServerCallback * server, Target target);
  108. ///cast with silent check for permitted cast
  109. bool castIfPossible(ServerCallback * server, Target target);
  110. std::vector<Target> findPotentialTargets(bool fast = false) const;
  111. private:
  112. ///spell school level
  113. OptionalValue magicSkillLevel;
  114. ///actual spell-power affecting effect values
  115. OptionalValue effectPower;
  116. ///actual spell-power affecting effect duration
  117. OptionalValue effectDuration;
  118. ///for Archangel-like casting
  119. OptionalValue64 effectValue;
  120. Mode mode;
  121. const CSpell * spell;
  122. const CBattleInfoCallback * cb;
  123. const IGameInfoCallback * gameCb;
  124. const Caster * caster;
  125. };
  126. class DLL_LINKAGE ISpellMechanicsFactory
  127. {
  128. public:
  129. virtual ~ISpellMechanicsFactory();
  130. virtual std::unique_ptr<Mechanics> create(const IBattleCast * event) const = 0;
  131. static std::unique_ptr<ISpellMechanicsFactory> get(const CSpell * s);
  132. protected:
  133. const CSpell * spell;
  134. ISpellMechanicsFactory(const CSpell * s);
  135. };
  136. class DLL_LINKAGE Mechanics
  137. {
  138. public:
  139. virtual ~Mechanics();
  140. virtual bool adaptProblem(ESpellCastProblem source, Problem & target) const = 0;
  141. virtual bool adaptGenericProblem(Problem & target) const = 0;
  142. virtual std::vector<BattleHex> rangeInHexes(BattleHex centralHex) const = 0;
  143. virtual std::vector<const CStack *> getAffectedStacks(const Target & target) const = 0;
  144. virtual bool canBeCast(Problem & problem) const = 0;
  145. virtual bool canBeCastAt(const Target & target, Problem & problem) const = 0;
  146. virtual void applyEffects(ServerCallback * server, const Target & targets, bool indirect, bool ignoreImmunity) const = 0;
  147. virtual void cast(ServerCallback * server, const Target & target) = 0;
  148. virtual void castEval(ServerCallback * server, const Target & target) = 0;
  149. virtual bool isReceptive(const battle::Unit * target) const = 0;
  150. virtual std::vector<AimType> getTargetTypes() const = 0;
  151. virtual std::vector<Destination> getPossibleDestinations(size_t index, AimType aimType, const Target & current, bool fast = false) const = 0;
  152. virtual const Spell * getSpell() const = 0;
  153. //Cast event facade
  154. virtual IBattleCast::Value getEffectLevel() const = 0;
  155. virtual IBattleCast::Value getRangeLevel() const = 0;
  156. virtual IBattleCast::Value getEffectPower() const = 0;
  157. virtual IBattleCast::Value getEffectDuration() const = 0;
  158. virtual IBattleCast::Value64 getEffectValue() const = 0;
  159. virtual PlayerColor getCasterColor() const = 0;
  160. //Spell facade
  161. virtual int32_t getSpellIndex() const = 0;
  162. virtual SpellID getSpellId() const = 0;
  163. virtual std::string getSpellName() const = 0;
  164. virtual int32_t getSpellLevel() const = 0;
  165. virtual bool isSmart() const = 0;
  166. virtual bool isMassive() const = 0;
  167. virtual bool alwaysHitFirstTarget() const = 0;
  168. virtual bool requiresClearTiles() const = 0;
  169. virtual bool isNegativeSpell() const = 0;
  170. virtual bool isPositiveSpell() const = 0;
  171. virtual bool isMagicalEffect() const = 0;
  172. virtual int64_t adjustEffectValue(const battle::Unit * target) const = 0;
  173. virtual int64_t applySpellBonus(int64_t value, const battle::Unit * target) const = 0;
  174. virtual int64_t applySpecificSpellBonus(int64_t value) const = 0;
  175. virtual int64_t calculateRawEffectValue(int32_t basePowerMultiplier, int32_t levelPowerMultiplier) const = 0;
  176. //Battle facade
  177. virtual bool ownerMatches(const battle::Unit * unit) const = 0;
  178. virtual bool ownerMatches(const battle::Unit * unit, const boost::logic::tribool positivness) const = 0;
  179. //Global environment facade
  180. virtual const CreatureService * creatures() const = 0;
  181. #if SCRIPTING_ENABLED
  182. virtual const scripting::Service * scripts() const = 0;
  183. #endif
  184. virtual const Service * spells() const = 0;
  185. virtual const IGameInfoCallback * game() const = 0;
  186. virtual const CBattleInfoCallback * battle() const = 0;
  187. const Caster * caster;
  188. ui8 casterSide;
  189. protected:
  190. Mechanics();
  191. };
  192. class DLL_LINKAGE BaseMechanics : public Mechanics
  193. {
  194. public:
  195. virtual ~BaseMechanics();
  196. bool adaptProblem(ESpellCastProblem source, Problem & target) const override;
  197. bool adaptGenericProblem(Problem & target) const override;
  198. int32_t getSpellIndex() const override;
  199. SpellID getSpellId() const override;
  200. std::string getSpellName() const override;
  201. int32_t getSpellLevel() const override;
  202. IBattleCast::Value getEffectLevel() const override;
  203. IBattleCast::Value getRangeLevel() const override;
  204. IBattleCast::Value getEffectPower() const override;
  205. IBattleCast::Value getEffectDuration() const override;
  206. IBattleCast::Value64 getEffectValue() const override;
  207. PlayerColor getCasterColor() const override;
  208. bool isSmart() const override;
  209. bool isMassive() const override;
  210. bool requiresClearTiles() const override;
  211. bool alwaysHitFirstTarget() const override;
  212. bool isNegativeSpell() const override;
  213. bool isPositiveSpell() const override;
  214. bool isMagicalEffect() const override;
  215. int64_t adjustEffectValue(const battle::Unit * target) const override;
  216. int64_t applySpellBonus(int64_t value, const battle::Unit * target) const override;
  217. int64_t applySpecificSpellBonus(int64_t value) const override;
  218. int64_t calculateRawEffectValue(int32_t basePowerMultiplier, int32_t levelPowerMultiplier) const override;
  219. bool ownerMatches(const battle::Unit * unit) const override;
  220. bool ownerMatches(const battle::Unit * unit, const boost::logic::tribool positivness) const override;
  221. std::vector<AimType> getTargetTypes() const override;
  222. const CreatureService * creatures() const override;
  223. #if SCRIPTING_ENABLED
  224. const scripting::Service * scripts() const override;
  225. #endif
  226. const Service * spells() const override;
  227. const IGameInfoCallback * game() const override;
  228. const CBattleInfoCallback * battle() const override;
  229. protected:
  230. const CSpell * owner;
  231. Mode mode;
  232. BaseMechanics(const IBattleCast * event);
  233. private:
  234. IBattleCast::Value rangeLevel;
  235. IBattleCast::Value effectLevel;
  236. ///actual spell-power affecting effect values
  237. IBattleCast::Value effectPower;
  238. ///actual spell-power affecting effect duration
  239. IBattleCast::Value effectDuration;
  240. ///raw damage/heal amount
  241. IBattleCast::Value64 effectValue;
  242. boost::logic::tribool smart;
  243. boost::logic::tribool massive;
  244. const IGameInfoCallback * gameCb;
  245. const CBattleInfoCallback * cb;
  246. };
  247. class DLL_LINKAGE IReceptiveCheck
  248. {
  249. public:
  250. virtual ~IReceptiveCheck() = default;
  251. virtual bool isReceptive(const Mechanics * m, const battle::Unit * target) const = 0;
  252. };
  253. }// namespace spells
  254. class DLL_LINKAGE AdventureSpellCastParameters
  255. {
  256. public:
  257. const spells::Caster * caster;
  258. int3 pos;
  259. };
  260. class DLL_LINKAGE IAdventureSpellMechanics
  261. {
  262. public:
  263. IAdventureSpellMechanics(const CSpell * s);
  264. virtual ~IAdventureSpellMechanics() = default;
  265. virtual bool adventureCast(SpellCastEnvironment * env, const AdventureSpellCastParameters & parameters) const = 0;
  266. static std::unique_ptr<IAdventureSpellMechanics> createMechanics(const CSpell * s);
  267. protected:
  268. const CSpell * owner;
  269. };
  270. VCMI_LIB_NAMESPACE_END