ScriptName footprintsFootstepsScriptBipedFX Extends ActiveMagicEffect

footprintsRefCountScript Property footprintsQuest Auto

string Property boneLeft  Auto
string Property boneRight Auto

ImpactDataSet[] Property footprintsIPDS Auto
ImpactDataSet[] Property footprintsIPDSNoFX Auto
{
	footprintsNPCWalkLImpactset 0
	footprintsNPCWalkRImpactset 1
}

Spell Property parentSpell Auto

Actor selfRef

bool refCounted = False

Function Register()
	If ( !refCounted )
		If ( !footprintsQuest.IncrementRefCount() )
			;Debug.Trace( "footprintsFootstepsScriptBiped - NPC Footprints at max limit, aborting." )
			GoToState( "dead" )
			return
		EndIf
		refCounted = True
	EndIf
EndFunction

Function Unregister()
	If ( refCounted )
		refCounted = False
		footprintsQuest.DecrementRefCount()
	EndIf
EndFunction

Event OnEffectStart( Actor target, Actor caster )
	;Debug.StartScriptProfiling( "footprintsFootstepsScriptBiped" )
	If ( !boneLeft  || !boneRight  )
		Debug.Trace( "Either boneLeft or boneRight are empty on " + self + ". Aborting..." )
		GoToState( "dead" )
	EndIf
	
	If ( footprintsIPDS.Length != 2 || footprintsIPDSNoFX.Length != 2 )
		Debug.Trace( "An IPDS array is incorrect length on " + self + ". Aborting..." )
		GoToState( "dead" )
	EndIf
	
	selfRef = target
	If ( footprintsQuest && selfRef && RegisterForAnimationEvent( selfRef, "FootLeft" ) && RegisterForAnimationEvent( selfRef, "FootRight" )  )
		;Debug.Trace( "Biped Footsteps Effect applied to " + selfRef + ". Race: " + selfRef.GetRace() +" on (" + self +")" )
		If ( footprintsQuest.FXCountLimitReached() )
			GoToState( "alive_nofx" )
		Else
			GoToState( "alive" )
		EndIf
	Else
		;Debug.Trace( "Failed to register for event" )
		GoToState( "dead" )
	EndIf
EndEvent

Function PlayImpact( ImpactDataSet[] ipds, ObjectReference akSelfRef, string e )
	;Debug.Trace( "AnimEvent: " + e + " delivered by " + selfRef.GetRace() + "("+self+") " )
	If ( e == "FootLeft" )
		;Debug.Trace( "Foot Left" )
		akSelfRef.PlayImpactEffect( ipds[0], boneLeft, 0, 0, -1, 512 )
	ElseIf ( e == "FootRight" )
		;Debug.Trace( "Foot Right" )
		akSelfRef.PlayImpactEffect( ipds[1], boneRight, 0, 0, -1, 512 )
	EndIf
EndFunction

State alive

	Event OnBeginState()
		Register()
		footprintsQuest.IncrementFXCount()
	EndEvent
	
	Event OnAnimationEvent( ObjectReference akSelfRef, string e )
		If ( akSelfRef )
			PlayImpact( footprintsIPDS, akSelfRef, e )
		EndIf
	EndEvent
	
	Event OnDying( Actor killer )
		;Debug.Trace( "footprintsFootstepsScriptBiped - OnDying event on " + selfRef )
		GoToState( "dead" )
	EndEvent
	
	Event OnEffectFinish( Actor akTarget, Actor akCaster )
		;Debug.Trace( "footprintsFootstepsScriptBiped - OnEffectFinish event on " + selfRef )
		GoToState( "dead" )
	EndEvent
	
	Event OnEndState()
		Unregister()
		footprintsQuest.DecrementFXCount()
	EndEvent

EndState

State alive_nofx

	Event OnBeginState()
		Register()
	EndEvent
	
	Event OnAnimationEvent( ObjectReference akSelfRef, string e )
		If ( !akSelfRef )
			return
		EndIf
		
		If ( !footprintsQuest.FXCountLimitReached() )
			GoToState( "alive" )
			return
		EndIf
		
		PlayImpact( footprintsIPDSNoFX, akSelfRef, e )
	EndEvent
	
	Event OnDying( Actor killer )
		;Debug.Trace( "footprintsFootstepsScriptBiped - OnDying event on " + selfRef )
		GoToState( "dead" )
	EndEvent
	
	Event OnEffectFinish( Actor akTarget, Actor akCaster )
		;Debug.Trace( "footprintsFootstepsScriptBiped - OnEffectFinish event on " + selfRef )
		GoToState( "dead" )
	EndEvent
	
	Event OnEndState()
		Unregister()
	EndEvent

EndState

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