BattleStacksController.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. auto iter = stackAnimation.find(stack->ID);
  134. if(iter == stackAnimation.end())
  135. {
  136. logGlobal->error("Unit %d have no animation", stack->ID);
  137. return;
  138. }
  139. auto animation = iter->second;
  140. if(stack->alive() && animation->isDeadOrDying())
  141. {
  142. addNewAnim(new CResurrectionAnimation(owner, stack));
  143. }
  144. static const ColorShifterMultiplyAndAdd shifterClone ({255, 255, 0, 255}, {0, 0, 255, 0});
  145. if (stack->isClone())
  146. {
  147. animation->shiftColor(&shifterClone);
  148. }
  149. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  150. }
  151. void BattleStacksController::stackAdded(const CStack * stack)
  152. {
  153. // Tower shooters have only their upper half visible
  154. static const int turretCreatureAnimationHeight = 235;
  155. stackFacingRight[stack->ID] = stack->side == BattleSide::ATTACKER; // must be set before getting stack position
  156. Point coords = getStackPositionAtHex(stack->getPosition(), stack);
  157. if(stack->initialPosition < 0) //turret
  158. {
  159. assert(owner.siegeController);
  160. const CCreature *turretCreature = owner.siegeController->getTurretCreature();
  161. stackAnimation[stack->ID] = AnimationControls::getAnimation(turretCreature);
  162. stackAnimation[stack->ID]->pos.h = turretCreatureAnimationHeight;
  163. coords = owner.siegeController->getTurretCreaturePosition(stack->initialPosition);
  164. }
  165. else
  166. {
  167. stackAnimation[stack->ID] = AnimationControls::getAnimation(stack->getCreature());
  168. stackAnimation[stack->ID]->onAnimationReset += std::bind(&onAnimationFinished, stack, stackAnimation[stack->ID]);
  169. stackAnimation[stack->ID]->pos.h = stackAnimation[stack->ID]->getHeight();
  170. }
  171. stackAnimation[stack->ID]->pos.x = coords.x;
  172. stackAnimation[stack->ID]->pos.y = coords.y;
  173. stackAnimation[stack->ID]->pos.w = stackAnimation[stack->ID]->getWidth();
  174. stackAnimation[stack->ID]->setType(ECreatureAnimType::HOLDING);
  175. }
  176. void BattleStacksController::setActiveStack(const CStack *stack)
  177. {
  178. if (activeStack) // update UI
  179. stackAnimation[activeStack->ID]->setBorderColor(AnimationControls::getNoBorder());
  180. activeStack = stack;
  181. if (activeStack) // update UI
  182. stackAnimation[activeStack->ID]->setBorderColor(AnimationControls::getGoldBorder());
  183. owner.controlPanel->blockUI(activeStack == nullptr);
  184. }
  185. void BattleStacksController::setHoveredStack(const CStack *stack)
  186. {
  187. if ( stack == mouseHoveredStack )
  188. return;
  189. if (mouseHoveredStack)
  190. stackAnimation[mouseHoveredStack->ID]->setBorderColor(AnimationControls::getNoBorder());
  191. // stack must be alive and not active (which uses gold border instead)
  192. if (stack && stack->alive() && stack != activeStack)
  193. {
  194. mouseHoveredStack = stack;
  195. if (mouseHoveredStack)
  196. {
  197. stackAnimation[mouseHoveredStack->ID]->setBorderColor(AnimationControls::getBlueBorder());
  198. if (stackAnimation[mouseHoveredStack->ID]->framesInGroup(ECreatureAnimType::MOUSEON) > 0)
  199. stackAnimation[mouseHoveredStack->ID]->playOnce(ECreatureAnimType::MOUSEON);
  200. }
  201. }
  202. else
  203. mouseHoveredStack = nullptr;
  204. }
  205. bool BattleStacksController::stackNeedsAmountBox(const CStack * stack) const
  206. {
  207. BattleHex currentActionTarget;
  208. if(owner.curInt->curAction)
  209. {
  210. auto target = owner.curInt->curAction->getTarget(owner.curInt->cb.get());
  211. if(!target.empty())
  212. currentActionTarget = target.at(0).hexValue;
  213. }
  214. 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
  215. return false;
  216. if (!owner.battleActionsStarted) // do not perform any further checks since they are related to actions that will only occur after intro music
  217. return true;
  218. if(!stack->alive())
  219. return false;
  220. if(stack->getCount() == 0) //hide box when target is going to die anyway - do not display "0 creatures"
  221. return false;
  222. for(auto anim : currentAnimations) //no matter what other conditions below are, hide box when creature is playing hit animation
  223. {
  224. auto hitAnimation = dynamic_cast<CDefenceAnimation*>(anim);
  225. if(hitAnimation && (hitAnimation->stack->ID == stack->ID))
  226. return false;
  227. }
  228. if(owner.curInt->curAction)
  229. {
  230. if(owner.curInt->curAction->stackNumber == stack->ID) //stack is currently taking action (is not a target of another creature's action etc)
  231. {
  232. if(owner.curInt->curAction->actionType == EActionType::WALK || owner.curInt->curAction->actionType == EActionType::SHOOT) //hide when stack walks or shoots
  233. return false;
  234. else if(owner.curInt->curAction->actionType == EActionType::WALK_AND_ATTACK && currentActionTarget != stack->getPosition()) //when attacking, hide until walk phase finished
  235. return false;
  236. }
  237. if(owner.curInt->curAction->actionType == EActionType::SHOOT && currentActionTarget == stack->getPosition()) //hide if we are ranged attack target
  238. return false;
  239. }
  240. return true;
  241. }
  242. std::shared_ptr<IImage> BattleStacksController::getStackAmountBox(const CStack * stack)
  243. {
  244. std::vector<si32> activeSpells = stack->activeSpells();
  245. if ( activeSpells.empty())
  246. return amountNormal;
  247. int effectsPositivness = 0;
  248. for ( auto const & spellID : activeSpells)
  249. effectsPositivness += CGI->spellh->objects.at(spellID)->positiveness;
  250. if (effectsPositivness > 0)
  251. return amountPositive;
  252. if (effectsPositivness < 0)
  253. return amountNegative;
  254. return amountEffNeutral;
  255. }
  256. void BattleStacksController::showStackAmountBox(Canvas & canvas, const CStack * stack)
  257. {
  258. //blitting amount background box
  259. auto amountBG = getStackAmountBox(stack);
  260. const int sideShift = stack->side == BattleSide::ATTACKER ? 1 : -1;
  261. const int reverseSideShift = stack->side == BattleSide::ATTACKER ? -1 : 1;
  262. const BattleHex nextPos = stack->getPosition() + sideShift;
  263. const bool edge = stack->getPosition() % GameConstants::BFIELD_WIDTH == (stack->side == BattleSide::ATTACKER ? GameConstants::BFIELD_WIDTH - 2 : 1);
  264. const bool moveInside = !edge && !owner.fieldController->stackCountOutsideHex(nextPos);
  265. int xAdd = (stack->side == BattleSide::ATTACKER ? 220 : 202) +
  266. (stack->doubleWide() ? 44 : 0) * sideShift +
  267. (moveInside ? amountBG->width() + 10 : 0) * reverseSideShift;
  268. int yAdd = 260 + ((stack->side == BattleSide::ATTACKER || moveInside) ? 0 : -15);
  269. canvas.draw(amountBG, stackAnimation[stack->ID]->pos.topLeft() + Point(xAdd, yAdd));
  270. //blitting amount
  271. Point textPos = stackAnimation[stack->ID]->pos.topLeft() + amountBG->dimensions()/2 + Point(xAdd, yAdd);
  272. canvas.drawText(textPos, EFonts::FONT_TINY, Colors::WHITE, ETextAlignment::CENTER, makeNumberShort(stack->getCount()));
  273. }
  274. void BattleStacksController::showStack(Canvas & canvas, const CStack * stack)
  275. {
  276. stackAnimation[stack->ID]->nextFrame(canvas, facingRight(stack)); // do actual blit
  277. stackAnimation[stack->ID]->incrementFrame(float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000);
  278. }
  279. void BattleStacksController::updateBattleAnimations()
  280. {
  281. for (auto & elem : currentAnimations)
  282. {
  283. if (!elem)
  284. continue;
  285. if (elem->isInitialized())
  286. elem->nextFrame();
  287. else
  288. elem->tryInitialize();
  289. }
  290. bool hadAnimations = !currentAnimations.empty();
  291. vstd::erase(currentAnimations, nullptr);
  292. if (hadAnimations && currentAnimations.empty())
  293. {
  294. //anims ended
  295. owner.controlPanel->blockUI(activeStack == nullptr);
  296. owner.setAnimationCondition(EAnimationEvents::ACTION, false);
  297. }
  298. }
  299. void BattleStacksController::addNewAnim(CBattleAnimation *anim)
  300. {
  301. currentAnimations.push_back(anim);
  302. owner.setAnimationCondition(EAnimationEvents::ACTION, true);
  303. }
  304. void BattleStacksController::stackActivated(const CStack *stack) //TODO: check it all before game state is changed due to abilities
  305. {
  306. stackToActivate = stack;
  307. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  308. if (stackToActivate) //during waiting stack may have gotten activated through show
  309. owner.activateStack();
  310. }
  311. void BattleStacksController::stackRemoved(uint32_t stackID)
  312. {
  313. if (getActiveStack() && getActiveStack()->ID == stackID)
  314. {
  315. BattleAction *action = new BattleAction();
  316. action->side = owner.defendingHeroInstance ? (owner.curInt->playerID == owner.defendingHeroInstance->tempOwner) : false;
  317. action->actionType = EActionType::CANCEL;
  318. action->stackNumber = getActiveStack()->ID;
  319. owner.givenCommand.setn(action);
  320. setActiveStack(nullptr);
  321. }
  322. }
  323. void BattleStacksController::stacksAreAttacked(std::vector<StackAttackedInfo> attackedInfos)
  324. {
  325. for(auto & attackedInfo : attackedInfos)
  326. {
  327. if (!attackedInfo.attacker)
  328. continue;
  329. bool needsReverse =
  330. owner.curInt->cb->isToReverse(
  331. attackedInfo.defender->getPosition(),
  332. attackedInfo.attacker->getPosition(),
  333. facingRight(attackedInfo.defender),
  334. attackedInfo.attacker->doubleWide(),
  335. facingRight(attackedInfo.attacker));
  336. if (needsReverse)
  337. addNewAnim(new CReverseAnimation(owner, attackedInfo.defender, attackedInfo.defender->getPosition()));
  338. }
  339. // raise flag that movement phase started, starting any queued movements
  340. owner.setAnimationCondition(EAnimationEvents::MOVEMENT, true);
  341. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  342. owner.setAnimationCondition(EAnimationEvents::MOVEMENT, false);
  343. for(auto & attackedInfo : attackedInfos)
  344. {
  345. bool useDefenceAnim = attackedInfo.defender->defendingAnim && !attackedInfo.indirectAttack && !attackedInfo.killed;
  346. EAnimationEvents usedEvent = useDefenceAnim ? EAnimationEvents::ATTACK : EAnimationEvents::HIT;
  347. owner.executeOnAnimationCondition(usedEvent, true, [=]()
  348. {
  349. addNewAnim(new CDefenceAnimation(attackedInfo, owner));
  350. if (attackedInfo.battleEffect != EBattleEffect::INVALID)
  351. owner.effectsController->displayEffect(EBattleEffect::EBattleEffect(attackedInfo.battleEffect), attackedInfo.defender->getPosition());
  352. if (attackedInfo.spellEffect != SpellID::NONE)
  353. owner.displaySpellEffect(attackedInfo.spellEffect, attackedInfo.defender->getPosition());
  354. });
  355. }
  356. owner.setAnimationCondition(EAnimationEvents::ATTACK, true);
  357. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  358. owner.setAnimationCondition(EAnimationEvents::ATTACK, false);
  359. owner.setAnimationCondition(EAnimationEvents::HIT, false);
  360. for (auto & attackedInfo : attackedInfos)
  361. {
  362. if (attackedInfo.rebirth)
  363. {
  364. owner.effectsController->displayEffect(EBattleEffect::RESURRECT, soundBase::RESURECT, attackedInfo.defender->getPosition());
  365. addNewAnim(new CResurrectionAnimation(owner, attackedInfo.defender));
  366. }
  367. if (attackedInfo.cloneKilled)
  368. stackRemoved(attackedInfo.defender->ID);
  369. }
  370. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  371. }
  372. void BattleStacksController::stackMoved(const CStack *stack, std::vector<BattleHex> destHex, int distance)
  373. {
  374. assert(destHex.size() > 0);
  375. //FIXME: there should be no more ongoing animations. If not - then some other method created animations but did not wait for them to end
  376. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  377. if(shouldRotate(stack, stack->getPosition(), destHex[0]))
  378. addNewAnim(new CReverseAnimation(owner, stack, destHex[0]));
  379. addNewAnim(new CMovementStartAnimation(owner, stack));
  380. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  381. addNewAnim(new CMovementAnimation(owner, stack, destHex, distance));
  382. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  383. addNewAnim(new CMovementEndAnimation(owner, stack, destHex.back()));
  384. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  385. }
  386. void BattleStacksController::stackAttacking( const CStack *attacker, BattleHex dest, const CStack *defender, bool shooting )
  387. {
  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. bool needsReverse =
  391. owner.curInt->cb->isToReverse(
  392. attacker->getPosition(),
  393. defender->getPosition(),
  394. facingRight(attacker),
  395. attacker->doubleWide(),
  396. facingRight(defender));
  397. if (needsReverse)
  398. {
  399. owner.executeOnAnimationCondition(EAnimationEvents::MOVEMENT, true, [=]()
  400. {
  401. addNewAnim(new CReverseAnimation(owner, attacker, attacker->getPosition()));
  402. });
  403. }
  404. owner.executeOnAnimationCondition(EAnimationEvents::ATTACK, true, [=]()
  405. {
  406. if (shooting)
  407. {
  408. addNewAnim(new CShootingAnimation(owner, attacker, dest, defender));
  409. }
  410. else
  411. {
  412. addNewAnim(new CMeleeAttackAnimation(owner, attacker, dest, defender));
  413. }
  414. });
  415. //waiting will be done in stacksAreAttacked
  416. }
  417. bool BattleStacksController::shouldRotate(const CStack * stack, const BattleHex & oldPos, const BattleHex & nextHex) const
  418. {
  419. Point begPosition = getStackPositionAtHex(oldPos,stack);
  420. Point endPosition = getStackPositionAtHex(nextHex, stack);
  421. if((begPosition.x > endPosition.x) && facingRight(stack))
  422. return true;
  423. else if((begPosition.x < endPosition.x) && !facingRight(stack))
  424. return true;
  425. return false;
  426. }
  427. void BattleStacksController::endAction(const BattleAction* action)
  428. {
  429. //FIXME: there should be no more ongoing animations. If not - then some other method created animations but did not wait for them to end
  430. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  431. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  432. //check if we should reverse stacks
  433. TStacks stacks = owner.curInt->cb->battleGetStacks(CBattleCallback::MINE_AND_ENEMY);
  434. for (const CStack *s : stacks)
  435. {
  436. bool shouldFaceRight = s && s->side == BattleSide::ATTACKER;
  437. if (s && facingRight(s) != shouldFaceRight && s->alive() && stackAnimation[s->ID]->isIdle())
  438. {
  439. addNewAnim(new CReverseAnimation(owner, s, s->getPosition()));
  440. }
  441. }
  442. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  443. //FIXME: there should be no more ongoing animations. If not - then some other method created animations but did not wait for them to end
  444. assert(owner.getAnimationCondition(EAnimationEvents::OPENING) == false);
  445. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  446. assert(owner.getAnimationCondition(EAnimationEvents::MOVEMENT) == false);
  447. assert(owner.getAnimationCondition(EAnimationEvents::ATTACK) == false);
  448. assert(owner.getAnimationCondition(EAnimationEvents::HIT) == false);
  449. assert(owner.getAnimationCondition(EAnimationEvents::PROJECTILES) == false);
  450. }
  451. void BattleStacksController::startAction(const BattleAction* action)
  452. {
  453. setHoveredStack(nullptr);
  454. }
  455. void BattleStacksController::activateStack()
  456. {
  457. if ( !currentAnimations.empty())
  458. return;
  459. if ( !stackToActivate)
  460. return;
  461. owner.trySetActivePlayer(stackToActivate->owner);
  462. setActiveStack(stackToActivate);
  463. stackToActivate = nullptr;
  464. const CStack * s = getActiveStack();
  465. if(!s)
  466. return;
  467. //set casting flag to true if creature can use it to not check it every time
  468. const auto spellcaster = s->getBonusLocalFirst(Selector::type()(Bonus::SPELLCASTER));
  469. const auto randomSpellcaster = s->getBonusLocalFirst(Selector::type()(Bonus::RANDOM_SPELLCASTER));
  470. if(s->canCast() && (spellcaster || randomSpellcaster))
  471. {
  472. stackCanCastSpell = true;
  473. if(randomSpellcaster)
  474. creatureSpellToCast = -1; //spell will be set later on cast
  475. else
  476. creatureSpellToCast = owner.curInt->cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), s, CBattleInfoCallback::RANDOM_AIMED); //faerie dragon can cast only one spell until their next move
  477. //TODO: what if creature can cast BOTH random genie spell and aimed spell?
  478. //TODO: faerie dragon type spell should be selected by server
  479. }
  480. else
  481. {
  482. stackCanCastSpell = false;
  483. creatureSpellToCast = -1;
  484. }
  485. }
  486. void BattleStacksController::setSelectedStack(const CStack *stack)
  487. {
  488. selectedStack = stack;
  489. }
  490. const CStack* BattleStacksController::getSelectedStack() const
  491. {
  492. return selectedStack;
  493. }
  494. const CStack* BattleStacksController::getActiveStack() const
  495. {
  496. return activeStack;
  497. }
  498. bool BattleStacksController::facingRight(const CStack * stack) const
  499. {
  500. return stackFacingRight.at(stack->ID);
  501. }
  502. bool BattleStacksController::activeStackSpellcaster()
  503. {
  504. return stackCanCastSpell;
  505. }
  506. SpellID BattleStacksController::activeStackSpellToCast()
  507. {
  508. if (!stackCanCastSpell)
  509. return SpellID::NONE;
  510. return SpellID(creatureSpellToCast);
  511. }
  512. Point BattleStacksController::getStackPositionAtHex(BattleHex hexNum, const CStack * stack) const
  513. {
  514. Point ret(-500, -500); //returned value
  515. if(stack && stack->initialPosition < 0) //creatures in turrets
  516. return owner.siegeController->getTurretCreaturePosition(stack->initialPosition);
  517. static const Point basePos(-190, -139); // position of creature in topleft corner
  518. static const int imageShiftX = 30; // X offset to base pos for facing right stacks, negative for facing left
  519. ret.x = basePos.x + 22 * ( (hexNum.getY() + 1)%2 ) + 44 * hexNum.getX();
  520. ret.y = basePos.y + 42 * hexNum.getY();
  521. if (stack)
  522. {
  523. if(facingRight(stack))
  524. ret.x += imageShiftX;
  525. else
  526. ret.x -= imageShiftX;
  527. //shifting position for double - hex creatures
  528. if(stack->doubleWide())
  529. {
  530. if(stack->side == BattleSide::ATTACKER)
  531. {
  532. if(facingRight(stack))
  533. ret.x -= 44;
  534. }
  535. else
  536. {
  537. if(!facingRight(stack))
  538. ret.x += 44;
  539. }
  540. }
  541. }
  542. //returning
  543. return ret + owner.pos.topLeft();
  544. }