/*
// Spino Ambushing Behaviour
//
*/

machine SpinoAmbush
{
	state AmbushStart
	{
		action SetRandomTimerBetween(4, 20.0, 30.0)
		action SetAutoMovementBlend(true)
		action ClearTarget()
		action SetSpeedModifierWalk(1.0)
		action SetRunMode(false)
		action StartLook("LookProximity", 0.5)

		transition
		{
			goto FindDeepWater
		}
	}

	state FindDeepWater
	{
		transition IsTimerUp(4)
		{
			goto CleanUp
		}
		transition SetWander() == true
		{
			goto WanderFindWater
		}
	}

	state WanderFindWater
	{
		transition AcquireWaterPoint(false, -3.0)
		;//transition AcquireWaterPoint(false)
		{
			action SeekWaterPoint()
			goto SeekAmbushLoop
		}
		transition HasArrived()
		{
			goto FindDeepWater
		}
		action AcquirePrefPrey()
		transition HasTarget()
		{
			goto CleanUp
		}
	}

	state SeekAmbushLoop
	{
		transition HasArrived()
		{
			action FaceLand()
			goto FaceLandWait
		}
	}

	state FaceLandWait
	{
		transition HasArrived()
		{
			action SetStandStill()
			action SetAutoMovementBlend(false)
			action PlayAnim("amb", "st", false)
			action SetRandomTimerBetween(4, 30.0, 40.0)
			action SetVisibility(0.0)
			goto AmbushWait
		}
	}

	state AmbushWait
	{
		action AcquirePrefPrey()
		transition HasTarget()
		{
			goto ProxCheck
		}

		transition IsTimerUp(4)
		{
			goto CleanUp
		}
	}

	state ProxCheck
	{
		action AcquirePrefPrey()
		transition HasTarget() == false
		{
			goto AmbushWait
		}
		transition InTargetProximity(25.0) == true
		{
			direct goto ProxFacingCheck
		}
	}

	state ProxFacingCheck
	{
		transition IsFacingTargetUnit(0.7)
		{
			action StopAnimState("amb")
			action SetAutoMovementBlend(true)
			goto ChaseTarget
		}
		transition
		{
			goto ProxCheck
		}
	}

	state ChaseTarget
	{
		action SetSpeedModifier(1.5)
		action SetRunMode(true)
		transition
		{
			action SetRandomTimerBetween(1, 15.0, 17.0)
			goto SeekLoop
		}
	}

	state SeekLoop
	{
		action SeekTargetOffset(1.0, 0.0, false, false, true)
		transition InTargetProximityRadius(0.7, true)
		{
			action SetVisibility(1.0)
			direct goto SeekFacingCheck
		}
		transition HasArrived()
		{
			direct goto SeekFacingCheckArrived
		}
		transition HasTarget() == false
		{
			action ClearTarget()
			goto AmbushStart
		}
		transition IsTimerUp(1)
		{
			goto FailRoar
		}
	}

	state SeekFacingCheck
	{
		transition IsTargetDead()
		{
			goto CleanUp
		}
		transition IsFacingTargetUnit(0.8)
		{
			action SetStandStill();
			action SetStandStillTarget();
			direct goto Attack
		}
		transition
		{
			action FaceTarget()
			goto SeekFacingAlignWait
		}
	}

	state SeekFacingAlignWait
	{
		transition HasArrived()
		{
			direct goto SeekLoop
		}
		transition InTargetProximityRadius(0.72, true) == false
		{
			action SeekTarget(true, false)
			goto SeekLoop
		}
	}

	state SeekFacingCheckArrived
	{
		transition
		{
			action SeekTarget(true, false)
			goto SeekLoop
		}
	}

	state Attack
	{
		transition IsTargetSmall() == false
		{
			direct goto KnockInit
		}

		transition
		{
			direct goto GrabChoose
		}
	}

	state GrabChoose
	{
		transition IsTargetHuman()
		{
			direct goto GrabHumanCheck
		}

		transition
		{
			action StopLook()
			direct call CarnGrab CleanUp
		}
	}

	state GrabHumanCheck
	{
		transition IsHumanWaitingToDie()
		{
			action StopLook()
			call CarnGrabHuman CleanUp
		}
		transition
		{
			goto AmbushStart
		}
	}

	state KnockInit
	{
		transition IsTargetDead()
		{
			goto CleanUp
		}
		transition IsTargetFSMRunning(fsm::"KnockOver")
		{
			action SetStandStill()
			goto CleanUp
		}
		transition CanKnockTarget(0.0) == false
		{
			action SetStandStill()
			goto CleanUp
		}

		transition
		{
			action StopLook()
			action SetRandomTimerBetween(3, 0.7, 0.7)
			action PlayAnim("snap", "lo_lp", false)
			goto KnockWait
		}

	}

	state KnockWait
	{
		transition IsTimerUp(3)
		{
			goto KnockSetParams
		}
	}
	

	state KnockSetParams
	{
		action SetStandStill()

		action SetTargetToSlave()
		action PushTarget()
		
		action SetNumPassedParamTarget(1)
		action SetPassedParamTargetKnockDir(0)
		
		action SetTargetsTargetToMe()
		action SetTargetFSM(fsm::"KnockOver")

		transition
		{
			action SetRandomTimerBetween(3, 2.0, 3.0)
			goto KnockPause
		}
	}

	state KnockPause
	{
		transition IsTimerUp(3)
		{
			goto ExpUpdate
		}
	}
	
	state ExpUpdate
	{
		transition IsTargetDead()
		{
			action AddKillExperience(1, 0.0)
			goto CleanUp
		}
		transition
		{
			goto CleanUp
		}
	}

	state FailRoar
	{
		action SetStandStill()
		action SetSpeedModifierWalk(1.0)
		action SetRunMode(false)
		action PlayAnim("call", "02_lp", false)
		action SendEnclosureEvent("Call_angry")
		action SetRandomTimerBetween(1, 2.0, 30.0)
		transition
		{
			goto RoarWait
		}
	}

	state RoarWait
	{
		transition IsTimerUp(1)
		{
			goto CleanUp
		}
	}
;------------------------ Exit States Begin ----------------------
	exitstart state CleanUp
	{
		transition
		{
			goto CleanUpEnd
		}
	}

	exitend state CleanUpEnd
	{
		action SetVisibility(1.0)
		action SetRandomTimerBetween(5, 40.0, 50.0)	;// water ambush delay (used during spnohngr)
		action StopAnimState("amb")
		action SetAutoMovementBlend(true)
	}
;------------------------ Exit States End ----------------------

}
