CBattleAnimations.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. {
  603. logAnim->debug("Created shooting anim for %s", stack->getName());
  604. }
  605. bool CShootingAnimation::init()
  606. {
  607. if( !CAttackAnimation::checkInitialConditions() )
  608. return false;
  609. if(!attackingStack || myAnim->isDead())
  610. {
  611. //FIXME: how is this possible?
  612. logAnim->warn("Shooting animation has not started yet but attacker is dead! Aborting...");
  613. endAnim();
  614. return false;
  615. }
  616. //reverse unit if necessary
  617. if (attackingStack && attackedStack && owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStack->getPosition(), attackedStack->getPosition(), stackFacingRight(attackingStack), attackingStack->doubleWide(), stackFacingRight(attackedStack)))
  618. {
  619. owner->stacksController->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->getPosition(), true));
  620. return false;
  621. }
  622. //FIXME: this cause freeze
  623. // opponent must face attacker ( = different directions) before he can be attacked
  624. //if (attackingStack && attackedStack && owner->creDir[attackingStack->ID] == owner->creDir[attackedStack->ID])
  625. // return false;
  626. setAnimationGroup();
  627. initializeProjectile();
  628. shooting = true;
  629. return true;
  630. }
  631. void CShootingAnimation::setAnimationGroup()
  632. {
  633. Point shooterPos = stackAnimation(attackingStack)->pos.topLeft();
  634. Point shotTarget = owner->stacksController->getStackPositionAtHex(dest, attackedStack) + Point(225, 225);
  635. //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  636. static const double straightAngle = 0.2;
  637. double projectileAngle = -atan2(shotTarget.y - shooterPos.y, std::abs(shotTarget.x - shooterPos.x));
  638. // Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
  639. if (projectileAngle > straightAngle)
  640. group = CCreatureAnim::SHOOT_UP;
  641. else if (projectileAngle < -straightAngle)
  642. group = CCreatureAnim::SHOOT_DOWN;
  643. else
  644. group = CCreatureAnim::SHOOT_FRONT;
  645. }
  646. void CShootingAnimation::initializeProjectile()
  647. {
  648. const CCreature *shooterInfo = getCreature();
  649. Point shotTarget = owner->stacksController->getStackPositionAtHex(dest, attackedStack) + Point(225, 225);
  650. Point shotOrigin = stackAnimation(attackingStack)->pos.topLeft() + Point(222, 265);
  651. int multiplier = stackFacingRight(attackingStack) ? 1 : -1;
  652. if (group == CCreatureAnim::SHOOT_UP)
  653. {
  654. shotOrigin.x += ( -25 + shooterInfo->animation.upperRightMissleOffsetX ) * multiplier;
  655. shotOrigin.y += shooterInfo->animation.upperRightMissleOffsetY;
  656. }
  657. else if (group == CCreatureAnim::SHOOT_DOWN)
  658. {
  659. shotOrigin.x += ( -25 + shooterInfo->animation.lowerRightMissleOffsetX ) * multiplier;
  660. shotOrigin.y += shooterInfo->animation.lowerRightMissleOffsetY;
  661. }
  662. else if (group == CCreatureAnim::SHOOT_FRONT)
  663. {
  664. shotOrigin.x += ( -25 + shooterInfo->animation.rightMissleOffsetX ) * multiplier;
  665. shotOrigin.y += shooterInfo->animation.rightMissleOffsetY;
  666. }
  667. else
  668. {
  669. assert(0);
  670. }
  671. owner->projectilesController->createProjectile(attackingStack, attackedStack, shotOrigin, shotTarget);
  672. }
  673. void CShootingAnimation::emitProjectile()
  674. {
  675. owner->projectilesController->emitStackProjectile(attackingStack);
  676. projectileEmitted = true;
  677. }
  678. void CShootingAnimation::nextFrame()
  679. {
  680. for(auto & it : pendingAnimations())
  681. {
  682. CMovementStartAnimation * anim = dynamic_cast<CMovementStartAnimation *>(it.first);
  683. CReverseAnimation * anim2 = dynamic_cast<CReverseAnimation *>(it.first);
  684. if( (anim && anim->stack->ID == stack->ID) || (anim2 && anim2->stack->ID == stack->ID && anim2->priority ) )
  685. return;
  686. }
  687. // animation should be paused if there is an active projectile
  688. if (projectileEmitted)
  689. {
  690. if (owner->projectilesController->hasActiveProjectile(attackingStack))
  691. {
  692. stackAnimation(attackingStack)->pause();
  693. return;
  694. }
  695. else
  696. stackAnimation(attackingStack)->play();
  697. }
  698. CAttackAnimation::nextFrame();
  699. if (!projectileEmitted)
  700. {
  701. const CCreature *shooterInfo = getCreature();
  702. assert(stackAnimation(attackingStack)->isShooting());
  703. // emit projectile once animation playback reached "climax" frame
  704. if ( stackAnimation(attackingStack)->getCurrentFrame() >= shooterInfo->animation.attackClimaxFrame )
  705. {
  706. emitProjectile();
  707. return;
  708. }
  709. }
  710. }
  711. void CShootingAnimation::endAnim()
  712. {
  713. assert(!owner->projectilesController->hasActiveProjectile(attackingStack));
  714. assert(projectileEmitted);
  715. // FIXME: is this possible? Animation is over but we're yet to fire projectile?
  716. if (!projectileEmitted)
  717. {
  718. logAnim->warn("Shooting animation has finished but projectile was not emitted! Emitting it now...");
  719. emitProjectile();
  720. }
  721. // play wall hit/miss sound for catapult attack
  722. if(!attackedStack)
  723. {
  724. if(catapultDamage > 0)
  725. {
  726. CCS->soundh->playSound("WALLHIT");
  727. }
  728. else
  729. {
  730. CCS->soundh->playSound("WALLMISS");
  731. }
  732. }
  733. CAttackAnimation::endAnim();
  734. delete this;
  735. }
  736. CCastAnimation::CCastAnimation(CBattleInterface * owner_, const CStack * attacker, BattleHex dest_, const CStack * defender)
  737. : CRangedAttackAnimation(owner_, attacker, dest_, defender)
  738. {
  739. if(!dest_.isValid() && defender)
  740. dest = defender->getPosition();
  741. }
  742. bool CCastAnimation::init()
  743. {
  744. if(!CAttackAnimation::checkInitialConditions())
  745. return false;
  746. if(!attackingStack || myAnim->isDead())
  747. {
  748. endAnim();
  749. return false;
  750. }
  751. //reverse unit if necessary
  752. if(attackedStack)
  753. {
  754. if(owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStack->getPosition(), attackedStack->getPosition(), stackFacingRight(attackingStack), attackingStack->doubleWide(), stackFacingRight(attackedStack)))
  755. {
  756. owner->stacksController->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->getPosition(), true));
  757. return false;
  758. }
  759. }
  760. else
  761. {
  762. if(dest.isValid() && owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStack->getPosition(), dest, stackFacingRight(attackingStack), false, false))
  763. {
  764. owner->stacksController->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->getPosition(), true));
  765. return false;
  766. }
  767. }
  768. //TODO: display spell projectile here
  769. static const double straightAngle = 0.2;
  770. Point fromPos;
  771. Point destPos;
  772. // NOTE: two lines below return different positions (very notable with 2-hex creatures). Obtaining via creanims seems to be more precise
  773. fromPos = stackAnimation(attackingStack)->pos.topLeft();
  774. //xycoord = owner->stacksController->getStackPositionAtHex(shooter->getPosition(), shooter);
  775. destPos = owner->stacksController->getStackPositionAtHex(dest, attackedStack);
  776. double projectileAngle = atan2(fabs((double)destPos.y - fromPos.y), fabs((double)destPos.x - fromPos.x));
  777. if(attackingStack->getPosition() < dest)
  778. projectileAngle = -projectileAngle;
  779. if(projectileAngle > straightAngle)
  780. group = CCreatureAnim::VCMI_CAST_UP;
  781. else if(projectileAngle < -straightAngle)
  782. group = CCreatureAnim::VCMI_CAST_DOWN;
  783. else
  784. group = CCreatureAnim::VCMI_CAST_FRONT;
  785. //fall back to H3 cast/2hex
  786. //even if creature have 2hex attack instead of cast it is ok since we fall back to attack anyway
  787. if(myAnim->framesInGroup(group) == 0)
  788. {
  789. if(projectileAngle > straightAngle)
  790. group = CCreatureAnim::CAST_UP;
  791. else if(projectileAngle < -straightAngle)
  792. group = CCreatureAnim::CAST_DOWN;
  793. else
  794. group = CCreatureAnim::CAST_FRONT;
  795. }
  796. //fall back to ranged attack
  797. if(myAnim->framesInGroup(group) == 0)
  798. {
  799. if(projectileAngle > straightAngle)
  800. group = CCreatureAnim::SHOOT_UP;
  801. else if(projectileAngle < -straightAngle)
  802. group = CCreatureAnim::SHOOT_DOWN;
  803. else
  804. group = CCreatureAnim::SHOOT_FRONT;
  805. }
  806. //fall back to normal attack
  807. if(myAnim->framesInGroup(group) == 0)
  808. {
  809. if(projectileAngle > straightAngle)
  810. group = CCreatureAnim::ATTACK_UP;
  811. else if(projectileAngle < -straightAngle)
  812. group = CCreatureAnim::ATTACK_DOWN;
  813. else
  814. group = CCreatureAnim::ATTACK_FRONT;
  815. }
  816. return true;
  817. }
  818. void CCastAnimation::nextFrame()
  819. {
  820. for(auto & it : pendingAnimations())
  821. {
  822. CReverseAnimation * anim = dynamic_cast<CReverseAnimation *>(it.first);
  823. if(anim && anim->stack->ID == stack->ID && anim->priority)
  824. return;
  825. }
  826. if(myAnim->getType() != group)
  827. {
  828. myAnim->setType(group);
  829. myAnim->onAnimationReset += std::bind(&CAttackAnimation::endAnim, this);
  830. }
  831. CBattleAnimation::nextFrame();
  832. }
  833. void CCastAnimation::endAnim()
  834. {
  835. CAttackAnimation::endAnim();
  836. delete this;
  837. }
  838. CEffectAnimation::CEffectAnimation(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx, int _dy, bool _Vflip, bool _alignToBottom)
  839. : CBattleAnimation(_owner),
  840. destTile(BattleHex::INVALID),
  841. x(_x),
  842. y(_y),
  843. dx(_dx),
  844. dy(_dy),
  845. Vflip(_Vflip),
  846. alignToBottom(_alignToBottom)
  847. {
  848. logAnim->debug("Created effect animation %s", _customAnim);
  849. customAnim = std::make_shared<CAnimation>(_customAnim);
  850. }
  851. CEffectAnimation::CEffectAnimation(CBattleInterface * _owner, std::shared_ptr<CAnimation> _customAnim, int _x, int _y, int _dx, int _dy)
  852. : CBattleAnimation(_owner),
  853. destTile(BattleHex::INVALID),
  854. customAnim(_customAnim),
  855. x(_x),
  856. y(_y),
  857. dx(_dx),
  858. dy(_dy),
  859. Vflip(false),
  860. alignToBottom(false)
  861. {
  862. logAnim->debug("Created custom effect animation");
  863. }
  864. CEffectAnimation::CEffectAnimation(CBattleInterface * _owner, std::string _customAnim, BattleHex _destTile, bool _Vflip, bool _alignToBottom)
  865. : CBattleAnimation(_owner),
  866. destTile(_destTile),
  867. x(-1),
  868. y(-1),
  869. dx(0),
  870. dy(0),
  871. Vflip(_Vflip),
  872. alignToBottom(_alignToBottom)
  873. {
  874. logAnim->debug("Created effect animation %s", _customAnim);
  875. customAnim = std::make_shared<CAnimation>(_customAnim);
  876. }
  877. bool CEffectAnimation::init()
  878. {
  879. if(!isEarliest(true))
  880. return false;
  881. const bool areaEffect = (!destTile.isValid() && x == -1 && y == -1);
  882. std::shared_ptr<CAnimation> animation = customAnim;
  883. animation->preload();
  884. if(Vflip)
  885. animation->verticalFlip();
  886. auto first = animation->getImage(0, 0, true);
  887. if(!first)
  888. {
  889. endAnim();
  890. return false;
  891. }
  892. if(areaEffect) //f.e. armageddon
  893. {
  894. for(int i=0; i * first->width() < owner->pos.w ; ++i)
  895. {
  896. for(int j=0; j * first->height() < owner->pos.h ; ++j)
  897. {
  898. BattleEffect be;
  899. be.effectID = ID;
  900. be.animation = animation;
  901. be.currentFrame = 0;
  902. be.x = i * first->width() + owner->pos.x;
  903. be.y = j * first->height() + owner->pos.y;
  904. be.position = BattleHex::INVALID;
  905. owner->effectsController->battleEffects.push_back(be);
  906. }
  907. }
  908. }
  909. else // Effects targeted at a specific creature/hex.
  910. {
  911. const CStack * destStack = owner->getCurrentPlayerInterface()->cb->battleGetStackByPos(destTile, false);
  912. BattleEffect be;
  913. be.effectID = ID;
  914. be.animation = animation;
  915. be.currentFrame = 0;
  916. //todo: lightning anim frame count override
  917. // if(effect == 1)
  918. // be.maxFrame = 3;
  919. be.x = x;
  920. be.y = y;
  921. if(destTile.isValid())
  922. {
  923. Rect tilePos = owner->fieldController->hexPosition(destTile);
  924. if(x == -1)
  925. be.x = tilePos.x + tilePos.w/2 - first->width()/2;
  926. if(y == -1)
  927. {
  928. if(alignToBottom)
  929. be.y = tilePos.y + tilePos.h - first->height();
  930. else
  931. be.y = tilePos.y - first->height()/2;
  932. }
  933. // Correction for 2-hex creatures.
  934. if(destStack != nullptr && destStack->doubleWide())
  935. be.x += (destStack->side == BattleSide::ATTACKER ? -1 : 1)*tilePos.w/2;
  936. }
  937. assert(be.x != -1 && be.y != -1);
  938. //Indicate if effect should be drawn on top of everything or just on top of the hex
  939. be.position = destTile;
  940. owner->effectsController->battleEffects.push_back(be);
  941. }
  942. return true;
  943. }
  944. void CEffectAnimation::nextFrame()
  945. {
  946. //notice: there may be more than one effect in owner->battleEffects correcponding to this animation (ie. armageddon)
  947. for(auto & elem : owner->effectsController->battleEffects)
  948. {
  949. if(elem.effectID == ID)
  950. {
  951. elem.currentFrame += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000;
  952. if(elem.currentFrame >= elem.animation->size())
  953. {
  954. endAnim();
  955. break;
  956. }
  957. else
  958. {
  959. elem.x += dx;
  960. elem.y += dy;
  961. }
  962. }
  963. }
  964. }
  965. void CEffectAnimation::endAnim()
  966. {
  967. CBattleAnimation::endAnim();
  968. auto & effects = owner->effectsController->battleEffects;
  969. for ( auto it = effects.begin(); it != effects.end(); )
  970. {
  971. if (it->effectID == ID)
  972. it = effects.erase(it);
  973. else
  974. it++;
  975. }
  976. delete this;
  977. }