CSpellHandler.h 11 KB

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