CSpellHandler.h 9.3 KB

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