ScriptName footprintsFootstepsScriptQuadrupedFX Extends ActiveMagicEffect

footprintsRefCountScript Property footprintsQuest Auto

string Property boneFrontLeft  Auto
string Property boneFrontRight Auto
string Property boneBackLeft   Auto
string Property boneBackRight  Auto

ImpactDataSet[] Property footprintsIPDS Auto
ImpactDataSet[] Property footprintsIPDSNoFX Auto
{
	NPCFootBackWalkImpactSetL  0
	NPCFootBackWalkImpactSetR  1
	NPCFootFrontWalkImpactSetL 2
	NPCFootFrontWalkImpactSetR 3
}

Spell Property parentSpell Auto

Actor selfRef
int leg = 1

bool refCounted = False


Function Register()
	If ( !refCounted )
		If ( !footprintsQuest.IncrementRefCount() )
			;Debug.Trace( "footprintsFootstepsScriptQuadruped - 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( "footprintsFootstepsScriptQuadruped" )
	If ( !boneFrontLeft  || !boneFrontRight || !boneBackLeft  || !boneBackRight )
		Debug.Trace( "One of the bone strings is empty on " + self + ". Aborting..." )
		GoToState( "dead" )
	EndIf
	
	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, "FootFront" ) && RegisterForAnimationEvent( selfRef, "FootBack" ) )
		;Debug.trace( "Quadruped 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 " + akSelfRef.GetRace() + "("+self+") " )
	If ( e == "FootFront" )
		If ( leg % 2 == 1 )
			akSelfRef.PlayImpactEffect( ipds[3], boneFrontRight, 0, 0, -1, 512 )
		Else
			akSelfRef.PlayImpactEffect( ipds[2], boneFrontLeft, 0, 0, -1, 512 )
		EndIf
	ElseIf ( e == "FootBack" )
		If ( leg % 2 == 0 )
			akSelfRef.PlayImpactEffect( ipds[0], boneBackLeft, 0, 0, -1, 512 )
		Else
			akSelfRef.PlayImpactEffect( ipds[1], boneBackRight, 0, 0, -1, 512 )
		EndIf
	EndIf
	
	If ( leg > 4 )
		leg = 0
	EndIf
	leg += 1
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( "footprintsFootstepsScriptQuadruped - OnDying event on " + selfRef )
		GoToState( "dead" )
	EndEvent
	
	Event OnEffectFinish( Actor akTarget, Actor akCaster )
		;Debug.Trace( "footprintsFootstepsScriptQuadruped - 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( "footprintsFootstepsScriptQuadruped - OnDying event on " + selfRef )
		GoToState( "dead" )
	EndEvent
	
	Event OnEffectFinish( Actor akTarget, Actor akCaster )
		;Debug.Trace( "footprintsFootstepsScriptQuadruped - 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
