BattleInterface.cpp 22 KB

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