CSpellHandler.h 12 KB

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