ISpellMechanics.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. BattleHex getFirstDestinationHex() const;
  46. const CSpell * spell;
  47. const BattleInfo * cb;
  48. const ISpellCaster * caster;
  49. const PlayerColor casterColor;
  50. const ui8 casterSide;
  51. std::vector<Destination> destinations;
  52. const CGHeroInstance * casterHero; //deprecated
  53. ECastingMode::ECastingMode mode;
  54. const CStack * casterStack; //deprecated
  55. ///spell school level
  56. int spellLvl;
  57. ///spell school level to use for effects
  58. int effectLevel;
  59. ///actual spell-power affecting effect values
  60. int effectPower;
  61. ///actual spell-power affecting effect duration
  62. int enchantPower;
  63. ///for Archangel-like casting
  64. int effectValue;
  65. };
  66. struct DLL_LINKAGE SpellTargetingContext
  67. {
  68. CSpell::TargetInfo ti;
  69. ECastingMode::ECastingMode mode;
  70. BattleHex destination;
  71. const ISpellCaster * caster;
  72. int schoolLvl;
  73. SpellTargetingContext(const CSpell * s, ECastingMode::ECastingMode mode_, const ISpellCaster * caster_, int schoolLvl_, BattleHex destination_)
  74. : ti(s,schoolLvl_, mode_), mode(mode_), destination(destination_), caster(caster_), schoolLvl(schoolLvl_)
  75. {};
  76. };
  77. class DLL_LINKAGE ISpellMechanics
  78. {
  79. public:
  80. ISpellMechanics(CSpell * s);
  81. virtual ~ISpellMechanics(){};
  82. virtual std::vector<BattleHex> rangeInHexes(BattleHex centralHex, ui8 schoolLvl, ui8 side, bool * outDroppedHexes = nullptr) const = 0;
  83. virtual std::vector<const CStack *> getAffectedStacks(const CBattleInfoCallback * cb, SpellTargetingContext & ctx) const = 0;
  84. virtual ESpellCastProblem::ESpellCastProblem canBeCast(const CBattleInfoCallback * cb, const ISpellCaster * caster) const = 0;
  85. virtual ESpellCastProblem::ESpellCastProblem canBeCast(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx) const = 0;
  86. virtual ESpellCastProblem::ESpellCastProblem isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const = 0;
  87. virtual void applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const = 0;
  88. virtual void battleCast(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters) const = 0;
  89. //if true use generic algorithm for target existence check, see CSpell::canBeCast
  90. virtual bool requiresCreatureTarget() const = 0;
  91. static std::unique_ptr<ISpellMechanics> createMechanics(CSpell * s);
  92. protected:
  93. CSpell * owner;
  94. };
  95. struct DLL_LINKAGE AdventureSpellCastParameters
  96. {
  97. const CGHeroInstance * caster;
  98. int3 pos;
  99. };
  100. class DLL_LINKAGE IAdventureSpellMechanics
  101. {
  102. public:
  103. IAdventureSpellMechanics(CSpell * s);
  104. virtual ~IAdventureSpellMechanics() = default;
  105. virtual bool adventureCast(const SpellCastEnvironment * env, AdventureSpellCastParameters & parameters) const = 0;
  106. static std::unique_ptr<IAdventureSpellMechanics> createMechanics(CSpell * s);
  107. protected:
  108. CSpell * owner;
  109. };