CSpellHandler.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 "../bonuses/Bonus.h"
  18. #include "../filesystem/ResourcePath.h"
  19. #include "../json/JsonNode.h"
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. class CSpell;
  22. class IAdventureSpellMechanics;
  23. class CBattleInfoCallback;
  24. class AdventureSpellCastParameters;
  25. class SpellCastEnvironment;
  26. class JsonSerializeFormat;
  27. namespace test
  28. {
  29. class CSpellTest;
  30. }
  31. namespace spells
  32. {
  33. class ISpellMechanicsFactory;
  34. class IBattleCast;
  35. struct SchoolInfo
  36. {
  37. SpellSchool id; //backlink
  38. std::string jsonName;
  39. };
  40. }
  41. namespace SpellConfig
  42. {
  43. extern const spells::SchoolInfo SCHOOL[4];
  44. }
  45. enum class VerticalPosition : ui8{TOP, CENTER, BOTTOM};
  46. class DLL_LINKAGE CSpell : public spells::Spell
  47. {
  48. public:
  49. struct ProjectileInfo
  50. {
  51. ///in radians. Only positive value. Negative angle is handled by vertical flip
  52. double minimumAngle;
  53. ///resource name
  54. AnimationPath resourceName;
  55. };
  56. struct AnimationItem
  57. {
  58. AnimationPath resourceName;
  59. std::string effectName;
  60. VerticalPosition verticalPosition;
  61. float transparency;
  62. int pause;
  63. AnimationItem();
  64. };
  65. using TAnimation = AnimationItem;
  66. using TAnimationQueue = std::vector<TAnimation>;
  67. struct DLL_LINKAGE AnimationInfo
  68. {
  69. ///displayed on all affected targets.
  70. TAnimationQueue affect;
  71. ///displayed on caster.
  72. TAnimationQueue cast;
  73. ///displayed on target hex. If spell was cast with no target selection displayed on entire battlefield (f.e. ARMAGEDDON)
  74. TAnimationQueue hit;
  75. ///displayed "between" caster and (first) target. Ignored if spell was cast with no target selection.
  76. ///use selectProjectile to access
  77. std::vector<ProjectileInfo> projectile;
  78. AnimationPath selectProjectile(const double angle) const;
  79. } animationInfo;
  80. public:
  81. struct LevelInfo
  82. {
  83. si32 cost = 0;
  84. si32 power = 0;
  85. si32 AIValue = 0;
  86. bool smartTarget = true;
  87. bool clearTarget = false;
  88. bool clearAffected = false;
  89. std::vector<int> range = { 0 };
  90. //TODO: remove these two when AI will understand special effects
  91. std::vector<std::shared_ptr<Bonus>> effects; //deprecated
  92. std::vector<std::shared_ptr<Bonus>> cumulativeEffects; //deprecated
  93. JsonNode battleEffects;
  94. };
  95. /** \brief Low level accessor. Don`t use it if absolutely necessary
  96. *
  97. * \param level. spell school level
  98. * \return Spell level info structure
  99. *
  100. */
  101. const CSpell::LevelInfo & getLevelInfo(const int32_t level) const;
  102. SpellID id;
  103. std::string identifier;
  104. std::string modScope;
  105. public:
  106. enum ESpellPositiveness
  107. {
  108. NEGATIVE = -1,
  109. NEUTRAL = 0,
  110. POSITIVE = 1
  111. };
  112. struct DLL_LINKAGE TargetInfo
  113. {
  114. spells::AimType type;
  115. bool smart;
  116. bool massive;
  117. bool clearAffected;
  118. bool clearTarget;
  119. TargetInfo(const CSpell * spell, const int32_t level, spells::Mode mode);
  120. };
  121. using BTVector = std::vector<BonusType>;
  122. std::map<SpellSchool, bool> school;
  123. std::map<FactionID, si32> probabilities; //% chance to gain for castles
  124. bool onlyOnWaterMap; //Spell will be banned on maps without water
  125. 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)
  126. JsonNode targetCondition; //custom condition on what spell can affect
  127. CSpell();
  128. ~CSpell();
  129. int64_t calculateDamage(const spells::Caster * caster) const override;
  130. bool hasSchool(SpellSchool school) const override;
  131. bool canCastOnSelf() const override;
  132. bool canCastOnlyOnSelf() 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 castOnlyOnSelf; // if set, creature caster can cast this spell on itself
  227. bool castWithoutSkip; // if set the creature will not skip the turn after casting a spell
  228. si8 positiveness; //1 if spell is positive for influenced stacks, 0 if it is indifferent, -1 if it's negative
  229. std::unique_ptr<spells::ISpellMechanicsFactory> mechanics;//(!) do not serialize
  230. std::unique_ptr<IAdventureSpellMechanics> adventureMechanics;//(!) do not serialize
  231. };
  232. bool DLL_LINKAGE isInScreenRange(const int3 &center, const int3 &pos); //for spells like Dimension Door
  233. class DLL_LINKAGE CSpellHandler: public CHandlerBase<SpellID, spells::Spell, CSpell, spells::Service>
  234. {
  235. std::vector<int> spellRangeInHexes(std::string rng) const;
  236. public:
  237. ///IHandler base
  238. std::vector<JsonNode> loadLegacyData() override;
  239. void afterLoadFinalization() override;
  240. void beforeValidate(JsonNode & object) override;
  241. /**
  242. * Gets a list of default allowed spells. OH3 spells are all allowed by default.
  243. *
  244. */
  245. std::set<SpellID> getDefaultAllowed() const;
  246. protected:
  247. const std::vector<std::string> & getTypeNames() const override;
  248. std::shared_ptr<CSpell> loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override;
  249. };
  250. VCMI_LIB_NAMESPACE_END