/*
// Special Attack Interaction
//
// caller needs to set up the following for both attacker and defender
//		action SetNumPassedParam(9)
//		action SetPassedParam(1, -1)
//		action SetFatalityAnims()
*/

;// param 0:
;// param 1: behaviour, (-1 = init)
;// param 2: fatal sync
;// param 3: string (fatal attack animation state)
;// param 4: string (fatal attack animation name)
;// param 5: string (fatal death animation state)
;// param 6: string (fatal death animation name)
;// param 7: float (req distance between units)
;// param 8: float (actual perfect distance between units)

machine SpecialAttack
{
	default state SpecialStart
	{
		transition HasTarget() == false
		{
			goto SpecialExit
		}

		action SetFlockImpulse(false)
		action StartLook("LookTarget", 0.5)
		action SetAutoMovementBlend(true)
		action SetSpeedModifierWalk(1.0)
		action SetRunMode(false)

		action LockMachine(true)

		transition
		{
			goto FaceCompetitor
		}
	}

	state FaceCompetitor
	{
		transition
		{
			goto CheckProximity
		}
	}

	state ChooseBehaviour
	{
		transition
		{
			action SetPassedParam(1, 0)
			direct goto SyncBehaviour
		}
	}

	state SyncBehaviour
	{
		transition GetNumPassedParamTarget() != 9
		{
			goto SpecialExit
		}
		transition GetPassedParamTarget(1) == 0
		{
			goto StartBehaviour
		}
	}

	state StartBehaviour
	{
		transition GetNumPassedParamTarget() != 9
		{
			goto SpecialExit
		}
		transition GetPassedParamTarget(1) > 0
		{
			direct goto ResponseBehaviour
		}

		/*
		;// temp
		transition IsAnimLoaded("anky", "attk01") == false
		;//transition IsAnimLoadedParam(3, 4) == false
		{
			goto StartBehaviour	;// wait for attacker to attack
		}
		;// temp
		*/

		transition GetPassedParamTarget(1) == 0
		{
			direct goto InitiateBehaviour
		}
	}

	state InitiateBehaviour
	{
;//		action SetRand(0, 2)
;//		transition GetRand() == 1
		transition CanWinFatality(0.1)
		{
			action SetPassedParam(1, 3)
			goto FatalAttack
		}
		transition
		{
			action SetPassedParam(1, 2)
			goto FatalDeath
		}
	}

	state ResponseBehaviour
	{
		transition GetNumPassedParamTarget() != 9
		{
			goto SpecialExit
		}
		transition GetPassedParamTarget(1) == 0
		{
			direct goto InitiateBehaviour
		}
		transition GetPassedParamTarget(1) == 2
		{
			action SetPassedParam(1, 2)
			goto FatalAttack
		}
		transition GetPassedParamTarget(1) == 3
		{
			action SetPassedParam(1, 3)
			goto FatalDeath
		}
		transition
		{
			action SetPassedParam(1, 1)
			goto ChangePosition
		}
	}

	/* ------- change position */
	state ChangePosition
	{
		action SetRandomTimerBetween(3, 5.0, 6.0)
		transition
		{
			direct goto ChangePositionStart
		}
	}
	state ChangePositionStart
	{
		transition SeekPosOffset(15.0, 0.7854, true, false, true)
		{
			action SetRandomTimerBetween(3, 6.0, 7.0)
			goto ChangePositionSeek
		}
		transition IsTimerUp(3)
		{
			goto FaceCompetitor
		}
	}

	state ChangePositionSeek
	{
		transition HasArrived()
		{
			goto FaceCompetitor
		}
		transition IsTimerUp(3)
		{
			goto FaceCompetitor
		}
	}

	/* ------- Face competitor */
	state CheckProximity
	{
		action SetPassedParam(1, -1)
		action SeekTarget(true, true)
		transition InTargetProximityRadius(15.0, true)
		{
			goto StandOffFaceTarget
		}
		transition
		{
			goto SeekLoop
		}
	}

	state SeekLoop
	{
		transition HasTarget() == false
		{
			goto SpecialExit
		}
		transition InTargetProximityRadius(15.0, true)
		{
			goto StandOffFaceTarget
		}
	}

	state StandOffFaceTarget
	{
		action FaceTarget()
		transition
		{
			action SetRandomTimerBetween(2, 15.0, 16.0)
			goto StandOffFaceTargetWait
		}
	}

	state StandOffFaceTargetWait
	{
		transition HasArrived()
		{
			action SetStandStill()
			goto MinDistanceMove
		}
		transition IsTimerUp(2)
		{
			action SetStandStill()
			goto MinDistanceMove
		}
	}
	state MinDistanceMove
	{
		transition InTargetProximityRadius(10.0, false)
		{
			action SetSpeedModifierWalk(1.0)
			action SetWalkBackMode(true)
			action StepBackFromTarget(20.0)
			action SetRandomTimerBetween(3, 7.0, 8.0)
			goto MinDistanceStep
		}
		transition
		{
			goto ChooseBehaviour
		}
	}
	state MinDistanceStep
	{
		transition InTargetProximityRadius(10.0, false) == false
		{
			action SetWalkBackMode(false)
			action SetLockFacing(false)
			action SetStandStill()
			goto ChooseBehaviour
		}
		transition IsTimerUp(3)
		{
			action SetWalkBackMode(false)
			action SetLockFacing(false)
			action SetStandStill()
			goto ChooseBehaviour
		}
	}

	/* ------- fatal attack */
	state FatalAttack
	{
		transition
		{
			action SetRandomTimerBetween(4, 15.0, 17.0)
			goto FatalAttackMinDistanceMove
		}
	}

	state FatalAttackMinDistanceMove
	{
		transition GetNumPassedParamTarget() != 9
		{
			goto SpecialExit
		}
		transition IsTimerUp(4)
		{
			goto FatalAttackFaceTarget
		}

		transition WithinTargetDistanceParam(7, -0.5, true)	;// too close
		{
			action SetSpeedModifierWalk(1.0)
			action SetWalkBackMode(true)
			action StepBackFromTarget(20.0)
			action SetRandomTimerBetween(3, 6.0, 6.0)
			goto FatalAttackMinDistanceStep
		}
		transition WithinTargetDistanceParam(7, 0.0, true) == false	;// too far
		{
			action SetSpeedModifierWalk(1.0)
			action SetRandomTimerBetween(3, 6.0, 6.0)
			action SeekTarget(true, true)
			goto FatalAttackStepForward
		}
		transition
		{
			goto FatalAttackFaceTarget
		}
	}

	state FatalAttackStepForward
	{
		transition WithinTargetDistanceParam(7, 0.0, true)
		{
			action SetSpeedDirect(0.0)
			action SetStandStill()
			goto FatalAttackMinDistanceMove
		}
		transition IsTimerUp(3)
		{
			action SetSpeedDirect(0.0)
			action SetStandStill()
			goto FatalAttackMinDistanceMove
		}
	}

	state FatalAttackMinDistanceStep
	{
		transition WithinTargetDistanceParam(7, -0.3, true) == false
		{
			action SetWalkBackMode(false)
			action SetLockFacing(false)
			action SetStandStill()
			goto FatalAttackStepForward
		}
		transition IsTimerUp(3)
		{
			action SetWalkBackMode(false)
			action SetLockFacing(false)
			action SetStandStill()
			goto FatalAttackStepForward
		}
	}

	state FatalAttackFaceTarget
	{
		action FaceTarget()
		action SetRandomTimerBetween(2, 3.0, 3.0)

		transition IsAnimLoaded("call", "02_lp")
		{
			action PlayAnim("call", "02_lp", false)
			goto FatalAttackFaceTargetWait
		}
		transition IsAnimLoaded("call", "f")
		{
			action PlayAnim("call", "f", false)
			goto FatalAttackFaceTargetWait
		}
		transition
		{
			goto FatalAttackFaceTargetWait
		}
	}

	state FatalAttackFaceTargetWait
	{
		transition IsTimerUp(2)
		{
			goto FatalAttackSync
		}
	}

	state FatalAttackSync
	{
		action SetPassedParam(2, 1)
		transition
		{
			action SetStandStill()
			action SetSpeedDirect(0.0)
			action SetRandomTimerBetween(3, 12.0, 15.0)
			direct goto FatalSyncWait
		}
	}

	state FatalSyncWait
	{
		transition GetNumPassedParamTarget() != 9
		{
			goto SpecialExit
		}
		transition GetPassedParamTarget(2) == 1
		{
			direct goto StartFatalAttack
		}
		transition IsTimerUp(3)
		{
			goto SpecialExit
		}
	}

	state StartFatalAttack
	{
		action SetAutoMovementBlend(false)
		action StopLook()
		action StopAllUserAnimations()

		action SetInterpolation(true)
		action SetAvoidanceSteer(false)

		;//action PlayAnim("anky", "attk01", false)
		;//action SetAesPosInterp(false)
		action PlayAnimParam(3, 4, false, 0.0)
		transition
		{
			goto FatalAttackLoop
		}
	}

	state FatalAttackLoop
	{
		;//transition IsAnimPlaying("anky", "attk01", false) == false
		transition IsAnimPlayingParam(3, 4, false) == false
		{
			action SetRandomTimerBetween(3, 1.0, 1.0)
			goto FatalPosture
		}
	}

	state FatalPosture
	{
		transition IsTimerUp(3)
		{
			action SetInterpolation(false)
			action SetInterpolationTarget(false)
			action SetAvoidanceSteer(true)

			action SetTransfromFromLocator("D", true)
			;//action StopAnimInstant("anky", "attk01")
			action StopAnimInstantParam(3, 4)
			action PlayAnimInstant("idle", "brth_lp", false)
			action SetStandStill()
			action SetAutoMovementBlend(true)
			action SetRandomTimerBetween(3, 1.0, 1.0)
			goto FatalPostureStartWait
		}
	}

	state FatalPostureStartWait
	{
		transition IsTimerUp(3)
		{
			action SetAutoMovementBlend(false)
			action PlayAnimInstant("idle", "post_lp", false)
			goto FatalPostureWait
		}
	}

	state FatalPostureWait
	{
		transition IsAnimPlaying("idle", "post_lp", false) == false
		{
			action AddKillExperience(1, 0.0)
			action SetAutoMovementBlend(true)
			goto SpecialExit
		}
	}

	/* ------- fatal death */
	state FatalDeath
	{
		transition
		{
			action SetRandomTimerBetween(4, 15.0, 17.0)
			goto FatalDeathMinDistanceMove
		}
	}
	state FatalDeathMinDistanceMove
	{
		transition GetNumPassedParamTarget() != 9
		{
			goto SpecialExit
		}
		transition IsTimerUp(4)
		{
			goto FatalDeathFaceTarget
		}

		transition WithinTargetDistanceParam(7, -0.5, true)	;// too close
		{
			action SetSpeedModifierWalk(1.0)
			action SetWalkBackMode(true)
			action StepBackFromTarget(20.0)
			action SetRandomTimerBetween(3, 6.0, 6.0)
			goto FatalDeathMinDistanceStep
		}
		transition WithinTargetDistanceParam(7, 0.0, true) == false	;// too far
		{
			action SetSpeedModifierWalk(1.0)
			action SetRandomTimerBetween(3, 6.0, 6.0)
			action SeekTarget(true, true)
			goto FatalDeathStepForward
		}
		transition
		{
			goto FatalDeathFaceTarget
		}
	}

	state FatalDeathStepForward
	{
		transition WithinTargetDistanceParam(7, 0.0, true)
		{
			action SetSpeedDirect(0.0)
			action SetStandStill()
			goto FatalDeathMinDistanceMove
		}
		transition IsTimerUp(3)
		{
			action SetSpeedDirect(0.0)
			action SetStandStill()
			goto FatalDeathMinDistanceMove
		}
	}

	state FatalDeathMinDistanceStep
	{
		transition WithinTargetDistanceParam(7, -0.3, true) == false
		{
			action SetWalkBackMode(false)
			action SetLockFacing(false)
			action SetStandStill()
			goto FatalDeathStepForward
		}
		transition IsTimerUp(3)
		{
			action SetWalkBackMode(false)
			action SetLockFacing(false)
			action SetStandStill()
			goto FatalDeathStepForward
		}
	}

	state FatalDeathFaceTarget
	{
		action FaceTarget()
		action SetRandomTimerBetween(2, 3.0, 3.0)

		transition IsAnimLoaded("call", "02_lp")
		{
			action PlayAnim("call", "02_lp", false)
			goto FatalDeathFaceTargetWait
		}
		transition IsAnimLoaded("call", "f")
		{
			action PlayAnim("call", "f", false)
			goto FatalDeathFaceTargetWait
		}
		transition
		{
			goto FatalDeathFaceTargetWait
		}
	}

	state FatalDeathFaceTargetWait
	{
		transition IsTimerUp(2)
		{
			goto FatalDeathSync
		}
	}

	state FatalDeathSync
	{
		action SetPassedParam(2, 1)
		transition
		{
			action SetStandStill()
			action SetSpeedDirect(0.0)
			action SetRandomTimerBetween(3, 15.0, 17.0)
			direct goto FatalDeathSyncWait
		}
	}

	state FatalDeathSyncWait
	{
		transition GetNumPassedParamTarget() != 9
		{
			goto SpecialExit
		}
		transition GetPassedParamTarget(2) == 1
		{
			direct goto StartFatalDeath
		}
		transition IsTimerUp(3)
		{
			goto SpecialExit
		}
	}

	state StartFatalDeath
	{
		action SetAutoMovementBlend(false)
		action StopLook()
		action StopAllUserAnimations(true)

		action SetInterpolation(true)
		action SetAvoidanceSteer(false)

		;//action PlayAnim("lcarn", "dth01", false)
		;//action SetLockLastPose("lcarn", "dth01", true)
		;//action SetAesPosInterp(false)
		action PlayAnimParam(5, 6, false, 0.0)
		action SetLockLastPoseParam(5, 6, true)
		transition
		{
			goto FatalDeathLoop
		}
	}

	state FatalDeathLoop
	{
		;//transition IsAnimPlaying("lcarn", "dth01", false) == false
		transition IsAnimPlayingParam(5, 6, false) == false
		{
			action SetInterpolation(false)
			action SetInterpolationTarget(false)
			action SetAvoidanceSteer(true)

			action SetVariable("health", 0.0)
			action LockDeadPose()
			action LockMachine(false)
			action SetDeathDrive()
			action SetKillCause("dino")

			goto SpecialExitEnd
		}
	}

;//-----------------------------------------------------------------
	exitstart state SpecialExit
	{
		action SetAutoMovementBlend(true)
		action StopLook()
		transition
		{
			goto SpecialExitEnd
		}
	}

	exitend state SpecialExitEnd
	{
		action SetFlockImpulse(true)
		action SetInterpolation(false)
		action SetAvoidanceSteer(true)

		action SetLockFacing(false)
		action SetWalkBackMode(false)
		action LockMachine(false)
		action DeleteAllCommands()
		;//action SetAesPosInterp(true)
	}
}