| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159 | 
							- /*
 
-  * CBattleAnimations.cpp, part of VCMI engine
 
-  *
 
-  * Authors: listed in file AUTHORS in main folder
 
-  *
 
-  * License: GNU General Public License v2.0 or later
 
-  * Full text of license available in license.txt file, in main folder
 
-  *
 
-  */
 
- #include "StdInc.h"
 
- #include "CBattleAnimations.h"
 
- #include <boost/math/constants/constants.hpp>
 
- #include "CBattleInterfaceClasses.h"
 
- #include "CBattleInterface.h"
 
- #include "CBattleProjectileController.h"
 
- #include "CBattleSiegeController.h"
 
- #include "CBattleFieldController.h"
 
- #include "CBattleEffectsController.h"
 
- #include "CBattleStacksController.h"
 
- #include "CCreatureAnimation.h"
 
- #include "../CGameInfo.h"
 
- #include "../CMusicHandler.h"
 
- #include "../CPlayerInterface.h"
 
- #include "../Graphics.h"
 
- #include "../gui/CAnimation.h"
 
- #include "../gui/CCursorHandler.h"
 
- #include "../gui/CGuiHandler.h"
 
- #include "../../CCallback.h"
 
- #include "../../lib/CStack.h"
 
- #include "../../lib/CTownHandler.h"
 
- #include "../../lib/mapObjects/CGTownInstance.h"
 
- CBattleAnimation::CBattleAnimation(CBattleInterface * _owner)
 
- 	: owner(_owner),
 
- 	  ID(_owner->stacksController->animIDhelper++),
 
- 	  initialized(false)
 
- {
 
- 	logAnim->trace("Animation #%d created", ID);
 
- }
 
- bool CBattleAnimation::tryInitialize()
 
- {
 
- 	assert(!initialized);
 
- 	if ( init() )
 
- 	{
 
- 		initialized = true;
 
- 		return true;
 
- 	}
 
- 	return false;
 
- }
 
- bool CBattleAnimation::isInitialized()
 
- {
 
- 	return initialized;
 
- }
 
- CBattleAnimation::~CBattleAnimation()
 
- {
 
- 	logAnim->trace("Animation #%d ended, type is %s", ID, typeid(this).name());
 
- 	for(auto & elem : pendingAnimations())
 
- 	{
 
- 		if(elem == this)
 
- 			elem = nullptr;
 
- 	}
 
- 	logAnim->trace("Animation #%d deleted", ID);
 
- }
 
- std::vector<CBattleAnimation *> & CBattleAnimation::pendingAnimations()
 
- {
 
- 	return owner->stacksController->currentAnimations;
 
- }
 
- std::shared_ptr<CCreatureAnimation> CBattleAnimation::stackAnimation(const CStack * stack)
 
- {
 
- 	return owner->stacksController->stackAnimation[stack->ID];
 
- }
 
- bool CBattleAnimation::stackFacingRight(const CStack * stack)
 
- {
 
- 	return owner->stacksController->stackFacingRight[stack->ID];
 
- }
 
- void CBattleAnimation::setStackFacingRight(const CStack * stack, bool facingRight)
 
- {
 
- 	owner->stacksController->stackFacingRight[stack->ID] = facingRight;
 
- }
 
- bool CBattleAnimation::checkInitialConditions()
 
- {
 
- 	int lowestMoveID = ID;
 
- 	CBattleStackAnimation * thAnim = dynamic_cast<CBattleStackAnimation *>(this);
 
- 	CEffectAnimation * thSen = dynamic_cast<CEffectAnimation *>(this);
 
- 	for(auto & elem : pendingAnimations())
 
- 	{
 
- 		CEffectAnimation * sen = dynamic_cast<CEffectAnimation *>(elem);
 
- 		// all effect animations can play concurrently with each other
 
- 		if(sen && thSen && sen != thSen)
 
- 			continue;
 
- 		CReverseAnimation * revAnim = dynamic_cast<CReverseAnimation *>(elem);
 
- 		// if there is high-priority reverse animation affecting our stack then this animation will wait
 
- 		if(revAnim && thAnim && revAnim && revAnim->stack->ID == thAnim->stack->ID && revAnim->priority)
 
- 			return false;
 
- 		if(elem)
 
- 			vstd::amin(lowestMoveID, elem->ID);
 
- 	}
 
- 	return ID == lowestMoveID;
 
- }
 
- CBattleStackAnimation::CBattleStackAnimation(CBattleInterface * owner, const CStack * stack)
 
- 	: CBattleAnimation(owner),
 
- 	  myAnim(stackAnimation(stack)),
 
- 	  stack(stack)
 
- {
 
- 	assert(myAnim);
 
- }
 
- void CBattleStackAnimation::shiftColor(const ColorShifter * shifter)
 
- {
 
- 	assert(myAnim);
 
- 	myAnim->shiftColor(shifter);
 
- }
 
- void CAttackAnimation::nextFrame()
 
- {
 
- 	if(myAnim->getType() != group)
 
- 	{
 
- 		myAnim->setType(group);
 
- 		myAnim->onAnimationReset += [&](){ delete this; };
 
- 	}
 
- 	if(!soundPlayed)
 
- 	{
 
- 		if(shooting)
 
- 			CCS->soundh->playSound(battle_sound(getCreature(), shoot));
 
- 		else
 
- 			CCS->soundh->playSound(battle_sound(getCreature(), attack));
 
- 		soundPlayed = true;
 
- 	}
 
- }
 
- CAttackAnimation::~CAttackAnimation()
 
