function res = LEM_scenario_LSD(roads,airways,requests,times,t_min,...
    t_max,del_t,sigma,hd)
% LEM_scenario_LSD - robust LSD simulation
% On input:
%     roads (road struct): road info
%     airways (airways struct): irways info
%     requests (requests struct): request info
%     times (n by 1 vector): arrival times of requests
%     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
%     hd (float): headway distance
% On output:
%     res (results struct): results of simulation
%       .roads (roads struct): road info
%       .airways (airways struct): airways info
%       .requests (requests struct): list of flight requests
%       .times (n by 1 vector): request arrival times
%       .reservations (reservations struct): actual reservations
%       .flights (flights struct): flight info
% Call:
%     res = LEM_sim4(roads,airways,requests,times,0,6000,0.5,0.1,10);
% Author:
%     T. Henderson
%     UU
%     Fall 2020
%

FAILED = 0;

num_lanes = length(airways.lanes(:,1));
for k = 1:num_lanes
    reservations(k).flights = [];
end
flights = [];
num_flights = 0;
t = -del_t;
wb = waitbar(0,'Flight Scenario LSD');
while t<t_max
    waitbar(t/t_max);
    t = t + del_t;
    [t,t_max];
    new_flights = [];
    indexes = find(times>=t&times<t+del_t);
    if ~isempty(indexes)
        num_indexes = length(indexes);
        for n = 1:num_indexes
            [reservations,new_flights] = LEM_gen_reservations(airways,...
                flights,reservations,requests(indexes(n)),t,1,hd);
            if new_flights.type~=FAILED
                flights(end+1).type = new_flights(1).type;
                flights(end).decon_time = new_flights(1).decon_time;
                flights(end).plan = new_flights(1).plan;
                flights(end).route = new_flights(1).route;
                flights(end).telemetry = new_flights(1).telemetry;
            else
                flights(end+1).type = FAILED;
                flights(end).decon_time = new_flights(1).decon_time;
                flights(end).plan = new_flights(1).plan;
                flights(end).route = new_flights(1).route;
                flights(end).telemetry = new_flights(1).telemetry;
            end
        end
    end
    flights = LEM_advance_flights(flights,t,del_t,sigma);
end
close(wb);
res.roads = roads;
res.airways = airways;
res.requests = requests;
res.times = times;
res.reservations = reservations;
res.flights = flights;
