ISpellMechanics.cpp 6.2 KB

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