- {
 
- 	myAnim->setType(CCreatureAnim::HOLDING);
 
- }
 
- bool CAttackAnimation::checkInitialConditions()
 
- {
 
- 	for(auto & elem : pendingAnimations())
 
- 	{
 
- 		CBattleStackAnimation * stAnim = dynamic_cast<CBattleStackAnimation *>(elem);
 
- 		CReverseAnimation * revAnim = dynamic_cast<CReverseAnimation *>(stAnim);
 
- 		if(revAnim && attackedStack) // enemy must be fully reversed
 
- 		{
 
- 			if (revAnim->stack->ID == attackedStack->ID)
 
- 				return false;
 
- 		}
 
- 	}
 
- 	return CBattleAnimation::checkInitialConditions();
 
- }
 
- const CCreature * CAttackAnimation::getCreature()
 
- {
 
- 	if (attackingStack->getCreature()->idNumber == CreatureID::ARROW_TOWERS)
 
- 		return owner->siegeController->getTurretCreature();
 
- 	else
 
- 		return attackingStack->getCreature();
 
- }
 
- CAttackAnimation::CAttackAnimation(CBattleInterface *_owner, const CStack *attacker, BattleHex _dest, const CStack *defender)
 
- 	: CBattleStackAnimation(_owner, attacker),
 
- 	  shooting(false),
 
- 	  group(CCreatureAnim::SHOOT_FRONT),
 
- 	  soundPlayed(false),
 
- 	  dest(_dest),
 
- 	  attackedStack(defender),
 
- 	  attackingStack(attacker)
 
- {
 
- 	assert(attackingStack && "attackingStack is nullptr in CBattleAttack::CBattleAttack !\n");
 
- 	attackingStackPosBeforeReturn = attackingStack->getPosition();
 
- }
 
- CDefenceAnimation::CDefenceAnimation(StackAttackedInfo _attackedInfo, CBattleInterface * _owner)
 
- 	: CBattleStackAnimation(_owner, _attackedInfo.defender),
 
- 	  attacker(_attackedInfo.attacker),
 
- 	  rangedAttack(_attackedInfo.indirectAttack),
 
- 	  killed(_attackedInfo.killed),
 
- 	  timeToWait(0)
 
- {
 
- 	logAnim->debug("Created defence anim for %s", _attackedInfo.defender->getName());
 
- }
 
- bool CDefenceAnimation::init()
 
- {
 
- 	ui32 lowestMoveID = ID;
 
- 	for(auto & elem : pendingAnimations())
 
- 	{
 
- 		CDefenceAnimation * defAnim = dynamic_cast<CDefenceAnimation *>(elem);
 
- 		if(defAnim && defAnim->stack->ID != stack->ID)
 
- 			continue;
 
- 		CAttackAnimation * attAnim = dynamic_cast<CAttackAnimation *>(elem);
 
- 		if(attAnim && attAnim->stack->ID != stack->ID)
 
- 			continue;
 
- 		CEffectAnimation * sen = dynamic_cast<CEffectAnimation *>(elem);
 
- 		if (sen && attacker == nullptr)
 
- 			return false;
 
- 		if (sen)
 
- 			continue;
 
- 		CReverseAnimation * animAsRev = dynamic_cast<CReverseAnimation *>(elem);
 
- 		if(animAsRev)
 
- 			return false;
 
- 		if(elem)
 
- 			vstd::amin(lowestMoveID, elem->ID);
 
- 	}
 
- 	if(ID > lowestMoveID)
 
- 		return false;
 
- 	//reverse unit if necessary
 
- 	if(attacker && owner->getCurrentPlayerInterface()->cb->isToReverse(stack->getPosition(), attacker->getPosition(), stackFacingRight(stack), attacker->doubleWide(), stackFacingRight(attacker)))
 
- 	{
 
- 		owner->stacksController->addNewAnim(new CReverseAnimation(owner, stack, stack->getPosition(), true));
 
- 		return false;
 
- 	}
 
- 	//unit reversed
 
- 	if(rangedAttack && attacker != nullptr && owner->projectilesController->hasActiveProjectile(attacker)) //delay hit animation
 
- 	{
 
- 		return false;
 
- 	}
 
- 	// synchronize animation with attacker, unless defending or attacked by shooter:
 
- 	// wait for 1/2 of attack animation
 
- 	if (!rangedAttack && getMyAnimType() != CCreatureAnim::DEFENCE)
 
- 	{
 
- 		float frameLength = AnimationControls::getCreatureAnimationSpeed(
 
- 								  stack->getCreature(), stackAnimation(stack).get(), getMyAnimType());
 
- 		timeToWait = myAnim->framesInGroup(getMyAnimType()) * frameLength / 2;
 
- 		myAnim->setType(CCreatureAnim::HOLDING);
 
- 	}
 
- 	else
 
- 	{
 
- 		timeToWait = 0;
 
- 		startAnimation();
 
- 	}
 
- 	return true; //initialized successfuly
 
- }
 
- std::string CDefenceAnimation::getMySound()
 
- {
 
- 	if(killed)
 
- 		return battle_sound(stack->getCreature(), killed);
 
- 	else if(stack->defendingAnim)
 
- 		return battle_sound(stack->getCreature(), defend);
 
- 	else
 
- 		return battle_sound(stack->getCreature(), wince);
 
- }
 
- CCreatureAnim::EAnimType CDefenceAnimation::getMyAnimType()
 
