BattleInterface.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * BattleInterface.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "BattleInterface.h"
  12. #include "BattleAnimationClasses.h"
  13. #include "BattleActionsController.h"
  14. #include "BattleInterfaceClasses.h"
  15. #include "CreatureAnimation.h"
  16. #include "BattleProjectileController.h"
  17. #include "BattleEffectsController.h"
  18. #include "BattleObstacleController.h"
  19. #include "BattleSiegeController.h"
  20. #include "BattleFieldController.h"
  21. #include "BattleWindow.h"
  22. #include "BattleStacksController.h"
  23. #include "BattleRenderer.h"
  24. #include "../CGameInfo.h"
  25. #include "../CMusicHandler.h"
  26. #include "../CPlayerInterface.h"
  27. #include "../gui/CursorHandler.h"
  28. #include "../gui/CGuiHandler.h"
  29. #include "../gui/WindowHandler.h"
  30. #include "../render/Canvas.h"
  31. #include "../adventureMap/AdventureMapInterface.h"
  32. #include "../../CCallback.h"
  33. #include "../../lib/CStack.h"
  34. #include "../../lib/CConfigHandler.h"
  35. #include "../../lib/CGeneralTextHandler.h"
  36. #include "../../lib/CHeroHandler.h"
  37. #include "../../lib/CondSh.h"
  38. #include "../../lib/gameState/InfoAboutArmy.h"
  39. #include "../../lib/mapObjects/CGTownInstance.h"
  40. #include "../../lib/NetPacks.h"
  41. #include "../../lib/UnlockGuard.h"
  42. #include "../../lib/TerrainHandler.h"
  43. CondSh<BattleAction *> BattleInterface::givenCommand(nullptr);
  44. BattleInterface::BattleInterface(const CCreatureSet *army1, const CCreatureSet *army2,
  45. const CGHeroInstance *hero1, const CGHeroInstance *hero2,
  46. std::shared_ptr<CPlayerInterface> att,
  47. std::shared_ptr<CPlayerInterface> defen,
  48. std::shared_ptr<CPlayerInterface> spectatorInt)
  49. : attackingHeroInstance(hero1)
  50. , defendingHeroInstance(hero2)
  51. , attackerInt(att)
  52. , defenderInt(defen)
  53. , curInt(att)
  54. , battleOpeningDelayActive(true)
  55. {
  56. if(spectatorInt)
  57. {
  58. curInt = spectatorInt;
  59. }
  60. else if(!curInt)
  61. {
  62. //May happen when we are defending during network MP game -> attacker interface is just not present
  63. curInt = defenderInt;
  64. }
  65. givenCommand.setn(nullptr);
  66. //hot-seat -> check tactics for both players (defender may be local human)
  67. if(attackerInt && attackerInt->cb->battleGetTacticDist())
  68. tacticianInterface = attackerInt;
  69. else if(defenderInt && defenderInt->cb->battleGetTacticDist())
  70. tacticianInterface = defenderInt;
  71. //if we found interface of player with tactics, then enter tactics mode
  72. tacticsMode = static_cast<bool>(tacticianInterface);
  73. //initializing armies
  74. this->army1 = army1;
  75. this->army2 = army2;
  76. const CGTownInstance *town = curInt->cb->battleGetDefendedTown();
  77. if(town && town->hasFort())
  78. siegeController.reset(new BattleSiegeController(*this, town));
  79. windowObject = std::make_shared<BattleWindow>(*this);
  80. projectilesController.reset(new BattleProjectileController(*this));
  81. stacksController.reset( new BattleStacksController(*this));
  82. actionsController.reset( new BattleActionsController(*this));
  83. effectsController.reset(new BattleEffectsController(*this));
  84. obstacleController.reset(new BattleObstacleController(*this));
  85. adventureInt->onAudioPaused();
  86. ongoingAnimationsState.set(true);
  87. GH.windows().pushWindow(windowObject);
  88. windowObject->blockUI(true);
  89. windowObject->updateQueue();
  90. playIntroSoundAndUnlockInterface();
  91. }
  92. void BattleInterface::playIntroSoundAndUnlockInterface()
  93. {
  94. auto onIntroPlayed = [this]()
  95. {
  96. if(LOCPLINT->battleInt)
  97. {
  98. boost::unique_lock<boost::recursive_mutex> un(*CPlayerInterface::pim);
  99. onIntroSoundPlayed();
  100. }
  101. };
  102. int battleIntroSoundChannel = CCS->soundh->playSoundFromSet(CCS->soundh->battleIntroSounds);
  103. if (battleIntroSoundChannel != -1)
  104. {
  105. CCS->soundh->setCallback(battleIntroSoundChannel, onIntroPlayed);
  106. if (settings["gameTweaks"]["skipBattleIntroMusic"].Bool())
  107. openingEnd();
  108. }
  109. else // failed to play sound
  110. {
  111. onIntroSoundPlayed();
  112. }
  113. }
  114. bool BattleInterface::openingPlaying()
  115. {
  116. return battleOpeningDelayActive;
  117. }
  118. void BattleInterface::onIntroSoundPlayed()
  119. {
  120. if (openingPlaying())
  121. openingEnd();
  122. CCS->musich->playMusicFromSet("battle", true, true);
  123. }
  124. void BattleInterface::openingEnd()
  125. {
  126. assert(openingPlaying());
  127. if (!openingPlaying())
  128. return;
  129. onAnimationsFinished();
  130. if(tacticsMode)
  131. tacticNextStack(nullptr);
  132. activateStack();
  133. battleOpeningDelayActive = false;
  134. }
  135. BattleInterface::~BattleInterface()
  136. {
  137. CPlayerInterface::battleInt = nullptr;
  138. givenCommand.cond.notify_all(); //that two lines should make any stacksController->getActiveStack() waiting thread to finish
  139. if (adventureInt)
  140. adventureInt->onAudioResumed();
  141. onAnimationsFinished();
  142. }
  143. void BattleInterface::redrawBattlefield()
  144. {
  145. fieldController->redrawBackgroundWithHexes();
  146. GH.windows().totalRedraw();
  147. }
  148. void BattleInterface::stackReset(const CStack * stack)
  149. {
  150. stacksController->stackReset(stack);
  151. }
  152. void BattleInterface::stackAdded(const CStack * stack)
  153. {
  154. stacksController->stackAdded(stack, false);
  155. }
  156. void BattleInterface::stackRemoved(uint32_t stackID)
  157. {
  158. stacksController->stackRemoved(stackID);
  159. fieldController->redrawBackgroundWithHexes();
  160. windowObject->updateQueue();
  161. }
  162. void BattleInterface::stackActivated(const CStack *stack)
  163. {
  164. stacksController->stackActivated(stack);
  165. }
  166. void BattleInterface::stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance, bool teleport)
  167. {
  168. if (teleport)
  169. stacksController->stackTeleported(stack, destHex, distance);
  170. else
  171. stacksController->stackMoved(stack, destHex, distance);
  172. }
  173. void BattleInterface::stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos)
  174. {
  175. stacksController->stacksAreAttacked(attackedInfos);
  176. std::array<int, 2> killedBySide = {0, 0};
  177. for(const StackAttackedInfo & attackedInfo : attackedInfos)
  178. {
  179. ui8 side = attackedInfo.defender->unitSide();
  180. killedBySide.at(side) += attackedInfo.amountKilled;
  181. }
  182. for(ui8 side = 0; side < 2; side++)
  183. {
  184. if(killedBySide.at(side) > killedBySide.at(1-side))
  185. setHeroAnimation(side, EHeroAnimType::DEFEAT);
  186. else if(killedBySide.at(side) < killedBySide.at(1-side))
  187. setHeroAnimation(side, EHeroAnimType::VICTORY);
  188. }
  189. }
  190. void BattleInterface::stackAttacking( const StackAttackInfo & attackInfo )
  191. {
  192. stacksController->stackAttacking(attackInfo);
  193. }
  194. void BattleInterface::newRoundFirst( int round )
  195. {
  196. waitForAnimations();
  197. }
  198. void BattleInterface::newRound(int number)
  199. {
  200. console->addText(CGI->generaltexth->allTexts[412]);
  201. }
  202. void BattleInterface::giveCommand(EActionType action, BattleHex tile, si32 additional)
  203. {
  204. const CStack * actor = nullptr;
  205. if(action != EActionType::HERO_SPELL && action != EActionType::RETREAT && action != EActionType::SURRENDER)
  206. {
  207. actor = stacksController->getActiveStack();
  208. }
  209. auto side = curInt->cb->playerToSide(curInt->playerID);
  210. if(!side)
  211. {
  212. logGlobal->error("Player %s is not in battle", curInt->playerID.getStr());
  213. return;
  214. }
  215. auto ba = new BattleAction(); //is deleted in CPlayerInterface::stacksController->getActiveStack()()
  216. ba->side = side.value();
  217. ba->actionType = action;
  218. ba->aimToHex(tile);
  219. ba->actionSubtype = additional;
  220. sendCommand(ba, actor);
  221. }
  222. void BattleInterface::sendCommand(BattleAction *& command, const CStack * actor)
  223. {
  224. command->stackNumber = actor ? actor->unitId() : ((command->side == BattleSide::ATTACKER) ? -1 : -2);
  225. if(!tacticsMode)
  226. {
  227. logGlobal->trace("Setting command for %s", (actor ? actor->nodeName() : "hero"));
  228. stacksController->setActiveStack(nullptr);
  229. givenCommand.setn(command);
  230. }
  231. else
  232. {
  233. curInt->cb->battleMakeTacticAction(command);
  234. vstd::clear_pointer(command);
  235. stacksController->setActiveStack(nullptr);
  236. //next stack will be activated when action ends
  237. }
  238. CCS->curh->set(Cursor::Combat::POINTER);
  239. }
  240. const CGHeroInstance * BattleInterface::getActiveHero()
  241. {
  242. const CStack *attacker = stacksController->getActiveStack();
  243. if(!attacker)
  244. {
  245. return nullptr;
  246. }
  247. if(attacker->unitSide() == BattleSide::ATTACKER)
  248. {
  249. return attackingHeroInstance;
  250. }
  251. return defendingHeroInstance;
  252. }
  253. void BattleInterface::stackIsCatapulting(const CatapultAttack & ca)
  254. {
  255. if (siegeController)
  256. siegeController->stackIsCatapulting(ca);
  257. }
  258. void BattleInterface::gateStateChanged(const EGateState state)
  259. {
  260. if (siegeController)
  261. siegeController->gateStateChanged(state);
  262. }
  263. void BattleInterface::battleFinished(const BattleResult& br, QueryID queryID)
  264. {
  265. checkForAnimations();
  266. stacksController->setActiveStack(nullptr);
  267. CCS->curh->set(Cursor::Map::POINTER);
  268. curInt->waitWhileDialog();
  269. if(settings["session"]["spectate"].Bool() && settings["session"]["spectate-skip-battle-result"].Bool())
  270. {
  271. curInt->cb->selectionMade(0, queryID);
  272. windowObject->close();
  273. return;
  274. }
  275. auto wnd = std::make_shared<BattleResultWindow>(br, *(this->curInt));
  276. wnd->resultCallback = [=](ui32 selection)
  277. {
  278. curInt->cb->selectionMade(selection, queryID);
  279. };
  280. GH.windows().pushWindow(wnd);
  281. curInt->waitWhileDialog(); // Avoid freeze when AI end turn after battle. Check bug #1897
  282. CPlayerInterface::battleInt = nullptr;
  283. }
  284. void BattleInterface::spellCast(const BattleSpellCast * sc)
  285. {
  286. // Do not deactivate anything in tactics mode
  287. // This is battlefield setup spells
  288. if(!tacticsMode)
  289. {
  290. windowObject->blockUI(true);
  291. // Disable current active stack duing the cast
  292. // Store the current activeStack to stackToActivate
  293. stacksController->deactivateStack();
  294. }
  295. CCS->curh->set(Cursor::Combat::BLOCKED);
  296. const SpellID spellID = sc->spellID;
  297. const CSpell * spell = spellID.toSpell();
  298. auto targetedTile = sc->tile;
  299. assert(spell);
  300. if(!spell)
  301. return;
  302. const std::string & castSoundPath = spell->getCastSound();
  303. if (!castSoundPath.empty())
  304. {
  305. auto group = spell->animationInfo.projectile.empty() ?
  306. EAnimationEvents::HIT:
  307. EAnimationEvents::BEFORE_HIT;//FIXME: recheck whether this should be on projectile spawning
  308. addToAnimationStage(group, [=]() {
  309. CCS->soundh->playSound(castSoundPath);
  310. });
  311. }
  312. if ( sc->activeCast )
  313. {
  314. const CStack * casterStack = curInt->cb->battleGetStackByID(sc->casterStack);
  315. if(casterStack != nullptr )
  316. {
  317. addToAnimationStage(EAnimationEvents::BEFORE_HIT, [=]()
  318. {
  319. stacksController->addNewAnim(new CastAnimation(*this, casterStack, targetedTile, curInt->cb->battleGetStackByPos(targetedTile), spell));
  320. displaySpellCast(spell, casterStack->getPosition());
  321. });
  322. }
  323. else
  324. {
  325. auto hero = sc->side ? defendingHero : attackingHero;
  326. assert(hero);
  327. addToAnimationStage(EAnimationEvents::BEFORE_HIT, [=]()
  328. {
  329. stacksController->addNewAnim(new HeroCastAnimation(*this, hero, targetedTile, curInt->cb->battleGetStackByPos(targetedTile), spell));
  330. });
  331. }
  332. }
  333. addToAnimationStage(EAnimationEvents::HIT, [=](){
  334. displaySpellHit(spell, targetedTile);
  335. });
  336. //queuing affect animation
  337. for(auto & elem : sc->affectedCres)
  338. {
  339. auto stack = curInt->cb->battleGetStackByID(elem, false);
  340. assert(stack);
  341. if(stack)
  342. {
  343. addToAnimationStage(EAnimationEvents::HIT, [=](){
  344. displaySpellEffect(spell, stack->getPosition());
  345. });
  346. }
  347. }
  348. for(auto & elem : sc->reflectedCres)
  349. {
  350. auto stack = curInt->cb->battleGetStackByID(elem, false);
  351. assert(stack);
  352. addToAnimationStage(EAnimationEvents::HIT, [=](){
  353. effectsController->displayEffect(EBattleEffect::MAGIC_MIRROR, stack->getPosition());
  354. });
  355. }
  356. if (!sc->resistedCres.empty())
  357. {
  358. addToAnimationStage(EAnimationEvents::HIT, [=](){
  359. CCS->soundh->playSound("MAGICRES");
  360. });
  361. }
  362. for(auto & elem : sc->resistedCres)
  363. {
  364. auto stack = curInt->cb->battleGetStackByID(elem, false);
  365. assert(stack);
  366. addToAnimationStage(EAnimationEvents::HIT, [=](){
  367. effectsController->displayEffect(EBattleEffect::RESISTANCE, stack->getPosition());
  368. });
  369. }
  370. //mana absorption
  371. if (sc->manaGained > 0)
  372. {
  373. Point leftHero = Point(15, 30);
  374. Point rightHero = Point(755, 30);
  375. bool side = sc->side;
  376. addToAnimationStage(EAnimationEvents::AFTER_HIT, [=](){
  377. stacksController->addNewAnim(new EffectAnimation(*this, side ? "SP07_A.DEF" : "SP07_B.DEF", leftHero));
  378. stacksController->addNewAnim(new EffectAnimation(*this, side ? "SP07_B.DEF" : "SP07_A.DEF", rightHero));
  379. });
  380. }
  381. }
  382. void BattleInterface::battleStacksEffectsSet(const SetStackEffect & sse)
  383. {
  384. if(stacksController->getActiveStack() != nullptr)
  385. fieldController->redrawBackgroundWithHexes();
  386. }
  387. void BattleInterface::setHeroAnimation(ui8 side, EHeroAnimType phase)
  388. {
  389. if(side == BattleSide::ATTACKER)
  390. {
  391. if(attackingHero)
  392. attackingHero->setPhase(phase);
  393. }
  394. else
  395. {
  396. if(defendingHero)
  397. defendingHero->setPhase(phase);
  398. }
  399. }
  400. void BattleInterface::displayBattleLog(const std::vector<MetaString> & battleLog)
  401. {
  402. for(const auto & line : battleLog)
  403. {
  404. std::string formatted = line.toString();
  405. boost::algorithm::trim(formatted);
  406. appendBattleLog(formatted);
  407. }
  408. }
  409. void BattleInterface::displaySpellAnimationQueue(const CSpell * spell, const CSpell::TAnimationQueue & q, BattleHex destinationTile, bool isHit)
  410. {
  411. for(const CSpell::TAnimation & animation : q)
  412. {
  413. if(animation.pause > 0)
  414. stacksController->addNewAnim(new DummyAnimation(*this, animation.pause));
  415. if (!animation.effectName.empty())
  416. {
  417. const CStack * destStack = getCurrentPlayerInterface()->cb->battleGetStackByPos(destinationTile, false);
  418. if (destStack)
  419. stacksController->addNewAnim(new ColorTransformAnimation(*this, destStack, animation.effectName, spell ));
  420. }
  421. if(!animation.resourceName.empty())
  422. {
  423. int flags = 0;
  424. if (isHit)
  425. flags |= EffectAnimation::FORCE_ON_TOP;
  426. if (animation.verticalPosition == VerticalPosition::BOTTOM)
  427. flags |= EffectAnimation::ALIGN_TO_BOTTOM;
  428. if (!destinationTile.isValid())
  429. flags |= EffectAnimation::SCREEN_FILL;
  430. if (!destinationTile.isValid())
  431. stacksController->addNewAnim(new EffectAnimation(*this, animation.resourceName, flags));
  432. else
  433. stacksController->addNewAnim(new EffectAnimation(*this, animation.resourceName, destinationTile, flags));
  434. }
  435. }
  436. }
  437. void BattleInterface::displaySpellCast(const CSpell * spell, BattleHex destinationTile)
  438. {
  439. if(spell)
  440. displaySpellAnimationQueue(spell, spell->animationInfo.cast, destinationTile, false);
  441. }
  442. void BattleInterface::displaySpellEffect(const CSpell * spell, BattleHex destinationTile)
  443. {
  444. if(spell)
  445. displaySpellAnimationQueue(spell, spell->animationInfo.affect, destinationTile, false);
  446. }
  447. void BattleInterface::displaySpellHit(const CSpell * spell, BattleHex destinationTile)
  448. {
  449. if(spell)
  450. displaySpellAnimationQueue(spell, spell->animationInfo.hit, destinationTile, true);
  451. }
  452. CPlayerInterface *BattleInterface::getCurrentPlayerInterface() const
  453. {
  454. return curInt.get();
  455. }
  456. void BattleInterface::trySetActivePlayer( PlayerColor player )
  457. {
  458. if ( attackerInt && attackerInt->playerID == player )
  459. curInt = attackerInt;
  460. if ( defenderInt && defenderInt->playerID == player )
  461. curInt = defenderInt;
  462. }
  463. void BattleInterface::activateStack()
  464. {
  465. stacksController->activateStack();
  466. const CStack * s = stacksController->getActiveStack();
  467. if(!s)
  468. return;
  469. windowObject->updateQueue();
  470. windowObject->blockUI(false);
  471. fieldController->redrawBackgroundWithHexes();
  472. actionsController->activateStack();
  473. GH.fakeMouseMove();
  474. }
  475. bool BattleInterface::makingTurn() const
  476. {
  477. return stacksController->getActiveStack() != nullptr;
  478. }
  479. void BattleInterface::endAction(const BattleAction* action)
  480. {
  481. // it is possible that tactics mode ended while opening music is still playing
  482. waitForAnimations();
  483. const CStack *stack = curInt->cb->battleGetStackByID(action->stackNumber);
  484. // Activate stack from stackToActivate because this might have been temporary disabled, e.g., during spell cast
  485. activateStack();
  486. stacksController->endAction(action);
  487. windowObject->updateQueue();
  488. //stack ended movement in tactics phase -> select the next one
  489. if (tacticsMode)
  490. tacticNextStack(stack);
  491. //we have activated next stack after sending request that has been just realized -> blockmap due to movement has changed
  492. if(action->actionType == EActionType::HERO_SPELL)
  493. fieldController->redrawBackgroundWithHexes();
  494. }
  495. void BattleInterface::appendBattleLog(const std::string & newEntry)
  496. {
  497. console->addText(newEntry);
  498. }
  499. void BattleInterface::startAction(const BattleAction* action)
  500. {
  501. if(action->actionType == EActionType::END_TACTIC_PHASE)
  502. {
  503. windowObject->tacticPhaseEnded();
  504. return;
  505. }
  506. const CStack *stack = curInt->cb->battleGetStackByID(action->stackNumber);
  507. if (stack)
  508. {
  509. windowObject->updateQueue();
  510. }
  511. else
  512. {
  513. assert(action->actionType == EActionType::HERO_SPELL); //only cast spell is valid action without acting stack number
  514. }
  515. stacksController->startAction(action);
  516. if(action->actionType == EActionType::HERO_SPELL) //when hero casts spell
  517. return;
  518. if (!stack)
  519. {
  520. logGlobal->error("Something wrong with stackNumber in actionStarted. Stack number: %d", action->stackNumber);
  521. return;
  522. }
  523. effectsController->startAction(action);
  524. }
  525. void BattleInterface::tacticPhaseEnd()
  526. {
  527. stacksController->setActiveStack(nullptr);
  528. tacticsMode = false;
  529. }
  530. static bool immobile(const CStack *s)
  531. {
  532. return !s->speed(0, true); //should bound stacks be immobile?
  533. }
  534. void BattleInterface::tacticNextStack(const CStack * current)
  535. {
  536. if (!current)
  537. current = stacksController->getActiveStack();
  538. //no switching stacks when the current one is moving
  539. checkForAnimations();
  540. TStacks stacksOfMine = tacticianInterface->cb->battleGetStacks(CBattleCallback::ONLY_MINE);
  541. vstd::erase_if (stacksOfMine, &immobile);
  542. if (stacksOfMine.empty())
  543. {
  544. tacticPhaseEnd();
  545. return;
  546. }
  547. auto it = vstd::find(stacksOfMine, current);
  548. if (it != stacksOfMine.end() && ++it != stacksOfMine.end())
  549. stackActivated(*it);
  550. else
  551. stackActivated(stacksOfMine.front());
  552. }
  553. void BattleInterface::obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> oi)
  554. {
  555. obstacleController->obstaclePlaced(oi);
  556. }
  557. void BattleInterface::obstacleRemoved(const std::vector<ObstacleChanges> & obstacles)
  558. {
  559. obstacleController->obstacleRemoved(obstacles);
  560. }
  561. const CGHeroInstance *BattleInterface::currentHero() const
  562. {
  563. if (attackingHeroInstance && attackingHeroInstance->tempOwner == curInt->playerID)
  564. return attackingHeroInstance;
  565. if (defendingHeroInstance && defendingHeroInstance->tempOwner == curInt->playerID)
  566. return defendingHeroInstance;
  567. return nullptr;
  568. }
  569. InfoAboutHero BattleInterface::enemyHero() const
  570. {
  571. InfoAboutHero ret;
  572. if (attackingHeroInstance->tempOwner == curInt->playerID)
  573. curInt->cb->getHeroInfo(defendingHeroInstance, ret);
  574. else
  575. curInt->cb->getHeroInfo(attackingHeroInstance, ret);
  576. return ret;
  577. }
  578. void BattleInterface::requestAutofightingAIToTakeAction()
  579. {
  580. assert(curInt->isAutoFightOn);
  581. boost::thread aiThread([&]()
  582. {
  583. if(curInt->cb->battleIsFinished())
  584. {
  585. return; // battle finished with spellcast
  586. }
  587. if (tacticsMode)
  588. {
  589. // Always end tactics mode. Player interface is blocked currently, so it's not possible that
  590. // the AI can take any action except end tactics phase (AI actions won't be triggered)
  591. //TODO implement the possibility that the AI will be triggered for further actions
  592. //TODO any solution to merge tactics phase & normal phase in the way it is handled by the player and battle interface?
  593. stacksController->setActiveStack(nullptr);
  594. tacticsMode = false;
  595. }
  596. else
  597. {
  598. const CStack* activeStack = stacksController->getActiveStack();
  599. // If enemy is moving, activeStack can be null
  600. if (activeStack)
  601. {
  602. auto ba = std::make_unique<BattleAction>(curInt->autofightingAI->activeStack(activeStack));
  603. givenCommand.setn(ba.release());
  604. }
  605. stacksController->setActiveStack(nullptr);
  606. }
  607. });
  608. aiThread.detach();
  609. }
  610. void BattleInterface::castThisSpell(SpellID spellID)
  611. {
  612. actionsController->castThisSpell(spellID);
  613. }
  614. void BattleInterface::executeStagedAnimations()
  615. {
  616. EAnimationEvents earliestStage = EAnimationEvents::COUNT;
  617. for(const auto & event : awaitingEvents)
  618. earliestStage = std::min(earliestStage, event.event);
  619. if(earliestStage != EAnimationEvents::COUNT)
  620. executeAnimationStage(earliestStage);
  621. }
  622. void BattleInterface::executeAnimationStage(EAnimationEvents event)
  623. {
  624. decltype(awaitingEvents) executingEvents;
  625. for(auto it = awaitingEvents.begin(); it != awaitingEvents.end();)
  626. {
  627. if(it->event == event)
  628. {
  629. executingEvents.push_back(*it);
  630. it = awaitingEvents.erase(it);
  631. }
  632. else
  633. ++it;
  634. }
  635. for(const auto & event : executingEvents)
  636. event.action();
  637. }
  638. void BattleInterface::onAnimationsStarted()
  639. {
  640. ongoingAnimationsState.setn(true);
  641. }
  642. void BattleInterface::onAnimationsFinished()
  643. {
  644. ongoingAnimationsState.setn(false);
  645. }
  646. void BattleInterface::waitForAnimations()
  647. {
  648. auto unlockPim = vstd::makeUnlockGuard(*CPlayerInterface::pim);
  649. ongoingAnimationsState.waitUntil(false);
  650. }
  651. bool BattleInterface::hasAnimations()
  652. {
  653. return ongoingAnimationsState.get();
  654. }
  655. void BattleInterface::checkForAnimations()
  656. {
  657. assert(!hasAnimations());
  658. if(hasAnimations())
  659. logGlobal->error("Unexpected animations state: expected all animations to be over, but some are still ongoing!");
  660. waitForAnimations();
  661. }
  662. void BattleInterface::addToAnimationStage(EAnimationEvents event, const AwaitingAnimationAction & action)
  663. {
  664. awaitingEvents.push_back({action, event});
  665. }
  666. void BattleInterface::setBattleQueueVisibility(bool visible)
  667. {
  668. windowObject->hideQueue();
  669. if(visible)
  670. windowObject->showQueue();
  671. }