CSpellHandler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * CSpellHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../IHandlerBase.h"
  12. #include "../ConstTransitivePtr.h"
  13. #include "../int3.h"
  14. #include "../GameConstants.h"
  15. #include "../BattleHex.h"
  16. #include "../HeroBonus.h"
  17. class CGObjectInstance;
  18. class CSpell;
  19. class ISpellMechanics;
  20. class CLegacyConfigParser;
  21. class CGHeroInstance;
  22. class CStack;
  23. class CBattleInfoCallback;
  24. class BattleInfo;
  25. struct CPackForClient;
  26. struct BattleSpellCast;
  27. class CGameInfoCallback;
  28. class CRandomGenerator;
  29. class CMap;
  30. struct SpellSchoolInfo
  31. {
  32. ESpellSchool id; //backlink
  33. Bonus::BonusType damagePremyBonus;
  34. Bonus::BonusType immunityBonus;
  35. std::string jsonName;
  36. SecondarySkill::ESecondarySkill skill;
  37. Bonus::BonusType knoledgeBonus;
  38. };
  39. ///callback to be provided by server
  40. class DLL_LINKAGE SpellCastEnvironment
  41. {
  42. public:
  43. virtual ~SpellCastEnvironment(){};
  44. virtual void sendAndApply(CPackForClient * info) const = 0;
  45. virtual CRandomGenerator & getRandomGenerator() const = 0;
  46. virtual void complain(const std::string & problem) const = 0;
  47. virtual const CMap * getMap() const = 0;
  48. virtual const CGameInfoCallback * getCb() const = 0;
  49. virtual bool moveHero(ObjectInstanceID hid, int3 dst, ui8 teleporting, PlayerColor asker = PlayerColor::NEUTRAL) const =0; //TODO: remove
  50. };
  51. ///helper struct
  52. struct DLL_LINKAGE BattleSpellCastParameters
  53. {
  54. public:
  55. BattleSpellCastParameters(const BattleInfo * cb);
  56. int spellLvl;
  57. BattleHex destination;
  58. ui8 casterSide;
  59. PlayerColor casterColor;
  60. const CGHeroInstance * caster;
  61. const CGHeroInstance * secHero;
  62. int usedSpellPower;
  63. ECastingMode::ECastingMode mode;
  64. const CStack * casterStack;
  65. const CStack * selectedStack;
  66. const BattleInfo * cb;
  67. };
  68. struct DLL_LINKAGE AdventureSpellCastParameters
  69. {
  70. const CGHeroInstance * caster;
  71. int3 pos;
  72. };
  73. enum class VerticalPosition : ui8{TOP, CENTER, BOTTOM};
  74. class DLL_LINKAGE CSpell
  75. {
  76. public:
  77. struct ProjectileInfo
  78. {
  79. ///in radians. Only positive value. Negative angle is handled by vertical flip
  80. double minimumAngle;
  81. ///resource name
  82. std::string resourceName;
  83. template <typename Handler> void serialize(Handler & h, const int version)
  84. {
  85. h & minimumAngle & resourceName;
  86. }
  87. };
  88. struct AnimationItem
  89. {
  90. std::string resourceName;
  91. VerticalPosition verticalPosition;
  92. template <typename Handler> void serialize(Handler & h, const int version)
  93. {
  94. h & resourceName & verticalPosition;
  95. }
  96. };
  97. typedef AnimationItem TAnimation;
  98. typedef std::vector<TAnimation> TAnimationQueue;
  99. struct DLL_LINKAGE AnimationInfo
  100. {
  101. AnimationInfo();
  102. ~AnimationInfo();
  103. ///displayed on all affected targets.
  104. TAnimationQueue affect;
  105. ///displayed on caster.
  106. TAnimationQueue cast;
  107. ///displayed on target hex. If spell was casted with no target selection displayed on entire battlefield (f.e. ARMAGEDDON)
  108. TAnimationQueue hit;
  109. ///displayed "between" caster and (first) target. Ignored if spell was casted with no target selection.
  110. ///use selectProjectile to access
  111. std::vector<ProjectileInfo> projectile;
  112. template <typename Handler> void serialize(Handler & h, const int version)
  113. {
  114. h & projectile & hit & cast;
  115. }
  116. std::string selectProjectile(const double angle) const;
  117. } animationInfo;
  118. public:
  119. struct LevelInfo
  120. {
  121. std::string description; //descriptions of spell for skill level
  122. si32 cost;
  123. si32 power;
  124. si32 AIValue;
  125. bool smartTarget;
  126. bool clearTarget;
  127. bool clearAffected;
  128. std::string range;
  129. std::vector<Bonus> effects;
  130. std::vector<Bonus *> effectsTmp; //TODO: this should replace effects
  131. LevelInfo();
  132. ~LevelInfo();
  133. template <typename Handler> void serialize(Handler &h, const int version)
  134. {
  135. h & description & cost & power & AIValue & smartTarget & range & effects;
  136. h & clearTarget & clearAffected;
  137. }
  138. };
  139. /** \brief Low level accessor. Don`t use it if absolutely necessary
  140. *
  141. * \param level. spell school level
  142. * \return Spell level info structure
  143. *
  144. */
  145. const CSpell::LevelInfo& getLevelInfo(const int level) const;
  146. public:
  147. enum ETargetType {NO_TARGET, CREATURE, OBSTACLE, LOCATION};
  148. enum ESpellPositiveness {NEGATIVE = -1, NEUTRAL = 0, POSITIVE = 1};
  149. struct TargetInfo
  150. {
  151. ETargetType type;
  152. bool smart;
  153. bool massive;
  154. bool onlyAlive;
  155. ///no immunity on primary target (mostly spell-like attack)
  156. bool alwaysHitDirectly;
  157. bool clearTarget;
  158. bool clearAffected;
  159. TargetInfo(const CSpell * spell, const int level);
  160. TargetInfo(const CSpell * spell, const int level, ECastingMode::ECastingMode mode);
  161. private:
  162. void init(const CSpell * spell, const int level);
  163. };
  164. SpellID id;
  165. std::string identifier; //???
  166. std::string name;
  167. si32 level;
  168. std::map<ESpellSchool, bool> school; //todo: use this instead of separate boolean fields
  169. si32 power; //spell's power
  170. std::map<TFaction, si32> probabilities; //% chance to gain for castles
  171. bool combatSpell; //is this spell combat (true) or adventure (false)
  172. bool creatureAbility; //if true, only creatures can use this spell
  173. si8 positiveness; //1 if spell is positive for influenced stacks, 0 if it is indifferent, -1 if it's negative
  174. 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)
  175. CSpell();
  176. ~CSpell();
  177. bool isCastableBy(const IBonusBearer * caster, bool hasSpellBook, const std::set<SpellID> & spellBook) const;
  178. 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)
  179. ETargetType getTargetType() const; //deprecated
  180. CSpell::TargetInfo getTargetInfo(const int level) const;
  181. bool isCombatSpell() const;
  182. bool isAdventureSpell() const;
  183. bool isCreatureAbility() const;
  184. bool isPositive() const;
  185. bool isNegative() const;
  186. bool isNeutral() const;
  187. bool isDamageSpell() const;
  188. bool isHealingSpell() const;
  189. bool isRisingSpell() const;
  190. bool isOffensiveSpell() const;
  191. bool isSpecialSpell() const;
  192. bool hasEffects() const;
  193. void getEffects(std::vector<Bonus> &lst, const int level) const;
  194. ///checks for creature immunity / anything that prevent casting *at given hex* - doesn't take into account general problems such as not having spellbook or mana points etc.
  195. ESpellCastProblem::ESpellCastProblem isImmuneAt(const CBattleInfoCallback * cb, const CGHeroInstance * caster, ECastingMode::ECastingMode mode, BattleHex destination) const;
  196. //internal, for use only by Mechanics classes
  197. ESpellCastProblem::ESpellCastProblem isImmuneBy(const IBonusBearer *obj) const;
  198. //internal, for use only by Mechanics classes. applying secondary skills
  199. ui32 calculateBonus(ui32 baseDamage, const CGHeroInstance * caster, const CStack * affectedCreature) const;
  200. ///calculate spell damage on stack taking caster`s secondary skills and affectedCreature`s bonuses into account
  201. ui32 calculateDamage(const CGHeroInstance * caster, const CStack * affectedCreature, int spellSchoolLevel, int usedSpellPower) const;
  202. ///selects from allStacks actually affected stacks
  203. std::set<const CStack *> getAffectedStacks(const CBattleInfoCallback * cb, ECastingMode::ECastingMode mode, PlayerColor casterColor, int spellLvl, BattleHex destination, const CGHeroInstance * caster = nullptr) const;
  204. si32 getCost(const int skillLevel) const;
  205. /**
  206. * Returns spell level power, base power ignored
  207. */
  208. si32 getPower(const int skillLevel) const;
  209. si32 getProbability(const TFaction factionId) const;
  210. /**
  211. * Calls cb for each school this spell belongs to
  212. *
  213. * Set stop to true to abort looping
  214. */
  215. void forEachSchool(const std::function<void (const SpellSchoolInfo &, bool &)> & cb) const;
  216. /**
  217. * Returns resource name of icon for SPELL_IMMUNITY bonus
  218. */
  219. const std::string& getIconImmune() const;
  220. const std::string& getCastSound() const;
  221. template <typename Handler> void serialize(Handler &h, const int version)
  222. {
  223. h & identifier & id & name & level & power
  224. & probabilities & attributes & combatSpell & creatureAbility & positiveness & counteredSpells;
  225. h & isRising & isDamage & isOffensive;
  226. h & targetType;
  227. h & immunities & limiters & absoluteImmunities & absoluteLimiters;
  228. h & iconImmune;
  229. h & defaultProbability;
  230. h & isSpecial;
  231. h & castSound & iconBook & iconEffect & iconScenarioBonus & iconScroll;
  232. h & levels;
  233. h & school;
  234. h & animationInfo;
  235. if(!h.saving)
  236. setup();
  237. }
  238. friend class CSpellHandler;
  239. friend class Graphics;
  240. public:
  241. ///internal interface (for callbacks)
  242. ///Checks general but spell-specific problems for all casting modes. Use only during battle.
  243. ESpellCastProblem::ESpellCastProblem canBeCasted(const CBattleInfoCallback * cb, PlayerColor player) const;
  244. ///checks for creature immunity / anything that prevent casting *at given target* - doesn't take into account general problems such as not having spellbook or mana points etc.
  245. ESpellCastProblem::ESpellCastProblem isImmuneByStack(const CGHeroInstance * caster, const CStack * obj) const;
  246. public:
  247. ///Server logic. Has write access to GameState via packets.
  248. ///May be executed on client side by (future) non-cheat-proof scripts.
  249. bool adventureCast(const SpellCastEnvironment * env, AdventureSpellCastParameters & parameters) const;
  250. void battleCast(const SpellCastEnvironment * env, BattleSpellCastParameters & parameters) const;
  251. public:
  252. ///Client-server logic. Has direct write access to GameState.
  253. ///Shall be called (only) when applying packets on BOTH SIDES
  254. ///implementation of BattleSpellCast applying
  255. void applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const;
  256. private:
  257. void setIsOffensive(const bool val);
  258. void setIsRising(const bool val);
  259. //call this after load or deserialization. cant be done in constructor.
  260. void setup();
  261. void setupMechanics();
  262. private:
  263. si32 defaultProbability;
  264. bool isRising;
  265. bool isDamage;
  266. bool isOffensive;
  267. bool isSpecial;
  268. std::string attributes; //reference only attributes //todo: remove or include in configuration format, currently unused
  269. ETargetType targetType;
  270. std::vector<Bonus::BonusType> immunities; //any of these grants immunity
  271. std::vector<Bonus::BonusType> absoluteImmunities; //any of these grants immunity, can't be negated
  272. std::vector<Bonus::BonusType> limiters; //all of them are required to be affected
  273. std::vector<Bonus::BonusType> absoluteLimiters; //all of them are required to be affected, can't be negated
  274. ///graphics related stuff
  275. std::string iconImmune;
  276. std::string iconBook;
  277. std::string iconEffect;
  278. std::string iconScenarioBonus;
  279. std::string iconScroll;
  280. ///sound related stuff
  281. std::string castSound;
  282. std::vector<LevelInfo> levels;
  283. ISpellMechanics * mechanics;//(!) do not serialize
  284. };
  285. bool DLL_LINKAGE isInScreenRange(const int3 &center, const int3 &pos); //for spells like Dimension Door
  286. class DLL_LINKAGE CSpellHandler: public CHandlerBase<SpellID, CSpell>
  287. {
  288. public:
  289. CSpellHandler();
  290. virtual ~CSpellHandler();
  291. ///IHandler base
  292. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  293. void afterLoadFinalization() override;
  294. void beforeValidate(JsonNode & object) override;
  295. /**
  296. * Gets a list of default allowed spells. OH3 spells are all allowed by default.
  297. *
  298. * @return a list of allowed spells, the index is the spell id and the value either 0 for not allowed or 1 for allowed
  299. */
  300. std::vector<bool> getDefaultAllowed() const override;
  301. const std::string getTypeName() const override;
  302. template <typename Handler> void serialize(Handler &h, const int version)
  303. {
  304. h & objects ;
  305. }
  306. protected:
  307. CSpell * loadFromJson(const JsonNode & json) override;
  308. };