CSpellHandler.h 9.6 KB

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