- {
 
- 	if(killed)
 
- 	{
 
- 		if(rangedAttack && myAnim->framesInGroup(CCreatureAnim::DEATH_RANGED) > 0)
 
- 			return CCreatureAnim::DEATH_RANGED;
 
- 		else
 
- 			return CCreatureAnim::DEATH;
 
- 	}
 
- 	if(stack->defendingAnim)
 
- 		return CCreatureAnim::DEFENCE;
 
- 	else
 
- 		return CCreatureAnim::HITTED;
 
- }
 
- void CDefenceAnimation::startAnimation()
 
- {
 
- 	CCS->soundh->playSound(getMySound());
 
- 	myAnim->setType(getMyAnimType());
 
- 	myAnim->onAnimationReset += [&](){ delete this; };
 
- }
 
- void CDefenceAnimation::nextFrame()
 
- {
 
- 	if (timeToWait > 0)
 
- 	{
 
- 		timeToWait -= float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000;
 
- 		if (timeToWait <= 0)
 
- 			startAnimation();
 
- 	}
 
- 	CBattleAnimation::nextFrame();
 
- }
 
- CDefenceAnimation::~CDefenceAnimation()
 
- {
 
- 	if(killed)
 
- 	{
 
- 		if(rangedAttack && myAnim->framesInGroup(CCreatureAnim::DEAD_RANGED) > 0)
 
- 			myAnim->setType(CCreatureAnim::DEAD_RANGED);
 
- 		else
 
- 			myAnim->setType(CCreatureAnim::DEAD);
 
- 	}
 
- 	else
 
- 	{
 
- 		myAnim->setType(CCreatureAnim::HOLDING);
 
- 	}
 
- }
 
- CDummyAnimation::CDummyAnimation(CBattleInterface * _owner, int howManyFrames)
 
- 	: CBattleAnimation(_owner),
 
- 	  counter(0),
 
- 	  howMany(howManyFrames)
 
- {
 
- 	logAnim->debug("Created dummy animation for %d frames", howManyFrames);
 
- }
 
- bool CDummyAnimation::init()
 
- {
 
- 	return true;
 
- }
 
- void CDummyAnimation::nextFrame()
 
- {
 
- 	counter++;
 
- 	if(counter > howMany)
 
- 		delete this;
 
- }
 
- bool CMeleeAttackAnimation::init()
 
- {
 
- 	if(!CAttackAnimation::checkInitialConditions())
 
- 		return false;
 
- 	if(!attackingStack || myAnim->isDeadOrDying())
 
- 	{
 
- 		delete this;
 
- 		return false;
 
- 	}
 
- 	bool toReverse = owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStackPosBeforeReturn, attackedStack->getPosition(), stackFacingRight(stack), attackedStack->doubleWide(), stackFacingRight(attackedStack));
 
- 	if(toReverse)
 
- 	{
 
- 		owner->stacksController->addNewAnim(new CReverseAnimation(owner, stack, attackingStackPosBeforeReturn, true));
 
- 		return false;
 
- 	}
 
- 	// opponent must face attacker ( = different directions) before he can be attacked
 
- 	if(attackingStack && attackedStack &&
 
- 		stackFacingRight(attackingStack) == stackFacingRight(attackedStack))
 
- 		return false;
 
- 	//reversed
 
- 	shooting = false;
 
- 	static const CCreatureAnim::EAnimType mutPosToGroup[] =
 
- 	{
 
- 		CCreatureAnim::ATTACK_UP,
 
- 		CCreatureAnim::ATTACK_UP,
 
- 		CCreatureAnim::ATTACK_FRONT,
 
- 		CCreatureAnim::ATTACK_DOWN,
 
- 		CCreatureAnim::ATTACK_DOWN,
 
- 		CCreatureAnim::ATTACK_FRONT
 
- 	};
 
- 	static const CCreatureAnim::EAnimType mutPosToGroup2H[] =
 
- 	{
 
- 		CCreatureAnim::VCMI_2HEX_UP,
 
- 		CCreatureAnim::VCMI_2HEX_UP,
 
- 		CCreatureAnim::VCMI_2HEX_FRONT,
 
- 		CCreatureAnim::VCMI_2HEX_DOWN,
 
- 		CCreatureAnim::VCMI_2HEX_DOWN,
 
- 		CCreatureAnim::VCMI_2HEX_FRONT
 
- 	};
 
- 	int revShiftattacker = (attackingStack->side == BattleSide::ATTACKER ? -1 : 1);
 
- 	int mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, dest);
 
- 	if(mutPos == -1 && attackingStack->doubleWide())
 
- 	{
 
- 		mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, attackedStack->getPosition());
 
- 	}
 
- 	if (mutPos == -1 && attackedStack->doubleWide())
 
- 	{
 
- 		mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, attackedStack->occupiedHex());
 
- 	}
 
- 	if (mutPos == -1 && attackedStack->doubleWide() && attackingStack->doubleWide())
 
- 	{
 
- 		mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, attackedStack->occupiedHex());
 
- 	}
 
- 	switch(mutPos) //attack direction
 
- 	{
 
- 	case 0:
 
- 	case 1:
 
- 	case 2:
 
- 	case 3:
 
- 	case 4:
 
- 	case 5:
 
- 		group = mutPosToGroup[mutPos];
 
- 		if(attackingStack->hasBonusOfType(Bonus::TWO_HEX_ATTACK_BREATH))
 
- 		{
 
- 			CCreatureAnim::EAnimType group2H = mutPosToGroup2H[mutPos];
 
- 			if(myAnim->framesInGroup(group2H)>0)
 
- 				group = group2H;
 
- 		}
 
- 		break;
 
- 	default:
 
- 		logGlobal->error("Critical Error! Wrong dest in stackAttacking! dest: %d; attacking stack pos: %d; mutual pos: %d", dest.hex, attackingStackPosBeforeReturn, mutPos);
 
- 		group = CCreatureAnim::ATTACK_FRONT;
 
- 		break;
 
- 	}
 
