CSpellHandler.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * CSpellHandler.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/Spell.h>
  12. #include <vcmi/spells/Service.h>
  13. #include <vcmi/spells/Magic.h>
  14. #include "../IHandlerBase.h"
  15. #include "../int3.h"
  16. #include "../bonuses/Bonus.h"
  17. #include "../filesystem/ResourcePath.h"
  18. #include "../json/JsonNode.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. class CSpell;
  21. class IAdventureSpellMechanics;
  22. class CBattleInfoCallback;
  23. class AdventureSpellCastParameters;
  24. class SpellCastEnvironment;
  25. class JsonSerializeFormat;
  26. namespace test
  27. {
  28. class CSpellTest;
  29. }
  30. namespace spells
  31. {
  32. class ISpellMechanicsFactory;
  33. class IBattleCast;
  34. }
  35. enum class VerticalPosition : ui8{TOP, CENTER, BOTTOM};
  36. class DLL_LINKAGE CSpell : public spells::Spell
  37. {
  38. public:
  39. struct ProjectileInfo
  40. {
  41. ///in radians. Only positive value. Negative angle is handled by vertical flip
  42. double minimumAngle;
  43. ///resource name
  44. AnimationPath resourceName;
  45. };
  46. struct AnimationItem
  47. {
  48. AnimationPath resourceName;
  49. std::string effectName;
  50. VerticalPosition verticalPosition;
  51. float transparency;
  52. int pause;
  53. AnimationItem();
  54. };
  55. using TAnimation = AnimationItem;
  56. using TAnimationQueue = std::vector<TAnimation>;
  57. struct DLL_LINKAGE AnimationInfo
  58. {
  59. ///displayed on all affected targets.
  60. TAnimationQueue affect;
  61. ///displayed on caster.
  62. TAnimationQueue cast;
  63. ///displayed on target hex. If spell was cast with no target selection displayed on entire battlefield (f.e. ARMAGEDDON)
  64. TAnimationQueue hit;
  65. ///displayed "between" caster and (first) target. Ignored if spell was cast with no target selection.
  66. ///use selectProjectile to access
  67. std::vector<ProjectileInfo> projectile;
  68. AnimationPath selectProjectile(const double angle) const;
  69. } animationInfo;
  70. public:
  71. struct LevelInfo
  72. {
  73. si32 cost = 0;
  74. si32 power = 0;
  75. bool smartTarget = true;
  76. bool clearTarget = false;
  77. bool clearAffected = false;
  78. std::vector<int> range = { 0 };
  79. //TODO: remove these two when AI will understand special effects
  80. std::vector<std::shared_ptr<Bonus>> effects; //deprecated
  81. std::vector<std::shared_ptr<Bonus>> cumulativeEffects; //deprecated
  82. JsonNode battleEffects;
  83. JsonNode adventureEffect;
  84. };
  85. /** \brief Low level accessor. Don`t use it if absolutely necessary
  86. *
  87. * \param level. spell school level
  88. * \return Spell level info structure
  89. *
  90. */
  91. const CSpell::LevelInfo & getLevelInfo(const int32_t level) const;
  92. SpellID id;
  93. std::string identifier;
  94. std::string modScope;
  95. public:
  96. enum ESpellPositiveness
  97. {
  98. NEGATIVE = -1,
  99. NEUTRAL = 0,
  100. POSITIVE = 1
  101. };
  102. struct DLL_LINKAGE TargetInfo
  103. {
  104. spells::AimType type;
  105. bool smart;
  106. bool massive;
  107. bool clearAffected;
  108. TargetInfo(const CSpell * spell, const int32_t level, spells::Mode mode);
  109. };
  110. using BTVector = std::vector<BonusType>;
  111. std::set<SpellSchool> schools;
  112. std::map<FactionID, si32> probabilities; //% chance to gain for castles
  113. bool onlyOnWaterMap; //Spell will be banned on maps without water
  114. std::vector<SpellID> counteredSpells; //spells that are removed when effect of this spell is placed on creature (for bless-curse, haste-slow, and similar pairs)
  115. JsonNode targetCondition; //custom condition on what spell can affect
  116. CSpell();
  117. ~CSpell();
  118. int64_t calculateDamage(const spells::Caster * caster) const override;
  119. bool hasSchool(SpellSchool school) const override;
  120. bool canCastOnSelf() const override;
  121. bool canCastOnlyOnSelf() const override;
  122. bool canCastWithoutSkip() const override;
  123. /**
  124. * Calls cb for each school this spell belongs to
  125. *
  126. * Set stop to true to abort looping
  127. */
  128. void forEachSchool(const std::function<void(const SpellSchool &, bool &)> & cb) const override;
  129. spells::AimType getTargetType() const;
  130. bool hasEffects() const;
  131. void getEffects(std::vector<Bonus> & lst, const int level, const bool cumulative, const si32 duration, std::optional<si32 *> maxDuration = std::nullopt) const;
  132. bool hasBattleEffects() const;
  133. int32_t getCost(const int32_t skillLevel) const override;
  134. si32 getProbability(const FactionID & factionId) const;
  135. int32_t getBasePower() const override;
  136. int32_t getLevelPower(const int32_t skillLevel) const override;
  137. int32_t getIndex() const override;
  138. int32_t getIconIndex() const override;
  139. std::string getJsonKey() const override;
  140. std::string getModScope() const override;
  141. SpellID getId() const override;
  142. std::string getNameTextID() const override;
  143. std::string getNameTranslated() const override;
  144. std::string getDescriptionTextID(int32_t level) const override;
  145. std::string getDescriptionTranslated(int32_t level) const override;
  146. int32_t getLevel() const override;
  147. boost::logic::tribool getPositiveness() const override;
  148. bool isPositive() const override;
  149. bool isNegative() const override;
  150. bool isNeutral() const override;
  151. bool isMagical() const override;
  152. bool isDamage() const override;
  153. bool isOffensive() const override;
  154. bool isSpecial() const override;
  155. bool isAdventure() const override;
  156. bool isCombat() const override;
  157. bool isCreatureAbility() const override;
  158. void registerIcons(const IconRegistar & cb) const override;
  159. const ImagePath & getIconImmune() const; ///< Returns resource name of icon for SPELL_IMMUNITY bonus
  160. const std::string & getIconBook() const;
  161. const std::string & getIconEffect() const;
  162. const std::string & getIconScenarioBonus() const;
  163. const std::string & getIconScroll() const;
  164. const AudioPath & getCastSound() const;
  165. void updateFrom(const JsonNode & data);
  166. void serializeJson(JsonSerializeFormat & handler);
  167. friend class CSpellHandler;
  168. friend class Graphics;
  169. friend class test::CSpellTest;
  170. public:
  171. ///internal interface (for callbacks)
  172. ///Checks general but spell-specific problems. Use only during battle.
  173. bool canBeCast(const CBattleInfoCallback * cb, spells::Mode mode, const spells::Caster * caster) const;
  174. bool canBeCast(spells::Problem & problem, const CBattleInfoCallback * cb, spells::Mode mode, const spells::Caster * caster) const;
  175. public:
  176. ///Server logic. Has write access to GameState via packets.
  177. ///May be executed on client side by (future) non-cheat-proof scripts.
  178. bool adventureCast(SpellCastEnvironment * env, const AdventureSpellCastParameters & parameters) const;
  179. public://internal, for use only by Mechanics classes
  180. ///applies caster`s secondary skills and affectedCreature`s to raw damage
  181. int64_t adjustRawDamage(const spells::Caster * caster, const battle::Unit * affectedCreature, int64_t rawDamage) const;
  182. ///returns raw damage or healed HP
  183. int64_t calculateRawEffectValue(int32_t effectLevel, int32_t basePowerMultiplier, int32_t levelPowerMultiplier) const;
  184. const IAdventureSpellMechanics & getAdventureMechanics() const;
  185. std::unique_ptr<spells::Mechanics> battleMechanics(const spells::IBattleCast * event) const;
  186. private:
  187. void setIsOffensive(const bool val);
  188. void setIsRising(const bool val);
  189. JsonNode convertTargetCondition(const BTVector & immunity, const BTVector & absImmunity, const BTVector & limit, const BTVector & absLimit) const;
  190. //call this after load or deserialization. cant be done in constructor.
  191. void setupMechanics();
  192. private:
  193. si32 defaultProbability;
  194. bool rising;
  195. bool damage;
  196. bool offensive;
  197. bool special;
  198. bool nonMagical; //For creature abilities like bind
  199. std::string attributes; //reference only attributes //todo: remove or include in configuration format, currently unused
  200. spells::AimType targetType;
  201. ///graphics related stuff
  202. ImagePath iconImmune;
  203. std::string iconBook;
  204. std::string iconEffect;
  205. std::string iconScenarioBonus;
  206. std::string iconScroll;
  207. ///sound related stuff
  208. AudioPath castSound;
  209. std::vector<LevelInfo> levels;
  210. si32 level;
  211. si32 power; //spell's power
  212. bool combat; //is this spell combat (true) or adventure (false)
  213. bool creatureAbility; //if true, only creatures can use this spell
  214. bool castOnSelf; // if set, creature caster can cast this spell on itself
  215. bool castOnlyOnSelf; // if set, creature caster can cast this spell on itself
  216. bool castWithoutSkip; // if set the creature will not skip the turn after casting a spell
  217. si8 positiveness; //1 if spell is positive for influenced stacks, 0 if it is indifferent, -1 if it's negative
  218. std::unique_ptr<spells::ISpellMechanicsFactory> mechanics;//(!) do not serialize
  219. std::unique_ptr<IAdventureSpellMechanics> adventureMechanics;//(!) do not serialize
  220. };
  221. class DLL_LINKAGE CSpellHandler: public CHandlerBase<SpellID, spells::Spell, CSpell, spells::Service>
  222. {
  223. std::vector<int> spellRangeInHexes(std::string rng) const;
  224. public:
  225. ///IHandler base
  226. std::vector<JsonNode> loadLegacyData() override;
  227. void afterLoadFinalization() override;
  228. void beforeValidate(JsonNode & object) override;
  229. /**
  230. * Gets a list of default allowed spells. OH3 spells are all allowed by default.
  231. *
  232. */
  233. std::set<SpellID> getDefaultAllowed() const;
  234. protected:
  235. const std::vector<std::string> & getTypeNames() const override;
  236. std::shared_ptr<CSpell> loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override;
  237. };
  238. VCMI_LIB_NAMESPACE_END