ScriptName footprintsFootstepsScriptQuadruped Extends ActiveMagicEffect

footprintsRefCountScript Property footprintsQuest Auto

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

ImpactDataSet Property NPCFootBackWalkImpactSetL  Auto
ImpactDataSet Property NPCFootBackWalkImpactSetR  Auto  
ImpactDataSet Property NPCFootFrontWalkImpactSetL Auto
ImpactDataSet Property NPCFootFrontWalkImpactSetR Auto

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
	
	selfRef = target
	If ( footprintsQuest && selfRef && RegisterForAnimationEvent( selfRef, "FootFront" ) && RegisterForAnimationEvent( selfRef, "FootBack" ) )
		;Debug.trace( "Quadruped Footsteps Effect applied to " + selfRef + ". Race: " + selfRef.GetRace() +" on (" + self +")" )
		GoToState( "alive" )
	Else
		;Debug.Trace( "Failed to register for event" )
		GoToState( "dead" )
	EndIf
EndEvent

State alive

	Event OnBeginState()
		If ( footprintsQuest )
			Register()
		EndIf
	EndEvent
	
	Event OnAnimationEvent( ObjectReference akSelfRef, string e )
		If ( !akSelfRef )
			return
		EndIf
		
		;Debug.trace( "AnimEvent: " + e + " delivered by " + akSelfRef.GetRace() + "("+self+") " )
		If ( e == "FootFront" )
			If ( leg % 2 == 1 )
				akSelfRef.PlayImpactEffect( NPCFootFrontWalkImpactSetR, boneFrontRight, 0, 0, -1, 512 )
			Else
				akSelfRef.PlayImpactEffect( NPCFootFrontWalkImpactSetL, boneFrontLeft, 0, 0, -1, 512 )
			EndIf
		ElseIf ( e == "FootBack" )
			If ( leg % 2 == 0 )
				akSelfRef.PlayImpactEffect( NPCFootBackWalkImpactSetL, boneBackLeft, 0, 0, -1, 512 )
			Else
				akSelfRef.PlayImpactEffect( NPCFootBackWalkImpactSetR, boneBackRight, 0, 0, -1, 512 )
			EndIf
		EndIf
		
		If ( leg > 4 )
			leg = 0
		EndIf
		leg += 1
	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 )
		;Unregister()
		GoToState( "dead" )
	EndEvent
	
	Event OnEndState()
		If ( footprintsQuest )
			Unregister()
		EndIf
	EndEvent

EndState

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