BattleStacksController.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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 (CStackMoveAnimation *move = dynamic_cast<CStackMoveAnimation*>(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 CResurrectionAnimation(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<CDefenceAnimation*>(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.controlPanel->blockUI(activeStack == nullptr);
  301. owner.setAnimationCondition(EAnimationEvents::ACTION, false);
  302. }
  303. }
  304. void BattleStacksController::addNewAnim(CBattleAnimation *anim)
  305. {
  306. currentAnimations.push_back(anim);
  307. owner.setAnimationCondition(EAnimationEvents::ACTION, true);
  308. }
  309. void BattleStacksController::stackActivated(const CStack *stack) //TODO: check it all before game state is changed due to abilities
  310. {
  311. stackToActivate = stack;
  312. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  313. if (stackToActivate) //during waiting stack may have gotten activated through show
  314. owner.activateStack();
  315. }
  316. void BattleStacksController::stackRemoved(uint32_t stackID)
  317. {
  318. if (getActiveStack() && getActiveStack()->ID == stackID)
  319. {
  320. BattleAction *action = new BattleAction();
  321. action->side = owner.defendingHeroInstance ? (owner.curInt->playerID == owner.defendingHeroInstance->tempOwner) : false;
  322. action->actionType = EActionType::CANCEL;
  323. action->stackNumber = getActiveStack()->ID;
  324. owner.givenCommand.setn(action);
  325. setActiveStack(nullptr);
  326. }
  327. }
  328. void BattleStacksController::stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos)
  329. {
  330. for(auto & attackedInfo : attackedInfos)
  331. {
  332. if (!attackedInfo.attacker)
  333. continue;
  334. bool needsReverse =
  335. owner.curInt->cb->isToReverse(
  336. attackedInfo.defender->getPosition(),
  337. attackedInfo.attacker->getPosition(),
  338. facingRight(attackedInfo.defender),
  339. attackedInfo.attacker->doubleWide(),
  340. facingRight(attackedInfo.attacker));
  341. if (needsReverse)
  342. {
  343. owner.executeOnAnimationCondition(EAnimationEvents::MOVEMENT, true, [=]()
  344. {
  345. addNewAnim(new CReverseAnimation(owner, attackedInfo.defender, attackedInfo.defender->getPosition()));
  346. });
  347. }
  348. }
  349. for(auto & attackedInfo : attackedInfos)
  350. {
  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. addNewAnim(new CDefenceAnimation(attackedInfo, owner));
  356. if (attackedInfo.battleEffect != EBattleEffect::INVALID)
  357. owner.effectsController->displayEffect(EBattleEffect::EBattleEffect(attackedInfo.battleEffect), attackedInfo.defender->getPosition());
  358. if (attackedInfo.spellEffect != SpellID::NONE)
  359. owner.displaySpellEffect(attackedInfo.spellEffect, attackedInfo.defender->getPosition());
  360. });
  361. }
  362. for (auto & attackedInfo : attackedInfos)
  363. {
  364. if (attackedInfo.rebirth)
  365. {
  366. owner.executeOnAnimationCondition(EAnimationEvents::AFTER_HIT, true, [=](){
  367. owner.effectsController->displayEffect(EBattleEffect::RESURRECT, soundBase::RESURECT, attackedInfo.defender->getPosition());
  368. addNewAnim(new CResurrectionAnimation(owner, attackedInfo.defender));
  369. });
  370. }
  371. if (attackedInfo.cloneKilled)
  372. {
  373. owner.executeOnAnimationCondition(EAnimationEvents::AFTER_HIT, true, [=](){
  374. stackRemoved(attackedInfo.defender->ID);
  375. });
  376. }
  377. }
  378. executeAttackAnimations();
  379. }
  380. void BattleStacksController::stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance)
  381. {
  382. assert(destHex.size() > 0);
  383. //FIXME: there should be no more ongoing animations. If not - then some other method created animations but did not wait for them to end
  384. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  385. if(shouldRotate(stack, stack->getPosition(), destHex[0]))
  386. addNewAnim(new CReverseAnimation(owner, stack, destHex[0]));
  387. addNewAnim(new CMovementStartAnimation(owner, stack));
  388. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  389. addNewAnim(new CMovementAnimation(owner, stack, destHex, distance));
  390. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  391. addNewAnim(new CMovementEndAnimation(owner, stack, destHex.back()));
  392. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  393. }
  394. void BattleStacksController::stackAttacking( const StackAttackInfo & info )
  395. {
  396. //FIXME: there should be no more ongoing animations. If not - then some other method created animations but did not wait for them to end
  397. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  398. bool needsReverse =
  399. owner.curInt->cb->isToReverse(
  400. info.attacker->getPosition(),
  401. info.defender->getPosition(),
  402. facingRight(info.attacker),
  403. info.attacker->doubleWide(),
  404. facingRight(info.defender));
  405. if (needsReverse)
  406. {
  407. owner.executeOnAnimationCondition(EAnimationEvents::MOVEMENT, true, [=]()
  408. {
  409. addNewAnim(new CReverseAnimation(owner, info.attacker, info.attacker->getPosition()));
  410. });
  411. }
  412. if(info.lucky)
  413. {
  414. owner.executeOnAnimationCondition(EAnimationEvents::BEFORE_HIT, true, [=]() {
  415. owner.controlPanel->console->addText(info.attacker->formatGeneralMessage(-45));
  416. owner.effectsController->displayEffect(EBattleEffect::GOOD_LUCK, soundBase::GOODLUCK, info.attacker->getPosition());
  417. });
  418. }
  419. if(info.unlucky)
  420. {
  421. owner.executeOnAnimationCondition(EAnimationEvents::BEFORE_HIT, true, [=]() {
  422. owner.controlPanel->console->addText(info.attacker->formatGeneralMessage(-44));
  423. owner.effectsController->displayEffect(EBattleEffect::BAD_LUCK, soundBase::BADLUCK, info.attacker->getPosition());
  424. });
  425. }
  426. if(info.deathBlow)
  427. {
  428. owner.executeOnAnimationCondition(EAnimationEvents::BEFORE_HIT, true, [=]() {
  429. owner.controlPanel->console->addText(info.attacker->formatGeneralMessage(365));
  430. owner.effectsController->displayEffect(EBattleEffect::DEATH_BLOW, soundBase::deathBlow, info.defender->getPosition());
  431. });
  432. for(auto & elem : info.secondaryDefender)
  433. {
  434. owner.executeOnAnimationCondition(EAnimationEvents::BEFORE_HIT, true, [=]() {
  435. owner.effectsController->displayEffect(EBattleEffect::DEATH_BLOW, elem->getPosition());
  436. });
  437. }
  438. }
  439. owner.executeOnAnimationCondition(EAnimationEvents::ATTACK, true, [=]()
  440. {
  441. if (info.indirectAttack)
  442. {
  443. addNewAnim(new CShootingAnimation(owner, info.attacker, info.tile, info.defender));
  444. }
  445. else
  446. {
  447. addNewAnim(new CMeleeAttackAnimation(owner, info.attacker, info.tile, info.defender));
  448. }
  449. });
  450. if (info.spellEffect)
  451. {
  452. owner.executeOnAnimationCondition(EAnimationEvents::HIT, true, [=]()
  453. {
  454. owner.displaySpellHit(info.spellEffect, info.tile);
  455. });
  456. }
  457. if (info.lifeDrain)
  458. {
  459. owner.executeOnAnimationCondition(EAnimationEvents::AFTER_HIT, true, [=]()
  460. {
  461. owner.effectsController->displayEffect(EBattleEffect::DRAIN_LIFE, soundBase::DRAINLIF, info.attacker->getPosition());
  462. });
  463. }
  464. //return, animation playback will be handled by stacksAreAttacked
  465. }
  466. void BattleStacksController::executeAttackAnimations()
  467. {
  468. owner.setAnimationCondition(EAnimationEvents::MOVEMENT, true);
  469. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  470. owner.setAnimationCondition(EAnimationEvents::MOVEMENT, false);
  471. owner.setAnimationCondition(EAnimationEvents::BEFORE_HIT, true);
  472. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  473. owner.setAnimationCondition(EAnimationEvents::BEFORE_HIT, false);
  474. owner.setAnimationCondition(EAnimationEvents::ATTACK, true);
  475. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  476. owner.setAnimationCondition(EAnimationEvents::ATTACK, false);
  477. // Note that HIT event can also be emitted by attack animation
  478. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  479. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  480. owner.setAnimationCondition(EAnimationEvents::HIT, false);
  481. owner.setAnimationCondition(EAnimationEvents::AFTER_HIT, true);
  482. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  483. owner.setAnimationCondition(EAnimationEvents::AFTER_HIT, false);
  484. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  485. }
  486. bool BattleStacksController::shouldRotate(const CStack * stack, const BattleHex & oldPos, const BattleHex & nextHex) const
  487. {
  488. Point begPosition = getStackPositionAtHex(oldPos,stack);
  489. Point endPosition = getStackPositionAtHex(nextHex, stack);
  490. if((begPosition.x > endPosition.x) && facingRight(stack))
  491. return true;
  492. else if((begPosition.x < endPosition.x) && !facingRight(stack))
  493. return true;
  494. return false;
  495. }
  496. void BattleStacksController::endAction(const BattleAction* action)
  497. {
  498. //FIXME: there should be no more ongoing animations. If not - then some other method created animations but did not wait for them to end
  499. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  500. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  501. //check if we should reverse stacks
  502. TStacks stacks = owner.curInt->cb->battleGetStacks(CBattleCallback::MINE_AND_ENEMY);
  503. for (const CStack *s : stacks)
  504. {
  505. bool shouldFaceRight = s && s->side == BattleSide::ATTACKER;
  506. if (s && facingRight(s) != shouldFaceRight && s->alive() && stackAnimation[s->ID]->isIdle())
  507. {
  508. addNewAnim(new CReverseAnimation(owner, s, s->getPosition()));
  509. }
  510. }
  511. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  512. //FIXME: there should be no more ongoing animations. If not - then some other method created animations but did not wait for them to end
  513. assert(owner.getAnimationCondition(EAnimationEvents::OPENING) == false);
  514. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  515. assert(owner.getAnimationCondition(EAnimationEvents::MOVEMENT) == false);
  516. assert(owner.getAnimationCondition(EAnimationEvents::ATTACK) == false);
  517. assert(owner.getAnimationCondition(EAnimationEvents::HIT) == false);
  518. assert(owner.getAnimationCondition(EAnimationEvents::PROJECTILES) == false);
  519. }
  520. void BattleStacksController::startAction(const BattleAction* action)
  521. {
  522. setHoveredStack(nullptr);
  523. }
  524. void BattleStacksController::activateStack()
  525. {
  526. if ( !currentAnimations.empty())
  527. return;
  528. if ( !stackToActivate)
  529. return;
  530. owner.trySetActivePlayer(stackToActivate->owner);
  531. setActiveStack(stackToActivate);
  532. stackToActivate = nullptr;
  533. const CStack * s = getActiveStack();
  534. if(!s)
  535. return;
  536. //set casting flag to true if creature can use it to not check it every time
  537. const auto spellcaster = s->getBonusLocalFirst(Selector::type()(Bonus::SPELLCASTER));
  538. const auto randomSpellcaster = s->getBonusLocalFirst(Selector::type()(Bonus::RANDOM_SPELLCASTER));
  539. if(s->canCast() && (spellcaster || randomSpellcaster))
  540. {
  541. stackCanCastSpell = true;
  542. if(randomSpellcaster)
  543. creatureSpellToCast = -1; //spell will be set later on cast
  544. else
  545. creatureSpellToCast = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), s, CBattleInfoCallback::RANDOM_AIMED); //faerie dragon can cast only one spell until their next move
  546. //TODO: what if creature can cast BOTH random genie spell and aimed spell?
  547. //TODO: faerie dragon type spell should be selected by server
  548. }
  549. else
  550. {
  551. stackCanCastSpell = false;
  552. creatureSpellToCast = -1;
  553. }
  554. }
  555. void BattleStacksController::setSelectedStack(const CStack *stack)
  556. {
  557. selectedStack = stack;
  558. }
  559. const CStack* BattleStacksController::getSelectedStack() const
  560. {
  561. return selectedStack;
  562. }
  563. const CStack* BattleStacksController::getActiveStack() const
  564. {
  565. return activeStack;
  566. }
  567. bool BattleStacksController::facingRight(const CStack * stack) const
  568. {
  569. return stackFacingRight.at(stack->ID);
  570. }
  571. bool BattleStacksController::activeStackSpellcaster()
  572. {
  573. return stackCanCastSpell;
  574. }
  575. SpellID BattleStacksController::activeStackSpellToCast()
  576. {
  577. if (!stackCanCastSpell)
  578. return SpellID::NONE;
  579. return SpellID(creatureSpellToCast);
  580. }
  581. Point BattleStacksController::getStackPositionAtHex(BattleHex hexNum, const CStack * stack) const
  582. {
  583. Point ret(-500, -500); //returned value
  584. if(stack && stack->initialPosition < 0) //creatures in turrets
  585. return owner.siegeController->getTurretCreaturePosition(stack->initialPosition);
  586. static const Point basePos(-190, -139); // position of creature in topleft corner
  587. static const int imageShiftX = 30; // X offset to base pos for facing right stacks, negative for facing left
  588. ret.x = basePos.x + 22 * ( (hexNum.getY() + 1)%2 ) + 44 * hexNum.getX();
  589. ret.y = basePos.y + 42 * hexNum.getY();
  590. if (stack)
  591. {
  592. if(facingRight(stack))
  593. ret.x += imageShiftX;
  594. else
  595. ret.x -= imageShiftX;
  596. //shifting position for double - hex creatures
  597. if(stack->doubleWide())
  598. {
  599. if(stack->side == BattleSide::ATTACKER)
  600. {
  601. if(facingRight(stack))
  602. ret.x -= 44;
  603. }
  604. else
  605. {
  606. if(!facingRight(stack))
  607. ret.x += 44;
  608. }
  609. }
  610. }
  611. //returning
  612. return ret + owner.pos.topLeft();
  613. }