CSpellHandler.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. struct CPackForClient;
  24. class CRandomGenerator;
  25. struct SpellSchoolInfo
  26. {
  27. ESpellSchool id; //backlink
  28. Bonus::BonusType damagePremyBonus;
  29. Bonus::BonusType immunityBonus;
  30. std::string jsonName;
  31. SecondarySkill::ESecondarySkill skill;
  32. Bonus::BonusType knoledgeBonus;
  33. };
  34. ///callback to be provided by server
  35. class DLL_LINKAGE SpellCastEnvironment
  36. {
  37. public:
  38. virtual void sendAndApply(CPackForClient * info) const = 0;
  39. virtual CRandomGenerator & getRandomGenerator() const = 0;
  40. };
  41. ///helper struct
  42. struct DLL_LINKAGE BattleSpellCastParameters
  43. {
  44. public:
  45. int spellLvl;
  46. BattleHex destination;
  47. ui8 casterSide;
  48. PlayerColor casterColor;
  49. const CGHeroInstance * caster;
  50. const CGHeroInstance * secHero;
  51. int usedSpellPower;
  52. ECastingMode::ECastingMode mode;
  53. const CStack * casterStack;
  54. const CStack * selectedStack;
  55. const CBattleInfoCallback * cb;
  56. };
  57. class DLL_LINKAGE CSpell
  58. {
  59. public:
  60. struct LevelInfo
  61. {
  62. std::string description; //descriptions of spell for skill level
  63. si32 cost;
  64. si32 power;
  65. si32 AIValue;
  66. bool smartTarget;
  67. bool clearTarget;
  68. bool clearAffected;
  69. std::string range;
  70. std::vector<Bonus> effects;
  71. LevelInfo();
  72. ~LevelInfo();
  73. template <typename Handler> void serialize(Handler &h, const int version)
  74. {
  75. h & description & cost & power & AIValue & smartTarget & range & effects;
  76. h & clearTarget & clearAffected;
  77. }
  78. };
  79. /** \brief Low level accessor. Don`t use it if absolutely necessary
  80. *
  81. * \param level. spell school level
  82. * \return Spell level info structure
  83. *
  84. */
  85. const CSpell::LevelInfo& getLevelInfo(const int level) const;
  86. public:
  87. enum ETargetType {NO_TARGET, CREATURE, OBSTACLE, LOCATION};
  88. enum ESpellPositiveness {NEGATIVE = -1, NEUTRAL = 0, POSITIVE = 1};
  89. struct TargetInfo
  90. {
  91. ETargetType type;
  92. bool smart;
  93. bool massive;
  94. bool onlyAlive;
  95. ///no immunity on primary target (mostly spell-like attack)
  96. bool alwaysHitDirectly;
  97. bool clearTarget;
  98. bool clearAffected;
  99. TargetInfo(const CSpell * spell, const int level);
  100. TargetInfo(const CSpell * spell, const int level, ECastingMode::ECastingMode mode);
  101. private:
  102. void init(const CSpell * spell, const int level);
  103. };
  104. SpellID id;
  105. std::string identifier; //???
  106. std::string name;
  107. si32 level;
  108. bool earth; //deprecated
  109. bool water; //deprecated
  110. bool fire; //deprecated
  111. bool air; //deprecated
  112. std::map<ESpellSchool, bool> school; //todo: use this instead of separate boolean fields
  113. si32 power; //spell's power
  114. std::map<TFaction, si32> probabilities; //% chance to gain for castles
  115. bool combatSpell; //is this spell combat (true) or adventure (false)
  116. bool creatureAbility; //if true, only creatures can use this spell
  117. si8 positiveness; //1 if spell is positive for influenced stacks, 0 if it is indifferent, -1 if it's negative
  118. 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)
  119. CSpell();
  120. ~CSpell();
  121. //void adventureCast() const;
  122. void battleCast(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters) const;
  123. bool isCastableBy(const IBonusBearer * caster, bool hasSpellBook, const std::set<SpellID> & spellBook) const;
  124. 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)
  125. si16 mainEffectAnim; //main spell effect animation, in AC format (or -1 when none)
  126. ETargetType getTargetType() const; //deprecated
  127. CSpell::TargetInfo getTargetInfo(const int level) const;
  128. bool isCombatSpell() const;
  129. bool isAdventureSpell() const;
  130. bool isCreatureAbility() const;
  131. bool isPositive() const;
  132. bool isNegative() const;
  133. bool isNeutral() const;
  134. bool isDamageSpell() const;
  135. bool isHealingSpell() const;
  136. bool isRisingSpell() const;
  137. bool isOffensiveSpell() const;
  138. bool isSpecialSpell() const;
  139. bool hasEffects() const;
  140. void getEffects(std::vector<Bonus> &lst, const int level) const;
  141. //internal, for use only by Mechanics classes
  142. ESpellCastProblem::ESpellCastProblem isImmuneBy(const IBonusBearer *obj) const;
  143. //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.
  144. ESpellCastProblem::ESpellCastProblem isImmuneByStack(const CGHeroInstance * caster, const CStack * obj) const;
  145. //internal, for use only by Mechanics classes. applying secondary skills
  146. ui32 calculateBonus(ui32 baseDamage, const CGHeroInstance * caster, const CStack * affectedCreature) const;
  147. ///calculate spell damage on stack taking caster`s secondary skills and affectedCreature`s bonuses into account
  148. ui32 calculateDamage(const CGHeroInstance * caster, const CStack * affectedCreature, int spellSchoolLevel, int usedSpellPower) const;
  149. ///calculate healed HP for all spells casted by hero
  150. ui32 calculateHealedHP(const CGHeroInstance * caster, const CStack * stack, const CStack * sacrificedStack = nullptr) const;
  151. ///selects from allStacks actually affected stacks
  152. std::set<const CStack *> getAffectedStacks(const CBattleInfoCallback * cb, ECastingMode::ECastingMode mode, PlayerColor casterColor, int spellLvl, BattleHex destination, const CGHeroInstance * caster = nullptr) const;
  153. si32 getCost(const int skillLevel) const;
  154. /**
  155. * Returns spell level power, base power ignored
  156. */
  157. si32 getPower(const int skillLevel) const;
  158. // /**
  159. // * Returns spell power, taking base power into account
  160. // */
  161. // si32 calculatePower(const int skillLevel) const;
  162. si32 getProbability(const TFaction factionId) const;
  163. /**
  164. * Calls cb for each school this spell belongs to
  165. *
  166. * Set stop to true to abort looping
  167. */
  168. void forEachSchool(const std::function<void (const SpellSchoolInfo &, bool &)> & cb) const;
  169. /**
  170. * Returns resource name of icon for SPELL_IMMUNITY bonus
  171. */
  172. const std::string& getIconImmune() const;
  173. const std::string& getCastSound() const;
  174. template <typename Handler> void serialize(Handler &h, const int version)
  175. {
  176. h & identifier & id & name & level & power
  177. & probabilities & attributes & combatSpell & creatureAbility & positiveness & counteredSpells & mainEffectAnim;
  178. h & isRising & isDamage & isOffensive;
  179. h & targetType;
  180. h & immunities & limiters & absoluteImmunities & absoluteLimiters;
  181. h & iconImmune;
  182. h & defaultProbability;
  183. h & isSpecial;
  184. h & castSound & iconBook & iconEffect & iconScenarioBonus & iconScroll;
  185. h & levels;
  186. h & school;
  187. if(!h.saving)
  188. setup();
  189. }
  190. friend class CSpellHandler;
  191. friend class Graphics;
  192. private:
  193. void setIsOffensive(const bool val);
  194. void setIsRising(const bool val);
  195. //call this after load or deserialization. cant be done in constructor.
  196. void setup();
  197. void setupMechanics();
  198. private:
  199. si32 defaultProbability;
  200. bool isRising;
  201. bool isDamage;
  202. bool isOffensive;
  203. bool isSpecial;
  204. std::string attributes; //reference only attributes //todo: remove or include in configuration format, currently unused
  205. ETargetType targetType;
  206. std::vector<Bonus::BonusType> immunities; //any of these grants immunity
  207. std::vector<Bonus::BonusType> absoluteImmunities; //any of these grants immunity, can't be negated
  208. std::vector<Bonus::BonusType> limiters; //all of them are required to be affected
  209. std::vector<Bonus::BonusType> absoluteLimiters; //all of them are required to be affected, can't be negated
  210. ///graphics related stuff
  211. std::string iconImmune;
  212. std::string iconBook;
  213. std::string iconEffect;
  214. std::string iconScenarioBonus;
  215. std::string iconScroll;
  216. ///sound related stuff
  217. std::string castSound;
  218. std::vector<LevelInfo> levels;
  219. ISpellMechanics * mechanics;//(!) do not serialize
  220. };
  221. bool DLL_LINKAGE isInScreenRange(const int3 &center, const int3 &pos); //for spells like Dimension Door
  222. class DLL_LINKAGE CSpellHandler: public CHandlerBase<SpellID, CSpell>
  223. {
  224. public:
  225. CSpellHandler();
  226. virtual ~CSpellHandler();
  227. ///IHandler base
  228. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  229. void afterLoadFinalization() override;
  230. void beforeValidate(JsonNode & object) override;
  231. /**
  232. * Gets a list of default allowed spells. OH3 spells are all allowed by default.
  233. *
  234. * @return a list of allowed spells, the index is the spell id and the value either 0 for not allowed or 1 for allowed
  235. */
  236. std::vector<bool> getDefaultAllowed() const override;
  237. const std::string getTypeName() const override;
  238. template <typename Handler> void serialize(Handler &h, const int version)
  239. {
  240. h & objects ;
  241. }
  242. protected:
  243. CSpell * loadFromJson(const JsonNode & json) override;
  244. };