ScriptName footprintsFootstepsScriptHuman Extends ActiveMagicEffect

footprintsRefCountScript Property footprintsQuest Auto

string boneLeft = "NPC L Calf [LClf]"
string boneRight = "NPC R Calf [RClf]"

ImpactDataSet[] Property footprintsIPDS Auto
ImpactDataSet[] Property footprintsIPDSNoFX Auto
{
	footprintsDefaultFootstepWalkLImpactset		0
	footprintsDefaultFootstepWalkRImpactset		1
	footprintsDefaultFootstepSneakLImpactset	2
	footprintsDefaultFootstepSneakRImpactset	3
}

Spell Property parentSpell Auto

Actor selfRef

bool refCounted = False


Function Register()
	If ( !refCounted )
		If ( !footprintsQuest.IncrementRefCount() )
			;Debug.Trace( "footprintsFootstepsScriptHuman - 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( "footprintsFootstepsScriptHuman" )
	If ( footprintsIPDS.Length != 4 || footprintsIPDSNoFX.Length != 4 )
		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( "footprintsFootstepsScriptHuman - Applied to " + selfRef + selfRef.GetRace() + Target.GetActorBase() )
		If ( footprintsQuest.FXCountLimitReached() )
			GoToState( "alive_nofx" )
		Else
			GoToState( "alive" )
		EndIf
	Else
		GoToState( "dead" )
	EndIf
EndEvent

Function PlayImpact( ImpactDataSet[] ipds, ObjectReference akSelfRef, string e )
	;If ( ipds.Length == 0 )
	;	GoToState( "dead" )
	;EndIf
	If ( selfRef.IsSneaking() )
		If ( e == "FootLeft" )
			;Debug.Trace("Foot Left")
			akSelfRef.PlayImpactEffect(ipds[2], boneLeft, 0, 0, -1, 512)
		ElseIf ( e == "FootRight" )
			;Debug.Trace("Foot Right")
			akSelfRef.PlayImpactEffect(ipds[3], boneRight, 0, 0, -1, 512)
		EndIf
	Else
		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
	EndIf
EndFunction

State alive

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

EndState

State alive_nofx

	Event OnBeginState()
		If ( footprintsQuest )
			Register()
		EndIf
	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( "footprintsFootstepsScriptHuman - OnDying event on " + selfRef )
		GoToState( "dead" )
	EndEvent
	
	Event OnEffectFinish( Actor akTarget, Actor akCaster )
		;Debug.Trace( "footprintsFootstepsScriptHuman - OnEffectFinish event on " + selfRef )
		GoToState( "dead" )
	EndEvent
	
	Event OnEndState()
		If ( footprintsQuest )
			Unregister()
		EndIf
	EndEvent

EndState

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