2
0

ISpellMechanics.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "CSpellHandler.h"
  12. #include "../BattleHex.h"
  13. ///callback to be provided by server
  14. class DLL_LINKAGE SpellCastEnvironment
  15. {
  16. public:
  17. virtual ~SpellCastEnvironment(){};
  18. virtual void sendAndApply(CPackForClient * info) const = 0;
  19. virtual CRandomGenerator & getRandomGenerator() const = 0;
  20. virtual void complain(const std::string & problem) const = 0;
  21. virtual const CMap * getMap() const = 0;
  22. virtual const CGameInfoCallback * getCb() const = 0;
  23. virtual bool moveHero(ObjectInstanceID hid, int3 dst, ui8 teleporting, PlayerColor asker = PlayerColor::NEUTRAL) const =0; //TODO: remove
  24. };
  25. ///all parameters of particular cast event
  26. struct DLL_LINKAGE BattleSpellCastParameters
  27. {
  28. public:
  29. ///Single spell destination.
  30. /// (assumes that anything but battle stack can share same hex)
  31. struct DLL_LINKAGE Destination
  32. {
  33. explicit Destination(const CStack * destination);
  34. explicit Destination(const BattleHex & destination);
  35. const CStack * stackValue;
  36. const BattleHex hexValue;
  37. };
  38. //normal constructor
  39. BattleSpellCastParameters(const BattleInfo * cb, const ISpellCaster * caster, const CSpell * spell_);
  40. //magic mirror constructor
  41. BattleSpellCastParameters(const BattleSpellCastParameters & orig, const ISpellCaster * caster);
  42. void aimToHex(const BattleHex & destination);
  43. void aimToStack(const CStack * destination);
  44. void cast(const SpellCastEnvironment * env);
  45. ///cast with silent check for permitted cast
  46. void castIfPossible(const SpellCastEnvironment * env);
  47. BattleHex getFirstDestinationHex() const;
  48. int getEffectValue() const;
  49. const CSpell * spell;
  50. const BattleInfo * cb;
  51. const ISpellCaster * caster;
  52. const PlayerColor casterColor;
  53. const ui8 casterSide;
  54. std::vector<Destination> destinations;
  55. const CGHeroInstance * casterHero; //deprecated
  56. ECastingMode::ECastingMode mode;
  57. const CStack * casterStack; //deprecated
  58. ///spell school level
  59. int spellLvl;
  60. ///spell school level to use for effects
  61. int effectLevel;
  62. ///actual spell-power affecting effect values
  63. int effectPower;
  64. ///actual spell-power affecting effect duration
  65. int enchantPower;
  66. private:
  67. ///for Archangel-like casting
  68. int effectValue;
  69. };
  70. struct DLL_LINKAGE SpellTargetingContext
  71. {
  72. CSpell::TargetInfo ti;
  73. ECastingMode::ECastingMode mode;
  74. BattleHex destination;
  75. const ISpellCaster * caster;
  76. int schoolLvl;
  77. SpellTargetingContext(const CSpell * s, ECastingMode::ECastingMode mode_, const ISpellCaster * caster_, int schoolLvl_, BattleHex destination_)
  78. : ti(s,schoolLvl_, mode_), mode(mode_), destination(destination_), caster(caster_), schoolLvl(schoolLvl_)
  79. {};
  80. };
  81. class DLL_LINKAGE ISpellMechanics
  82. {
  83. public:
  84. ISpellMechanics(CSpell * s);
  85. virtual ~ISpellMechanics(){};
  86. virtual std::vector<BattleHex> rangeInHexes(BattleHex centralHex, ui8 schoolLvl, ui8 side, bool * outDroppedHexes = nullptr) const = 0;
  87. virtual std::vector<const CStack *> getAffectedStacks(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx) const = 0;
  88. virtual ESpellCastProblem::ESpellCastProblem canBeCast(const CBattleInfoCallback * cb, const ECastingMode::ECastingMode mode, const ISpellCaster * caster) const = 0;
  89. virtual ESpellCastProblem::ESpellCastProblem canBeCast(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx) const = 0;
  90. virtual ESpellCastProblem::ESpellCastProblem isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const = 0;
  91. virtual void applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const = 0;
  92. virtual void battleCast(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters) const = 0;
  93. //if true use generic algorithm for target existence check, see CSpell::canBeCast
  94. virtual bool requiresCreatureTarget() const = 0;
  95. static std::unique_ptr<ISpellMechanics> createMechanics(CSpell * s);
  96. protected:
  97. CSpell * owner;
  98. };
  99. struct DLL_LINKAGE AdventureSpellCastParameters
  100. {
  101. const CGHeroInstance * caster;
  102. int3 pos;
  103. };
  104. class DLL_LINKAGE IAdventureSpellMechanics
  105. {
  106. public:
  107. IAdventureSpellMechanics(CSpell * s);
  108. virtual ~IAdventureSpellMechanics() = default;
  109. virtual bool adventureCast(const SpellCastEnvironment * env, AdventureSpellCastParameters & parameters) const = 0;
  110. static std::unique_ptr<IAdventureSpellMechanics> createMechanics(CSpell * s);
  111. protected:
  112. CSpell * owner;
  113. };