local state = {}
local health = {}
local destx = {}
local destz = {}
local speed = {}
local patrol_speed = {}
local alert_speed = {}
local angle = {}
local angle_found = {}
local turn_speed = {}
local damage_cannon = {}
local fire_delay_cannon = {}
local fire_delay_cannon_init = {}
local dest_angle = {}
local turret_angle = {}
local dest_turret_angle = {}
local machinegun_range = {}
local machinegun_delay = {}
local machinegun_damage = {}
local fired = {}
local cannon_fire_range = {}
local patrol_range = {}
local waypoint_angle = {}
local soundplaying = 0


function tank_init(e)
state[e] = "patrol"

--how fast the tank moves while patroling
patrol_speed[e] = 100
--how fast the tank moves when actively hunting the player
alert_speed[e] = 200
--damage applied to player on cannon blast hit
damage_cannon[e] = 10
--delay before the cannon fires
fire_delay_cannon[e] = 1300 -- keep about same length as cannon fire sound
--range player needs to be within to start targeting cannon / fire
cannon_fire_range[e] = 750
--if player is outside this distance then patrol mode is active
patrol_range[e] = 2000
--how fast the tank turns (doubles while patroling - i.e. twice as fast)
turn_speed[e] = 0.25
--range at with player can be hit by machine gun (player needs to be at front of tank)
machinegun_range[e] = 650
--damage per hit for machine gun (damage is applied once every 0.15 seconds)
machinegun_damage[e] = 1


------
fire_delay_cannon_init[e] = fire_delay_cannon[e]
angle_found[e] = -1
turret_angle[e] = 1100
machinegun_delay[e] = 150
fired[e] = 0
dest_turret_angle[e] = 0
end --init


function tank_main(e)

if health[e] == nil then
health[e] = g_Entity[e]['health']
StartTimer(e)
else
if ai_soldier_pathindex[e] == nil then
SetCharacterSound(e,"dagored")
ai_soldier_pathindex[e] = -1
else

--find the angle required to face the player
x = g_PlayerPosX - g_Entity[e]['x']
z = g_PlayerPosZ - g_Entity[e]['z']
angle[e] = math.atan2(x,z)
angle[e] = angle[e] * (180.0 / math.pi)
if angle[e] < 0 then
angle[e] = 360 + angle[e]
elseif angle[e] > 360 then
angle[e] = angle[e] - 360
end


if angle_found[e] < 0 then
local x = g_PlayerPosX - g_Entity[e]['x']
local z = g_PlayerPosZ - g_Entity[e]['z']
angle[e] = math.atan2(x,z)
angle[e] = angle[e] * (180.0 / math.pi)
if angle[e] < 0 then
angle[e] = 360 + angle[e]
elseif angle[e] > 360 then
angle[e] = angle[e] - 360
end
SetRotation(e,0,angle[e],0)
angle_found[e] = angle[e]
dest_angle[e] = angle[e]
end

--patrol code
if state[e] == "patrol" then
speed[e] = patrol_speed[e]
if g_Entity[e]['health'] < health[e] then
speed[e] = alert_speed[e]
state[e] = "alert"
end
else
if health[e] == g_Entity[e]['health'] and GetPlayerFlatDistance(e) > patrol_range[e] then
state[e] = "patrol"
end

end

if GetPlayerFlatDistance(e) > patrol_range[e] and state[e] == "patrol" then
if g_Entity[e]['animating'] == 0 then
	SetAnimationFrames(0,360)
	g_Entity[e]['animating'] = 1
	PlayAnimation(e)
