ISpellMechanics.h 10 KB

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