CBattleAnimations.cpp 30 KB

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