function res = LEM_EQ_LBSD_driver(num_deliveries,...
    num_depot_sites,num_UAS)
% LEM_EQ_LBSD driver - earthquake FAA/NASA baseline scenario
% On input:
%     num_deliveries (int): number of requested deliveries
%     num_depot_sites (int): number of launch sites (random locs)
%     num_UAS (int): number of UAS
% On output:
%     res (): results of simulation
% Call:
%     res = LEM_EQ_LBSD_driver(1,10,10);
% Author:
%     T. Henderson
%     UU
%     Spring 2021
%

MIN_ALTITUDE = 467;
MAX_ALTITUDE = 534;
SPEED = 10;
HD = 6;
MAX_DELAY = 100;
LOAD_TIME = 300;
UNLOAD_TIME = 300;
PLANNED = 1;
FAILED = 0;

res = [];

% Initialize time
t_start = 0;
t_end = 24*60*60;

% Initialize flights
flights = [];

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

load('EB_EQ_airways.mat');   % Eastbench airways
num_vertexes = length(EB_airways.land_lane_vertexes);
num_lanes = length(EB_airways.lanes(:,1));
for k = 1:num_lanes
    reservations(k).flights  = [];
    reservations(k).hd = HD;
end

% Set depot sites
p = randperm(num_vertexes);
depot_sites = p(1:num_depot_sites);
clear p

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

request_times = sort(t_end*rand(num_deliveries*num_vertexes,1));
%destinations = [];
destinations = randperm(3409)';
%for k = 1:num_deliveries
%    destinations = [destinations;dests];
%end
num_destinations = length(destinations);
num_requests = 0;
for p = 1:num_deliveries
    for d = 1:num_destinations
        [p,d,num_destinations]
        ground_land_index = destinations(d);
        land_vertex = EB_airways.land_lane_vertexes(ground_land_index);
        [UAS_index,avail_time] = LEM_EQ_get_UAS(UAS,request_times(d));
        ground_launch_index = UAS(UAS_index).schedule(1,1);
        launch_vertex = EB_airways.launch_lane_vertexes(ground_launch_index);
        [path,v_path] = LEM_get_path(EB_airways,launch_vertex,land_vertex,[]);
        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 = launch_vertex;
        requests(num_requests).land_index = land_vertex;
        [t_reservations,fr] = LEM_gen_reservations(EB_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 = launch_vertex;
        fr.land_vertex = land_vertex;
        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
            num_requests = num_requests + 1;
            fr.start_time = fr.plan(1,1);
            fr.end_time = fr.plan(end,2);
            b_launch_vertex = EB_airways.launch_lane_vertexes(ground_land_index);
            b_land_vertex = EB_airways.land_lane_vertexes(ground_launch_index);
            [b_path,b_v_path] = LEM_get_path(EB_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(EB_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 = launch_vertex;
            fbr.land_vertex = land_vertex;
            fbr.start_time = r_time;
            fbr.end_time = fbr.start_interval(2);
            fbr.traj = [];
            flights = [flights;fbr];
            if fbr.type==PLANNED
                reservations = t_reservations;
            else
                flights(end-1).type = FAILED;
            end
            tch= 0;
        end
        tch = 0;
    end
end

tch = 0;

res.flights = flights;
res.UAS = UAS;
res.reservations = reservations;
res.requests = requests;

tch = 0;