end
	
  if ai_soldier_pathindex[e] == -1 then
   ai_soldier_pathindex[e] = -2
   PathIndex = -1
   PathPointIndex = -1
   pClosest = 99999
   for pa = 1, AIGetTotalPaths(), 1 do
    for po = 1 , AIGetPathCountPoints(pa), 1 do
     pDX = g_Entity[e]['x'] - AIPathGetPointX(pa,po)
     pDZ = g_Entity[e]['z'] - AIPathGetPointZ(pa,po)
     pDist = math.sqrt(math.abs(pDX*pDX)+math.abs(pDZ*pDZ))
     if pDist < pClosest and pDist < 400 then
      pClosest = pDist
      PathIndex = pa
	  PathPointIndex = po
     end
    end -- po
   end -- pa
   if PathIndex > -1 then
    ai_soldier_pathindex[e] = PathIndex
	ai_path_point_index[e] = PathPointIndex
	ai_path_point_direction[e] = 1
	ai_path_point_max[e] = AIGetPathCountPoints(ai_soldier_pathindex[e])
   end   
  end
  if ai_soldier_pathindex[e] > -1 then 
   ai_patrol_x[e] = AIPathGetPointX(ai_soldier_pathindex[e],ai_path_point_index[e])
   ai_patrol_z[e] = AIPathGetPointZ(ai_soldier_pathindex[e],ai_path_point_index[e])
	--check waypoint rotation compared to tanks and update as needed
	waypoint_angle[e] = RotateToPoint(e,ai_patrol_x[e],ai_patrol_z[e])
   if waypoint_angle[e] < angle_found[e] - 3 or waypoint_angle[e] > angle_found[e] + 3 then
   if angle_found[e] > 180 then
   if waypoint_angle[e] > angle_found[e] - 180 and waypoint_angle[e] < angle_found[e] then
   angle_found[e] = angle_found[e] - (turn_speed[e] * 2)
   else
   angle_found[e] = angle_found[e] + (turn_speed[e] * 2)
   end
   else
   if waypoint_angle[e] < angle_found[e] + 180 and waypoint_angle[e] > angle_found[e] then
   angle_found[e] = angle_found[e] + (turn_speed[e] * 2)
   else
   angle_found[e] = angle_found[e] - (turn_speed[e] * 2)
   end
   end
   end
   --keep angle within 0 ~ 360
	if angle_found[e] < 0 then
	angle_found[e] = 360 + angle_found[e]
	elseif angle_found[e] > 360 then
	angle_found[e] = angle_found[e] - 360
	end
   turret_angle[e] = angle_found[e]
   SetRotation(e,0,angle_found[e],0)
    if soundplaying == 0 then
	PlayCharacterSound(e,"onHurt")
	end
   MoveForward(e,speed[e])
   tDistX = g_Entity[e]['x'] - ai_patrol_x[e]
   tDistZ = g_Entity[e]['z'] - ai_patrol_z[e]
   DistFromPath = math.sqrt(math.abs(tDistX*tDistX)+math.abs(tDistZ*tDistZ))	
   if DistFromPath < 150 then
	if ai_path_point_direction[e] == 1 then
	 ai_path_point_index[e] = ai_path_point_index[e] + 1
	 if ( ai_path_point_index[e] > ai_path_point_max[e] ) then
	  ai_path_point_index[e] = ai_path_point_max[e] - 1
	  ai_path_point_direction[e] = 0
	 end
	else
	 ai_path_point_index[e] = ai_path_point_index[e] - 1
	 if ( ai_path_point_index[e] < 1 ) then
	  ai_path_point_index[e] = 2
	  ai_path_point_direction[e] = 1
	 end		
	end
   end
  end
elseif GetPlayerFlatDistance(e) > cannon_fire_range[e] then

if soundplaying == 0 then

PlayCharacterSound(e,"onHurt")
end
--compare angle to face player with our tank's current angle and update as needed
--if tank isn't already facing player then
if angle_found[e] < angle[e] - 0.5 or angle_found[e] > angle[e] + 0.5 then
--check tank angle is more than 180 check for player on left (to avoid 360/0 wrap confusion)
if angle_found[e] > 180 then
--if player is to the left of tank turn left otherwise turn right
if angle[e] > angle_found[e] - 180 and angle[e] < angle_found[e] then
angle_found[e] = angle_found[e] - turn_speed[e]
else
angle_found[e] = angle_found[e] + turn_speed[e]
end
--if tank angle is less than 180 check if player is on right (to avoid 360/0 wrap confusion)
else
if angle[e] < angle_found[e] + 180 and angle[e] > angle_found[e] then
angle_found[e] = angle_found[e] + turn_speed[e]
else
angle_found[e] = angle_found[e] - turn_speed[e]
end
end
end


--keep angle within 0 ~ 360
if angle_found[e] < 0 then
angle_found[e] = 360 + angle_found[e]
elseif angle_found[e] > 360 then
angle_found[e] = angle_found[e] - 360
end
turret_angle[e] = angle_found[e]



--update the new angle to be closer to player's position
SetRotation(e,0,angle_found[e],0)

