-- LUA Script - precede every function and global member with lowercase name of script + '_main'
-- Default script - does nothing.
local state = {}
local walking = {}
local curious = {}
local attacking = {}
local aggressive = {}
local approaching = {}
local animationcomplete = {}
local hitting = {}
local attacktype = {}
local xpos = {}
local zpos = {}
local ypos = {}
local setpos = {}
local tooClose = {}
local timer = {}
local timed = {}
local started = {}
local mutantpos = {}
local maxdistance = {}
local direction = {}
local health = {}
local healthset = {}
local range = {}
local threatCount = {}
local threat = {}
local soundPlayed = {}
local goalx = {}
local goalz = {}

function mutanthead_init(e)
state[e] = "walking"
walking[e] = 1
curious[e] = 0
attacking[e] = 0
aggressive[e] = 0
approaching[e] = 0
animationcomplete[e] = 1
hitting[e] = 0
attacktype[e] = 0
xpos[e] = 0
zpos[e] = 0
ypos[e] = 0
setpos[e] = 0
tooClose[e] = 0
timer[e] = 0
timed[e] = 0
started[e] = 0
mutantpos[e] = 0
maxdistance[e] = 0
direction[e] = 0
health[e] = 1000
healthset[e] = 0
ai_soldier_pathindex[e] = -1
range[e] = 2000
threatCount[e] = 0
threat[e] = 0
soundPlayed[e] = 0
goalx[e] = 0
goalz[e] = 0
end

function mutanthead_main(e)

AIObjNo = g_Entity[e]['obj']

if g_PlayerHealth < 1 then 
	mutanthead_init(e)
end


if healthset[e] == 0 then
	health[e] = g_Entity[e]['health']
	healthset[e] = 1
end

if setpos[e] == 0 then
	xpos[e] = g_Entity[e]['x']
	zpos[e] = g_Entity[e]['z']
	ypos[e] = g_Entity[e]['y']
	setpos[e] = 1
	end

if GoalmutantheadDistance(e) < 100 then
	tooClose[e] = 1
	else
	tooClose[e] = 0
end

if g_Entity[e]['health'] < health[e] then	
	aggressive[e] = 1
end
	
if aggressive[e] == 0 and animationcomplete[e] == 1 then

if GetPlayerDistance(e) > 800 then
	if state[e] ~= "walking" then
		g_Entity[e]['animating'] = 0
			state[e] = "walking"
			end
	walking[e] = 1
	attacking[e] = 0
	approaching[e] = 0
		
elseif GetPlayerDistance(e) < 800 and GetPlayerDistance(e) > 70 then
	if state[e] ~= "approaching" then
		g_Entity[e]['animating'] = 0
			state[e] = "approaching"
			end
	walking[e] = 0
	attacking[e] = 0
	approaching[e] = 1
	
elseif GetPlayerDistance(e) < 70 then
	if state[e] ~= "attacking" then
		g_Entity[e]['animating'] = 0
			state[e] = "attacking"
	end
	walking[e] = 0
	approaching[e] = 0
	attacking[e] = 1
end
elseif aggressive[e] == 1 and animationcomplete[e] == 1 then
	if GetPlayerDistance(e) < 70 then
		if state[e] ~= "attacking" then
			g_Entity[e]['animating'] = 0
			state[e] = "attacking"
			end
		walking[e] = 0
		attacking[e] = 1
		approaching[e] = 0
	end
	if GetPlayerDistance(e) > 110 and GetPlayerDistance(e) < 1000 then
		if state[e] ~= "approaching" then
			g_Entity[e]['animating'] = 0
			state[e] = "approaching"
			end
		walking[e] = 0
		attacking[e] = 0
		approaching[e] = 1
	end
	if GetPlayerDistance(e) > 1000  then
		if state[e] ~= "walking" then
			g_Entity[e]['animating'] = 0
			state[e] = "walking"
			end
		walking[e] = 1
		attacking[e] = 0
		approaching[e] = 0
		aggressive[e] = 0
	end
	


end

if walking[e] == 1 then
	ModulateSpeed(e,1)
	
	-- MoveForward(e,50)
	 --RotateToPointSlowly(e,goalx[e],goalz[e],0.8)
	CharacterControlManual(e)
	AISetEntityControl(AIObjNo,AI_MANUAL)
	AIEntityGoToPosition(g_Entity[e]['obj'],goalx[e],goalz[e])
	MoveForward(e,75)
	RotateToPointSlowly(e,goalx[e],goalz[e],9)
	if (GetTimer(e) - timer[e] > (10000 + math.random(-2000,2000))) or tooClose[e] == 1 then 
		goalx[e] = xpos[e] + math.random(-range[e],range[e])
		goalz[e] = zpos[e] + math.random(-range[e],range[e])
		timer[e] = GetTimer(e)
	end

end

if approaching[e] == 1 then
	CharacterControlLimbo(e)
	SetAnimationSpeed(e,1.5)
	if g_Entity[e]['animating'] == 0 then
		SetAnimationFrames(31,60)
		LoopAnimation(e)
		g_Entity[e]['animating'] = 1
	end
	RotateToPlayerSlowly(e,8)
	MoveForward(e,150)
	if GetAnimationFrame(e) == 59 then
		PlaySound(e,0)
	end
 end
 
if attacking[e] == 1 then
	StopSound(e,0)
	CharacterControlLimbo(e)
	SetAnimationSpeed(e,1)
	RotateToPlayer(e)
if GetAnimationFrame(e) == 75 then
	PlaySound(e,1)
end
	
	if g_Entity[e]['animating'] == 0 then
		SetAnimationFrames(51,90)
		PlayAnimation(e)
		g_Entity[e]['animating'] = 1
	end
	if GetAnimationFrame(e)== 75 then
		hitting[e] = 1
		PlaySound(e,1)
	else
		hitting[e] = 0
	end
	if GetAnimationFrame(e) == 61 then
		animationcomplete[e] = 0
	end
	if GetAnimationFrame(e) == 90 then
		animationcomplete[e] = 1
	end
	if hitting[e] == 1 and GetPlayerDistance(e) < 100 then
		HurtPlayer(e,10)
	end
end
if g_PlayerHealth < 1 then
	StopSound(e,0)
	StopSound(e,1)
	mutanthead_init(e)
end
end

 
 function mutantheadDistance(e)
  mutantheadDX = (g_Entity[e]['x'] - xpos[e])
  mutantheadDY = (g_Entity[e]['y'] - ypos[e])
  mutantheadDZ = (g_Entity[e]['z'] - zpos[e])
  if math.abs(mutantheadDY) > 100 then 
   mutantheadDY = mutantheadDY * 4
  end
  return math.sqrt(math.abs(mutantheadDX*mutantheadDX)+math.abs(mutantheadDY*mutantheadDY)+math.abs(mutantheadDZ*mutantheadDZ));
end  

function GoalmutantheadDistance(e)
if g_Entity[e] ~= nil and g_Entity[e] ~= 0 then
local disx = g_Entity[e]['x'] - goalx[e]
local disz = g_Entity[e]['z'] - goalz[e]
return math.sqrt(disx^2 + disz^2)
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


