CSpellHandler.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #pragma once
  2. #include "IHandlerBase.h"
  3. #include "../lib/ConstTransitivePtr.h"
  4. #include "int3.h"
  5. #include "GameConstants.h"
  6. #include "HeroBonus.h"
  7. /*
  8. * CSpellHandler.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CLegacyConfigParser;
  17. struct BattleHex;
  18. class DLL_LINKAGE CSpell
  19. {
  20. public:
  21. // struct LevelInfo
  22. // {
  23. //
  24. // };
  25. //
  26. // /** \brief Low level accessor. Don`t use it if absolutely necessary
  27. // *
  28. // * \param level. spell school level
  29. // * \return Spell level info structure
  30. // *
  31. // */
  32. // const LevelInfo& getLevelInfo(const int level);
  33. public:
  34. enum ETargetType {NO_TARGET, CREATURE, CREATURE_EXPERT_MASSIVE, OBSTACLE};
  35. enum ESpellPositiveness {NEGATIVE = -1, NEUTRAL = 0, POSITIVE = 1};
  36. SpellID id;
  37. std::string identifier; //???
  38. std::string name;
  39. std::string abbName; //abbreviated name
  40. std::vector<std::string> descriptions; //descriptions of spell for skill levels: 0 - none, 1 - basic, etc
  41. si32 level;
  42. bool earth;
  43. bool water;
  44. bool fire;
  45. bool air;
  46. si32 power; //spell's power
  47. std::map<TFaction, si32> probabilities; //% chance to gain for castles
  48. bool combatSpell; //is this spell combat (true) or adventure (false)
  49. bool creatureAbility; //if true, only creatures can use this spell
  50. si8 positiveness; //1 if spell is positive for influenced stacks, 0 if it is indifferent, -1 if it's negative
  51. std::vector<std::string> range; //description of spell's range in SRSL by magic school level
  52. 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)
  53. CSpell();
  54. ~CSpell();
  55. 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)
  56. si16 mainEffectAnim; //main spell effect animation, in AC format (or -1 when none)
  57. ETargetType getTargetType() const;
  58. inline bool isCombatSpell() const;
  59. inline bool isAdventureSpell() const;
  60. inline bool isCreatureAbility() const;
  61. inline bool isPositive() const;
  62. inline bool isNegative() const;
  63. inline bool isRisingSpell() const;
  64. inline bool isDamageSpell() const;
  65. inline bool isOffensiveSpell() const;
  66. inline bool isSpecialSpell() const;
  67. inline bool hasEffects() const;
  68. void getEffects(std::vector<Bonus> &lst, const int level) const;
  69. bool isImmuneBy(const IBonusBearer *obj) const;
  70. inline si32 getCost(const int skillLevel) const;
  71. /**
  72. * Returns spell level power, base power ignored
  73. */
  74. inline si32 getPower(const int skillLevel) const;
  75. // /**
  76. // * Returns spell power, taking base power into account
  77. // */
  78. // inline si32 calculatePower(const int skillLevel) const;
  79. inline si32 getProbability(const TFaction factionId) const;
  80. /**
  81. * Returns resource name of icon for SPELL_IMMUNITY bonus
  82. */
  83. inline const std::string& getIconImmune() const;
  84. template <typename Handler> void serialize(Handler &h, const int version)
  85. {
  86. h & identifier & id & name & abbName & descriptions & level & earth & water & fire & air & power & costs
  87. & powers & probabilities & AIVals & attributes & combatSpell & creatureAbility & positiveness & range & counteredSpells & mainEffectAnim;
  88. h & isRising & isDamage & isOffensive;
  89. h & targetType;
  90. h & effects & immunities & limiters;
  91. h & iconImmune;
  92. h & absoluteImmunities & defaultProbability;
  93. h & isSpecial;
  94. }
  95. friend class CSpellHandler;
  96. private:
  97. void setIsOffensive(const bool val);
  98. void setIsRising(const bool val);
  99. void setAttributes(const std::string& newValue);
  100. private:
  101. si32 defaultProbability;
  102. std::vector<si32> costs; //per skill level: 0 - none, 1 - basic, etc
  103. std::vector<si32> powers; //per skill level: 0 - none, 1 - basic, etc
  104. std::vector<si32> AIVals; //AI values: per skill level: 0 - none, 1 - basic, etc
  105. bool isRising;
  106. bool isDamage;
  107. bool isOffensive;
  108. bool isSpecial;
  109. std::string attributes; //reference only attributes //todo: remove or include in configuration format, currently unused
  110. ETargetType targetType;
  111. std::vector<std::vector<Bonus *> > effects; // [level 0-3][list of effects]
  112. std::vector<Bonus::BonusType> immunities; //any of these grants immunity
  113. std::vector<Bonus::BonusType> absoluteImmunities; //any of these grants immunity, cant be negated
  114. std::vector<Bonus::BonusType> limiters; //all of them are required to be affected
  115. ///graphics related stuff
  116. std::string iconImmune;
  117. };
  118. ///CSpell inlines
  119. bool CSpell::isCombatSpell() const
  120. {
  121. return combatSpell;
  122. }
  123. bool CSpell::isAdventureSpell() const
  124. {
  125. return !combatSpell;
  126. }
  127. bool CSpell::isCreatureAbility() const
  128. {
  129. return creatureAbility;
  130. }
  131. bool CSpell::isPositive() const
  132. {
  133. return positiveness == POSITIVE;
  134. }
  135. bool CSpell::isNegative() const
  136. {
  137. return positiveness == NEGATIVE;
  138. }
  139. bool CSpell::isRisingSpell() const
  140. {
  141. return isRising;
  142. }
  143. bool CSpell::isDamageSpell() const
  144. {
  145. return isDamage;
  146. }
  147. bool CSpell::isOffensiveSpell() const
  148. {
  149. return isOffensive;
  150. }
  151. bool CSpell::isSpecialSpell() const
  152. {
  153. return isSpecial;
  154. }
  155. bool CSpell::hasEffects() const
  156. {
  157. return effects.size() && effects[0].size();
  158. }
  159. const std::string& CSpell::getIconImmune() const
  160. {
  161. return iconImmune;
  162. }
  163. si32 CSpell::getCost(const int skillLevel) const
  164. {
  165. return costs[skillLevel];
  166. }
  167. si32 CSpell::getPower(const int skillLevel) const
  168. {
  169. return powers[skillLevel];
  170. }
  171. //si32 CSpell::calculatePower(const int skillLevel) const
  172. //{
  173. // return power + getPower(skillLevel);
  174. //}
  175. si32 CSpell::getProbability(const TFaction factionId) const
  176. {
  177. if (! vstd::contains(probabilities,factionId))
  178. {
  179. return defaultProbability;
  180. }
  181. return probabilities.at(factionId);
  182. }
  183. bool DLL_LINKAGE isInScreenRange(const int3 &center, const int3 &pos); //for spells like Dimension Door
  184. class DLL_LINKAGE CSpellHandler: public CHandlerBase<SpellID, CSpell>
  185. {
  186. //CSpell * loadSpell(CLegacyConfigParser & parser, const SpellID id);
  187. public:
  188. CSpellHandler();
  189. virtual ~CSpellHandler();
  190. ///IHandler base
  191. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  192. void afterLoadFinalization() override;
  193. /**
  194. * Gets a list of default allowed spells. OH3 spells are all allowed by default.
  195. *
  196. * @return a list of allowed spells, the index is the spell id and the value either 0 for not allowed or 1 for allowed
  197. */
  198. std::vector<bool> getDefaultAllowed() const override;
  199. const std::string getTypeName() const override;
  200. template <typename Handler> void serialize(Handler &h, const int version)
  201. {
  202. h & objects ;
  203. }
  204. protected:
  205. CSpell * loadFromJson(const JsonNode & json) override;
  206. };