BattleInterface.cpp 21 KB

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