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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
|
/*
* TargetCondition.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 "TargetCondition.h"
#include "../GameConstants.h"
#include "../CBonusTypeHandler.h"
#include "../battle/CBattleInfoCallback.h"
#include "../battle/Unit.h"
#include "../bonuses/BonusParams.h"
#include "../bonuses/BonusList.h"
#include "../json/JsonBonus.h"
#include "../modding/IdentifierStorage.h"
#include "../modding/ModUtility.h"
#include "../serializer/JsonSerializeFormat.h"
#include "../VCMI_Lib.h"
#include <vcmi/spells/Spell.h>
VCMI_LIB_NAMESPACE_BEGIN
namespace spells
{
class TargetConditionItemBase : public TargetConditionItem
{
public:
bool inverted = false;
bool exclusive = false;
void setInverted(bool value) override
{
inverted = value;
}
void setExclusive(bool value) override
{
exclusive = value;
}
bool isExclusive() const override
{
return exclusive;
}
bool isReceptive(const Mechanics * m, const battle::Unit * target) const override
{
bool result = check(m, target);
return inverted != result;
}
protected:
virtual bool check(const Mechanics * m, const battle::Unit * target) const = 0;
};
class SelectorCondition : public TargetConditionItemBase
{
public:
SelectorCondition(const CSelector & csel):
sel(csel)
{
}
SelectorCondition(const CSelector & csel, si32 minVal, si32 maxVal):
sel(csel),
minVal(minVal),
maxVal(maxVal)
{
}
protected:
bool check(const Mechanics * m, const battle::Unit * target) const override
{
if(target->hasBonus(sel)) {
auto b = target->valOfBonuses(sel,"");
return b >= minVal && b <= maxVal;
}
return false;
}
private:
CSelector sel;
si32 minVal = std::numeric_limits<si32>::min();
si32 maxVal = std::numeric_limits<si32>::max();
};
class ResistanceCondition : public TargetConditionItemBase
{
protected:
bool check(const Mechanics * m, const battle::Unit * target) const override
{
if(m->isPositiveSpell()) //Always pass on positive
return true;
return target->magicResistance() < 100;
}
};
class CreatureCondition : public TargetConditionItemBase
{
public:
CreatureCondition(const CreatureID & type_): type(type_) {}
protected:
bool check(const Mechanics * m, const battle::Unit * target) const override
{
return target->creatureId() == type;
}
private:
CreatureID type;
};
class AbsoluteLevelCondition : public TargetConditionItemBase
{
public:
AbsoluteLevelCondition()
{
inverted = false;
exclusive = true;
}
protected:
bool check(const Mechanics * m, const battle::Unit * target) const override
{
if(!m->isMagicalEffect()) //Always pass on non-magical
return true;
std::stringstream cachingStr;
cachingStr << "type_" << vstd::to_underlying(BonusType::LEVEL_SPELL_IMMUNITY) << "addInfo_1";
TConstBonusListPtr levelImmunities = target->getBonuses(Selector::type()(BonusType::LEVEL_SPELL_IMMUNITY).And(Selector::info()(1)), cachingStr.str());
return (levelImmunities->size() == 0 || levelImmunities->totalValue() < m->getSpellLevel() || m->getSpellLevel() <= 0);
}
};
class AbsoluteSpellCondition : public TargetConditionItemBase
{
public:
AbsoluteSpellCondition()
{
inverted = false;
exclusive = true;
}
protected:
bool check(const Mechanics * m, const battle::Unit * target) const override
{
std::stringstream cachingStr;
cachingStr << "type_" << vstd::to_underlying(BonusType::SPELL_IMMUNITY) << "subtype_" << m->getSpellIndex() << "addInfo_1";
return !target->hasBonus(Selector::typeSubtypeInfo(BonusType::SPELL_IMMUNITY, BonusSubtypeID(m->getSpellId()), 1), cachingStr.str());
}
};
class ElementalCondition : public TargetConditionItemBase
{
public:
ElementalCondition()
{
inverted = true;
exclusive = true;
}
protected:
bool check(const Mechanics * m, const battle::Unit * target) const override
{
bool elementalImmune = false;
auto bearer = target->getBonusBearer();
m->getSpell()->forEachSchool([&](const SpellSchool & cnf, bool & stop)
{
if (bearer->hasBonusOfType(BonusType::SPELL_SCHOOL_IMMUNITY, BonusSubtypeID(cnf)))
{
elementalImmune = true;
stop = true; //only bonus from one school is used
}
else if(!m->isPositiveSpell()) //negative or indifferent
{
if (bearer->hasBonusOfType(BonusType::NEGATIVE_EFFECTS_IMMUNITY, BonusSubtypeID(cnf)))
{
elementalImmune = true;
stop = true; //only bonus from one school is used
}
}
});
return elementalImmune;
}
};
class NormalLevelCondition : public TargetConditionItemBase
{
public:
NormalLevelCondition()
{
inverted = false;
exclusive = true;
}
protected:
bool check(const Mechanics * m, const battle::Unit * target) const override
{
if(!m->isMagicalEffect()) //Always pass on non-magical
return true;
TConstBonusListPtr levelImmunities = target->getBonusesOfType(BonusType::LEVEL_SPELL_IMMUNITY);
return levelImmunities->size() == 0 ||
levelImmunities->totalValue() < m->getSpellLevel() ||
m->getSpellLevel() <= 0;
}
};
class NormalSpellCondition : public TargetConditionItemBase
{
public:
NormalSpellCondition()
{
inverted = false;
exclusive = true;
}
protected:
bool check(const Mechanics * m, const battle::Unit * target) const override
{
return !target->hasBonusOfType(BonusType::SPELL_IMMUNITY, BonusSubtypeID(m->getSpellId()));
}
};
//for Hypnotize
class HealthValueCondition : public TargetConditionItemBase
{
protected:
bool check(const Mechanics * m, const battle::Unit * target) const override
{
//todo: maybe do not resist on passive cast
//TODO: what with other creatures casting hypnotize, Faerie Dragons style?
int64_t subjectHealth = target->getAvailableHealth();
//apply 'damage' bonus for hypnotize, including hero specialty
auto maxHealth = m->applySpellBonus(m->getEffectValue(), target);
return subjectHealth <= maxHealth;
}
};
class SpellEffectCondition : public TargetConditionItemBase
{
public:
SpellEffectCondition(const SpellID & spellID_): spellID(spellID_)
{
std::stringstream builder;
builder << "source_" << vstd::to_underlying(BonusSource::SPELL_EFFECT) << "id_" << spellID.num;
cachingString = builder.str();
selector = Selector::source(BonusSource::SPELL_EFFECT, BonusSourceID(spellID));
}
protected:
bool check(const Mechanics * m, const battle::Unit * target) const override
{
return target->hasBonus(selector, cachingString);
}
private:
CSelector selector;
std::string cachingString;
SpellID spellID;
};
class ReceptiveFeatureCondition : public TargetConditionItemBase
{
protected:
bool check(const Mechanics * m, const battle::Unit * target) const override
{
return m->isPositiveSpell() && target->hasBonus(selector, cachingString);
}
private:
CSelector selector = Selector::type()(BonusType::RECEPTIVE);
std::string cachingString = "type_RECEPTIVE";
};
class ImmunityNegationCondition : public TargetConditionItemBase
{
protected:
bool check(const Mechanics * m, const battle::Unit * target) const override
{
const bool battleWideNegation = target->hasBonusOfType(BonusType::NEGATE_ALL_NATURAL_IMMUNITIES, BonusCustomSubtype::immunityBattleWide);
const bool heroNegation = target->hasBonusOfType(BonusType::NEGATE_ALL_NATURAL_IMMUNITIES, BonusCustomSubtype::immunityEnemyHero);
//Non-magical effects is not affected by orb of vulnerability
if(!m->isMagicalEffect())
return false;
//anyone can cast on artifact holder`s stacks
if(heroNegation)
{
return true;
}
//this stack is from other player
else if(battleWideNegation)
{
if(m->ownerMatches(target, false))
return true;
}
return false;
}
};
class DefaultTargetConditionItemFactory : public TargetConditionItemFactory
{
public:
Object createAbsoluteLevel() const override
{
static auto antimagicCondition = std::make_shared<AbsoluteLevelCondition>();
return antimagicCondition;
}
Object createAbsoluteSpell() const override
{
static auto alCondition = std::make_shared<AbsoluteSpellCondition>();
return alCondition;
}
Object createElemental() const override
{
static auto elementalCondition = std::make_shared<ElementalCondition>();
return elementalCondition;
}
Object createResistance() const override
{
static auto elementalCondition = std::make_shared<ResistanceCondition>();
return elementalCondition;
}
Object createNormalLevel() const override
{
static auto nlCondition = std::make_shared<NormalLevelCondition>();
return nlCondition;
}
Object createNormalSpell() const override
{
static auto nsCondition = std::make_shared<NormalSpellCondition>();
return nsCondition;
}
Object createConfigurable(std::string scope, std::string type, std::string identifier) const override
{
if(type == "bonus")
{
//TODO: support custom bonus types
auto it = bonusNameMap.find(identifier);
if(it != bonusNameMap.end())
return std::make_shared<SelectorCondition>(Selector::type()(it->second));
auto params = BonusParams(identifier, "", -1);
if(params.isConverted)
{
if(params.val)
return std::make_shared<SelectorCondition>(params.toSelector(), *params.val, *params.val);
return std::make_shared<SelectorCondition>(params.toSelector());
}
logMod->error("Invalid bonus type %s in spell target condition.", identifier);
}
else if(type == "creature")
{
auto rawId = VLC->identifiers()->getIdentifier(scope, type, identifier, true);
if(rawId)
return std::make_shared<CreatureCondition>(CreatureID(rawId.value()));
else
logMod->error("Invalid creature %s type in spell target condition.", identifier);
}
else if(type == "spell")
{
auto rawId = VLC->identifiers()->getIdentifier(scope, type, identifier, true);
if(rawId)
return std::make_shared<SpellEffectCondition>(SpellID(rawId.value()));
else
logMod->error("Invalid spell %s in spell target condition.", identifier);
}
else if(type == "healthValueSpecial")
{
return std::make_shared<HealthValueCondition>();
}
else
{
logMod->error("Invalid type %s in spell target condition.", type);
}
return Object();
}
Object createFromJsonStruct(const JsonNode & jsonStruct) const override
{
auto type = jsonStruct["type"].String();
auto parameters = jsonStruct["parameters"];
if(type == "selector")
{
auto minVal = std::numeric_limits<si32>::min();
auto maxVal = std::numeric_limits<si32>::max();
if(parameters["minVal"].isNumber())
minVal = parameters["minVal"].Integer();
if(parameters["maxVal"].isNumber())
maxVal = parameters["maxVal"].Integer();
auto sel = JsonUtils::parseSelector(parameters);
return std::make_shared<SelectorCondition>(sel, minVal, maxVal);
}
logMod->error("Invalid type %s in spell target condition.", type);
return Object();
}
Object createReceptiveFeature() const override
{
static auto condition = std::make_shared<ReceptiveFeatureCondition>();
return condition;
}
Object createImmunityNegation() const override
{
static auto condition = std::make_shared<ImmunityNegationCondition>();
return condition;
}
};
const TargetConditionItemFactory * TargetConditionItemFactory::getDefault()
{
static std::unique_ptr<TargetConditionItemFactory> singleton;
if(!singleton)
singleton = std::make_unique<DefaultTargetConditionItemFactory>();
return singleton.get();
}
bool TargetCondition::isReceptive(const Mechanics * m, const battle::Unit * target) const
{
if(!check(absolute, m, target))
return false;
for(const auto & item : negation)
{
if(item->isReceptive(m, target))
return true;
}
return check(normal, m, target);
}
void TargetCondition::serializeJson(JsonSerializeFormat & handler, const ItemFactory * itemFactory)
{
if(handler.saving)
{
logGlobal->error("Spell target condition saving is not supported");
return;
}
absolute.clear();
normal.clear();
negation.clear();
absolute.push_back(itemFactory->createAbsoluteSpell());
absolute.push_back(itemFactory->createAbsoluteLevel());
normal.push_back(itemFactory->createElemental());
normal.push_back(itemFactory->createResistance());
normal.push_back(itemFactory->createNormalLevel());
normal.push_back(itemFactory->createNormalSpell());
negation.push_back(itemFactory->createReceptiveFeature());
negation.push_back(itemFactory->createImmunityNegation());
{
auto anyOf = handler.enterStruct("anyOf");
loadConditions(anyOf->getCurrent(), false, false, itemFactory);
}
{
auto allOf = handler.enterStruct("allOf");
loadConditions(allOf->getCurrent(), true, false, itemFactory);
}
{
auto noneOf = handler.enterStruct("noneOf");
loadConditions(noneOf->getCurrent(), true, true, itemFactory);
}
}
bool TargetCondition::check(const ItemVector & condition, const Mechanics * m, const battle::Unit * target) const
{
bool nonExclusiveCheck = false;
bool nonExclusiveExits = false;
for(const auto & item : condition)
{
if(item->isExclusive())
{
if(!item->isReceptive(m, target))
return false;
}
else
{
if(item->isReceptive(m, target))
nonExclusiveCheck = true;
nonExclusiveExits = true;
}
}
return !nonExclusiveExits || nonExclusiveCheck;
}
void TargetCondition::loadConditions(const JsonNode & source, bool exclusive, bool inverted, const ItemFactory * itemFactory)
{
for(const auto & keyValue : source.Struct())
{
bool isAbsolute;
const JsonNode & value = keyValue.second;
if(value.String() == "absolute")
isAbsolute = true;
else if(value.String() == "normal")
isAbsolute = false;
else if(value.isStruct()) //assume conditions have a new struct format
isAbsolute = value["absolute"].Bool();
else
continue;
std::shared_ptr<TargetConditionItem> item;
if(value.isStruct())
item = itemFactory->createFromJsonStruct(value);
else
{
std::string scope;
std::string type;
std::string identifier;
ModUtility::parseIdentifier(keyValue.first, scope, type, identifier);
item = itemFactory->createConfigurable(keyValue.second.getModScope(), type, identifier);
}
if(item)
{
item->setExclusive(exclusive);
item->setInverted(inverted);
if(isAbsolute)
absolute.push_back(item);
else
normal.push_back(item);
}
}
}
}
VCMI_LIB_NAMESPACE_END
|