BattleStacksController.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * BattleStacksController.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 "BattleStacksController.h"
  12. #include "BattleSiegeController.h"
  13. #include "BattleInterfaceClasses.h"
  14. #include "BattleInterface.h"
  15. #include "BattleAnimationClasses.h"
  16. #include "BattleFieldController.h"
  17. #include "BattleEffectsController.h"
  18. #include "BattleProjectileController.h"
  19. #include "BattleControlPanel.h"
  20. #include "BattleRenderer.h"
  21. #include "CreatureAnimation.h"
  22. #include "../CPlayerInterface.h"
  23. #include "../CMusicHandler.h"
  24. #include "../CGameInfo.h"
  25. #include "../gui/CAnimation.h"
  26. #include "../gui/CGuiHandler.h"
  27. #include "../gui/Canvas.h"
  28. #include "../../CCallback.h"
  29. #include "../../lib/battle/BattleHex.h"
  30. #include "../../lib/CGameState.h"
  31. #include "../../lib/CStack.h"
  32. #include "../../lib/CondSh.h"
  33. static void onAnimationFinished(const CStack *stack, std::weak_ptr<CreatureAnimation> anim)
  34. {
  35. std::shared_ptr<CreatureAnimation> animation = anim.lock();
  36. if(!animation)
  37. return;
  38. if (animation->isIdle())
  39. {
  40. const CCreature *creature = stack->getCreature();
  41. if (animation->framesInGroup(ECreatureAnimType::MOUSEON) > 0)
  42. {
  43. if (CRandomGenerator::getDefault().nextDouble(99.0) < creature->animation.timeBetweenFidgets *10)
  44. animation->playOnce(ECreatureAnimType::MOUSEON);
  45. else
  46. animation->setType(ECreatureAnimType::HOLDING);
  47. }
  48. else
  49. {
  50. animation->setType(ECreatureAnimType::HOLDING);
  51. }
  52. }
  53. // always reset callback
  54. animation->onAnimationReset += std::bind(&onAnimationFinished, stack, anim);
  55. }
  56. BattleStacksController::BattleStacksController(BattleInterface & owner):
  57. owner(owner),
  58. activeStack(nullptr),
  59. mouseHoveredStack(nullptr),
  60. stackToActivate(nullptr),
  61. selectedStack(nullptr),
  62. stackCanCastSpell(false),
  63. creatureSpellToCast(uint32_t(-1)),
  64. animIDhelper(0)
  65. {
  66. //preparing graphics for displaying amounts of creatures
  67. amountNormal = IImage::createFromFile("CMNUMWIN.BMP");
  68. amountPositive = IImage::createFromFile("CMNUMWIN.BMP");
  69. amountNegative = IImage::createFromFile("CMNUMWIN.BMP");
  70. amountEffNeutral = IImage::createFromFile("CMNUMWIN.BMP");
  71. static const ColorShifterMultiplyAndAddExcept shifterNormal ({150, 50, 255, 255}, {0,0,0,0}, {255, 231, 132, 255});
  72. static const ColorShifterMultiplyAndAddExcept shifterPositive({ 50, 255, 50, 255}, {0,0,0,0}, {255, 231, 132, 255});
  73. static const ColorShifterMultiplyAndAddExcept shifterNegative({255, 50, 50, 255}, {0,0,0,0}, {255, 231, 132, 255});
  74. static const ColorShifterMultiplyAndAddExcept shifterNeutral ({255, 255, 50, 255}, {0,0,0,0}, {255, 231, 132, 255});
  75. amountNormal->adjustPalette(&shifterNormal);
  76. amountPositive->adjustPalette(&shifterPositive);
  77. amountNegative->adjustPalette(&shifterNegative);
  78. amountEffNeutral->adjustPalette(&shifterNeutral);
  79. std::vector<const CStack*> stacks = owner.curInt->cb->battleGetAllStacks(true);
  80. for(const CStack * s : stacks)
  81. {
  82. stackAdded(s);
  83. }
  84. }
  85. BattleHex BattleStacksController::getStackCurrentPosition(const CStack * stack) const
  86. {
  87. if ( !stackAnimation.at(stack->ID)->isMoving())
  88. return stack->getPosition();
  89. if (stack->hasBonusOfType(Bonus::FLYING))
  90. return BattleHex::HEX_AFTER_ALL;
  91. for (auto & anim : currentAnimations)
  92. {
  93. // certainly ugly workaround but fixes quite annoying bug
  94. // stack position will be updated only *after* movement is finished
  95. // before this - stack is always at its initial position. Thus we need to find
  96. // its current position. Which can be found only in this class
  97. if (StackMoveAnimation *move = dynamic_cast<StackMoveAnimation*>(anim))
  98. {
  99. if (move->stack == stack)
  100. return move->currentHex;
  101. }
  102. }
  103. return stack->getPosition();
  104. }
  105. void BattleStacksController::collectRenderableObjects(BattleRenderer & renderer)
  106. {
  107. auto stacks = owner.curInt->cb->battleGetAllStacks(false);
  108. for (auto stack : stacks)
  109. {
  110. if (stackAnimation.find(stack->ID) == stackAnimation.end()) //e.g. for summoned but not yet handled stacks
  111. continue;
  112. //FIXME: hack to ignore ghost stacks
  113. if ((stackAnimation[stack->ID]->getType() == ECreatureAnimType::DEAD || stackAnimation[stack->ID]->getType() == ECreatureAnimType::HOLDING) && stack->isGhost())
  114. continue;
  115. auto layer = stackAnimation[stack->ID]->isDead() ? EBattleFieldLayer::CORPSES : EBattleFieldLayer::STACKS;
  116. auto location = getStackCurrentPosition(stack);
  117. renderer.insert(layer, location, [this, stack]( BattleRenderer::RendererRef renderer ){
  118. showStack(renderer, stack);
  119. });
  120. if (stackNeedsAmountBox(stack))
  121. {
  122. renderer.insert(EBattleFieldLayer::STACK_AMOUNTS, location, [this, stack]( BattleRenderer::RendererRef renderer ){
  123. showStackAmountBox(renderer, stack);
  124. });
  125. }
  126. }
  127. }
  128. void BattleStacksController::stackReset(const CStack * stack)
  129. {
  130. //FIXME: there should be no more ongoing animations. If not - then some other method created animations but did not wait for them to end
  131. //assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  132. //owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  133. //reset orientation?
  134. //stackFacingRight[stack->ID] = stack->side == BattleSide::ATTACKER;
  135. auto iter = stackAnimation.find(stack->ID);
  136. if(iter == stackAnimation.end())
  137. {
  138. logGlobal->error("Unit %d have no animation", stack->ID);
  139. return;
  140. }
  141. auto animation = iter->second;
  142. if(stack->alive() && animation->isDeadOrDying())
  143. {
  144. owner.executeOnAnimationCondition(EAnimationEvents::HIT, true, [=]()
  145. {
  146. addNewAnim(new ResurrectionAnimation(owner, stack));
  147. });
  148. }
  149. static const ColorShifterMultiplyAndAdd shifterClone ({255, 255, 0, 255}, {0, 0, 255, 0});
  150. if (stack->isClone())
  151. {
  152. animation->shiftColor(&shifterClone);
  153. }
  154. //owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  155. }
  156. void BattleStacksController::stackAdded(const CStack * stack)
  157. {
  158. // Tower shooters have only their upper half visible
  159. static const int turretCreatureAnimationHeight = 235;
  160. stackFacingRight[stack->ID] = stack->side == BattleSide::ATTACKER; // must be set before getting stack position
  161. Point coords = getStackPositionAtHex(stack->getPosition(), stack);
  162. if(stack->initialPosition < 0) //turret
  163. {
  164. assert(owner.siegeController);
  165. const CCreature *turretCreature = owner.siegeController->getTurretCreature();
  166. stackAnimation[stack->ID] = AnimationControls::getAnimation(turretCreature);
  167. stackAnimation[stack->ID]->pos.h = turretCreatureAnimationHeight;
  168. coords = owner.siegeController->getTurretCreaturePosition(stack->initialPosition);
  169. }
  170. else
  171. {
  172. stackAnimation[stack->ID] = AnimationControls::getAnimation(stack->getCreature());
  173. stackAnimation[stack->ID]->onAnimationReset += std::bind(&onAnimationFinished, stack, stackAnimation[stack->ID]);
  174. stackAnimation[stack->ID]->pos.h = stackAnimation[stack->ID]->getHeight();
  175. }
  176. stackAnimation[stack->ID]->pos.x = coords.x;
  177. stackAnimation[stack->ID]->pos.y = coords.y;
  178. stackAnimation[stack->ID]->pos.w = stackAnimation[stack->ID]->getWidth();
  179. stackAnimation[stack->ID]->setType(ECreatureAnimType::HOLDING);
  180. }
  181. void BattleStacksController::setActiveStack(const CStack *stack)
  182. {
  183. if (activeStack) // update UI
  184. stackAnimation[activeStack->ID]->setBorderColor(AnimationControls::getNoBorder());
  185. activeStack = stack;
  186. if (activeStack) // update UI
  187. stackAnimation[activeStack->ID]->setBorderColor(AnimationControls::getGoldBorder());
  188. owner.controlPanel->blockUI(activeStack == nullptr);
  189. }
  190. void BattleStacksController::setHoveredStack(const CStack *stack)
  191. {
  192. if ( stack == mouseHoveredStack )
  193. return;
  194. if (mouseHoveredStack)
  195. stackAnimation[mouseHoveredStack->ID]->setBorderColor(AnimationControls::getNoBorder());
  196. // stack must be alive and not active (which uses gold border instead)
  197. if (stack && stack->alive() && stack != activeStack)
  198. {
  199. mouseHoveredStack = stack;
  200. if (mouseHoveredStack)
  201. {
  202. stackAnimation[mouseHoveredStack->ID]->setBorderColor(AnimationControls::getBlueBorder());
  203. if (stackAnimation[mouseHoveredStack->ID]->framesInGroup(ECreatureAnimType::MOUSEON) > 0)
  204. stackAnimation[mouseHoveredStack->ID]->playOnce(ECreatureAnimType::MOUSEON);
  205. }
  206. }
  207. else
  208. mouseHoveredStack = nullptr;
  209. }
  210. bool BattleStacksController::stackNeedsAmountBox(const CStack * stack) const
  211. {
  212. BattleHex currentActionTarget;
  213. if(owner.curInt->curAction)
  214. {
  215. auto target = owner.curInt->curAction->getTarget(owner.curInt->cb.get());
  216. if(!target.empty())
  217. currentActionTarget = target.at(0).hexValue;
  218. }
  219. if(stack->hasBonusOfType(Bonus::SIEGE_WEAPON) && stack->getCount() == 1) //do not show box for singular war machines, stacked war machines with box shown are supported as extension feature
  220. return false;
  221. if (!owner.battleActionsStarted) // do not perform any further checks since they are related to actions that will only occur after intro music
  222. return true;
  223. if(!stack->alive())
  224. return false;
  225. if(stack->getCount() == 0) //hide box when target is going to die anyway - do not display "0 creatures"
  226. return false;
  227. for(auto anim : currentAnimations) //no matter what other conditions below are, hide box when creature is playing hit animation
  228. {
  229. auto hitAnimation = dynamic_cast<DefenceAnimation*>(anim);
  230. if(hitAnimation && (hitAnimation->stack->ID == stack->ID))
  231. return false;
  232. }
  233. if(owner.curInt->curAction)
  234. {
  235. if(owner.curInt->curAction->stackNumber == stack->ID) //stack is currently taking action (is not a target of another creature's action etc)
  236. {
  237. if(owner.curInt->curAction->actionType == EActionType::WALK || owner.curInt->curAction->actionType == EActionType::SHOOT) //hide when stack walks or shoots
  238. return false;
  239. else if(owner.curInt->curAction->actionType == EActionType::WALK_AND_ATTACK && currentActionTarget != stack->getPosition()) //when attacking, hide until walk phase finished
  240. return false;
  241. }
  242. if(owner.curInt->curAction->actionType == EActionType::SHOOT && currentActionTarget == stack->getPosition()) //hide if we are ranged attack target
  243. return false;
  244. }
  245. return true;
  246. }
  247. std::shared_ptr<IImage> BattleStacksController::getStackAmountBox(const CStack * stack)
  248. {
  249. std::vector<si32> activeSpells = stack->activeSpells();
  250. if ( activeSpells.empty())
  251. return amountNormal;
  252. int effectsPositivness = 0;
  253. for ( auto const & spellID : activeSpells)
  254. effectsPositivness += CGI->spellh->objects.at(spellID)->positiveness;
  255. if (effectsPositivness > 0)
  256. return amountPositive;
  257. if (effectsPositivness < 0)
  258. return amountNegative;
  259. return amountEffNeutral;
  260. }
  261. void BattleStacksController::showStackAmountBox(Canvas & canvas, const CStack * stack)
  262. {
  263. //blitting amount background box
  264. auto amountBG = getStackAmountBox(stack);
  265. const int sideShift = stack->side == BattleSide::ATTACKER ? 1 : -1;
  266. const int reverseSideShift = stack->side == BattleSide::ATTACKER ? -1 : 1;
  267. const BattleHex nextPos = stack->getPosition() + sideShift;
  268. const bool edge = stack->getPosition() % GameConstants::BFIELD_WIDTH == (stack->side == BattleSide::ATTACKER ? GameConstants::BFIELD_WIDTH - 2 : 1);
  269. const bool moveInside = !edge && !owner.fieldController->stackCountOutsideHex(nextPos);
  270. int xAdd = (stack->side == BattleSide::ATTACKER ? 220 : 202) +
  271. (stack->doubleWide() ? 44 : 0) * sideShift +
  272. (moveInside ? amountBG->width() + 10 : 0) * reverseSideShift;
  273. int yAdd = 260 + ((stack->side == BattleSide::ATTACKER || moveInside) ? 0 : -15);
  274. canvas.draw(amountBG, stackAnimation[stack->ID]->pos.topLeft() + Point(xAdd, yAdd));
  275. //blitting amount
  276. Point textPos = stackAnimation[stack->ID]->pos.topLeft() + amountBG->dimensions()/2 + Point(xAdd, yAdd);
  277. canvas.drawText(textPos, EFonts::FONT_TINY, Colors::WHITE, ETextAlignment::CENTER, makeNumberShort(stack->getCount()));
  278. }
  279. void BattleStacksController::showStack(Canvas & canvas, const CStack * stack)
  280. {
  281. stackAnimation[stack->ID]->nextFrame(canvas, facingRight(stack)); // do actual blit
  282. stackAnimation[stack->ID]->incrementFrame(float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000);
  283. }
  284. void BattleStacksController::updateBattleAnimations()
  285. {
  286. for (auto & elem : currentAnimations)
  287. {
  288. if (!elem)
  289. continue;
  290. if (elem->isInitialized())
  291. elem->nextFrame();
  292. else
  293. elem->tryInitialize();
  294. }
  295. bool hadAnimations = !currentAnimations.empty();
  296. vstd::erase(currentAnimations, nullptr);
  297. if (hadAnimations && currentAnimations.empty())
  298. {
  299. //anims ended
  300. owner.setAnimationCondition(EAnimationEvents::ACTION, false);
  301. }
  302. }
  303. void BattleStacksController::addNewAnim(BattleAnimation *anim)
  304. {
  305. currentAnimations.push_back(anim);
  306. owner.setAnimationCondition(EAnimationEvents::ACTION, true);
  307. }
  308. void BattleStacksController::stackActivated(const CStack *stack) //TODO: check it all before game state is changed due to abilities
  309. {
  310. stackToActivate = stack;
  311. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  312. if (stackToActivate) //during waiting stack may have gotten activated through show
  313. owner.activateStack();
  314. }
  315. void BattleStacksController::stackRemoved(uint32_t stackID)
  316. {
  317. if (getActiveStack() && getActiveStack()->ID == stackID)
  318. {
  319. BattleAction *action = new BattleAction();
  320. action->side = owner.defendingHeroInstance ? (owner.curInt->playerID == owner.defendingHeroInstance->tempOwner) : false;
  321. action->actionType = EActionType::CANCEL;
  322. action->stackNumber = getActiveStack()->ID;
  323. owner.givenCommand.setn(action);
  324. setActiveStack(nullptr);
  325. }
  326. }
  327. void BattleStacksController::stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos)
  328. {
  329. for(auto & attackedInfo : attackedInfos)
  330. {
  331. if (!attackedInfo.attacker)
  332. continue;
  333. bool needsReverse =
  334. owner.curInt->cb->isToReverse(
  335. attackedInfo.defender->getPosition(),
  336. attackedInfo.attacker->getPosition(),
  337. facingRight(attackedInfo.defender),
  338. attackedInfo.attacker->doubleWide(),
  339. facingRight(attackedInfo.attacker));
  340. if (needsReverse)
  341. {
  342. owner.executeOnAnimationCondition(EAnimationEvents::MOVEMENT, true, [=]()
  343. {
  344. addNewAnim(new ReverseAnimation(owner, attackedInfo.defender, attackedInfo.defender->getPosition()));
  345. });
  346. }
  347. }
  348. for(auto & attackedInfo : attackedInfos)
  349. {
  350. bool useDeathAnim = attackedInfo.killed;
  351. bool useDefenceAnim = attackedInfo.defender->defendingAnim && !attackedInfo.indirectAttack && !attackedInfo.killed;
  352. EAnimationEvents usedEvent = useDefenceAnim ? EAnimationEvents::ATTACK : EAnimationEvents::HIT;
  353. owner.executeOnAnimationCondition(usedEvent, true, [=]()
  354. {
  355. if (useDeathAnim)
  356. addNewAnim(new DeathAnimation(owner, attackedInfo.defender, attackedInfo.indirectAttack));
  357. else if(useDefenceAnim)
  358. addNewAnim(new DefenceAnimation(owner, attackedInfo.defender));
  359. else
  360. addNewAnim(new HittedAnimation(owner, attackedInfo.defender));
  361. if (attackedInfo.battleEffect != EBattleEffect::INVALID)
  362. owner.effectsController->displayEffect(EBattleEffect::EBattleEffect(attackedInfo.battleEffect), attackedInfo.defender->getPosition());
  363. if (attackedInfo.spellEffect != SpellID::NONE)
  364. owner.displaySpellEffect(attackedInfo.spellEffect, attackedInfo.defender->getPosition());
  365. });
  366. }
  367. for (auto & attackedInfo : attackedInfos)
  368. {
  369. if (attackedInfo.rebirth)
  370. {
  371. owner.executeOnAnimationCondition(EAnimationEvents::AFTER_HIT, true, [=](){
  372. owner.effectsController->displayEffect(EBattleEffect::RESURRECT, soundBase::RESURECT, attackedInfo.defender->getPosition());
  373. addNewAnim(new ResurrectionAnimation(owner, attackedInfo.defender));
  374. });
  375. }
  376. if (attackedInfo.cloneKilled)
  377. {
  378. owner.executeOnAnimationCondition(EAnimationEvents::AFTER_HIT, true, [=](){
  379. stackRemoved(attackedInfo.defender->ID);
  380. });
  381. }
  382. }
  383. executeAttackAnimations();
  384. }
  385. void BattleStacksController::stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance)
  386. {
  387. assert(destHex.size() > 0);
  388. //FIXME: there should be no more ongoing animations. If not - then some other method created animations but did not wait for them to end
  389. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  390. if(shouldRotate(stack, stack->getPosition(), destHex[0]))
  391. addNewAnim(new ReverseAnimation(owner, stack, destHex[0]));
  392. addNewAnim(new MovementStartAnimation(owner, stack));
  393. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  394. addNewAnim(new MovementAnimation(owner, stack, destHex, distance));
  395. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  396. addNewAnim(new MovementEndAnimation(owner, stack, destHex.back()));
  397. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  398. }
  399. void BattleStacksController::stackAttacking( const StackAttackInfo & info )
  400. {
  401. //FIXME: there should be no more ongoing animations. If not - then some other method created animations but did not wait for them to end
  402. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  403. bool needsReverse =
  404. owner.curInt->cb->isToReverse(
  405. info.attacker->getPosition(),
  406. info.defender->getPosition(),
  407. facingRight(info.attacker),
  408. info.attacker->doubleWide(),
  409. facingRight(info.defender));
  410. auto attacker = info.attacker;
  411. auto defender = info.defender;
  412. auto tile = info.tile;
  413. auto spellEffect = info.spellEffect;
  414. if (needsReverse)
  415. {
  416. owner.executeOnAnimationCondition(EAnimationEvents::MOVEMENT, true, [=]()
  417. {
  418. addNewAnim(new ReverseAnimation(owner, attacker, attacker->getPosition()));
  419. });
  420. }
  421. if(info.lucky)
  422. {
  423. owner.executeOnAnimationCondition(EAnimationEvents::BEFORE_HIT, true, [=]() {
  424. owner.controlPanel->console->addText(info.attacker->formatGeneralMessage(-45));
  425. owner.effectsController->displayEffect(EBattleEffect::GOOD_LUCK, soundBase::GOODLUCK, attacker->getPosition());
  426. });
  427. }
  428. if(info.unlucky)
  429. {
  430. owner.executeOnAnimationCondition(EAnimationEvents::BEFORE_HIT, true, [=]() {
  431. owner.controlPanel->console->addText(info.attacker->formatGeneralMessage(-44));
  432. owner.effectsController->displayEffect(EBattleEffect::BAD_LUCK, soundBase::BADLUCK, attacker->getPosition());
  433. });
  434. }
  435. if(info.deathBlow)
  436. {
  437. owner.executeOnAnimationCondition(EAnimationEvents::BEFORE_HIT, true, [=]() {
  438. owner.controlPanel->console->addText(info.attacker->formatGeneralMessage(365));
  439. owner.effectsController->displayEffect(EBattleEffect::DEATH_BLOW, soundBase::deathBlow, defender->getPosition());
  440. });
  441. for(auto elem : info.secondaryDefender)
  442. {
  443. owner.executeOnAnimationCondition(EAnimationEvents::BEFORE_HIT, true, [=]() {
  444. owner.effectsController->displayEffect(EBattleEffect::DEATH_BLOW, elem->getPosition());
  445. });
  446. }
  447. }
  448. owner.executeOnAnimationCondition(EAnimationEvents::ATTACK, true, [=]()
  449. {
  450. if (info.indirectAttack)
  451. {
  452. addNewAnim(new ShootingAnimation(owner, attacker, tile, defender));
  453. }
  454. else
  455. {
  456. addNewAnim(new MeleeAttackAnimation(owner, attacker, tile, defender));
  457. }
  458. });
  459. if (info.spellEffect)
  460. {
  461. owner.executeOnAnimationCondition(EAnimationEvents::HIT, true, [=]()
  462. {
  463. owner.displaySpellHit(spellEffect, tile);
  464. });
  465. }
  466. if (info.lifeDrain)
  467. {
  468. owner.executeOnAnimationCondition(EAnimationEvents::AFTER_HIT, true, [=]()
  469. {
  470. owner.effectsController->displayEffect(EBattleEffect::DRAIN_LIFE, soundBase::DRAINLIF, attacker->getPosition());
  471. });
  472. }
  473. //return, animation playback will be handled by stacksAreAttacked
  474. }
  475. void BattleStacksController::executeAttackAnimations()
  476. {
  477. owner.setAnimationCondition(EAnimationEvents::MOVEMENT, true);
  478. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  479. owner.setAnimationCondition(EAnimationEvents::MOVEMENT, false);
  480. owner.setAnimationCondition(EAnimationEvents::BEFORE_HIT, true);
  481. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  482. owner.setAnimationCondition(EAnimationEvents::BEFORE_HIT, false);
  483. owner.setAnimationCondition(EAnimationEvents::ATTACK, true);
  484. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  485. owner.setAnimationCondition(EAnimationEvents::ATTACK, false);
  486. // Note that HIT event can also be emitted by attack animation
  487. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  488. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  489. owner.setAnimationCondition(EAnimationEvents::HIT, false);
  490. owner.setAnimationCondition(EAnimationEvents::AFTER_HIT, true);
  491. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  492. owner.setAnimationCondition(EAnimationEvents::AFTER_HIT, false);
  493. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  494. }
  495. bool BattleStacksController::shouldRotate(const CStack * stack, const BattleHex & oldPos, const BattleHex & nextHex) const
  496. {
  497. Point begPosition = getStackPositionAtHex(oldPos,stack);
  498. Point endPosition = getStackPositionAtHex(nextHex, stack);
  499. if((begPosition.x > endPosition.x) && facingRight(stack))
  500. return true;
  501. else if((begPosition.x < endPosition.x) && !facingRight(stack))
  502. return true;
  503. return false;
  504. }
  505. void BattleStacksController::endAction(const BattleAction* action)
  506. {
  507. //FIXME: there should be no more ongoing animations. If not - then some other method created animations but did not wait for them to end
  508. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  509. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  510. //check if we should reverse stacks
  511. TStacks stacks = owner.curInt->cb->battleGetStacks(CBattleCallback::MINE_AND_ENEMY);
  512. for (const CStack *s : stacks)
  513. {
  514. bool shouldFaceRight = s && s->side == BattleSide::ATTACKER;
  515. if (s && facingRight(s) != shouldFaceRight && s->alive() && stackAnimation[s->ID]->isIdle())
  516. {
  517. addNewAnim(new ReverseAnimation(owner, s, s->getPosition()));
  518. }
  519. }
  520. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  521. //FIXME: there should be no more ongoing animations. If not - then some other method created animations but did not wait for them to end
  522. assert(owner.getAnimationCondition(EAnimationEvents::OPENING) == false);
  523. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  524. assert(owner.getAnimationCondition(EAnimationEvents::MOVEMENT) == false);
  525. assert(owner.getAnimationCondition(EAnimationEvents::ATTACK) == false);
  526. assert(owner.getAnimationCondition(EAnimationEvents::HIT) == false);
  527. assert(owner.getAnimationCondition(EAnimationEvents::PROJECTILES) == false);
  528. owner.controlPanel->blockUI(activeStack == nullptr);
  529. }
  530. void BattleStacksController::startAction(const BattleAction* action)
  531. {
  532. setHoveredStack(nullptr);
  533. }
  534. void BattleStacksController::activateStack()
  535. {
  536. if ( !currentAnimations.empty())
  537. return;
  538. if ( !stackToActivate)
  539. return;
  540. owner.trySetActivePlayer(stackToActivate->owner);
  541. setActiveStack(stackToActivate);
  542. stackToActivate = nullptr;
  543. const CStack * s = getActiveStack();
  544. if(!s)
  545. return;
  546. //set casting flag to true if creature can use it to not check it every time
  547. const auto spellcaster = s->getBonusLocalFirst(Selector::type()(Bonus::SPELLCASTER));
  548. const auto randomSpellcaster = s->getBonusLocalFirst(Selector::type()(Bonus::RANDOM_SPELLCASTER));
  549. if(s->canCast() && (spellcaster || randomSpellcaster))
  550. {
  551. stackCanCastSpell = true;
  552. if(randomSpellcaster)
  553. creatureSpellToCast = -1; //spell will be set later on cast
  554. else
  555. creatureSpellToCast = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), s, CBattleInfoCallback::RANDOM_AIMED); //faerie dragon can cast only one spell until their next move
  556. //TODO: what if creature can cast BOTH random genie spell and aimed spell?
  557. //TODO: faerie dragon type spell should be selected by server
  558. }
  559. else
  560. {
  561. stackCanCastSpell = false;
  562. creatureSpellToCast = -1;
  563. }
  564. }
  565. void BattleStacksController::setSelectedStack(const CStack *stack)
  566. {
  567. selectedStack = stack;
  568. }
  569. const CStack* BattleStacksController::getSelectedStack() const
  570. {
  571. return selectedStack;
  572. }
  573. const CStack* BattleStacksController::getActiveStack() const
  574. {
  575. return activeStack;
  576. }
  577. bool BattleStacksController::facingRight(const CStack * stack) const
  578. {
  579. return stackFacingRight.at(stack->ID);
  580. }
  581. bool BattleStacksController::activeStackSpellcaster()
  582. {
  583. return stackCanCastSpell;
  584. }
  585. SpellID BattleStacksController::activeStackSpellToCast()
  586. {
  587. if (!stackCanCastSpell)
  588. return SpellID::NONE;
  589. return SpellID(creatureSpellToCast);
  590. }
  591. Point BattleStacksController::getStackPositionAtHex(BattleHex hexNum, const CStack * stack) const
  592. {
  593. Point ret(-500, -500); //returned value
  594. if(stack && stack->initialPosition < 0) //creatures in turrets
  595. return owner.siegeController->getTurretCreaturePosition(stack->initialPosition);
  596. static const Point basePos(-190, -139); // position of creature in topleft corner
  597. static const int imageShiftX = 30; // X offset to base pos for facing right stacks, negative for facing left
  598. ret.x = basePos.x + 22 * ( (hexNum.getY() + 1)%2 ) + 44 * hexNum.getX();
  599. ret.y = basePos.y + 42 * hexNum.getY();
  600. if (stack)
  601. {
  602. if(facingRight(stack))
  603. ret.x += imageShiftX;
  604. else
  605. ret.x -= imageShiftX;
  606. //shifting position for double - hex creatures
  607. if(stack->doubleWide())
  608. {
  609. if(stack->side == BattleSide::ATTACKER)
  610. {
  611. if(facingRight(stack))
  612. ret.x -= 44;
  613. }
  614. else
  615. {
  616. if(!facingRight(stack))
  617. ret.x += 44;
  618. }
  619. }
  620. }
  621. //returning
  622. return ret + owner.pos.topLeft();
  623. }