2014-11-12 10:36:34 +02:00
|
|
|
/*
|
2015-02-02 11:22:19 +02:00
|
|
|
* ISpellMechanics.cpp, part of VCMI engine
|
2014-11-12 10:36:34 +02:00
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "ISpellMechanics.h"
|
2014-11-12 10:36:34 +02:00
|
|
|
|
2017-03-17 17:48:44 +02:00
|
|
|
#include "../CStack.h"
|
2017-06-24 16:42:05 +02:00
|
|
|
#include "../battle/BattleInfo.h"
|
2017-03-17 17:48:44 +02:00
|
|
|
|
2015-09-17 07:42:30 +02:00
|
|
|
#include "../NetPacks.h"
|
|
|
|
|
2015-02-02 11:22:19 +02:00
|
|
|
#include "CDefaultSpellMechanics.h"
|
2014-11-12 10:36:34 +02:00
|
|
|
|
2015-02-02 11:22:19 +02:00
|
|
|
#include "AdventureSpellMechanics.h"
|
|
|
|
#include "BattleSpellMechanics.h"
|
|
|
|
#include "CreatureSpellMechanics.h"
|
2014-11-13 03:53:25 +02:00
|
|
|
|
2015-09-29 20:47:04 +02:00
|
|
|
BattleSpellCastParameters::Destination::Destination(const CStack * destination):
|
|
|
|
stackValue(destination),
|
|
|
|
hexValue(destination->position)
|
|
|
|
{
|
2016-02-15 12:34:37 +02:00
|
|
|
|
2015-09-29 20:47:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BattleSpellCastParameters::Destination::Destination(const BattleHex & destination):
|
|
|
|
stackValue(nullptr),
|
2016-02-15 12:34:37 +02:00
|
|
|
hexValue(destination)
|
2015-09-29 20:47:04 +02:00
|
|
|
{
|
2016-02-15 12:34:37 +02:00
|
|
|
|
2015-09-29 20:47:04 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 12:33:11 +02:00
|
|
|
BattleSpellCastParameters::BattleSpellCastParameters(const BattleInfo * cb, const ISpellCaster * caster, const CSpell * spell_)
|
|
|
|
: spell(spell_), cb(cb), caster(caster), casterColor(caster->getOwner()), casterSide(cb->whatSide(casterColor)),
|
2015-09-29 20:47:04 +02:00
|
|
|
casterHero(nullptr),
|
2016-09-06 11:26:01 +02:00
|
|
|
mode(ECastingMode::HERO_CASTING), casterStack(nullptr),
|
2016-09-10 08:37:18 +02:00
|
|
|
spellLvl(0), effectLevel(0), effectPower(0), enchantPower(0), effectValue(0)
|
2015-09-16 09:50:33 +02:00
|
|
|
{
|
2015-09-17 07:42:30 +02:00
|
|
|
casterStack = dynamic_cast<const CStack *>(caster);
|
|
|
|
casterHero = dynamic_cast<const CGHeroInstance *>(caster);
|
2016-09-06 12:33:11 +02:00
|
|
|
|
|
|
|
spellLvl = caster->getSpellSchoolLevel(spell);
|
|
|
|
effectLevel = caster->getEffectLevel(spell);
|
|
|
|
effectPower = caster->getEffectPower(spell);
|
|
|
|
effectValue = caster->getEffectValue(spell);
|
|
|
|
enchantPower = caster->getEnchantPower(spell);
|
|
|
|
|
|
|
|
vstd::amax(spellLvl, 0);
|
|
|
|
vstd::amax(effectLevel, 0);
|
|
|
|
vstd::amax(enchantPower, 0);
|
|
|
|
vstd::amax(enchantPower, 0);
|
|
|
|
vstd::amax(effectValue, 0);
|
2015-09-16 09:50:33 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 12:51:53 +02:00
|
|
|
BattleSpellCastParameters::BattleSpellCastParameters(const BattleSpellCastParameters & orig, const ISpellCaster * caster)
|
|
|
|
: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)
|
|
|
|
{
|
|
|
|
casterStack = dynamic_cast<const CStack *>(caster);
|
|
|
|
casterHero = dynamic_cast<const CGHeroInstance *>(caster);
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:47:04 +02:00
|
|
|
void BattleSpellCastParameters::aimToHex(const BattleHex& destination)
|
|
|
|
{
|
|
|
|
destinations.push_back(Destination(destination));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleSpellCastParameters::aimToStack(const CStack * destination)
|
|
|
|
{
|
2016-09-06 11:26:01 +02:00
|
|
|
if(nullptr == destination)
|
|
|
|
logGlobal->error("BattleSpellCastParameters::aimToStack invalid stack.");
|
|
|
|
else
|
|
|
|
destinations.push_back(Destination(destination));
|
2015-09-29 20:47:04 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 12:33:11 +02:00
|
|
|
void BattleSpellCastParameters::cast(const SpellCastEnvironment * env)
|
2015-09-29 20:47:04 +02:00
|
|
|
{
|
2017-03-18 13:08:02 +02:00
|
|
|
if(destinations.empty())
|
|
|
|
aimToHex(BattleHex::INVALID);
|
2016-09-06 12:33:11 +02:00
|
|
|
spell->battleCast(env, *this);
|
2015-09-29 20:47:04 +02:00
|
|
|
}
|
|
|
|
|
2017-06-05 20:41:27 +02:00
|
|
|
bool BattleSpellCastParameters::castIfPossible(const SpellCastEnvironment * env)
|
2017-03-18 13:08:02 +02:00
|
|
|
{
|
2017-06-05 22:46:55 +02:00
|
|
|
if(ESpellCastProblem::OK == spell->canBeCast(cb, mode, caster))
|
2017-06-05 20:41:27 +02:00
|
|
|
{
|
2017-03-18 13:08:02 +02:00
|
|
|
cast(env);
|
2017-06-05 20:41:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-03-18 13:08:02 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 12:33:11 +02:00
|
|
|
BattleHex BattleSpellCastParameters::getFirstDestinationHex() const
|
2015-09-17 07:42:30 +02:00
|
|
|
{
|
2017-03-18 13:08:02 +02:00
|
|
|
if(destinations.empty())
|
|
|
|
{
|
|
|
|
logGlobal->error("Spell have no destinations.");
|
|
|
|
return BattleHex::INVALID;
|
|
|
|
}
|
2016-09-06 12:33:11 +02:00
|
|
|
return destinations.at(0).hexValue;
|
2015-09-17 07:42:30 +02:00
|
|
|
}
|
2015-09-16 09:50:33 +02:00
|
|
|
|
2016-09-18 14:27:22 +02:00
|
|
|
int BattleSpellCastParameters::getEffectValue() const
|
|
|
|
{
|
|
|
|
return (effectValue == 0) ? spell->calculateRawEffectValue(effectLevel, effectPower) : effectValue;
|
|
|
|
}
|
|
|
|
|
2014-11-13 06:34:20 +02:00
|
|
|
///ISpellMechanics
|
2017-06-06 06:53:51 +02:00
|
|
|
ISpellMechanics::ISpellMechanics(const CSpell * s):
|
2014-11-13 06:34:20 +02:00
|
|
|
owner(s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
std::unique_ptr<ISpellMechanics> ISpellMechanics::createMechanics(const CSpell * s)
|
2014-11-25 13:58:42 +02:00
|
|
|
{
|
|
|
|
switch (s->id)
|
|
|
|
{
|
2015-04-03 02:28:20 +02:00
|
|
|
case SpellID::ANTI_MAGIC:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<AntimagicMechanics>(s);
|
2014-11-25 22:59:21 +02:00
|
|
|
case SpellID::ACID_BREATH_DAMAGE:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<AcidBreathDamageMechanics>(s);
|
2014-11-25 22:59:21 +02:00
|
|
|
case SpellID::CHAIN_LIGHTNING:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<ChainLightningMechanics>(s);
|
2014-11-25 13:58:42 +02:00
|
|
|
case SpellID::CLONE:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<CloneMechanics>(s);
|
2014-11-25 22:59:21 +02:00
|
|
|
case SpellID::CURE:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<CureMechanics>(s);
|
2014-11-25 22:59:21 +02:00
|
|
|
case SpellID::DEATH_STARE:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<DeathStareMechanics>(s);
|
2014-11-25 22:59:21 +02:00
|
|
|
case SpellID::DISPEL:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<DispellMechanics>(s);
|
2014-11-25 13:58:42 +02:00
|
|
|
case SpellID::DISPEL_HELPFUL_SPELLS:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<DispellHelpfulMechanics>(s);
|
2015-03-18 12:27:07 +02:00
|
|
|
case SpellID::EARTHQUAKE:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<EarthquakeMechanics>(s);
|
2014-11-25 13:58:42 +02:00
|
|
|
case SpellID::FIRE_WALL:
|
2016-09-05 12:34:48 +02:00
|
|
|
return make_unique<FireWallMechanics>(s);
|
2014-11-25 13:58:42 +02:00
|
|
|
case SpellID::FORCE_FIELD:
|
2016-09-05 12:34:48 +02:00
|
|
|
return make_unique<ForceFieldMechanics>(s);
|
2014-11-25 22:59:21 +02:00
|
|
|
case SpellID::HYPNOTIZE:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<HypnotizeMechanics>(s);
|
2014-11-25 13:58:42 +02:00
|
|
|
case SpellID::LAND_MINE:
|
2016-09-05 12:34:48 +02:00
|
|
|
return make_unique<LandMineMechanics>(s);
|
2014-11-25 13:58:42 +02:00
|
|
|
case SpellID::QUICKSAND:
|
2016-09-05 12:34:48 +02:00
|
|
|
return make_unique<QuicksandMechanics>(s);
|
2014-11-25 22:59:21 +02:00
|
|
|
case SpellID::REMOVE_OBSTACLE:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<RemoveObstacleMechanics>(s);
|
2014-11-25 22:59:21 +02:00
|
|
|
case SpellID::SACRIFICE:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<SacrificeMechanics>(s);
|
2014-11-25 16:35:20 +02:00
|
|
|
case SpellID::SUMMON_FIRE_ELEMENTAL:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<SummonMechanics>(s, CreatureID::FIRE_ELEMENTAL);
|
2014-11-25 16:35:20 +02:00
|
|
|
case SpellID::SUMMON_EARTH_ELEMENTAL:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<SummonMechanics>(s, CreatureID::EARTH_ELEMENTAL);
|
2014-11-25 16:35:20 +02:00
|
|
|
case SpellID::SUMMON_WATER_ELEMENTAL:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<SummonMechanics>(s, CreatureID::WATER_ELEMENTAL);
|
2014-11-25 16:35:20 +02:00
|
|
|
case SpellID::SUMMON_AIR_ELEMENTAL:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<SummonMechanics>(s, CreatureID::AIR_ELEMENTAL);
|
2014-11-25 22:59:21 +02:00
|
|
|
case SpellID::TELEPORT:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<TeleportMechanics>(s);
|
|
|
|
default:
|
|
|
|
if(s->isRisingSpell())
|
|
|
|
return make_unique<SpecialRisingSpellMechanics>(s);
|
|
|
|
else
|
|
|
|
return make_unique<DefaultSpellMechanics>(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//IAdventureSpellMechanics
|
2017-06-06 06:53:51 +02:00
|
|
|
IAdventureSpellMechanics::IAdventureSpellMechanics(const CSpell * s):
|
2016-09-04 07:19:28 +02:00
|
|
|
owner(s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:53:51 +02:00
|
|
|
std::unique_ptr<IAdventureSpellMechanics> IAdventureSpellMechanics::createMechanics(const CSpell * s)
|
2016-09-04 07:19:28 +02:00
|
|
|
{
|
|
|
|
switch (s->id)
|
|
|
|
{
|
2014-12-25 23:02:50 +02:00
|
|
|
case SpellID::SUMMON_BOAT:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<SummonBoatMechanics>(s);
|
2015-02-26 19:59:18 +02:00
|
|
|
case SpellID::SCUTTLE_BOAT:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<ScuttleBoatMechanics>(s);
|
2014-12-25 23:02:50 +02:00
|
|
|
case SpellID::DIMENSION_DOOR:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<DimensionDoorMechanics>(s);
|
2014-12-25 23:02:50 +02:00
|
|
|
case SpellID::FLY:
|
|
|
|
case SpellID::WATER_WALK:
|
2015-02-06 14:41:26 +02:00
|
|
|
case SpellID::VISIONS:
|
|
|
|
case SpellID::DISGUISE:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<AdventureSpellMechanics>(s); //implemented using bonus system
|
2014-12-25 23:02:50 +02:00
|
|
|
case SpellID::TOWN_PORTAL:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<TownPortalMechanics>(s);
|
2014-12-25 23:02:50 +02:00
|
|
|
case SpellID::VIEW_EARTH:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<ViewEarthMechanics>(s);
|
2015-02-26 19:59:18 +02:00
|
|
|
case SpellID::VIEW_AIR:
|
2016-09-04 07:19:28 +02:00
|
|
|
return make_unique<ViewAirMechanics>(s);
|
2015-02-26 19:59:18 +02:00
|
|
|
default:
|
2016-09-04 07:19:28 +02:00
|
|
|
return std::unique_ptr<IAdventureSpellMechanics>();
|
2015-02-26 19:59:18 +02:00
|
|
|
}
|
2014-11-25 13:58:42 +02:00
|
|
|
}
|