- 	return true;
 
- }
 
- CMeleeAttackAnimation::CMeleeAttackAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked)
 
- 	: CAttackAnimation(_owner, attacker, _dest, _attacked)
 
- {
 
- 	logAnim->debug("Created melee attack anim for %s", attacker->getName());
 
- }
 
- CStackMoveAnimation::CStackMoveAnimation(CBattleInterface * _owner, const CStack * _stack, BattleHex _currentHex):
 
- 	CBattleStackAnimation(_owner, _stack),
 
- 	currentHex(_currentHex)
 
- {
 
- }
 
- bool CMovementAnimation::init()
 
- {
 
- 	if( !CBattleAnimation::checkInitialConditions() )
 
- 		return false;
 
- 	if(!stack || myAnim->isDeadOrDying())
 
- 	{
 
- 		delete this;
 
- 		return false;
 
- 	}
 
- 	if(stackAnimation(stack)->framesInGroup(CCreatureAnim::MOVING) == 0 ||
 
- 	   stack->hasBonus(Selector::typeSubtype(Bonus::FLYING, 1)))
 
- 	{
 
- 		//no movement or teleport, end immediately
 
- 		delete this;
 
- 		return false;
 
- 	}
 
- 	//reverse unit if necessary
 
- 	if(owner->stacksController->shouldRotate(stack, oldPos, currentHex))
 
- 	{
 
- 		// it seems that H3 does NOT plays full rotation animation here in most situations
 
- 		// Logical since it takes quite a lot of time
 
- 		if (curentMoveIndex == 0) // full rotation only for moving towards first tile.
 
- 		{
 
- 			owner->stacksController->addNewAnim(new CReverseAnimation(owner, stack, oldPos, true));
 
- 			return false;
 
- 		}
 
- 		else
 
- 		{
 
- 			rotateStack(oldPos);
 
- 		}
 
- 	}
 
- 	if(myAnim->getType() != CCreatureAnim::MOVING)
 
- 	{
 
- 		myAnim->setType(CCreatureAnim::MOVING);
 
- 	}
 
- 	if (owner->moveSoundHander == -1)
 
- 	{
 
- 		owner->moveSoundHander = CCS->soundh->playSound(battle_sound(stack->getCreature(), move), -1);
 
- 	}
 
- 	Point begPosition = owner->stacksController->getStackPositionAtHex(oldPos, stack);
 
- 	Point endPosition = owner->stacksController->getStackPositionAtHex(currentHex, stack);
 
- 	timeToMove = AnimationControls::getMovementDuration(stack->getCreature());
 
- 	begX = begPosition.x;
 
- 	begY = begPosition.y;
 
- 	progress = 0;
 
- 	distanceX = endPosition.x - begPosition.x;
 
- 	distanceY = endPosition.y - begPosition.y;
 
- 	if (stack->hasBonus(Selector::type()(Bonus::FLYING)))
 
- 	{
 
- 		float distance = static_cast<float>(sqrt(distanceX * distanceX + distanceY * distanceY));
 
- 		timeToMove *= AnimationControls::getFlightDistance(stack->getCreature()) / distance;
 
- 	}
 
- 	return true;
 
- }
 
- void CMovementAnimation::nextFrame()
 
- {
 
- 	progress += float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000 * timeToMove;
 
- 	//moving instructions
 
- 	myAnim->pos.x = static_cast<Sint16>(begX + distanceX * progress );
 
- 	myAnim->pos.y = static_cast<Sint16>(begY + distanceY * progress );
 
- 	CBattleAnimation::nextFrame();
 
- 	if(progress >= 1.0)
 
- 	{
 
- 		// Sets the position of the creature animation sprites
 
- 		Point coords = owner->stacksController->getStackPositionAtHex(currentHex, stack);
 
- 		myAnim->pos = coords;
 
- 		// true if creature haven't reached the final destination hex
 
- 		if ((curentMoveIndex + 1) < destTiles.size())
 
- 		{
 
- 			// update the next hex field which has to be reached by the stack
 
- 			curentMoveIndex++;
 
- 			oldPos = currentHex;
 
- 			currentHex = destTiles[curentMoveIndex];
 
- 			// request re-initialization
 
- 			initialized = false;
 
- 		}
 
- 		else
 
- 			delete this;
 
- 	}
 
- }
 
- CMovementAnimation::~CMovementAnimation()
 
- {
 
- 	assert(stack);
 
- 	myAnim->pos = owner->stacksController->getStackPositionAtHex(currentHex, stack);
 
- 	owner->stacksController->addNewAnim(new CMovementEndAnimation(owner, stack, currentHex));
 
- 	if(owner->moveSoundHander != -1)
 
- 	{
 
- 		CCS->soundh->stopSound(owner->moveSoundHander);
 
- 		owner->moveSoundHander = -1;
 
- 	}
 
- }
 
- CMovementAnimation::CMovementAnimation(CBattleInterface *_owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance)
 
- 	: CStackMoveAnimation(_owner, _stack, _destTiles.front()),
 
- 	  destTiles(_destTiles),
 
- 	  curentMoveIndex(0),
 
- 	  oldPos(stack->getPosition()),
 
- 	  begX(0), begY(0),
 
- 	  distanceX(0), distanceY(0),
 
- 	  timeToMove(0.0),
 
- 	  progress(0.0)
 
