CSpellHandler.h 9.3 KB

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