if dest_turret_angle[e] > 1101 and dest_turret_angle[e] < 1460 then
if dest_turret_angle[e] < 1280 then
dest_turret_angle[e] = dest_turret_angle[e] - 2
turret_angle[e] = turret_angle[e] - 2
else
dest_turret_angle[e] = dest_turret_angle[e] + 2
turret_angle[e] = turret_angle[e] + 2
end
if g_Entity[e]['animating'] == 0 then
SetAnimationFrames(dest_turret_angle[e],dest_turret_angle[e])
g_Entity[e]['animating'] = 1
PlayAnimation(e)
end
else
dest_turret_angle[e] = 1100
turret_angle[e] = 1100
MoveForward(e,speed[e])
if g_Entity[e]['animating'] == 0 then
SetAnimationFrames(0,360)
g_Entity[e]['animating'] = 1
PlayAnimation(e)
end
end



fire_delay_cannon[e] = GetTimer(e) + fire_delay_cannon_init[e]
else
state[e] = "attack"
health[e] = g_Entity[e]['health']
if GetPlayerFlatDistance(e) <= machinegun_range[e] then
if angle[e] >= angle_found[e] - 17.5 and angle[e] <= angle_found[e] + 17.5 then
soundplaying = 1
if GetTimer(e) > machinegun_delay[e] then
PlayCharacterSound(e,"onAggro")
HurtPlayer(e,machinegun_damage[e])
machinegun_delay[e] = GetTimer(e) + 150
end
else
machinegun_delay[e] = GetTimer(e) + 150
soundplaying = 0
end
end


if GetAnimationFrame(e) <= 360 then
StopAnimation(e)
end

if g_Entity[e]['animating'] == 0 then
if angle[e] > angle_found[e] then
dest_turret_angle[e] = 1461 +  angle_found[e] - angle[e]
else
dest_turret_angle[e] = 1101 + angle_found[e] - angle[e]
end
if turret_angle[e] <= dest_turret_angle[e] - (turn_speed[e] * 25) or turret_angle[e] >= dest_turret_angle[e] + (turn_speed[e] * 25) then
if turret_angle[e] > 1280 then
if dest_turret_angle[e] > turret_angle[e] - 180 and dest_turret_angle[e] < turret_angle[e] then
turret_angle[e] = turret_angle[e] - (turn_speed[e] * 25)
else
turret_angle[e] = turret_angle[e] + (turn_speed[e] * 25)
end
else
if dest_turret_angle[e] < turret_angle[e] + 180 and dest_turret_angle[e] > turret_angle[e] then
turret_angle[e] = turret_angle[e] + (turn_speed[e] * 25)
else
turret_angle[e] = turret_angle[e] - (turn_speed[e] * 25)
end
end
end
if turret_angle[e] < 1100 then
turret_angle[e] = turret_angle[e] + 360
elseif turret_angle[e] > 1460 then
turret_angle[e] =  turret_angle[e] - 360
end


SetAnimationFrames(turret_angle[e],turret_angle[e])
g_Entity[e]['animating'] = 1
PlayAnimation(e)
end

--if we are facing the player
if dest_turret_angle[e] ~= nil then
if GetAnimationFrame(e) >= dest_turret_angle[e] - (turn_speed[e] * 25) and GetAnimationFrame(e) <= dest_turret_angle[e] +  (turn_speed[e] * 25) then
--if haven't fired lately then fire! :)
--FireWeapon(e)
soundplaying = 2
if GetTimer(e) >= fire_delay_cannon[e] then
fired[e] = 1
PlayCharacterSound(e,"onHurtPlayer")
--FireWeapon(e)
if GetTimer(e) >= fire_delay_cannon[e] + (fire_delay_cannon_init[e] * 0.5) and fired[e] == 1 then
--FireWeapon(e)
HurtPlayer(e,damage_cannon[e])
soundplaying = 0
fire_delay_cannon[e] = GetTimer(e) + fire_delay_cannon_init[e]
fired[e] = 0
end

end
else
fire_delay_cannon[e] = GetTimer(e) + fire_delay_cannon_init[e]
fired[e] = 0
soundplaying = 0
end
end
end


end
end
end --main


function tank_exit(e)

end --exit


function RotateToPoint(e,x,z)
if g_Entity[e] ~= nil and x > 0 and z > 0 then
local destx = x - g_Entity[e]['x']
local destz = z - g_Entity[e]['z']
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
--SetRotation(e,0,angle,0)
return angle
end
end

function GetPlayerFlatDistance(e)
  local tPlayerDX = (g_Entity[e]['x'] - g_PlayerPosX)
  local tPlayerDZ = (g_Entity[e]['z'] - g_PlayerPosZ)
  return math.sqrt(math.abs(tPlayerDX*tPlayerDX)+math.abs(tPlayerDZ*tPlayerDZ));
end