CSpellHandler.h 11 KB

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