CSpellHandler.h 9.4 KB

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