2015-02-02 10:40:06 +02:00
|
|
|
/*
|
|
|
|
* BattleSpellMechanics.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
#include "BattleSpellMechanics.h"
|
2015-02-02 11:22:19 +02:00
|
|
|
|
|
|
|
#include "../NetPacks.h"
|
|
|
|
#include "../BattleState.h"
|
2015-09-17 12:23:13 +02:00
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
2015-12-02 21:05:10 +02:00
|
|
|
#include "../mapObjects/CGTownInstance.h"
|
2015-02-02 11:22:19 +02:00
|
|
|
|
2015-09-16 06:33:36 +02:00
|
|
|
///HealingSpellMechanics
|
2015-09-26 20:35:30 +02:00
|
|
|
void HealingSpellMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
|
2015-09-16 06:33:36 +02:00
|
|
|
{
|
2015-09-17 04:47:32 +02:00
|
|
|
EHealLevel healLevel = getHealLevel(parameters.effectLevel);
|
2015-09-16 08:44:18 +02:00
|
|
|
int hpGained = calculateHealedHP(env, parameters, ctx);
|
2015-09-16 06:33:36 +02:00
|
|
|
StacksHealedOrResurrected shr;
|
|
|
|
shr.lifeDrain = false;
|
|
|
|
shr.tentHealing = false;
|
2016-09-24 09:06:24 +02:00
|
|
|
//special case for Archangel
|
|
|
|
shr.cure = parameters.mode == ECastingMode::CREATURE_ACTIVE_CASTING && owner->id == SpellID::RESURRECTION;
|
2015-09-16 06:33:36 +02:00
|
|
|
|
2015-09-16 06:56:02 +02:00
|
|
|
const bool resurrect = (healLevel != EHealLevel::HEAL);
|
2015-09-16 06:33:36 +02:00
|
|
|
for(auto & attackedCre : ctx.attackedCres)
|
|
|
|
{
|
|
|
|
StacksHealedOrResurrected::HealInfo hi;
|
2016-02-15 12:34:37 +02:00
|
|
|
hi.stackID = (attackedCre)->ID;
|
2015-09-17 08:29:57 +02:00
|
|
|
int stackHPgained = parameters.caster->getSpellBonus(owner, hpGained, attackedCre);
|
2015-09-16 14:43:39 +02:00
|
|
|
hi.healedHP = attackedCre->calculateHealedHealthPoints(stackHPgained, resurrect);
|
2015-09-16 06:56:02 +02:00
|
|
|
hi.lowLevelResurrection = (healLevel == EHealLevel::RESURRECT);
|
2015-09-16 06:33:36 +02:00
|
|
|
shr.healedStacks.push_back(hi);
|
|
|
|
}
|
|
|
|
if(!shr.healedStacks.empty())
|
2015-09-16 08:44:18 +02:00
|
|
|
env->sendAndApply(&shr);
|
|
|
|
}
|
|
|
|
|
2015-09-16 14:43:39 +02:00
|
|
|
int HealingSpellMechanics::calculateHealedHP(const SpellCastEnvironment* env, const BattleSpellCastParameters& parameters, SpellCastContext& ctx) const
|
2015-09-16 08:44:18 +02:00
|
|
|
{
|
2016-09-18 14:27:22 +02:00
|
|
|
return parameters.getEffectValue();
|
2015-09-16 06:33:36 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 02:28:20 +02:00
|
|
|
///AntimagicMechanics
|
|
|
|
void AntimagicMechanics::applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const
|
|
|
|
{
|
|
|
|
DefaultSpellMechanics::applyBattle(battle, packet);
|
|
|
|
|
2016-09-19 23:36:35 +02:00
|
|
|
doDispell(battle, packet, [this](const Bonus *b) -> bool
|
2015-04-03 02:28:20 +02:00
|
|
|
{
|
2016-10-01 13:47:21 +02:00
|
|
|
if(b->source == Bonus::SPELL_EFFECT)
|
2016-02-15 12:34:37 +02:00
|
|
|
{
|
2016-10-01 13:47:21 +02:00
|
|
|
const CSpell * sourceSpell = SpellID(b->sid).toSpell();
|
|
|
|
if(!sourceSpell)
|
|
|
|
return false;//error
|
2016-09-30 00:54:40 +02:00
|
|
|
//keep positive effects
|
|
|
|
if(sourceSpell->isPositive())
|
|
|
|
return false;
|
|
|
|
//keep own effects
|
|
|
|
if(sourceSpell == owner)
|
|
|
|
return false;
|
|
|
|
//remove all others
|
|
|
|
return true;
|
2015-04-03 02:28:20 +02:00
|
|
|
}
|
2016-09-30 00:54:40 +02:00
|
|
|
|
2015-04-11 15:27:14 +02:00
|
|
|
return false; //not a spell effect
|
2016-02-15 12:34:37 +02:00
|
|
|
});
|
2015-04-03 02:28:20 +02:00
|
|
|
}
|
|
|
|
|
2015-02-02 11:22:19 +02:00
|
|
|
///ChainLightningMechanics
|
2016-09-06 06:05:55 +02:00
|
|
|
std::vector<const CStack *> ChainLightningMechanics::calculateAffectedStacks(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx) const
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
2016-09-06 05:40:23 +02:00
|
|
|
std::vector<const CStack *> res;
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 11:22:19 +02:00
|
|
|
std::set<BattleHex> possibleHexes;
|
2016-09-04 04:15:37 +02:00
|
|
|
for(auto stack : cb->battleGetAllStacks())
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
|
|
|
if(stack->isValidTarget())
|
|
|
|
{
|
|
|
|
for(auto hex : stack->getHexes())
|
|
|
|
{
|
|
|
|
possibleHexes.insert (hex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int targetsOnLevel[4] = {4, 4, 5, 5};
|
|
|
|
|
|
|
|
BattleHex lightningHex = ctx.destination;
|
|
|
|
for(int i = 0; i < targetsOnLevel[ctx.schoolLvl]; ++i)
|
|
|
|
{
|
2016-09-04 04:15:37 +02:00
|
|
|
auto stack = cb->battleGetStackByPos(lightningHex, true);
|
2015-02-02 11:22:19 +02:00
|
|
|
if(!stack)
|
|
|
|
break;
|
2016-09-06 05:40:23 +02:00
|
|
|
res.push_back(stack);
|
2015-02-02 11:22:19 +02:00
|
|
|
for(auto hex : stack->getHexes())
|
|
|
|
{
|
2016-09-06 05:40:23 +02:00
|
|
|
possibleHexes.erase(hex); //can't hit same stack twice
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
if(possibleHexes.empty()) //not enough targets
|
|
|
|
break;
|
2016-09-06 05:40:23 +02:00
|
|
|
lightningHex = BattleHex::getClosestTile(stack->attackerOwned, lightningHex, possibleHexes);
|
2015-02-26 19:59:18 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 05:40:23 +02:00
|
|
|
return res;
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///CloneMechanics
|
2015-09-26 20:35:30 +02:00
|
|
|
void CloneMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
|
|
|
const CStack * clonedStack = nullptr;
|
|
|
|
if(ctx.attackedCres.size())
|
|
|
|
clonedStack = *ctx.attackedCres.begin();
|
|
|
|
if(!clonedStack)
|
|
|
|
{
|
|
|
|
env->complain ("No target stack to clone!");
|
|
|
|
return;
|
|
|
|
}
|
2015-02-26 19:59:18 +02:00
|
|
|
const int attacker = !(bool)parameters.casterSide;
|
2015-02-02 11:22:19 +02:00
|
|
|
|
|
|
|
BattleStackAdded bsa;
|
|
|
|
bsa.creID = clonedStack->type->idNumber;
|
|
|
|
bsa.attacker = attacker;
|
|
|
|
bsa.summoned = true;
|
|
|
|
bsa.pos = parameters.cb->getAvaliableHex(bsa.creID, attacker); //TODO: unify it
|
|
|
|
bsa.amount = clonedStack->count;
|
|
|
|
env->sendAndApply(&bsa);
|
|
|
|
|
|
|
|
BattleSetStackProperty ssp;
|
|
|
|
ssp.stackID = bsa.newStackID;//we know stack ID after apply
|
|
|
|
ssp.which = BattleSetStackProperty::CLONED;
|
|
|
|
ssp.val = 0;
|
|
|
|
ssp.absolute = 1;
|
2015-02-26 19:59:18 +02:00
|
|
|
env->sendAndApply(&ssp);
|
2016-02-15 12:34:37 +02:00
|
|
|
|
2015-09-14 19:13:26 +02:00
|
|
|
ssp.stackID = clonedStack->ID;
|
|
|
|
ssp.which = BattleSetStackProperty::HAS_CLONE;
|
|
|
|
ssp.val = bsa.newStackID;
|
|
|
|
ssp.absolute = 1;
|
2016-02-15 12:34:37 +02:00
|
|
|
env->sendAndApply(&ssp);
|
2016-03-05 03:46:32 +02:00
|
|
|
|
|
|
|
SetStackEffect sse;
|
|
|
|
sse.stacks.push_back(bsa.newStackID);
|
|
|
|
Bonus lifeTimeMarker(Bonus::N_TURNS, Bonus::NONE, Bonus::SPELL_EFFECT, 0, owner->id.num);
|
|
|
|
lifeTimeMarker.turnsRemain = parameters.enchantPower;
|
|
|
|
sse.effect.push_back(lifeTimeMarker);
|
|
|
|
env->sendAndApply(&sse);
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
|
2015-09-26 19:09:54 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem CloneMechanics::isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
|
|
|
//can't clone already cloned creature
|
|
|
|
if(vstd::contains(obj->state, EBattleStackState::CLONED))
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
2015-09-14 19:13:26 +02:00
|
|
|
if(obj->cloneID != -1)
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
2015-02-02 11:22:19 +02:00
|
|
|
ui8 schoolLevel;
|
|
|
|
if(caster)
|
|
|
|
{
|
2015-09-26 19:09:54 +02:00
|
|
|
schoolLevel = caster->getEffectLevel(owner);
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-09-28 15:06:26 +02:00
|
|
|
schoolLevel = 3;//todo: remove
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(schoolLevel < 3)
|
|
|
|
{
|
|
|
|
int maxLevel = (std::max(schoolLevel, (ui8)1) + 4);
|
|
|
|
int creLevel = obj->getCreature()->level;
|
|
|
|
if(maxLevel < creLevel) //tier 1-5 for basic, 1-6 for advanced, any level for expert
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
}
|
2015-02-26 19:59:18 +02:00
|
|
|
//use default algorithm only if there is no mechanics-related problem
|
|
|
|
return DefaultSpellMechanics::isImmuneByStack(caster, obj);
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///CureMechanics
|
|
|
|
void CureMechanics::applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const
|
|
|
|
{
|
|
|
|
DefaultSpellMechanics::applyBattle(battle, packet);
|
2016-09-11 12:48:14 +02:00
|
|
|
|
|
|
|
doDispell(battle, packet, dispellSelector);
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
|
2015-09-16 06:56:02 +02:00
|
|
|
HealingSpellMechanics::EHealLevel CureMechanics::getHealLevel(int effectLevel) const
|
|
|
|
{
|
|
|
|
return EHealLevel::HEAL;
|
|
|
|
}
|
|
|
|
|
2016-09-11 12:48:14 +02:00
|
|
|
bool CureMechanics::dispellSelector(const Bonus * b)
|
|
|
|
{
|
|
|
|
if(b->source == Bonus::SPELL_EFFECT)
|
|
|
|
{
|
2016-10-02 14:22:55 +02:00
|
|
|
const CSpell * sp = SpellID(b->sid).toSpell();
|
|
|
|
return sp && sp->isNegative();
|
2016-09-11 12:48:14 +02:00
|
|
|
}
|
|
|
|
return false; //not a spell effect
|
|
|
|
}
|
|
|
|
|
|
|
|
ESpellCastProblem::ESpellCastProblem CureMechanics::isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const
|
|
|
|
{
|
|
|
|
//Selector method name is ok as cashing string. --AVS
|
2016-10-01 09:31:59 +02:00
|
|
|
if(!obj->canBeHealed() && !canDispell(obj, dispellSelector, "CureMechanics::dispellSelector"))
|
2016-09-11 12:48:14 +02:00
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
|
|
|
|
return DefaultSpellMechanics::isImmuneByStack(caster, obj);
|
|
|
|
}
|
|
|
|
|
2015-02-02 11:22:19 +02:00
|
|
|
///DispellMechanics
|
|
|
|
void DispellMechanics::applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const
|
|
|
|
{
|
|
|
|
DefaultSpellMechanics::applyBattle(battle, packet);
|
2016-10-01 09:31:59 +02:00
|
|
|
doDispell(battle, packet, Selector::all);
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
|
2015-09-26 19:09:54 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem DispellMechanics::isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const
|
2015-04-03 00:52:20 +02:00
|
|
|
{
|
2016-09-29 15:50:33 +02:00
|
|
|
//just in case
|
|
|
|
if(!obj->alive())
|
|
|
|
return ESpellCastProblem::WRONG_SPELL_TARGET;
|
|
|
|
|
2016-02-15 12:34:37 +02:00
|
|
|
//DISPELL ignores all immunities, except specific absolute immunity
|
2015-09-18 07:04:13 +02:00
|
|
|
{
|
|
|
|
//SPELL_IMMUNITY absolute case
|
|
|
|
std::stringstream cachingStr;
|
|
|
|
cachingStr << "type_" << Bonus::SPELL_IMMUNITY << "subtype_" << owner->id.toEnum() << "addInfo_1";
|
|
|
|
if(obj->hasBonus(Selector::typeSubtypeInfo(Bonus::SPELL_IMMUNITY, owner->id.toEnum(), 1), cachingStr.str()))
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
2015-04-03 02:02:16 +02:00
|
|
|
}
|
|
|
|
|
2016-10-01 09:31:59 +02:00
|
|
|
if(canDispell(obj, Selector::all, "DefaultSpellMechanics::dispellSelector"))
|
2016-09-29 15:50:33 +02:00
|
|
|
return ESpellCastProblem::OK;
|
|
|
|
else
|
|
|
|
return ESpellCastProblem::WRONG_SPELL_TARGET;
|
2015-09-18 07:04:13 +02:00
|
|
|
//any other immunities are ignored - do not execute default algorithm
|
2015-04-03 00:52:20 +02:00
|
|
|
}
|
2015-04-03 00:16:41 +02:00
|
|
|
|
2015-09-26 20:35:30 +02:00
|
|
|
void DispellMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
|
2015-04-03 02:01:05 +02:00
|
|
|
{
|
|
|
|
if(parameters.spellLvl > 2)
|
|
|
|
{
|
|
|
|
//expert DISPELL also removes spell-created obstacles
|
|
|
|
ObstaclesRemoved packet;
|
2015-04-03 02:02:16 +02:00
|
|
|
|
2015-04-03 02:01:05 +02:00
|
|
|
for(const auto obstacle : parameters.cb->obstacles)
|
|
|
|
{
|
2015-04-03 02:02:16 +02:00
|
|
|
if(obstacle->obstacleType == CObstacleInstance::FIRE_WALL
|
|
|
|
|| obstacle->obstacleType == CObstacleInstance::FORCE_FIELD
|
2015-04-03 02:01:05 +02:00
|
|
|
|| obstacle->obstacleType == CObstacleInstance::LAND_MINE)
|
|
|
|
packet.obstacles.insert(obstacle->uniqueID);
|
|
|
|
}
|
2015-04-03 02:02:16 +02:00
|
|
|
|
2015-04-03 02:01:05 +02:00
|
|
|
if(!packet.obstacles.empty())
|
|
|
|
env->sendAndApply(&packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 12:27:07 +02:00
|
|
|
///EarthquakeMechanics
|
2015-09-26 20:35:30 +02:00
|
|
|
void EarthquakeMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
|
2015-03-18 12:27:07 +02:00
|
|
|
{
|
2015-09-17 10:49:15 +02:00
|
|
|
if(nullptr == parameters.cb->battleGetDefendedTown())
|
2015-03-18 12:27:07 +02:00
|
|
|
{
|
|
|
|
env->complain("EarthquakeMechanics: not town siege");
|
2015-03-19 09:35:05 +02:00
|
|
|
return;
|
2015-03-18 12:27:07 +02:00
|
|
|
}
|
2015-03-19 09:35:05 +02:00
|
|
|
|
2015-09-17 10:49:15 +02:00
|
|
|
if(CGTownInstance::NONE == parameters.cb->battleGetDefendedTown()->fortLevel())
|
2015-03-18 12:27:07 +02:00
|
|
|
{
|
|
|
|
env->complain("EarthquakeMechanics: town has no fort");
|
2015-03-19 09:35:05 +02:00
|
|
|
return;
|
2015-03-18 12:27:07 +02:00
|
|
|
}
|
2015-03-19 09:35:05 +02:00
|
|
|
|
|
|
|
//start with all destructible parts
|
|
|
|
std::set<EWallPart::EWallPart> possibleTargets =
|
2015-03-18 12:27:07 +02:00
|
|
|
{
|
|
|
|
EWallPart::KEEP,
|
2015-03-19 09:35:05 +02:00
|
|
|
EWallPart::BOTTOM_TOWER,
|
|
|
|
EWallPart::BOTTOM_WALL,
|
|
|
|
EWallPart::BELOW_GATE,
|
|
|
|
EWallPart::OVER_GATE,
|
|
|
|
EWallPart::UPPER_WALL,
|
|
|
|
EWallPart::UPPER_TOWER,
|
2015-03-18 12:27:07 +02:00
|
|
|
EWallPart::GATE
|
|
|
|
};
|
2015-03-19 09:35:05 +02:00
|
|
|
|
2015-03-18 12:27:07 +02:00
|
|
|
assert(possibleTargets.size() == EWallPart::PARTS_COUNT);
|
2015-03-19 09:35:05 +02:00
|
|
|
|
2015-03-18 13:28:34 +02:00
|
|
|
const int targetsToAttack = 2 + std::max<int>(parameters.spellLvl - 1, 0);
|
2015-03-19 09:35:05 +02:00
|
|
|
|
2015-03-18 12:27:07 +02:00
|
|
|
CatapultAttack ca;
|
|
|
|
ca.attacker = -1;
|
2015-03-19 09:35:05 +02:00
|
|
|
|
2015-03-18 13:28:34 +02:00
|
|
|
for(int i = 0; i < targetsToAttack; i++)
|
2015-03-18 12:27:07 +02:00
|
|
|
{
|
2015-03-18 13:28:34 +02:00
|
|
|
//Any destructible part can be hit regardless of its HP. Multiple hit on same target is allowed.
|
2015-03-18 12:27:07 +02:00
|
|
|
EWallPart::EWallPart target = *RandomGeneratorUtil::nextItem(possibleTargets, env->getRandomGenerator());
|
2015-03-19 09:35:05 +02:00
|
|
|
|
|
|
|
auto & currentHP = parameters.cb->si.wallState;
|
|
|
|
|
|
|
|
if(currentHP.at(target) == EWallState::DESTROYED || currentHP.at(target) == EWallState::NONE)
|
|
|
|
continue;
|
|
|
|
|
2015-03-18 12:27:07 +02:00
|
|
|
CatapultAttack::AttackInfo attackInfo;
|
2015-03-19 09:35:05 +02:00
|
|
|
|
2015-03-18 12:27:07 +02:00
|
|
|
attackInfo.damageDealt = 1;
|
|
|
|
attackInfo.attackedPart = target;
|
2015-03-18 16:48:32 +02:00
|
|
|
attackInfo.destinationTile = parameters.cb->wallPartToBattleHex(target);
|
2015-03-19 09:35:05 +02:00
|
|
|
|
2015-03-18 12:27:07 +02:00
|
|
|
ca.attackedParts.push_back(attackInfo);
|
2015-03-19 09:35:05 +02:00
|
|
|
|
2015-03-18 13:12:26 +02:00
|
|
|
//removing creatures in turrets / keep if one is destroyed
|
|
|
|
BattleHex posRemove;
|
|
|
|
|
|
|
|
switch(target)
|
|
|
|
{
|
|
|
|
case EWallPart::KEEP:
|
|
|
|
posRemove = -2;
|
|
|
|
break;
|
|
|
|
case EWallPart::BOTTOM_TOWER:
|
|
|
|
posRemove = -3;
|
|
|
|
break;
|
|
|
|
case EWallPart::UPPER_TOWER:
|
|
|
|
posRemove = -4;
|
|
|
|
break;
|
2015-03-19 09:35:05 +02:00
|
|
|
}
|
|
|
|
|
2015-03-18 13:12:26 +02:00
|
|
|
if(posRemove != BattleHex::INVALID)
|
|
|
|
{
|
|
|
|
BattleStacksRemoved bsr;
|
|
|
|
for(auto & elem : parameters.cb->stacks)
|
|
|
|
{
|
|
|
|
if(elem->position == posRemove)
|
|
|
|
{
|
|
|
|
bsr.stackIDs.insert(elem->ID);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-03-19 08:54:53 +02:00
|
|
|
if(bsr.stackIDs.size() > 0)
|
2015-03-18 13:12:26 +02:00
|
|
|
env->sendAndApply(&bsr);
|
2015-03-18 13:28:34 +02:00
|
|
|
}
|
|
|
|
};
|
2015-03-19 09:35:05 +02:00
|
|
|
|
2015-03-18 12:27:07 +02:00
|
|
|
env->sendAndApply(&ca);
|
|
|
|
}
|
2015-02-02 11:22:19 +02:00
|
|
|
|
2016-09-18 15:30:03 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem EarthquakeMechanics::canBeCast(const CBattleInfoCallback * cb, const ECastingMode::ECastingMode mode, const ISpellCaster * caster) const
|
2015-03-18 15:39:07 +02:00
|
|
|
{
|
2016-09-18 15:30:03 +02:00
|
|
|
if(mode == ECastingMode::AFTER_ATTACK_CASTING || mode == ECastingMode::SPELL_LIKE_ATTACK || mode == ECastingMode::MAGIC_MIRROR)
|
|
|
|
{
|
|
|
|
logGlobal->warn("Invalid spell cast attempt: spell %s, mode %d", owner->name, mode); //should not even try to do it
|
|
|
|
return ESpellCastProblem::INVALID;
|
|
|
|
}
|
|
|
|
|
2015-03-18 15:39:07 +02:00
|
|
|
if(nullptr == cb->battleGetDefendedTown())
|
|
|
|
{
|
2015-03-19 09:35:05 +02:00
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
|
2015-03-18 15:39:07 +02:00
|
|
|
}
|
2015-03-19 09:35:05 +02:00
|
|
|
|
2015-03-18 15:39:07 +02:00
|
|
|
if(CGTownInstance::NONE == cb->battleGetDefendedTown()->fortLevel())
|
|
|
|
{
|
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
|
|
|
|
}
|
2016-02-15 12:34:37 +02:00
|
|
|
|
2016-03-01 06:32:02 +02:00
|
|
|
CSpell::TargetInfo ti(owner, caster->getSpellSchoolLevel(owner));
|
2015-09-19 15:51:06 +02:00
|
|
|
if(ti.smart)
|
2015-03-18 16:48:32 +02:00
|
|
|
{
|
|
|
|
//if spell targeting is smart, then only attacker can use it
|
2016-11-28 02:33:39 +02:00
|
|
|
if(cb->playerToSide(caster->getOwner()) != BattleSide::ATTACKER)
|
2015-03-18 16:48:32 +02:00
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
|
|
|
|
}
|
2015-03-19 09:35:05 +02:00
|
|
|
|
2016-03-01 14:14:27 +02:00
|
|
|
const auto attackableBattleHexes = cb->getAttackableBattleHexes();
|
|
|
|
|
|
|
|
if(attackableBattleHexes.empty())
|
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
|
|
|
|
|
2015-03-18 15:39:07 +02:00
|
|
|
return ESpellCastProblem::OK;
|
|
|
|
}
|
|
|
|
|
2016-09-05 10:36:25 +02:00
|
|
|
bool EarthquakeMechanics::requiresCreatureTarget() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-02 11:22:19 +02:00
|
|
|
///HypnotizeMechanics
|
2015-09-26 19:09:54 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem HypnotizeMechanics::isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
2015-09-26 19:09:54 +02:00
|
|
|
//todo: maybe do not resist on passive cast
|
2016-02-15 12:34:37 +02:00
|
|
|
if(nullptr != caster)
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
|
|
|
//TODO: what with other creatures casting hypnotize, Faerie Dragons style?
|
2016-11-18 13:27:20 +02:00
|
|
|
ui32 subjectHealth = obj->totalHealth();
|
2015-02-02 11:22:19 +02:00
|
|
|
//apply 'damage' bonus for hypnotize, including hero specialty
|
2016-11-18 13:27:20 +02:00
|
|
|
ui32 maxHealth = caster->getSpellBonus(owner, owner->calculateRawEffectValue(caster->getEffectLevel(owner), caster->getEffectPower(owner)), obj);
|
2015-02-02 11:22:19 +02:00
|
|
|
if (subjectHealth > maxHealth)
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
2015-02-26 19:59:18 +02:00
|
|
|
}
|
2015-02-02 11:22:19 +02:00
|
|
|
return DefaultSpellMechanics::isImmuneByStack(caster, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
///ObstacleMechanics
|
2016-09-04 04:15:37 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem ObstacleMechanics::canBeCast(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx) const
|
2016-03-04 18:11:42 +02:00
|
|
|
{
|
2016-11-28 02:33:39 +02:00
|
|
|
const si8 side = cb->playerToSide(ctx.caster->getOwner());
|
|
|
|
if(side < 0)
|
|
|
|
return ESpellCastProblem::INVALID;
|
2016-03-04 18:11:42 +02:00
|
|
|
|
2016-09-23 12:14:12 +02:00
|
|
|
bool hexesOutsideBattlefield = false;
|
2016-03-04 18:11:42 +02:00
|
|
|
|
2016-09-23 12:14:12 +02:00
|
|
|
auto tilesThatMustBeClear = owner->rangeInHexes(ctx.destination, ctx.schoolLvl, side, &hexesOutsideBattlefield);
|
2016-03-04 18:11:42 +02:00
|
|
|
|
2016-09-23 19:27:55 +02:00
|
|
|
for(const BattleHex & hex : tilesThatMustBeClear)
|
|
|
|
if(!isHexAviable(cb, hex, ctx.ti.clearAffected))
|
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
|
2016-03-04 18:11:42 +02:00
|
|
|
|
2016-09-23 19:27:55 +02:00
|
|
|
if(hexesOutsideBattlefield)
|
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
|
2016-09-23 12:14:12 +02:00
|
|
|
|
2016-09-23 19:27:55 +02:00
|
|
|
return ESpellCastProblem::OK;
|
|
|
|
}
|
2016-03-04 18:11:42 +02:00
|
|
|
|
2016-09-23 19:27:55 +02:00
|
|
|
bool ObstacleMechanics::isHexAviable(const CBattleInfoCallback * cb, const BattleHex & hex, const bool mustBeClear)
|
|
|
|
{
|
|
|
|
if(!hex.isAvailable())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!mustBeClear)
|
|
|
|
return true;
|
|
|
|
|
2016-09-24 10:25:29 +02:00
|
|
|
if(cb->battleGetStackByPos(hex, true))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto obst = cb->battleGetObstacleOnPos(hex, false);
|
|
|
|
if(obst /*&& obst->type != CObstacleInstance::MOAT*/)//todo: issue 2366, uncomment once obstacle tile sharing implemented
|
2016-09-23 19:27:55 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if(nullptr != cb->battleGetDefendedTown() && CGTownInstance::NONE != cb->battleGetDefendedTown()->fortLevel())
|
2016-09-23 12:14:12 +02:00
|
|
|
{
|
2016-09-23 19:27:55 +02:00
|
|
|
EWallPart::EWallPart part = cb->battleHexToWallPart(hex);
|
|
|
|
|
2016-09-24 03:55:48 +02:00
|
|
|
if(part == EWallPart::INVALID)
|
|
|
|
return true;//no fortification here
|
|
|
|
else if(static_cast<int>(part) < 0)
|
|
|
|
return false;//indestuctible part (cant be checked by battleGetWallState)
|
|
|
|
else if(part == EWallPart::BOTTOM_TOWER || part == EWallPart::UPPER_TOWER)
|
|
|
|
return false;//destructible, but should not be available
|
|
|
|
else if(cb->battleGetWallState(part) != EWallState::DESTROYED && cb->battleGetWallState(part) != EWallState::NONE)
|
|
|
|
return false;
|
2016-09-23 12:14:12 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:27:55 +02:00
|
|
|
return true;
|
2016-03-04 18:11:42 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 12:34:48 +02:00
|
|
|
void ObstacleMechanics::placeObstacle(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, const BattleHex & pos) const
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
2016-09-05 12:34:48 +02:00
|
|
|
const int obstacleIdToGive = parameters.cb->obstacles.size()+1;
|
2015-02-02 11:22:19 +02:00
|
|
|
|
2016-09-05 12:34:48 +02:00
|
|
|
auto obstacle = std::make_shared<SpellCreatedObstacle>();
|
|
|
|
setupObstacle(obstacle.get());
|
2015-02-02 11:22:19 +02:00
|
|
|
|
2016-09-05 12:34:48 +02:00
|
|
|
obstacle->pos = pos;
|
|
|
|
obstacle->casterSide = parameters.casterSide;
|
|
|
|
obstacle->ID = owner->id;
|
|
|
|
obstacle->spellLevel = parameters.effectLevel;
|
|
|
|
obstacle->casterSpellPower = parameters.effectPower;
|
|
|
|
obstacle->uniqueID = obstacleIdToGive;
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2016-09-05 12:34:48 +02:00
|
|
|
BattleObstaclePlaced bop;
|
|
|
|
bop.obstacle = obstacle;
|
|
|
|
env->sendAndApply(&bop);
|
|
|
|
}
|
2015-09-29 20:47:04 +02:00
|
|
|
|
2016-09-05 12:34:48 +02:00
|
|
|
///PatchObstacleMechanics
|
|
|
|
void PatchObstacleMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
|
|
|
|
{
|
|
|
|
std::vector<BattleHex> availableTiles;
|
|
|
|
for(int i = 0; i < GameConstants::BFIELD_SIZE; i += 1)
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
2016-09-05 12:34:48 +02:00
|
|
|
BattleHex hex = i;
|
2016-09-23 19:27:55 +02:00
|
|
|
if(isHexAviable(parameters.cb, hex, true))
|
2016-09-05 12:34:48 +02:00
|
|
|
availableTiles.push_back(hex);
|
|
|
|
}
|
2016-09-12 09:13:40 +02:00
|
|
|
RandomGeneratorUtil::randomShuffle(availableTiles, env->getRandomGenerator());
|
2016-09-05 12:34:48 +02:00
|
|
|
const int patchesForSkill[] = {4, 4, 6, 8};
|
|
|
|
const int patchesToPut = std::min<int>(patchesForSkill[parameters.spellLvl], availableTiles.size());
|
2015-02-02 11:22:19 +02:00
|
|
|
|
2016-09-05 12:34:48 +02:00
|
|
|
//land mines or quicksand patches are handled as spell created obstacles
|
|
|
|
for (int i = 0; i < patchesToPut; i++)
|
|
|
|
placeObstacle(env, parameters, availableTiles.at(i));
|
|
|
|
}
|
2015-02-02 11:22:19 +02:00
|
|
|
|
2016-10-31 14:38:53 +02:00
|
|
|
///LandMineMechanics
|
|
|
|
ESpellCastProblem::ESpellCastProblem LandMineMechanics::canBeCast(const CBattleInfoCallback * cb, const ECastingMode::ECastingMode mode, const ISpellCaster * caster) const
|
2016-09-24 10:25:29 +02:00
|
|
|
{
|
2016-10-31 14:38:53 +02:00
|
|
|
//LandMine are useless if enemy has native stack and can see mines, check for LandMine damage immunity is done in general way by CSpell
|
2016-11-28 02:33:39 +02:00
|
|
|
const si8 playerSide = cb->playerToSide(caster->getOwner());
|
|
|
|
if(playerSide < 0)
|
|
|
|
return ESpellCastProblem::INVALID;
|
|
|
|
|
|
|
|
const si8 otherSide = !playerSide;
|
2016-09-24 10:25:29 +02:00
|
|
|
|
|
|
|
if(cb->battleHasNativeStack(otherSide))
|
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
|
|
|
|
|
|
|
|
return DefaultSpellMechanics::canBeCast(cb, mode, caster);
|
|
|
|
}
|
|
|
|
|
2016-09-05 12:34:48 +02:00
|
|
|
bool LandMineMechanics::requiresCreatureTarget() const
|
|
|
|
{
|
|
|
|
return true;
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 12:34:48 +02:00
|
|
|
void LandMineMechanics::setupObstacle(SpellCreatedObstacle * obstacle) const
|
2016-09-05 10:36:25 +02:00
|
|
|
{
|
2016-09-05 12:34:48 +02:00
|
|
|
obstacle->obstacleType = CObstacleInstance::LAND_MINE;
|
|
|
|
obstacle->turnsRemaining = -1;
|
|
|
|
obstacle->visibleForAnotherSide = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
///QuicksandMechanics
|
|
|
|
bool QuicksandMechanics::requiresCreatureTarget() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuicksandMechanics::setupObstacle(SpellCreatedObstacle * obstacle) const
|
|
|
|
{
|
|
|
|
obstacle->obstacleType = CObstacleInstance::QUICKSAND;
|
|
|
|
obstacle->turnsRemaining = -1;
|
|
|
|
obstacle->visibleForAnotherSide = false;
|
2016-09-05 10:36:25 +02:00
|
|
|
}
|
|
|
|
|
2015-02-02 11:22:19 +02:00
|
|
|
///WallMechanics
|
|
|
|
std::vector<BattleHex> WallMechanics::rangeInHexes(BattleHex centralHex, ui8 schoolLvl, ui8 side, bool * outDroppedHexes) const
|
|
|
|
{
|
2015-02-26 19:59:18 +02:00
|
|
|
std::vector<BattleHex> ret;
|
|
|
|
|
2015-02-02 11:22:19 +02:00
|
|
|
//Special case - shape of obstacle depends on caster's side
|
|
|
|
//TODO make it possible through spell config
|
|
|
|
|
|
|
|
BattleHex::EDir firstStep, secondStep;
|
|
|
|
if(side)
|
|
|
|
{
|
|
|
|
firstStep = BattleHex::TOP_LEFT;
|
|
|
|
secondStep = BattleHex::TOP_RIGHT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
firstStep = BattleHex::TOP_RIGHT;
|
|
|
|
secondStep = BattleHex::TOP_LEFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Adds hex to the ret if it's valid. Otherwise sets output arg flag if given.
|
|
|
|
auto addIfValid = [&](BattleHex hex)
|
|
|
|
{
|
|
|
|
if(hex.isValid())
|
|
|
|
ret.push_back(hex);
|
|
|
|
else if(outDroppedHexes)
|
|
|
|
*outDroppedHexes = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
ret.push_back(centralHex);
|
|
|
|
addIfValid(centralHex.moveInDir(firstStep, false));
|
|
|
|
if(schoolLvl >= 2) //advanced versions of fire wall / force field cotnains of 3 hexes
|
|
|
|
addIfValid(centralHex.moveInDir(secondStep, false)); //moveInDir function modifies subject hex
|
|
|
|
|
2015-02-26 19:59:18 +02:00
|
|
|
return ret;
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 12:34:48 +02:00
|
|
|
///FireWallMechanics
|
|
|
|
bool FireWallMechanics::requiresCreatureTarget() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FireWallMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
|
|
|
|
{
|
|
|
|
const BattleHex destination = parameters.getFirstDestinationHex();
|
|
|
|
|
|
|
|
if(!destination.isValid())
|
|
|
|
{
|
|
|
|
env->complain("Invalid destination for FIRE_WALL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//firewall is build from multiple obstacles - one fire piece for each affected hex
|
|
|
|
auto affectedHexes = owner->rangeInHexes(destination, parameters.spellLvl, parameters.casterSide);
|
|
|
|
for(BattleHex hex : affectedHexes)
|
|
|
|
placeObstacle(env, parameters, hex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FireWallMechanics::setupObstacle(SpellCreatedObstacle * obstacle) const
|
|
|
|
{
|
|
|
|
obstacle->obstacleType = CObstacleInstance::FIRE_WALL;
|
|
|
|
obstacle->turnsRemaining = 2;
|
|
|
|
obstacle->visibleForAnotherSide = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
///ForceFieldMechanics
|
|
|
|
bool ForceFieldMechanics::requiresCreatureTarget() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForceFieldMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
|
|
|
|
{
|
|
|
|
const BattleHex destination = parameters.getFirstDestinationHex();
|
|
|
|
|
|
|
|
if(!destination.isValid())
|
|
|
|
{
|
|
|
|
env->complain("Invalid destination for FORCE_FIELD");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
placeObstacle(env, parameters, destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForceFieldMechanics::setupObstacle(SpellCreatedObstacle * obstacle) const
|
|
|
|
{
|
|
|
|
obstacle->obstacleType = CObstacleInstance::FORCE_FIELD;
|
|
|
|
obstacle->turnsRemaining = 2;
|
|
|
|
obstacle->visibleForAnotherSide = true;
|
|
|
|
}
|
|
|
|
|
2015-02-02 11:22:19 +02:00
|
|
|
///RemoveObstacleMechanics
|
2015-09-26 20:35:30 +02:00
|
|
|
void RemoveObstacleMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
2015-09-29 20:47:04 +02:00
|
|
|
if(auto obstacleToRemove = parameters.cb->battleGetObstacleOnPos(parameters.getFirstDestinationHex(), false))
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
2016-03-01 11:20:38 +02:00
|
|
|
if(canRemove(obstacleToRemove.get(), parameters.spellLvl))
|
|
|
|
{
|
|
|
|
ObstaclesRemoved obr;
|
|
|
|
obr.obstacles.insert(obstacleToRemove->uniqueID);
|
|
|
|
env->sendAndApply(&obr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
env->complain("Cant remove this obstacle!");
|
|
|
|
}
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
else
|
2015-02-26 19:59:18 +02:00
|
|
|
env->complain("There's no obstacle to remove!");
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 15:30:03 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem RemoveObstacleMechanics::canBeCast(const CBattleInfoCallback * cb, const ECastingMode::ECastingMode mode, const ISpellCaster * caster) const
|
2016-03-01 10:55:09 +02:00
|
|
|
{
|
2016-09-18 15:30:03 +02:00
|
|
|
if(mode == ECastingMode::AFTER_ATTACK_CASTING || mode == ECastingMode::SPELL_LIKE_ATTACK || mode == ECastingMode::MAGIC_MIRROR)
|
|
|
|
{
|
|
|
|
logGlobal->warn("Invalid spell cast attempt: spell %s, mode %d", owner->name, mode); //should not even try to do it
|
|
|
|
return ESpellCastProblem::INVALID;
|
|
|
|
}
|
|
|
|
|
2016-03-01 11:20:38 +02:00
|
|
|
const int spellLevel = caster->getSpellSchoolLevel(owner);
|
|
|
|
|
|
|
|
for(auto obstacle : cb->battleGetAllObstacles())
|
|
|
|
if(canRemove(obstacle.get(), spellLevel))
|
|
|
|
return ESpellCastProblem::OK;
|
2016-03-01 10:55:09 +02:00
|
|
|
|
2016-03-01 11:20:38 +02:00
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
|
|
|
|
}
|
|
|
|
|
2016-09-04 04:15:37 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem RemoveObstacleMechanics::canBeCast(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx) const
|
2016-03-01 11:20:38 +02:00
|
|
|
{
|
2016-09-04 04:15:37 +02:00
|
|
|
if(auto obstacle = cb->battleGetObstacleOnPos(ctx.destination, false))
|
2016-03-01 11:20:38 +02:00
|
|
|
if(canRemove(obstacle.get(), ctx.schoolLvl))
|
|
|
|
return ESpellCastProblem::OK;
|
|
|
|
|
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoveObstacleMechanics::canRemove(const CObstacleInstance * obstacle, const int spellLevel) const
|
|
|
|
{
|
|
|
|
switch (obstacle->obstacleType)
|
2016-03-01 10:55:09 +02:00
|
|
|
{
|
2016-03-01 11:20:38 +02:00
|
|
|
case CObstacleInstance::ABSOLUTE_OBSTACLE: //cliff-like obstacles can't be removed
|
|
|
|
case CObstacleInstance::MOAT:
|
|
|
|
return false;
|
|
|
|
case CObstacleInstance::USUAL:
|
|
|
|
return true;
|
|
|
|
case CObstacleInstance::FIRE_WALL:
|
|
|
|
if(spellLevel >= 2)
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case CObstacleInstance::QUICKSAND:
|
|
|
|
case CObstacleInstance::LAND_MINE:
|
|
|
|
case CObstacleInstance::FORCE_FIELD:
|
|
|
|
if(spellLevel >= 3)
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-03-01 10:55:09 +02:00
|
|
|
}
|
2016-03-01 11:20:38 +02:00
|
|
|
return false;
|
2016-03-01 10:55:09 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 10:36:25 +02:00
|
|
|
bool RemoveObstacleMechanics::requiresCreatureTarget() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-01 10:55:09 +02:00
|
|
|
///RisingSpellMechanics
|
2015-09-16 06:56:02 +02:00
|
|
|
HealingSpellMechanics::EHealLevel RisingSpellMechanics::getHealLevel(int effectLevel) const
|
|
|
|
{
|
|
|
|
//this may be even distinct class
|
|
|
|
if((effectLevel <= 1) && (owner->id == SpellID::RESURRECTION))
|
|
|
|
return EHealLevel::RESURRECT;
|
2016-02-15 12:34:37 +02:00
|
|
|
|
2015-09-16 06:56:02 +02:00
|
|
|
return EHealLevel::TRUE_RESURRECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
///SacrificeMechanics
|
2016-09-18 15:30:03 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem SacrificeMechanics::canBeCast(const CBattleInfoCallback * cb, const ECastingMode::ECastingMode mode, const ISpellCaster * caster) const
|
2015-04-02 23:35:29 +02:00
|
|
|
{
|
2016-09-18 15:30:03 +02:00
|
|
|
if(mode == ECastingMode::AFTER_ATTACK_CASTING || mode == ECastingMode::SPELL_LIKE_ATTACK || mode == ECastingMode::MAGIC_MIRROR)
|
|
|
|
{
|
|
|
|
logGlobal->warn("Invalid spell cast attempt: spell %s, mode %d", owner->name, mode); //should not even try to do it
|
|
|
|
return ESpellCastProblem::INVALID;
|
|
|
|
}
|
|
|
|
|
2015-04-02 23:35:29 +02:00
|
|
|
// for sacrifice we have to check for 2 targets (one dead to resurrect and one living to destroy)
|
2015-04-03 02:02:16 +02:00
|
|
|
|
2015-04-02 23:35:29 +02:00
|
|
|
bool targetExists = false;
|
|
|
|
bool targetToSacrificeExists = false;
|
2016-02-15 12:34:37 +02:00
|
|
|
|
2015-04-02 23:35:29 +02:00
|
|
|
for(const CStack * stack : cb->battleGetAllStacks())
|
|
|
|
{
|
|
|
|
//using isImmuneBy directly as this mechanics does not have overridden immunity check
|
|
|
|
//therefore we do not need to check caster and casting mode
|
|
|
|
//TODO: check that we really should check immunity for both stacks
|
2015-09-17 12:23:13 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem res = owner->internalIsImmune(caster, stack);
|
2015-09-15 03:31:43 +02:00
|
|
|
const bool immune = ESpellCastProblem::OK != res && ESpellCastProblem::NOT_DECIDED != res;
|
2016-03-01 06:32:02 +02:00
|
|
|
const bool casterStack = stack->owner == caster->getOwner();
|
2015-04-02 23:35:29 +02:00
|
|
|
|
|
|
|
if(!immune && casterStack)
|
|
|
|
{
|
|
|
|
if(stack->alive())
|
|
|
|
targetToSacrificeExists = true;
|
|
|
|
else
|
|
|
|
targetExists = true;
|
|
|
|
if(targetExists && targetToSacrificeExists)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 02:02:16 +02:00
|
|
|
|
2015-04-02 23:35:29 +02:00
|
|
|
if(targetExists && targetToSacrificeExists)
|
|
|
|
return ESpellCastProblem::OK;
|
|
|
|
else
|
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-26 20:35:30 +02:00
|
|
|
void SacrificeMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
2015-10-01 01:36:00 +02:00
|
|
|
const CStack * victim = nullptr;
|
|
|
|
if(parameters.destinations.size() == 2)
|
|
|
|
{
|
|
|
|
victim = parameters.destinations[1].stackValue;
|
|
|
|
}
|
2016-09-06 11:26:01 +02:00
|
|
|
|
2015-10-01 01:36:00 +02:00
|
|
|
if(nullptr == victim)
|
|
|
|
{
|
|
|
|
env->complain("SacrificeMechanics: No stack to sacrifice");
|
|
|
|
return;
|
|
|
|
}
|
2015-10-05 19:33:05 +02:00
|
|
|
//resurrect target after basic checks
|
|
|
|
RisingSpellMechanics::applyBattleEffects(env, parameters, ctx);
|
2015-10-06 02:59:05 +02:00
|
|
|
//it is safe to remove even active stack
|
2015-02-02 11:22:19 +02:00
|
|
|
BattleStacksRemoved bsr;
|
2015-10-01 01:36:00 +02:00
|
|
|
bsr.stackIDs.insert(victim->ID);
|
2015-02-02 11:22:19 +02:00
|
|
|
env->sendAndApply(&bsr);
|
|
|
|
}
|
|
|
|
|
2015-09-16 14:43:39 +02:00
|
|
|
int SacrificeMechanics::calculateHealedHP(const SpellCastEnvironment* env, const BattleSpellCastParameters& parameters, SpellCastContext& ctx) const
|
2015-09-16 08:44:18 +02:00
|
|
|
{
|
2015-10-01 01:36:00 +02:00
|
|
|
const CStack * victim = nullptr;
|
|
|
|
|
|
|
|
if(parameters.destinations.size() == 2)
|
|
|
|
{
|
|
|
|
victim = parameters.destinations[1].stackValue;
|
|
|
|
}
|
2016-09-06 11:26:01 +02:00
|
|
|
|
2015-10-01 01:36:00 +02:00
|
|
|
if(nullptr == victim)
|
|
|
|
{
|
|
|
|
env->complain("SacrificeMechanics: No stack to sacrifice");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-18 14:27:22 +02:00
|
|
|
return (parameters.effectPower + victim->MaxHealth() + owner->getPower(parameters.effectLevel)) * victim->count;
|
2015-09-16 08:44:18 +02:00
|
|
|
}
|
2015-02-02 11:22:19 +02:00
|
|
|
|
2016-09-05 10:36:25 +02:00
|
|
|
bool SacrificeMechanics::requiresCreatureTarget() const
|
|
|
|
{
|
|
|
|
return false;//canBeCast do all target existence checks
|
|
|
|
}
|
|
|
|
|
2015-02-02 11:22:19 +02:00
|
|
|
///SpecialRisingSpellMechanics
|
2016-09-04 04:15:37 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem SpecialRisingSpellMechanics::canBeCast(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx) const
|
2016-03-04 15:42:55 +02:00
|
|
|
{
|
2016-09-29 17:55:07 +02:00
|
|
|
//find alive possible target
|
|
|
|
const CStack * stackToHeal = cb->getStackIf([ctx, this](const CStack * s)
|
2016-03-04 15:42:55 +02:00
|
|
|
{
|
2016-09-29 18:37:59 +02:00
|
|
|
const bool ownerMatches = !ctx.ti.smart || s->owner == ctx.caster->getOwner();
|
2016-03-04 15:42:55 +02:00
|
|
|
|
2016-09-29 17:55:07 +02:00
|
|
|
return ownerMatches && s->isValidTarget(false) && s->coversPos(ctx.destination) && ESpellCastProblem::OK == owner->isImmuneByStack(ctx.caster, s);
|
2016-03-04 15:42:55 +02:00
|
|
|
});
|
|
|
|
|
2016-09-29 17:55:07 +02:00
|
|
|
if(nullptr == stackToHeal)
|
2016-09-29 09:45:22 +02:00
|
|
|
{
|
2016-09-29 17:55:07 +02:00
|
|
|
//find dead possible target if there is no alive target
|
|
|
|
stackToHeal = cb->getStackIf([ctx, this](const CStack * s)
|
|
|
|
{
|
2016-09-29 18:37:59 +02:00
|
|
|
const bool ownerMatches = !ctx.ti.smart || s->owner == ctx.caster->getOwner();
|
2016-09-29 09:45:22 +02:00
|
|
|
|
2016-09-29 17:55:07 +02:00
|
|
|
return ownerMatches && s->isValidTarget(true) && s->coversPos(ctx.destination) && ESpellCastProblem::OK == owner->isImmuneByStack(ctx.caster, s);
|
|
|
|
});
|
|
|
|
|
|
|
|
//we have found dead target
|
|
|
|
if(nullptr != stackToHeal)
|
|
|
|
{
|
|
|
|
for(const BattleHex & hex : stackToHeal->getHexes())
|
|
|
|
{
|
|
|
|
const CStack * other = cb->getStackIf([hex, stackToHeal](const CStack * s)
|
|
|
|
{
|
|
|
|
return s->isValidTarget(false) && s->coversPos(hex) && s != stackToHeal;
|
|
|
|
});
|
|
|
|
if(nullptr != other)
|
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;//alive stack blocks resurrection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-29 09:45:22 +02:00
|
|
|
|
2016-09-29 17:55:07 +02:00
|
|
|
if(nullptr == stackToHeal)
|
2016-03-04 15:42:55 +02:00
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
|
|
|
|
|
|
|
|
return ESpellCastProblem::OK;
|
|
|
|
}
|
|
|
|
|
2015-09-26 19:09:54 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem SpecialRisingSpellMechanics::isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
|
|
|
// following does apply to resurrect and animate dead(?) only
|
|
|
|
// for sacrifice health calculation and health limit check don't matter
|
|
|
|
|
|
|
|
if(obj->count >= obj->baseAmount)
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-09-16 05:04:21 +02:00
|
|
|
//FIXME: Archangels can cast immune stack and this should be applied for them and not hero
|
2016-02-15 12:34:37 +02:00
|
|
|
// if(caster)
|
2015-09-16 05:11:13 +02:00
|
|
|
// {
|
|
|
|
// auto maxHealth = calculateHealedHP(caster, obj, nullptr);
|
|
|
|
// if (maxHealth < obj->MaxHealth()) //must be able to rise at least one full creature
|
|
|
|
// return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
// }
|
2015-02-26 19:59:18 +02:00
|
|
|
|
|
|
|
return DefaultSpellMechanics::isImmuneByStack(caster,obj);
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///SummonMechanics
|
2016-09-18 15:30:03 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem SummonMechanics::canBeCast(const CBattleInfoCallback * cb, const ECastingMode::ECastingMode mode, const ISpellCaster * caster) const
|
2015-04-02 23:02:15 +02:00
|
|
|
{
|
2016-09-18 15:30:03 +02:00
|
|
|
if(mode == ECastingMode::AFTER_ATTACK_CASTING || mode == ECastingMode::SPELL_LIKE_ATTACK || mode == ECastingMode::MAGIC_MIRROR)
|
|
|
|
{
|
|
|
|
logGlobal->warn("Invalid spell cast attempt: spell %s, mode %d", owner->name, mode); //should not even try to do it
|
|
|
|
return ESpellCastProblem::INVALID;
|
|
|
|
}
|
|
|
|
|
2015-04-11 11:09:48 +02:00
|
|
|
//check if there are summoned elementals of other type
|
2016-02-15 12:34:37 +02:00
|
|
|
|
2016-03-01 06:32:02 +02:00
|
|
|
auto otherSummoned = cb->battleGetStacksIf([caster, this](const CStack * st)
|
2015-04-02 23:02:15 +02:00
|
|
|
{
|
2016-03-01 06:32:02 +02:00
|
|
|
return (st->owner == caster->getOwner())
|
2015-04-11 11:09:48 +02:00
|
|
|
&& (vstd::contains(st->state, EBattleStackState::SUMMONED))
|
2016-03-01 03:16:51 +02:00
|
|
|
&& (!vstd::contains(st->state, EBattleStackState::CLONED))
|
2015-04-11 11:09:48 +02:00
|
|
|
&& (st->getCreature()->idNumber != creatureToSummon);
|
|
|
|
});
|
2016-02-15 12:34:37 +02:00
|
|
|
|
2015-04-11 11:09:48 +02:00
|
|
|
if(!otherSummoned.empty())
|
2016-02-15 12:34:37 +02:00
|
|
|
return ESpellCastProblem::ANOTHER_ELEMENTAL_SUMMONED;
|
2015-04-02 23:02:15 +02:00
|
|
|
|
|
|
|
return ESpellCastProblem::OK;
|
|
|
|
}
|
|
|
|
|
2015-09-26 20:35:30 +02:00
|
|
|
void SummonMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
|
|
|
BattleStackAdded bsa;
|
2015-04-11 11:09:48 +02:00
|
|
|
bsa.creID = creatureToSummon;
|
2015-02-02 11:22:19 +02:00
|
|
|
bsa.attacker = !(bool)parameters.casterSide;
|
|
|
|
bsa.summoned = true;
|
2015-04-11 11:09:48 +02:00
|
|
|
bsa.pos = parameters.cb->getAvaliableHex(creatureToSummon, !(bool)parameters.casterSide); //TODO: unify it
|
2015-02-02 11:22:19 +02:00
|
|
|
|
|
|
|
//TODO stack casting -> probably power will be zero; set the proper number of creatures manually
|
2015-03-30 18:12:40 +02:00
|
|
|
int percentBonus = parameters.casterHero ? parameters.casterHero->valOfBonuses(Bonus::SPECIFIC_SPELL_DAMAGE, owner->id.toEnum()) : 0;
|
2015-02-02 11:22:19 +02:00
|
|
|
|
2015-09-17 04:47:32 +02:00
|
|
|
bsa.amount = parameters.effectPower
|
2015-02-02 11:22:19 +02:00
|
|
|
* owner->getPower(parameters.spellLvl)
|
|
|
|
* (100 + percentBonus) / 100.0; //new feature - percentage bonus
|
|
|
|
if(bsa.amount)
|
|
|
|
env->sendAndApply(&bsa);
|
|
|
|
else
|
2015-02-26 19:59:18 +02:00
|
|
|
env->complain("Summoning didn't summon any!");
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 10:36:25 +02:00
|
|
|
bool SummonMechanics::requiresCreatureTarget() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-02 11:22:19 +02:00
|
|
|
///TeleportMechanics
|
2015-09-26 20:35:30 +02:00
|
|
|
void TeleportMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
|
2015-02-02 11:22:19 +02:00
|
|
|
{
|
2015-09-30 21:38:50 +02:00
|
|
|
if(parameters.destinations.size() == 2)
|
|
|
|
{
|
2016-09-06 11:26:01 +02:00
|
|
|
//first destination hex to move to
|
|
|
|
const BattleHex destination = parameters.destinations[0].hexValue;
|
|
|
|
if(!destination.isValid())
|
2015-09-30 21:38:50 +02:00
|
|
|
{
|
2016-09-06 11:26:01 +02:00
|
|
|
env->complain("TeleportMechanics: invalid teleport destination");
|
2015-09-30 21:38:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-09-06 11:26:01 +02:00
|
|
|
|
|
|
|
//second destination creature to move
|
|
|
|
const CStack * target = parameters.destinations[1].stackValue;
|
|
|
|
if(nullptr == target)
|
2015-09-30 21:38:50 +02:00
|
|
|
{
|
2016-09-06 11:26:01 +02:00
|
|
|
env->complain("TeleportMechanics: no stack to teleport");
|
2016-02-15 12:34:37 +02:00
|
|
|
return;
|
2015-09-30 21:38:50 +02:00
|
|
|
}
|
2016-09-06 11:26:01 +02:00
|
|
|
|
2016-09-24 08:27:58 +02:00
|
|
|
if(!parameters.cb->battleCanTeleportTo(target, destination, parameters.effectLevel))
|
|
|
|
{
|
|
|
|
env->complain("TeleportMechanics: forbidden teleport");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-30 21:38:50 +02:00
|
|
|
BattleStackMoved bsm;
|
|
|
|
bsm.distance = -1;
|
|
|
|
bsm.stack = target->ID;
|
|
|
|
std::vector<BattleHex> tiles;
|
|
|
|
tiles.push_back(destination);
|
|
|
|
bsm.tilesToMove = tiles;
|
|
|
|
bsm.teleporting = true;
|
2016-02-15 12:34:37 +02:00
|
|
|
env->sendAndApply(&bsm);
|
2015-09-30 21:38:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-06 11:26:01 +02:00
|
|
|
env->complain("TeleportMechanics: 2 destinations required.");
|
|
|
|
return;
|
2016-02-15 12:34:37 +02:00
|
|
|
}
|
2015-02-02 11:22:19 +02:00
|
|
|
}
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2016-09-18 15:30:03 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem TeleportMechanics::canBeCast(const CBattleInfoCallback* cb, const ECastingMode::ECastingMode mode, const ISpellCaster * caster) const
|
|
|
|
{
|
|
|
|
if(mode == ECastingMode::AFTER_ATTACK_CASTING || mode == ECastingMode::SPELL_LIKE_ATTACK || mode == ECastingMode::MAGIC_MIRROR)
|
|
|
|
{
|
|
|
|
logGlobal->warn("Invalid spell cast attempt: spell %s, mode %d", owner->name, mode); //should not even try to do it
|
|
|
|
return ESpellCastProblem::INVALID;
|
|
|
|
}
|
2015-09-30 21:38:50 +02:00
|
|
|
|
2016-09-18 15:30:03 +02:00
|
|
|
return DefaultSpellMechanics::canBeCast(cb, mode, caster);
|
|
|
|
}
|