ISpellMechanics.h 10 KB

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