ISpellMechanics.cpp 5.8 KB

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