CBattleAnimations.cpp 30 KB

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