ISpellMechanics.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "../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. spell->battleCast(env, *this);
  70. }
  71. BattleHex BattleSpellCastParameters::getFirstDestinationHex() const
  72. {
  73. return destinations.at(0).hexValue;
  74. }
  75. int BattleSpellCastParameters::getEffectValue() const
  76. {
  77. return (effectValue == 0) ? spell->calculateRawEffectValue(effectLevel, effectPower) : effectValue;
  78. }
  79. ///ISpellMechanics
  80. ISpellMechanics::ISpellMechanics(CSpell * s):
  81. owner(s)
  82. {
  83. }
  84. std::unique_ptr<ISpellMechanics> ISpellMechanics::createMechanics(CSpell * s)
  85. {
  86. switch (s->id)
  87. {
  88. case SpellID::ANTI_MAGIC:
  89. return make_unique<AntimagicMechanics>(s);
  90. case SpellID::ACID_BREATH_DAMAGE:
  91. return make_unique<AcidBreathDamageMechanics>(s);
  92. case SpellID::CHAIN_LIGHTNING:
  93. return make_unique<ChainLightningMechanics>(s);
  94. case SpellID::CLONE:
  95. return make_unique<CloneMechanics>(s);
  96. case SpellID::CURE:
  97. return make_unique<CureMechanics>(s);
  98. case SpellID::DEATH_STARE:
  99. return make_unique<DeathStareMechanics>(s);
  100. case SpellID::DISPEL:
  101. return make_unique<DispellMechanics>(s);
  102. case SpellID::DISPEL_HELPFUL_SPELLS:
  103. return make_unique<DispellHelpfulMechanics>(s);
  104. case SpellID::EARTHQUAKE:
  105. return make_unique<EarthquakeMechanics>(s);
  106. case SpellID::FIRE_WALL:
  107. return make_unique<FireWallMechanics>(s);
  108. case SpellID::FORCE_FIELD:
  109. return make_unique<ForceFieldMechanics>(s);
  110. case SpellID::HYPNOTIZE:
  111. return make_unique<HypnotizeMechanics>(s);
  112. case SpellID::LAND_MINE:
  113. return make_unique<LandMineMechanics>(s);
  114. case SpellID::QUICKSAND:
  115. return make_unique<QuicksandMechanics>(s);
  116. case SpellID::REMOVE_OBSTACLE:
  117. return make_unique<RemoveObstacleMechanics>(s);
  118. case SpellID::SACRIFICE:
  119. return make_unique<SacrificeMechanics>(s);
  120. case SpellID::SUMMON_FIRE_ELEMENTAL:
  121. return make_unique<SummonMechanics>(s, CreatureID::FIRE_ELEMENTAL);
  122. case SpellID::SUMMON_EARTH_ELEMENTAL:
  123. return make_unique<SummonMechanics>(s, CreatureID::EARTH_ELEMENTAL);
  124. case SpellID::SUMMON_WATER_ELEMENTAL:
  125. return make_unique<SummonMechanics>(s, CreatureID::WATER_ELEMENTAL);
  126. case SpellID::SUMMON_AIR_ELEMENTAL:
  127. return make_unique<SummonMechanics>(s, CreatureID::AIR_ELEMENTAL);
  128. case SpellID::TELEPORT:
  129. return make_unique<TeleportMechanics>(s);
  130. default:
  131. if(s->isRisingSpell())
  132. return make_unique<SpecialRisingSpellMechanics>(s);
  133. else
  134. return make_unique<DefaultSpellMechanics>(s);
  135. }
  136. }
  137. //IAdventureSpellMechanics
  138. IAdventureSpellMechanics::IAdventureSpellMechanics(CSpell * s):
  139. owner(s)
  140. {
  141. }
  142. std::unique_ptr<IAdventureSpellMechanics> IAdventureSpellMechanics::createMechanics(CSpell * s)
  143. {
  144. switch (s->id)
  145. {
  146. case SpellID::SUMMON_BOAT:
  147. return make_unique<SummonBoatMechanics>(s);
  148. case SpellID::SCUTTLE_BOAT:
  149. return make_unique<ScuttleBoatMechanics>(s);
  150. case SpellID::DIMENSION_DOOR:
  151. return make_unique<DimensionDoorMechanics>(s);
  152. case SpellID::FLY:
  153. case SpellID::WATER_WALK:
  154. case SpellID::VISIONS:
  155. case SpellID::DISGUISE:
  156. return make_unique<AdventureSpellMechanics>(s); //implemented using bonus system
  157. case SpellID::TOWN_PORTAL:
  158. return make_unique<TownPortalMechanics>(s);
  159. case SpellID::VIEW_EARTH:
  160. return make_unique<ViewEarthMechanics>(s);
  161. case SpellID::VIEW_AIR:
  162. return make_unique<ViewAirMechanics>(s);
  163. default:
  164. return std::unique_ptr<IAdventureSpellMechanics>();
  165. }
  166. }