/* Generic Herbivore Idle Behaviour
//
*/

machine HerbIdle
{
	state Start
	{
		transition CanFlock()
		{
			direct goto FlockCheck
		}
		transition
		{
			goto IdleStart
		}
	}

	state FlockCheck
	{
		transition IsInFlock() == false
		{
			call JoinFlock IdleStart
		}
		transition
		{
			goto IdleStart
		}
	}

	state IdleStart
	{
		action StartLook("LookProximity", 0.5)

		action SetAutoMovementBlend(true)
		action SetSpeedModifierWalk(1.0)
		action SetRunMode(false)
		action SetSpeedDirect(0.0)
		action SetStandStill()

		transition
		{
			goto IdleChoose
		}
	}

	state IdleChoose
	{
		action SetRand(0, 10)
		transition GetRand() == 0
		{
			direct goto SniffStart
		}
		transition GetRand() == 1
		{
			direct goto ScratchStart
		}
		transition GetRand() == 2
		{
			direct goto SpecStart
		}
		transition GetRand() == 3
		{
			direct goto FullCallStart
		}
		transition GetRand() == 4
		{
			direct goto RandSpecStart
		}
		transition GetRand() <= 8
		{
			direct goto RestStart
		}
		transition
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}

	;// resting ------------------------------
	state RestStart
	{
		transition DisallowRestState()
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
		transition IsAnimLoaded("rest", "st")
		{
			action SetSpeedDirect(0.0)
			action SetStandStill()
			action SetAutoMovementBlend(false)
			action StartLook("LookRandom", 0.5)
			action PlayAnim("rest", "st", false)
			action SetRandomTimerBetween(1, 20.0, 30.0)

			action StopAllRandomAnimations()
			;//action AddRandomAnimation("call", "02_lp", 5.0, 10.0)
			;//action AddRandomAnimation("tail", "flck_lp", 1.0, 2.0)
			goto Rest
		}
		transition
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}
	state Rest
	{
		transition IsTimerUp(1) 
		{
			action StopLook()
			action StopAnimState("rest")
			goto RestEnd
		}
	}

	state RestEnd
	{
		transition IsAnimStatePlaying("rest", false) == false
		{
			;//action StopAllRandomAnimations()
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}

	;// sniffing ------------------------------
	state SniffStart
	{
		action SendEnclosureEvent("special_low")
		transition IsAnimLoaded("idle", "snif_lp")
		{
			action SetSpeedDirect(0.0)
			action SetStandStill()
			action StopAllUserAnimations()
			action SetAutoMovementBlend(false)
			action StopLook()
			action PlayAnimInstant("idle", "snif_lp", true)
			goto Sniff
		}
		transition
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}
	state Sniff
	{
		transition AnimStateFinished("idle")
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}

		;// temp fix if not have sniff anim will not be able to exit
		transition IsAnimStatePlaying("idle") == false
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}

	;// scratching ------------------------------
	state ScratchStart
	{
		action SendEnclosureEvent("special_low")
		transition IsAnimLoaded("spcl", "sctch_lp")
		{
			action SetSpeedDirect(0.0)
			action SetStandStill()
			action StopAllUserAnimations()
			action SetAutoMovementBlend(false)
			action StopLook()
			action PlayAnimInstant("spcl", "sctch_lp", true)
			goto Scratch
		}
		transition
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}
	state Scratch
	{
		transition AnimStateFinished("spcl")
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}

		;// temp fix if not have sniff anim will not be able to exit
		transition IsAnimStatePlaying("spcl") == false
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}

	;// full body calling ------------------------------
	state FullCallStart
	{
		action SendEnclosureEvent("special_high")
		transition IsAnimLoaded("call", "f")
		{
			action SetSpeedDirect(0.0)
			action SetStandStill()
			action StopAllUserAnimations()
			action SetAutoMovementBlend(false)
			action StopLook()
			action PlayAnimInstant("call", "f", true)
			goto FullCall
		}
		transition
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}
	state FullCall
	{
		transition IsAnimPlaying("call", "f", false) == false
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}

	;// special ------------------------------
	state SpecStart
	{
		action SendEnclosureEvent("special_high")
		transition IsAnimLoaded("spcl", "gen_lp")
		{
			action SetSpeedDirect(0.0)
			action SetStandStill()
			action StopAllUserAnimations()
			action SetAutoMovementBlend(false)
			action StopLook()
			action PlayAnimInstant("spcl", "gen_lp", true)
			goto SpecWait
		}
		transition
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}
	state SpecWait
	{
		transition AnimStateFinished("spcl")
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}

		;// temp fix if not have sniff anim will not be able to exit
		transition IsAnimStatePlaying("spcl") == false
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}

	;// random special ------------------------------
	state RandSpecStart
	{
		action SendEnclosureEvent("special_high")
		transition IsRandomAnimLoaded("spcl", "gen")
		{
			action SetSpeedDirect(0.0)
			action SetStandStill()
			action StopAllUserAnimations()
			action SetAutoMovementBlend(false)
			action StopLook()
			action PlayAnimInstantRandom("spcl", "gen", true)
			goto RandSpecWait
		}
		transition
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}
	state RandSpecWait
	{
		transition AnimStateFinished("spcl")
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}

		transition IsAnimStatePlaying("spcl") == false
		{
			action SetAutoMovementBlend(true)
			goto WanderStart
		}
	}

	;// wander ------------------------------
	state WanderStart
	{
		action SendEnclosureEvent("idle_wander")
		action AddRandomAnimation("call", "02_lp", 15.0, 30.0)
		action AddRandomAnimation("tail", "flck_lp", 7.0, 10.0)
		action AddRandomAnimation("spcl", "blink", 2.0, 3.0)
		action SetRandomTimerBetween(1, 2.0, 4.0)
		transition
		{
			goto IdleLoop
		}
	}

	state IdleLoop
	{
		transition IsTimerUp(1)
		{
			goto IdleWander
		}
	}

	state IdleWander
	{
		transition SetWander() == true
		{
			direct goto WanderTimeChoose
		}
	}

	state WanderTimeChoose
	{
		transition IAmLarge()	;// should be based on speed
		{
			action SetRandomTimerBetween(1, 30.0, 35.0)
			goto IdleWanderLoop
		}

		transition
		{
			action SetRandomTimerBetween(1, 12.0, 17.0)
			goto IdleWanderLoop
		}
	}

	state IdleWanderLoop
	{
		action AcquireTarget("food")
		transition HasArrived()
		{
			action SetStandStill()
			goto AcquireNewFlock
		}
		transition IsTimerUp(1)
		{
			action SetStandStill()
			goto AcquireNewFlock
		}
	}

	state AcquireNewFlock
	{
		transition AcquireFlockTarget(true)
		{
			goto FlockChangeCheck
		}
		transition
		{
			goto IdleChoose
		}
	}
	
	state FlockChangeCheck
	{
		transition CanFlock()
		{
			direct goto FlockChange
		}
		transition
		{
			action ClearTarget()
			goto IdleChoose
		}
	}

	state FlockChange
	{
		transition CanJoinTargetFlock() == false
		{
			action ClearTarget()
			goto IdleChoose
		}
		transition IsInFlock() == false
		{
			action JoinTargetFlock()
			action ClearTarget()
			goto IdleChoose
		}
		transition IsTargetFlockBetter()
		{
			action JoinTargetFlock()
			action ClearTarget()
			goto IdleChoose
		}
		transition
		{
			action ClearTarget()
			goto IdleChoose
		}
	}