- {
 
- 	logAnim->debug("Created movement anim for %s", stack->getName());
 
- }
 
- CMovementEndAnimation::CMovementEndAnimation(CBattleInterface * _owner, const CStack * _stack, BattleHex destTile)
 
- : CStackMoveAnimation(_owner, _stack, destTile)
 
- {
 
- 	logAnim->debug("Created movement end anim for %s", stack->getName());
 
- }
 
- bool CMovementEndAnimation::init()
 
- {
 
- 	//if( !isEarliest(true) )
 
- 	//	return false;
 
- 	if(!stack || myAnim->framesInGroup(CCreatureAnim::MOVE_END) == 0 ||
 
- 		myAnim->isDeadOrDying())
 
- 	{
 
- 		delete this;
 
- 		return false;
 
- 	}
 
- 	CCS->soundh->playSound(battle_sound(stack->getCreature(), endMoving));
 
- 	myAnim->setType(CCreatureAnim::MOVE_END);
 
- 	myAnim->onAnimationReset += [&](){ delete this; };
 
- 	return true;
 
- }
 
- CMovementEndAnimation::~CMovementEndAnimation()
 
- {
 
- 	if(myAnim->getType() != CCreatureAnim::DEAD)
 
- 		myAnim->setType(CCreatureAnim::HOLDING); //resetting to default
 
- 	CCS->curh->show();
 
- }
 
- CMovementStartAnimation::CMovementStartAnimation(CBattleInterface * _owner, const CStack * _stack)
 
- 	: CStackMoveAnimation(_owner, _stack, _stack->getPosition())
 
- {
 
- 	logAnim->debug("Created movement start anim for %s", stack->getName());
 
- }
 
- bool CMovementStartAnimation::init()
 
- {
 
- 	if( !CBattleAnimation::checkInitialConditions() )
 
- 		return false;
 
- 	if(!stack || myAnim->isDeadOrDying())
 
- 	{
 
- 		delete this;
 
- 		return false;
 
- 	}
 
- 	CCS->soundh->playSound(battle_sound(stack->getCreature(), startMoving));
 
- 	myAnim->setType(CCreatureAnim::MOVE_START);
 
- 	myAnim->onAnimationReset += [&](){ delete this; };
 
- 	return true;
 
- }
 
- CReverseAnimation::CReverseAnimation(CBattleInterface * _owner, const CStack * stack, BattleHex dest, bool _priority)
 
- 	: CStackMoveAnimation(_owner, stack, dest),
 
- 	  priority(_priority)
 
- {
 
- 	logAnim->debug("Created reverse anim for %s", stack->getName());
 
- }
 
- bool CReverseAnimation::init()
 
- {
 
- 	if(myAnim == nullptr || myAnim->isDeadOrDying())
 
- 	{
 
- 		delete this;
 
- 		return false; //there is no such creature
 
- 	}
 
- 	if(!priority && !CBattleAnimation::checkInitialConditions())
 
- 		return false;
 
- 	if(myAnim->framesInGroup(CCreatureAnim::TURN_L))
 
- 	{
 
- 		myAnim->setType(CCreatureAnim::TURN_L);
 
- 		myAnim->onAnimationReset += std::bind(&CReverseAnimation::setupSecondPart, this);
 
- 	}
 
- 	else
 
- 	{
 
- 		setupSecondPart();
 
- 	}
 
- 	return true;
 
- }
 
- CReverseAnimation::~CReverseAnimation()
 
- {
 
- 	if( stack && stack->alive() )//don't do that if stack is dead
 
- 		myAnim->setType(CCreatureAnim::HOLDING);
 
- }
 
- void CBattleStackAnimation::rotateStack(BattleHex hex)
 
- {
 
- 	setStackFacingRight(stack, !stackFacingRight(stack));
 
- 	stackAnimation(stack)->pos = owner->stacksController->getStackPositionAtHex(hex, stack);
 
- }
 
- void CReverseAnimation::setupSecondPart()
 
- {
 
- 	if(!stack)
 
- 	{
 
- 		delete this;
 
- 		return;
 
- 	}
 
- 	rotateStack(currentHex);
 
- 	if(myAnim->framesInGroup(CCreatureAnim::TURN_R))
 
- 	{
 
- 		myAnim->setType(CCreatureAnim::TURN_R);
 
- 		myAnim->onAnimationReset += [&](){ delete this; };
 
- 	}
 
- 	else
 
- 		delete this;
 
- }
 
- CRangedAttackAnimation::CRangedAttackAnimation(CBattleInterface * owner_, const CStack * attacker, BattleHex dest_, const CStack * defender)
 
- 	: CAttackAnimation(owner_, attacker, dest_, defender)
 
- {
 
- }
 
- CShootingAnimation::CShootingAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, bool _catapult, int _catapultDmg)
 
- 	: CRangedAttackAnimation(_owner, attacker, _dest, _attacked),
 
- 	catapultDamage(_catapultDmg),
 
- 	projectileEmitted(false),
 
- 	explosionEmitted(false)
 
- {
 
- 	logAnim->debug("Created shooting anim for %s", stack->getName());
 
- }
 
- bool CShootingAnimation::init()
 
- {
 
- 	if( !CAttackAnimation::checkInitialConditions() )
 
- 		return false;
 
- 	assert(attackingStack);
 
- 	assert(!myAnim->isDeadOrDying());
 
- 	if(!attackingStack || myAnim->isDeadOrDying())
 
- 	{
 
- 		//FIXME: how is this possible?
 
- 		logAnim->warn("Shooting animation has not started yet but attacker is dead! Aborting...");
 
- 		delete this;
 
- 		return false;
 
- 	}
 
- 	//reverse unit if necessary
 
- 	if (attackingStack && attackedStack && owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStack->getPosition(), attackedStack->getPosition(), stackFacingRight(attackingStack), attackingStack->doubleWide(), stackFacingRight(attackedStack)))
 
