CBattleAnimations.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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 "../gui/SDL_Extensions.h"
  13. #include "../Graphics.h"
  14. #include "../gui/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. //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  586. static const double straightAngle = 0.2;
  587. // Get further info about the shooter e.g. relative pos of projectile to unit.
  588. // If the creature id is 149 then it's a arrow tower which has no additional info so get the
  589. // actual arrow tower shooter instead.
  590. const CCreature *shooterInfo = shooter->getCreature();
  591. if (shooterInfo->idNumber == CreatureID::ARROW_TOWERS)
  592. {
  593. int creID = owner->siegeH->town->town->clientInfo.siegeShooter;
  594. shooterInfo = CGI->creh->creatures[creID];
  595. }
  596. ProjectileInfo spi;
  597. spi.creID = shooter->getCreature()->idNumber;
  598. spi.stackID = shooter->ID;
  599. // reverse if creature is facing right OR this is non-existing stack that is not tower (war machines)
  600. spi.reverse = attackingStack ? !owner->creDir[attackingStack->ID] : shooter->getCreature()->idNumber != CreatureID::ARROW_TOWERS ;
  601. spi.step = 0;
  602. spi.frameNum = 0;
  603. Point fromPos;
  604. Point destPos;
  605. // NOTE: two lines below return different positions (very notable with 2-hex creatures). Obtaining via creanims seems to be more precise
  606. fromPos = owner->creAnims[spi.stackID]->pos.topLeft();
  607. //xycoord = CClickableHex::getXYUnitAnim(shooter->position, true, shooter, owner);
  608. destPos = CClickableHex::getXYUnitAnim(dest, false, attackedStack, owner);
  609. // to properly translate coordinates when shooter is rotated
  610. int multiplier = spi.reverse ? -1 : 1;
  611. double projectileAngle = atan2(fabs((double)destPos.y - fromPos.y), fabs((double)destPos.x - fromPos.x));
  612. if(shooter->position < dest)
  613. projectileAngle = -projectileAngle;
  614. // Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
  615. if (projectileAngle > straightAngle)
  616. {
  617. //upper shot
  618. spi.x = fromPos.x + 222 + ( -25 + shooterInfo->animation.upperRightMissleOffsetX ) * multiplier;
  619. spi.y = fromPos.y + 265 + shooterInfo->animation.upperRightMissleOffsetY;
  620. }
  621. else if (projectileAngle < -straightAngle)
  622. {
  623. //lower shot
  624. spi.x = fromPos.x + 222 + ( -25 + shooterInfo->animation.lowerRightMissleOffsetX ) * multiplier;
  625. spi.y = fromPos.y + 265 + shooterInfo->animation.lowerRightMissleOffsetY;
  626. }
  627. else
  628. {
  629. //straight shot
  630. spi.x = fromPos.x + 222 + ( -25 + shooterInfo->animation.rightMissleOffsetX ) * multiplier;
  631. spi.y = fromPos.y + 265 + shooterInfo->animation.rightMissleOffsetY;
  632. }
  633. destPos += Point(225, 225);
  634. // recalculate angle taking in account offsets
  635. //projectileAngle = atan2(fabs(destPos.y - spi.y), fabs(destPos.x - spi.x));
  636. //if(shooter->position < dest)
  637. // projectileAngle = -projectileAngle;
  638. if (attackedStack)
  639. {
  640. double animSpeed = 23.0 * owner->getAnimSpeed(); // flight speed of projectile
  641. 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);
  642. if(spi.lastStep == 0)
  643. spi.lastStep = 1;
  644. spi.dx = (destPos.x - spi.x) / spi.lastStep;
  645. spi.dy = (destPos.y - spi.y) / spi.lastStep;
  646. }
  647. else
  648. {
  649. // Catapult attack
  650. spi.catapultInfo.reset(new CatapultProjectileInfo(Point(spi.x, spi.y), destPos));
  651. double animSpeed = 3.318 * owner->getAnimSpeed();
  652. spi.lastStep = abs((destPos.x - spi.x) / animSpeed);
  653. spi.dx = animSpeed;
  654. spi.dy = 0;
  655. SDL_Surface * img = owner->idToProjectile[spi.creID]->ourImages[0].bitmap;
  656. // Add explosion anim
  657. Point animPos(destPos.x - 126 + img->w / 2,
  658. destPos.y - 105 + img->h / 2);
  659. owner->addNewAnim( new CSpellEffectAnimation(owner, catapultDamage ? "SGEXPL.DEF" : "CSGRCK.DEF", animPos.x, animPos.y));
  660. }
  661. auto & angles = shooterInfo->animation.missleFrameAngles;
  662. double pi = boost::math::constants::pi<double>();
  663. // only frames below maxFrame are usable: anything higher is either no present or we don't know when it should be used
  664. size_t maxFrame = std::min<size_t>(angles.size(), owner->idToProjectile[spi.creID]->ourImages.size());
  665. assert(maxFrame > 0);
  666. // values in angles array indicate position from which this frame was rendered, in degrees.
  667. // find frame that has closest angle to one that we need for this shot
  668. size_t bestID = 0;
  669. double bestDiff = fabs( angles[0] / 180 * pi - projectileAngle );
  670. for (size_t i=1; i<maxFrame; i++)
  671. {
  672. double currentDiff = fabs( angles[i] / 180 * pi - projectileAngle );
  673. if (currentDiff < bestDiff)
  674. {
  675. bestID = i;
  676. bestDiff = currentDiff;
  677. }
  678. }
  679. spi.frameNum = bestID;
  680. // Set projectile animation start delay which is specified in frames
  681. spi.animStartDelay = shooterInfo->animation.attackClimaxFrame;
  682. owner->projectiles.push_back(spi);
  683. //attack animation
  684. shooting = true;
  685. if(projectileAngle > straightAngle) //upper shot
  686. group = CCreatureAnim::SHOOT_UP;
  687. else if(projectileAngle < -straightAngle) //lower shot
  688. group = CCreatureAnim::SHOOT_DOWN;
  689. else //straight shot
  690. group = CCreatureAnim::SHOOT_FRONT;
  691. return true;
  692. }
  693. void CShootingAnimation::nextFrame()
  694. {
  695. for(std::list<std::pair<CBattleAnimation *, bool> >::const_iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  696. {
  697. CMovementStartAnimation * anim = dynamic_cast<CMovementStartAnimation *>(it->first);
  698. CReverseAnimation * anim2 = dynamic_cast<CReverseAnimation *>(it->first);
  699. if( (anim && anim->stack->ID == stack->ID) || (anim2 && anim2->stack->ID == stack->ID && anim2->priority ) )
  700. return;
  701. }
  702. CAttackAnimation::nextFrame();
  703. }
  704. void CShootingAnimation::endAnim()
  705. {
  706. CBattleAnimation::endAnim();
  707. delete this;
  708. }
  709. CSpellEffectAnimation::CSpellEffectAnimation(CBattleInterface * _owner, ui32 _effect, BattleHex _destTile, int _dx, int _dy, bool _Vflip)
  710. :CBattleAnimation(_owner), effect(_effect), destTile(_destTile), customAnim(""), x(0), y(0), dx(_dx), dy(_dy), Vflip(_Vflip)
  711. {}
  712. CSpellEffectAnimation::CSpellEffectAnimation(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx, int _dy, bool _Vflip)
  713. :CBattleAnimation(_owner), effect(-1), destTile(0), customAnim(_customAnim), x(_x), y(_y), dx(_dx), dy(_dy), Vflip(_Vflip)
  714. {}
  715. bool CSpellEffectAnimation::init()
  716. {
  717. if(!isEarliest(true))
  718. return false;
  719. if(effect == 12) //armageddon
  720. {
  721. if(effect == -1 || graphics->battleACToDef[effect].size() != 0)
  722. {
  723. CDefHandler * anim;
  724. if(customAnim.size())
  725. anim = CDefHandler::giveDef(customAnim);
  726. else
  727. anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  728. if (Vflip)
  729. {
  730. for (size_t v = 0; v < anim->ourImages.size(); ++v)
  731. {
  732. CSDL_Ext::VflipSurf(anim->ourImages[v].bitmap);
  733. }
  734. }
  735. for(int i=0; i * anim->width < owner->pos.w ; ++i)
  736. {
  737. for(int j=0; j * anim->height < owner->pos.h ; ++j)
  738. {
  739. BattleEffect be;
  740. be.effectID = ID;
  741. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  742. if (Vflip)
  743. {
  744. for (size_t v = 0; v < be.anim->ourImages.size(); ++v)
  745. {
  746. CSDL_Ext::VflipSurf(be.anim->ourImages[v].bitmap);
  747. }
  748. }
  749. be.frame = 0;
  750. be.maxFrame = be.anim->ourImages.size();
  751. be.x = i * anim->width + owner->pos.x;
  752. be.y = j * anim->height + owner->pos.y;
  753. owner->battleEffects.push_back(be);
  754. }
  755. }
  756. }
  757. else //there is nothing to play
  758. {
  759. endAnim();
  760. return false;
  761. }
  762. }
  763. else // Effects targeted at a specific creature/hex.
  764. {
  765. if(effect == -1 || graphics->battleACToDef[effect].size() != 0)
  766. {
  767. const CStack* destStack = owner->curInt->cb->battleGetStackByPos(destTile, false);
  768. Rect &tilePos = owner->bfield[destTile]->pos;
  769. BattleEffect be;
  770. be.effectID = ID;
  771. if(customAnim.size())
  772. be.anim = CDefHandler::giveDef(customAnim);
  773. else
  774. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  775. if (Vflip)
  776. {
  777. for (size_t v = 0; v < be.anim->ourImages.size(); ++v)
  778. {
  779. CSDL_Ext::VflipSurf(be.anim->ourImages[v].bitmap);
  780. }
  781. }
  782. be.frame = 0;
  783. be.maxFrame = be.anim->ourImages.size();
  784. if(effect == 1)
  785. be.maxFrame = 3;
  786. switch (effect)
  787. {
  788. case ui32(-1):
  789. be.x = x;
  790. be.y = y;
  791. break;
  792. case 0: // Prayer and Lightning Bolt.
  793. case 1:
  794. // Position effect with it's bottom center touching the bottom center of affected tile(s).
  795. be.x = tilePos.x + tilePos.w/2 - be.anim->width/2;
  796. be.y = tilePos.y + tilePos.h - be.anim->height;
  797. break;
  798. default:
  799. // Position effect with it's center touching the top center of affected tile(s).
  800. be.x = tilePos.x + tilePos.w/2 - be.anim->width/2;
  801. be.y = tilePos.y - be.anim->height/2;
  802. break;
  803. }
  804. // Correction for 2-hex creatures.
  805. if (destStack != NULL && destStack->doubleWide())
  806. be.x += (destStack->attackerOwned ? -1 : 1)*tilePos.w/2;
  807. owner->battleEffects.push_back(be);
  808. }
  809. else //there is nothing to play
  810. {
  811. endAnim();
  812. return false;
  813. }
  814. }
  815. //battleEffects
  816. return true;
  817. }
  818. void CSpellEffectAnimation::nextFrame()
  819. {
  820. //notice: there may be more than one effect in owner->battleEffects correcponding to this animation (ie. armageddon)
  821. for(std::list<BattleEffect>::iterator it = owner->battleEffects.begin(); it != owner->battleEffects.end(); ++it)
  822. {
  823. if(it->effectID == ID)
  824. {
  825. ++(it->frame);
  826. if(it->frame == it->maxFrame)
  827. {
  828. endAnim();
  829. break;
  830. }
  831. else
  832. {
  833. it->x += dx;
  834. it->y += dy;
  835. }
  836. }
  837. }
  838. }
  839. void CSpellEffectAnimation::endAnim()
  840. {
  841. CBattleAnimation::endAnim();
  842. std::vector<std::list<BattleEffect>::iterator> toDel;
  843. for(std::list<BattleEffect>::iterator it = owner->battleEffects.begin(); it != owner->battleEffects.end(); ++it)
  844. {
  845. if(it->effectID == ID)
  846. {
  847. toDel.push_back(it);
  848. }
  849. }
  850. for(size_t b = 0; b < toDel.size(); ++b)
  851. {
  852. delete toDel[b]->anim;
  853. owner->battleEffects.erase(toDel[b]);
  854. }
  855. delete this;
  856. }