function res = LEM_sim4_FAA_NASA(h_x,t_min,t_max,del_t,sigma,requests,...
    airways,grid)
% LEM_sim4_FAA_NASA - robust FAA-NASA vs LSD simulation - FAA-NASA part
% On input:
%     x_min (float): min x values in roads
%     x_max (float): max x values in roads
%     y_min (float): min y values in roads
%     y_max (float): max y values in roads
%     del_x (float): distance between intersections in grid road layout
%     h_x (float): headway distance
%     t_min (float): min sim time
%     t_max (float): max sim time
%     del_t (float): time step
%     sigma (float): varince (in each dimension) of flight position
%     requests (requests struct): requests info
%     airways (airways struct): airways info
%     grid (grid struct): grid info
% On output:
%     res (results struct): results of simulation
%       .requests (requests struct): list of flight requests
%       .flights (flights struct): flight info
% Call:
%     res = LEM_sim4_FAA_NASA(-18500,18500,-11500,11500,2000,50,0,5000,...
%         0.5,0.1,requests,airways,grid);
% Author:
%     T. Henderson
%     UU
%     Fall 2020
%

LOWER_ALTITUDE = 467;
UPPER_ALTITUDE = 534;

num_flights = length(requests);
vertexes = airways.vertexes;
launch_vertexes = airways.launch_vertexes;
num_launch_vertexes = length(launch_vertexes);
land_vertexes = airways.land_vertexes;
num_land_vertexes = length(land_vertexes);

flights = [];

for f = 1:num_flights
    f
    launch_index = requests(f).launch_index;
    launch_pt = vertexes(launch_vertexes(launch_index),:);
    land_index = requests(f).land_index;
    land_pt = vertexes(land_vertexes(land_index),:);
    speed = requests(f).speed;
    min_start = requests(f).launch_interval(1);;
    max_start = requests(f).launch_interval(2);
    flights(f).speed = speed;
    flights(f).start_time = min_start;
    flights(f).start_interval = [min_start,max_start];
    pt1 = launch_pt;
    altitude = LOWER_ALTITUDE + rand*(UPPER_ALTITUDE-LOWER_ALTITUDE);
    pt4 = land_pt;
    pt2 = [pt1(1:2),altitude];
    pt3 = [pt4(1:2),altitude];
    traj = [pt1,pt2;pt2,pt3;pt3,pt4];
    flights(f).traj = traj;
    len1 = norm(pt2-pt1);
    len2 = norm(pt3-pt2);
    len3 = norm(pt4-pt3);
    total_len = len1 + len2 + len3;
    flight_path = zeros(3,4);
    flight_path(1,1) = flights(f).start_time;
    flight_path(1,2) = flight_path(1,1) + len1/flights(f).speed;
    flight_path(1,3) = flights(f).speed;
    flight_path(1,4) = 1;
    flight_path(2,1) = flight_path(1,2);
    flight_path(2,2) = flight_path(2,1) + len2/flights(f).speed;
    flight_path(2,3) = flights(f).speed;
    flight_path(2,4) = 2;
    flight_path(3,1) = flight_path(2,2);
    flight_path(3,2) = flight_path(3,1) + len3/flights(f).speed;
    flight_path(3,3) = flights(f).speed;
    flight_path(3,4) = 3;
    flights(f).flight_path = flight_path;
    flights(f).d_count = 0;
    flights(f).grid_count = 0;
    flights(f).pinch_count = 0;
    flights(f).time_count = 0;
    flights(f).d_time = 0;
    flights(f).end_time = flights(f).start_time + total_len/flights(f).speed;
    index1 = find(launch_vertexes==launch_index);
    index2 = find(land_vertexes==land_index);
    flights(f).grid_els = grid.grid_lists(launch_index,land_index).grid_els;
%    flights(f).grid_els = LEM_grid_els(grid,traj(1,1:3),traj(end,4:6),M,N);
end

s_flights = [];
f = 0;
wb = waitbar(0,'Deconflict');
while f<num_flights
    waitbar(f/num_flights);
    f = f + 1;
    [f,num_flights]
    if ~isempty(flights(f).traj)
        n_flight = flights(f);
        tic;
        n_flight = LEM_deconflict(s_flights,n_flight,del_t,h_x);
        n_flight.d_time = toc;
        s_flights = [s_flights;n_flight];
    end
end
close(wb);

res.flights = s_flights;
res.grid = grid;

tch = 0;