- 	{
 
- 		owner->stacksController->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->getPosition(), true));
 
- 		return false;
 
- 	}
 
- 	setAnimationGroup();
 
- 	initializeProjectile();
 
- 	shooting = true;
 
- 	return true;
 
- }
 
- void CShootingAnimation::setAnimationGroup()
 
- {
 
- 	Point shooterPos = stackAnimation(attackingStack)->pos.topLeft();
 
- 	Point shotTarget = owner->stacksController->getStackPositionAtHex(dest, attackedStack) + Point(225, 225);
 
- 	//maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
 
- 	static const double straightAngle = 0.2;
 
- 	double projectileAngle = -atan2(shotTarget.y - shooterPos.y, std::abs(shotTarget.x - shooterPos.x));
 
- 	// Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
 
- 	if (projectileAngle > straightAngle)
 
- 		group = CCreatureAnim::SHOOT_UP;
 
- 	else if (projectileAngle < -straightAngle)
 
- 		group = CCreatureAnim::SHOOT_DOWN;
 
- 	else
 
- 		group = CCreatureAnim::SHOOT_FRONT;
 
- }
 
- void CShootingAnimation::initializeProjectile()
 
- {
 
- 	const CCreature *shooterInfo = getCreature();
 
- 	Point shotTarget = owner->stacksController->getStackPositionAtHex(dest, attackedStack) + Point(225, 225);
 
- 	Point shotOrigin = stackAnimation(attackingStack)->pos.topLeft() + Point(222, 265);
 
- 	int multiplier = stackFacingRight(attackingStack) ? 1 : -1;
 
- 	if (group == CCreatureAnim::SHOOT_UP)
 
- 	{
 
- 		shotOrigin.x += ( -25 + shooterInfo->animation.upperRightMissleOffsetX ) * multiplier;
 
- 		shotOrigin.y += shooterInfo->animation.upperRightMissleOffsetY;
 
- 	}
 
- 	else if (group == CCreatureAnim::SHOOT_DOWN)
 
- 	{
 
- 		shotOrigin.x += ( -25 + shooterInfo->animation.lowerRightMissleOffsetX ) * multiplier;
 
- 		shotOrigin.y += shooterInfo->animation.lowerRightMissleOffsetY;
 
- 	}
 
- 	else if (group == CCreatureAnim::SHOOT_FRONT)
 
- 	{
 
- 		shotOrigin.x += ( -25 + shooterInfo->animation.rightMissleOffsetX ) * multiplier;
 
- 		shotOrigin.y += shooterInfo->animation.rightMissleOffsetY;
 
- 	}
 
- 	else
 
- 	{
 
- 		assert(0);
 
- 	}
 
- 	owner->projectilesController->createProjectile(attackingStack, attackedStack, shotOrigin, shotTarget);
 
- }
 
- void CShootingAnimation::emitProjectile()
 
- {
 
- 	owner->projectilesController->emitStackProjectile(attackingStack);
 
- 	projectileEmitted = true;
 
- }
 
- void CShootingAnimation::nextFrame()
 
- {
 
- 	for(auto & it : pendingAnimations())
 
- 	{
 
- 		CMovementStartAnimation * anim = dynamic_cast<CMovementStartAnimation *>(it);
 
- 		CReverseAnimation * anim2 = dynamic_cast<CReverseAnimation *>(it);
 
- 		if( (anim && anim->stack->ID == stack->ID) || (anim2 && anim2->stack->ID == stack->ID && anim2->priority ) )
 
- 			return;
 
- 	}
 
- 	// animation should be paused if there is an active projectile
 
- 	if (projectileEmitted)
 
- 	{
 
- 		if (owner->projectilesController->hasActiveProjectile(attackingStack))
 
- 		{
 
- 			stackAnimation(attackingStack)->pause();
 
- 			return;
 
- 		}
 
- 		else
 
- 		{
 
- 			stackAnimation(attackingStack)->play();
 
- 			emitExplosion();
 
- 		}
 
- 	}
 
- 	CAttackAnimation::nextFrame();
 
- 	if (!projectileEmitted)
 
- 	{
 
- 		const CCreature *shooterInfo = getCreature();
 
- 		assert(stackAnimation(attackingStack)->isShooting());
 
- 		// emit projectile once animation playback reached "climax" frame
 
- 		if ( stackAnimation(attackingStack)->getCurrentFrame() >= shooterInfo->animation.attackClimaxFrame )
 
- 		{
 
- 			emitProjectile();
 
- 			return;
 
- 		}
 
- 	}
 
- }
 
- void CShootingAnimation::emitExplosion()
 
- {
 
- 	if (attackedStack)
 
- 		return;
 
- 	if (explosionEmitted)
 
- 		return;
 
- 	explosionEmitted = true;
 
- 	Point shotTarget = owner->stacksController->getStackPositionAtHex(dest, attackedStack) + Point(225, 225) - Point(126, 105);
 
- 	owner->stacksController->addNewAnim( new CEffectAnimation(owner, catapultDamage ? "SGEXPL.DEF" : "CSGRCK.DEF", shotTarget.x, shotTarget.y));
 
- 	if(catapultDamage > 0)
 
- 	{
 
- 		CCS->soundh->playSound("WALLHIT");
 
- 	}
 
- 	else
 
- 	{
 
- 		CCS->soundh->playSound("WALLMISS");
 
- 	}
 
- }
 
