CBattleAnimations.cpp 30 KB

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