;------------------------ Exit States Begin ----------------------
	exitstart state CleanUp
	{
		transition IsAnimPlaying("rest", "lp", false) == true
		{
			direct goto RestToSleepTest
		}
		transition IsAnimStatePlaying("rest", false)
		{
			goto CleanUp ;// wait for rest_st to switch to rest_lp, or rest_ed to finish
		}
		
		transition
		{
			direct goto CleanUpNormal
		}
	}

	state RestToSleepTest
	{
		transition IsNextDrive("Sleep")
		{
			action StopLook()
			goto CleanUpEnd
		}
		transition
		{
			direct goto CleanUpNormal
		}
	}

	state CleanUpNormal
	{
		action SetSpeedModifier(1.0)
		action StopAnimState("rest")
		transition
		{
			goto CleanUpWait
		}
	}

	state CleanUpWait
	{
		transition IsAnimStatePlaying("rest", false) == false
		{
			action SetRandomTimerBetween(1, 20.0, 30.0)
			action StopLook()
			goto CleanUpIdleWait
		}
	}
	state CleanUpIdleWait
	{
		transition IsAutoMovementBlendOn() == false
		{
			action SetRandomTimerBetween(1, 20.0, 30.0)
			goto WaitForAnimFinish
		}
		transition IsIdleLocked() == false
		{
			goto CleanUpEnd
		}
		transition IsTimerUp(1)
		{
			action SetAutoMovementBlend(true)
			goto CleanUpEnd
		}
	}

	state WaitForAnimFinish
	{
		transition IsTimerUp(1)
		{
			goto CleanUpEnd
		}
		transition IsAnimPlaying("idle", "snif_lp", false)
		{
			goto WaitForAnimFinish
		}
		transition IsAnimStatePlaying("spcl", false)
		{
			goto WaitForAnimFinish
		}
		transition IsAnimPlaying("call", "f", false)
		{
			goto WaitForAnimFinish
		}
		transition
		{
			action SetAutoMovementBlend(true)
			goto CleanUpEnd
		}
	}

	exitend state CleanUpEnd
	{
	}
;------------------------ Exit States End ----------------------
}