- CShootingAnimation::~CShootingAnimation()
 
- {
 
- 	assert(!owner->projectilesController->hasActiveProjectile(attackingStack));
 
- 	assert(projectileEmitted);
 
- 	// FIXME: is this possible? Animation is over but we're yet to fire projectile?
 
- 	if (!projectileEmitted)
 
- 	{
 
- 		logAnim->warn("Shooting animation has finished but projectile was not emitted! Emitting it now...");
 
- 		emitProjectile();
 
- 	}
 
- }
 
- CCastAnimation::CCastAnimation(CBattleInterface * owner_, const CStack * attacker, BattleHex dest_, const CStack * defender)
 
- 	: CRangedAttackAnimation(owner_, attacker, dest_, defender)
 
- {
 
- 	if(!dest_.isValid() && defender)
 
- 		dest = defender->getPosition();
 
- }
 
- bool CCastAnimation::init()
 
- {
 
- 	if(!CAttackAnimation::checkInitialConditions())
 
- 		return false;
 
- 	if(!attackingStack || myAnim->isDeadOrDying())
 
- 	{
 
- 		delete this;
 
- 		return false;
 
- 	}
 
- 	//reverse unit if necessary
 
- 	if(attackedStack)
 
- 	{
 
- 		if(owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStack->getPosition(), attackedStack->getPosition(), stackFacingRight(attackingStack), attackingStack->doubleWide(), stackFacingRight(attackedStack)))
 
- 		{
 
- 			owner->stacksController->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->getPosition(), true));
 
- 			return false;
 
- 		}
 
- 	}
 
- 	else
 
- 	{
 
- 		if(dest.isValid() && owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStack->getPosition(), dest, stackFacingRight(attackingStack), false, false))
 
- 		{
 
- 			owner->stacksController->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->getPosition(), true));
 
- 			return false;
 
- 		}
 
- 	}
 
- 	//TODO: display spell projectile here
 
- 	static const double straightAngle = 0.2;
 
- 	Point fromPos;
 
- 	Point destPos;
 
- 	// NOTE: two lines below return different positions (very notable with 2-hex creatures). Obtaining via creanims seems to be more precise
 
- 	fromPos = stackAnimation(attackingStack)->pos.topLeft();
 
- 	//xycoord = owner->stacksController->getStackPositionAtHex(shooter->getPosition(), shooter);
 
- 	destPos = owner->stacksController->getStackPositionAtHex(dest, attackedStack);
 
- 	double projectileAngle = atan2(fabs((double)destPos.y - fromPos.y), fabs((double)destPos.x - fromPos.x));
 
- 	if(attackingStack->getPosition() < dest)
 
- 		projectileAngle = -projectileAngle;
 
- 	if(projectileAngle > straightAngle)
 
- 		group = CCreatureAnim::VCMI_CAST_UP;
 
- 	else if(projectileAngle < -straightAngle)
 
- 		group = CCreatureAnim::VCMI_CAST_DOWN;
 
- 	else
 
- 		group = CCreatureAnim::VCMI_CAST_FRONT;
 
- 	//fall back to H3 cast/2hex
 
- 	//even if creature have 2hex attack instead of cast it is ok since we fall back to attack anyway
 
- 	if(myAnim->framesInGroup(group) == 0)
 
- 	{
 
- 		if(projectileAngle > straightAngle)
 
- 			group = CCreatureAnim::CAST_UP;
 
- 		else if(projectileAngle < -straightAngle)
 
- 			group = CCreatureAnim::CAST_DOWN;
 
- 		else
 
- 			group = CCreatureAnim::CAST_FRONT;
 
- 	}
 
- 	//fall back to ranged attack
 
- 	if(myAnim->framesInGroup(group) == 0)
 
- 	{
 
- 		if(projectileAngle > straightAngle)
 
- 			group = CCreatureAnim::SHOOT_UP;
 
- 		else if(projectileAngle < -straightAngle)
 
- 			group = CCreatureAnim::SHOOT_DOWN;
 
- 		else
 
- 			group = CCreatureAnim::SHOOT_FRONT;
 
- 	}
 
- 	//fall back to normal attack
 
- 	if(myAnim->framesInGroup(group) == 0)
 
- 	{
 
- 		if(projectileAngle > straightAngle)
 
- 			group = CCreatureAnim::ATTACK_UP;
 
- 		else if(projectileAngle < -straightAngle)
 
- 			group = CCreatureAnim::ATTACK_DOWN;
 
- 		else
 
- 			group = CCreatureAnim::ATTACK_FRONT;
 
- 	}
 
- 	return true;
 
- }
 
- void CCastAnimation::nextFrame()
 
- {
 
- 	for(auto & it : pendingAnimations())
 
- 	{
 
- 		CReverseAnimation * anim = dynamic_cast<CReverseAnimation *>(it);
 
- 		if(anim && anim->stack->ID == stack->ID && anim->priority)
 
- 			return;
 
- 	}
 
- 	if(myAnim->getType() != group)
 
- 	{
 
- 		myAnim->setType(group);
 
- 		myAnim->onAnimationReset += [&](){ delete this; };
 
- 	}
 
- 	CBattleAnimation::nextFrame();
 
- }
 
- CEffectAnimation::CEffectAnimation(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx, int _dy, bool _Vflip, bool _alignToBottom)
 
- 	: CBattleAnimation(_owner),
 
- 	destTile(BattleHex::INVALID),
 
- 	x(_x),
 
