CBattleAnimations.cpp 29 KB

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