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 "../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. si32 AIValue = 0;
  85. bool smartTarget = true;
  86. bool clearTarget = false;
  87. bool clearAffected = false;
  88. std::vector<int> range = { 0 };
  89. //TODO: remove these two when AI will understand special effects
  90. std::vector<std::shared_ptr<Bonus>> effects; //deprecated
  91. std::vector<std::shared_ptr<Bonus>> cumulativeEffects; //deprecated
  92. JsonNode battleEffects;
  93. };
  94. /** \brief Low level accessor. Don`t use it if absolutely necessary
  95. *
  96. * \param level. spell school level
  97. * \return Spell level info structure
  98. *
  99. */
  100. const CSpell::LevelInfo & getLevelInfo(const int32_t level) const;
  101. SpellID id;
  102. std::string identifier;
  103. std::string modScope;
  104. public:
  105. enum ESpellPositiveness
  106. {
  107. NEGATIVE = -1,
  108. NEUTRAL = 0,
  109. POSITIVE = 1
  110. };
  111. struct DLL_LINKAGE TargetInfo
  112. {
  113. spells::AimType type;
  114. bool smart;
  115. bool massive;
  116. bool clearAffected;
  117. bool clearTarget;
  118. TargetInfo(const CSpell * spell, const int32_t level, spells::Mode mode);
  119. };
  120. using BTVector = std::vector<BonusType>;
  121. std::map<SpellSchool, bool> school;
  122. std::map<FactionID, si32> probabilities; //% chance to gain for castles
  123. bool onlyOnWaterMap; //Spell will be banned on maps without water
  124. 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)
  125. JsonNode targetCondition; //custom condition on what spell can affect
  126. CSpell();
  127. ~CSpell();
  128. int64_t calculateDamage(const spells::Caster * caster) const override;
  129. bool hasSchool(SpellSchool school) const override;
  130. bool canCastOnSelf() const override;
  131. bool canCastOnlyOnSelf() const override;
  132. bool canCastWithoutSkip() const override;
  133. /**
  134. * Calls cb for each school this spell belongs to
  135. *
  136. * Set stop to true to abort looping
  137. */
  138. void forEachSchool(const std::function<void(const SpellSchool &, bool &)> & cb) const override;
  139. spells::AimType getTargetType() const;
  140. bool hasEffects() const;
  141. void getEffects(std::vector<Bonus> & lst, const int level, const bool cumulative, const si32 duration, std::optional<si32 *> maxDuration = std::nullopt) const;
  142. bool hasBattleEffects() const;
  143. int32_t getCost(const int32_t skillLevel) const override;
  144. si32 getProbability(const FactionID & factionId) const;
  145. int32_t getBasePower() const override;
  146. int32_t getLevelPower(const int32_t skillLevel) const override;
  147. int32_t getIndex() const override;
  148. int32_t getIconIndex() const override;
  149. std::string getJsonKey() const override;
  150. std::string getModScope() const override;
  151. SpellID getId() const override;
  152. std::string getNameTextID() const override;
  153. std::string getNameTranslated() const override;
  154. std::string getDescriptionTextID(int32_t level) const override;
  155. std::string getDescriptionTranslated(int32_t level) const override;
  156. int32_t getLevel() const override;
  157. boost::logic::tribool getPositiveness() const override;
  158. bool isPositive() const override;
  159. bool isNegative() const override;
  160. bool isNeutral() const override;
  161. bool isMagical() const override;
  162. bool isDamage() const override;
  163. bool isOffensive() const override;
  164. bool isSpecial() const override;
  165. bool isAdventure() const override;
  166. bool isCombat() const override;
  167. bool isCreatureAbility() const override;
  168. void registerIcons(const IconRegistar & cb) const override;
  169. const ImagePath & getIconImmune() const; ///< Returns resource name of icon for SPELL_IMMUNITY bonus
  170. const std::string & getIconBook() const;
  171. const std::string & getIconEffect() const;
  172. const std::string & getIconScenarioBonus() const;
  173. const std::string & getIconScroll() const;
  174. const AudioPath & getCastSound() const;
  175. void updateFrom(const JsonNode & data);
  176. void serializeJson(JsonSerializeFormat & handler);
  177. friend class CSpellHandler;
  178. friend class Graphics;
  179. friend class test::CSpellTest;
  180. public:
  181. ///internal interface (for callbacks)
  182. ///Checks general but spell-specific problems. Use only during battle.
  183. bool canBeCast(const CBattleInfoCallback * cb, spells::Mode mode, const spells::Caster * caster) const;
  184. bool canBeCast(spells::Problem & problem, const CBattleInfoCallback * cb, spells::Mode mode, const spells::Caster * caster) const;
  185. public:
  186. ///Server logic. Has write access to GameState via packets.
  187. ///May be executed on client side by (future) non-cheat-proof scripts.
  188. bool adventureCast(SpellCastEnvironment * env, const AdventureSpellCastParameters & parameters) const;
  189. public://internal, for use only by Mechanics classes
  190. ///applies caster`s secondary skills and affectedCreature`s to raw damage
  191. int64_t adjustRawDamage(const spells::Caster * caster, const battle::Unit * affectedCreature, int64_t rawDamage) const;
  192. ///returns raw damage or healed HP
  193. int64_t calculateRawEffectValue(int32_t effectLevel, int32_t basePowerMultiplier, int32_t levelPowerMultiplier) const;
  194. const IAdventureSpellMechanics & getAdventureMechanics() const;
  195. std::unique_ptr<spells::Mechanics> battleMechanics(const spells::IBattleCast * event) const;
  196. private:
  197. void setIsOffensive(const bool val);
  198. void setIsRising(const bool val);
  199. JsonNode convertTargetCondition(const BTVector & immunity, const BTVector & absImmunity, const BTVector & limit, const BTVector & absLimit) const;
  200. //call this after load or deserialization. cant be done in constructor.
  201. void setupMechanics();
  202. private:
  203. si32 defaultProbability;
  204. bool rising;
  205. bool damage;
  206. bool offensive;
  207. bool special;
  208. bool nonMagical; //For creature abilities like bind
  209. std::string attributes; //reference only attributes //todo: remove or include in configuration format, currently unused
  210. spells::AimType targetType;
  211. ///graphics related stuff
  212. ImagePath iconImmune;
  213. std::string iconBook;
  214. std::string iconEffect;
  215. std::string iconScenarioBonus;
  216. std::string iconScroll;
  217. ///sound related stuff
  218. AudioPath castSound;
  219. std::vector<LevelInfo> levels;
  220. si32 level;
  221. si32 power; //spell's power
  222. bool combat; //is this spell combat (true) or adventure (false)
  223. bool creatureAbility; //if true, only creatures can use this spell
  224. bool castOnSelf; // if set, creature caster can cast this spell on itself
  225. bool castOnlyOnSelf; // 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