ISpellMechanics.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * ISpellMechanics.cpp, 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. #include "StdInc.h"
  11. #include "ISpellMechanics.h"
  12. #include "../CStack.h"
  13. #include "../battle/BattleInfo.h"
  14. #include "../NetPacks.h"
  15. #include "CDefaultSpellMechanics.h"
  16. #include "AdventureSpellMechanics.h"
  17. #include "BattleSpellMechanics.h"
  18. #include "CreatureSpellMechanics.h"
  19. BattleSpellCastParameters::Destination::Destination(const CStack * destination):
  20. stackValue(destination),
  21. hexValue(destination->position)
  22. {
  23. }
  24. BattleSpellCastParameters::Destination::Destination(const BattleHex & destination):
  25. stackValue(nullptr),
  26. hexValue(destination)
  27. {
  28. }
  29. BattleSpellCastParameters::BattleSpellCastParameters(const BattleInfo * cb, const ISpellCaster * caster, const CSpell * spell_)
  30. : spell(spell_), cb(cb), caster(caster), casterColor(caster->getOwner()), casterSide(cb->whatSide(casterColor)),
  31. casterHero(nullptr),
  32. mode(ECastingMode::HERO_CASTING), casterStack(nullptr),
  33. spellLvl(0), effectLevel(0), effectPower(0), enchantPower(0), effectValue(0)
  34. {
  35. casterStack = dynamic_cast<const CStack *>(caster);
  36. casterHero = dynamic_cast<const CGHeroInstance *>(caster);
  37. spellLvl = caster->getSpellSchoolLevel(spell);
  38. effectLevel = caster->getEffectLevel(spell);
  39. effectPower = caster->getEffectPower(spell);
  40. effectValue = caster->getEffectValue(spell);
  41. enchantPower = caster->getEnchantPower(spell);
  42. vstd::amax(spellLvl, 0);
  43. vstd::amax(effectLevel, 0);
  44. vstd::amax(enchantPower, 0);
  45. vstd::amax(enchantPower, 0);
  46. vstd::amax(effectValue, 0);
  47. }
  48. BattleSpellCastParameters::BattleSpellCastParameters(const BattleSpellCastParameters & orig, const ISpellCaster * caster)
  49. :spell(orig.spell), cb(orig.cb), caster(caster), casterColor(caster->getOwner()), casterSide(cb->whatSide(casterColor)),
  50. casterHero(nullptr), mode(ECastingMode::MAGIC_MIRROR), casterStack(nullptr),
  51. spellLvl(orig.spellLvl), effectLevel(orig.effectLevel), effectPower(orig.effectPower), enchantPower(orig.enchantPower), effectValue(orig.effectValue)
  52. {
  53. casterStack = dynamic_cast<const CStack *>(caster);
  54. casterHero = dynamic_cast<const CGHeroInstance *>(caster);
  55. }
  56. void BattleSpellCastParameters::aimToHex(const BattleHex& destination)
  57. {
  58. destinations.push_back(Destination(destination));
  59. }
  60. void BattleSpellCastParameters::aimToStack(const CStack * destination)
  61. {
  62. if(nullptr == destination)
  63. logGlobal->error("BattleSpellCastParameters::aimToStack invalid stack.");
  64. else
  65. destinations.push_back(Destination(destination));
  66. }
  67. void BattleSpellCastParameters::cast(const SpellCastEnvironment * env)
  68. {
  69. if(destinations.empty())
  70. aimToHex(BattleHex::INVALID);
  71. spell->battleCast(env, *this);
  72. }
  73. bool BattleSpellCastParameters::castIfPossible(const SpellCastEnvironment * env)
  74. {
  75. if(ESpellCastProblem::OK == spell->canBeCast(cb, mode, caster))
  76. {
  77. cast(env);
  78. return true;
  79. }
  80. return false;
  81. }
  82. BattleHex BattleSpellCastParameters::getFirstDestinationHex() const
  83. {
  84. if(destinations.empty())
  85. {
  86. logGlobal->error("Spell have no destinations.");
  87. return BattleHex::INVALID;
  88. }
  89. return destinations.at(0).hexValue;
  90. }
  91. int BattleSpellCastParameters::getEffectValue() const
  92. {
  93. return (effectValue == 0) ? spell->calculateRawEffectValue(effectLevel, effectPower) : effectValue;
  94. }
  95. ///ISpellMechanics
  96. ISpellMechanics::ISpellMechanics(const CSpell * s):
  97. owner(s)
  98. {
  99. }
  100. std::unique_ptr<ISpellMechanics> ISpellMechanics::createMechanics(const CSpell * s)
  101. {
  102. switch (s->id)
  103. {
  104. case SpellID::ANTI_MAGIC:
  105. return make_unique<AntimagicMechanics>(s);
  106. case SpellID::ACID_BREATH_DAMAGE:
  107. return make_unique<AcidBreathDamageMechanics>(s);
  108. case SpellID::CHAIN_LIGHTNING:
  109. return make_unique<ChainLightningMechanics>(s);
  110. case SpellID::CLONE:
  111. return make_unique<CloneMechanics>(s);
  112. case SpellID::CURE:
  113. return make_unique<CureMechanics>(s);
  114. case SpellID::DEATH_STARE:
  115. return make_unique<DeathStareMechanics>(s);
  116. case SpellID::DISPEL:
  117. return make_unique<DispellMechanics>(s);
  118. case SpellID::DISPEL_HELPFUL_SPELLS:
  119. return make_unique<DispellHelpfulMechanics>(s);
  120. case SpellID::EARTHQUAKE:
  121. return make_unique<EarthquakeMechanics>(s);
  122. case SpellID::FIRE_WALL:
  123. return make_unique<FireWallMechanics>(s);
  124. case SpellID::FORCE_FIELD:
  125. return make_unique<ForceFieldMechanics>(s);
  126. case SpellID::HYPNOTIZE:
  127. return make_unique<HypnotizeMechanics>(s);
  128. case SpellID::LAND_MINE:
  129. return make_unique<LandMineMechanics>(s);
  130. case SpellID::QUICKSAND:
  131. return make_unique<QuicksandMechanics>(s);
  132. case SpellID::REMOVE_OBSTACLE:
  133. return make_unique<RemoveObstacleMechanics>(s);
  134. case SpellID::SACRIFICE:
  135. return make_unique<SacrificeMechanics>(s);
  136. case SpellID::SUMMON_FIRE_ELEMENTAL:
  137. return make_unique<SummonMechanics>(s, CreatureID::FIRE_ELEMENTAL);
  138. case SpellID::SUMMON_EARTH_ELEMENTAL:
  139. return make_unique<SummonMechanics>(s, CreatureID::EARTH_ELEMENTAL);
  140. case SpellID::SUMMON_WATER_ELEMENTAL:
  141. return make_unique<SummonMechanics>(s, CreatureID::WATER_ELEMENTAL);
  142. case SpellID::SUMMON_AIR_ELEMENTAL:
  143. return make_unique<SummonMechanics>(s, CreatureID::AIR_ELEMENTAL);
  144. case SpellID::TELEPORT:
  145. return make_unique<TeleportMechanics>(s);
  146. default:
  147. if(s->isRisingSpell())
  148. return make_unique<SpecialRisingSpellMechanics>(s);
  149. else
  150. return make_unique<DefaultSpellMechanics>(s);
  151. }
  152. }
  153. //IAdventureSpellMechanics
  154. IAdventureSpellMechanics::IAdventureSpellMechanics(const CSpell * s):
  155. owner(s)
  156. {
  157. }
  158. std::unique_ptr<IAdventureSpellMechanics> IAdventureSpellMechanics::createMechanics(const CSpell * s)
  159. {
  160. switch (s->id)
  161. {
  162. case SpellID::SUMMON_BOAT:
  163. return make_unique<SummonBoatMechanics>(s);
  164. case SpellID::SCUTTLE_BOAT:
  165. return make_unique<ScuttleBoatMechanics>(s);
  166. case SpellID::DIMENSION_DOOR:
  167. return make_unique<DimensionDoorMechanics>(s);
  168. case SpellID::FLY:
  169. case SpellID::WATER_WALK:
  170. case SpellID::VISIONS:
  171. case SpellID::DISGUISE:
  172. return make_unique<AdventureSpellMechanics>(s); //implemented using bonus system
  173. case SpellID::TOWN_PORTAL:
  174. return make_unique<TownPortalMechanics>(s);
  175. case SpellID::VIEW_EARTH:
  176. return make_unique<ViewEarthMechanics>(s);
  177. case SpellID::VIEW_AIR:
  178. return make_unique<ViewAirMechanics>(s);
  179. default:
  180. return std::unique_ptr<IAdventureSpellMechanics>();
  181. }
  182. }