2
0

CBattleAnimations.cpp 33 KB

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