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->battleHexToWallPart(_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 == -1)
  352. {
  353. owner->moveSh = CCS->soundh->playSound(battle_sound(movedStack->getCreature(), move), -1);
  354. }
  355. //step shift calculation
  356. posX = myAnim()->pos.x, posY = myAnim()->pos.y; // for precise calculations ;]
  357. if(mutPos == -1 && movedStack->hasBonusOfType(Bonus::FLYING))
  358. {
  359. steps *= distance;
  360. steps /= 2; //to make animation faster
  361. stepX = (endPosition.x - begPosition.x) / static_cast<double>(steps);
  362. stepY = (endPosition.y - begPosition.y) / static_cast<double>(steps);
  363. }
  364. else
  365. {
  366. switch(mutPos)
  367. {
  368. case 0:
  369. stepX = -1.0 * (hexWbase / (2.0 * steps));
  370. stepY = -1.0 * (hexHbase / (static_cast<double>(steps)));
  371. break;
  372. case 1:
  373. stepX = hexWbase / (2.0 * steps);
  374. stepY = -1.0 * hexHbase / (static_cast<double>(steps));
  375. break;
  376. case 2:
  377. stepX = hexWbase / static_cast<double>(steps);
  378. stepY = 0.0;
  379. break;
  380. case 3:
  381. stepX = hexWbase / (2.0 * steps);
  382. stepY = hexHbase / static_cast<double>(steps);
  383. break;
  384. case 4:
  385. stepX = -1.0 * hexWbase / (2.0 * steps);
  386. stepY = hexHbase / static_cast<double>(steps);
  387. break;
  388. case 5:
  389. stepX = -1.0 * hexWbase / static_cast<double>(steps);
  390. stepY = 0.0;
  391. break;
  392. }
  393. }
  394. //step shifts calculated
  395. return true;
  396. }
  397. void CMovementAnimation::nextFrame()
  398. {
  399. //moving instructions
  400. posX += stepX;
  401. myAnim()->pos.x = static_cast<Sint16>(posX);
  402. posY += stepY;
  403. myAnim()->pos.y = static_cast<Sint16>(posY);
  404. // Increments step count and check if we are finished with current animation
  405. ++whichStep;
  406. if(whichStep == steps)
  407. {
  408. // Sets the position of the creature animation sprites
  409. Point coords = CClickableHex::getXYUnitAnim(nextHex, owner->creDir[stack->ID], stack, owner);
  410. myAnim()->pos = coords;
  411. // true if creature haven't reached the final destination hex
  412. if ((nextPos + 1) < destTiles.size())
  413. {
  414. // update the next hex field which has to be reached by the stack
  415. nextPos++;
  416. curStackPos = nextHex;
  417. nextHex = destTiles[nextPos];
  418. // update position of double wide creatures
  419. bool twoTiles = stack->doubleWide();
  420. if(twoTiles && bool(stack->attackerOwned) && (owner->creDir[stack->ID] != bool(stack->attackerOwned) )) //big attacker creature is reversed
  421. myAnim()->pos.x -= 44;
  422. else if(twoTiles && (! bool(stack->attackerOwned) ) && (owner->creDir[stack->ID] != bool(stack->attackerOwned) )) //big defender creature is reversed
  423. myAnim()->pos.x += 44;
  424. // re-init animation
  425. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  426. {
  427. if (it->first == this)
  428. {
  429. it->second = false;
  430. break;
  431. }
  432. }
  433. }
  434. else
  435. endAnim();
  436. }
  437. }
  438. void CMovementAnimation::endAnim()
  439. {
  440. const CStack * movedStack = stack;
  441. CBattleAnimation::endAnim();
  442. if(movedStack)
  443. owner->addNewAnim(new CMovementEndAnimation(owner, stack, nextHex));
  444. if(owner->moveSh >= 0)
  445. {
  446. CCS->soundh->stopSound(owner->moveSh);
  447. owner->moveSh = -1;
  448. }
  449. delete this;
  450. }
  451. CMovementAnimation::CMovementAnimation(CBattleInterface *_owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance)
  452. : CBattleStackAnimation(_owner, _stack), destTiles(_destTiles), nextPos(0), distance(_distance), stepX(0.0), stepY(0.0)
  453. {
  454. curStackPos = stack->position;
  455. nextHex = destTiles.front();
  456. }
  457. CMovementEndAnimation::CMovementEndAnimation(CBattleInterface * _owner, const CStack * _stack, BattleHex destTile)
  458. : CBattleStackAnimation(_owner, _stack), destinationTile(destTile)
  459. {}
  460. bool CMovementEndAnimation::init()
  461. {
  462. if( !isEarliest(true) )
  463. return false;
  464. if(!stack || myAnim()->framesInGroup(CCreatureAnim::MOVE_END) == 0 ||
  465. myAnim()->getType() == CCreatureAnim::DEATH)
  466. {
  467. endAnim();
  468. return false;
  469. }
  470. CCS->soundh->playSound(battle_sound(stack->getCreature(), endMoving));
  471. myAnim()->setType(CCreatureAnim::MOVE_END);
  472. return true;
  473. }
  474. void CMovementEndAnimation::nextFrame()
  475. {
  476. if(myAnim()->onLastFrameInGroup())
  477. {
  478. endAnim();
  479. }
  480. }
  481. void CMovementEndAnimation::endAnim()
  482. {
  483. CBattleAnimation::endAnim();
  484. if(myAnim()->getType() != CCreatureAnim::DEATH)
  485. myAnim()->setType(CCreatureAnim::HOLDING); //resetting to default
  486. CCS->curh->show();
  487. delete this;
  488. }
  489. CMovementStartAnimation::CMovementStartAnimation(CBattleInterface * _owner, const CStack * _stack)
  490. : CBattleStackAnimation(_owner, _stack)
  491. {}
  492. bool CMovementStartAnimation::init()
  493. {
  494. if( !isEarliest(false) )
  495. return false;
  496. if(!stack || myAnim()->getType() == 5)
  497. {
  498. CMovementStartAnimation::endAnim();
  499. return false;
  500. }
  501. CCS->soundh->playSound(battle_sound(stack->getCreature(), startMoving));
  502. myAnim()->setType(CCreatureAnim::MOVE_START);
  503. return true;
  504. }
  505. void CMovementStartAnimation::nextFrame()
  506. {
  507. if(myAnim()->onLastFrameInGroup())
  508. {
  509. endAnim();
  510. }
  511. else
  512. {
  513. if((owner->animCount+1)%(4/owner->getAnimSpeed())==0)
  514. myAnim()->incrementFrame();
  515. }
  516. }
  517. void CMovementStartAnimation::endAnim()
  518. {
  519. CBattleAnimation::endAnim();
  520. delete this;
  521. }
  522. CReverseAnimation::CReverseAnimation(CBattleInterface * _owner, const CStack * stack, BattleHex dest, bool _priority)
  523. : CBattleStackAnimation(_owner, stack), partOfAnim(1), secondPartSetup(false), hex(dest), priority(_priority)
  524. {}
  525. bool CReverseAnimation::init()
  526. {
  527. if(myAnim() == NULL || myAnim()->getType() == 5)
  528. {
  529. endAnim();
  530. return false; //there is no such creature
  531. }
  532. if(!priority && !isEarliest(false))
  533. return false;
  534. if(myAnim()->framesInGroup(CCreatureAnim::TURN_R))
  535. myAnim()->setType(CCreatureAnim::TURN_R);
  536. else
  537. setupSecondPart();
  538. return true;
  539. }
  540. void CReverseAnimation::nextFrame()
  541. {
  542. if(partOfAnim == 1) //first part of animation
  543. {
  544. if(myAnim()->onLastFrameInGroup())
  545. {
  546. partOfAnim = 2;
  547. }
  548. }
  549. else if(partOfAnim == 2)
  550. {
  551. if(!secondPartSetup)
  552. {
  553. setupSecondPart();
  554. }
  555. if(myAnim()->onLastFrameInGroup())
  556. {
  557. endAnim();
  558. }
  559. }
  560. }
  561. void CReverseAnimation::endAnim()
  562. {
  563. CBattleAnimation::endAnim();
  564. if( stack->alive() )//don't do that if stack is dead
  565. myAnim()->setType(CCreatureAnim::HOLDING);
  566. delete this;
  567. }
  568. void CReverseAnimation::setupSecondPart()
  569. {
  570. if(!stack)
  571. {
  572. endAnim();
  573. return;
  574. }
  575. owner->creDir[stack->ID] = !owner->creDir[stack->ID];
  576. Point coords = CClickableHex::getXYUnitAnim(hex, owner->creDir[stack->ID], stack, owner);
  577. myAnim()->pos.x = coords.x;
  578. //creAnims[stackID]->pos.y = coords.second;
  579. if(stack->doubleWide())
  580. {
  581. if(stack->attackerOwned)
  582. {
  583. if(!owner->creDir[stack->ID])
  584. myAnim()->pos.x -= 44;
  585. }
  586. else
  587. {
  588. if(owner->creDir[stack->ID])
  589. myAnim()->pos.x += 44;
  590. }
  591. }
  592. secondPartSetup = true;
  593. if(myAnim()->framesInGroup(CCreatureAnim::TURN_L))
  594. myAnim()->setType(CCreatureAnim::TURN_L);
  595. else
  596. endAnim();
  597. }
  598. CShootingAnimation::CShootingAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, bool _catapult, int _catapultDmg)
  599. : CAttackAnimation(_owner, attacker, _dest, _attacked), catapultDamage(_catapultDmg), catapult(_catapult)
  600. {}
  601. bool CShootingAnimation::init()
  602. {
  603. if( !CAttackAnimation::checkInitialConditions() )
  604. return false;
  605. const CStack * shooter = attackingStack;
  606. if(!shooter || myAnim()->getType() == 5)
  607. {
  608. endAnim();
  609. return false;
  610. }
  611. // Create the projectile animation
  612. double projectileAngle; //in radians; if positive, projectiles goes up
  613. 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)
  614. int fromHex = shooter->position;
  615. projectileAngle = atan2(static_cast<double>(abs(dest - fromHex) / GameConstants::BFIELD_WIDTH), static_cast<double>(abs(dest - fromHex) % GameConstants::BFIELD_WIDTH));
  616. if(fromHex < dest)
  617. projectileAngle = -projectileAngle;
  618. // Get further info about the shooter e.g. relative pos of projectile to unit.
  619. // If the creature id is 149 then it's a arrow tower which has no additional info so get the
  620. // actual arrow tower shooter instead.
  621. const CCreature *shooterInfo = shooter->getCreature();
  622. if (shooterInfo->idNumber == 149)
  623. {
  624. int creID = CGI->creh->factionToTurretCreature[owner->siegeH->town->town->typeID];
  625. shooterInfo = CGI->creh->creatures[creID];
  626. }
  627. ProjectileInfo spi;
  628. spi.creID = shooter->getCreature()->idNumber;
  629. spi.stackID = shooter->ID;
  630. spi.reverse = !shooter->attackerOwned;
  631. spi.step = 0;
  632. spi.frameNum = 0;
  633. if(vstd::contains(CGI->creh->idToProjectileSpin, shooterInfo->idNumber))
  634. spi.spin = CGI->creh->idToProjectileSpin[shooterInfo->idNumber];
  635. else
  636. {
  637. tlog2 << "Warning - no projectile spin for spi.creID " << shooterInfo->idNumber << std::endl;
  638. spi.spin = false;
  639. }
  640. Point xycoord = CClickableHex::getXYUnitAnim(shooter->position, true, shooter, owner);
  641. Point destcoord;
  642. // The "master" point where all projectile positions relate to.
  643. static const Point projectileOrigin(181, 252);
  644. if (attackedStack)
  645. {
  646. destcoord = CClickableHex::getXYUnitAnim(dest, false, attackedStack, owner);
  647. destcoord.x += 250; destcoord.y += 210; //TODO: find a better place to shoot
  648. // Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
  649. if (projectileAngle > straightAngle)
  650. {
  651. //upper shot
  652. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->upperRightMissleOffsetX;
  653. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->upperRightMissleOffsetY;
  654. }
  655. else if (projectileAngle < -straightAngle)
  656. {
  657. //lower shot
  658. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->lowerRightMissleOffsetX;
  659. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->lowerRightMissleOffsetY;
  660. }
  661. else
  662. {
  663. //straight shot
  664. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->rightMissleOffsetX;
  665. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->rightMissleOffsetY;
  666. }
  667. double animSpeed = 23.0 * owner->getAnimSpeed(); // flight speed of projectile
  668. 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);
  669. if(spi.lastStep == 0)
  670. spi.lastStep = 1;
  671. spi.dx = (destcoord.x - spi.x) / spi.lastStep;
  672. spi.dy = (destcoord.y - spi.y) / spi.lastStep;
  673. spi.catapultInfo = 0;
  674. }
  675. else
  676. {
  677. // Catapult attack
  678. // These are the values for equations of this kind: f(x) = ax^2 + bx + c
  679. static const std::vector<CatapultProjectileInfo*> trajectoryCurves = boost::assign::list_of<CatapultProjectileInfo*>(new CatapultProjectileInfo(4.309, -3.198, 569.2, -296, 182))
  680. (new CatapultProjectileInfo(4.710, -3.11, 558.68, -258, 175))(new CatapultProjectileInfo(5.056, -3.003, 546.9, -236, 174))
  681. (new CatapultProjectileInfo(4.760, -2.74, 526.47, -216, 215))(new CatapultProjectileInfo(4.288, -2.496, 508.98, -223, 274))
  682. (new CatapultProjectileInfo(3.683, -3.018, 558.39, -324, 176))(new CatapultProjectileInfo(2.884, -2.607, 528.95, -366, 312))
  683. (new CatapultProjectileInfo(3.783, -2.364, 501.35, -227, 318));
  684. 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);
  685. std::map<int, int>::iterator it = hexToCurve.find(dest.hex);
  686. if (it == hexToCurve.end())
  687. {
  688. tlog1 << "For the hex position " << dest.hex << " is no curve defined.";
  689. endAnim();
  690. return false;
  691. }
  692. else
  693. {
  694. int curveID = it->second;
  695. spi.catapultInfo = trajectoryCurves[curveID];
  696. double animSpeed = 3.318 * owner->getAnimSpeed();
  697. spi.lastStep = static_cast<int>((spi.catapultInfo->toX - spi.catapultInfo->fromX) / animSpeed);
  698. spi.dx = animSpeed;
  699. spi.dy = 0;
  700. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->rightMissleOffsetX + 17.;
  701. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->rightMissleOffsetY + 10.;
  702. // Add explosion anim
  703. int xEnd = static_cast<int>(spi.x + spi.lastStep * spi.dx);
  704. int yEnd = static_cast<int>(spi.catapultInfo->calculateY(xEnd));
  705. owner->addNewAnim( new CSpellEffectAnimation(owner, catapultDamage ? "SGEXPL.DEF" : "CSGRCK.DEF", xEnd - 126, yEnd - 105));
  706. }
  707. }
  708. // Set starting frame
  709. if(spi.spin)
  710. {
  711. spi.frameNum = 0;
  712. }
  713. else
  714. {
  715. double pi = boost::math::constants::pi<double>();
  716. 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));
  717. }
  718. // Set projectile animation start delay which is specified in frames
  719. spi.animStartDelay = shooterInfo->attackClimaxFrame;
  720. owner->projectiles.push_back(spi);
  721. //attack animation
  722. shooting = true;
  723. if(projectileAngle > straightAngle) //upper shot
  724. group = CCreatureAnim::SHOOT_UP;
  725. else if(projectileAngle < -straightAngle) //lower shot
  726. group = CCreatureAnim::SHOOT_DOWN;
  727. else //straight shot
  728. group = CCreatureAnim::SHOOT_FRONT;
  729. return true;
  730. }
  731. void CShootingAnimation::nextFrame()
  732. {
  733. for(std::list<std::pair<CBattleAnimation *, bool> >::const_iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  734. {
  735. CMovementStartAnimation * anim = dynamic_cast<CMovementStartAnimation *>(it->first);
  736. CReverseAnimation * anim2 = dynamic_cast<CReverseAnimation *>(it->first);
  737. if( (anim && anim->stack->ID == stack->ID) || (anim2 && anim2->stack->ID == stack->ID && anim2->priority ) )
  738. return;
  739. }
  740. CAttackAnimation::nextFrame();
  741. }
  742. void CShootingAnimation::endAnim()
  743. {
  744. CBattleAnimation::endAnim();
  745. delete this;
  746. }
  747. CSpellEffectAnimation::CSpellEffectAnimation(CBattleInterface * _owner, ui32 _effect, BattleHex _destTile, int _dx, int _dy, bool _Vflip)
  748. :CBattleAnimation(_owner), effect(_effect), destTile(_destTile), customAnim(""), x(0), y(0), dx(_dx), dy(_dy), Vflip(_Vflip)
  749. {}
  750. CSpellEffectAnimation::CSpellEffectAnimation(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx, int _dy, bool _Vflip)
  751. :CBattleAnimation(_owner), effect(-1), destTile(0), customAnim(_customAnim), x(_x), y(_y), dx(_dx), dy(_dy), Vflip(_Vflip)
  752. {}
  753. bool CSpellEffectAnimation::init()
  754. {
  755. if(!isEarliest(true))
  756. return false;
  757. if(effect == 12) //armageddon
  758. {
  759. if(effect == -1 || graphics->battleACToDef[effect].size() != 0)
  760. {
  761. CDefHandler * anim;
  762. if(customAnim.size())
  763. anim = CDefHandler::giveDef(customAnim);
  764. else
  765. anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  766. if (Vflip)
  767. {
  768. for (size_t v = 0; v < anim->ourImages.size(); ++v)
  769. {
  770. CSDL_Ext::VflipSurf(anim->ourImages[v].bitmap);
  771. }
  772. }
  773. for(int i=0; i * anim->width < owner->pos.w ; ++i)
  774. {
  775. for(int j=0; j * anim->height < owner->pos.h ; ++j)
  776. {
  777. BattleEffect be;
  778. be.effectID = ID;
  779. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  780. if (Vflip)
  781. {
  782. for (size_t v = 0; v < be.anim->ourImages.size(); ++v)
  783. {
  784. CSDL_Ext::VflipSurf(be.anim->ourImages[v].bitmap);
  785. }
  786. }
  787. be.frame = 0;
  788. be.maxFrame = be.anim->ourImages.size();
  789. be.x = i * anim->width + owner->pos.x;
  790. be.y = j * anim->height + owner->pos.y;
  791. owner->battleEffects.push_back(be);
  792. }
  793. }
  794. }
  795. else //there is nothing to play
  796. {
  797. endAnim();
  798. return false;
  799. }
  800. }
  801. else // Effects targeted at a specific creature/hex.
  802. {
  803. if(effect == -1 || graphics->battleACToDef[effect].size() != 0)
  804. {
  805. const CStack* destStack = owner->curInt->cb->battleGetStackByPos(destTile, false);
  806. Rect &tilePos = owner->bfield[destTile]->pos;
  807. BattleEffect be;
  808. be.effectID = ID;
  809. if(customAnim.size())
  810. be.anim = CDefHandler::giveDef(customAnim);
  811. else
  812. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  813. if (Vflip)
  814. {
  815. for (size_t v = 0; v < be.anim->ourImages.size(); ++v)
  816. {
  817. CSDL_Ext::VflipSurf(be.anim->ourImages[v].bitmap);
  818. }
  819. }
  820. be.frame = 0;
  821. be.maxFrame = be.anim->ourImages.size();
  822. if(effect == 1)
  823. be.maxFrame = 3;
  824. switch (effect)
  825. {
  826. case ui32(-1):
  827. be.x = x;
  828. be.y = y;
  829. break;
  830. case 0: // Prayer and Lightning Bolt.
  831. case 1:
  832. // Position effect with it's bottom center touching the bottom center of affected tile(s).
  833. be.x = tilePos.x + tilePos.w/2 - be.anim->width/2;
  834. be.y = tilePos.y + tilePos.h - be.anim->height;
  835. break;
  836. default:
  837. // Position effect with it's center touching the top center of affected tile(s).
  838. be.x = tilePos.x + tilePos.w/2 - be.anim->width/2;
  839. be.y = tilePos.y - be.anim->height/2;
  840. break;
  841. }
  842. // Correction for 2-hex creatures.
  843. if (destStack != NULL && destStack->doubleWide())
  844. be.x += (destStack->attackerOwned ? -1 : 1)*tilePos.w/2;
  845. owner->battleEffects.push_back(be);
  846. }
  847. else //there is nothing to play
  848. {
  849. endAnim();
  850. return false;
  851. }
  852. }
  853. //battleEffects
  854. return true;
  855. }
  856. void CSpellEffectAnimation::nextFrame()
  857. {
  858. //notice: there may be more than one effect in owner->battleEffects correcponding to this animation (ie. armageddon)
  859. for(std::list<BattleEffect>::iterator it = owner->battleEffects.begin(); it != owner->battleEffects.end(); ++it)
  860. {
  861. if(it->effectID == ID)
  862. {
  863. ++(it->frame);
  864. if(it->frame == it->maxFrame)
  865. {
  866. endAnim();
  867. break;
  868. }
  869. else
  870. {
  871. it->x += dx;
  872. it->y += dy;
  873. }
  874. }
  875. }
  876. }
  877. void CSpellEffectAnimation::endAnim()
  878. {
  879. CBattleAnimation::endAnim();
  880. std::vector<std::list<BattleEffect>::iterator> toDel;
  881. for(std::list<BattleEffect>::iterator it = owner->battleEffects.begin(); it != owner->battleEffects.end(); ++it)
  882. {
  883. if(it->effectID == ID)
  884. {
  885. toDel.push_back(it);
  886. }
  887. }
  888. for(size_t b = 0; b < toDel.size(); ++b)
  889. {
  890. delete toDel[b]->anim;
  891. owner->battleEffects.erase(toDel[b]);
  892. }
  893. delete this;
  894. }