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