2
0

CBattleAnimations.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. #include "StdInc.h"
  2. #include "CBattleAnimations.h"
  3. #include <boost/math/constants/constants.hpp>
  4. #include "../CMusicHandler.h"
  5. #include "../CGameInfo.h"
  6. #include "CBattleInterface.h"
  7. #include "CBattleInterfaceClasses.h"
  8. #include "CCreatureAnimation.h"
  9. #include "../../lib/BattleState.h"
  10. #include "../CPlayerInterface.h"
  11. #include "../../CCallback.h"
  12. #include "../UIFramework/SDL_Extensions.h"
  13. #include "../Graphics.h"
  14. #include "../UIFramework/CCursorHandler.h"
  15. #include "../../lib/CTownHandler.h"
  16. CBattleAnimation::CBattleAnimation(CBattleInterface * _owner)
  17. : owner(_owner), ID(_owner->animIDhelper++)
  18. {}
  19. void CBattleAnimation::endAnim()
  20. {
  21. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  22. {
  23. if(it->first == this)
  24. {
  25. it->first = NULL;
  26. }
  27. }
  28. }
  29. bool CBattleAnimation::isEarliest(bool perStackConcurrency)
  30. {
  31. int lowestMoveID = owner->animIDhelper + 5;
  32. CBattleStackAnimation * thAnim = dynamic_cast<CBattleStackAnimation *>(this);
  33. CSpellEffectAnimation * thSen = dynamic_cast<CSpellEffectAnimation *>(this);
  34. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  35. {
  36. CBattleStackAnimation * stAnim = dynamic_cast<CBattleStackAnimation *>(it->first);
  37. CSpellEffectAnimation * sen = dynamic_cast<CSpellEffectAnimation *>(it->first);
  38. if(perStackConcurrency && stAnim && thAnim && stAnim->stack->ID != thAnim->stack->ID)
  39. continue;
  40. if(sen && thSen && sen != thSen && perStackConcurrency)
  41. continue;
  42. CReverseAnimation * revAnim = dynamic_cast<CReverseAnimation *>(stAnim);
  43. if(revAnim && thAnim && stAnim && stAnim->stack->ID == thAnim->stack->ID && revAnim->priority)
  44. return false;
  45. if(it->first)
  46. vstd::amin(lowestMoveID, it->first->ID);
  47. }
  48. return (ID == lowestMoveID) || (lowestMoveID == (owner->animIDhelper + 5));
  49. }
  50. CBattleStackAnimation::CBattleStackAnimation(CBattleInterface * _owner, const CStack * _stack)
  51. : CBattleAnimation(_owner), stack(_stack)
  52. {}
  53. CCreatureAnimation* CBattleStackAnimation::myAnim()
  54. {
  55. return owner->creAnims[stack->ID];
  56. }
  57. void CAttackAnimation::nextFrame()
  58. {
  59. if(myAnim()->getType() != group)
  60. myAnim()->setType(group);
  61. if(myAnim()->onFirstFrameInGroup())
  62. {
  63. if(shooting)
  64. CCS->soundh->playSound(battle_sound(attackingStack->getCreature(), shoot));
  65. else
  66. CCS->soundh->playSound(battle_sound(attackingStack->getCreature(), attack));
  67. }
  68. else if(myAnim()->onLastFrameInGroup())
  69. {
  70. myAnim()->setType(CCreatureAnim::HOLDING);
  71. endAnim();
  72. return; //execution of endAnim deletes this !!!
  73. }
  74. }
  75. bool CAttackAnimation::checkInitialConditions()
  76. {
  77. return isEarliest(false);
  78. }
  79. CAttackAnimation::CAttackAnimation(CBattleInterface *_owner, const CStack *attacker, BattleHex _dest, const CStack *defender)
  80. : CBattleStackAnimation(_owner, attacker), dest(_dest), attackedStack(defender), attackingStack(attacker)
  81. {
  82. assert(attackingStack && "attackingStack is NULL in CBattleAttack::CBattleAttack !\n");
  83. bool isCatapultAttack = attackingStack->hasBonusOfType(Bonus::CATAPULT)
  84. && owner->curInt->cb->battleHexToWallPart(_dest) >= 0;
  85. assert(attackedStack || isCatapultAttack);
  86. attackingStackPosBeforeReturn = attackingStack->position;
  87. }
  88. CDefenceAnimation::CDefenceAnimation(StackAttackedInfo _attackedInfo, CBattleInterface * _owner)
  89. : CBattleStackAnimation(_owner, _attackedInfo.defender),
  90. attacker(_attackedInfo.attacker), byShooting(_attackedInfo.byShooting),
  91. killed(_attackedInfo.killed)
  92. {}
  93. bool CDefenceAnimation::init()
  94. {
  95. //checking initial conditions
  96. //if(owner->creAnims[stackID]->getType() != 2)
  97. //{
  98. // return false;
  99. //}
  100. if(attacker == NULL && owner->battleEffects.size() > 0)
  101. return false;
  102. ui32 lowestMoveID = owner->animIDhelper + 5;
  103. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  104. {
  105. CDefenceAnimation * defAnim = dynamic_cast<CDefenceAnimation *>(it->first);
  106. if(defAnim && defAnim->stack->ID != stack->ID)
  107. continue;
  108. CAttackAnimation * attAnim = dynamic_cast<CAttackAnimation *>(it->first);
  109. if(attAnim && attAnim->stack->ID != stack->ID)
  110. continue;
  111. if(attacker != NULL)
  112. {
  113. int attackerAnimType = owner->creAnims[attacker->ID]->getType();
  114. if( ( attackerAnimType == CCreatureAnim::ATTACK_UP ||
  115. attackerAnimType == CCreatureAnim::ATTACK_FRONT ||
  116. attackerAnimType == CCreatureAnim::ATTACK_DOWN ) &&
  117. owner->creAnims[attacker->ID]->getFrame() < attacker->getCreature()->animation.attackClimaxFrame )
  118. return false;
  119. }
  120. CReverseAnimation * animAsRev = dynamic_cast<CReverseAnimation *>(it->first);
  121. if(animAsRev && animAsRev->priority)
  122. return false;
  123. if(it->first)
  124. vstd::amin(lowestMoveID, it->first->ID);
  125. }
  126. if(ID > lowestMoveID)
  127. return false;
  128. //reverse unit if necessary
  129. if (attacker && owner->curInt->cb->isToReverse(stack->position, attacker->position, owner->creDir[stack->ID], attacker->doubleWide(), owner->creDir[attacker->ID]))
  130. {
  131. owner->addNewAnim(new CReverseAnimation(owner, stack, stack->position, true));
  132. return false;
  133. }
  134. //unit reversed
  135. if(byShooting) //delay hit animation
  136. {
  137. for(std::list<ProjectileInfo>::const_iterator it = owner->projectiles.begin(); it != owner->projectiles.end(); ++it)
  138. {
  139. if(it->creID == attacker->getCreature()->idNumber)
  140. {
  141. return false;
  142. }
  143. }
  144. }
  145. //initializing
  146. if(killed)
  147. {
  148. CCS->soundh->playSound(battle_sound(stack->getCreature(), killed));
  149. myAnim()->setType(CCreatureAnim::DEATH); //death
  150. }
  151. else
  152. {
  153. // TODO: this block doesn't seems correct if the unit is defending.
  154. CCS->soundh->playSound(battle_sound(stack->getCreature(), wince));
  155. myAnim()->setType(CCreatureAnim::HITTED); //getting hit
  156. }
  157. return true; //initialized successfuly
  158. }
  159. void CDefenceAnimation::nextFrame()
  160. {
  161. if(!killed && myAnim()->getType() != CCreatureAnim::HITTED)
  162. {
  163. myAnim()->setType(CCreatureAnim::HITTED);
  164. }
  165. if(!myAnim()->onLastFrameInGroup())
  166. {
  167. if( myAnim()->getType() == CCreatureAnim::DEATH && (owner->animCount+1)%(4/owner->getAnimSpeed())==0
  168. && !myAnim()->onLastFrameInGroup() )
  169. {
  170. myAnim()->incrementFrame();
  171. }
  172. }
  173. else
  174. {
  175. endAnim();
  176. }
  177. }
  178. void CDefenceAnimation::endAnim()
  179. {
  180. //restoring animType
  181. if(myAnim()->getType() == CCreatureAnim::HITTED)
  182. myAnim()->setType(CCreatureAnim::HOLDING);
  183. //printing info to console
  184. //if(attacker!=NULL)
  185. // owner->printConsoleAttacked(stack, dmg, amountKilled, attacker);
  186. //const CStack * attacker = owner->curInt->cb->battleGetStackByID(IDby, false);
  187. //const CStack * attacked = owner->curInt->cb->battleGetStackByID(stackID, false);
  188. CBattleAnimation::endAnim();
  189. delete this;
  190. }
  191. CDummyAnimation::CDummyAnimation(CBattleInterface * _owner, int howManyFrames)
  192. : CBattleAnimation(_owner), counter(0), howMany(howManyFrames)
  193. {}
  194. bool CDummyAnimation::init()
  195. {
  196. return true;
  197. }
  198. void CDummyAnimation::nextFrame()
  199. {
  200. counter++;
  201. if(counter > howMany)
  202. endAnim();
  203. }
  204. void CDummyAnimation::endAnim()
  205. {
  206. CBattleAnimation::endAnim();
  207. delete this;
  208. }
  209. bool CMeleeAttackAnimation::init()
  210. {
  211. if( !CAttackAnimation::checkInitialConditions() )
  212. return false;
  213. //if(owner->creAnims[stackID]->getType()!=2)
  214. //{
  215. // return false;
  216. //}
  217. if(!attackingStack || myAnim()->getType() == 5)
  218. {
  219. endAnim();
  220. return false;
  221. }
  222. bool toReverse = owner->curInt->cb->isToReverse(attackingStackPosBeforeReturn, dest, owner->creDir[stack->ID], attackedStack->doubleWide(), owner->creDir[attackedStack->ID]);
  223. if (toReverse)
  224. {
  225. owner->addNewAnim(new CReverseAnimation(owner, stack, attackingStackPosBeforeReturn, true));
  226. return false;
  227. }
  228. //reversed
  229. shooting = false;
  230. static const CCreatureAnim::EAnimType mutPosToGroup[] = {CCreatureAnim::ATTACK_UP, CCreatureAnim::ATTACK_UP,
  231. CCreatureAnim::ATTACK_FRONT, CCreatureAnim::ATTACK_DOWN, CCreatureAnim::ATTACK_DOWN, CCreatureAnim::ATTACK_FRONT};
  232. int revShiftattacker = (attackingStack->attackerOwned ? -1 : 1);
  233. int mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, dest);
  234. if(mutPos == -1 && attackingStack->doubleWide())
  235. {
  236. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, attackedStack->position);
  237. }
  238. if (mutPos == -1 && attackedStack->doubleWide())
  239. {
  240. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, attackedStack->occupiedHex());
  241. }
  242. if (mutPos == -1 && attackedStack->doubleWide() && attackingStack->doubleWide())
  243. {
  244. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, attackedStack->occupiedHex());
  245. }
  246. switch(mutPos) //attack direction
  247. {
  248. case 0: case 1: case 2: case 3: case 4: case 5:
  249. group = mutPosToGroup[mutPos];
  250. break;
  251. default:
  252. tlog1<<"Critical Error! Wrong dest in stackAttacking! dest: "<<dest<<" attacking stack pos: "<<attackingStackPosBeforeReturn<<" mutual pos: "<<mutPos<<std::endl;
  253. group = CCreatureAnim::ATTACK_FRONT;
  254. break;
  255. }
  256. return true;
  257. }
  258. CMeleeAttackAnimation::CMeleeAttackAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked)
  259. : CAttackAnimation(_owner, attacker, _dest, _attacked)
  260. {}
  261. void CMeleeAttackAnimation::nextFrame()
  262. {
  263. /*for(std::list<std::pair<CBattleAnimation *, bool> >::const_iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  264. {
  265. CBattleMoveStart * anim = dynamic_cast<CBattleMoveStart *>(it->first);
  266. CReverseAnim * anim2 = dynamic_cast<CReverseAnim *>(it->first);
  267. if( (anim && anim->stackID == stackID) || (anim2 && anim2->stackID == stackID ) )
  268. return;
  269. }*/
  270. CAttackAnimation::nextFrame();
  271. }
  272. void CMeleeAttackAnimation::endAnim()
  273. {
  274. CBattleAnimation::endAnim();
  275. delete this;
  276. }
  277. bool CMovementAnimation::init()
  278. {
  279. if( !isEarliest(false) )
  280. return false;
  281. //a few useful variables
  282. steps = static_cast<int>(myAnim()->framesInGroup(CCreatureAnim::MOVING) * owner->getAnimSpeedMultiplier() - 1);
  283. const CStack * movedStack = stack;
  284. if(!movedStack || myAnim()->getType() == 5)
  285. {
  286. endAnim();
  287. return false;
  288. }
  289. Point begPosition = CClickableHex::getXYUnitAnim(curStackPos, movedStack->attackerOwned, movedStack, owner);
  290. Point endPosition = CClickableHex::getXYUnitAnim(nextHex, movedStack->attackerOwned, movedStack, owner);
  291. if(steps < 0 || stack->hasBonus(Selector::typeSubtype(Bonus::FLYING, 1))) //no movement or teleport
  292. {
  293. //this creature seems to have no move animation so we can end it immediately
  294. endAnim();
  295. return false;
  296. }
  297. whichStep = 0;
  298. int hexWbase = 44, hexHbase = 42;
  299. //bool twoTiles = movedStack->doubleWide();
  300. int mutPos = BattleHex::mutualPosition(curStackPos, nextHex);
  301. //reverse unit if necessary
  302. if((begPosition.x > endPosition.x) && owner->creDir[stack->ID] == true)
  303. {
  304. owner->addNewAnim(new CReverseAnimation(owner, stack, curStackPos, true));
  305. return false;
  306. }
  307. else if ((begPosition.x < endPosition.x) && owner->creDir[stack->ID] == false)
  308. {
  309. owner->addNewAnim(new CReverseAnimation(owner, stack, curStackPos, true));
  310. return false;
  311. }
  312. if(myAnim()->getType() != CCreatureAnim::MOVING)
  313. {
  314. myAnim()->setType(CCreatureAnim::MOVING);
  315. }
  316. //unit reversed
  317. if (owner->moveSh == -1)
  318. {
  319. owner->moveSh = CCS->soundh->playSound(battle_sound(movedStack->getCreature(), move), -1);
  320. }
  321. //step shift calculation
  322. posX = myAnim()->pos.x, posY = myAnim()->pos.y; // for precise calculations ;]
  323. if(mutPos == -1 && movedStack->hasBonusOfType(Bonus::FLYING))
  324. {
  325. steps *= distance;
  326. steps /= 2; //to make animation faster
  327. stepX = (endPosition.x - begPosition.x) / static_cast<double>(steps);
  328. stepY = (endPosition.y - begPosition.y) / static_cast<double>(steps);
  329. }
  330. else
  331. {
  332. switch(mutPos)
  333. {
  334. case 0:
  335. stepX = -1.0 * (hexWbase / (2.0 * steps));
  336. stepY = -1.0 * (hexHbase / (static_cast<double>(steps)));
  337. break;
  338. case 1:
  339. stepX = hexWbase / (2.0 * steps);
  340. stepY = -1.0 * hexHbase / (static_cast<double>(steps));
  341. break;
  342. case 2:
  343. stepX = hexWbase / static_cast<double>(steps);
  344. stepY = 0.0;
  345. break;
  346. case 3:
  347. stepX = hexWbase / (2.0 * steps);
  348. stepY = hexHbase / static_cast<double>(steps);
  349. break;
  350. case 4:
  351. stepX = -1.0 * hexWbase / (2.0 * steps);
  352. stepY = hexHbase / static_cast<double>(steps);
  353. break;
  354. case 5:
  355. stepX = -1.0 * hexWbase / static_cast<double>(steps);
  356. stepY = 0.0;
  357. break;
  358. }
  359. }
  360. //step shifts calculated
  361. return true;
  362. }
  363. void CMovementAnimation::nextFrame()
  364. {
  365. //moving instructions
  366. posX += stepX;
  367. myAnim()->pos.x = static_cast<Sint16>(posX);
  368. posY += stepY;
  369. myAnim()->pos.y = static_cast<Sint16>(posY);
  370. // Increments step count and check if we are finished with current animation
  371. ++whichStep;
  372. if(whichStep == steps)
  373. {
  374. // Sets the position of the creature animation sprites
  375. Point coords = CClickableHex::getXYUnitAnim(nextHex, owner->creDir[stack->ID], stack, owner);
  376. myAnim()->pos = coords;
  377. // true if creature haven't reached the final destination hex
  378. if ((nextPos + 1) < destTiles.size())
  379. {
  380. // update the next hex field which has to be reached by the stack
  381. nextPos++;
  382. curStackPos = nextHex;
  383. nextHex = destTiles[nextPos];
  384. // update position of double wide creatures
  385. bool twoTiles = stack->doubleWide();
  386. if(twoTiles && bool(stack->attackerOwned) && (owner->creDir[stack->ID] != bool(stack->attackerOwned) )) //big attacker creature is reversed
  387. myAnim()->pos.x -= 44;
  388. else if(twoTiles && (! bool(stack->attackerOwned) ) && (owner->creDir[stack->ID] != bool(stack->attackerOwned) )) //big defender creature is reversed
  389. myAnim()->pos.x += 44;
  390. // re-init animation
  391. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  392. {
  393. if (it->first == this)
  394. {
  395. it->second = false;
  396. break;
  397. }
  398. }
  399. }
  400. else
  401. endAnim();
  402. }
  403. }
  404. void CMovementAnimation::endAnim()
  405. {
  406. const CStack * movedStack = stack;
  407. myAnim()->pos = CClickableHex::getXYUnitAnim(nextHex, movedStack->attackerOwned, movedStack, owner);
  408. CBattleAnimation::endAnim();
  409. if(movedStack)
  410. owner->addNewAnim(new CMovementEndAnimation(owner, stack, nextHex));
  411. if(owner->moveSh >= 0)
  412. {
  413. CCS->soundh->stopSound(owner->moveSh);
  414. owner->moveSh = -1;
  415. }
  416. delete this;
  417. }
  418. CMovementAnimation::CMovementAnimation(CBattleInterface *_owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance)
  419. : CBattleStackAnimation(_owner, _stack), destTiles(_destTiles), nextPos(0), distance(_distance), stepX(0.0), stepY(0.0)
  420. {
  421. curStackPos = stack->position;
  422. nextHex = destTiles.front();
  423. }
  424. CMovementEndAnimation::CMovementEndAnimation(CBattleInterface * _owner, const CStack * _stack, BattleHex destTile)
  425. : CBattleStackAnimation(_owner, _stack), destinationTile(destTile)
  426. {}
  427. bool CMovementEndAnimation::init()
  428. {
  429. if( !isEarliest(true) )
  430. return false;
  431. if(!stack || myAnim()->framesInGroup(CCreatureAnim::MOVE_END) == 0 ||
  432. myAnim()->getType() == CCreatureAnim::DEATH)
  433. {
  434. endAnim();
  435. return false;
  436. }
  437. CCS->soundh->playSound(battle_sound(stack->getCreature(), endMoving));
  438. myAnim()->setType(CCreatureAnim::MOVE_END);
  439. return true;
  440. }
  441. void CMovementEndAnimation::nextFrame()
  442. {
  443. if(myAnim()->onLastFrameInGroup())
  444. {
  445. endAnim();
  446. }
  447. }
  448. void CMovementEndAnimation::endAnim()
  449. {
  450. CBattleAnimation::endAnim();
  451. if(myAnim()->getType() != CCreatureAnim::DEATH)
  452. myAnim()->setType(CCreatureAnim::HOLDING); //resetting to default
  453. CCS->curh->show();
  454. delete this;
  455. }
  456. CMovementStartAnimation::CMovementStartAnimation(CBattleInterface * _owner, const CStack * _stack)
  457. : CBattleStackAnimation(_owner, _stack)
  458. {}
  459. bool CMovementStartAnimation::init()
  460. {
  461. if( !isEarliest(false) )
  462. return false;
  463. if(!stack || myAnim()->getType() == CCreatureAnim::DEATH)
  464. {
  465. CMovementStartAnimation::endAnim();
  466. return false;
  467. }
  468. CCS->soundh->playSound(battle_sound(stack->getCreature(), startMoving));
  469. myAnim()->setType(CCreatureAnim::MOVE_START);
  470. return true;
  471. }
  472. void CMovementStartAnimation::nextFrame()
  473. {
  474. if(myAnim()->onLastFrameInGroup())
  475. {
  476. endAnim();
  477. }
  478. else
  479. {
  480. if((owner->animCount+1)%(4/owner->getAnimSpeed())==0)
  481. myAnim()->incrementFrame();
  482. }
  483. }
  484. void CMovementStartAnimation::endAnim()
  485. {
  486. CBattleAnimation::endAnim();
  487. delete this;
  488. }
  489. CReverseAnimation::CReverseAnimation(CBattleInterface * _owner, const CStack * stack, BattleHex dest, bool _priority)
  490. : CBattleStackAnimation(_owner, stack), partOfAnim(1), secondPartSetup(false), hex(dest), priority(_priority)
  491. {}
  492. bool CReverseAnimation::init()
  493. {
  494. if(myAnim() == NULL || myAnim()->getType() == CCreatureAnim::DEATH)
  495. {
  496. endAnim();
  497. return false; //there is no such creature
  498. }
  499. if(!priority && !isEarliest(false))
  500. return false;
  501. if(myAnim()->framesInGroup(CCreatureAnim::TURN_R))
  502. myAnim()->setType(CCreatureAnim::TURN_R);
  503. else
  504. setupSecondPart();
  505. return true;
  506. }
  507. void CReverseAnimation::nextFrame()
  508. {
  509. if(partOfAnim == 1) //first part of animation
  510. {
  511. if(myAnim()->onLastFrameInGroup())
  512. {
  513. partOfAnim = 2;
  514. }
  515. }
  516. else if(partOfAnim == 2)
  517. {
  518. if(!secondPartSetup)
  519. {
  520. setupSecondPart();
  521. }
  522. if(myAnim()->onLastFrameInGroup())
  523. {
  524. endAnim();
  525. }
  526. }
  527. }
  528. void CReverseAnimation::endAnim()
  529. {
  530. CBattleAnimation::endAnim();
  531. if( stack->alive() )//don't do that if stack is dead
  532. myAnim()->setType(CCreatureAnim::HOLDING);
  533. delete this;
  534. }
  535. void CReverseAnimation::setupSecondPart()
  536. {
  537. if(!stack)
  538. {
  539. endAnim();
  540. return;
  541. }
  542. owner->creDir[stack->ID] = !owner->creDir[stack->ID];
  543. Point coords = CClickableHex::getXYUnitAnim(hex, owner->creDir[stack->ID], stack, owner);
  544. myAnim()->pos.x = coords.x;
  545. //creAnims[stackID]->pos.y = coords.second;
  546. if(stack->doubleWide())
  547. {
  548. if(stack->attackerOwned)
  549. {
  550. if(!owner->creDir[stack->ID])
  551. myAnim()->pos.x -= 44;
  552. }
  553. else
  554. {
  555. if(owner->creDir[stack->ID])
  556. myAnim()->pos.x += 44;
  557. }
  558. }
  559. secondPartSetup = true;
  560. if(myAnim()->framesInGroup(CCreatureAnim::TURN_L))
  561. myAnim()->setType(CCreatureAnim::TURN_L);
  562. else
  563. endAnim();
  564. }
  565. CShootingAnimation::CShootingAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, bool _catapult, int _catapultDmg)
  566. : CAttackAnimation(_owner, attacker, _dest, _attacked), catapultDamage(_catapultDmg)
  567. {}
  568. bool CShootingAnimation::init()
  569. {
  570. if( !CAttackAnimation::checkInitialConditions() )
  571. return false;
  572. const CStack * shooter = attackingStack;
  573. if(!shooter || myAnim()->getType() == CCreatureAnim::DEATH)
  574. {
  575. endAnim();
  576. return false;
  577. }
  578. //reverse unit if necessary
  579. if (attackingStack && attackedStack && owner->curInt->cb->isToReverse(attackingStack->position, attackedStack->position, owner->creDir[attackingStack->ID], attackingStack->doubleWide(), owner->creDir[attackedStack->ID]))
  580. {
  581. owner->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->position, true));
  582. return false;
  583. }
  584. // Create the projectile animation
  585. double projectileAngle; //in radians; if positive, projectiles goes up
  586. double straightAngle = 0.2; //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  587. int fromHex = shooter->position;
  588. projectileAngle = atan2(static_cast<double>(abs(dest - fromHex) / GameConstants::BFIELD_WIDTH), static_cast<double>(abs(dest - fromHex) % GameConstants::BFIELD_WIDTH));
  589. if(fromHex < dest)
  590. projectileAngle = -projectileAngle;
  591. // Get further info about the shooter e.g. relative pos of projectile to unit.
  592. // If the creature id is 149 then it's a arrow tower which has no additional info so get the
  593. // actual arrow tower shooter instead.
  594. const CCreature *shooterInfo = shooter->getCreature();
  595. if (shooterInfo->idNumber == CreatureID::ARROW_TOWERS)
  596. {
  597. int creID = owner->siegeH->town->town->clientInfo.siegeShooter;
  598. shooterInfo = CGI->creh->creatures[creID];
  599. }
  600. ProjectileInfo spi;
  601. spi.creID = shooter->getCreature()->idNumber;
  602. spi.stackID = shooter->ID;
  603. spi.reverse = !shooter->attackerOwned;
  604. spi.step = 0;
  605. spi.frameNum = 0;
  606. spi.spin = shooterInfo->animation.projectileSpin;
  607. Point xycoord = CClickableHex::getXYUnitAnim(shooter->position, true, shooter, owner);
  608. // The "master" point where all projectile positions relate to.
  609. static const Point projectileOrigin(181, 252);
  610. if (attackedStack)
  611. {
  612. Point destcoord = CClickableHex::getXYUnitAnim(dest, false, attackedStack, owner);
  613. destcoord.x += 250; destcoord.y += 210; //TODO: find a better place to shoot
  614. // Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
  615. if (projectileAngle > straightAngle)
  616. {
  617. //upper shot
  618. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->animation.upperRightMissleOffsetX;
  619. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->animation.upperRightMissleOffsetY;
  620. }
  621. else if (projectileAngle < -straightAngle)
  622. {
  623. //lower shot
  624. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->animation.lowerRightMissleOffsetX;
  625. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->animation.lowerRightMissleOffsetY;
  626. }
  627. else
  628. {
  629. //straight shot
  630. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->animation.rightMissleOffsetX;
  631. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->animation.rightMissleOffsetY;
  632. }
  633. double animSpeed = 23.0 * owner->getAnimSpeed(); // flight speed of projectile
  634. spi.lastStep = static_cast<int>(sqrt(static_cast<double>((destcoord.x - spi.x) * (destcoord.x - spi.x) + (destcoord.y - spi.y) * (destcoord.y - spi.y))) / animSpeed);
  635. if(spi.lastStep == 0)
  636. spi.lastStep = 1;
  637. spi.dx = (destcoord.x - spi.x) / spi.lastStep;
  638. spi.dy = (destcoord.y - spi.y) / spi.lastStep;
  639. spi.catapultInfo = 0;
  640. }
  641. else
  642. {
  643. // Catapult attack
  644. // These are the values for equations of this kind: f(x) = ax^2 + bx + c
  645. static const std::vector<CatapultProjectileInfo*> trajectoryCurves = boost::assign::list_of<CatapultProjectileInfo*>(new CatapultProjectileInfo(4.309, -3.198, 569.2, -296, 182))
  646. (new CatapultProjectileInfo(4.710, -3.11, 558.68, -258, 175))(new CatapultProjectileInfo(5.056, -3.003, 546.9, -236, 174))
  647. (new CatapultProjectileInfo(4.760, -2.74, 526.47, -216, 215))(new CatapultProjectileInfo(4.288, -2.496, 508.98, -223, 274))
  648. (new CatapultProjectileInfo(3.683, -3.018, 558.39, -324, 176))(new CatapultProjectileInfo(2.884, -2.607, 528.95, -366, 312))
  649. (new CatapultProjectileInfo(3.783, -2.364, 501.35, -227, 318));
  650. static std::map<int, int> hexToCurve = boost::assign::map_list_of<int, int>(29, 0)(62, 1)(95, 2)(130, 3)(182, 4)(12, 5)(50, 6)(183, 7);
  651. std::map<int, int>::iterator it = hexToCurve.find(dest.hex);
  652. if (it == hexToCurve.end())
  653. {
  654. tlog1 << "For the hex position " << dest.hex << " is no curve defined.";
  655. endAnim();
  656. return false;
  657. }
  658. else
  659. {
  660. int curveID = it->second;
  661. spi.catapultInfo = trajectoryCurves[curveID];
  662. double animSpeed = 3.318 * owner->getAnimSpeed();
  663. spi.lastStep = static_cast<int>((spi.catapultInfo->toX - spi.catapultInfo->fromX) / animSpeed);
  664. spi.dx = animSpeed;
  665. spi.dy = 0;
  666. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->animation.rightMissleOffsetX + 17.;
  667. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->animation.rightMissleOffsetY + 10.;
  668. // Add explosion anim
  669. int xEnd = static_cast<int>(spi.x + spi.lastStep * spi.dx);
  670. int yEnd = static_cast<int>(spi.catapultInfo->calculateY(xEnd));
  671. owner->addNewAnim( new CSpellEffectAnimation(owner, catapultDamage ? "SGEXPL.DEF" : "CSGRCK.DEF", xEnd - 126, yEnd - 105));
  672. }
  673. }
  674. // Set starting frame
  675. if(spi.spin)
  676. {
  677. spi.frameNum = 0;
  678. }
  679. else
  680. {
  681. double pi = boost::math::constants::pi<double>();
  682. spi.frameNum = static_cast<int>(((pi / 2.0 - projectileAngle) / (2.0 * pi) + 1/(static_cast<double>(2*(owner->idToProjectile[spi.creID]->ourImages.size()-1)))) * (owner->idToProjectile[spi.creID]->ourImages.size()-1));
  683. }
  684. // Set projectile animation start delay which is specified in frames
  685. spi.animStartDelay = shooterInfo->animation.attackClimaxFrame;
  686. owner->projectiles.push_back(spi);
  687. //attack animation
  688. shooting = true;
  689. if(projectileAngle > straightAngle) //upper shot
  690. group = CCreatureAnim::SHOOT_UP;
  691. else if(projectileAngle < -straightAngle) //lower shot
  692. group = CCreatureAnim::SHOOT_DOWN;
  693. else //straight shot
  694. group = CCreatureAnim::SHOOT_FRONT;
  695. return true;
  696. }
  697. void CShootingAnimation::nextFrame()
  698. {
  699. for(std::list<std::pair<CBattleAnimation *, bool> >::const_iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  700. {
  701. CMovementStartAnimation * anim = dynamic_cast<CMovementStartAnimation *>(it->first);
  702. CReverseAnimation * anim2 = dynamic_cast<CReverseAnimation *>(it->first);
  703. if( (anim && anim->stack->ID == stack->ID) || (anim2 && anim2->stack->ID == stack->ID && anim2->priority ) )
  704. return;
  705. }
  706. CAttackAnimation::nextFrame();
  707. }
  708. void CShootingAnimation::endAnim()
  709. {
  710. CBattleAnimation::endAnim();
  711. delete this;
  712. }
  713. CSpellEffectAnimation::CSpellEffectAnimation(CBattleInterface * _owner, ui32 _effect, BattleHex _destTile, int _dx, int _dy, bool _Vflip)
  714. :CBattleAnimation(_owner), effect(_effect), destTile(_destTile), customAnim(""), x(0), y(0), dx(_dx), dy(_dy), Vflip(_Vflip)
  715. {}
  716. CSpellEffectAnimation::CSpellEffectAnimation(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx, int _dy, bool _Vflip)
  717. :CBattleAnimation(_owner), effect(-1), destTile(0), customAnim(_customAnim), x(_x), y(_y), dx(_dx), dy(_dy), Vflip(_Vflip)
  718. {}
  719. bool CSpellEffectAnimation::init()
  720. {
  721. if(!isEarliest(true))
  722. return false;
  723. if(effect == 12) //armageddon
  724. {
  725. if(effect == -1 || graphics->battleACToDef[effect].size() != 0)
  726. {
  727. CDefHandler * anim;
  728. if(customAnim.size())
  729. anim = CDefHandler::giveDef(customAnim);
  730. else
  731. anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  732. if (Vflip)
  733. {
  734. for (size_t v = 0; v < anim->ourImages.size(); ++v)
  735. {
  736. CSDL_Ext::VflipSurf(anim->ourImages[v].bitmap);
  737. }
  738. }
  739. for(int i=0; i * anim->width < owner->pos.w ; ++i)
  740. {
  741. for(int j=0; j * anim->height < owner->pos.h ; ++j)
  742. {
  743. BattleEffect be;
  744. be.effectID = ID;
  745. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  746. if (Vflip)
  747. {
  748. for (size_t v = 0; v < be.anim->ourImages.size(); ++v)
  749. {
  750. CSDL_Ext::VflipSurf(be.anim->ourImages[v].bitmap);
  751. }
  752. }
  753. be.frame = 0;
  754. be.maxFrame = be.anim->ourImages.size();
  755. be.x = i * anim->width + owner->pos.x;
  756. be.y = j * anim->height + owner->pos.y;
  757. owner->battleEffects.push_back(be);
  758. }
  759. }
  760. }
  761. else //there is nothing to play
  762. {
  763. endAnim();
  764. return false;
  765. }
  766. }
  767. else // Effects targeted at a specific creature/hex.
  768. {
  769. if(effect == -1 || graphics->battleACToDef[effect].size() != 0)
  770. {
  771. const CStack* destStack = owner->curInt->cb->battleGetStackByPos(destTile, false);
  772. Rect &tilePos = owner->bfield[destTile]->pos;
  773. BattleEffect be;
  774. be.effectID = ID;
  775. if(customAnim.size())
  776. be.anim = CDefHandler::giveDef(customAnim);
  777. else
  778. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  779. if (Vflip)
  780. {
  781. for (size_t v = 0; v < be.anim->ourImages.size(); ++v)
  782. {
  783. CSDL_Ext::VflipSurf(be.anim->ourImages[v].bitmap);
  784. }
  785. }
  786. be.frame = 0;
  787. be.maxFrame = be.anim->ourImages.size();
  788. if(effect == 1)
  789. be.maxFrame = 3;
  790. switch (effect)
  791. {
  792. case ui32(-1):
  793. be.x = x;
  794. be.y = y;
  795. break;
  796. case 0: // Prayer and Lightning Bolt.
  797. case 1:
  798. // Position effect with it's bottom center touching the bottom center of affected tile(s).
  799. be.x = tilePos.x + tilePos.w/2 - be.anim->width/2;
  800. be.y = tilePos.y + tilePos.h - be.anim->height;
  801. break;
  802. default:
  803. // Position effect with it's center touching the top center of affected tile(s).
  804. be.x = tilePos.x + tilePos.w/2 - be.anim->width/2;
  805. be.y = tilePos.y - be.anim->height/2;
  806. break;
  807. }
  808. // Correction for 2-hex creatures.
  809. if (destStack != NULL && destStack->doubleWide())
  810. be.x += (destStack->attackerOwned ? -1 : 1)*tilePos.w/2;
  811. owner->battleEffects.push_back(be);
  812. }
  813. else //there is nothing to play
  814. {
  815. endAnim();
  816. return false;
  817. }
  818. }
  819. //battleEffects
  820. return true;
  821. }
  822. void CSpellEffectAnimation::nextFrame()
  823. {
  824. //notice: there may be more than one effect in owner->battleEffects correcponding to this animation (ie. armageddon)
  825. for(std::list<BattleEffect>::iterator it = owner->battleEffects.begin(); it != owner->battleEffects.end(); ++it)
  826. {
  827. if(it->effectID == ID)
  828. {
  829. ++(it->frame);
  830. if(it->frame == it->maxFrame)
  831. {
  832. endAnim();
  833. break;
  834. }
  835. else
  836. {
  837. it->x += dx;
  838. it->y += dy;
  839. }
  840. }
  841. }
  842. }
  843. void CSpellEffectAnimation::endAnim()
  844. {
  845. CBattleAnimation::endAnim();
  846. std::vector<std::list<BattleEffect>::iterator> toDel;
  847. for(std::list<BattleEffect>::iterator it = owner->battleEffects.begin(); it != owner->battleEffects.end(); ++it)
  848. {
  849. if(it->effectID == ID)
  850. {
  851. toDel.push_back(it);
  852. }
  853. }
  854. for(size_t b = 0; b < toDel.size(); ++b)
  855. {
  856. delete toDel[b]->anim;
  857. owner->battleEffects.erase(toDel[b]);
  858. }
  859. delete this;
  860. }