ISpellMechanics.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 getSpellLevel() const = 0;
  74. virtual OptionalValue getEffectPower() const = 0;
  75. virtual OptionalValue getEffectDuration() const = 0;
  76. virtual OptionalValue64 getEffectValue() const = 0;
  77. virtual boost::logic::tribool isSmart() const = 0;
  78. virtual boost::logic::tribool isMassive() const = 0;
  79. };
  80. ///all parameters of particular cast event
  81. class DLL_LINKAGE BattleCast : public IBattleCast
  82. {
  83. public:
  84. Target target;
  85. boost::logic::tribool smart;
  86. boost::logic::tribool massive;
  87. //normal constructor
  88. BattleCast(const CBattleInfoCallback * cb, const Caster * caster_, const Mode mode_, const CSpell * spell_);
  89. //magic mirror constructor
  90. BattleCast(const BattleCast & orig, const Caster * caster_);
  91. virtual ~BattleCast();
  92. ///IBattleCast
  93. const CSpell * getSpell() const override;
  94. Mode getMode() const override;
  95. const Caster * getCaster() const override;
  96. const CBattleInfoCallback * getBattle() const override;
  97. OptionalValue getSpellLevel() const override;
  98. OptionalValue getEffectPower() const override;
  99. OptionalValue getEffectDuration() const override;
  100. OptionalValue64 getEffectValue() const override;
  101. boost::logic::tribool isSmart() const override;
  102. boost::logic::tribool isMassive() const override;
  103. void setSpellLevel(Value value);
  104. void setEffectPower(Value value);
  105. void setEffectDuration(Value value);
  106. void setEffectValue(Value64 value);
  107. void aimToHex(const BattleHex & destination);
  108. void aimToUnit(const battle::Unit * destination);
  109. ///only apply effects to specified targets
  110. void applyEffects(const SpellCastEnvironment * env, bool indirect = false, bool ignoreImmunity = false) const;
  111. ///normal cast
  112. void cast(const SpellCastEnvironment * env);
  113. ///cast evaluation
  114. void cast(IBattleState * battleState, vstd::RNG & rng);
  115. ///cast with silent check for permitted cast
  116. bool castIfPossible(const SpellCastEnvironment * env);
  117. std::vector<Target> findPotentialTargets() const;
  118. private:
  119. ///spell school level
  120. OptionalValue magicSkillLevel;
  121. ///actual spell-power affecting effect values
  122. OptionalValue effectPower;
  123. ///actual spell-power affecting effect duration
  124. OptionalValue effectDuration;
  125. ///for Archangel-like casting
  126. OptionalValue64 effectValue;
  127. Mode mode;
  128. const CSpell * spell;
  129. const CBattleInfoCallback * cb;
  130. const Caster * caster;
  131. };
  132. class DLL_LINKAGE ISpellMechanicsFactory
  133. {
  134. public:
  135. virtual ~ISpellMechanicsFactory();
  136. virtual std::unique_ptr<Mechanics> create(const IBattleCast * event) const = 0;
  137. static std::unique_ptr<ISpellMechanicsFactory> get(const CSpell * s);
  138. protected:
  139. const CSpell * spell;
  140. ISpellMechanicsFactory(const CSpell * s);
  141. };
  142. class DLL_LINKAGE Mechanics
  143. {
  144. public:
  145. Mechanics();
  146. virtual ~Mechanics();
  147. virtual bool adaptProblem(ESpellCastProblem::ESpellCastProblem source, Problem & target) const = 0;
  148. virtual bool adaptGenericProblem(Problem & target) const = 0;
  149. virtual std::vector<BattleHex> rangeInHexes(BattleHex centralHex, bool * outDroppedHexes = nullptr) const = 0;
  150. virtual std::vector<const CStack *> getAffectedStacks(const Target & target) const = 0;
  151. virtual bool canBeCast(Problem & problem) const = 0;
  152. virtual bool canBeCastAt(const Target & target) const = 0;
  153. virtual void applyEffects(BattleStateProxy * battleState, vstd::RNG & rng, const Target & targets, bool indirect, bool ignoreImmunity) const = 0;
  154. virtual void cast(const PacketSender * server, vstd::RNG & rng, const Target & target) = 0;
  155. virtual void cast(IBattleState * battleState, vstd::RNG & rng, const Target & target) = 0;
  156. virtual bool isReceptive(const battle::Unit * target) const = 0;
  157. virtual std::vector<AimType> getTargetTypes() const = 0;
  158. virtual std::vector<Destination> getPossibleDestinations(size_t index, AimType aimType, const Target & current) const = 0;
  159. //Cast event facade
  160. virtual IBattleCast::Value getEffectLevel() const = 0;
  161. virtual IBattleCast::Value getRangeLevel() const = 0;
  162. virtual IBattleCast::Value getEffectPower() const = 0;
  163. virtual IBattleCast::Value getEffectDuration() const = 0;
  164. virtual IBattleCast::Value64 getEffectValue() const = 0;
  165. virtual PlayerColor getCasterColor() const = 0;
  166. //Spell facade
  167. virtual int32_t getSpellIndex() const = 0;
  168. virtual SpellID getSpellId() const = 0;
  169. virtual std::string getSpellName() const = 0;
  170. virtual int32_t getSpellLevel() const = 0;
  171. virtual bool isSmart() const = 0;
  172. virtual bool isMassive() const = 0;
  173. virtual bool alwaysHitFirstTarget() const = 0;
  174. virtual bool requiresClearTiles() const = 0;
  175. virtual bool isNegativeSpell() const = 0;
  176. virtual bool isPositiveSpell() const = 0;
  177. virtual int64_t adjustEffectValue(const battle::Unit * target) const = 0;
  178. virtual int64_t applySpellBonus(int64_t value, const battle::Unit * target) const = 0;
  179. virtual int64_t applySpecificSpellBonus(int64_t value) const = 0;
  180. virtual int64_t calculateRawEffectValue(int32_t basePowerMultiplier, int32_t levelPowerMultiplier) const = 0;
  181. virtual std::vector<Bonus::BonusType> getElementalImmunity() const = 0;
  182. //Battle facade
  183. virtual bool ownerMatches(const battle::Unit * unit) const = 0;
  184. virtual bool ownerMatches(const battle::Unit * unit, const boost::logic::tribool positivness) const = 0;
  185. const CBattleInfoCallback * cb;
  186. const Caster * caster;
  187. ui8 casterSide;
  188. };
  189. class DLL_LINKAGE BaseMechanics : public Mechanics
  190. {
  191. public:
  192. BaseMechanics(const IBattleCast * event);
  193. virtual ~BaseMechanics();
  194. bool adaptProblem(ESpellCastProblem::ESpellCastProblem source, Problem & target) const override;
  195. bool adaptGenericProblem(Problem & target) const override;
  196. int32_t getSpellIndex() const override;
  197. SpellID getSpellId() const override;
  198. std::string getSpellName() const override;
  199. int32_t getSpellLevel() const override;
  200. IBattleCast::Value getEffectLevel() const override;
  201. IBattleCast::Value getRangeLevel() const override;
  202. IBattleCast::Value getEffectPower() const override;
  203. IBattleCast::Value getEffectDuration() const override;
  204. IBattleCast::Value64 getEffectValue() const override;
  205. PlayerColor getCasterColor() const override;
  206. bool isSmart() const override;
  207. bool isMassive() const override;
  208. bool requiresClearTiles() const override;
  209. bool alwaysHitFirstTarget() const override;
  210. bool isNegativeSpell() const override;
  211. bool isPositiveSpell() const override;
  212. int64_t adjustEffectValue(const battle::Unit * target) const override;
  213. int64_t applySpellBonus(int64_t value, const battle::Unit * target) const override;
  214. int64_t applySpecificSpellBonus(int64_t value) const override;
  215. int64_t calculateRawEffectValue(int32_t basePowerMultiplier, int32_t levelPowerMultiplier) const override;
  216. std::vector<Bonus::BonusType> getElementalImmunity() const override;
  217. bool ownerMatches(const battle::Unit * unit) const override;
  218. bool ownerMatches(const battle::Unit * unit, const boost::logic::tribool positivness) const override;
  219. std::vector<AimType> getTargetTypes() const override;
  220. protected:
  221. const CSpell * owner;
  222. Mode mode;
  223. private:
  224. IBattleCast::Value rangeLevel;
  225. IBattleCast::Value effectLevel;
  226. ///actual spell-power affecting effect values
  227. IBattleCast::Value effectPower;
  228. ///actual spell-power affecting effect duration
  229. IBattleCast::Value effectDuration;
  230. ///raw damage/heal amount
  231. IBattleCast::Value64 effectValue;
  232. boost::logic::tribool smart;
  233. boost::logic::tribool massive;
  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(const SpellCastEnvironment * env, const AdventureSpellCastParameters & parameters) const = 0;
  254. static std::unique_ptr<IAdventureSpellMechanics> createMechanics(const CSpell * s);
  255. protected:
  256. const CSpell * owner;
  257. };