function res = LEM_sim4_FAA_NASA1(x_min,x_max,y_min,y_max,del_x,h_x,t_min,...
    t_max,del_t,num_flights,launch_interval,speeds,sigma,launch_sites,...
    land_sites,requests)
% 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
%     num_flights (int): number of requested flight starts across time
%     launch_interval (float): possible launch time spread
%     speeds (1x2 vector): [min_speed, max_speed]
%     sigma (float): varince (in each dimension) of flight position
%     launch_sites (p1x3 array): ground vertexes for launch sites
%     land_sites (p2x2 array): ground vertexes for land sites
%     requests (requests struct): requests 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,5000,4,[30,30],0.1,sites1,sites2,requests);
% Author:
%     T. Henderson
%     UU
%     Fall 2020
%

LOWER_ALTITUDE = 467;
UPPER_ALTITUDE = 534;

M = ceil(x_max-x_min);
N = ceil(y_max-y_min);
grid.x_min = x_min;
grid.x_max = x_max;
grid.y_min = y_min;
grid.y_max = y_max;
grid.del_x = del_x;
grid.del_y = del_x;
x_vals = [x_min:del_x:x_max];
N = length(x_vals) - 1;
y_vals = [y_min:del_x:y_max];
M = length(y_vals) - 1;
grid.M = M;
grid.N = N;

num_launch_sites = length(launch_sites(:,1));
num_land_sites = length(land_sites(:,1));

for s1 = 1:num_launch_sites
    pt1 = launch_sites(s1,:);
    for s2 = 1:num_land_sites
        pt2 = land_sites(s2,:);
        grid_lists(s1,s2).grid_els = LEM_grid_els(grid,pt1,pt2,0.1);
    end
end

min_speed = speeds(1);
max_speed = speeds(end);
start_times = t_min + (t_max-t_min)*rand(num_flights,1);
flights = [];

for f = 1:num_flights
    f
    launch_index = randi(num_launch_sites);
    launch_pt = launch_sites(launch_index,:);
    land_index = randi(num_land_sites);
    land_pt = land_sites(land_index,:);
    speed = min_speed+rand*(max_speed-min_speed);
    min_start = start_times(f);
    max_start = min_start + launch_interval;
    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;
    flights(f).grid_els = 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;
    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;
