CSpellHandler.h 9.3 KB

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