CBattleAnimations.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. const CStack * movedStack = stack;
  319. if(!movedStack || myAnim()->getType() == 5)
  320. {
  321. endAnim();
  322. return false;
  323. }
  324. Point begPosition = CClickableHex::getXYUnitAnim(curStackPos, movedStack->attackerOwned, movedStack, owner);
  325. Point endPosition = CClickableHex::getXYUnitAnim(nextHex, movedStack->attackerOwned, movedStack, owner);
  326. if(steps < 0 || stack->hasBonus(Selector::typeSubtype(Bonus::FLYING, 1))) //no movement or teleport
  327. {
  328. //this creature seems to have no move animation so we can end it immediately
  329. endAnim();
  330. return false;
  331. }
  332. whichStep = 0;
  333. int hexWbase = 44, hexHbase = 42;
  334. //bool twoTiles = movedStack->doubleWide();
  335. int mutPos = BattleHex::mutualPosition(curStackPos, nextHex);
  336. //reverse unit if necessary
  337. if((begPosition.x > endPosition.x) && owner->creDir[stack->ID] == true)
  338. {
  339. owner->addNewAnim(new CReverseAnimation(owner, stack, curStackPos, true));
  340. return false;
  341. }
  342. else if ((begPosition.x < endPosition.x) && owner->creDir[stack->ID] == false)
  343. {
  344. owner->addNewAnim(new CReverseAnimation(owner, stack, curStackPos, true));
  345. return false;
  346. }
  347. if(myAnim()->getType() != CCreatureAnim::MOVING)
  348. {
  349. myAnim()->setType(CCreatureAnim::MOVING);
  350. }
  351. //unit reversed
  352. if (owner->moveSh == -1)
  353. {
  354. owner->moveSh = CCS->soundh->playSound(battle_sound(movedStack->getCreature(), move), -1);
  355. }
  356. //step shift calculation
  357. posX = myAnim()->pos.x, posY = myAnim()->pos.y; // for precise calculations ;]
  358. if(mutPos == -1 && movedStack->hasBonusOfType(Bonus::FLYING))
  359. {
  360. steps *= distance;
  361. steps /= 2; //to make animation faster
  362. stepX = (endPosition.x - begPosition.x) / static_cast<double>(steps);
  363. stepY = (endPosition.y - begPosition.y) / static_cast<double>(steps);
  364. }
  365. else
  366. {
  367. switch(mutPos)
  368. {
  369. case 0:
  370. stepX = -1.0 * (hexWbase / (2.0 * steps));
  371. stepY = -1.0 * (hexHbase / (static_cast<double>(steps)));
  372. break;
  373. case 1:
  374. stepX = hexWbase / (2.0 * steps);
  375. stepY = -1.0 * hexHbase / (static_cast<double>(steps));
  376. break;
  377. case 2:
  378. stepX = hexWbase / static_cast<double>(steps);
  379. stepY = 0.0;
  380. break;
  381. case 3:
  382. stepX = hexWbase / (2.0 * steps);
  383. stepY = hexHbase / static_cast<double>(steps);
  384. break;
  385. case 4:
  386. stepX = -1.0 * hexWbase / (2.0 * steps);
  387. stepY = hexHbase / static_cast<double>(steps);
  388. break;
  389. case 5:
  390. stepX = -1.0 * hexWbase / static_cast<double>(steps);
  391. stepY = 0.0;
  392. break;
  393. }
  394. }
  395. //step shifts calculated
  396. return true;
  397. }
  398. void CMovementAnimation::nextFrame()
  399. {
  400. //moving instructions
  401. posX += stepX;
  402. myAnim()->pos.x = static_cast<Sint16>(posX);
  403. posY += stepY;
  404. myAnim()->pos.y = static_cast<Sint16>(posY);
  405. // Increments step count and check if we are finished with current animation
  406. ++whichStep;
  407. if(whichStep == steps)
  408. {
  409. // Sets the position of the creature animation sprites
  410. Point coords = CClickableHex::getXYUnitAnim(nextHex, owner->creDir[stack->ID], stack, owner);
  411. myAnim()->pos = coords;
  412. // true if creature haven't reached the final destination hex
  413. if ((nextPos + 1) < destTiles.size())
  414. {
  415. // update the next hex field which has to be reached by the stack
  416. nextPos++;
  417. curStackPos = nextHex;
  418. nextHex = destTiles[nextPos];
  419. // update position of double wide creatures
  420. bool twoTiles = stack->doubleWide();
  421. if(twoTiles && bool(stack->attackerOwned) && (owner->creDir[stack->ID] != bool(stack->attackerOwned) )) //big attacker creature is reversed
  422. myAnim()->pos.x -= 44;
  423. else if(twoTiles && (! bool(stack->attackerOwned) ) && (owner->creDir[stack->ID] != bool(stack->attackerOwned) )) //big defender creature is reversed
  424. myAnim()->pos.x += 44;
  425. // re-init animation
  426. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  427. {
  428. if (it->first == this)
  429. {
  430. it->second = false;
  431. break;
  432. }
  433. }
  434. }
  435. else
  436. endAnim();
  437. }
  438. }
  439. void CMovementAnimation::endAnim()
  440. {
  441. const CStack * movedStack = stack;
  442. myAnim()->pos = CClickableHex::getXYUnitAnim(nextHex, movedStack->attackerOwned, movedStack, owner);
  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. if(!stack)
  573. {
  574. endAnim();
  575. return;
  576. }
  577. owner->creDir[stack->ID] = !owner->creDir[stack->ID];
  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 = owner->siegeH->town->town->clientInfo.siegeShooter;
  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. spi.spin = shooterInfo->projectileSpin;
  636. Point xycoord = CClickableHex::getXYUnitAnim(shooter->position, true, shooter, owner);
  637. Point destcoord;
  638. // The "master" point where all projectile positions relate to.
  639. static const Point projectileOrigin(181, 252);
  640. if (attackedStack)
  641. {
  642. destcoord = CClickableHex::getXYUnitAnim(dest, false, attackedStack, owner);
  643. destcoord.x += 250; destcoord.y += 210; //TODO: find a better place to shoot
  644. // Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
  645. if (projectileAngle > straightAngle)
  646. {
  647. //upper shot
  648. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->upperRightMissleOffsetX;
  649. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->upperRightMissleOffsetY;
  650. }
  651. else if (projectileAngle < -straightAngle)
  652. {
  653. //lower shot
  654. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->lowerRightMissleOffsetX;
  655. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->lowerRightMissleOffsetY;
  656. }
  657. else
  658. {
  659. //straight shot
  660. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->rightMissleOffsetX;
  661. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->rightMissleOffsetY;
  662. }
  663. double animSpeed = 23.0 * owner->getAnimSpeed(); // flight speed of projectile
  664. 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);
  665. if(spi.lastStep == 0)
  666. spi.lastStep = 1;
  667. spi.dx = (destcoord.x - spi.x) / spi.lastStep;
  668. spi.dy = (destcoord.y - spi.y) / spi.lastStep;
  669. spi.catapultInfo = 0;
  670. }
  671. else
  672. {
  673. // Catapult attack
  674. // These are the values for equations of this kind: f(x) = ax^2 + bx + c
  675. static const std::vector<CatapultProjectileInfo*> trajectoryCurves = boost::assign::list_of<CatapultProjectileInfo*>(new CatapultProjectileInfo(4.309, -3.198, 569.2, -296, 182))
  676. (new CatapultProjectileInfo(4.710, -3.11, 558.68, -258, 175))(new CatapultProjectileInfo(5.056, -3.003, 546.9, -236, 174))
  677. (new CatapultProjectileInfo(4.760, -2.74, 526.47, -216, 215))(new CatapultProjectileInfo(4.288, -2.496, 508.98, -223, 274))
  678. (new CatapultProjectileInfo(3.683, -3.018, 558.39, -324, 176))(new CatapultProjectileInfo(2.884, -2.607, 528.95, -366, 312))
  679. (new CatapultProjectileInfo(3.783, -2.364, 501.35, -227, 318));
  680. 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);
  681. std::map<int, int>::iterator it = hexToCurve.find(dest.hex);
  682. if (it == hexToCurve.end())
  683. {
  684. tlog1 << "For the hex position " << dest.hex << " is no curve defined.";
  685. endAnim();
  686. return false;
  687. }
  688. else
  689. {
  690. int curveID = it->second;
  691. spi.catapultInfo = trajectoryCurves[curveID];
  692. double animSpeed = 3.318 * owner->getAnimSpeed();
  693. spi.lastStep = static_cast<int>((spi.catapultInfo->toX - spi.catapultInfo->fromX) / animSpeed);
  694. spi.dx = animSpeed;
  695. spi.dy = 0;
  696. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->rightMissleOffsetX + 17.;
  697. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->rightMissleOffsetY + 10.;
  698. // Add explosion anim
  699. int xEnd = static_cast<int>(spi.x + spi.lastStep * spi.dx);
  700. int yEnd = static_cast<int>(spi.catapultInfo->calculateY(xEnd));
  701. owner->addNewAnim( new CSpellEffectAnimation(owner, catapultDamage ? "SGEXPL.DEF" : "CSGRCK.DEF", xEnd - 126, yEnd - 105));
  702. }
  703. }
  704. // Set starting frame
  705. if(spi.spin)
  706. {
  707. spi.frameNum = 0;
  708. }
  709. else
  710. {
  711. double pi = boost::math::constants::pi<double>();
  712. 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));
  713. }
  714. // Set projectile animation start delay which is specified in frames
  715. spi.animStartDelay = shooterInfo->attackClimaxFrame;
  716. owner->projectiles.push_back(spi);
  717. //attack animation
  718. shooting = true;
  719. if(projectileAngle > straightAngle) //upper shot
  720. group = CCreatureAnim::SHOOT_UP;
  721. else if(projectileAngle < -straightAngle) //lower shot
  722. group = CCreatureAnim::SHOOT_DOWN;
  723. else //straight shot
  724. group = CCreatureAnim::SHOOT_FRONT;
  725. return true;
  726. }
  727. void CShootingAnimation::nextFrame()
  728. {
  729. for(std::list<std::pair<CBattleAnimation *, bool> >::const_iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  730. {
  731. CMovementStartAnimation * anim = dynamic_cast<CMovementStartAnimation *>(it->first);
  732. CReverseAnimation * anim2 = dynamic_cast<CReverseAnimation *>(it->first);
  733. if( (anim && anim->stack->ID == stack->ID) || (anim2 && anim2->stack->ID == stack->ID && anim2->priority ) )
  734. return;
  735. }
  736. CAttackAnimation::nextFrame();
  737. }
  738. void CShootingAnimation::endAnim()
  739. {
  740. CBattleAnimation::endAnim();
  741. delete this;
  742. }
  743. CSpellEffectAnimation::CSpellEffectAnimation(CBattleInterface * _owner, ui32 _effect, BattleHex _destTile, int _dx, int _dy, bool _Vflip)
  744. :CBattleAnimation(_owner), effect(_effect), destTile(_destTile), customAnim(""), x(0), y(0), dx(_dx), dy(_dy), Vflip(_Vflip)
  745. {}
  746. CSpellEffectAnimation::CSpellEffectAnimation(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx, int _dy, bool _Vflip)
  747. :CBattleAnimation(_owner), effect(-1), destTile(0), customAnim(_customAnim), x(_x), y(_y), dx(_dx), dy(_dy), Vflip(_Vflip)
  748. {}
  749. bool CSpellEffectAnimation::init()
  750. {
  751. if(!isEarliest(true))
  752. return false;
  753. if(effect == 12) //armageddon
  754. {
  755. if(effect == -1 || graphics->battleACToDef[effect].size() != 0)
  756. {
  757. CDefHandler * anim;
  758. if(customAnim.size())
  759. anim = CDefHandler::giveDef(customAnim);
  760. else
  761. anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  762. if (Vflip)
  763. {
  764. for (size_t v = 0; v < anim->ourImages.size(); ++v)
  765. {
  766. CSDL_Ext::VflipSurf(anim->ourImages[v].bitmap);
  767. }
  768. }
  769. for(int i=0; i * anim->width < owner->pos.w ; ++i)
  770. {
  771. for(int j=0; j * anim->height < owner->pos.h ; ++j)
  772. {
  773. BattleEffect be;
  774. be.effectID = ID;
  775. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  776. if (Vflip)
  777. {
  778. for (size_t v = 0; v < be.anim->ourImages.size(); ++v)
  779. {
  780. CSDL_Ext::VflipSurf(be.anim->ourImages[v].bitmap);
  781. }
  782. }
  783. be.frame = 0;
  784. be.maxFrame = be.anim->ourImages.size();
  785. be.x = i * anim->width + owner->pos.x;
  786. be.y = j * anim->height + owner->pos.y;
  787. owner->battleEffects.push_back(be);
  788. }
  789. }
  790. }
  791. else //there is nothing to play
  792. {
  793. endAnim();
  794. return false;
  795. }
  796. }
  797. else // Effects targeted at a specific creature/hex.
  798. {
  799. if(effect == -1 || graphics->battleACToDef[effect].size() != 0)
  800. {
  801. const CStack* destStack = owner->curInt->cb->battleGetStackByPos(destTile, false);
  802. Rect &tilePos = owner->bfield[destTile]->pos;
  803. BattleEffect be;
  804. be.effectID = ID;
  805. if(customAnim.size())
  806. be.anim = CDefHandler::giveDef(customAnim);
  807. else
  808. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  809. if (Vflip)
  810. {
  811. for (size_t v = 0; v < be.anim->ourImages.size(); ++v)
  812. {
  813. CSDL_Ext::VflipSurf(be.anim->ourImages[v].bitmap);
  814. }
  815. }
  816. be.frame = 0;
  817. be.maxFrame = be.anim->ourImages.size();
  818. if(effect == 1)
  819. be.maxFrame = 3;
  820. switch (effect)
  821. {
  822. case ui32(-1):
  823. be.x = x;
  824. be.y = y;
  825. break;
  826. case 0: // Prayer and Lightning Bolt.
  827. case 1:
  828. // Position effect with it's bottom center touching the bottom center of affected tile(s).
  829. be.x = tilePos.x + tilePos.w/2 - be.anim->width/2;
  830. be.y = tilePos.y + tilePos.h - be.anim->height;
  831. break;
  832. default:
  833. // Position effect with it's center touching the top center of affected tile(s).
  834. be.x = tilePos.x + tilePos.w/2 - be.anim->width/2;
  835. be.y = tilePos.y - be.anim->height/2;
  836. break;
  837. }
  838. // Correction for 2-hex creatures.
  839. if (destStack != NULL && destStack->doubleWide())
  840. be.x += (destStack->attackerOwned ? -1 : 1)*tilePos.w/2;
  841. owner->battleEffects.push_back(be);
  842. }
  843. else //there is nothing to play
  844. {
  845. endAnim();
  846. return false;
  847. }
  848. }
  849. //battleEffects
  850. return true;
  851. }
  852. void CSpellEffectAnimation::nextFrame()
  853. {
  854. //notice: there may be more than one effect in owner->battleEffects correcponding to this animation (ie. armageddon)
  855. for(std::list<BattleEffect>::iterator it = owner->battleEffects.begin(); it != owner->battleEffects.end(); ++it)
  856. {
  857. if(it->effectID == ID)
  858. {
  859. ++(it->frame);
  860. if(it->frame == it->maxFrame)
  861. {
  862. endAnim();
  863. break;
  864. }
  865. else
  866. {
  867. it->x += dx;
  868. it->y += dy;
  869. }
  870. }
  871. }
  872. }
  873. void CSpellEffectAnimation::endAnim()
  874. {
  875. CBattleAnimation::endAnim();
  876. std::vector<std::list<BattleEffect>::iterator> toDel;
  877. for(std::list<BattleEffect>::iterator it = owner->battleEffects.begin(); it != owner->battleEffects.end(); ++it)
  878. {
  879. if(it->effectID == ID)
  880. {
  881. toDel.push_back(it);
  882. }
  883. }
  884. for(size_t b = 0; b < toDel.size(); ++b)
  885. {
  886. delete toDel[b]->anim;
  887. owner->battleEffects.erase(toDel[b]);
  888. }
  889. delete this;
  890. }