-- LUA Script - precede every function and global member with lowercase name of script + '_main'
-- Default script - does nothing.
local state = {}
local running = {}
local walking = {}
local idle = {}

function mandog_init(e)
state[e] = "relaxed"
running[e] = 0
walking[e] = 1
idle[e] = 0
end

function mandog_main(e)

EntObjNo = g_Entity[e]['obj']

if GetPlayerDistance(e) < 150 then
	if state[e] ~="idle" then
		g_Entity[e]['animating'] = 0
		state[e] ="idle"
	end
	running[e] = 0
	walking[e] = 0
	idle[e] = 1

	elseif GetPlayerDistance(e) > 150 and GetPlayerDistance(e) < 250 then
	if state[e] ~="walking" then
		g_Entity[e]['animating'] = 0
		state[e] ="walking"
	end
	running[e] = 0
	walking[e] = 1
	idle[e] = 0
	
	else
	if state[e] ~="running" then
		g_Entity[e]['animating'] = 0
		state[e] ="running"
	end
	running[e] = 1
	walking[e] = 0
	idle[e] = 0
end


if idle[e] == 1 then
	CharacterControlLimbo(e)
	ModulateSpeed(e,1.5)
	if g_Entity[e]['animating'] == 0 then
			SetAnimationFrames(171,470)
			LoopAnimation(e)
			g_Entity[e]['animating'] = 1
	end
	if GetAnimationFrame(e) == 314 then
		PlaySound(e,0)
	end
end

if walking[e] == 1 then
	ModulateSpeed(e,1)
	if g_Entity[e]['animating'] == 0 then
			SetAnimationFrames(37,86)
			LoopAnimation(e)
			g_Entity[e]['animating'] = 1
	end
	--RotateToPlayerSlowly(e,5)
	MoveForward(e,10)
	SetCharacterToWalk(e)
			CharacterControlUnarmed(e)
			rndx=math.random(1,360)
			rndz=math.random(1,360)
			rndx2=math.sin(rndx)*10
			rndz2=math.cos(rndz)*10
			AIEntityGoToPosition(EntObjNo,g_PlayerPosX+rndx2,g_PlayerPosZ+rndz2)
end

if running[e] == 1 then
	ModulateSpeed(e,1.5)
	if g_Entity[e]['animating'] == 0 then
			SetAnimationFrames(5,32)
			LoopAnimation(e)
			g_Entity[e]['animating'] = 1
	end
	--RotateToPlayerSlowly(e,5)
	MoveForward(e,100)
	SetCharacterToRun(e)
			CharacterControlUnarmed(e)
			rndx=math.random(1,360)
			rndz=math.random(1,360)
			rndx2=math.sin(rndx)*10
			rndz2=math.cos(rndz)*10
			AIEntityGoToPosition(EntObjNo,g_PlayerPosX+rndx2,g_PlayerPosZ+rndz2)
end

end
