BattleInterface.cpp 21 KB

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