CSpellHandler.h 10 KB

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