CSpellHandler.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #pragma once
  2. #include "IHandlerBase.h"
  3. #include "../lib/ConstTransitivePtr.h"
  4. #include "int3.h"
  5. #include "GameConstants.h"
  6. #include "HeroBonus.h"
  7. /*
  8. * CSpellHandler.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CLegacyConfigParser;
  17. struct BattleHex;
  18. class CSpell;
  19. class CGHeroInstance;
  20. class CStack;
  21. class DLL_LINKAGE CSpellMechanics
  22. {
  23. public:
  24. CSpellMechanics(CSpell * s);
  25. virtual ~CSpellMechanics();
  26. virtual ESpellCastProblem::ESpellCastProblem isImmuneByStack(const CGHeroInstance * caster, ECastingMode::ECastingMode mode, const CStack * obj);
  27. protected:
  28. CSpell * owner;
  29. };
  30. class DLL_LINKAGE CSpell
  31. {
  32. public:
  33. struct LevelInfo
  34. {
  35. std::string description; //descriptions of spell for skill level
  36. si32 cost; //per skill level: 0 - none, 1 - basic, etc
  37. si32 power; //per skill level: 0 - none, 1 - basic, etc
  38. si32 AIValue; //AI values: per skill level: 0 - none, 1 - basic, etc
  39. bool smartTarget;
  40. std::string range;
  41. std::vector<Bonus> effects;
  42. LevelInfo();
  43. ~LevelInfo();
  44. template <typename Handler> void serialize(Handler &h, const int version)
  45. {
  46. h & description & cost & power & AIValue & smartTarget & range & effects;
  47. }
  48. };
  49. /** \brief Low level accessor. Don`t use it if absolutely necessary
  50. *
  51. * \param level. spell school level
  52. * \return Spell level info structure
  53. *
  54. */
  55. const CSpell::LevelInfo& getLevelInfo(const int level) const;
  56. public:
  57. enum ETargetType {NO_TARGET, CREATURE, OBSTACLE};
  58. enum ESpellPositiveness {NEGATIVE = -1, NEUTRAL = 0, POSITIVE = 1};
  59. struct TargetInfo
  60. {
  61. ETargetType type;
  62. bool smart;
  63. bool massive;
  64. bool onlyAlive;
  65. };
  66. SpellID id;
  67. std::string identifier; //???
  68. std::string name;
  69. si32 level;
  70. bool earth;
  71. bool water;
  72. bool fire;
  73. bool air;
  74. si32 power; //spell's power
  75. std::map<TFaction, si32> probabilities; //% chance to gain for castles
  76. bool combatSpell; //is this spell combat (true) or adventure (false)
  77. bool creatureAbility; //if true, only creatures can use this spell
  78. si8 positiveness; //1 if spell is positive for influenced stacks, 0 if it is indifferent, -1 if it's negative
  79. 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)
  80. CSpell();
  81. ~CSpell();
  82. 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)
  83. si16 mainEffectAnim; //main spell effect animation, in AC format (or -1 when none)
  84. ETargetType getTargetType() const; //deprecated
  85. const CSpell::TargetInfo getTargetInfo(const int level) const;
  86. bool isCombatSpell() const;
  87. bool isAdventureSpell() const;
  88. bool isCreatureAbility() const;
  89. bool isPositive() const;
  90. bool isNegative() const;
  91. bool isNeutral() const;
  92. bool isDamageSpell() const;
  93. bool isHealingSpell() const;
  94. bool isRisingSpell() const;
  95. bool isOffensiveSpell() const;
  96. bool isSpecialSpell() const;
  97. bool hasEffects() const;
  98. void getEffects(std::vector<Bonus> &lst, const int level) const;
  99. //internal, for use only by Mechanics classes
  100. ESpellCastProblem::ESpellCastProblem isImmuneBy(const IBonusBearer *obj) const;
  101. //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.
  102. ESpellCastProblem::ESpellCastProblem isImmuneByStack(const CGHeroInstance * caster, ECastingMode::ECastingMode mode, const CStack * obj) const;
  103. //applying secondary skills
  104. ui32 calculateBonus(ui32 baseDamage, const CGHeroInstance * caster, const CStack * affectedCreature) const;
  105. ///calculate healed HP for all spells casted by hero
  106. ui32 calculateHealedHP(const CGHeroInstance * caster, const CStack * stack, const CStack * sacrificedStack = nullptr) const;
  107. si32 getCost(const int skillLevel) const;
  108. /**
  109. * Returns spell level power, base power ignored
  110. */
  111. si32 getPower(const int skillLevel) const;
  112. // /**
  113. // * Returns spell power, taking base power into account
  114. // */
  115. // si32 calculatePower(const int skillLevel) const;
  116. si32 getProbability(const TFaction factionId) const;
  117. /**
  118. * Returns resource name of icon for SPELL_IMMUNITY bonus
  119. */
  120. const std::string& getIconImmune() const;
  121. const std::string& getCastSound() const;
  122. template <typename Handler> void serialize(Handler &h, const int version)
  123. {
  124. h & identifier & id & name & level & earth & water & fire & air & power
  125. & probabilities & attributes & combatSpell & creatureAbility & positiveness & counteredSpells & mainEffectAnim;
  126. h & isRising & isDamage & isOffensive;
  127. h & targetType;
  128. h & immunities & limiters & absoluteImmunities & absoluteLimiters;
  129. h & iconImmune;
  130. h & defaultProbability;
  131. h & isSpecial;
  132. h & castSound & iconBook & iconEffect & iconScenarioBonus & iconScroll;
  133. h & levels;
  134. if(!h.saving)
  135. setupMechanics();
  136. }
  137. friend class CSpellHandler;
  138. friend class Graphics;
  139. private:
  140. void setIsOffensive(const bool val);
  141. void setIsRising(const bool val);
  142. //call this after load or deserialization. cant be done in constructor.
  143. void setupMechanics();
  144. private:
  145. si32 defaultProbability;
  146. bool isRising;
  147. bool isDamage;
  148. bool isOffensive;
  149. bool isSpecial;
  150. std::string attributes; //reference only attributes //todo: remove or include in configuration format, currently unused
  151. ETargetType targetType;
  152. std::vector<Bonus::BonusType> immunities; //any of these grants immunity
  153. std::vector<Bonus::BonusType> absoluteImmunities; //any of these grants immunity, can't be negated
  154. std::vector<Bonus::BonusType> limiters; //all of them are required to be affected
  155. std::vector<Bonus::BonusType> absoluteLimiters; //all of them are required to be affected, can't be negated
  156. ///graphics related stuff
  157. std::string iconImmune;
  158. std::string iconBook;
  159. std::string iconEffect;
  160. std::string iconScenarioBonus;
  161. std::string iconScroll;
  162. ///sound related stuff
  163. std::string castSound;
  164. std::vector<LevelInfo> levels;
  165. CSpellMechanics * mechanics;//(!) do not serialize
  166. };
  167. bool DLL_LINKAGE isInScreenRange(const int3 &center, const int3 &pos); //for spells like Dimension Door
  168. class DLL_LINKAGE CSpellHandler: public CHandlerBase<SpellID, CSpell>
  169. {
  170. public:
  171. CSpellHandler();
  172. virtual ~CSpellHandler();
  173. ///IHandler base
  174. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  175. void afterLoadFinalization() override;
  176. void beforeValidate(JsonNode & object) override;
  177. /**
  178. * Gets a list of default allowed spells. OH3 spells are all allowed by default.
  179. *
  180. * @return a list of allowed spells, the index is the spell id and the value either 0 for not allowed or 1 for allowed
  181. */
  182. std::vector<bool> getDefaultAllowed() const override;
  183. const std::string getTypeName() const override;
  184. template <typename Handler> void serialize(Handler &h, const int version)
  185. {
  186. h & objects ;
  187. }
  188. protected:
  189. CSpell * loadFromJson(const JsonNode & json) override;
  190. };