- 	y(_y),
 
- 	dx(_dx),
 
- 	dy(_dy),
 
- 	Vflip(_Vflip),
 
- 	alignToBottom(_alignToBottom)
 
- {
 
- 	logAnim->debug("Created effect animation %s", _customAnim);
 
- 	customAnim = std::make_shared<CAnimation>(_customAnim);
 
- }
 
- CEffectAnimation::CEffectAnimation(CBattleInterface * _owner, std::shared_ptr<CAnimation> _customAnim, int _x, int _y, int _dx, int _dy)
 
- 	: CBattleAnimation(_owner),
 
- 	destTile(BattleHex::INVALID),
 
- 	customAnim(_customAnim),
 
- 	x(_x),
 
- 	y(_y),
 
- 	dx(_dx),
 
- 	dy(_dy),
 
- 	Vflip(false),
 
- 	alignToBottom(false)
 
- {
 
- 	logAnim->debug("Created custom effect animation");
 
- }
 
- CEffectAnimation::CEffectAnimation(CBattleInterface * _owner, std::string _customAnim, BattleHex _destTile, bool _Vflip, bool _alignToBottom)
 
- 	: CBattleAnimation(_owner),
 
- 	destTile(_destTile),
 
- 	x(-1),
 
- 	y(-1),
 
- 	dx(0),
 
- 	dy(0),
 
- 	Vflip(_Vflip),
 
- 	alignToBottom(_alignToBottom)
 
- {
 
- 	logAnim->debug("Created effect animation %s", _customAnim);
 
- 	customAnim = std::make_shared<CAnimation>(_customAnim);
 
- }
 
- bool CEffectAnimation::init()
 
- {
 
- 	if(!CBattleAnimation::checkInitialConditions())
 
- 		return false;
 
- 	const bool areaEffect = (!destTile.isValid() && x == -1 && y == -1);
 
- 	std::shared_ptr<CAnimation> animation = customAnim;
 
- 	animation->preload();
 
- 	if(Vflip)
 
- 		animation->verticalFlip();
 
- 	auto first = animation->getImage(0, 0, true);
 
- 	if(!first)
 
- 	{
 
- 		delete this;
 
- 		return false;
 
- 	}
 
- 	if(areaEffect) //f.e. armageddon
 
- 	{
 
- 		for(int i=0; i * first->width() < owner->pos.w ; ++i)
 
- 		{
 
- 			for(int j=0; j * first->height() < owner->pos.h ; ++j)
 
- 			{
 
- 				BattleEffect be;
 
- 				be.effectID = ID;
 
- 				be.animation = animation;
 
- 				be.currentFrame = 0;
 
- 				be.x = i * first->width() + owner->pos.x;
 
- 				be.y = j * first->height() + owner->pos.y;
 
- 				be.position = BattleHex::INVALID;
 
- 				owner->effectsController->battleEffects.push_back(be);
 
- 			}
 
- 		}
 
- 	}
 
- 	else // Effects targeted at a specific creature/hex.
 
- 	{
 
- 		const CStack * destStack = owner->getCurrentPlayerInterface()->cb->battleGetStackByPos(destTile, false);
 
- 		BattleEffect be;
 
- 		be.effectID = ID;
 
- 		be.animation = animation;
 
- 		be.currentFrame = 0;
 
- 		//todo: lightning anim frame count override
 
- //			if(effect == 1)
 
- //				be.maxFrame = 3;
 
- 		be.x = x;
 
- 		be.y = y;
 
- 		if(destTile.isValid())
 
- 		{
 
- 			Rect tilePos = owner->fieldController->hexPosition(destTile);
 
- 			if(x == -1)
 
- 				be.x = tilePos.x + tilePos.w/2 - first->width()/2;
 
- 			if(y == -1)
 
- 			{
 
- 				if(alignToBottom)
 
- 					be.y = tilePos.y + tilePos.h - first->height();
 
- 				else
 
- 					be.y = tilePos.y - first->height()/2;
 
- 			}
 
- 			// Correction for 2-hex creatures.
 
- 			if(destStack != nullptr && destStack->doubleWide())
 
- 				be.x += (destStack->side == BattleSide::ATTACKER ? -1 : 1)*tilePos.w/2;
 
- 		}
 
- 		assert(be.x != -1 && be.y != -1);
 
- 		//Indicate if effect should be drawn on top of everything or just on top of the hex
 
- 		be.position = destTile;
 
- 		owner->effectsController->battleEffects.push_back(be);
 
- 	}
 
- 	return true;
 
- }
 
- void CEffectAnimation::nextFrame()
 
- {
 
- 	//notice: there may be more than one effect in owner->battleEffects correcponding to this animation (ie. armageddon)
 
- 	for(auto & elem : owner->effectsController->battleEffects)
 
- 	{
 
- 		if(elem.effectID == ID)
 
- 		{
 
- 			elem.currentFrame += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000;
 
- 			if(elem.currentFrame >= elem.animation->size())
 
- 			{
 
- 				delete this;
 
- 				return;
 
- 			}
 
- 			else
 
- 			{
 
- 				elem.x += dx;
 
- 				elem.y += dy;
 
- 			}
 
- 		}
 
- 	}
 
- }
 
- CEffectAnimation::~CEffectAnimation()
 
- {
 
- 	auto & effects = owner->effectsController->battleEffects;
 
- 	for ( auto it = effects.begin(); it != effects.end(); )
 
- 	{
 
- 		if (it->effectID == ID)
 
- 			it = effects.erase(it);
 
- 		else
 
- 			it++;
 
- 	}
 
- }
 
 
  |