ScriptName footprintsFootstepsScriptMammoth Extends ActiveMagicEffect  

string Property boneFrontLeft = "Mammoth Front L Foot" Auto 
string Property boneFrontRight = "Mammoth Front R Foot" Auto
string Property boneBackLeft = "Mammoth Back L Foot" Auto
string Property boneBackRight = "Mammoth Back R Foot" Auto

ImpactDataSet Property footprintsNPCMammothFootRunFrontImpactSetL   Auto  
ImpactDataSet Property footprintsNPCMammothFootRunBackImpactSetL    Auto
ImpactDataSet Property footprintsNPCMammothFootWalkFrontImpactSetL  Auto  
ImpactDataSet Property footprintsNPCMammothFootWalkBackImpactSetL   Auto
ImpactDataSet Property footprintsNPCMammothFootRunFrontImpactSetR   Auto  
ImpactDataSet Property footprintsNPCMammothFootRunBackImpactSetR    Auto
ImpactDataSet Property footprintsNPCMammothFootWalkFrontImpactSetR  Auto  
ImpactDataSet Property footprintsNPCMammothFootWalkBackImpactSetR   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( "Effect applied to " + selfRef + ". Race: " + selfRef.GetRace() +" on (" + self +")" )
		;RegisterForAnimationEvent( selfRef, "SyncLeft" )
		;RegisterForAnimationEvent( selfRef, "SyncRight" )
		GoToState( "alive" )
	Else
		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.GetCombatState() == 0 )
			If ( e == "FootFront" )
				If ( leg % 2 == 1 )
					akSelfRef.PlayImpactEffect( footprintsNPCMammothFootWalkFrontImpactSetR, boneFrontRight, 0, 0, -1, 512 )
				Else
					akSelfRef.PlayImpactEffect( footprintsNPCMammothFootWalkFrontImpactSetL, boneFrontLeft, 0, 0, -1, 512 )
				EndIf
			ElseIf ( e == "FootBack" )
				If ( leg % 2 == 0 )
					akSelfRef.PlayImpactEffect( footprintsNPCMammothFootWalkBackImpactSetL, boneBackLeft, 0, 0, -1, 512 )
				Else
					akSelfRef.PlayImpactEffect( footprintsNPCMammothFootWalkBackImpactSetR, boneBackRight, 0, 0, -1, 512 )
				EndIf
			EndIf
		Else
			If ( e == "FootFront" )
				If ( leg % 2 == 1 )
					akSelfRef.PlayImpactEffect( footprintsNPCMammothFootRunFrontImpactSetR, boneFrontRight, 0, 0, -1, 512 )
				Else
					akSelfRef.PlayImpactEffect( footprintsNPCMammothFootRunFrontImpactSetL, boneFrontLeft, 0, 0, -1, 512 )
				EndIf
			ElseIf ( e == "FootBack" )
				If ( leg % 2 == 0 )
					akSelfRef.PlayImpactEffect( footprintsNPCMammothFootRunBackImpactSetL, boneBackLeft, 0, 0, -1, 512 )
				Else
					akSelfRef.PlayImpactEffect( footprintsNPCMammothFootRunBackImpactSetR, 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
