


--when to start being able to attack
local attack_range = 120
--delay before resetting state (if cant see player etc)
local delay_init = 2000
--time before warning becomes attacking
local w2a_delay_init = 800
--how long before dead body gets removed
local remove_delay_init = 3000
--damage per attack
local damage = 25
--how fast to run
local run_speed = 125

--animation speed for idle
local idle_speed = 1
--animation speed for walking
local move_speed = 1.0
--animation speed for warning
local warning_speed = 3
--animation speed for attacking
local attack_speed = 2 
--animation speed for dying
local die_speed = 1

local remove_delay = {}
local state = {}
local health = {}
local delay = {}
local w2a = {}
local hit = {}



function zombie2_crawl_init(e)
state[e] = "wander"
delay[e] = delay_init
hit[e] = 0
end


function zombie2_crawl_main(e)
EntObjNo = g_Entity[e]['obj']
if health[e] == nil then
health[e] = g_Entity[e]['health'] + 5000
SetEntityHealth(e,g_Entity[e]['health'] + 5000)
ai_soldier_pathindex[e] = -1
elseif g_Entity[e]['health'] > 5000 then


if state[e] == "wander" then
CharacterControlUnarmed(e)
if g_Entity[e]['health'] < health[e] then
StopAnimation(e)
CharacterControlLimbo(e)
state[e] = "attack"
delay[e] = GetTimer(e) + delay_init
PlaySound(e,1)
else
if g_Entity[e]['plrvisible'] == 1 or AIGetEntityHeardSound(g_Entity[e]['obj']) == 1 then
StopAnimation(e)
CharacterControlLimbo(e)
w2a[e] = GetTimer(e) + w2a_delay_init
state[e] = "attack" --change line to "alert" for warning
delay[e] = GetTimer(e) + delay_init
PlaySound(e,1)
else

  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 < 200 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])
   SetCharacterToWalk(e)
   ModulateSpeed(e,move_speed)
   AIEntityGoToPosition(g_Entity[e]['obj'],ai_patrol_x[e],ai_patrol_z[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 < 50 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
   else
   ModulateSpeed(e,idle_speed)
   LookAtPlayer(e)
   CharacterControlLimbo(e)
   if g_Entity[e]['animating'] == 0 then
   --idle
  SetAnimation(2)
  PlayAnimation(e)
  g_Entity[e]['animating'] = 1 
  end
  end
  end
  end

 elseif state[e] == "alert" then

 LoopSound(e,1)
 LookAtPlayer(e)
 ModulateSpeed(e,warning_speed)
 CharacterControlLimbo(e)
 if g_Entity[e]['animating'] == 0 then
 StopAnimation(e)
  RotateToPlayer(e)
  ModulateSpeed(e,warning_speed)
  SetAnimationFrames(280,349)
 PlayAnimation(e)
 g_Entity[e]['animating'] = 1 
 end
 if GetPlayerDistance(e) <= attack_range or g_Entity[e]['health'] < health[e] or GetTimer(e) > w2a[e] or (GetAnimationFrame(e) > 280 and GetAnimationFrame(e) < 349) then
 StopAnimation(e)
 CharacterControlLimbo(e)
 state[e] = "attack"
 delay[e] = GetTimer(e) + delay_init
 StopSound(e,1)
 end
 if g_Entity[e]['plrvisible'] == 0 and AIGetEntityHeardSound(g_Entity[e]['obj']) == 0 and GetTimer(e) >= delay[e] then
 StopAnimation(e)
 CharacterControlLimbo(e)
 state[e] = "wander"
 StopSound(e,1)
 end
 
 
 elseif state[e] == "attack" then
--StopSound(e,1)
 LookAtPlayer(e)
 SetCharacterToRun(e)
 if GetPlayerDistance(e) > attack_range then
  --CharacterControlUnarmed(e)
  if g_Entity[e]['animating'] == 0 then
   ModulateSpeed(e,move_speed)
  SetAnimation(1)
  LoopAnimation(e)
  g_Entity[e]['animating'] = 1 
  end
  RotateToPlayer(e)
  if GetAnimationFrame(e) > 240 and GetAnimationFrame(e) < 262 then
  MoveForward(e,run_speed)
  end
 --AIEntityGoToPosition(g_Entity[e]['obj'],g_PlayerPosX,g_PlayerPosZ)
 else
 --AIEntityGoToPosition(EntObjNo,g_Entity[e]['x'],g_Entity[e]['z'])
 --AIEntityStop(e)
 health[e] = g_Entity[e]['health']
 if GetAnimationFrame(e) < 280 or GetAnimationFrame(e) > 349 then
 StopAnimation(e)
 end
 CharacterControlLimbo(e)
 RotateToPlayer(e)
 if g_Entity[e]['animating'] == 0 then
 ModulateSpeed(e,attack_speed)
  hit[e] = 0
 SetAnimationFrames(280,349)
 PlayAnimation(e)
 g_Entity[e]['animating'] = 1 
 end
 if GetAnimationFrame(e) >= 340 and GetAnimationFrame(e) <= 349 and hit[e] == 0 then
 FireWeapon(e)
 HurtPlayer(e,damage)
 hit[e] = 1
 end
 end
 if g_Entity[e]['plrvisible'] == 0 and AIGetEntityHeardSound(g_Entity[e]['obj']) == 0 and GetTimer(e) >= delay[e] then
 StopAnimation(e)
 CharacterControlLimbo(e)
 state[e] = "wander"
 end
 
 
 end --state
 
 --Prompt(state[e].."   "..GetAnimationFrame(e))
 else

 if state[e] ~= "dead" then
 PlaySound(e,1)
 StopAnimation(e)
 AIEntityStop(e)
 CharacterControlLimbo(e)
 state[e] = "dead"
 remove_delay[e] = GetTimer(e) + remove_delay_init
 else
 CollisionOff(e)
 ModulateSpeed(e,die_speed)
 if g_Entity[e]['animating'] == 0 and hit[e] < 2 then
  CharacterControlLimbo(e)
 RotateToPlayer(e)
 SetAnimation(4)
 hit[e] = 2
 PlayAnimation(e)
 g_Entity[e]['animating'] = 1 
 end
 if GetAnimationFrame(e) >= 405 and GetTimer(e) >= remove_delay[e] then
 Hide(e)
 Destroy(e)
 end
 end

--

 --Prompt(state[e])
end --health
end --main


function zombie2_crawl_exit(e)

end