BattleInterface.cpp 21 KB

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