1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*
* 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"
#include "../../NetPacksBase.h"
#include "../../battle/CBattleInfoCallback.h"
#include "../../battle/Unit.h"
#include "../../serializer/JsonSerializeFormat.h"
namespace spells
{
namespace effects
{
UnitEffect::UnitEffect()
: Effect(),
chainLength(0),
chainFactor(0.0),
ignoreImmunity(false)
{
}
UnitEffect::~UnitEffect() = default;
void UnitEffect::adjustTargetTypes(std::vector<TargetType> & types) const
{
}
void UnitEffect::adjustAffectedHexes(std::set<BattleHex> & hexes, const Mechanics * m, const Target & spellTarget) const
{
for(auto & destnation : spellTarget)
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
auto mainFilter = std::bind(&UnitEffect::getStackFilter, this, m, true, _1);
auto predicate = std::bind(&UnitEffect::eraseByImmunityFilter, this, m, _1);
auto targets = m->cb->battleGetUnitsIf(mainFilter);
vstd::erase_if(targets, predicate);
if(targets.empty())
{
MetaString text;
text.addTxt(MetaString::GENERAL_TXT, 185);
problem.add(std::move(text), Problem::NORMAL);
return false;
}
return true;
}
bool UnitEffect::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
{
//stack effect is applicable if it affects at least one smart target
//assume target correctly transformed, just reapply smart filter
for(auto & item : target)
if(item.unitValue)
if(getStackFilter(m, true, item.unitValue))
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)
{
if(!d.unitValue)
return false;
if(!isValidTarget(m, d.unitValue))
return false;
if(!isReceptive(m, d.unitValue))
return false;
return true;
});
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);
//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
if(!aimPoint.empty())
spellTargetCopy.insert(spellTargetCopy.begin(), Destination(aimPoint.front()));
std::set<const battle::Unit *> targets;
if(m->isMassive())
{
//ignore spellTarget and add all stacks
auto units = m->cb->battleGetUnitsIf(mainFilter);
for(auto unit : units)
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);
};
auto units = m->cb->battleGetUnitsIf(predicate);
for(auto unit : units)
{
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);
else
logGlobal->error("Spell-like attack with no primary target.");
}
EffectTarget effectTarget;
for(auto s : targets)
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;
auto possibleTargets = m->cb->battleGetUnitsIf([&](const battle::Unit * unit) -> bool
{
return isValidTarget(m, unit);
});
for(auto unit : possibleTargets)
{
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)
{
auto unit = m->cb->battleGetUnitByPos(destHex, true);
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;
cachingStr << "type_" << Bonus::SPELL_IMMUNITY << "subtype_" << m->getSpellIndex() << "addInfo_1";
if(unit->hasBonus(Selector::typeSubtypeInfo(Bonus::SPELL_IMMUNITY, m->getSpellIndex(), 1), cachingStr.str()))
return false;
return true;
}
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);
}
}
}
|