-- LUA Script - precede every function and global member with lowercase name of script + '_main'
-- Default script - does nothing.
local alert = {}
local fleeing = {}
local idle = {}
local approaching = {}
local attacking = {}
local state = {}
local fled = {}
local animComplete = {}
local fleeSet = {}
local fleeRange = {}
local fleex = {}
local fleez = {}

function mutant_init(e)
alert[e] = 0
fleeing[e] = 0
idle[e] = 1
approaching[e] = 0
attacking[e] = 0
state[e] = "relaxed"
fled[e] = 0
animComplete[e] = 0
fleeSet[e] = 0
fleeRange[e] = 1000
fleex[e] = 0
fleez[e] = 0
end

function mutant_main(e)

if g_PlayerHealth < 1 then
	mutant_init(e)
end
--Prompt(GetPlayerDistance(e).."  "..alert[e].."  "..state[e].."  attacking-"..attacking[e].."  Approaching-"..approaching[e])
if GetPlayerDistance(e) < 400 or EntityLookingAtPlayer(e,1000,60) == 1 then
	alert[e] = 1
end

if GetPlayerDistance(e) > 2000 then
	alert[e] = 0
end

if alert[e] == 0 then
	if state[e] ~= "idle" then
			g_Entity[e]['animating'] = 0
			state[e] = "idle"
	end
	approaching[e] = 0
	idle[e] = 1
	attacking[e] = 0
	fleeing[e] = 0
end

if alert[e] == 1 then

if PlayerLooking(e,2000,45) == 1 then

	if GetPlayerDistance(e) < 500 and animComplete[e] == 1then
		if state[e] ~= "fleeing" then
			g_Entity[e]['animating'] = 0
			state[e] = "fleeing"
		end
		approaching[e] = 0
		idle[e] = 0
		attacking[e] = 0
		fleeing[e] = 1
		fled[e] = 0
	end
	
	if GetPlayerDistance(e) > 500 and GetPlayerDistance(e) < 800 and fled[e] == 1 and animComplete[e] == 1 then
		if state[e] ~= "idle" then
			g_Entity[e]['animating'] = 0
			state[e] = "idle"
		end
		approaching[e] = 0
		idle[e] = 1
		attacking[e] = 0
		fleeing[e] = 0
	end
	
	if GetPlayerDistance(e) > 1000 and animComplete[e] == 1 then 
		if state[e] ~= "approaching" then
			g_Entity[e]['animating'] = 0
			state[e] = "approaching"
		end
		approaching[e] = 1
		idle[e] = 0
		attacking[e] = 0
		fleeing[e] = 0
		fled[e] = 1
		fleeSet[e] = 0
	end

else
	if GetPlayerDistance(e) > 75 and animComplete[e] == 1 then
		if state[e] ~= "approaching" then
			g_Entity[e]['animating'] = 0
			state[e] = "approaching"
		end
		approaching[e] = 1
		idle[e] = 0
		attacking[e] = 0
		fleeing[e] = 0
		fled[e] = 0
		
	elseif GetPlayerDistance(e) < 75 then
		if state[e] ~= "attacking" and animComplete[e] == 1 then
			g_Entity[e]['animating'] = 0
			state[e] = "attacking"
		end
		approaching[e] = 0
		idle[e] = 0
		attacking[e] = 1
		fleeing[e] = 0
		fled[e] = 0
	end
	
end
end

if idle[e] == 1 then
	ModulateSpeed(e,1)
	CharacterControlLimbo(e)
	if g_Entity[e]['animating'] == 0 then
		SetAnimationFrames(65,501)
		PlayAnimation(e)
		g_Entity[e]['animating'] = 1
	end
	animComplete[e] = 1
end

if fleeing[e] == 1 then
	ModulateSpeed(e,1.5)
	CharacterControlLimbo(e)
	if g_Entity[e]['animating'] == 0 then
		SetAnimationFrames(1,30)
		PlayAnimation(e)
		g_Entity[e]['animating'] = 1
	end
	
	if fleeSet[e] == 0 then
		if g_PlayerPosX < g_Entity[e]['x'] then
			fleex[e] = g_Entity[e]['x'] + fleeRange[e]
		else
			fleex[e] = g_Entity[e]['x'] - fleeRange[e]
		end
		if g_PlayerPosZ < g_Entity[e]['z'] then
			fleez[e] = g_Entity[e]['z'] + fleeRange[e]
		else
			fleez[e] = g_Entity[e]['z'] - fleeRange[e]
		end
		fleeSet[e] = 1
	end
	if fleeDistance(e) < 100 and fleeing[e] == 1 then
		fleeSet[e] = 0
	end
	
	RotateToPointSlowly(e,fleex[e],fleez[e],5)
	MoveForward(e,200)
	if GetAnimationFrame(e) == 1 or GetAnimationFrame(e) == 30 then
		animComplete[e] = 1
	else
		animComplete[e] = 0
	end
end

if approaching[e] == 1 then
	ModulateSpeed(e,1)
	CharacterControlLimbo(e)
	if g_Entity[e]['animating'] == 0 then
		SetAnimationFrames(1,30)
		PlayAnimation(e)
		g_Entity[e]['animating'] = 1
	end
RotateToPlayerSlowly(e,5)
MoveForward(e,150)
	if GetAnimationFrame(e) == 1 or GetAnimationFrame(e) == 30 then
		animComplete[e] = 1
	else
		animComplete[e] = 0
	end
