CSpellHandler.h 9.7 KB

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