BattleInterface.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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 "../render/Canvas.h"
  30. #include "../adventureMap/CAdvMapInt.h"
  31. #include "../../CCallback.h"
  32. #include "../../lib/CStack.h"
  33. #include "../../lib/CConfigHandler.h"
  34. #include "../../lib/CGeneralTextHandler.h"
  35. #include "../../lib/CHeroHandler.h"
  36. #include "../../lib/CondSh.h"
  37. #include "../../lib/mapObjects/CGTownInstance.h"
  38. #include "../../lib/NetPacks.h"
  39. #include "../../lib/UnlockGuard.h"
  40. #include "../../lib/TerrainHandler.h"
  41. CondSh<BattleAction *> BattleInterface::givenCommand(nullptr);
  42. BattleInterface::BattleInterface(const CCreatureSet *army1, const CCreatureSet *army2,
  43. const CGHeroInstance *hero1, const CGHeroInstance *hero2,
  44. std::shared_ptr<CPlayerInterface> att,
  45. std::shared_ptr<CPlayerInterface> defen,
  46. std::shared_ptr<CPlayerInterface> spectatorInt)
  47. : attackingHeroInstance(hero1)
  48. , defendingHeroInstance(hero2)
  49. , attackerInt(att)
  50. , defenderInt(defen)
  51. , curInt(att)
  52. , moveSoundHander(-1)
  53. {
  54. for ( auto & event : animationEvents)
  55. event.setn(false);
  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. setAnimationCondition(EAnimationEvents::OPENING, true);
  87. GH.pushInt(windowObject);
  88. windowObject->blockUI(true);
  89. windowObject->updateQueue();
  90. playIntroSoundAndUnlockInterface();
  91. }
  92. void BattleInterface::playIntroSoundAndUnlockInterface()
  93. {
  94. if(settings["gameTweaks"]["skipBattleIntroMusic"].Bool())
  95. {
  96. onIntroSoundPlayed();
  97. return;
  98. }
  99. auto onIntroPlayed = [this]()
  100. {
  101. if(LOCPLINT->battleInt)
  102. {
  103. boost::unique_lock<boost::recursive_mutex> un(*CPlayerInterface::pim);
  104. onIntroSoundPlayed();
  105. }
  106. };
  107. battleIntroSoundChannel = CCS->soundh->playSoundFromSet(CCS->soundh->battleIntroSounds);
  108. if (battleIntroSoundChannel != -1)
  109. CCS->soundh->setCallback(battleIntroSoundChannel, onIntroPlayed);
  110. else
  111. onIntroSoundPlayed();
  112. }
  113. void BattleInterface::onIntroSoundPlayed()
  114. {
  115. setAnimationCondition(EAnimationEvents::OPENING, false);
  116. CCS->musich->playMusicFromSet("battle", true, true);
  117. if(tacticsMode)
  118. tacticNextStack(nullptr);
  119. activateStack();
  120. battleIntroSoundChannel = -1;
  121. }
  122. BattleInterface::~BattleInterface()
  123. {
  124. CPlayerInterface::battleInt = nullptr;
  125. givenCommand.cond.notify_all(); //that two lines should make any stacksController->getActiveStack() waiting thread to finish
  126. if (adventureInt)
  127. adventureInt->onAudioResumed();
  128. // may happen if user decided to close game while in battle
  129. if (getAnimationCondition(EAnimationEvents::ACTION) == true)
  130. logGlobal->error("Shutting down BattleInterface during animation playback!");
  131. setAnimationCondition(EAnimationEvents::ACTION, false);
  132. }
  133. void BattleInterface::redrawBattlefield()
  134. {
  135. fieldController->redrawBackgroundWithHexes();
  136. GH.totalRedraw();
  137. }
  138. void BattleInterface::stackReset(const CStack * stack)
  139. {
  140. stacksController->stackReset(stack);
  141. }
  142. void BattleInterface::stackAdded(const CStack * stack)
  143. {
  144. stacksController->stackAdded(stack, false);
  145. }
  146. void BattleInterface::stackRemoved(uint32_t stackID)
  147. {
  148. stacksController->stackRemoved(stackID);
  149. fieldController->redrawBackgroundWithHexes();
  150. windowObject->updateQueue();
  151. }
  152. void BattleInterface::stackActivated(const CStack *stack)
  153. {
  154. stacksController->stackActivated(stack);
  155. }
  156. void BattleInterface::stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance, bool teleport)
  157. {
  158. if (teleport)
  159. stacksController->stackTeleported(stack, destHex, distance);
  160. else
  161. stacksController->stackMoved(stack, destHex, distance);
  162. }
  163. void BattleInterface::stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos)
  164. {
  165. stacksController->stacksAreAttacked(attackedInfos);
  166. std::array<int, 2> killedBySide = {0, 0};
  167. for(const StackAttackedInfo & attackedInfo : attackedInfos)
  168. {
  169. ui8 side = attackedInfo.defender->side;
  170. killedBySide.at(side) += attackedInfo.amountKilled;
  171. }
  172. for(ui8 side = 0; side < 2; side++)
  173. {
  174. if(killedBySide.at(side) > killedBySide.at(1-side))
  175. setHeroAnimation(side, EHeroAnimType::DEFEAT);
  176. else if(killedBySide.at(side) < killedBySide.at(1-side))
  177. setHeroAnimation(side, EHeroAnimType::VICTORY);
  178. }
  179. }
  180. void BattleInterface::stackAttacking( const StackAttackInfo & attackInfo )
  181. {
  182. stacksController->stackAttacking(attackInfo);
  183. }
  184. void BattleInterface::newRoundFirst( int round )
  185. {
  186. waitForAnimationCondition(EAnimationEvents::OPENING, false);
  187. }
  188. void BattleInterface::newRound(int number)
  189. {
  190. console->addText(CGI->generaltexth->allTexts[412]);
  191. }
  192. void BattleInterface::giveCommand(EActionType action, BattleHex tile, si32 additional)
  193. {
  194. const CStack * actor = nullptr;
  195. if(action != EActionType::HERO_SPELL && action != EActionType::RETREAT && action != EActionType::SURRENDER)
  196. {
  197. actor = stacksController->getActiveStack();
  198. }
  199. auto side = curInt->cb->playerToSide(curInt->playerID);
  200. if(!side)
  201. {
  202. logGlobal->error("Player %s is not in battle", curInt->playerID.getStr());
  203. return;
  204. }
  205. auto ba = new BattleAction(); //is deleted in CPlayerInterface::stacksController->getActiveStack()()
  206. ba->side = side.get();
  207. ba->actionType = action;
  208. ba->aimToHex(tile);
  209. ba->actionSubtype = additional;
  210. sendCommand(ba, actor);
  211. }
  212. void BattleInterface::sendCommand(BattleAction *& command, const CStack * actor)
  213. {
  214. command->stackNumber = actor ? actor->unitId() : ((command->side == BattleSide::ATTACKER) ? -1 : -2);
  215. if(!tacticsMode)
  216. {
  217. logGlobal->trace("Setting command for %s", (actor ? actor->nodeName() : "hero"));
  218. stacksController->setActiveStack(nullptr);
  219. givenCommand.setn(command);
  220. }
  221. else
  222. {
  223. curInt->cb->battleMakeTacticAction(command);
  224. vstd::clear_pointer(command);
  225. stacksController->setActiveStack(nullptr);
  226. //next stack will be activated when action ends
  227. }
  228. CCS->curh->set(Cursor::Combat::POINTER);
  229. }
  230. const CGHeroInstance * BattleInterface::getActiveHero()
  231. {
  232. const CStack *attacker = stacksController->getActiveStack();
  233. if(!attacker)
  234. {
  235. return nullptr;
  236. }
  237. if(attacker->side == BattleSide::ATTACKER)
  238. {
  239. return attackingHeroInstance;
  240. }
  241. return defendingHeroInstance;
  242. }
  243. void BattleInterface::stackIsCatapulting(const CatapultAttack & ca)
  244. {
  245. if (siegeController)
  246. siegeController->stackIsCatapulting(ca);
  247. }
  248. void BattleInterface::gateStateChanged(const EGateState state)
  249. {
  250. if (siegeController)
  251. siegeController->gateStateChanged(state);
  252. }
  253. void BattleInterface::battleFinished(const BattleResult& br)
  254. {
  255. assert(getAnimationCondition(EAnimationEvents::ACTION) == false);
  256. waitForAnimationCondition(EAnimationEvents::ACTION, false);
  257. stacksController->setActiveStack(nullptr);
  258. CCS->curh->set(Cursor::Map::POINTER);
  259. curInt->waitWhileDialog();
  260. if(settings["session"]["spectate"].Bool() && settings["session"]["spectate-skip-battle-result"].Bool())
  261. {
  262. windowObject->close();
  263. return;
  264. }
  265. GH.pushInt(std::make_shared<BattleResultWindow>(br, *(this->curInt)));
  266. curInt->waitWhileDialog(); // Avoid freeze when AI end turn after battle. Check bug #1897
  267. CPlayerInterface::battleInt = nullptr;
  268. }
  269. void BattleInterface::spellCast(const BattleSpellCast * sc)
  270. {
  271. windowObject->blockUI(true);
  272. const SpellID spellID = sc->spellID;
  273. const CSpell * spell = spellID.toSpell();
  274. auto targetedTile = sc->tile;
  275. assert(spell);
  276. if(!spell)
  277. return;
  278. const std::string & castSoundPath = spell->getCastSound();
  279. if (!castSoundPath.empty())
  280. {
  281. auto group = spell->animationInfo.projectile.empty() ?
  282. EAnimationEvents::HIT:
  283. EAnimationEvents::BEFORE_HIT;//FIXME: recheck whether this should be on projectile spawning
  284. executeOnAnimationCondition(group, true, [=]() {
  285. CCS->soundh->playSound(castSoundPath);
  286. });
  287. }
  288. if ( sc->activeCast )
  289. {
  290. const CStack * casterStack = curInt->cb->battleGetStackByID(sc->casterStack);
  291. if(casterStack != nullptr )
  292. {
  293. executeOnAnimationCondition(EAnimationEvents::BEFORE_HIT, true, [=]()
  294. {
  295. stacksController->addNewAnim(new CastAnimation(*this, casterStack, targetedTile, curInt->cb->battleGetStackByPos(targetedTile), spell));
  296. displaySpellCast(spell, casterStack->getPosition());
  297. });
  298. }
  299. else
  300. {
  301. auto hero = sc->side ? defendingHero : attackingHero;
  302. assert(hero);
  303. executeOnAnimationCondition(EAnimationEvents::BEFORE_HIT, true, [=]()
  304. {
  305. stacksController->addNewAnim(new HeroCastAnimation(*this, hero, targetedTile, curInt->cb->battleGetStackByPos(targetedTile), spell));
  306. });
  307. }
  308. }
  309. executeOnAnimationCondition(EAnimationEvents::HIT, true, [=](){
  310. displaySpellHit(spell, targetedTile);
  311. });
  312. //queuing affect animation
  313. for(auto & elem : sc->affectedCres)
  314. {
  315. auto stack = curInt->cb->battleGetStackByID(elem, false);
  316. assert(stack);
  317. if(stack)
  318. {
  319. executeOnAnimationCondition(EAnimationEvents::HIT, true, [=](){
  320. displaySpellEffect(spell, stack->getPosition());
  321. });
  322. }
  323. }
  324. for(auto & elem : sc->reflectedCres)
  325. {
  326. auto stack = curInt->cb->battleGetStackByID(elem, false);
  327. assert(stack);
  328. executeOnAnimationCondition(EAnimationEvents::HIT, true, [=](){
  329. effectsController->displayEffect(EBattleEffect::MAGIC_MIRROR, stack->getPosition());
  330. });
  331. }
  332. if (!sc->resistedCres.empty())
  333. {
  334. executeOnAnimationCondition(EAnimationEvents::HIT, true, [=](){
  335. CCS->soundh->playSound("MAGICRES");
  336. });
  337. }
  338. for(auto & elem : sc->resistedCres)
  339. {
  340. auto stack = curInt->cb->battleGetStackByID(elem, false);
  341. assert(stack);
  342. executeOnAnimationCondition(EAnimationEvents::HIT, true, [=](){
  343. effectsController->displayEffect(EBattleEffect::RESISTANCE, stack->getPosition());
  344. });
  345. }
  346. //mana absorption
  347. if (sc->manaGained > 0)
  348. {
  349. Point leftHero = Point(15, 30);
  350. Point rightHero = Point(755, 30);
  351. bool side = sc->side;
  352. executeOnAnimationCondition(EAnimationEvents::AFTER_HIT, true, [=](){
  353. stacksController->addNewAnim(new EffectAnimation(*this, side ? "SP07_A.DEF" : "SP07_B.DEF", leftHero));
  354. stacksController->addNewAnim(new EffectAnimation(*this, side ? "SP07_B.DEF" : "SP07_A.DEF", rightHero));
  355. });
  356. }
  357. }
  358. void BattleInterface::battleStacksEffectsSet(const SetStackEffect & sse)
  359. {
  360. if(stacksController->getActiveStack() != nullptr)
  361. fieldController->redrawBackgroundWithHexes();
  362. }
  363. void BattleInterface::setHeroAnimation(ui8 side, EHeroAnimType phase)
  364. {
  365. if(side == BattleSide::ATTACKER)
  366. {
  367. if(attackingHero)
  368. attackingHero->setPhase(phase);
  369. }
  370. else
  371. {
  372. if(defendingHero)
  373. defendingHero->setPhase(phase);
  374. }
  375. }
  376. void BattleInterface::displayBattleLog(const std::vector<MetaString> & battleLog)
  377. {
  378. for(const auto & line : battleLog)
  379. {
  380. std::string formatted = line.toString();
  381. boost::algorithm::trim(formatted);
  382. appendBattleLog(formatted);
  383. }
  384. }
  385. void BattleInterface::displaySpellAnimationQueue(const CSpell * spell, const CSpell::TAnimationQueue & q, BattleHex destinationTile, bool isHit)
  386. {
  387. for(const CSpell::TAnimation & animation : q)
  388. {
  389. if(animation.pause > 0)
  390. stacksController->addNewAnim(new DummyAnimation(*this, animation.pause));
  391. if (!animation.effectName.empty())
  392. {
  393. const CStack * destStack = getCurrentPlayerInterface()->cb->battleGetStackByPos(destinationTile, false);
  394. if (destStack)
  395. stacksController->addNewAnim(new ColorTransformAnimation(*this, destStack, animation.effectName, spell ));
  396. }
  397. if(!animation.resourceName.empty())
  398. {
  399. int flags = 0;
  400. if (isHit)
  401. flags |= EffectAnimation::FORCE_ON_TOP;
  402. if (animation.verticalPosition == VerticalPosition::BOTTOM)
  403. flags |= EffectAnimation::ALIGN_TO_BOTTOM;
  404. if (!destinationTile.isValid())
  405. flags |= EffectAnimation::SCREEN_FILL;
  406. if (!destinationTile.isValid())
  407. stacksController->addNewAnim(new EffectAnimation(*this, animation.resourceName, flags));
  408. else
  409. stacksController->addNewAnim(new EffectAnimation(*this, animation.resourceName, destinationTile, flags));
  410. }
  411. }
  412. }
  413. void BattleInterface::displaySpellCast(const CSpell * spell, BattleHex destinationTile)
  414. {
  415. if(spell)
  416. displaySpellAnimationQueue(spell, spell->animationInfo.cast, destinationTile, false);
  417. }
  418. void BattleInterface::displaySpellEffect(const CSpell * spell, BattleHex destinationTile)
  419. {
  420. if(spell)
  421. displaySpellAnimationQueue(spell, spell->animationInfo.affect, destinationTile, false);
  422. }
  423. void BattleInterface::displaySpellHit(const CSpell * spell, BattleHex destinationTile)
  424. {
  425. if(spell)
  426. displaySpellAnimationQueue(spell, spell->animationInfo.hit, destinationTile, true);
  427. }
  428. CPlayerInterface *BattleInterface::getCurrentPlayerInterface() const
  429. {
  430. return curInt.get();
  431. }
  432. void BattleInterface::trySetActivePlayer( PlayerColor player )
  433. {
  434. if ( attackerInt && attackerInt->playerID == player )
  435. curInt = attackerInt;
  436. if ( defenderInt && defenderInt->playerID == player )
  437. curInt = defenderInt;
  438. }
  439. void BattleInterface::activateStack()
  440. {
  441. stacksController->activateStack();
  442. const CStack * s = stacksController->getActiveStack();
  443. if(!s)
  444. return;
  445. windowObject->updateQueue();
  446. windowObject->blockUI(false);
  447. fieldController->redrawBackgroundWithHexes();
  448. actionsController->activateStack();
  449. GH.fakeMouseMove();
  450. }
  451. bool BattleInterface::makingTurn() const
  452. {
  453. return stacksController->getActiveStack() != nullptr;
  454. }
  455. void BattleInterface::endAction(const BattleAction* action)
  456. {
  457. const CStack *stack = curInt->cb->battleGetStackByID(action->stackNumber);
  458. stacksController->endAction(action);
  459. windowObject->updateQueue();
  460. //stack ended movement in tactics phase -> select the next one
  461. if (tacticsMode)
  462. tacticNextStack(stack);
  463. //we have activated next stack after sending request that has been just realized -> blockmap due to movement has changed
  464. if(action->actionType == EActionType::HERO_SPELL)
  465. fieldController->redrawBackgroundWithHexes();
  466. }
  467. void BattleInterface::appendBattleLog(const std::string & newEntry)
  468. {
  469. console->addText(newEntry);
  470. }
  471. void BattleInterface::startAction(const BattleAction* action)
  472. {
  473. if(action->actionType == EActionType::END_TACTIC_PHASE)
  474. {
  475. windowObject->tacticPhaseEnded();
  476. return;
  477. }
  478. const CStack *stack = curInt->cb->battleGetStackByID(action->stackNumber);
  479. if (stack)
  480. {
  481. windowObject->updateQueue();
  482. }
  483. else
  484. {
  485. assert(action->actionType == EActionType::HERO_SPELL); //only cast spell is valid action without acting stack number
  486. }
  487. stacksController->startAction(action);
  488. if(action->actionType == EActionType::HERO_SPELL) //when hero casts spell
  489. return;
  490. if (!stack)
  491. {
  492. logGlobal->error("Something wrong with stackNumber in actionStarted. Stack number: %d", action->stackNumber);
  493. return;
  494. }
  495. effectsController->startAction(action);
  496. }
  497. void BattleInterface::tacticPhaseEnd()
  498. {
  499. stacksController->setActiveStack(nullptr);
  500. tacticsMode = false;
  501. }
  502. static bool immobile(const CStack *s)
  503. {
  504. return !s->Speed(0, true); //should bound stacks be immobile?
  505. }
  506. void BattleInterface::tacticNextStack(const CStack * current)
  507. {
  508. if (!current)
  509. current = stacksController->getActiveStack();
  510. //no switching stacks when the current one is moving
  511. assert(getAnimationCondition(EAnimationEvents::ACTION) == false);
  512. waitForAnimationCondition(EAnimationEvents::ACTION, false);
  513. TStacks stacksOfMine = tacticianInterface->cb->battleGetStacks(CBattleCallback::ONLY_MINE);
  514. vstd::erase_if (stacksOfMine, &immobile);
  515. if (stacksOfMine.empty())
  516. {
  517. tacticPhaseEnd();
  518. return;
  519. }
  520. auto it = vstd::find(stacksOfMine, current);
  521. if (it != stacksOfMine.end() && ++it != stacksOfMine.end())
  522. stackActivated(*it);
  523. else
  524. stackActivated(stacksOfMine.front());
  525. }
  526. void BattleInterface::obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> oi)
  527. {
  528. obstacleController->obstaclePlaced(oi);
  529. }
  530. const CGHeroInstance *BattleInterface::currentHero() const
  531. {
  532. if (attackingHeroInstance && attackingHeroInstance->tempOwner == curInt->playerID)
  533. return attackingHeroInstance;
  534. if (defendingHeroInstance && defendingHeroInstance->tempOwner == curInt->playerID)
  535. return defendingHeroInstance;
  536. return nullptr;
  537. }
  538. InfoAboutHero BattleInterface::enemyHero() const
  539. {
  540. InfoAboutHero ret;
  541. if (attackingHeroInstance->tempOwner == curInt->playerID)
  542. curInt->cb->getHeroInfo(defendingHeroInstance, ret);
  543. else
  544. curInt->cb->getHeroInfo(attackingHeroInstance, ret);
  545. return ret;
  546. }
  547. void BattleInterface::requestAutofightingAIToTakeAction()
  548. {
  549. assert(curInt->isAutoFightOn);
  550. boost::thread aiThread([&]()
  551. {
  552. if(curInt->cb->battleIsFinished())
  553. {
  554. return; // battle finished with spellcast
  555. }
  556. if (tacticsMode)
  557. {
  558. // Always end tactics mode. Player interface is blocked currently, so it's not possible that
  559. // the AI can take any action except end tactics phase (AI actions won't be triggered)
  560. //TODO implement the possibility that the AI will be triggered for further actions
  561. //TODO any solution to merge tactics phase & normal phase in the way it is handled by the player and battle interface?
  562. stacksController->setActiveStack(nullptr);
  563. tacticsMode = false;
  564. }
  565. else
  566. {
  567. const CStack* activeStack = stacksController->getActiveStack();
  568. // If enemy is moving, activeStack can be null
  569. if (activeStack)
  570. {
  571. auto ba = std::make_unique<BattleAction>(curInt->autofightingAI->activeStack(activeStack));
  572. givenCommand.setn(ba.release());
  573. }
  574. }
  575. });
  576. aiThread.detach();
  577. }
  578. void BattleInterface::castThisSpell(SpellID spellID)
  579. {
  580. actionsController->castThisSpell(spellID);
  581. }
  582. void BattleInterface::setAnimationCondition( EAnimationEvents event, bool state)
  583. {
  584. logAnim->debug("setAnimationCondition: %d -> %s", static_cast<int>(event), state ? "ON" : "OFF");
  585. size_t index = static_cast<size_t>(event);
  586. animationEvents[index].setn(state);
  587. decltype(awaitingEvents) executingEvents;
  588. for (auto it = awaitingEvents.begin(); it != awaitingEvents.end();)
  589. {
  590. if (it->event == event && it->eventState == state)
  591. {
  592. executingEvents.push_back(*it);
  593. it = awaitingEvents.erase(it);
  594. }
  595. else
  596. ++it;
  597. }
  598. for (auto const & event : executingEvents)
  599. event.action();
  600. }
  601. bool BattleInterface::getAnimationCondition( EAnimationEvents event)
  602. {
  603. size_t index = static_cast<size_t>(event);
  604. return animationEvents[index].get();
  605. }
  606. void BattleInterface::waitForAnimationCondition( EAnimationEvents event, bool state)
  607. {
  608. auto unlockPim = vstd::makeUnlockGuard(*CPlayerInterface::pim);
  609. size_t index = static_cast<size_t>(event);
  610. animationEvents[index].waitUntil(state);
  611. }
  612. void BattleInterface::executeOnAnimationCondition( EAnimationEvents event, bool state, const AwaitingAnimationAction & action)
  613. {
  614. awaitingEvents.push_back({action, event, state});
  615. }
  616. void BattleInterface::setBattleQueueVisibility(bool visible)
  617. {
  618. windowObject->hideQueue();
  619. if(visible)
  620. windowObject->showQueue();
  621. }