function res = LEM_EQ_LBSD1(airways,depots,num_deliveries,num_UAS,speed,...
    hd,EB)
% LEM_EQ_LBSD1 - earthquake FAA/NASA baseline scenario
% On input:
%     airways (airways struct): airways info
%     depots (kx1 array): depot indexes (in vertexes)
%     num_deliveries (int): number of deliveries per vertex
%     num_UAS (int): number of UAS
%     speed (float): in feet per second
%     hd (float): feet headway separation
%     EB (Booelan): 1 if load pre-computed EB cells, else compute them
% On output:
%     res (): results of simulation
%       .flights (flights struct): flight info
%       .UAS (UAS struct): UAS info
%         (k).scheudule (nx4 array):
%            launch index,land index,launch time,land time
% Call:
%     res = LEM_EQ_LBSD1(roads.vertexes,depots_EB,1,200,100,6,1);
% Author:
%     T. Henderson
%     UU
%     Spring 2021
%

MIN_ALTITUDE = 467;
MAX_ALTITUDE = 534;
MAX_DELAY = 300;
LOAD_TIME = 300;
UNLOAD_TIME = 300;
PLANNED = 1;
FAILED = 0;

res = [];

% Initialize flights
flights = [];

% Initialize UAS info
for k = 1:num_UAS
    UAS(k).schedule = [0,0,0,0];
end

% Ground vertexes
vertexes = airways.vertexes;
[num_vertexes,dummy] = size(vertexes);

% Airway launch/land vertexes
launch_vertexes = airways.launch_lane_vertexes;
land_vertexes = airways.land_lane_vertexes;
lane_vertexes = airways.lane_vertexes;

% Distribute UAS across depot sites
num_depots = length(depots);
for k = 1:num_UAS
    index = rem(k-1,num_depots) + 1;
    UAS(k).schedule(1,1) = depots(index);
end

% Set up airway depot vertexes
a_depots = zeros(num_depots,1);
for k = 1:num_depots
    a_depots(k) = airways.launch_lane_vertexes(depots(k));
end

% Set up reservations
num_lanes = length(airways.lanes(:,1));
for k = 1:num_lanes
    reservations(k).flights  = [];
    reservations(k).hd = hd;
end

num_requests = 0;
% repeat the deliveries num_deliveries times
for d = 1:num_deliveries
    for s = 1:num_vertexes    % s is ground land site index
        [d,s,num_vertexes]
        if isempty(find(depots==s)) % don't deliver to depot
            out_land_index = land_vertexes(s);
            return_launch_index = launch_vertexes(s);
            % find UAS
            [u,avail_time] = LEM_LBSD_get_UAS(UAS);
            q = UAS(u).schedule(1,1);   % q is ground launch site index
            out_launch_index = launch_vertexes(q);
            return_land_index = land_vertexes(q);
            [path,v_path]  = LEM_get_path(airways,out_launch_index,...
                out_land_index,[]);
            num_requests = num_requests + 1;
            requests(num_requests).request_time = avail_time;
            requests(num_requests).launch_interval = ...
                [avail_time,avail_time+MAX_DELAY];
            requests(num_requests).speed = speed;
            requests(num_requests).path = path;
            requests(num_requests).path_vertexes = v_path;
            requests(num_requests).launch_index = out_launch_index;
            requests(num_requests).land_index = out_land_index;
            [t_reservations,fr] = LEM_gen_reservations(airways,flights,...
                reservations,requests(num_requests),1,hd);
            fr.start_interval = requests(num_requests).launch_interval;
            fr.speed = speed;
            fr.path = path;
            fr.path_vertexes = v_path;
            fr.launch_vertex = out_launch_index;
            fr.land_vertex = out_land_index;
            fr.start_time = requests(num_requests).launch_interval(1);
            fr.end_time = requests(num_requests).launch_interval(2);
            fr.traj = [];
            flights = [flights; fr];
            if fr.type==PLANNED
                UAS(u).schedule(end,2) = out_land_index;
                UAS(u).schedule(end,3) = fr.start_time;
                UAS(u).schedule(end,4) = fr.end_time;
                UAS(u).schedule = [UAS(u).schedule;out_land_index,0,0,0];
                num_requests = num_requests + 1;
                fr.start_time = fr.plan(1,1);
                fr.end_time = fr.plan(end,2);
                b_launch_vertex = return_launch_index;
                b_land_vertex = return_land_index;
                [b_path,b_v_path] = LEM_get_path(airways,...
                    b_launch_vertex,b_land_vertex,[]);
                r_time = fr.route(end,end-1) + UNLOAD_TIME;
                requests(num_requests).request_time = r_time;
                requests(num_requests).launch_interval = ...
                    [r_time,r_time+MAX_DELAY];
                requests(num_requests).speed = speed;
                requests(num_requests).path = b_path;
                requests(num_requests).path_vertexes = b_v_path;
                requests(num_requests).launch_index = b_launch_vertex;
                requests(num_requests).land_index = b_land_vertex;
                [t_reservations,fbr] = LEM_gen_reservations(airways,...
                    flights,t_reservations,requests(num_requests),1,hd);
                fbr.start_interval = requests(num_requests).launch_interval;
                fbr.speed = speed;
                fbr.path = path;
                fbr.path_vertexes = v_path;
                fbr.launch_vertex = return_launch_index;
                fbr.land_vertex = return_land_index;
                fbr.start_time = r_time;
                fbr.end_time = fbr.start_interval(2);
                fbr.traj = [];
                flights = [flights;fbr];
                if fbr.type==PLANNED
                    reservations = t_reservations;
                    UAS(u).schedule(end,2) = return_land_index;
                    UAS(u).schedule(end,3) = fbr.start_time;
                    UAS(u).schedule(end,4) = fbr.end_time;
                    UAS(u).schedule = [UAS(u).schedule;out_launch_index,...
                        0,0,0];
                else
                    flights(end-1).type = FAILED;
                end
                tch= 0;
            end
        end
    end
    tch = 0;
end
%close(wb);
res.flights = flights;
res.UAS = UAS;
res.reservations = reservations;

tch = 0;
