2
0

CBattleAnimations.cpp 29 KB

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