CSpellHandler.h 8.8 KB

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