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 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
|
/*
* CDefaultSpellMechanics.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 "CDefaultSpellMechanics.h"
#include "../BattleState.h"
#include "../CGeneralTextHandler.h"
namespace SRSLPraserHelpers
{
static int XYToHex(int x, int y)
{
return x + GameConstants::BFIELD_WIDTH * y;
}
static int XYToHex(std::pair<int, int> xy)
{
return XYToHex(xy.first, xy.second);
}
static int hexToY(int battleFieldPosition)
{
return battleFieldPosition/GameConstants::BFIELD_WIDTH;
}
static int hexToX(int battleFieldPosition)
{
int pos = battleFieldPosition - hexToY(battleFieldPosition) * GameConstants::BFIELD_WIDTH;
return pos;
}
static std::pair<int, int> hexToPair(int battleFieldPosition)
{
return std::make_pair(hexToX(battleFieldPosition), hexToY(battleFieldPosition));
}
//moves hex by one hex in given direction
//0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left
static std::pair<int, int> gotoDir(int x, int y, int direction)
{
switch(direction)
{
case 0: //top left
return std::make_pair((y%2) ? x-1 : x, y-1);
case 1: //top right
return std::make_pair((y%2) ? x : x+1, y-1);
case 2: //right
return std::make_pair(x+1, y);
case 3: //right bottom
return std::make_pair((y%2) ? x : x+1, y+1);
case 4: //left bottom
return std::make_pair((y%2) ? x-1 : x, y+1);
case 5: //left
return std::make_pair(x-1, y);
default:
throw std::runtime_error("Disaster: wrong direction in SRSLPraserHelpers::gotoDir!\n");
}
}
static std::pair<int, int> gotoDir(std::pair<int, int> xy, int direction)
{
return gotoDir(xy.first, xy.second, direction);
}
static bool isGoodHex(std::pair<int, int> xy)
{
return xy.first >=0 && xy.first < GameConstants::BFIELD_WIDTH && xy.second >= 0 && xy.second < GameConstants::BFIELD_HEIGHT;
}
//helper function for rangeInHexes
static std::set<ui16> getInRange(unsigned int center, int low, int high)
{
std::set<ui16> ret;
if(low == 0)
{
ret.insert(center);
}
std::pair<int, int> mainPointForLayer[6]; //A, B, C, D, E, F points
for(auto & elem : mainPointForLayer)
elem = hexToPair(center);
for(int it=1; it<=high; ++it) //it - distance to the center
{
for(int b=0; b<6; ++b)
mainPointForLayer[b] = gotoDir(mainPointForLayer[b], b);
if(it>=low)
{
std::pair<int, int> curHex;
//adding lines (A-b, B-c, C-d, etc)
for(int v=0; v<6; ++v)
{
curHex = mainPointForLayer[v];
for(int h=0; h<it; ++h)
{
if(isGoodHex(curHex))
ret.insert(XYToHex(curHex));
curHex = gotoDir(curHex, (v+2)%6);
}
}
} //if(it>=low)
}
return ret;
}
}
SpellCastContext::SpellCastContext(const DefaultSpellMechanics * mechanics_, const SpellCastEnvironment * env_, const BattleSpellCastParameters & parameters_):
mechanics(mechanics_), env(env_), attackedCres(), sc(), si(), parameters(parameters_), otherHero(nullptr), spellCost(0), damageToDisplay(0)
{
sc.side = parameters.casterSide;
sc.id = mechanics->owner->id;
sc.skill = parameters.spellLvl;
sc.tile = parameters.getFirstDestinationHex();
sc.castByHero = parameters.mode == ECastingMode::HERO_CASTING;
sc.casterStack = (parameters.casterStack ? parameters.casterStack->ID : -1);
sc.manaGained = 0;
//check it there is opponent hero
const ui8 otherSide = 1-parameters.casterSide;
if(parameters.cb->battleHasHero(otherSide))
otherHero = parameters.cb->battleGetFightingHero(otherSide);
logGlobal->debugStream() << "Started spell cast. Spell: " << mechanics->owner->name << "; mode:" << parameters.mode;
}
SpellCastContext::~SpellCastContext()
{
logGlobal->debugStream() << "Finished spell cast. Spell: " << mechanics->owner->name << "; mode:" << parameters.mode;
}
void SpellCastContext::addDamageToDisplay(const si32 value)
{
damageToDisplay += value;
}
void SpellCastContext::setDamageToDisplay(const si32 value)
{
damageToDisplay = value;
}
void SpellCastContext::prepareBattleLog()
{
bool displayDamage = true;
mechanics->battleLog(sc.battleLog, parameters, attackedCres, damageToDisplay, displayDamage);
displayDamage = displayDamage && damageToDisplay > 0;
if(displayDamage)
{
MetaString line;
line.addTxt(MetaString::GENERAL_TXT, 376);
line.addReplacement(MetaString::SPELL_NAME, mechanics->owner->id.toEnum());
line.addReplacement(damageToDisplay);
sc.battleLog.push_back(line);
}
}
void SpellCastContext::beforeCast()
{
//calculate spell cost
if(parameters.mode == ECastingMode::HERO_CASTING)
{
spellCost = parameters.cb->battleGetSpellCost(mechanics->owner, parameters.casterHero);
if(nullptr != otherHero) //handle mana channel
{
int manaChannel = 0;
for(const CStack * stack : parameters.cb->battleGetAllStacks(true)) //TODO: shouldn't bonus system handle it somehow?
{
if(stack->owner == otherHero->tempOwner)
{
vstd::amax(manaChannel, stack->valOfBonuses(Bonus::MANA_CHANNELING));
}
}
sc.manaGained = (manaChannel * spellCost) / 100;
}
logGlobal->debugStream() << "spellCost: " << spellCost;
}
}
void SpellCastContext::afterCast()
{
for(auto sta : attackedCres)
{
sc.affectedCres.insert(sta->ID);
}
prepareBattleLog();
env->sendAndApply(&sc);
if(parameters.mode == ECastingMode::HERO_CASTING)
{
//spend mana
SetMana sm;
sm.absolute = false;
sm.hid = parameters.casterHero->id;
sm.val = -spellCost;
env->sendAndApply(&sm);
if(sc.manaGained > 0)
{
assert(otherHero);
sm.hid = otherHero->id;
sm.val = sc.manaGained;
env->sendAndApply(&sm);
}
}
else if (parameters.mode == ECastingMode::CREATURE_ACTIVE_CASTING || parameters.mode == ECastingMode::ENCHANTER_CASTING)
{
//reduce number of casts remaining
assert(parameters.casterStack);
BattleSetStackProperty ssp;
ssp.stackID = parameters.casterStack->ID;
ssp.which = BattleSetStackProperty::CASTS;
ssp.val = -1;
ssp.absolute = false;
env->sendAndApply(&ssp);
}
if(!si.stacks.empty()) //after spellcast info shows
env->sendAndApply(&si);
}
///DefaultSpellMechanics
void DefaultSpellMechanics::applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const
{
if (packet->castByHero)
{
if (packet->side < 2)
{
battle->sides[packet->side].castSpellsCount++;
}
}
//handle countering spells
for(auto stackID : packet->affectedCres)
{
CStack * s = battle->getStack(stackID);
s->popBonuses([&](const Bonus * b) -> bool
{
//check for each bonus if it should be removed
const bool isSpellEffect = Selector::sourceType(Bonus::SPELL_EFFECT)(b);
const int spellID = isSpellEffect ? b->sid : -1;
//No exceptions, ANY spell can be countered, even if it can`t be dispelled.
return isSpellEffect && vstd::contains(owner->counteredSpells, spellID);
});
}
}
void DefaultSpellMechanics::battleCast(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters) const
{
if(nullptr == parameters.caster)
{
env->complain("No spell-caster provided.");
return;
}
std::vector <const CStack*> reflected;//for magic mirror
cast(env, parameters, reflected);
//Magic Mirror effect
for(auto & attackedCre : reflected)
{
if(parameters.mode == ECastingMode::MAGIC_MIRROR)
{
logGlobal->error("Magic mirror recurrence!");
return;
}
TStacks mirrorTargets = parameters.cb->battleGetStacksIf([this, parameters](const CStack * battleStack)
{
//Get all enemy stacks. Magic mirror can reflect to immune creature (with no effect)
return battleStack->owner == parameters.casterColor && battleStack->isValidTarget(false);
});
if(!mirrorTargets.empty())
{
int targetHex = (*RandomGeneratorUtil::nextItem(mirrorTargets, env->getRandomGenerator()))->position;
BattleSpellCastParameters mirrorParameters(parameters, attackedCre);
mirrorParameters.aimToHex(targetHex);
mirrorParameters.cast(env);
}
}
}
void DefaultSpellMechanics::cast(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, std::vector <const CStack*> & reflected) const
{
SpellCastContext ctx(this, env, parameters);
ctx.beforeCast();
ctx.attackedCres = owner->getAffectedStacks(parameters.cb, parameters.mode, parameters.caster, parameters.spellLvl, parameters.getFirstDestinationHex());
logGlobal->debugStream() << "will affect " << ctx.attackedCres.size() << " stacks";
handleResistance(env, ctx);
if(parameters.mode != ECastingMode::MAGIC_MIRROR)
handleMagicMirror(env, ctx, reflected);
applyBattleEffects(env, parameters, ctx);
ctx.afterCast();
}
void DefaultSpellMechanics::battleLog(std::vector<MetaString> & logLines, const BattleSpellCastParameters & parameters,
const std::vector<const CStack *> & attacked, const si32 damageToDisplay, bool & displayDamage) const
{
if(attacked.size() != 1)
{
displayDamage = true;
battleLogDefault(logLines, parameters, attacked);
return;
}
auto attackedStack = attacked.at(0);
auto getPluralFormat = [attackedStack](const int baseTextID) -> si32
{
return attackedStack->count > 1 ? baseTextID + 1 : baseTextID;
};
auto logSimple = [attackedStack, &logLines, getPluralFormat](const int baseTextID)
{
MetaString line;
line.addTxt(MetaString::GENERAL_TXT, getPluralFormat(baseTextID));
line.addReplacement(*attackedStack);
logLines.push_back(line);
};
auto logPlural = [attackedStack, &logLines, getPluralFormat](const int baseTextID)
{
MetaString line;
line.addTxt(MetaString::GENERAL_TXT, baseTextID);
line.addReplacement(MetaString::CRE_PL_NAMES, attackedStack->getCreature()->idNumber.num);
logLines.push_back(line);
};
displayDamage = false; //in most following cases damage info text is custom
switch(owner->id)
{
case SpellID::STONE_GAZE:
logSimple(558);
break;
case SpellID::POISON:
logSimple(561);
break;
case SpellID::BIND:
logPlural(560);//Roots and vines bind the %s to the ground!
break;
case SpellID::DISEASE:
logSimple(553);
break;
case SpellID::PARALYZE:
logSimple(563);
break;
case SpellID::AGE:
{
//The %s shrivel with age, and lose %d hit points."
MetaString line;
line.addTxt(MetaString::GENERAL_TXT, getPluralFormat(551));
line.addReplacement(MetaString::CRE_PL_NAMES, attackedStack->getCreature()->idNumber.num);
//todo: display effect from only this cast
TBonusListPtr bl = attackedStack->getBonuses(Selector::type(Bonus::STACK_HEALTH));
const int fullHP = bl->totalValue();
bl->remove_if(Selector::source(Bonus::SPELL_EFFECT, SpellID::AGE));
line.addReplacement(fullHP - bl->totalValue());
logLines.push_back(line);
}
break;
case SpellID::THUNDERBOLT:
{
logPlural(367);
MetaString line;
//todo: handle newlines in metastring
std::string text = VLC->generaltexth->allTexts[343].substr(1, VLC->generaltexth->allTexts[343].size() - 1); //Does %d points of damage.
line << text;
line.addReplacement(damageToDisplay); //no more text afterwards
logLines.push_back(line);
}
break;
case SpellID::DISPEL_HELPFUL_SPELLS:
logPlural(555);
break;
case SpellID::DEATH_STARE:
if (damageToDisplay > 0)
{
MetaString line;
if (damageToDisplay > 1)
{
line.addTxt(MetaString::GENERAL_TXT, 119); //%d %s die under the terrible gaze of the %s.
line.addReplacement(damageToDisplay);
line.addReplacement(MetaString::CRE_PL_NAMES, attackedStack->getCreature()->idNumber.num);
}
else
{
line.addTxt(MetaString::GENERAL_TXT, 118); //One %s dies under the terrible gaze of the %s.
line.addReplacement(MetaString::CRE_SING_NAMES, attackedStack->getCreature()->idNumber.num);
}
parameters.caster->getCasterName(line);
logLines.push_back(line);
}
break;
default:
displayDamage = true;
battleLogDefault(logLines, parameters, attacked);
break;
}
}
void DefaultSpellMechanics::battleLogDefault(std::vector<MetaString> & logLines, const BattleSpellCastParameters & parameters, const std::vector<const CStack*> & attacked) const
{
MetaString line;
parameters.caster->getCastDescription(owner, attacked, line);
logLines.push_back(line);
}
void DefaultSpellMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
{
//applying effects
if(owner->isOffensiveSpell())
{
const int rawDamage = parameters.getEffectValue();
int chainLightningModifier = 0;
for(auto & attackedCre : ctx.attackedCres)
{
BattleStackAttacked bsa;
bsa.damageAmount = owner->adjustRawDamage(parameters.caster, attackedCre, rawDamage) >> chainLightningModifier;
ctx.addDamageToDisplay(bsa.damageAmount);
bsa.stackAttacked = (attackedCre)->ID;
if(parameters.mode == ECastingMode::ENCHANTER_CASTING) //multiple damage spells cast
bsa.attackerID = parameters.casterStack->ID;
else
bsa.attackerID = -1;
(attackedCre)->prepareAttacked(bsa, env->getRandomGenerator());
ctx.si.stacks.push_back(bsa);
if(owner->id == SpellID::CHAIN_LIGHTNING)
++chainLightningModifier;
}
}
if(owner->hasEffects())
{
SetStackEffect sse;
//get default spell duration (spell power with bonuses for heroes)
int duration = parameters.enchantPower;
//generate actual stack bonuses
{
int maxDuration = 0;
std::vector<Bonus> tmp;
owner->getEffects(tmp, parameters.effectLevel);
for(Bonus& b : tmp)
{
//use configured duration if present
if(b.turnsRemain == 0)
b.turnsRemain = duration;
vstd::amax(maxDuration, b.turnsRemain);
sse.effect.push_back(b);
}
//if all spell effects have special duration, use it
duration = maxDuration;
}
//fix to original config: shield should display damage reduction
if(owner->id == SpellID::SHIELD || owner->id == SpellID::AIR_SHIELD)
{
sse.effect.back().val = (100 - sse.effect.back().val);
}
//we need to know who cast Bind
if(owner->id == SpellID::BIND && parameters.casterStack)
{
sse.effect.back().additionalInfo = parameters.casterStack->ID;
}
std::shared_ptr<Bonus> bonus = nullptr;
if(parameters.casterHero)
bonus = parameters.casterHero->getBonusLocalFirst(Selector::typeSubtype(Bonus::SPECIAL_PECULIAR_ENCHANT, owner->id));
//TODO does hero specialty should affects his stack casting spells?
si32 power = 0;
for(const CStack * affected : ctx.attackedCres)
{
sse.stacks.push_back(affected->ID);
//Apply hero specials - peculiar enchants
const ui8 tier = std::max((ui8)1, affected->getCreature()->level); //don't divide by 0 for certain creatures (commanders, war machines)
if(bonus)
{
switch(bonus->additionalInfo)
{
case 0: //normal
{
switch(tier)
{
case 1: case 2:
power = 3;
break;
case 3: case 4:
power = 2;
break;
case 5: case 6:
power = 1;
break;
}
Bonus specialBonus(sse.effect.back());
specialBonus.val = power; //it doesn't necessarily make sense for some spells, use it wisely
sse.uniqueBonuses.push_back (std::pair<ui32,Bonus> (affected->ID, specialBonus)); //additional premy to given effect
}
break;
case 1: //only Coronius as yet
{
power = std::max(5 - tier, 0);
Bonus specialBonus(Bonus::N_TURNS, Bonus::PRIMARY_SKILL, Bonus::SPELL_EFFECT, power, owner->id, PrimarySkill::ATTACK);
specialBonus.turnsRemain = duration;
sse.uniqueBonuses.push_back(std::pair<ui32,Bonus> (affected->ID, specialBonus)); //additional attack to Slayer effect
}
break;
}
}
if (parameters.casterHero && parameters.casterHero->hasBonusOfType(Bonus::SPECIAL_BLESS_DAMAGE, owner->id)) //TODO: better handling of bonus percentages
{
int damagePercent = parameters.casterHero->level * parameters.casterHero->valOfBonuses(Bonus::SPECIAL_BLESS_DAMAGE, owner->id.toEnum()) / tier;
Bonus specialBonus(Bonus::N_TURNS, Bonus::CREATURE_DAMAGE, Bonus::SPELL_EFFECT, damagePercent, owner->id, 0, Bonus::PERCENT_TO_ALL);
specialBonus.turnsRemain = duration;
sse.uniqueBonuses.push_back (std::pair<ui32,Bonus> (affected->ID, specialBonus));
}
}
if(!sse.stacks.empty())
env->sendAndApply(&sse);
}
}
std::vector<BattleHex> DefaultSpellMechanics::rangeInHexes(BattleHex centralHex, ui8 schoolLvl, ui8 side, bool *outDroppedHexes) const
{
using namespace SRSLPraserHelpers;
std::vector<BattleHex> ret;
std::string rng = owner->getLevelInfo(schoolLvl).range + ','; //copy + artificial comma for easier handling
if(rng.size() >= 2 && rng[0] != 'X') //there is at least one hex in range (+artificial comma)
{
std::string number1, number2;
int beg, end;
bool readingFirst = true;
for(auto & elem : rng)
{
if(std::isdigit(elem) ) //reading number
{
if(readingFirst)
number1 += elem;
else
number2 += elem;
}
else if(elem == ',') //comma
{
//calculating variables
if(readingFirst)
{
beg = atoi(number1.c_str());
number1 = "";
}
else
{
end = atoi(number2.c_str());
number2 = "";
}
//obtaining new hexes
std::set<ui16> curLayer;
if(readingFirst)
{
curLayer = getInRange(centralHex, beg, beg);
}
else
{
curLayer = getInRange(centralHex, beg, end);
readingFirst = true;
}
//adding abtained hexes
for(auto & curLayer_it : curLayer)
{
ret.push_back(curLayer_it);
}
}
else if(elem == '-') //dash
{
beg = atoi(number1.c_str());
number1 = "";
readingFirst = false;
}
}
}
//remove duplicates (TODO check if actually needed)
range::unique(ret);
return ret;
}
std::vector<const CStack *> DefaultSpellMechanics::getAffectedStacks(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx) const
{
std::vector<const CStack *> attackedCres = calculateAffectedStacks(cb, ctx);
handleImmunities(cb, ctx, attackedCres);
return attackedCres;
}
std::vector<const CStack *> DefaultSpellMechanics::calculateAffectedStacks(const CBattleInfoCallback* cb, const SpellTargetingContext& ctx) const
{
std::set<const CStack* > attackedCres;//std::set to exclude multiple occurrences of two hex creatures
const ui8 attackerSide = cb->playerToSide(ctx.caster->getOwner()) == 1;
auto attackedHexes = rangeInHexes(ctx.destination, ctx.schoolLvl, attackerSide);
//hackfix for banned creature massive spells
if(!ctx.ti.massive && owner->getLevelInfo(ctx.schoolLvl).range == "X")
attackedHexes.push_back(ctx.destination);
auto mainFilter = [=](const CStack * s)
{
const bool positiveToAlly = owner->isPositive() && s->owner == ctx.caster->getOwner();
const bool negativeToEnemy = owner->isNegative() && s->owner != ctx.caster->getOwner();
const bool validTarget = s->isValidTarget(!ctx.ti.onlyAlive); //todo: this should be handled by spell class
const bool positivenessFlag = !ctx.ti.smart || owner->isNeutral() || positiveToAlly || negativeToEnemy;
return positivenessFlag && validTarget;
};
if(ctx.ti.type == CSpell::CREATURE && attackedHexes.size() == 1)
{
//for single target spells we must select one target. Alive stack is preferred (issue #1763)
auto predicate = [&](const CStack * s)
{
return s->coversPos(attackedHexes.at(0)) && mainFilter(s);
};
TStacks stacks = cb->battleGetStacksIf(predicate);
for(auto stack : stacks)
{
if(stack->alive())
{
attackedCres.insert(stack);
break;
}
}
if(attackedCres.empty() && !stacks.empty())
{
attackedCres.insert(stacks.front());
}
}
else if(ctx.ti.massive)
{
TStacks stacks = cb->battleGetStacksIf(mainFilter);
for (auto stack : stacks)
attackedCres.insert(stack);
}
else //custom range from attackedHexes
{
for(BattleHex hex : attackedHexes)
{
if(const CStack * st = cb->battleGetStackByPos(hex, ctx.ti.onlyAlive))
if(mainFilter(st))
attackedCres.insert(st);;
}
}
std::vector<const CStack *> res;
std::copy(attackedCres.begin(), attackedCres.end(), std::back_inserter(res));
return res;
}
ESpellCastProblem::ESpellCastProblem DefaultSpellMechanics::canBeCast(const CBattleInfoCallback * cb, const ECastingMode::ECastingMode mode, const ISpellCaster * caster) const
{
//no problems by default, this method is for spell-specific problems
return ESpellCastProblem::OK;
}
ESpellCastProblem::ESpellCastProblem DefaultSpellMechanics::canBeCast(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx) const
{
if(ctx.mode == ECastingMode::CREATURE_ACTIVE_CASTING || ctx.mode == ECastingMode::HERO_CASTING)
{
std::vector<const CStack *> affected = getAffectedStacks(cb, ctx);
//allow to cast spell if affects is at least one smart target
bool targetExists = false;
for(const CStack * stack : affected)
{
bool casterStack = stack->owner == ctx.caster->getOwner();
switch (owner->positiveness)
{
case CSpell::POSITIVE:
if(casterStack)
targetExists = true;
break;
case CSpell::NEUTRAL:
targetExists = true;
break;
case CSpell::NEGATIVE:
if(!casterStack)
targetExists = true;
break;
}
if(targetExists)
break;
}
if(!targetExists)
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
}
return ESpellCastProblem::OK;
}
ESpellCastProblem::ESpellCastProblem DefaultSpellMechanics::isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const
{
//by default use general algorithm
return owner->internalIsImmune(caster, obj);
}
bool DefaultSpellMechanics::dispellSelector(const Bonus * bonus)
{
if(bonus->source == Bonus::SPELL_EFFECT)
{
const CSpell * sourceSpell = SpellID(bonus->sid).toSpell();
if(!sourceSpell)
return false;//error
//Special case: DISRUPTING_RAY is "immune" to dispell
//Other even PERMANENT effects can be removed (f.e. BIND)
if(sourceSpell->id == SpellID::DISRUPTING_RAY)
return false;
//Special case:do not remove lifetime marker
if(sourceSpell->id == SpellID::CLONE)
return false;
//stack may have inherited effects
if(sourceSpell->isAdventureSpell())
return false;
return true;
}
//not spell effect
return false;
}
void DefaultSpellMechanics::doDispell(BattleInfo * battle, const BattleSpellCast * packet, const CSelector & selector) const
{
for(auto stackID : packet->affectedCres)
{
CStack *s = battle->getStack(stackID);
s->popBonuses(selector.And(dispellSelector));
}
}
bool DefaultSpellMechanics::canDispell(const IBonusBearer * obj, const CSelector & selector, const std::string & cachingStr/* = "" */) const
{
return obj->hasBonus(selector.And(dispellSelector), Selector::all, cachingStr);
}
void DefaultSpellMechanics::handleImmunities(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx, std::vector<const CStack*> & stacks) const
{
auto predicate = [&, this](const CStack * s)->bool
{
bool hitDirectly = ctx.ti.alwaysHitDirectly && s->coversPos(ctx.destination);
bool notImmune = (ESpellCastProblem::OK == owner->isImmuneByStack(ctx.caster, s));
return !(hitDirectly || notImmune);
};
vstd::erase_if(stacks, predicate);
}
void DefaultSpellMechanics::handleMagicMirror(const SpellCastEnvironment * env, SpellCastContext & ctx, std::vector <const CStack*> & reflected) const
{
//reflection is applied only to negative spells
//if it is actual spell and can be reflected to single target, no recurrence
const bool tryMagicMirror = owner->isNegative() && owner->level && owner->getLevelInfo(0).range == "0";
if(tryMagicMirror)
{
for(auto s : ctx.attackedCres)
{
const int mirrorChance = (s)->valOfBonuses(Bonus::MAGIC_MIRROR);
if(env->getRandomGenerator().nextInt(99) < mirrorChance)
reflected.push_back(s);
}
vstd::erase_if(ctx.attackedCres, [&reflected](const CStack * s)
{
return vstd::contains(reflected, s);
});
for(auto s : reflected)
{
BattleSpellCast::CustomEffect effect;
effect.effect = 3;
effect.stack = s->ID;
ctx.sc.customEffects.push_back(effect);
}
}
}
void DefaultSpellMechanics::handleResistance(const SpellCastEnvironment * env, SpellCastContext & ctx) const
{
//checking if creatures resist
//resistance is applied only to negative spells
if(owner->isNegative())
{
std::vector <const CStack*> resisted;
for(auto s : ctx.attackedCres)
{
//magic resistance
const int prob = std::min((s)->magicResistance(), 100); //probability of resistance in %
if(env->getRandomGenerator().nextInt(99) < prob)
{
resisted.push_back(s);
}
}
vstd::erase_if(ctx.attackedCres, [&resisted](const CStack * s)
{
return vstd::contains(resisted, s);
});
for(auto s : resisted)
{
BattleSpellCast::CustomEffect effect;
effect.effect = 78;
effect.stack = s->ID;
ctx.sc.customEffects.push_back(effect);
}
}
}
bool DefaultSpellMechanics::requiresCreatureTarget() const
{
//most spells affects creatures somehow regardless of Target Type
//for few exceptions see overrides
return true;
}
ESpellCastProblem::ESpellCastProblem SpecialSpellMechanics::canBeCast(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx) const
{
//no problems by default
//common problems handled by CSpell
return ESpellCastProblem::OK;
}
std::vector<const CStack *> SpecialSpellMechanics::calculateAffectedStacks(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx) const
{
return std::vector<const CStack *>();
}
|