ISpellMechanics.h 10 KB

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