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 882 883 884 885 886 887 888 889 890 891 892 893 894
|
/*
* BattleStacksController.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 "BattleStacksController.h"
#include "BattleSiegeController.h"
#include "BattleInterfaceClasses.h"
#include "BattleInterface.h"
#include "BattleActionsController.h"
#include "BattleAnimationClasses.h"
#include "BattleFieldController.h"
#include "BattleEffectsController.h"
#include "BattleProjectileController.h"
#include "BattleWindow.h"
#include "BattleRenderer.h"
#include "CreatureAnimation.h"
#include "../CPlayerInterface.h"
#include "../CGameInfo.h"
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
#include "../media/ISoundPlayer.h"
#include "../render/Colors.h"
#include "../render/Canvas.h"
#include "../render/IRenderHandler.h"
#include "../render/Graphics.h"
#include "../render/IFont.h"
#include "../../CCallback.h"
#include "../../lib/spells/ISpellMechanics.h"
#include "../../lib/battle/BattleAction.h"
#include "../../lib/battle/BattleHex.h"
#include "../../lib/texts/TextOperations.h"
#include "../../lib/CRandomGenerator.h"
#include "../../lib/CStack.h"
static void onAnimationFinished(const CStack *stack, std::weak_ptr<CreatureAnimation> anim)
{
std::shared_ptr<CreatureAnimation> animation = anim.lock();
if(!animation)
return;
if (!stack->isFrozen() && animation->getType() == ECreatureAnimType::FROZEN)
animation->setType(ECreatureAnimType::HOLDING);
if (animation->isIdle())
{
const CCreature *creature = stack->unitType();
if (stack->isFrozen())
animation->setType(ECreatureAnimType::FROZEN);
else
if (animation->framesInGroup(ECreatureAnimType::MOUSEON) > 0)
{
if (CRandomGenerator::getDefault().nextDouble(99.0) < creature->animation.timeBetweenFidgets *10)
animation->playOnce(ECreatureAnimType::MOUSEON);
else
animation->setType(ECreatureAnimType::HOLDING);
}
else
{
animation->setType(ECreatureAnimType::HOLDING);
}
}
// always reset callback
animation->onAnimationReset += std::bind(&onAnimationFinished, stack, anim);
}
BattleStacksController::BattleStacksController(BattleInterface & owner):
owner(owner),
activeStack(nullptr),
stackToActivate(nullptr),
animIDhelper(0)
{
//preparing graphics for displaying amounts of creatures
amountNormal = GH.renderHandler().loadImage(ImagePath::builtin("combatUnitNumberWindowDefault"), EImageBlitMode::COLORKEY);
amountPositive = GH.renderHandler().loadImage(ImagePath::builtin("combatUnitNumberWindowPositive"), EImageBlitMode::COLORKEY);
amountNegative = GH.renderHandler().loadImage(ImagePath::builtin("combatUnitNumberWindowNegative"), EImageBlitMode::COLORKEY);
amountEffNeutral = GH.renderHandler().loadImage(ImagePath::builtin("combatUnitNumberWindowNeutral"), EImageBlitMode::COLORKEY);
std::vector<const CStack*> stacks = owner.getBattle()->battleGetAllStacks(true);
for(const CStack * s : stacks)
{
stackAdded(s, true);
}
}
BattleHex BattleStacksController::getStackCurrentPosition(const CStack * stack) const
{
if ( !stackAnimation.at(stack->unitId())->isMoving())
return stack->getPosition();
if (stack->hasBonusOfType(BonusType::FLYING) && stackAnimation.at(stack->unitId())->getType() == ECreatureAnimType::MOVING )
return BattleHex::HEX_AFTER_ALL;
for (auto & anim : currentAnimations)
{
// certainly ugly workaround but fixes quite annoying bug
// stack position will be updated only *after* movement is finished
// before this - stack is always at its initial position. Thus we need to find
// its current position. Which can be found only in this class
if (StackMoveAnimation *move = dynamic_cast<StackMoveAnimation*>(anim))
{
if (move->stack == stack)
return std::max(move->prevHex, move->nextHex);
}
}
return stack->getPosition();
}
void BattleStacksController::collectRenderableObjects(BattleRenderer & renderer)
{
auto stacks = owner.getBattle()->battleGetAllStacks(false);
for (auto stack : stacks)
{
if (stackAnimation.find(stack->unitId()) == stackAnimation.end()) //e.g. for summoned but not yet handled stacks
continue;
//FIXME: hack to ignore ghost stacks
if ((stackAnimation[stack->unitId()]->getType() == ECreatureAnimType::DEAD || stackAnimation[stack->unitId()]->getType() == ECreatureAnimType::HOLDING) && stack->isGhost())
continue;
auto layer = stackAnimation[stack->unitId()]->isDead() ? EBattleFieldLayer::CORPSES : EBattleFieldLayer::STACKS;
auto location = getStackCurrentPosition(stack);
renderer.insert(layer, location, [this, stack]( BattleRenderer::RendererRef renderer ){
showStack(renderer, stack);
});
if (stackNeedsAmountBox(stack))
{
renderer.insert(EBattleFieldLayer::STACK_AMOUNTS, location, [this, stack]( BattleRenderer::RendererRef renderer ){
showStackAmountBox(renderer, stack);
});
}
}
}
void BattleStacksController::stackReset(const CStack * stack)
{
auto iter = stackAnimation.find(stack->unitId());
if(iter == stackAnimation.end())
{
logGlobal->error("Unit %d have no animation", stack->unitId());
return;
}
auto animation = iter->second;
if(stack->alive() && animation->isDeadOrDying())
{
owner.addToAnimationStage(EAnimationEvents::HIT, [=]()
{
addNewAnim(new ResurrectionAnimation(owner, stack));
});
}
}
void BattleStacksController::stackAdded(const CStack * stack, bool instant)
{
// Tower shooters have only their upper half visible
static const int turretCreatureAnimationHeight = 232;
stackFacingRight[stack->unitId()] = stack->unitSide() == BattleSide::ATTACKER; // must be set before getting stack position
Point coords = getStackPositionAtHex(stack->getPosition(), stack);
if(stack->initialPosition < 0) //turret
{
assert(owner.siegeController);
const CCreature *turretCreature = owner.siegeController->getTurretCreature(stack->initialPosition);
stackAnimation[stack->unitId()] = AnimationControls::getAnimation(turretCreature);
stackAnimation[stack->unitId()]->pos.h = turretCreatureAnimationHeight;
stackAnimation[stack->unitId()]->pos.w = stackAnimation[stack->unitId()]->getWidth();
// FIXME: workaround for visible animation of Medusa tails (animation disabled in H3)
if (turretCreature->getId() == CreatureID::MEDUSA )
stackAnimation[stack->unitId()]->pos.w = 250;
coords = owner.siegeController->getTurretCreaturePosition(stack->initialPosition);
}
else
{
stackAnimation[stack->unitId()] = AnimationControls::getAnimation(stack->unitType());
stackAnimation[stack->unitId()]->onAnimationReset += std::bind(&onAnimationFinished, stack, stackAnimation[stack->unitId()]);
stackAnimation[stack->unitId()]->pos.h = stackAnimation[stack->unitId()]->getHeight();
stackAnimation[stack->unitId()]->pos.w = stackAnimation[stack->unitId()]->getWidth();
}
stackAnimation[stack->unitId()]->pos.x = coords.x;
stackAnimation[stack->unitId()]->pos.y = coords.y;
stackAnimation[stack->unitId()]->setType(ECreatureAnimType::HOLDING);
if (!instant)
{
// immediately make stack transparent, giving correct shifter time to start
auto shifterFade = ColorFilter::genAlphaShifter(0);
setStackColorFilter(shifterFade, stack, nullptr, true);
owner.addToAnimationStage(EAnimationEvents::HIT, [=]()
{
addNewAnim(new ColorTransformAnimation(owner, stack, "summonFadeIn", nullptr));
if (stack->isClone())
addNewAnim(new ColorTransformAnimation(owner, stack, "cloning", SpellID(SpellID::CLONE).toSpell() ));
});
}
}
void BattleStacksController::setActiveStack(const CStack *stack)
{
if (activeStack) // update UI
stackAnimation[activeStack->unitId()]->setBorderColor(AnimationControls::getNoBorder());
activeStack = stack;
if (activeStack) // update UI
stackAnimation[activeStack->unitId()]->setBorderColor(AnimationControls::getGoldBorder());
owner.windowObject->blockUI(activeStack == nullptr);
if (activeStack)
stackAmountBoxHidden.clear();
}
bool BattleStacksController::stackNeedsAmountBox(const CStack * stack) const
{
//do not show box for singular war machines, stacked war machines with box shown are supported as extension feature
if(stack->hasBonusOfType(BonusType::SIEGE_WEAPON) && stack->getCount() == 1)
return false;
if(!stack->alive())
return false;
//hide box when target is going to die anyway - do not display "0 creatures"
if(stack->getCount() == 0)
return false;
// if stack has any ongoing animation - hide the box
if (stackAmountBoxHidden.count(stack->unitId()))
return false;
return true;
}
std::shared_ptr<IImage> BattleStacksController::getStackAmountBox(const CStack * stack)
{
std::vector<SpellID> activeSpells = stack->activeSpells();
if ( activeSpells.empty())
return amountNormal;
int effectsPositivness = 0;
for(const auto & spellID : activeSpells)
{
auto positiveness = CGI->spells()->getByIndex(spellID)->getPositiveness();
if(!boost::logic::indeterminate(positiveness))
{
if(positiveness)
effectsPositivness++;
else
effectsPositivness--;
}
}
if (effectsPositivness > 0)
return amountPositive;
if (effectsPositivness < 0)
return amountNegative;
return amountEffNeutral;
}
void BattleStacksController::showStackAmountBox(Canvas & canvas, const CStack * stack)
{
auto amountBG = getStackAmountBox(stack);
bool doubleWide = stack->doubleWide();
bool turnedRight = facingRight(stack);
bool attacker = stack->unitSide() == BattleSide::ATTACKER;
BattleHex stackPos = stack->getPosition();
// double-wide unit turned around - use opposite hex for stack label
if (doubleWide && turnedRight != attacker)
stackPos = stack->occupiedHex();
BattleHex frontPos = turnedRight ?
stackPos.cloneInDirection(BattleHex::RIGHT) :
stackPos.cloneInDirection(BattleHex::LEFT);
bool moveInside = !owner.fieldController->stackCountOutsideHex(frontPos);
Point boxPosition;
if (moveInside)
{
boxPosition = owner.fieldController->hexPositionLocal(stackPos).center() + Point(-15, 1);
}
else
{
if (turnedRight)
boxPosition = owner.fieldController->hexPositionLocal(frontPos).center() + Point (-22, 1);
else
boxPosition = owner.fieldController->hexPositionLocal(frontPos).center() + Point(-8, -14);
}
Point textPosition = Point(amountBG->dimensions().x/2 + boxPosition.x, boxPosition.y + amountBG->dimensions().y/2);
canvas.draw(amountBG, boxPosition);
canvas.drawText(textPosition, EFonts::FONT_TINY, Colors::WHITE, ETextAlignment::CENTER, TextOperations::formatMetric(stack->getCount(), 4));
}
void BattleStacksController::showStack(Canvas & canvas, const CStack * stack)
{
ColorFilter fullFilter = ColorFilter::genEmptyShifter();
for(const auto & filter : stackFilterEffects)
{
if (filter.target == stack)
fullFilter = ColorFilter::genCombined(fullFilter, filter.effect);
}
stackAnimation[stack->unitId()]->nextFrame(canvas, fullFilter, facingRight(stack)); // do actual blit
}
void BattleStacksController::tick(uint32_t msPassed)
{
updateHoveredStacks();
updateBattleAnimations(msPassed);
}
void BattleStacksController::initializeBattleAnimations()
{
auto copiedVector = currentAnimations;
for (auto & elem : copiedVector)
if (elem && !elem->isInitialized())
elem->tryInitialize();
}
void BattleStacksController::tickFrameBattleAnimations(uint32_t msPassed)
{
for (auto stack : owner.getBattle()->battleGetAllStacks(true))
{
if (stackAnimation.find(stack->unitId()) == stackAnimation.end()) //e.g. for summoned but not yet handled stacks
continue;
stackAnimation[stack->unitId()]->incrementFrame(msPassed / 1000.f);
}
// operate on copy - to prevent potential iterator invalidation due to push_back's
// FIXME? : remove remaining calls to addNewAnim from BattleAnimation::nextFrame (only Catapult explosion at the time of writing)
auto copiedVector = currentAnimations;
for (auto & elem : copiedVector)
if (elem && elem->isInitialized())
elem->tick(msPassed);
}
void BattleStacksController::updateBattleAnimations(uint32_t msPassed)
{
bool hadAnimations = !currentAnimations.empty();
initializeBattleAnimations();
tickFrameBattleAnimations(msPassed);
vstd::erase(currentAnimations, nullptr);
if (currentAnimations.empty())
owner.executeStagedAnimations();
if (hadAnimations && currentAnimations.empty())
owner.onAnimationsFinished();
initializeBattleAnimations();
}
void BattleStacksController::addNewAnim(BattleAnimation *anim)
{
if (currentAnimations.empty())
stackAmountBoxHidden.clear();
owner.onAnimationsStarted();
currentAnimations.push_back(anim);
auto stackAnimation = dynamic_cast<BattleStackAnimation*>(anim);
if(stackAnimation)
stackAmountBoxHidden.insert(stackAnimation->stack->unitId());
}
void BattleStacksController::stackRemoved(uint32_t stackID)
{
if (getActiveStack() && getActiveStack()->unitId() == stackID)
setActiveStack(nullptr);
}
void BattleStacksController::stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos)
{
owner.addToAnimationStage(EAnimationEvents::HIT, [=](){
// remove any potentially erased petrification effect
removeExpiredColorFilters();
});
for(auto & attackedInfo : attackedInfos)
{
if (!attackedInfo.attacker)
continue;
// In H3, attacked stack will not reverse on ranged attack
if (attackedInfo.indirectAttack)
continue;
// Another type of indirect attack - dragon breath
if (!CStack::isMeleeAttackPossible(attackedInfo.attacker, attackedInfo.defender))
continue;
// defender need to face in direction opposited to out attacker
bool needsReverse = shouldAttackFacingRight(attackedInfo.attacker, attackedInfo.defender) == facingRight(attackedInfo.defender);
// FIXME: this check is better, however not usable since stacksAreAttacked is called after net pack is applied - petrification is already removed
// if (needsReverse && !attackedInfo.defender->isFrozen())
if (needsReverse && stackAnimation[attackedInfo.defender->unitId()]->getType() != ECreatureAnimType::FROZEN)
{
owner.addToAnimationStage(EAnimationEvents::MOVEMENT, [=]()
{
addNewAnim(new ReverseAnimation(owner, attackedInfo.defender, attackedInfo.defender->getPosition()));
});
}
}
for(auto & attackedInfo : attackedInfos)
{
bool useDeathAnim = attackedInfo.killed;
bool useDefenceAnim = attackedInfo.defender->defendingAnim && !attackedInfo.indirectAttack && !attackedInfo.killed;
EAnimationEvents usedEvent = useDefenceAnim ? EAnimationEvents::ATTACK : EAnimationEvents::HIT;
owner.addToAnimationStage(usedEvent, [=]()
{
if (useDeathAnim)
addNewAnim(new DeathAnimation(owner, attackedInfo.defender, attackedInfo.indirectAttack));
else if(useDefenceAnim)
addNewAnim(new DefenceAnimation(owner, attackedInfo.defender));
else
addNewAnim(new HittedAnimation(owner, attackedInfo.defender));
if (attackedInfo.fireShield)
owner.effectsController->displayEffect(EBattleEffect::FIRE_SHIELD, AudioPath::builtin("FIRESHIE"), attackedInfo.attacker->getPosition());
if (attackedInfo.spellEffect != SpellID::NONE)
{
auto spell = attackedInfo.spellEffect.toSpell();
if (!spell->getCastSound().empty())
CCS->soundh->playSound(spell->getCastSound());
owner.displaySpellEffect(spell, attackedInfo.defender->getPosition());
}
});
}
for (auto & attackedInfo : attackedInfos)
{
if (attackedInfo.rebirth)
{
owner.addToAnimationStage(EAnimationEvents::AFTER_HIT, [=](){
owner.effectsController->displayEffect(EBattleEffect::RESURRECT, AudioPath::builtin("RESURECT"), attackedInfo.defender->getPosition());
addNewAnim(new ResurrectionAnimation(owner, attackedInfo.defender));
});
}
if (attackedInfo.killed && attackedInfo.defender->summoned)
{
owner.addToAnimationStage(EAnimationEvents::AFTER_HIT, [=](){
addNewAnim(new ColorTransformAnimation(owner, attackedInfo.defender, "summonFadeOut", nullptr));
stackRemoved(attackedInfo.defender->unitId());
});
}
}
owner.executeStagedAnimations();
owner.waitForAnimations();
}
void BattleStacksController::stackTeleported(const CStack *stack, const BattleHexArray & destHex, int distance)
{
assert(destHex.size() > 0);
//owner.checkForAnimations(); // NOTE: at this point spellcast animations were added, but not executed
owner.addToAnimationStage(EAnimationEvents::HIT, [=](){
addNewAnim( new ColorTransformAnimation(owner, stack, "teleportFadeOut", nullptr) );
});
owner.addToAnimationStage(EAnimationEvents::AFTER_HIT, [=](){
stackAnimation[stack->unitId()]->pos.moveTo(getStackPositionAtHex(destHex.back(), stack));
addNewAnim( new ColorTransformAnimation(owner, stack, "teleportFadeIn", nullptr) );
});
// animations will be executed by spell
}
void BattleStacksController::stackMoved(const CStack *stack, const BattleHexArray & destHex, int distance)
{
assert(destHex.size() > 0);
owner.checkForAnimations();
if(shouldRotate(stack, stack->getPosition(), destHex[0]))
{
owner.addToAnimationStage(EAnimationEvents::ROTATE, [&]()
{
addNewAnim(new ReverseAnimation(owner, stack, stack->getPosition()));
});
}
owner.addToAnimationStage(EAnimationEvents::MOVE_START, [&]()
{
addNewAnim(new MovementStartAnimation(owner, stack));
});
if (!stack->hasBonus(Selector::typeSubtype(BonusType::FLYING, BonusCustomSubtype::movementTeleporting)))
{
owner.addToAnimationStage(EAnimationEvents::MOVEMENT, [&]()
{
addNewAnim(new MovementAnimation(owner, stack, destHex, distance));
});
}
owner.addToAnimationStage(EAnimationEvents::MOVE_END, [&]()
{
addNewAnim(new MovementEndAnimation(owner, stack, destHex.back()));
});
owner.executeStagedAnimations();
owner.waitForAnimations();
}
bool BattleStacksController::shouldAttackFacingRight(const CStack * attacker, const CStack * defender)
{
bool mustReverse = owner.getBattle()->isToReverse(attacker, defender);
if (attacker->unitSide() == BattleSide::ATTACKER)
return !mustReverse;
else
return mustReverse;
}
void BattleStacksController::stackAttacking( const StackAttackInfo & info )
{
owner.checkForAnimations();
auto attacker = info.attacker;
auto defender = info.defender;
auto tile = info.tile;
auto spellEffect = info.spellEffect;
auto multiAttack = !info.secondaryDefender.empty();
bool needsReverse = false;
if (info.indirectAttack)
{
needsReverse = shouldRotate(attacker, attacker->position, info.tile);
}
else
{
needsReverse = shouldAttackFacingRight(attacker, defender) != facingRight(attacker);
}
if (needsReverse)
{
owner.addToAnimationStage(EAnimationEvents::MOVEMENT, [=]()
{
addNewAnim(new ReverseAnimation(owner, attacker, attacker->getPosition()));
});
}
if(info.lucky)
{
owner.addToAnimationStage(EAnimationEvents::BEFORE_HIT, [=]() {
owner.appendBattleLog(info.attacker->formatGeneralMessage(-45));
owner.effectsController->displayEffect(EBattleEffect::GOOD_LUCK, AudioPath::builtin("GOODLUCK"), attacker->getPosition());
});
}
if(info.unlucky)
{
owner.addToAnimationStage(EAnimationEvents::BEFORE_HIT, [=]() {
owner.appendBattleLog(info.attacker->formatGeneralMessage(-44));
owner.effectsController->displayEffect(EBattleEffect::BAD_LUCK, AudioPath::builtin("BADLUCK"), attacker->getPosition());
});
}
if(info.deathBlow)
{
owner.addToAnimationStage(EAnimationEvents::BEFORE_HIT, [=]() {
owner.appendBattleLog(info.attacker->formatGeneralMessage(365));
owner.effectsController->displayEffect(EBattleEffect::DEATH_BLOW, AudioPath::builtin("DEATHBLO"), defender->getPosition());
});
for(auto elem : info.secondaryDefender)
{
owner.addToAnimationStage(EAnimationEvents::BEFORE_HIT, [=]() {
owner.effectsController->displayEffect(EBattleEffect::DEATH_BLOW, elem->getPosition());
});
}
}
owner.addToAnimationStage(EAnimationEvents::ATTACK, [=]()
{
if (info.indirectAttack)
{
addNewAnim(new ShootingAnimation(owner, attacker, tile, defender));
}
else
{
addNewAnim(new MeleeAttackAnimation(owner, attacker, tile, defender, multiAttack));
}
});
if (info.spellEffect != SpellID::NONE)
{
owner.addToAnimationStage(EAnimationEvents::HIT, [=]()
{
owner.displaySpellHit(spellEffect.toSpell(), tile);
});
}
if (info.lifeDrain)
{
owner.addToAnimationStage(EAnimationEvents::AFTER_HIT, [=]()
{
owner.effectsController->displayEffect(EBattleEffect::DRAIN_LIFE, AudioPath::builtin("DRAINLIF"), attacker->getPosition(), 0.5);
});
}
//return, animation playback will be handled by stacksAreAttacked
}
bool BattleStacksController::shouldRotate(const CStack * stack, const BattleHex & oldPos, const BattleHex & nextHex) const
{
Point begPosition = getStackPositionAtHex(oldPos,stack);
Point endPosition = getStackPositionAtHex(nextHex, stack);
if((begPosition.x > endPosition.x) && facingRight(stack))
return true;
else if((begPosition.x < endPosition.x) && !facingRight(stack))
return true;
return false;
}
void BattleStacksController::endAction(const BattleAction & action)
{
owner.checkForAnimations();
//check if we should reverse stacks
TStacks stacks = owner.getBattle()->battleGetStacks(CPlayerBattleCallback::MINE_AND_ENEMY);
for (const CStack *s : stacks)
{
bool shouldFaceRight = s && s->unitSide() == BattleSide::ATTACKER;
if (s && facingRight(s) != shouldFaceRight && s->alive() && stackAnimation[s->unitId()]->isIdle())
{
addNewAnim(new ReverseAnimation(owner, s, s->getPosition()));
}
}
owner.executeStagedAnimations();
owner.waitForAnimations();
stackAmountBoxHidden.clear();
owner.windowObject->blockUI(activeStack == nullptr);
removeExpiredColorFilters();
}
void BattleStacksController::startAction(const BattleAction & action)
{
// if timer run out and we did not act in time - deactivate current stack
setActiveStack(nullptr);
removeExpiredColorFilters();
}
void BattleStacksController::stackActivated(const CStack *stack)
{
stackToActivate = stack;
owner.waitForAnimations();
logAnim->debug("Activating next stack");
owner.activateStack();
}
void BattleStacksController::deactivateStack()
{
if (!activeStack) {
return;
}
stackToActivate = activeStack;
setActiveStack(nullptr);
}
void BattleStacksController::activateStack()
{
if ( !currentAnimations.empty())
return;
if ( !stackToActivate)
return;
owner.trySetActivePlayer(stackToActivate->unitOwner());
setActiveStack(stackToActivate);
stackToActivate = nullptr;
const CStack * s = getActiveStack();
if(!s)
return;
}
const CStack* BattleStacksController::getActiveStack() const
{
return activeStack;
}
bool BattleStacksController::facingRight(const CStack * stack) const
{
return stackFacingRight.at(stack->unitId());
}
Point BattleStacksController::getStackPositionAtHex(const BattleHex & hexNum, const CStack * stack) const
{
Point ret(-500, -500); //returned value
if(stack && stack->initialPosition < 0) //creatures in turrets
return owner.siegeController->getTurretCreaturePosition(stack->initialPosition);
static const Point basePos(-189, -139); // position of creature in topleft corner
static const int imageShiftX = 29; // X offset to base pos for facing right stacks, negative for facing left
ret.x = basePos.x + 22 * ( (hexNum.getY() + 1)%2 ) + 44 * hexNum.getX();
ret.y = basePos.y + 42 * hexNum.getY();
if (stack)
{
if(facingRight(stack))
ret.x += imageShiftX;
else
ret.x -= imageShiftX;
//shifting position for double - hex creatures
if(stack->doubleWide())
{
if(stack->unitSide() == BattleSide::ATTACKER)
{
if(facingRight(stack))
ret.x -= 44;
}
else
{
if(!facingRight(stack))
ret.x += 44;
}
}
}
//returning
return ret;
}
void BattleStacksController::setStackColorFilter(const ColorFilter & effect, const CStack * target, const CSpell * source, bool persistent)
{
for (auto & filter : stackFilterEffects)
{
if (filter.target == target && filter.source == source)
{
filter.effect = effect;
filter.persistent = persistent;
return;
}
}
stackFilterEffects.push_back({ effect, target, source, persistent });
}
void BattleStacksController::removeExpiredColorFilters()
{
vstd::erase_if(stackFilterEffects, [&](const BattleStackFilterEffect & filter)
{
if (!filter.persistent)
{
if (filter.source && !filter.target->hasBonus(Selector::source(BonusSource::SPELL_EFFECT, BonusSourceID(filter.source->id)), Selector::all))
return true;
if (filter.effect == ColorFilter::genEmptyShifter())
return true;
}
return false;
});
}
void BattleStacksController::updateHoveredStacks()
{
auto newStacks = selectHoveredStacks();
if(newStacks.size() == 0)
owner.windowObject->updateStackInfoWindow(nullptr);
for(const auto * stack : mouseHoveredStacks)
{
if (vstd::contains(newStacks, stack))
continue;
if (stack == activeStack)
stackAnimation[stack->unitId()]->setBorderColor(AnimationControls::getGoldBorder());
else
stackAnimation[stack->unitId()]->setBorderColor(AnimationControls::getNoBorder());
}
for(const auto * stack : newStacks)
{
if (vstd::contains(mouseHoveredStacks, stack))
continue;
owner.windowObject->updateStackInfoWindow(newStacks.size() == 1 && vstd::find_pos(newStacks, stack) == 0 ? stack : nullptr);
stackAnimation[stack->unitId()]->setBorderColor(AnimationControls::getBlueBorder());
if (stackAnimation[stack->unitId()]->framesInGroup(ECreatureAnimType::MOUSEON) > 0 && stack->alive() && !stack->isFrozen())
stackAnimation[stack->unitId()]->playOnce(ECreatureAnimType::MOUSEON);
}
if(mouseHoveredStacks != newStacks)
GH.windows().totalRedraw(); //fix for frozen stack info window and blue border in action bar
mouseHoveredStacks = newStacks;
}
std::vector<const CStack *> BattleStacksController::selectHoveredStacks()
{
// only allow during our turn - do not try to highlight creatures while they are in the middle of actions
if (!activeStack)
return {};
if(owner.hasAnimations())
return {};
auto hoveredQueueUnitId = owner.windowObject->getQueueHoveredUnitId();
if(hoveredQueueUnitId.has_value())
{
return { owner.getBattle()->battleGetStackByID(hoveredQueueUnitId.value(), true) };
}
auto hoveredHex = owner.fieldController->getHoveredHex();
if (!hoveredHex.isValid())
return {};
const spells::Caster *caster = nullptr;
const CSpell *spell = nullptr;
spells::Mode mode = owner.actionsController->getCurrentCastMode();
spell = owner.actionsController->getCurrentSpell(hoveredHex);
caster = owner.actionsController->getCurrentSpellcaster();
//casting spell or in explicit spellcasting mode that also handles SPELL_LIKE_ATTACK
if(caster && spell && (owner.actionsController->currentActionSpellcasting(hoveredHex) || owner.actionsController->creatureSpellcastingModeActive()))
{
spells::Target target;
target.emplace_back(hoveredHex);
spells::BattleCast event(owner.getBattle().get(), caster, mode, spell);
auto mechanics = spell->battleMechanics(&event);
return mechanics->getAffectedStacks(target);
}
if(hoveredHex.isValid())
{
const CStack * const stack = owner.getBattle()->battleGetStackByPos(hoveredHex, true);
if (stack)
return {stack};
}
return {};
}
const std::vector<uint32_t> BattleStacksController::getHoveredStacksUnitIds() const
{
auto result = std::vector<uint32_t>();
for(const auto * stack : mouseHoveredStacks)
{
result.push_back(stack->unitId());
}
return result;
}
|