CBattleAnimations.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. /*
  2. * CBattleAnimations.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 "CBattleAnimations.h"
  12. #include <boost/math/constants/constants.hpp>
  13. #include "CBattleInterfaceClasses.h"
  14. #include "CBattleInterface.h"
  15. #include "CBattleProjectileController.h"
  16. #include "CBattleSiegeController.h"
  17. #include "CBattleFieldController.h"
  18. #include "CBattleEffectsController.h"
  19. #include "CBattleStacksController.h"
  20. #include "CCreatureAnimation.h"
  21. #include "../CGameInfo.h"
  22. #include "../CMusicHandler.h"
  23. #include "../CPlayerInterface.h"
  24. #include "../Graphics.h"
  25. #include "../gui/CAnimation.h"
  26. #include "../gui/CCursorHandler.h"
  27. #include "../gui/CGuiHandler.h"
  28. #include "../../CCallback.h"
  29. #include "../../lib/CStack.h"
  30. #include "../../lib/CTownHandler.h"
  31. #include "../../lib/mapObjects/CGTownInstance.h"
  32. CBattleAnimation::CBattleAnimation(CBattleInterface * _owner)
  33. : owner(_owner), ID(_owner->stacksController->animIDhelper++)
  34. {
  35. logAnim->trace("Animation #%d created", ID);
  36. }
  37. CBattleAnimation::~CBattleAnimation()
  38. {
  39. logAnim->trace("Animation #%d deleted", ID);
  40. }
  41. std::list<std::pair<CBattleAnimation *, bool>> & CBattleAnimation::pendingAnimations()
  42. {
  43. return owner->stacksController->pendingAnims;
  44. }
  45. std::shared_ptr<CCreatureAnimation> CBattleAnimation::stackAnimation(const CStack * stack)
  46. {
  47. return owner->stacksController->creAnims[stack->ID];
  48. }
  49. bool CBattleAnimation::stackFacingRight(const CStack * stack)
  50. {
  51. return owner->stacksController->creDir[stack->ID];
  52. }
  53. ui32 CBattleAnimation::maxAnimationID()
  54. {
  55. return owner->stacksController->animIDhelper;
  56. }
  57. void CBattleAnimation::setStackFacingRight(const CStack * stack, bool facingRight)
  58. {
  59. owner->stacksController->creDir[stack->ID] = facingRight;
  60. }
  61. void CBattleAnimation::endAnim()
  62. {
  63. logAnim->trace("Animation #%d ended, type is %s", ID, typeid(this).name());
  64. for(auto & elem : pendingAnimations())
  65. {
  66. if(elem.first == this)
  67. {
  68. elem.first = nullptr;
  69. }
  70. }
  71. }
  72. bool CBattleAnimation::isEarliest(bool perStackConcurrency)
  73. {
  74. int lowestMoveID = maxAnimationID() + 5;//FIXME: why 5?
  75. CBattleStackAnimation * thAnim = dynamic_cast<CBattleStackAnimation *>(this);
  76. CEffectAnimation * thSen = dynamic_cast<CEffectAnimation *>(this);
  77. for(auto & elem : pendingAnimations())
  78. {
  79. CBattleStackAnimation * stAnim = dynamic_cast<CBattleStackAnimation *>(elem.first);
  80. CEffectAnimation * sen = dynamic_cast<CEffectAnimation *>(elem.first);
  81. if(perStackConcurrency && stAnim && thAnim && stAnim->stack->ID != thAnim->stack->ID)
  82. continue;
  83. if(perStackConcurrency && sen && thSen && sen != thSen)
  84. continue;
  85. CReverseAnimation * revAnim = dynamic_cast<CReverseAnimation *>(stAnim);
  86. if(revAnim && thAnim && stAnim && stAnim->stack->ID == thAnim->stack->ID && revAnim->priority)
  87. return false;
  88. if(elem.first)
  89. vstd::amin(lowestMoveID, elem.first->ID);
  90. }
  91. return (ID == lowestMoveID) || (lowestMoveID == (maxAnimationID() + 5));
  92. }
  93. CBattleStackAnimation::CBattleStackAnimation(CBattleInterface * owner, const CStack * stack)
  94. : CBattleAnimation(owner),
  95. myAnim(stackAnimation(stack)),
  96. stack(stack)
  97. {
  98. assert(myAnim);
  99. }
  100. void CBattleStackAnimation::shiftColor(const ColorShifter * shifter)
  101. {
  102. assert(myAnim);
  103. myAnim->shiftColor(shifter);
  104. }
  105. void CAttackAnimation::nextFrame()
  106. {
  107. if(myAnim->getType() != group)
  108. {
  109. myAnim->setType(group);
  110. myAnim->onAnimationReset += std::bind(&CAttackAnimation::endAnim, this);
  111. }
  112. if(!soundPlayed)
  113. {
  114. if(shooting)
  115. CCS->soundh->playSound(battle_sound(getCreature(), shoot));
  116. else
  117. CCS->soundh->playSound(battle_sound(getCreature(), attack));
  118. soundPlayed = true;
  119. }
  120. CBattleAnimation::nextFrame();
  121. }
  122. void CAttackAnimation::endAnim()
  123. {
  124. myAnim->setType(CCreatureAnim::HOLDING);
  125. CBattleStackAnimation::endAnim();
  126. }
  127. bool CAttackAnimation::checkInitialConditions()
  128. {
  129. for(auto & elem : pendingAnimations())
  130. {
  131. CBattleStackAnimation * stAnim = dynamic_cast<CBattleStackAnimation *>(elem.first);
  132. CReverseAnimation * revAnim = dynamic_cast<CReverseAnimation *>(stAnim);
  133. if(revAnim && attackedStack) // enemy must be fully reversed
  134. {
  135. if (revAnim->stack->ID == attackedStack->ID)
  136. return false;
  137. }
  138. }
  139. return isEarliest(false);
  140. }
  141. const CCreature * CAttackAnimation::getCreature()
  142. {
  143. if (attackingStack->getCreature()->idNumber == CreatureID::ARROW_TOWERS)
  144. return owner->siegeController->getTurretCreature();
  145. else
  146. return attackingStack->getCreature();
  147. }
  148. CAttackAnimation::CAttackAnimation(CBattleInterface *_owner, const CStack *attacker, BattleHex _dest, const CStack *defender)
  149. : CBattleStackAnimation(_owner, attacker),
  150. shooting(false), group(CCreatureAnim::SHOOT_FRONT),
  151. soundPlayed(false),
  152. dest(_dest), attackedStack(defender), attackingStack(attacker)
  153. {
  154. assert(attackingStack && "attackingStack is nullptr in CBattleAttack::CBattleAttack !\n");
  155. attackingStackPosBeforeReturn = attackingStack->getPosition();
  156. }
  157. CDefenceAnimation::CDefenceAnimation(StackAttackedInfo _attackedInfo, CBattleInterface * _owner)
  158. : CBattleStackAnimation(_owner, _attackedInfo.defender),
  159. attacker(_attackedInfo.attacker), rangedAttack(_attackedInfo.indirectAttack),
  160. killed(_attackedInfo.killed), timeToWait(0)
  161. {
  162. logAnim->debug("Created defence anim for %s", _attackedInfo.defender->getName());
  163. }
  164. bool CDefenceAnimation::init()
  165. {
  166. ui32 lowestMoveID = maxAnimationID() + 5;
  167. for(auto & elem : pendingAnimations())
  168. {
  169. CDefenceAnimation * defAnim = dynamic_cast<CDefenceAnimation *>(elem.first);
  170. if(defAnim && defAnim->stack->ID != stack->ID)
  171. continue;
  172. CAttackAnimation * attAnim = dynamic_cast<CAttackAnimation *>(elem.first);
  173. if(attAnim && attAnim->stack->ID != stack->ID)
  174. continue;
  175. CEffectAnimation * sen = dynamic_cast<CEffectAnimation *>(elem.first);
  176. if (sen && attacker == nullptr)
  177. return false;
  178. if (sen)
  179. continue;
  180. CReverseAnimation * animAsRev = dynamic_cast<CReverseAnimation *>(elem.first);
  181. if(animAsRev)
  182. return false;
  183. if(elem.first)
  184. vstd::amin(lowestMoveID, elem.first->ID);
  185. }
  186. if(ID > lowestMoveID)
  187. return false;
  188. //reverse unit if necessary
  189. if(attacker && owner->getCurrentPlayerInterface()->cb->isToReverse(stack->getPosition(), attacker->getPosition(), stackFacingRight(stack), attacker->doubleWide(), stackFacingRight(attacker)))
  190. {
  191. owner->stacksController->addNewAnim(new CReverseAnimation(owner, stack, stack->getPosition(), true));
  192. return false;
  193. }
  194. //unit reversed
  195. if(rangedAttack && attacker != nullptr && owner->projectilesController->hasActiveProjectile(attacker)) //delay hit animation
  196. {
  197. return false;
  198. }
  199. // synchronize animation with attacker, unless defending or attacked by shooter:
  200. // wait for 1/2 of attack animation
  201. if (!rangedAttack && getMyAnimType() != CCreatureAnim::DEFENCE)
  202. {
  203. float frameLength = AnimationControls::getCreatureAnimationSpeed(
  204. stack->getCreature(), stackAnimation(stack).get(), getMyAnimType());
  205. timeToWait = myAnim->framesInGroup(getMyAnimType()) * frameLength / 2;
  206. myAnim->setType(CCreatureAnim::HOLDING);
  207. }
  208. else
  209. {
  210. timeToWait = 0;
  211. startAnimation();
  212. }
  213. return true; //initialized successfuly
  214. }
  215. std::string CDefenceAnimation::getMySound()
  216. {
  217. if(killed)
  218. return battle_sound(stack->getCreature(), killed);
  219. else if(stack->defendingAnim)
  220. return battle_sound(stack->getCreature(), defend);
  221. else
  222. return battle_sound(stack->getCreature(), wince);
  223. }
  224. CCreatureAnim::EAnimType CDefenceAnimation::getMyAnimType()
  225. {
  226. if(killed)
  227. {
  228. if(rangedAttack && myAnim->framesInGroup(CCreatureAnim::DEATH_RANGED) > 0)
  229. return CCreatureAnim::DEATH_RANGED;
  230. else
  231. return CCreatureAnim::DEATH;
  232. }
  233. if(stack->defendingAnim)
  234. return CCreatureAnim::DEFENCE;
  235. else
  236. return CCreatureAnim::HITTED;
  237. }
  238. void CDefenceAnimation::startAnimation()
  239. {
  240. CCS->soundh->playSound(getMySound());
  241. myAnim->setType(getMyAnimType());
  242. myAnim->onAnimationReset += std::bind(&CDefenceAnimation::endAnim, this);
  243. }
  244. void CDefenceAnimation::nextFrame()
  245. {
  246. if (timeToWait > 0)
  247. {
  248. timeToWait -= float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000;
  249. if (timeToWait <= 0)
  250. startAnimation();
  251. }
  252. CBattleAnimation::nextFrame();
  253. }
  254. void CDefenceAnimation::endAnim()
  255. {
  256. if(killed)
  257. {
  258. if(rangedAttack && myAnim->framesInGroup(CCreatureAnim::DEAD_RANGED) > 0)
  259. myAnim->setType(CCreatureAnim::DEAD_RANGED);
  260. else
  261. myAnim->setType(CCreatureAnim::DEAD);
  262. }
  263. else
  264. {
  265. myAnim->setType(CCreatureAnim::HOLDING);
  266. }
  267. CBattleAnimation::endAnim();
  268. delete this;
  269. }
  270. CDummyAnimation::CDummyAnimation(CBattleInterface * _owner, int howManyFrames)
  271. : CBattleAnimation(_owner), counter(0), howMany(howManyFrames)
  272. {
  273. logAnim->debug("Created dummy animation for %d frames", howManyFrames);
  274. }
  275. bool CDummyAnimation::init()
  276. {
  277. return true;
  278. }
  279. void CDummyAnimation::nextFrame()
  280. {
  281. counter++;
  282. if(counter > howMany)
  283. endAnim();
  284. }
  285. void CDummyAnimation::endAnim()
  286. {
  287. CBattleAnimation::endAnim();
  288. delete this;
  289. }
  290. bool CMeleeAttackAnimation::init()
  291. {
  292. if(!CAttackAnimation::checkInitialConditions())
  293. return false;
  294. if(!attackingStack || myAnim->isDead())
  295. {
  296. endAnim();
  297. return false;
  298. }
  299. bool toReverse = owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStackPosBeforeReturn, attackedStack->getPosition(), stackFacingRight(stack), attackedStack->doubleWide(), stackFacingRight(attackedStack));
  300. if(toReverse)
  301. {
  302. owner->stacksController->addNewAnim(new CReverseAnimation(owner, stack, attackingStackPosBeforeReturn, true));
  303. return false;
  304. }
  305. // opponent must face attacker ( = different directions) before he can be attacked
  306. if(attackingStack && attackedStack &&
  307. stackFacingRight(attackingStack) == stackFacingRight(attackedStack))
  308. return false;
  309. //reversed
  310. shooting = false;
  311. static const CCreatureAnim::EAnimType mutPosToGroup[] =
  312. {
  313. CCreatureAnim::ATTACK_UP,
  314. CCreatureAnim::ATTACK_UP,
  315. CCreatureAnim::ATTACK_FRONT,
  316. CCreatureAnim::ATTACK_DOWN,
  317. CCreatureAnim::ATTACK_DOWN,
  318. CCreatureAnim::ATTACK_FRONT
  319. };
  320. static const CCreatureAnim::EAnimType mutPosToGroup2H[] =
  321. {
  322. CCreatureAnim::VCMI_2HEX_UP,
  323. CCreatureAnim::VCMI_2HEX_UP,
  324. CCreatureAnim::VCMI_2HEX_FRONT,
  325. CCreatureAnim::VCMI_2HEX_DOWN,
  326. CCreatureAnim::VCMI_2HEX_DOWN,
  327. CCreatureAnim::VCMI_2HEX_FRONT
  328. };
  329. int revShiftattacker = (attackingStack->side == BattleSide::ATTACKER ? -1 : 1);
  330. int mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, dest);
  331. if(mutPos == -1 && attackingStack->doubleWide())
  332. {
  333. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, attackedStack->getPosition());
  334. }
  335. if (mutPos == -1 && attackedStack->doubleWide())
  336. {
  337. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, attackedStack->occupiedHex());
  338. }
  339. if (mutPos == -1 && attackedStack->doubleWide() && attackingStack->doubleWide())
  340. {
  341. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, attackedStack->occupiedHex());
  342. }
  343. switch(mutPos) //attack direction
  344. {
  345. case 0:
  346. case 1:
  347. case 2:
  348. case 3:
  349. case 4:
  350. case 5:
  351. group = mutPosToGroup[mutPos];
  352. if(attackingStack->hasBonusOfType(Bonus::TWO_HEX_ATTACK_BREATH))
  353. {
  354. CCreatureAnim::EAnimType group2H = mutPosToGroup2H[mutPos];
  355. if(myAnim->framesInGroup(group2H)>0)
  356. group = group2H;
  357. }
  358. break;
  359. default:
  360. logGlobal->error("Critical Error! Wrong dest in stackAttacking! dest: %d; attacking stack pos: %d; mutual pos: %d", dest.hex, attackingStackPosBeforeReturn, mutPos);
  361. group = CCreatureAnim::ATTACK_FRONT;
  362. break;
  363. }
  364. return true;
  365. }
  366. CMeleeAttackAnimation::CMeleeAttackAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked)
  367. : CAttackAnimation(_owner, attacker, _dest, _attacked)
  368. {
  369. logAnim->debug("Created melee attack anim for %s", attacker->getName());
  370. }
  371. void CMeleeAttackAnimation::endAnim()
  372. {
  373. CAttackAnimation::endAnim();
  374. delete this;
  375. }
  376. bool CMovementAnimation::init()
  377. {
  378. if( !isEarliest(false) )
  379. return false;
  380. if(!stack || myAnim->isDead())
  381. {
  382. endAnim();
  383. return false;
  384. }
  385. if(stackAnimation(stack)->framesInGroup(CCreatureAnim::MOVING) == 0 ||
  386. stack->hasBonus(Selector::typeSubtype(Bonus::FLYING, 1)))
  387. {
  388. //no movement or teleport, end immediately
  389. endAnim();
  390. return false;
  391. }
  392. //reverse unit if necessary
  393. if(owner->stacksController->shouldRotate(stack, oldPos, nextHex))
  394. {
  395. // it seems that H3 does NOT plays full rotation animation here in most situations
  396. // Logical since it takes quite a lot of time
  397. if (curentMoveIndex == 0) // full rotation only for moving towards first tile.
  398. {
  399. owner->stacksController->addNewAnim(new CReverseAnimation(owner, stack, oldPos, true));
  400. return false;
  401. }
  402. else
  403. {
  404. rotateStack(oldPos);
  405. }
  406. }
  407. if(myAnim->getType() != CCreatureAnim::MOVING)
  408. {
  409. myAnim->setType(CCreatureAnim::MOVING);
  410. }
  411. if (owner->moveSoundHander == -1)
  412. {
  413. owner->moveSoundHander = CCS->soundh->playSound(battle_sound(stack->getCreature(), move), -1);
  414. }
  415. Point begPosition = owner->stacksController->getStackPositionAtHex(oldPos, stack);
  416. Point endPosition = owner->stacksController->getStackPositionAtHex(nextHex, stack);
  417. timeToMove = AnimationControls::getMovementDuration(stack->getCreature());
  418. begX = begPosition.x;
  419. begY = begPosition.y;
  420. progress = 0;
  421. distanceX = endPosition.x - begPosition.x;
  422. distanceY = endPosition.y - begPosition.y;
  423. if (stack->hasBonus(Selector::type()(Bonus::FLYING)))
  424. {
  425. float distance = static_cast<float>(sqrt(distanceX * distanceX + distanceY * distanceY));
  426. timeToMove *= AnimationControls::getFlightDistance(stack->getCreature()) / distance;
  427. }
  428. return true;
  429. }
  430. void CMovementAnimation::nextFrame()
  431. {
  432. progress += float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000 * timeToMove;
  433. //moving instructions
  434. myAnim->pos.x = static_cast<Sint16>(begX + distanceX * progress );
  435. myAnim->pos.y = static_cast<Sint16>(begY + distanceY * progress );
  436. CBattleAnimation::nextFrame();
  437. if(progress >= 1.0)
  438. {
  439. // Sets the position of the creature animation sprites
  440. Point coords = owner->stacksController->getStackPositionAtHex(nextHex, stack);
  441. myAnim->pos = coords;
  442. // true if creature haven't reached the final destination hex
  443. if ((curentMoveIndex + 1) < destTiles.size())
  444. {
  445. // update the next hex field which has to be reached by the stack
  446. curentMoveIndex++;
  447. oldPos = nextHex;
  448. nextHex = destTiles[curentMoveIndex];
  449. // re-init animation
  450. for(auto & elem : pendingAnimations())
  451. {
  452. if (elem.first == this)
  453. {
  454. elem.second = false;
  455. break;
  456. }
  457. }
  458. }
  459. else
  460. endAnim();
  461. }
  462. }
  463. void CMovementAnimation::endAnim()
  464. {
  465. assert(stack);
  466. myAnim->pos = owner->stacksController->getStackPositionAtHex(nextHex, stack);
  467. CBattleAnimation::endAnim();
  468. owner->stacksController->addNewAnim(new CMovementEndAnimation(owner, stack, nextHex));
  469. if(owner->moveSoundHander != -1)
  470. {
  471. CCS->soundh->stopSound(owner->moveSoundHander);
  472. owner->moveSoundHander = -1;
  473. }
  474. delete this;
  475. }
  476. CMovementAnimation::CMovementAnimation(CBattleInterface *_owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance)
  477. : CBattleStackAnimation(_owner, _stack),
  478. destTiles(_destTiles),
  479. curentMoveIndex(0),
  480. oldPos(stack->getPosition()),
  481. begX(0), begY(0),
  482. distanceX(0), distanceY(0),
  483. timeToMove(0.0),
  484. progress(0.0),
  485. nextHex(destTiles.front())
  486. {
  487. logAnim->debug("Created movement anim for %s", stack->getName());
  488. }
  489. CMovementEndAnimation::CMovementEndAnimation(CBattleInterface * _owner, const CStack * _stack, BattleHex destTile)
  490. : CBattleStackAnimation(_owner, _stack), destinationTile(destTile)
  491. {
  492. logAnim->debug("Created movement end anim for %s", stack->getName());
  493. }
  494. bool CMovementEndAnimation::init()
  495. {
  496. if( !isEarliest(true) )
  497. return false;
  498. if(!stack || myAnim->framesInGroup(CCreatureAnim::MOVE_END) == 0 ||
  499. myAnim->isDead())
  500. {
  501. endAnim();
  502. return false;
  503. }
  504. CCS->soundh->playSound(battle_sound(stack->getCreature(), endMoving));
  505. myAnim->setType(CCreatureAnim::MOVE_END);
  506. myAnim->onAnimationReset += std::bind(&CMovementEndAnimation::endAnim, this);
  507. return true;
  508. }
  509. void CMovementEndAnimation::endAnim()
  510. {
  511. CBattleAnimation::endAnim();
  512. if(myAnim->getType() != CCreatureAnim::DEAD)
  513. myAnim->setType(CCreatureAnim::HOLDING); //resetting to default
  514. CCS->curh->show();
  515. delete this;
  516. }
  517. CMovementStartAnimation::CMovementStartAnimation(CBattleInterface * _owner, const CStack * _stack)
  518. : CBattleStackAnimation(_owner, _stack)
  519. {
  520. logAnim->debug("Created movement start anim for %s", stack->getName());
  521. }
  522. bool CMovementStartAnimation::init()
  523. {
  524. if( !isEarliest(false) )
  525. return false;
  526. if(!stack || myAnim->isDead())
  527. {
  528. CMovementStartAnimation::endAnim();
  529. return false;
  530. }
  531. CCS->soundh->playSound(battle_sound(stack->getCreature(), startMoving));
  532. myAnim->setType(CCreatureAnim::MOVE_START);
  533. myAnim->onAnimationReset += std::bind(&CMovementStartAnimation::endAnim, this);
  534. return true;
  535. }
  536. void CMovementStartAnimation::endAnim()
  537. {
  538. CBattleAnimation::endAnim();
  539. delete this;
  540. }
  541. CReverseAnimation::CReverseAnimation(CBattleInterface * _owner, const CStack * stack, BattleHex dest, bool _priority)
  542. : CBattleStackAnimation(_owner, stack), hex(dest), priority(_priority)
  543. {
  544. logAnim->debug("Created reverse anim for %s", stack->getName());
  545. }
  546. bool CReverseAnimation::init()
  547. {
  548. if(myAnim == nullptr || myAnim->isDead())
  549. {
  550. endAnim();
  551. return false; //there is no such creature
  552. }
  553. if(!priority && !isEarliest(false))
  554. return false;
  555. if(myAnim->framesInGroup(CCreatureAnim::TURN_L))
  556. {
  557. myAnim->setType(CCreatureAnim::TURN_L);
  558. myAnim->onAnimationReset += std::bind(&CReverseAnimation::setupSecondPart, this);
  559. }
  560. else
  561. {
  562. setupSecondPart();
  563. }
  564. return true;
  565. }
  566. void CReverseAnimation::endAnim()
  567. {
  568. CBattleAnimation::endAnim();
  569. if( stack->alive() )//don't do that if stack is dead
  570. myAnim->setType(CCreatureAnim::HOLDING);
  571. delete this;
  572. }
  573. void CBattleStackAnimation::rotateStack(BattleHex hex)
  574. {
  575. setStackFacingRight(stack, !stackFacingRight(stack));
  576. stackAnimation(stack)->pos = owner->stacksController->getStackPositionAtHex(hex, stack);
  577. }
  578. void CReverseAnimation::setupSecondPart()
  579. {
  580. if(!stack)
  581. {
  582. endAnim();
  583. return;
  584. }
  585. rotateStack(hex);
  586. if(myAnim->framesInGroup(CCreatureAnim::TURN_R))
  587. {
  588. myAnim->setType(CCreatureAnim::TURN_R);
  589. myAnim->onAnimationReset += std::bind(&CReverseAnimation::endAnim, this);
  590. }
  591. else
  592. endAnim();
  593. }
  594. CRangedAttackAnimation::CRangedAttackAnimation(CBattleInterface * owner_, const CStack * attacker, BattleHex dest_, const CStack * defender)
  595. : CAttackAnimation(owner_, attacker, dest_, defender)
  596. {
  597. }
  598. CShootingAnimation::CShootingAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, bool _catapult, int _catapultDmg)
  599. : CRangedAttackAnimation(_owner, attacker, _dest, _attacked),
  600. catapultDamage(_catapultDmg),
  601. projectileEmitted(false),
  602. explosionEmitted(false)
  603. {
  604. logAnim->debug("Created shooting anim for %s", stack->getName());
  605. }
  606. bool CShootingAnimation::init()
  607. {
  608. if( !CAttackAnimation::checkInitialConditions() )
  609. return false;
  610. if(!attackingStack || myAnim->isDead())
  611. {
  612. //FIXME: how is this possible?
  613. logAnim->warn("Shooting animation has not started yet but attacker is dead! Aborting...");
  614. endAnim();
  615. return false;
  616. }
  617. //reverse unit if necessary
  618. if (attackingStack && attackedStack && owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStack->getPosition(), attackedStack->getPosition(), stackFacingRight(attackingStack), attackingStack->doubleWide(), stackFacingRight(attackedStack)))
  619. {
  620. owner->stacksController->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->getPosition(), true));
  621. return false;
  622. }
  623. //FIXME: this cause freeze
  624. // opponent must face attacker ( = different directions) before he can be attacked
  625. //if (attackingStack && attackedStack && owner->creDir[attackingStack->ID] == owner->creDir[attackedStack->ID])
  626. // return false;
  627. setAnimationGroup();
  628. initializeProjectile();
  629. shooting = true;
  630. return true;
  631. }
  632. void CShootingAnimation::setAnimationGroup()
  633. {
  634. Point shooterPos = stackAnimation(attackingStack)->pos.topLeft();
  635. Point shotTarget = owner->stacksController->getStackPositionAtHex(dest, attackedStack) + Point(225, 225);
  636. //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  637. static const double straightAngle = 0.2;
  638. double projectileAngle = -atan2(shotTarget.y - shooterPos.y, std::abs(shotTarget.x - shooterPos.x));
  639. // Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
  640. if (projectileAngle > straightAngle)
  641. group = CCreatureAnim::SHOOT_UP;
  642. else if (projectileAngle < -straightAngle)
  643. group = CCreatureAnim::SHOOT_DOWN;
  644. else
  645. group = CCreatureAnim::SHOOT_FRONT;
  646. }
  647. void CShootingAnimation::initializeProjectile()
  648. {
  649. const CCreature *shooterInfo = getCreature();
  650. Point shotTarget = owner->stacksController->getStackPositionAtHex(dest, attackedStack) + Point(225, 225);
  651. Point shotOrigin = stackAnimation(attackingStack)->pos.topLeft() + Point(222, 265);
  652. int multiplier = stackFacingRight(attackingStack) ? 1 : -1;
  653. if (group == CCreatureAnim::SHOOT_UP)
  654. {
  655. shotOrigin.x += ( -25 + shooterInfo->animation.upperRightMissleOffsetX ) * multiplier;
  656. shotOrigin.y += shooterInfo->animation.upperRightMissleOffsetY;
  657. }
  658. else if (group == CCreatureAnim::SHOOT_DOWN)
  659. {
  660. shotOrigin.x += ( -25 + shooterInfo->animation.lowerRightMissleOffsetX ) * multiplier;
  661. shotOrigin.y += shooterInfo->animation.lowerRightMissleOffsetY;
  662. }
  663. else if (group == CCreatureAnim::SHOOT_FRONT)
  664. {
  665. shotOrigin.x += ( -25 + shooterInfo->animation.rightMissleOffsetX ) * multiplier;
  666. shotOrigin.y += shooterInfo->animation.rightMissleOffsetY;
  667. }
  668. else
  669. {
  670. assert(0);
  671. }
  672. owner->projectilesController->createProjectile(attackingStack, attackedStack, shotOrigin, shotTarget);
  673. }
  674. void CShootingAnimation::emitProjectile()
  675. {
  676. owner->projectilesController->emitStackProjectile(attackingStack);
  677. projectileEmitted = true;
  678. }
  679. void CShootingAnimation::nextFrame()
  680. {
  681. for(auto & it : pendingAnimations())
  682. {
  683. CMovementStartAnimation * anim = dynamic_cast<CMovementStartAnimation *>(it.first);
  684. CReverseAnimation * anim2 = dynamic_cast<CReverseAnimation *>(it.first);
  685. if( (anim && anim->stack->ID == stack->ID) || (anim2 && anim2->stack->ID == stack->ID && anim2->priority ) )
  686. return;
  687. }
  688. // animation should be paused if there is an active projectile
  689. if (projectileEmitted)
  690. {
  691. if (owner->projectilesController->hasActiveProjectile(attackingStack))
  692. {
  693. stackAnimation(attackingStack)->pause();
  694. return;
  695. }
  696. else
  697. {
  698. stackAnimation(attackingStack)->play();
  699. emitExplosion();
  700. }
  701. }
  702. CAttackAnimation::nextFrame();
  703. if (!projectileEmitted)
  704. {
  705. const CCreature *shooterInfo = getCreature();
  706. assert(stackAnimation(attackingStack)->isShooting());
  707. // emit projectile once animation playback reached "climax" frame
  708. if ( stackAnimation(attackingStack)->getCurrentFrame() >= shooterInfo->animation.attackClimaxFrame )
  709. {
  710. emitProjectile();
  711. return;
  712. }
  713. }
  714. }
  715. void CShootingAnimation::emitExplosion()
  716. {
  717. if (attackedStack)
  718. return;
  719. if (explosionEmitted)
  720. return;
  721. explosionEmitted = true;
  722. Point shotTarget = owner->stacksController->getStackPositionAtHex(dest, attackedStack) + Point(225, 225) - Point(126, 105);
  723. owner->stacksController->addNewAnim( new CEffectAnimation(owner, catapultDamage ? "SGEXPL.DEF" : "CSGRCK.DEF", shotTarget.x, shotTarget.y));
  724. if(catapultDamage > 0)
  725. {
  726. CCS->soundh->playSound("WALLHIT");
  727. }
  728. else
  729. {
  730. CCS->soundh->playSound("WALLMISS");
  731. }
  732. }
  733. void CShootingAnimation::endAnim()
  734. {
  735. assert(!owner->projectilesController->hasActiveProjectile(attackingStack));
  736. assert(projectileEmitted);
  737. // FIXME: is this possible? Animation is over but we're yet to fire projectile?
  738. if (!projectileEmitted)
  739. {
  740. logAnim->warn("Shooting animation has finished but projectile was not emitted! Emitting it now...");
  741. emitProjectile();
  742. }
  743. CAttackAnimation::endAnim();
  744. delete this;
  745. }
  746. CCastAnimation::CCastAnimation(CBattleInterface * owner_, const CStack * attacker, BattleHex dest_, const CStack * defender)
  747. : CRangedAttackAnimation(owner_, attacker, dest_, defender)
  748. {
  749. if(!dest_.isValid() && defender)
  750. dest = defender->getPosition();
  751. }
  752. bool CCastAnimation::init()
  753. {
  754. if(!CAttackAnimation::checkInitialConditions())
  755. return false;
  756. if(!attackingStack || myAnim->isDead())
  757. {
  758. endAnim();
  759. return false;
  760. }
  761. //reverse unit if necessary
  762. if(attackedStack)
  763. {
  764. if(owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStack->getPosition(), attackedStack->getPosition(), stackFacingRight(attackingStack), attackingStack->doubleWide(), stackFacingRight(attackedStack)))
  765. {
  766. owner->stacksController->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->getPosition(), true));
  767. return false;
  768. }
  769. }
  770. else
  771. {
  772. if(dest.isValid() && owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStack->getPosition(), dest, stackFacingRight(attackingStack), false, false))
  773. {
  774. owner->stacksController->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->getPosition(), true));
  775. return false;
  776. }
  777. }
  778. //TODO: display spell projectile here
  779. static const double straightAngle = 0.2;
  780. Point fromPos;
  781. Point destPos;
  782. // NOTE: two lines below return different positions (very notable with 2-hex creatures). Obtaining via creanims seems to be more precise
  783. fromPos = stackAnimation(attackingStack)->pos.topLeft();
  784. //xycoord = owner->stacksController->getStackPositionAtHex(shooter->getPosition(), shooter);
  785. destPos = owner->stacksController->getStackPositionAtHex(dest, attackedStack);
  786. double projectileAngle = atan2(fabs((double)destPos.y - fromPos.y), fabs((double)destPos.x - fromPos.x));
  787. if(attackingStack->getPosition() < dest)
  788. projectileAngle = -projectileAngle;
  789. if(projectileAngle > straightAngle)
  790. group = CCreatureAnim::VCMI_CAST_UP;
  791. else if(projectileAngle < -straightAngle)
  792. group = CCreatureAnim::VCMI_CAST_DOWN;
  793. else
  794. group = CCreatureAnim::VCMI_CAST_FRONT;
  795. //fall back to H3 cast/2hex
  796. //even if creature have 2hex attack instead of cast it is ok since we fall back to attack anyway
  797. if(myAnim->framesInGroup(group) == 0)
  798. {
  799. if(projectileAngle > straightAngle)
  800. group = CCreatureAnim::CAST_UP;
  801. else if(projectileAngle < -straightAngle)
  802. group = CCreatureAnim::CAST_DOWN;
  803. else
  804. group = CCreatureAnim::CAST_FRONT;
  805. }
  806. //fall back to ranged attack
  807. if(myAnim->framesInGroup(group) == 0)
  808. {
  809. if(projectileAngle > straightAngle)
  810. group = CCreatureAnim::SHOOT_UP;
  811. else if(projectileAngle < -straightAngle)
  812. group = CCreatureAnim::SHOOT_DOWN;
  813. else
  814. group = CCreatureAnim::SHOOT_FRONT;
  815. }
  816. //fall back to normal attack
  817. if(myAnim->framesInGroup(group) == 0)
  818. {
  819. if(projectileAngle > straightAngle)
  820. group = CCreatureAnim::ATTACK_UP;
  821. else if(projectileAngle < -straightAngle)
  822. group = CCreatureAnim::ATTACK_DOWN;
  823. else
  824. group = CCreatureAnim::ATTACK_FRONT;
  825. }
  826. return true;
  827. }
  828. void CCastAnimation::nextFrame()
  829. {
  830. for(auto & it : pendingAnimations())
  831. {
  832. CReverseAnimation * anim = dynamic_cast<CReverseAnimation *>(it.first);
  833. if(anim && anim->stack->ID == stack->ID && anim->priority)
  834. return;
  835. }
  836. if(myAnim->getType() != group)
  837. {
  838. myAnim->setType(group);
  839. myAnim->onAnimationReset += std::bind(&CAttackAnimation::endAnim, this);
  840. }
  841. CBattleAnimation::nextFrame();
  842. }
  843. void CCastAnimation::endAnim()
  844. {
  845. CAttackAnimation::endAnim();
  846. delete this;
  847. }
  848. CEffectAnimation::CEffectAnimation(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx, int _dy, bool _Vflip, bool _alignToBottom)
  849. : CBattleAnimation(_owner),
  850. destTile(BattleHex::INVALID),
  851. x(_x),
  852. y(_y),
  853. dx(_dx),
  854. dy(_dy),
  855. Vflip(_Vflip),
  856. alignToBottom(_alignToBottom)
  857. {
  858. logAnim->debug("Created effect animation %s", _customAnim);
  859. customAnim = std::make_shared<CAnimation>(_customAnim);
  860. }
  861. CEffectAnimation::CEffectAnimation(CBattleInterface * _owner, std::shared_ptr<CAnimation> _customAnim, int _x, int _y, int _dx, int _dy)
  862. : CBattleAnimation(_owner),
  863. destTile(BattleHex::INVALID),
  864. customAnim(_customAnim),
  865. x(_x),
  866. y(_y),
  867. dx(_dx),
  868. dy(_dy),
  869. Vflip(false),
  870. alignToBottom(false)
  871. {
  872. logAnim->debug("Created custom effect animation");
  873. }
  874. CEffectAnimation::CEffectAnimation(CBattleInterface * _owner, std::string _customAnim, BattleHex _destTile, bool _Vflip, bool _alignToBottom)
  875. : CBattleAnimation(_owner),
  876. destTile(_destTile),
  877. x(-1),
  878. y(-1),
  879. dx(0),
  880. dy(0),
  881. Vflip(_Vflip),
  882. alignToBottom(_alignToBottom)
  883. {
  884. logAnim->debug("Created effect animation %s", _customAnim);
  885. customAnim = std::make_shared<CAnimation>(_customAnim);
  886. }
  887. bool CEffectAnimation::init()
  888. {
  889. if(!isEarliest(true))
  890. return false;
  891. const bool areaEffect = (!destTile.isValid() && x == -1 && y == -1);
  892. std::shared_ptr<CAnimation> animation = customAnim;
  893. animation->preload();
  894. if(Vflip)
  895. animation->verticalFlip();
  896. auto first = animation->getImage(0, 0, true);
  897. if(!first)
  898. {
  899. endAnim();
  900. return false;
  901. }
  902. if(areaEffect) //f.e. armageddon
  903. {
  904. for(int i=0; i * first->width() < owner->pos.w ; ++i)
  905. {
  906. for(int j=0; j * first->height() < owner->pos.h ; ++j)
  907. {
  908. BattleEffect be;
  909. be.effectID = ID;
  910. be.animation = animation;
  911. be.currentFrame = 0;
  912. be.x = i * first->width() + owner->pos.x;
  913. be.y = j * first->height() + owner->pos.y;
  914. be.position = BattleHex::INVALID;
  915. owner->effectsController->battleEffects.push_back(be);
  916. }
  917. }
  918. }
  919. else // Effects targeted at a specific creature/hex.
  920. {
  921. const CStack * destStack = owner->getCurrentPlayerInterface()->cb->battleGetStackByPos(destTile, false);
  922. BattleEffect be;
  923. be.effectID = ID;
  924. be.animation = animation;
  925. be.currentFrame = 0;
  926. //todo: lightning anim frame count override
  927. // if(effect == 1)
  928. // be.maxFrame = 3;
  929. be.x = x;
  930. be.y = y;
  931. if(destTile.isValid())
  932. {
  933. Rect tilePos = owner->fieldController->hexPosition(destTile);
  934. if(x == -1)
  935. be.x = tilePos.x + tilePos.w/2 - first->width()/2;
  936. if(y == -1)
  937. {
  938. if(alignToBottom)
  939. be.y = tilePos.y + tilePos.h - first->height();
  940. else
  941. be.y = tilePos.y - first->height()/2;
  942. }
  943. // Correction for 2-hex creatures.
  944. if(destStack != nullptr && destStack->doubleWide())
  945. be.x += (destStack->side == BattleSide::ATTACKER ? -1 : 1)*tilePos.w/2;
  946. }
  947. assert(be.x != -1 && be.y != -1);
  948. //Indicate if effect should be drawn on top of everything or just on top of the hex
  949. be.position = destTile;
  950. owner->effectsController->battleEffects.push_back(be);
  951. }
  952. return true;
  953. }
  954. void CEffectAnimation::nextFrame()
  955. {
  956. //notice: there may be more than one effect in owner->battleEffects correcponding to this animation (ie. armageddon)
  957. for(auto & elem : owner->effectsController->battleEffects)
  958. {
  959. if(elem.effectID == ID)
  960. {
  961. elem.currentFrame += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000;
  962. if(elem.currentFrame >= elem.animation->size())
  963. {
  964. endAnim();
  965. break;
  966. }
  967. else
  968. {
  969. elem.x += dx;
  970. elem.y += dy;
  971. }
  972. }
  973. }
  974. }
  975. void CEffectAnimation::endAnim()
  976. {
  977. CBattleAnimation::endAnim();
  978. auto & effects = owner->effectsController->battleEffects;
  979. for ( auto it = effects.begin(); it != effects.end(); )
  980. {
  981. if (it->effectID == ID)
  982. it = effects.erase(it);
  983. else
  984. it++;
  985. }
  986. delete this;
  987. }