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
|
/*
* BattleInterface.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 "BattleInterface.h"
#include "BattleAnimationClasses.h"
#include "BattleActionsController.h"
#include "BattleInterfaceClasses.h"
#include "CreatureAnimation.h"
#include "BattleProjectileController.h"
#include "BattleEffectsController.h"
#include "BattleObstacleController.h"
#include "BattleSiegeController.h"
#include "BattleFieldController.h"
#include "BattleWindow.h"
#include "BattleStacksController.h"
#include "BattleRenderer.h"
#include "../CGameInfo.h"
#include "../CPlayerInterface.h"
#include "../gui/CursorHandler.h"
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
#include "../media/IMusicPlayer.h"
#include "../media/ISoundPlayer.h"
#include "../windows/CTutorialWindow.h"
#include "../render/Canvas.h"
#include "../adventureMap/AdventureMapInterface.h"
#include "../../CCallback.h"
#include "../../lib/BattleFieldHandler.h"
#include "../../lib/CStack.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/texts/CGeneralTextHandler.h"
#include "../../lib/gameState/InfoAboutArmy.h"
#include "../../lib/mapObjects/CGTownInstance.h"
#include "../../lib/networkPacks/PacksForClientBattle.h"
#include "../../lib/UnlockGuard.h"
#include "../../lib/TerrainHandler.h"
#include "../../lib/CThreadHelper.h"
BattleInterface::BattleInterface(const BattleID & battleID, const CCreatureSet *army1, const CCreatureSet *army2,
const CGHeroInstance *hero1, const CGHeroInstance *hero2,
std::shared_ptr<CPlayerInterface> att,
std::shared_ptr<CPlayerInterface> defen,
std::shared_ptr<CPlayerInterface> spectatorInt)
: attackingHeroInstance(hero1)
, defendingHeroInstance(hero2)
, attackerInt(att)
, defenderInt(defen)
, curInt(att)
, battleID(battleID)
, battleOpeningDelayActive(true)
, round(0)
{
if(spectatorInt)
{
curInt = spectatorInt;
}
else if(!curInt)
{
//May happen when we are defending during network MP game -> attacker interface is just not present
curInt = defenderInt;
}
//hot-seat -> check tactics for both players (defender may be local human)
if(attackerInt && attackerInt->cb->getBattle(getBattleID())->battleGetTacticDist())
tacticianInterface = attackerInt;
else if(defenderInt && defenderInt->cb->getBattle(getBattleID())->battleGetTacticDist())
tacticianInterface = defenderInt;
//if we found interface of player with tactics, then enter tactics mode
tacticsMode = static_cast<bool>(tacticianInterface);
//initializing armies
this->army1 = army1;
this->army2 = army2;
const CGTownInstance *town = getBattle()->battleGetDefendedTown();
if(town && town->fortificationsLevel().wallsHealth > 0)
siegeController.reset(new BattleSiegeController(*this, town));
windowObject = std::make_shared<BattleWindow>(*this);
projectilesController.reset(new BattleProjectileController(*this));
stacksController.reset( new BattleStacksController(*this));
actionsController.reset( new BattleActionsController(*this));
effectsController.reset(new BattleEffectsController(*this));
obstacleController.reset(new BattleObstacleController(*this));
adventureInt->onAudioPaused();
ongoingAnimationsState.setBusy();
GH.windows().pushWindow(windowObject);
windowObject->blockUI(true);
windowObject->updateQueue();
playIntroSoundAndUnlockInterface();
}
void BattleInterface::playIntroSoundAndUnlockInterface()
{
auto onIntroPlayed = [this]()
{
// Make sure that battle have not ended while intro was playing AND that a different one has not started
if(LOCPLINT->battleInt.get() == this)
onIntroSoundPlayed();
};
auto bfieldType = getBattle()->battleGetBattlefieldType();
const auto & battlefieldSound = bfieldType.getInfo()->musicFilename;
std::vector<soundBase::soundID> battleIntroSounds =
{
soundBase::battle00, soundBase::battle01,
soundBase::battle02, soundBase::battle03, soundBase::battle04,
soundBase::battle05, soundBase::battle06, soundBase::battle07
};
int battleIntroSoundChannel = -1;
if (!battlefieldSound.empty())
battleIntroSoundChannel = CCS->soundh->playSound(battlefieldSound);
else
battleIntroSoundChannel = CCS->soundh->playSoundFromSet(battleIntroSounds);
if (battleIntroSoundChannel != -1)
{
CCS->soundh->setCallback(battleIntroSoundChannel, onIntroPlayed);
if (settings["gameTweaks"]["skipBattleIntroMusic"].Bool())
openingEnd();
}
else // failed to play sound
{
onIntroSoundPlayed();
}
}
bool BattleInterface::openingPlaying() const
{
return battleOpeningDelayActive;
}
void BattleInterface::onIntroSoundPlayed()
{
if (openingPlaying())
openingEnd();
auto bfieldType = getBattle()->battleGetBattlefieldType();
const auto & battlefieldMusic = bfieldType.getInfo()->musicFilename;
if (!battlefieldMusic.empty())
CCS->musich->playMusic(battlefieldMusic, true, true);
else
CCS->musich->playMusicFromSet("battle", true, true);
}
void BattleInterface::openingEnd()
{
assert(openingPlaying());
if (!openingPlaying())
return;
onAnimationsFinished();
if(tacticsMode)
tacticNextStack(nullptr);
activateStack();
battleOpeningDelayActive = false;
CTutorialWindow::openWindowFirstTime(TutorialMode::TOUCH_BATTLE);
}
BattleInterface::~BattleInterface()
{
CPlayerInterface::battleInt = nullptr;
if (adventureInt)
adventureInt->onAudioResumed();
awaitingEvents.clear();
onAnimationsFinished();
}
void BattleInterface::redrawBattlefield()
{
fieldController->redrawBackgroundWithHexes();
GH.windows().totalRedraw();
}
void BattleInterface::stackReset(const CStack * stack)
{
stacksController->stackReset(stack);
}
void BattleInterface::stackAdded(const CStack * stack)
{
stacksController->stackAdded(stack, false);
}
void BattleInterface::stackRemoved(uint32_t stackID)
{
stacksController->stackRemoved(stackID);
fieldController->redrawBackgroundWithHexes();
windowObject->updateQueue();
}
void BattleInterface::stackActivated(const CStack *stack)
{
stacksController->stackActivated(stack);
}
void BattleInterface::stackMoved(const CStack *stack, const BattleHexArray & destHex, int distance, bool teleport)
{
if (teleport)
stacksController->stackTeleported(stack, destHex, distance);
else
stacksController->stackMoved(stack, destHex, distance);
}
void BattleInterface::stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos)
{
stacksController->stacksAreAttacked(attackedInfos);
BattleSideArray<int> killedBySide;
for(const StackAttackedInfo & attackedInfo : attackedInfos)
{
BattleSide side = attackedInfo.defender->unitSide();
killedBySide.at(side) += attackedInfo.amountKilled;
}
for(BattleSide side : { BattleSide::ATTACKER, BattleSide::DEFENDER })
{
if(killedBySide.at(side) > killedBySide.at(getBattle()->otherSide(side)))
setHeroAnimation(side, EHeroAnimType::DEFEAT);
else if(killedBySide.at(side) < killedBySide.at(getBattle()->otherSide(side)))
setHeroAnimation(side, EHeroAnimType::VICTORY);
}
}
void BattleInterface::stackAttacking( const StackAttackInfo & attackInfo )
{
stacksController->stackAttacking(attackInfo);
}
void BattleInterface::newRoundFirst()
{
waitForAnimations();
}
void BattleInterface::newRound()
{
console->addText(CGI->generaltexth->allTexts[412]);
round++;
}
void BattleInterface::giveCommand(EActionType action, const BattleHex & tile, SpellID spell)
{
const CStack * actor = nullptr;
if(action != EActionType::HERO_SPELL && action != EActionType::RETREAT && action != EActionType::SURRENDER)
{
actor = stacksController->getActiveStack();
}
auto side = getBattle()->playerToSide(curInt->playerID);
if(side == BattleSide::NONE)
{
logGlobal->error("Player %s is not in battle", curInt->playerID.toString());
return;
}
BattleAction ba;
ba.side = side;
ba.actionType = action;
ba.aimToHex(tile);
ba.spell = spell;
sendCommand(ba, actor);
}
void BattleInterface::sendCommand(BattleAction command, const CStack * actor)
{
command.stackNumber = actor ? actor->unitId() : ((command.side == BattleSide::ATTACKER) ? -1 : -2);
if(!tacticsMode)
{
logGlobal->trace("Setting command for %s", (actor ? actor->nodeName() : "hero"));
stacksController->setActiveStack(nullptr);
curInt->cb->battleMakeUnitAction(battleID, command);
}
else
{
curInt->cb->battleMakeTacticAction(battleID, command);
stacksController->setActiveStack(nullptr);
//next stack will be activated when action ends
}
CCS->curh->set(Cursor::Combat::POINTER);
}
const CGHeroInstance * BattleInterface::getActiveHero()
{
const CStack *attacker = stacksController->getActiveStack();
if(!attacker)
{
return nullptr;
}
if(attacker->unitSide() == BattleSide::ATTACKER)
{
return attackingHeroInstance;
}
return defendingHeroInstance;
}
void BattleInterface::stackIsCatapulting(const CatapultAttack & ca)
{
if (siegeController)
siegeController->stackIsCatapulting(ca);
}
void BattleInterface::gateStateChanged(const EGateState state)
{
if (siegeController)
siegeController->gateStateChanged(state);
}
void BattleInterface::battleFinished(const BattleResult& br, QueryID queryID)
{
checkForAnimations();
stacksController->setActiveStack(nullptr);
CCS->curh->set(Cursor::Map::POINTER);
curInt->waitWhileDialog();
if(settings["session"]["spectate"].Bool() && settings["session"]["spectate-skip-battle-result"].Bool())
{
curInt->cb->selectionMade(0, queryID);
windowObject->close();
return;
}
auto wnd = std::make_shared<BattleResultWindow>(br, *(this->curInt));
wnd->resultCallback = [=](ui32 selection)
{
curInt->cb->selectionMade(selection, queryID);
};
GH.windows().pushWindow(wnd);
curInt->waitWhileDialog(); // Avoid freeze when AI end turn after battle. Check bug #1897
CPlayerInterface::battleInt.reset();
}
void BattleInterface::spellCast(const BattleSpellCast * sc)
{
// Do not deactivate anything in tactics mode
// This is battlefield setup spells
if(!tacticsMode)
{
windowObject->blockUI(true);
// Disable current active stack duing the cast
// Store the current activeStack to stackToActivate
stacksController->deactivateStack();
}
CCS->curh->set(Cursor::Combat::BLOCKED);
const SpellID spellID = sc->spellID;
if(!spellID.hasValue())
return;
const CSpell * spell = spellID.toSpell();
auto targetedTile = sc->tile;
const AudioPath & castSoundPath = spell->getCastSound();
if (!castSoundPath.empty())
{
auto group = spell->animationInfo.projectile.empty() ?
EAnimationEvents::HIT:
EAnimationEvents::BEFORE_HIT;//FIXME: recheck whether this should be on projectile spawning
addToAnimationStage(group, [=]() {
CCS->soundh->playSound(castSoundPath);
});
}
if ( sc->activeCast )
{
const CStack * casterStack = getBattle()->battleGetStackByID(sc->casterStack);
if(casterStack != nullptr )
{
addToAnimationStage(EAnimationEvents::BEFORE_HIT, [=]()
{
stacksController->addNewAnim(new CastAnimation(*this, casterStack, targetedTile, getBattle()->battleGetStackByPos(targetedTile), spell));
displaySpellCast(spell, casterStack->getPosition());
});
}
else
{
auto hero = sc->side == BattleSide::DEFENDER ? defendingHero : attackingHero;
assert(hero);
addToAnimationStage(EAnimationEvents::BEFORE_HIT, [=]()
{
stacksController->addNewAnim(new HeroCastAnimation(*this, hero, targetedTile, getBattle()->battleGetStackByPos(targetedTile), spell));
});
}
}
addToAnimationStage(EAnimationEvents::HIT, [=](){
displaySpellHit(spell, targetedTile);
});
//queuing affect animation
for(auto & elem : sc->affectedCres)
{
auto stack = getBattle()->battleGetStackByID(elem, false);
assert(stack);
if(stack)
{
addToAnimationStage(EAnimationEvents::HIT, [=](){
displaySpellEffect(spell, stack->getPosition());
});
}
}
for(auto & elem : sc->reflectedCres)
{
auto stack = getBattle()->battleGetStackByID(elem, false);
assert(stack);
addToAnimationStage(EAnimationEvents::HIT, [=](){
effectsController->displayEffect(EBattleEffect::MAGIC_MIRROR, stack->getPosition());
});
}
if (!sc->resistedCres.empty())
{
addToAnimationStage(EAnimationEvents::HIT, [=](){
CCS->soundh->playSound(AudioPath::builtin("MAGICRES"));
});
}
for(auto & elem : sc->resistedCres)
{
auto stack = getBattle()->battleGetStackByID(elem, false);
assert(stack);
addToAnimationStage(EAnimationEvents::HIT, [=](){
effectsController->displayEffect(EBattleEffect::RESISTANCE, stack->getPosition());
});
}
//mana absorption
if (sc->manaGained > 0)
{
Point leftHero = Point(15, 30);
Point rightHero = Point(755, 30);
BattleSide side = sc->side;
addToAnimationStage(EAnimationEvents::AFTER_HIT, [=](){
stacksController->addNewAnim(new EffectAnimation(*this, AnimationPath::builtin(side == BattleSide::DEFENDER ? "SP07_A.DEF" : "SP07_B.DEF"), leftHero));
stacksController->addNewAnim(new EffectAnimation(*this, AnimationPath::builtin(side == BattleSide::DEFENDER ? "SP07_B.DEF" : "SP07_A.DEF"), rightHero));
});
}
// animations will be executed by spell effects
}
void BattleInterface::battleStacksEffectsSet(const SetStackEffect & sse)
{
if(stacksController->getActiveStack() != nullptr)
fieldController->redrawBackgroundWithHexes();
}
void BattleInterface::setHeroAnimation(BattleSide side, EHeroAnimType phase)
{
if(side == BattleSide::ATTACKER)
{
if(attackingHero)
attackingHero->setPhase(phase);
}
else
{
if(defendingHero)
defendingHero->setPhase(phase);
}
}
void BattleInterface::displayBattleLog(const std::vector<MetaString> & battleLog)
{
for(const auto & line : battleLog)
{
std::string formatted = line.toString();
boost::algorithm::trim(formatted);
appendBattleLog(formatted);
}
}
void BattleInterface::displaySpellAnimationQueue(const CSpell * spell, const CSpell::TAnimationQueue & q, const BattleHex & destinationTile, bool isHit)
{
for(const CSpell::TAnimation & animation : q)
{
if(animation.pause > 0)
stacksController->addNewAnim(new DummyAnimation(*this, animation.pause));
if (!animation.effectName.empty())
{
const CStack * destStack = getBattle()->battleGetStackByPos(destinationTile, false);
if (destStack)
stacksController->addNewAnim(new ColorTransformAnimation(*this, destStack, animation.effectName, spell ));
}
if(!animation.resourceName.empty())
{
int flags = 0;
if (isHit)
flags |= EffectAnimation::FORCE_ON_TOP;
if (animation.verticalPosition == VerticalPosition::BOTTOM)
flags |= EffectAnimation::ALIGN_TO_BOTTOM;
if (!destinationTile.isValid())
flags |= EffectAnimation::SCREEN_FILL;
if (!destinationTile.isValid())
stacksController->addNewAnim(new EffectAnimation(*this, animation.resourceName, flags, animation.transparency));
else
stacksController->addNewAnim(new EffectAnimation(*this, animation.resourceName, destinationTile, flags, animation.transparency));
}
}
}
void BattleInterface::displaySpellCast(const CSpell * spell, const BattleHex & destinationTile)
{
if(spell)
displaySpellAnimationQueue(spell, spell->animationInfo.cast, destinationTile, false);
}
void BattleInterface::displaySpellEffect(const CSpell * spell, const BattleHex & destinationTile)
{
if(spell)
displaySpellAnimationQueue(spell, spell->animationInfo.affect, destinationTile, false);
}
void BattleInterface::displaySpellHit(const CSpell * spell, const BattleHex & destinationTile)
{
if(spell)
displaySpellAnimationQueue(spell, spell->animationInfo.hit, destinationTile, true);
}
CPlayerInterface *BattleInterface::getCurrentPlayerInterface() const
{
return curInt.get();
}
void BattleInterface::trySetActivePlayer( PlayerColor player )
{
if ( attackerInt && attackerInt->playerID == player )
curInt = attackerInt;
if ( defenderInt && defenderInt->playerID == player )
curInt = defenderInt;
}
void BattleInterface::activateStack()
{
stacksController->activateStack();
const CStack * s = stacksController->getActiveStack();
if(!s)
return;
windowObject->updateQueue();
windowObject->blockUI(false);
fieldController->redrawBackgroundWithHexes();
actionsController->activateStack();
GH.fakeMouseMove();
}
bool BattleInterface::makingTurn() const
{
return stacksController->getActiveStack() != nullptr;
}
BattleID BattleInterface::getBattleID() const
{
return battleID;
}
std::shared_ptr<CPlayerBattleCallback> BattleInterface::getBattle() const
{
return curInt->cb->getBattle(battleID);
}
void BattleInterface::endAction(const BattleAction &action)
{
// it is possible that tactics mode ended while opening music is still playing
waitForAnimations();
const CStack *stack = getBattle()->battleGetStackByID(action.stackNumber);
// Activate stack from stackToActivate because this might have been temporary disabled, e.g., during spell cast
activateStack();
stacksController->endAction(action);
windowObject->updateQueue();
//stack ended movement in tactics phase -> select the next one
if (tacticsMode)
tacticNextStack(stack);
//we have activated next stack after sending request that has been just realized -> blockmap due to movement has changed
if(action.actionType == EActionType::HERO_SPELL)
fieldController->redrawBackgroundWithHexes();
}
void BattleInterface::appendBattleLog(const std::string & newEntry)
{
console->addText(newEntry);
}
void BattleInterface::startAction(const BattleAction & action)
{
if(action.actionType == EActionType::END_TACTIC_PHASE)
{
windowObject->tacticPhaseEnded();
return;
}
stacksController->startAction(action);
if (!action.isUnitAction())
return;
assert(getBattle()->battleGetStackByID(action.stackNumber));
windowObject->updateQueue();
effectsController->startAction(action);
}
void BattleInterface::tacticPhaseEnd()
{
stacksController->setActiveStack(nullptr);
tacticsMode = false;
auto side = tacticianInterface->cb->getBattle(battleID)->playerToSide(tacticianInterface->playerID);
auto action = BattleAction::makeEndOFTacticPhase(side);
tacticianInterface->cb->battleMakeTacticAction(battleID, action);
}
static bool immobile(const CStack *s)
{
return s->getMovementRange() == 0; //should bound stacks be immobile?
}
void BattleInterface::tacticNextStack(const CStack * current)
{
if (!current)
current = stacksController->getActiveStack();
//no switching stacks when the current one is moving
checkForAnimations();
TStacks stacksOfMine = tacticianInterface->cb->getBattle(battleID)->battleGetStacks(CPlayerBattleCallback::ONLY_MINE);
vstd::erase_if (stacksOfMine, &immobile);
if (stacksOfMine.empty())
{
tacticPhaseEnd();
return;
}
auto it = vstd::find(stacksOfMine, current);
if (it != stacksOfMine.end() && ++it != stacksOfMine.end())
stackActivated(*it);
else
stackActivated(stacksOfMine.front());
}
void BattleInterface::obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> oi)
{
obstacleController->obstaclePlaced(oi);
}
void BattleInterface::obstacleRemoved(const std::vector<ObstacleChanges> & obstacles)
{
obstacleController->obstacleRemoved(obstacles);
}
const CGHeroInstance *BattleInterface::currentHero() const
{
if (attackingHeroInstance && attackingHeroInstance->tempOwner == curInt->playerID)
return attackingHeroInstance;
if (defendingHeroInstance && defendingHeroInstance->tempOwner == curInt->playerID)
return defendingHeroInstance;
return nullptr;
}
InfoAboutHero BattleInterface::enemyHero() const
{
InfoAboutHero ret;
if (attackingHeroInstance->tempOwner == curInt->playerID)
curInt->cb->getHeroInfo(defendingHeroInstance, ret);
else
curInt->cb->getHeroInfo(attackingHeroInstance, ret);
return ret;
}
void BattleInterface::requestAutofightingAIToTakeAction()
{
assert(curInt->isAutoFightOn);
if(getBattle()->battleIsFinished())
{
return; // battle finished with spellcast
}
if (tacticsMode)
{
// Always end tactics mode. Player interface is blocked currently, so it's not possible that
// the AI can take any action except end tactics phase (AI actions won't be triggered)
//TODO implement the possibility that the AI will be triggered for further actions
//TODO any solution to merge tactics phase & normal phase in the way it is handled by the player and battle interface?
tacticPhaseEnd();
stacksController->setActiveStack(nullptr);
}
else
{
const CStack* activeStack = stacksController->getActiveStack();
// If enemy is moving, activeStack can be null
if (activeStack)
{
stacksController->setActiveStack(nullptr);
// FIXME: unsafe
// Run task in separate thread to avoid UI lock while AI is making turn (which might take some time)
// HOWEVER this thread won't atttempt to lock game state, potentially leading to races
boost::thread aiThread([localBattleID = battleID, localCurInt = curInt, activeStack]()
{
setThreadName("autofightingAI");
localCurInt->autofightingAI->activeStack(localBattleID, activeStack);
});
aiThread.detach();
}
}
}
void BattleInterface::castThisSpell(SpellID spellID)
{
actionsController->castThisSpell(spellID);
}
void BattleInterface::endNetwork()
{
ongoingAnimationsState.requestTermination();
}
void BattleInterface::executeStagedAnimations()
{
EAnimationEvents earliestStage = EAnimationEvents::COUNT;
for(const auto & event : awaitingEvents)
earliestStage = std::min(earliestStage, event.event);
if(earliestStage != EAnimationEvents::COUNT)
executeAnimationStage(earliestStage);
}
void BattleInterface::executeAnimationStage(EAnimationEvents event)
{
decltype(awaitingEvents) executingEvents;
for(auto it = awaitingEvents.begin(); it != awaitingEvents.end();)
{
if(it->event == event)
{
executingEvents.push_back(*it);
it = awaitingEvents.erase(it);
}
else
++it;
}
for(const auto & event : executingEvents)
event.action();
}
void BattleInterface::onAnimationsStarted()
{
ongoingAnimationsState.setBusy();
}
void BattleInterface::onAnimationsFinished()
{
ongoingAnimationsState.setFree();
}
void BattleInterface::waitForAnimations()
{
{
auto unlockInterface = vstd::makeUnlockGuard(GH.interfaceMutex);
ongoingAnimationsState.waitWhileBusy();
}
assert(!hasAnimations());
assert(awaitingEvents.empty());
if (!awaitingEvents.empty())
{
logGlobal->error("Wait for animations finished but we still have awaiting events!");
awaitingEvents.clear();
}
}
bool BattleInterface::hasAnimations()
{
return ongoingAnimationsState.isBusy();
}
void BattleInterface::checkForAnimations()
{
assert(!hasAnimations());
if(hasAnimations())
logGlobal->error("Unexpected animations state: expected all animations to be over, but some are still ongoing!");
waitForAnimations();
}
void BattleInterface::addToAnimationStage(EAnimationEvents event, const AwaitingAnimationAction & action)
{
awaitingEvents.push_back({action, event});
}
void BattleInterface::setBattleQueueVisibility(bool visible)
{
windowObject->hideQueue();
if(visible)
windowObject->showQueue();
}
void BattleInterface::setStickyHeroWindowsVisibility(bool visible)
{
windowObject->hideStickyHeroWindows();
if(visible)
windowObject->showStickyHeroWindows();
}
void BattleInterface::setStickyQuickSpellWindowVisibility(bool visible)
{
windowObject->hideStickyQuickSpellWindow();
if(visible)
windowObject->showStickyQuickSpellWindow();
}
|