end

if attacking[e] == 1 then
RotateToPlayerSlowly(e,6)
	ModulateSpeed(e,1)
	CharacterControlLimbo(e)
	if g_Entity[e]['animating'] == 0 then
		SetAnimationFrames(31,60)
		PlayAnimation(e)
		g_Entity[e]['animating'] = 1
	end 
	if GetAnimationFrame(e) == 31 or GetAnimationFrame(e) == 60 then
		animComplete[e] = 1
	else
		animComplete[e] = 0
	end
	if GetAnimationFrame(e) == 45 and GetPlayerDistance(e) < 75 then
		HurtPlayer(e,2)
		PlaySound(e,4)
	end
end

end

function PlayerLooking(e,dis,v)
if g_Entity[e] ~= nil then
if dis == nil then
dis = 3000
end
if v == nil then
v = 0.5
end
if GetPlayerDistance(e) <= dis then
local destx = g_Entity[e]['x'] - g_PlayerPosX
local destz = g_Entity[e]['z'] - g_PlayerPosZ
local angle = math.atan2(destx,destz)
angle = angle * (180.0 / math.pi)
 
if angle <= 0 then
angle = 360 + angle
elseif angle > 360 then
angle = angle - 360
end
while g_PlayerAngY < 0 or g_PlayerAngY > 360 do
if g_PlayerAngY <= 0 then
g_PlayerAngY = 360 + g_PlayerAngY
elseif g_PlayerAngY > 360 then
g_PlayerAngY = g_PlayerAngY - 360
end
end
local L = angle - v
local R = angle + v
if L <= 0 then
L = 360 + L 
elseif L > 360 then
L = L - 360
end
if R <= 0 then
R = 360 + R
elseif R > 360 then
R = R - 360
end
 
if (L < R and math.abs(g_PlayerAngY) > L and math.abs(g_PlayerAngY) < R) then
return 1
elseif (L > R and (math.abs(g_PlayerAngY) > L or math.abs(g_PlayerAngY) < R)) then
return 1
else
return 0
end
else
return 0
end
end
end


function EntityLookingAtPlayer(e,dis,v)
if g_Entity[e] ~= nil then
if dis == nil then
dis = 3000
end
if v == nil then
v = 0.5
end
local destx = g_PlayerPosX - g_Entity[e]['x']
local destz = g_PlayerPosZ - g_Entity[e]['z']
if math.sqrt((destx*destx)+(destz*destz)) <= dis then
local angle = math.atan2(destx,destz)
angle = angle * (180.0 / math.pi)
    
if angle <= 0 then
angle = 360 + angle
elseif angle > 360 then
angle = angle - 360
end
while g_Entity[e]['angley'] < 0 or g_Entity[e]['angley'] > 360 do
if g_Entity[e]['angley'] <= 0 then
g_Entity[e]['angley'] = 360 + g_Entity[e]['angley']
elseif g_Entity[e]['angley'] > 360 then
g_Entity[e]['angley'] = g_Entity[e]['angley'] - 360
end
end
local L = angle - v
local R = angle + v
if L <= 0 then
L = 360 + L 
elseif L > 360 then
L = L - 360
end
if R <= 0 then
R = 360 + R
elseif R > 360 then
R = R - 360
end
    
if (L < R and math.abs(g_Entity[e]['angley']) > L and math.abs(g_Entity[e]['angley']) < R) then
return 1
elseif (L > R and (math.abs(g_Entity[e]['angley']) > L or math.abs(g_Entity[e]['angley']) < R)) then
return 1
else
return 0
end
else
return 0
end
end
end

function RotateToPointSlowly(e,x,z,v)
if g_Entity[e] ~= nil and x > 0 and z > 0 then
if v == nil then
v = 1 
end
local destx = x - g_Entity[e]['x']
local destz = z - g_Entity[e]['z']
local angleneeded = math.atan2(destx,destz)
angleneeded = angleneeded * (180.0 / math.pi)
if angleneeded < 0 then
angleneeded = 360 + angleneeded
elseif angleneeded > 360 then
angleneeded = angleneeded - 360
end
local current_angy = g_Entity[e]['angley']
while current_angy < 0 or current_angy > 360 do
if current_angy <= 0 then
current_angy = 360 + current_angy
elseif current_angy > 360 then
current_angy = current_angy - 360
end
end
local L = angleneeded - v
local R = angleneeded + v
if L <= 0 then
L = 360 + L 
elseif L > 360 then
L = L - 360
end
if R <= 0 then
R = 360 + R
elseif R > 360 then
R = R - 360
end
 
--if not already facing the target direction (+/- the rotation speed)
if current_angy < L or current_angy > R then
--do it in 2 halfs so we can account for 0/360 wrap
if current_angy > 180 then
if angleneeded > current_angy - 180 and angleneeded < current_angy then
current_angy = current_angy - v
else
current_angy = current_angy + v
end
else
if angleneeded < current_angy + 180 and angleneeded > current_angy then
current_angy = current_angy + v
else
current_angy = current_angy - v
end
end
SetRotation(e,0,current_angy,0)
return current_angy
end
end
end

function fleeDistance(e)
  fleeDX = (g_Entity[e]['x'] - fleex[e])
  fleeDZ = (g_Entity[e]['z'] - fleez[e])
  
  return math.sqrt(math.abs(fleeDX*fleeDX)+math.abs(fleeDZ*fleeDZ));
end 
