CBattleAnimations.cpp 30 KB

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