/* Generic Herbivore Play Behaviour
//
// No Running
*/

machine HerbPlayWalk
{
	default state Start
	{
		transition CanFlock()
		{
			direct goto FlockCheck
		}
		transition
		{
			goto StartInit
		}
	}

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

	state StartInit
	{
		action SendEnclosureEvent("play")
		action SetAutoMovementBlend(true)
		action SetSpeedModifierWalk(1.0)
		action SetRunMode(false)
		transition
		{
			goto SetMoveMode
		}
	}

	state SetMoveMode
	{
		action ClearTarget()
		action SetRand(0, 2)
		transition
		{
			goto GetTarget
		}
	}

	state GetTarget
	{
		action SetSpeedModifierWalk(1.0)
		action SetRunMode(false)
		action SetRand(0, 2)
		transition GetRand() == 0
		{
			direct goto PlayChoose
		}
		transition AcquireFlockTarget(true)
		{
			goto FlockChangeCheck
		}
	}

	state PlayChoose
	{
		/*
		;// testing
		transition
		{
			direct goto Nuzzle
		}
		;// testing
		*/

		/*
		;// testing
		transition
		{
			direct goto TerritoryPlay
		}
		;// testing
		*/

		action StopAllRandomAnimations()
		
		action SetRand(0, 10)
		transition GetRand() == 0
		{
			action SetStandStill()
			direct goto SniffStart
		}
		transition GetRand() == 1
		{
			action SetStandStill()
			direct goto ScratchStart
		}
		transition GetRand() == 2
		{
			action SetStandStill()
			direct goto SpecStart
		}
		transition GetRand() == 3
		{
			action SetStandStill()
			direct goto FullCallStart
		}
		transition GetRand() == 4
		{
			direct goto RandSpecStart
		}
		transition GetRand() == 5
		{
			direct goto SpecPlay
		}
		transition GetRand() == 6
		{
			direct goto Nuzzle
		}
		transition GetRand() == 7
		{
			direct goto TerritoryPlay
		}

		transition
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
	}

	;// sniffing ------------------------------
	state SniffStart
	{
		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 SetWanderChose
		}
	}
	state Sniff
	{
		transition AnimStateFinished("idle")
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}

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

	;// scratching ------------------------------
	state ScratchStart
	{
		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 SetWanderChose
		}
	}
	state Scratch
	{
		transition AnimStateFinished("spcl")
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}

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

	;// full body calling ------------------------------
	state FullCallStart
	{
		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 SetWanderChose
		}
	}
	state FullCall
	{
		transition IsAnimPlaying("call", "f", false) == false
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
	}

	;// special ------------------------------
	state SpecStart
	{
		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 SetWanderChose
		}
	}
	state SpecWait
	{
		transition AnimStateFinished("spcl")
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}

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

	;// random special ------------------------------
	state RandSpecStart
	{
		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 SetWanderChose
		}
	}
	state RandSpecWait
	{
		transition AnimStateFinished("spcl")
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}

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

	;// special play ------------------------------
	state SpecPlay
	{
		transition IsAnimLoaded("spcl", "play")
		{
			action SetSpeedDirect(0.0)
			action SetStandStill()
			action StopAllUserAnimations()
			action SetAutoMovementBlend(false)
			action StopLook()
			action PlayAnimInstant("spcl", "play", true)
			goto SpecWait
		}
		transition
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
	}
	state SpecWait
	{
		transition AnimStateFinished("spcl")
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}

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

	;// nuzzle ------------------------------
	state Nuzzle
	{
		transition AcquireTarget("friend") == false
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
		transition IsTargetSameSpecies() == false
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
		transition CanNuzzleTarget() == false
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
		transition IsAnimLoaded("spcl", "nuz_lt") == false
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
		transition IsAnimLoaded("spcl", "nuz_rt") == false
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
		transition IsTargetFSMRunning(fsm::"Nuzzle")	;// should be a CanNuzzle check
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
		transition
		{
			action SetNumPassedParam(2)
			action SetPassedParam(0, -1)

			action SetTargetToSlave()
			action SetTargetFSM(fsm::"Nuzzle")
			action SetNumPassedParamTarget(2)
			action SetPassedParamTarget(0, -1)
			action SetTargetsTargetToMe()
			action LockTargetMachine(true)

			call Nuzzle SetWanderChose
		}
	}

	;// wander ------------------------------
	state TerritoryPlay
	{
		transition IsTargetTerritoryBhvrLocked()
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
		transition GetVariable("Territory") < 0.7
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
		transition AcquireTarget("friend") == false
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
		transition IsTargetSameSpecies() == false
		{
			action SetAutoMovementBlend(true)
			goto SetWanderChose
		}
		transition
		{
			direct call ToroTerritory SetWanderChose
		}
	}

	;// wander ------------------------------
	state SetWanderChose
	{
		action SetAutoMovementBlend(true)
		action AddRandomAnimation("tail", "play_lp", 3.0, 8.0)
		action AddRandomAnimation("tail", "flck_lp", 7.0, 15.0)
		action AddRandomAnimation("spcl", "blink", 2.0, 3.0)
		transition SetWander()
		{
			goto SpeedChoose
		}
	}

	state SpeedChoose
	{
		action SetRand(0, 2)
		action SetRandomTimerBetween(1, 6.0, 8.0)
		transition GetRand() == 0
		{
			action SetRunMode(true)
			action SetSpeedModifier(1.0)
			goto SeekWait
		}
		transition
		{
			action SetRunMode(false)
			action SetSpeedModifierWalk(1.0)
			goto SeekWait
		}
	}

	state FlockChangeCheck
	{
		transition CanFlock()
		{
			direct goto FlockChange
		}
		transition
		{
			goto SeekTarget
		}
	}

	state FlockChange
	{
		transition IsTargetFlockBetter()
		{
			action JoinTargetFlock()
			goto SeekTarget
		}
		transition
		{
			goto SeekTarget
		}
	}

	state SeekTarget
	{
		transition SeekTarget(false, false)
		{
			action SetRandomTimerBetween(1, 5.0, 8.0)
			goto SeekWait
		}
		transition
		{
			action ClearTarget()
			goto SetMoveMode
		}
	}

	state SeekWait
	{
		transition InTargetProximityRadius(1.5)
		{
			direct goto TalkInit
		}
		transition HasArrived()
		{
			direct goto TalkInit
		}
		transition IsTimerUp(1)
		{
			goto GetTarget
		}
	}

	state TalkInit
	{
		action StopLook()
		action SetStandStill()
		action SetRandomTimerBetween(1, 2.0, 3.0)
		action LookAtRandomPositionExtent(0.7, 0.9, 0.5)
		action PlayAnim("call", "02_lp", false)
		action SendEnclosureEvent("call_social")
		transition
		{
			goto Talk
		}
	}

	state Talk
	{
		transition IsTimerUp(1)
		{
			action LookEnd()
			goto SetMoveMode
		}
	}

;//------------------------ Exit States Begin ----------------------
	exitstart state CleanUp
	{
		action StopLook()
		action SetRandomTimerBetween(1, 20.0, 30.0)
		transition
		{
			goto CleanUpIdleWait
		}
	}

	state CleanUpIdleWait
	{
		transition IsAutoMovementBlendOn() == false
		{
			action SetRandomTimerBetween(1, 20.0, 30.0)
			goto WaitForAnimFinish
		}
		transition IsIdleLocked() == false
		{
			goto CleanUpEnd
		}
		transition IsTimerUp(1)
		{
			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
	{
		action SetAutoMovementBlend(true)
	}
;------------------------ Exit States End ----------------------
}
