CSpellHandler.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 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. LevelInfo();
  131. ~LevelInfo();
  132. template <typename Handler> void serialize(Handler &h, const int version)
  133. {
  134. h & description & cost & power & AIValue & smartTarget & range & effects;
  135. h & clearTarget & clearAffected;
  136. }
  137. };
  138. /** \brief Low level accessor. Don`t use it if absolutely necessary
  139. *
  140. * \param level. spell school level
  141. * \return Spell level info structure
  142. *
  143. */
  144. const CSpell::LevelInfo& getLevelInfo(const int level) const;
  145. public:
  146. enum ETargetType {NO_TARGET, CREATURE, OBSTACLE, LOCATION};
  147. enum ESpellPositiveness {NEGATIVE = -1, NEUTRAL = 0, POSITIVE = 1};
  148. struct TargetInfo
  149. {
  150. ETargetType type;
  151. bool smart;
  152. bool massive;
  153. bool onlyAlive;
  154. ///no immunity on primary target (mostly spell-like attack)
  155. bool alwaysHitDirectly;
  156. bool clearTarget;
  157. bool clearAffected;
  158. TargetInfo(const CSpell * spell, const int level);
  159. TargetInfo(const CSpell * spell, const int level, ECastingMode::ECastingMode mode);
  160. private:
  161. void init(const CSpell * spell, const int level);
  162. };
  163. SpellID id;
  164. std::string identifier; //???
  165. std::string name;
  166. si32 level;
  167. bool earth; //deprecated
  168. bool water; //deprecated
  169. bool fire; //deprecated
  170. bool air; //deprecated
  171. std::map<ESpellSchool, bool> school; //todo: use this instead of separate boolean fields
  172. si32 power; //spell's power
  173. std::map<TFaction, si32> probabilities; //% chance to gain for castles
  174. bool combatSpell; //is this spell combat (true) or adventure (false)
  175. bool creatureAbility; //if true, only creatures can use this spell
  176. si8 positiveness; //1 if spell is positive for influenced stacks, 0 if it is indifferent, -1 if it's negative
  177. 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)
  178. CSpell();
  179. ~CSpell();
  180. bool isCastableBy(const IBonusBearer * caster, bool hasSpellBook, const std::set<SpellID> & spellBook) const;
  181. 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)
  182. ETargetType getTargetType() const; //deprecated
  183. CSpell::TargetInfo getTargetInfo(const int level) const;
  184. bool isCombatSpell() const;
  185. bool isAdventureSpell() const;
  186. bool isCreatureAbility() const;
  187. bool isPositive() const;
  188. bool isNegative() const;
  189. bool isNeutral() const;
  190. bool isDamageSpell() const;
  191. bool isHealingSpell() const;
  192. bool isRisingSpell() const;
  193. bool isOffensiveSpell() const;
  194. bool isSpecialSpell() const;
  195. bool hasEffects() const;
  196. void getEffects(std::vector<Bonus> &lst, const int level) const;
  197. ///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.
  198. ESpellCastProblem::ESpellCastProblem isImmuneAt(const CBattleInfoCallback * cb, const CGHeroInstance * caster, ECastingMode::ECastingMode mode, BattleHex destination) const;
  199. //internal, for use only by Mechanics classes
  200. ESpellCastProblem::ESpellCastProblem isImmuneBy(const IBonusBearer *obj) const;
  201. //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.
  202. ESpellCastProblem::ESpellCastProblem isImmuneByStack(const CGHeroInstance * caster, const CStack * obj) const;
  203. //internal, for use only by Mechanics classes. applying secondary skills
  204. ui32 calculateBonus(ui32 baseDamage, const CGHeroInstance * caster, const CStack * affectedCreature) const;
  205. ///calculate spell damage on stack taking caster`s secondary skills and affectedCreature`s bonuses into account
  206. ui32 calculateDamage(const CGHeroInstance * caster, const CStack * affectedCreature, int spellSchoolLevel, int usedSpellPower) const;
  207. ///calculate healed HP for all spells casted by hero
  208. ui32 calculateHealedHP(const CGHeroInstance * caster, const CStack * stack, const CStack * sacrificedStack = nullptr) const;
  209. ///selects from allStacks actually affected stacks
  210. std::set<const CStack *> getAffectedStacks(const CBattleInfoCallback * cb, ECastingMode::ECastingMode mode, PlayerColor casterColor, int spellLvl, BattleHex destination, const CGHeroInstance * caster = nullptr) const;
  211. si32 getCost(const int skillLevel) const;
  212. /**
  213. * Returns spell level power, base power ignored
  214. */
  215. si32 getPower(const int skillLevel) const;
  216. // /**
  217. // * Returns spell power, taking base power into account
  218. // */
  219. // si32 calculatePower(const int skillLevel) const;
  220. si32 getProbability(const TFaction factionId) const;
  221. /**
  222. * Calls cb for each school this spell belongs to
  223. *
  224. * Set stop to true to abort looping
  225. */
  226. void forEachSchool(const std::function<void (const SpellSchoolInfo &, bool &)> & cb) const;
  227. /**
  228. * Returns resource name of icon for SPELL_IMMUNITY bonus
  229. */
  230. const std::string& getIconImmune() const;
  231. const std::string& getCastSound() const;
  232. template <typename Handler> void serialize(Handler &h, const int version)
  233. {
  234. h & identifier & id & name & level & power
  235. & probabilities & attributes & combatSpell & creatureAbility & positiveness & counteredSpells;
  236. h & isRising & isDamage & isOffensive;
  237. h & targetType;
  238. h & immunities & limiters & absoluteImmunities & absoluteLimiters;
  239. h & iconImmune;
  240. h & defaultProbability;
  241. h & isSpecial;
  242. h & castSound & iconBook & iconEffect & iconScenarioBonus & iconScroll;
  243. h & levels;
  244. h & school;
  245. h & animationInfo;
  246. if(!h.saving)
  247. setup();
  248. }
  249. friend class CSpellHandler;
  250. friend class Graphics;
  251. public:
  252. ///Server logic. Has write access to GameState via packets.
  253. ///May be executed on client side by (future) non-cheat-proof scripts.
  254. bool adventureCast(const SpellCastEnvironment * env, AdventureSpellCastParameters & parameters) const;
  255. void battleCast(const SpellCastEnvironment * env, BattleSpellCastParameters & parameters) const;
  256. public:
  257. ///Client-server logic. Has direct write access to GameState.
  258. ///Shall be called (only) when applying packets on BOTH SIDES
  259. ///implementation of BattleSpellCast applying
  260. void afterCast(BattleInfo * battle, const BattleSpellCast * packet) const;
  261. private:
  262. void setIsOffensive(const bool val);
  263. void setIsRising(const bool val);
  264. //call this after load or deserialization. cant be done in constructor.
  265. void setup();
  266. void setupMechanics();
  267. private:
  268. si32 defaultProbability;
  269. bool isRising;
  270. bool isDamage;
  271. bool isOffensive;
  272. bool isSpecial;
  273. std::string attributes; //reference only attributes //todo: remove or include in configuration format, currently unused
  274. ETargetType targetType;
  275. std::vector<Bonus::BonusType> immunities; //any of these grants immunity
  276. std::vector<Bonus::BonusType> absoluteImmunities; //any of these grants immunity, can't be negated
  277. std::vector<Bonus::BonusType> limiters; //all of them are required to be affected
  278. std::vector<Bonus::BonusType> absoluteLimiters; //all of them are required to be affected, can't be negated
  279. ///graphics related stuff
  280. std::string iconImmune;
  281. std::string iconBook;
  282. std::string iconEffect;
  283. std::string iconScenarioBonus;
  284. std::string iconScroll;
  285. ///sound related stuff
  286. std::string castSound;
  287. std::vector<LevelInfo> levels;
  288. ISpellMechanics * mechanics;//(!) do not serialize
  289. };
  290. bool DLL_LINKAGE isInScreenRange(const int3 &center, const int3 &pos); //for spells like Dimension Door
  291. class DLL_LINKAGE CSpellHandler: public CHandlerBase<SpellID, CSpell>
  292. {
  293. public:
  294. CSpellHandler();
  295. virtual ~CSpellHandler();
  296. ///IHandler base
  297. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  298. void afterLoadFinalization() override;
  299. void beforeValidate(JsonNode & object) override;
  300. /**
  301. * Gets a list of default allowed spells. OH3 spells are all allowed by default.
  302. *
  303. * @return a list of allowed spells, the index is the spell id and the value either 0 for not allowed or 1 for allowed
  304. */
  305. std::vector<bool> getDefaultAllowed() const override;
  306. const std::string getTypeName() const override;
  307. template <typename Handler> void serialize(Handler &h, const int version)
  308. {
  309. h & objects ;
  310. }
  311. protected:
  312. CSpell * loadFromJson(const JsonNode & json) override;
  313. };