ScriptName footprintsFootstepsScriptHorse extends ActiveMagicEffect  

string Property boneFrontLeft = "HorseLPhalangesManus" Auto 
string Property boneFrontRight = "HorseFrontRLegPhalangesManus" Auto
string Property boneBackLeft = "HorseLPhalanxPrima" Auto
string Property boneBackRight = "HorseRPhalanxPrima" Auto

ImpactDataSet Property footprintsNPCHorseFootWalkBackImpactSetL    Auto
ImpactDataSet Property footprintsNPCHorseFootWalkBackImpactSetR    Auto  
ImpactDataSet Property footprintsNPCHorseFootWalkFrontImpactSetL   Auto
ImpactDataSet Property footprintsNPCHorseFootWalkFrontImpactSetR   Auto
ImpactDataSet Property footprintsNPCHorseFootSprintBackImpactSetL  Auto
ImpactDataSet Property footprintsNPCHorseFootSprintBackImpactSetR  Auto  
ImpactDataSet Property footprintsNPCHorseFootSprintFrontImpactSetL Auto
ImpactDataSet Property footprintsNPCHorseFootSprintFrontImpactSetR Auto

Spell Property parentSpell Auto

Actor selfRef
int leg = 1


Event OnEffectStart( Actor target, Actor caster )
	selfRef = target
	If ( selfRef && RegisterForAnimationEvent( selfRef, "FootFront" ) && RegisterForAnimationEvent( selfRef, "FootBack" ) )
		;Debug.trace( "Horse Footsteps Effect applied to " + selfRef + ". Race: " + selfRef.GetRace() +" on (" + self +")" )
		;RegisterForAnimationEvent( selfRef, "SyncLeft" )
		;RegisterForAnimationEvent( selfRef, "SyncRight" )
		GoToState( "alive" )
	Else
		;Debug.Trace( "Failed to register for event" )
		GoToState( "dead" )
	EndIf
EndEvent

State alive

	Event OnAnimationEvent( ObjectReference akSelfRef, string e )
		If ( !akSelfRef )
			return
		EndIf
		
		;Debug.Trace("AnimEvent: " + e + " delivered by " + selfRef.GetRace() + "("+self+") ")
		If ( selfRef.IsRunning() || selfRef.IsSprinting() )
			If ( e == "FootFront" )
				If ( leg % 2 == 1 )
					akSelfRef.PlayImpactEffect( footprintsNPCHorseFootSprintFrontImpactSetR, boneFrontRight, 0, 0, -1, 512 )
				Else
					akSelfRef.PlayImpactEffect( footprintsNPCHorseFootSprintFrontImpactSetL, boneFrontLeft, 0, 0, -1, 512 )
				EndIf
			ElseIf ( e == "FootBack" )
				If ( leg % 2 == 0 )
					akSelfRef.PlayImpactEffect( footprintsNPCHorseFootSprintBackImpactSetL, boneBackLeft, 0, 0, -1, 512 )
				Else
					akSelfRef.PlayImpactEffect( footprintsNPCHorseFootSprintBackImpactSetR, boneBackRight, 0, 0, -1, 512 )
				EndIf
			EndIf
		Else
			If ( e == "FootFront" )
				If ( leg % 2 == 1 )
					akSelfRef.PlayImpactEffect( footprintsNPCHorseFootWalkFrontImpactSetR, boneFrontRight, 0, 0, -1, 512 )
				Else
					akSelfRef.PlayImpactEffect( footprintsNPCHorseFootWalkFrontImpactSetL, boneFrontLeft, 0, 0, -1, 512 )
				EndIf
			ElseIf ( e == "FootBack" )
				If ( leg % 2 == 0 )
					akSelfRef.PlayImpactEffect( footprintsNPCHorseFootWalkBackImpactSetL, boneBackLeft, 0, 0, -1, 512 )
				Else
					akSelfRef.PlayImpactEffect( footprintsNPCHorseFootWalkBackImpactSetR, boneBackRight, 0, 0, -1, 512 )
				EndIf
			EndIf
		EndIf
		
		If ( leg > 4 )
			leg = 0
		EndIf
		leg += 1
	EndEvent

	Event OnDying( Actor killer )
		GoToState( "dead" )
	EndEvent
	
	Event OnEffectFinish( Actor akTarget, Actor akCaster )
		GoToState( "dead" )
	EndEvent
	
EndState

State dead
	Event OnBeginState()
		If ( selfRef && parentSpell )
			selfRef.RemoveSpell( parentSpell )
		EndIf
	EndEvent
EndState
