2017-07-20 06:08:49 +02:00
|
|
|
/*
|
|
|
|
* UnitEffect.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 "UnitEffect.h"
|
|
|
|
|
|
|
|
#include "../ISpellMechanics.h"
|
|
|
|
|
2023-04-30 21:55:04 +02:00
|
|
|
#include "../../bonuses/BonusSelector.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
#include "../../battle/CBattleInfoCallback.h"
|
|
|
|
#include "../../battle/Unit.h"
|
|
|
|
#include "../../serializer/JsonSerializeFormat.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
namespace spells
|
|
|
|
{
|
|
|
|
namespace effects
|
|
|
|
{
|
|
|
|
|
|
|
|
void UnitEffect::adjustTargetTypes(std::vector<TargetType> & types) const
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnitEffect::adjustAffectedHexes(std::set<BattleHex> & hexes, const Mechanics * m, const Target & spellTarget) const
|
|
|
|
{
|
2023-02-07 00:40:01 +02:00
|
|
|
for(const auto & destnation : spellTarget)
|
2017-07-20 06:08:49 +02:00
|
|
|
hexes.insert(destnation.hexValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnitEffect::applicable(Problem & problem, const Mechanics * m) const
|
|
|
|
{
|
|
|
|
//stack effect is applicable in general if there is at least one smart target
|
|
|
|
|
2023-03-19 19:52:07 +02:00
|
|
|
auto mainFilter = std::bind(&UnitEffect::getStackFilter, this, m, false, _1);
|
2017-07-20 06:08:49 +02:00
|
|
|
auto predicate = std::bind(&UnitEffect::eraseByImmunityFilter, this, m, _1);
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
auto targets = m->battle()->battleGetUnitsIf(mainFilter);
|
2017-07-20 06:08:49 +02:00
|
|
|
vstd::erase_if(targets, predicate);
|
|
|
|
if(targets.empty())
|
2023-03-22 11:04:28 +02:00
|
|
|
return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnitEffect::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
|
|
|
|
{
|
2023-03-19 19:52:07 +02:00
|
|
|
//stack effect is applicable if it affects at least one target (smartness should not be checked)
|
|
|
|
//assume target correctly transformed, just reapply filter
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2023-02-07 00:40:01 +02:00
|
|
|
for(const auto & item : target)
|
2017-07-20 06:08:49 +02:00
|
|
|
if(item.unitValue)
|
2023-03-19 19:52:07 +02:00
|
|
|
if(getStackFilter(m, false, item.unitValue))
|
2017-07-20 06:08:49 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnitEffect::getStackFilter(const Mechanics * m, bool alwaysSmart, const battle::Unit * s) const
|
|
|
|
{
|
|
|
|
return isValidTarget(m, s) && isSmartTarget(m, s, alwaysSmart);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnitEffect::eraseByImmunityFilter(const Mechanics * m, const battle::Unit * s) const
|
|
|
|
{
|
|
|
|
return !isReceptive(m, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectTarget UnitEffect::filterTarget(const Mechanics * m, const EffectTarget & target) const
|
|
|
|
{
|
|
|
|
EffectTarget res;
|
|
|
|
vstd::copy_if(target, std::back_inserter(res), [this, m](const Destination & d)
|
|
|
|
{
|
2019-03-20 21:58:15 +02:00
|
|
|
return d.unitValue && isValidTarget(m, d.unitValue) && isReceptive(m, d.unitValue);
|
2017-07-20 06:08:49 +02:00
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectTarget UnitEffect::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
|
|
|
|
{
|
|
|
|
if(chainLength > 1)
|
|
|
|
return transformTargetByChain(m, aimPoint, spellTarget);
|
|
|
|
else
|
|
|
|
return transformTargetByRange(m, aimPoint, spellTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectTarget UnitEffect::transformTargetByRange(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
|
|
|
|
{
|
|
|
|
auto mainFilter = std::bind(&UnitEffect::getStackFilter, this, m, false, _1);
|
|
|
|
|
|
|
|
Target spellTargetCopy(spellTarget);
|
|
|
|
|
2022-12-18 19:00:06 +02:00
|
|
|
// make sure that we have valid target with valid aim, even if spell have invalid range configured
|
|
|
|
// TODO: check than spell range is actually not valid
|
|
|
|
// also hackfix for banned creature massive spells
|
|
|
|
// FIXME: potentially breaking change: aimPoint may NOT be in Target - example: frost ring
|
|
|
|
if(!aimPoint.empty() && spellTarget.empty())
|
2017-07-20 06:08:49 +02:00
|
|
|
spellTargetCopy.insert(spellTargetCopy.begin(), Destination(aimPoint.front()));
|
|
|
|
|
|
|
|
std::set<const battle::Unit *> targets;
|
|
|
|
|
|
|
|
if(m->isMassive())
|
|
|
|
{
|
|
|
|
//ignore spellTarget and add all stacks
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
auto units = m->battle()->battleGetUnitsIf(mainFilter);
|
2023-02-07 00:40:01 +02:00
|
|
|
for(const auto *unit : units)
|
2017-07-20 06:08:49 +02:00
|
|
|
targets.insert(unit);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//process each tile
|
|
|
|
for(const Destination & dest : spellTargetCopy)
|
|
|
|
{
|
|
|
|
if(dest.unitValue)
|
|
|
|
{
|
|
|
|
if(mainFilter(dest.unitValue))
|
|
|
|
targets.insert(dest.unitValue);
|
|
|
|
}
|
|
|
|
else if(dest.hexValue.isValid())
|
|
|
|
{
|
|
|
|
//select one unit on tile, prefer alive one
|
|
|
|
const battle::Unit * targetOnTile = nullptr;
|
|
|
|
|
|
|
|
auto predicate = [&](const battle::Unit * unit)
|
|
|
|
{
|
|
|
|
return unit->coversPos(dest.hexValue) && mainFilter(unit);
|
|
|
|
};
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
auto units = m->battle()->battleGetUnitsIf(predicate);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2023-02-07 00:40:01 +02:00
|
|
|
for(const auto *unit : units)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
if(unit->alive())
|
|
|
|
{
|
|
|
|
targetOnTile = unit;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(targetOnTile == nullptr && !units.empty())
|
|
|
|
targetOnTile = units.front();
|
|
|
|
|
|
|
|
if(targetOnTile)
|
|
|
|
targets.insert(targetOnTile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logGlobal->debug("Invalid destination in spell Target");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto predicate = std::bind(&UnitEffect::eraseByImmunityFilter, this, m, _1);
|
|
|
|
|
|
|
|
vstd::erase_if(targets, predicate);
|
|
|
|
|
|
|
|
if(m->alwaysHitFirstTarget())
|
|
|
|
{
|
|
|
|
if(!aimPoint.empty() && aimPoint.front().unitValue)
|
|
|
|
targets.insert(aimPoint.front().unitValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectTarget effectTarget;
|
|
|
|
|
2023-02-07 00:40:01 +02:00
|
|
|
for(const auto *s : targets)
|
2017-07-20 06:08:49 +02:00
|
|
|
effectTarget.push_back(Destination(s));
|
|
|
|
|
|
|
|
return effectTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectTarget UnitEffect::transformTargetByChain(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
|
|
|
|
{
|
|
|
|
EffectTarget byRange = transformTargetByRange(m, aimPoint, spellTarget);
|
|
|
|
|
|
|
|
if(byRange.empty())
|
|
|
|
{
|
|
|
|
return EffectTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Destination & mainDestination = byRange.front();
|
|
|
|
|
|
|
|
if(!mainDestination.hexValue.isValid())
|
|
|
|
{
|
|
|
|
return EffectTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<BattleHex> possibleHexes;
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
auto possibleTargets = m->battle()->battleGetUnitsIf([&](const battle::Unit * unit) -> bool
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
return isValidTarget(m, unit);
|
|
|
|
});
|
|
|
|
|
2023-02-07 00:40:01 +02:00
|
|
|
for(const auto *unit : possibleTargets)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
for(auto hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
|
|
|
|
possibleHexes.insert(hex);
|
|
|
|
}
|
|
|
|
|
|
|
|
BattleHex destHex = mainDestination.hexValue;
|
|
|
|
EffectTarget effectTarget;
|
|
|
|
|
|
|
|
for(int32_t targetIndex = 0; targetIndex < chainLength; ++targetIndex)
|
|
|
|
{
|
2023-02-07 00:40:01 +02:00
|
|
|
const auto *unit = m->battle()->battleGetUnitByPos(destHex, true);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
if(!unit)
|
|
|
|
break;
|
|
|
|
if(m->alwaysHitFirstTarget() && targetIndex == 0)
|
|
|
|
effectTarget.emplace_back(unit);
|
|
|
|
else if(isReceptive(m, unit) && isValidTarget(m, unit))
|
|
|
|
effectTarget.emplace_back(unit);
|
|
|
|
else
|
|
|
|
effectTarget.emplace_back();
|
|
|
|
|
|
|
|
for(auto hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
|
|
|
|
possibleHexes.erase(hex);
|
|
|
|
|
|
|
|
if(possibleHexes.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
destHex = BattleHex::getClosestTile(unit->unitSide(), destHex, possibleHexes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return effectTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnitEffect::isValidTarget(const Mechanics * m, const battle::Unit * unit) const
|
|
|
|
{
|
|
|
|
// TODO: override in rising effect
|
|
|
|
// TODO: check absolute immunity here
|
|
|
|
|
|
|
|
return unit->isValidTarget(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnitEffect::isReceptive(const Mechanics * m, const battle::Unit * unit) const
|
|
|
|
{
|
|
|
|
if(ignoreImmunity)
|
|
|
|
{
|
|
|
|
//ignore all immunities, except specific absolute immunity(VCMI addition)
|
|
|
|
|
|
|
|
//SPELL_IMMUNITY absolute case
|
|
|
|
std::stringstream cachingStr;
|
2023-05-01 00:20:01 +02:00
|
|
|
cachingStr << "type_" << vstd::to_underlying(BonusType::SPELL_IMMUNITY) << "subtype_" << m->getSpellIndex() << "addInfo_1";
|
2023-10-21 13:50:42 +02:00
|
|
|
return !unit->hasBonus(Selector::typeSubtypeInfo(BonusType::SPELL_IMMUNITY, BonusSubtypeID(m->getSpellId()), 1), cachingStr.str());
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return m->isReceptive(unit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnitEffect::isSmartTarget(const Mechanics * m, const battle::Unit * unit, bool alwaysSmart) const
|
|
|
|
{
|
|
|
|
const bool smart = m->isSmart() || alwaysSmart;
|
|
|
|
const bool ignoreOwner = !smart;
|
|
|
|
return ignoreOwner || m->ownerMatches(unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnitEffect::serializeJsonEffect(JsonSerializeFormat & handler)
|
|
|
|
{
|
|
|
|
handler.serializeBool("ignoreImmunity", ignoreImmunity);
|
|
|
|
handler.serializeInt("chainLength", chainLength, 0);
|
|
|
|
handler.serializeFloat("chainFactor", chainFactor, 0);
|
|
|
|
serializeJsonUnitEffect(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|