CSpellHandler.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. std::string description; //descriptions of spell for skill level
  24. si32 cost; //per skill level: 0 - none, 1 - basic, etc
  25. si32 power; //per skill level: 0 - none, 1 - basic, etc
  26. si32 AIValue; //AI values: per skill level: 0 - none, 1 - basic, etc
  27. bool smartTarget;
  28. std::string range;
  29. std::vector<Bonus> effects;
  30. LevelInfo();
  31. ~LevelInfo();
  32. template <typename Handler> void serialize(Handler &h, const int version)
  33. {
  34. h & description & cost & power & AIValue & smartTarget & range & effects;
  35. }
  36. };
  37. /** \brief Low level accessor. Don`t use it if absolutely necessary
  38. *
  39. * \param level. spell school level
  40. * \return Spell level info structure
  41. *
  42. */
  43. const CSpell::LevelInfo& getLevelInfo(const int level) const;
  44. public:
  45. enum ETargetType {NO_TARGET, CREATURE, OBSTACLE};
  46. enum ESpellPositiveness {NEGATIVE = -1, NEUTRAL = 0, POSITIVE = 1};
  47. struct TargetInfo
  48. {
  49. ETargetType type;
  50. bool smart;
  51. bool massive;
  52. bool onlyAlive;
  53. };
  54. SpellID id;
  55. std::string identifier; //???
  56. std::string name;
  57. si32 level;
  58. bool earth;
  59. bool water;
  60. bool fire;
  61. bool air;
  62. si32 power; //spell's power
  63. std::map<TFaction, si32> probabilities; //% chance to gain for castles
  64. bool combatSpell; //is this spell combat (true) or adventure (false)
  65. bool creatureAbility; //if true, only creatures can use this spell
  66. si8 positiveness; //1 if spell is positive for influenced stacks, 0 if it is indifferent, -1 if it's negative
  67. 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)
  68. CSpell();
  69. ~CSpell();
  70. 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)
  71. si16 mainEffectAnim; //main spell effect animation, in AC format (or -1 when none)
  72. ETargetType getTargetType() const; //deprecated
  73. const CSpell::TargetInfo getTargetInfo(const int level) const;
  74. bool isCombatSpell() const;
  75. bool isAdventureSpell() const;
  76. bool isCreatureAbility() const;
  77. bool isPositive() const;
  78. bool isNegative() const;
  79. bool isNeutral() const;
  80. bool isRisingSpell() const;
  81. bool isDamageSpell() const;
  82. bool isOffensiveSpell() const;
  83. bool isSpecialSpell() const;
  84. bool hasEffects() const;
  85. void getEffects(std::vector<Bonus> &lst, const int level) const;
  86. ESpellCastProblem::ESpellCastProblem isImmuneBy(const IBonusBearer *obj) const;
  87. si32 getCost(const int skillLevel) const;
  88. /**
  89. * Returns spell level power, base power ignored
  90. */
  91. si32 getPower(const int skillLevel) const;
  92. // /**
  93. // * Returns spell power, taking base power into account
  94. // */
  95. // si32 calculatePower(const int skillLevel) const;
  96. si32 getProbability(const TFaction factionId) const;
  97. /**
  98. * Returns resource name of icon for SPELL_IMMUNITY bonus
  99. */
  100. const std::string& getIconImmune() const;
  101. const std::string& getCastSound() const;
  102. template <typename Handler> void serialize(Handler &h, const int version)
  103. {
  104. h & identifier & id & name & level & earth & water & fire & air & power
  105. & probabilities & attributes & combatSpell & creatureAbility & positiveness & counteredSpells & mainEffectAnim;
  106. h & isRising & isDamage & isOffensive;
  107. h & targetType;
  108. h & immunities & limiters & absoluteImmunities & absoluteLimiters;
  109. h & iconImmune;
  110. h & defaultProbability;
  111. h & isSpecial;
  112. h & castSound & iconBook & iconEffect & iconScenarioBonus & iconScroll;
  113. h & levels;
  114. }
  115. friend class CSpellHandler;
  116. friend class Graphics;
  117. private:
  118. void setIsOffensive(const bool val);
  119. void setIsRising(const bool val);
  120. private:
  121. si32 defaultProbability;
  122. bool isRising;
  123. bool isDamage;
  124. bool isOffensive;
  125. bool isSpecial;
  126. std::string attributes; //reference only attributes //todo: remove or include in configuration format, currently unused
  127. ETargetType targetType;
  128. std::vector<Bonus::BonusType> immunities; //any of these grants immunity
  129. std::vector<Bonus::BonusType> absoluteImmunities; //any of these grants immunity, can't be negated
  130. std::vector<Bonus::BonusType> limiters; //all of them are required to be affected
  131. std::vector<Bonus::BonusType> absoluteLimiters; //all of them are required to be affected, can't be negated
  132. ///graphics related stuff
  133. std::string iconImmune;
  134. std::string iconBook;
  135. std::string iconEffect;
  136. std::string iconScenarioBonus;
  137. std::string iconScroll;
  138. ///sound related stuff
  139. std::string castSound;
  140. std::vector<LevelInfo> levels;
  141. };
  142. bool DLL_LINKAGE isInScreenRange(const int3 &center, const int3 &pos); //for spells like Dimension Door
  143. class DLL_LINKAGE CSpellHandler: public CHandlerBase<SpellID, CSpell>
  144. {
  145. public:
  146. CSpellHandler();
  147. virtual ~CSpellHandler();
  148. ///IHandler base
  149. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  150. void afterLoadFinalization() override;
  151. void beforeValidate(JsonNode & object) override;
  152. /**
  153. * Gets a list of default allowed spells. OH3 spells are all allowed by default.
  154. *
  155. * @return a list of allowed spells, the index is the spell id and the value either 0 for not allowed or 1 for allowed
  156. */
  157. std::vector<bool> getDefaultAllowed() const override;
  158. const std::string getTypeName() const override;
  159. template <typename Handler> void serialize(Handler &h, const int version)
  160. {
  161. h & objects ;
  162. }
  163. protected:
  164. CSpell * loadFromJson(const JsonNode & json) override;
  165. };