local alert_range = {} local state = {} local watch_range = {} local turn_speed = {} local destx = {} local destz = {} local safe_range = {} local health = {} function civillian2_init(e) watch_range[e] = 600 alert_range[e] = 300 turn_speed[e] = 4 safe_range[e] = 1000 destx[e] = 0 destz[e] = 0 CharacterControlUnarmed(e) state[e] = "normal" health[e] = -1 ai_soldier_pathindex[e] = -1 end function civillian2_main(e) EntObjNo = g_Entity[e]['obj'] CharacterControlUnarmed(e) if g_Entity[e]['health'] < health[e] then state[e] = "hurt" CharacterControlStand(e) SetCharacterToRun(e) health[e] = g_Entity[e]['health'] else health[e] = g_Entity[e]['health'] end if state[e] == "normal" then CharacterControlStand(e) SetCharacterToWalk(e) if ai_soldier_pathindex[e] == -1 then ai_soldier_pathindex[e] = -2 -- find initial waypoint path to follow PathIndex = -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; end end -- po end -- pa -- follow found path if PathIndex > -1 then ai_soldier_pathindex[e] = PathIndex; ai_path_point_index[e] = 2 ModulateSpeed(e,1.0) SetCharacterToWalk(e) ai_path_point_direction[e] = 1 ai_path_point_max[e] = AIGetPathCountPoints(ai_soldier_pathindex[e]) end end -- If we have a path then lets patrol it 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]) AIEntityGoToPosition(EntObjNo,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 < 300 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 CharacterControlFidget(e) end if GetPlayerDistance(e) <= alert_range[e] or AIGetEntityHeardSound(EntObjNo) == 1 then --or (g_MouseClick = 1 and g_PlayerGunID > 0 and GetPlayerDistance(e) < safe_range[e]) then state[e] = "alerted" --ai_soldier_pathindex[e] = -1 AIEntityGoToPosition(EntObjNo,g_Entity[e]['x'],g_Entity[e]['z']) end elseif state[e] == "alerted" then if GetPlayerDistance(e) <= watch_range[e] then RotateToPlayerSlowly(e,turn_speed[e]) if GetPlayerDistance(e) <= alert_range[e] then RotateToPlayer(e) end else state[e] = "normal" CharacterControlStand(e) end if AIGetEntityHeardSound(EntObjNo) == 1 then --CharacterControlDucked(e) state[e] = "hurt" end elseif state[e] == "hurt" then if g_Entity[e]['x'] < g_PlayerPosX then destx[e] = g_PlayerPosX - safe_range[e] else destx[e] = g_PlayerPosX + safe_range[e] end if g_Entity[e]['z'] < g_PlayerPosZ then destz[e] = g_PlayerPosZ - safe_range[e] else destz[e] = g_PlayerPosZ + safe_range[e] end SetCharacterToRun(e) AIEntityGoToPosition(EntObjNo,destx[e],destz[e]) if GetPlayerDistance(e) >= safe_range[e] then state[e] = "normal" end end end