CSpellHandler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 "../IHandlerBase.h"
  13. #include "../ConstTransitivePtr.h"
  14. #include "../int3.h"
  15. #include "../GameConstants.h"
  16. #include "../BattleHex.h"
  17. #include "../HeroBonus.h"
  18. class CGObjectInstance;
  19. class CSpell;
  20. class ISpellMechanics;
  21. class CLegacyConfigParser;
  22. class CGHeroInstance;
  23. class CStack;
  24. class CBattleInfoCallback;
  25. class BattleInfo;
  26. struct CPackForClient;
  27. struct BattleSpellCast;
  28. class CGameInfoCallback;
  29. class CRandomGenerator;
  30. class CMap;
  31. struct AdventureSpellCastParameters;
  32. struct BattleSpellCastParameters;
  33. class SpellCastEnvironment;
  34. struct SpellSchoolInfo
  35. {
  36. ESpellSchool id; //backlink
  37. Bonus::BonusType damagePremyBonus;
  38. Bonus::BonusType immunityBonus;
  39. std::string jsonName;
  40. SecondarySkill::ESecondarySkill skill;
  41. Bonus::BonusType knoledgeBonus;
  42. };
  43. enum class VerticalPosition : ui8{TOP, CENTER, BOTTOM};
  44. class DLL_LINKAGE CSpell
  45. {
  46. public:
  47. struct ProjectileInfo
  48. {
  49. ///in radians. Only positive value. Negative angle is handled by vertical flip
  50. double minimumAngle;
  51. ///resource name
  52. std::string resourceName;
  53. template <typename Handler> void serialize(Handler & h, const int version)
  54. {
  55. h & minimumAngle & resourceName;
  56. }
  57. };
  58. struct AnimationItem
  59. {
  60. std::string resourceName;
  61. VerticalPosition verticalPosition;
  62. int pause;
  63. AnimationItem();
  64. template <typename Handler> void serialize(Handler & h, const int version)
  65. {
  66. h & resourceName & verticalPosition;
  67. if(version >= 754)
  68. {
  69. h & pause;
  70. }
  71. else if(!h.saving)
  72. {
  73. pause = 0;
  74. }
  75. }
  76. };
  77. typedef AnimationItem TAnimation;
  78. typedef std::vector<TAnimation> TAnimationQueue;
  79. struct DLL_LINKAGE AnimationInfo
  80. {
  81. AnimationInfo();
  82. ~AnimationInfo();
  83. ///displayed on all affected targets.
  84. TAnimationQueue affect;
  85. ///displayed on caster.
  86. TAnimationQueue cast;
  87. ///displayed on target hex. If spell was cast with no target selection displayed on entire battlefield (f.e. ARMAGEDDON)
  88. TAnimationQueue hit;
  89. ///displayed "between" caster and (first) target. Ignored if spell was cast with no target selection.
  90. ///use selectProjectile to access
  91. std::vector<ProjectileInfo> projectile;
  92. template <typename Handler> void serialize(Handler & h, const int version)
  93. {
  94. h & projectile & hit & cast;
  95. }
  96. std::string selectProjectile(const double angle) const;
  97. } animationInfo;
  98. public:
  99. struct LevelInfo
  100. {
  101. std::string description; //descriptions of spell for skill level
  102. si32 cost;
  103. si32 power;
  104. si32 AIValue;
  105. bool smartTarget;
  106. bool clearTarget;
  107. bool clearAffected;
  108. std::string range;
  109. std::vector<Bonus> effects;
  110. std::vector<Bonus *> effectsTmp; //TODO: this should replace effects
  111. LevelInfo();
  112. ~LevelInfo();
  113. template <typename Handler> void serialize(Handler &h, const int version)
  114. {
  115. h & description & cost & power & AIValue & smartTarget & range & effects;
  116. h & clearTarget & clearAffected;
  117. }
  118. };
  119. /** \brief Low level accessor. Don`t use it if absolutely necessary
  120. *
  121. * \param level. spell school level
  122. * \return Spell level info structure
  123. *
  124. */
  125. const CSpell::LevelInfo& getLevelInfo(const int level) const;
  126. public:
  127. enum ETargetType {NO_TARGET, CREATURE, OBSTACLE, LOCATION};
  128. enum ESpellPositiveness {NEGATIVE = -1, NEUTRAL = 0, POSITIVE = 1};
  129. struct DLL_LINKAGE TargetInfo
  130. {
  131. ETargetType type;
  132. bool smart;
  133. bool massive;
  134. bool onlyAlive;
  135. ///no immunity on primary target (mostly spell-like attack)
  136. bool alwaysHitDirectly;
  137. bool clearTarget;
  138. bool clearAffected;
  139. TargetInfo(const CSpell * spell, const int level);
  140. TargetInfo(const CSpell * spell, const int level, ECastingMode::ECastingMode mode);
  141. private:
  142. void init(const CSpell * spell, const int level);
  143. };
  144. SpellID id;
  145. std::string identifier; //???
  146. std::string name;
  147. si32 level;
  148. std::map<ESpellSchool, bool> school; //todo: use this instead of separate boolean fields
  149. si32 power; //spell's power
  150. std::map<TFaction, si32> probabilities; //% chance to gain for castles
  151. bool combatSpell; //is this spell combat (true) or adventure (false)
  152. bool creatureAbility; //if true, only creatures can use this spell
  153. si8 positiveness; //1 if spell is positive for influenced stacks, 0 if it is indifferent, -1 if it's negative
  154. 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)
  155. CSpell();
  156. ~CSpell();
  157. std::vector<BattleHex> rangeInHexes(BattleHex centralHex, ui8 schoolLvl, ui8 side, bool * outDroppedHexes = nullptr ) const; //convert range to specific hexes; last optional out parameter is set to true, if spell would cover unavailable hexes (that are not included in ret)
  158. ETargetType getTargetType() const; //deprecated
  159. bool isCombatSpell() const;
  160. bool isAdventureSpell() const;
  161. bool isCreatureAbility() const;
  162. bool isPositive() const;
  163. bool isNegative() const;
  164. bool isNeutral() const;
  165. bool isDamageSpell() const;
  166. bool isHealingSpell() const;
  167. bool isRisingSpell() const;
  168. bool isOffensiveSpell() const;
  169. bool isSpecialSpell() const;
  170. bool hasEffects() const;
  171. void getEffects(std::vector<Bonus> &lst, const int level) const;
  172. ///calculate spell damage on stack taking caster`s secondary skills and affectedCreature`s bonuses into account
  173. ui32 calculateDamage(const ISpellCaster * caster, const CStack * affectedCreature, int spellSchoolLevel, int usedSpellPower) const;
  174. ///selects from allStacks actually affected stacks
  175. std::set<const CStack *> getAffectedStacks(const CBattleInfoCallback * cb, ECastingMode::ECastingMode mode, PlayerColor casterColor, int spellLvl, BattleHex destination, const ISpellCaster * caster) const;
  176. si32 getCost(const int skillLevel) const;
  177. /**
  178. * Returns spell level power, base power ignored
  179. */
  180. si32 getPower(const int skillLevel) const;
  181. si32 getProbability(const TFaction factionId) const;
  182. /**
  183. * Calls cb for each school this spell belongs to
  184. *
  185. * Set stop to true to abort looping
  186. */
  187. void forEachSchool(const std::function<void (const SpellSchoolInfo &, bool &)> & cb) const;
  188. /**
  189. * Returns resource name of icon for SPELL_IMMUNITY bonus
  190. */
  191. const std::string& getIconImmune() const;
  192. const std::string& getCastSound() const;
  193. template <typename Handler> void serialize(Handler &h, const int version)
  194. {
  195. h & identifier & id & name & level & power
  196. & probabilities & attributes & combatSpell & creatureAbility & positiveness & counteredSpells;
  197. h & isRising & isDamage & isOffensive;
  198. h & targetType;
  199. h & immunities & limiters & absoluteImmunities & absoluteLimiters;
  200. h & iconImmune;
  201. h & defaultProbability;
  202. h & isSpecial;
  203. h & castSound & iconBook & iconEffect & iconScenarioBonus & iconScroll;
  204. h & levels;
  205. h & school;
  206. h & animationInfo;
  207. if(!h.saving)
  208. setup();
  209. }
  210. friend class CSpellHandler;
  211. friend class Graphics;
  212. public:
  213. ///internal interface (for callbacks)
  214. ///Checks general but spell-specific problems for all casting modes. Use only during battle.
  215. ESpellCastProblem::ESpellCastProblem canBeCast(const CBattleInfoCallback * cb, PlayerColor player) const;
  216. ///checks for creature immunity / anything that prevent casting *at given hex* - doesn't take into account general problems such as not having spellbook or mana points etc.
  217. ESpellCastProblem::ESpellCastProblem isImmuneAt(const CBattleInfoCallback * cb, const ISpellCaster * caster, ECastingMode::ECastingMode mode, BattleHex destination) const;
  218. ///checks for creature immunity / anything that prevent casting *at given target* - doesn't take into account general problems such as not having spellbook or mana points etc.
  219. ESpellCastProblem::ESpellCastProblem isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const;
  220. public:
  221. ///Server logic. Has write access to GameState via packets.
  222. ///May be executed on client side by (future) non-cheat-proof scripts.
  223. bool adventureCast(const SpellCastEnvironment * env, AdventureSpellCastParameters & parameters) const;
  224. void battleCast(const SpellCastEnvironment * env, BattleSpellCastParameters & parameters) const;
  225. public:
  226. ///Client-server logic. Has direct write access to GameState.
  227. ///Shall be called (only) when applying packets on BOTH SIDES
  228. ///implementation of BattleSpellCast applying
  229. void applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const;
  230. public://Client logic.
  231. void prepareBattleLog(const CBattleInfoCallback * cb, const BattleSpellCast * packet, std::vector<std::string> & logLines) const;
  232. public://internal, for use only by Mechanics classes
  233. ///applies caster`s secondary skills and affectedCreature`s to raw damage
  234. int adjustRawDamage(const ISpellCaster * caster, const CStack * affectedCreature, int rawDamage) const;
  235. ///returns raw damage or healed HP
  236. int calculateRawEffectValue(int effectLevel, int effectPower) const;
  237. ///generic immunity calculation
  238. ESpellCastProblem::ESpellCastProblem internalIsImmune(const ISpellCaster * caster, const CStack *obj) const;
  239. private:
  240. void setIsOffensive(const bool val);
  241. void setIsRising(const bool val);
  242. //call this after load or deserialization. cant be done in constructor.
  243. void setup();
  244. void setupMechanics();
  245. private:
  246. si32 defaultProbability;
  247. bool isRising;
  248. bool isDamage;
  249. bool isOffensive;
  250. bool isSpecial;
  251. std::string attributes; //reference only attributes //todo: remove or include in configuration format, currently unused
  252. ETargetType targetType;
  253. std::vector<Bonus::BonusType> immunities; //any of these grants immunity
  254. std::vector<Bonus::BonusType> absoluteImmunities; //any of these grants immunity, can't be negated
  255. std::vector<Bonus::BonusType> limiters; //all of them are required to be affected
  256. std::vector<Bonus::BonusType> absoluteLimiters; //all of them are required to be affected, can't be negated
  257. ///graphics related stuff
  258. std::string iconImmune;
  259. std::string iconBook;
  260. std::string iconEffect;
  261. std::string iconScenarioBonus;
  262. std::string iconScroll;
  263. ///sound related stuff
  264. std::string castSound;
  265. std::vector<LevelInfo> levels;
  266. ISpellMechanics * mechanics;//(!) do not serialize
  267. };
  268. bool DLL_LINKAGE isInScreenRange(const int3 &center, const int3 &pos); //for spells like Dimension Door
  269. class DLL_LINKAGE CSpellHandler: public CHandlerBase<SpellID, CSpell>
  270. {
  271. public:
  272. CSpellHandler();
  273. virtual ~CSpellHandler();
  274. ///IHandler base
  275. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  276. void afterLoadFinalization() override;
  277. void beforeValidate(JsonNode & object) override;
  278. /**
  279. * Gets a list of default allowed spells. OH3 spells are all allowed by default.
  280. *
  281. * @return a list of allowed spells, the index is the spell id and the value either 0 for not allowed or 1 for allowed
  282. */
  283. std::vector<bool> getDefaultAllowed() const override;
  284. const std::string getTypeName() const override;
  285. template <typename Handler> void serialize(Handler &h, const int version)
  286. {
  287. h & objects ;
  288. }
  289. protected:
  290. CSpell * loadFromJson(const JsonNode & json) override;
  291. };