function res = LEM_sim_art_LBSD(num_flights,num_artillery,b)
% LEM_sim_art_LBSD - small-scale simulation with artillery
% On input:
%     num_flights (int): number of flights
%     num_artillery (int): num artillery shots
%     b (Boolean): make movie
% On output:
%     res (results struct): results info
%       .roads (roads struct): roads info
%       .airways (airways struct): airways info
%       .requests (requests struct): LBSD requests
%       .reservations (reservations struct): reservations info
%       .flights (flights struct): flights info
%       .failures_LBSD (n by 1 vector): 0 if LBSD flight success; else 1
%       .flights_cells (flights struct): cells flight info
%       .cells (cells struct): FAA-NASA cells corresponding to LBSD layout
%       .requests_cells (requests_cells struct): FAA-NASA flight requests 
%       .P (nx5 array): performance info for LBSD
%         col 1 (float): flight ID
%         col 2 (float): delay (actual_start - requested_min_start)
%         col 3 (float): flight duration
%         col 4 (float): deconflict time
%         col 5 (float): failed to schedule
% Call:
%     res = LEM_sim_art_LBSD(500,100,0);
% Author:
%     T. Henderson
%     UU
%     Fall 2020/Fall 2022
%

FAILED = 0;
INDEX_ARTILLERY = 6;

res = [];

rng('default');
roads = LEM_gen_grid_roads(0,50,0,50,10,10);
launch_sites = [1:6];
land_sites = [31:36];
airways = LEM_gen_airways(roads,launch_sites,land_sites,2,467,534);
hd = 50;   % headway distance
t_min = 0;
t_max = 1000;
launch_time_interval = 120;
min_speed = 10;
max_speed = 15;
%num_flights = 100;
requests = LEM_gen_requests_LBSD(t_min,t_max,airways,num_flights,...
    launch_time_interval,[min_speed,max_speed]);
[reservations,flights] = LEM_requests2reservations(airways,requests,hd);
xmin = Inf;
xmax = -Inf;
ymin = Inf;
ymax = -Inf;
zmin = Inf;
zmax = -Inf;
del_t = 0.1;
for f = 1:num_flights
    f_out = LEM_gen_traj(flights(f),del_t);
    flights(f).traj = f_out.traj;
    traj = f_out.traj;
    xmin = min(xmin,min(traj(:,2)));
    xmax = max(xmax,max(traj(:,2)));
    ymin = min(ymin,min(traj(:,3)));
    ymax = max(ymax,max(traj(:,3)));
    zmin = min(zmin,min(traj(:,4)));
    zmax = max(zmax,max(traj(:,4)));
end

% Generate artillery
model = LEM_lanes2model(airways.lanes,1);
res.model = model;
for k = 1:num_artillery
    [k num_artillery]
    x0 = 5 + 35*rand;
    y0 = 5 + 35*rand;
    z0 = 0;
    x1 = 50 + 50*rand;
    y1 = 50 + 50*rand;
    z1 = 0;
    theta = LEM_artillery_angle(x1-x0,y1-y0,z1-z0,150); % 80 <-- 827
    atraj = LEM_artillery_traj(x0,y0,z0,x1,y1,z1,150,theta,0.1);
    time_offset = 5*rand;
    atraj(:,4) = atraj(:,4) + time_offset;
    atraj = LEM_schedule_artillery(model,airways,atraj,theta,hd,t_max,...
        reservations);
    xmin = min(xmin,min(atraj(:,1)));
    xmax = max(xmax,max(atraj(:,1)));
    ymin = min(ymin,min(atraj(:,2)));
    ymax = max(ymax,max(atraj(:,2)));
    zmin = min(zmin,min(atraj(:,3)));
    zmax = max(zmax,max(atraj(:,3)));
    aa = atraj;
    atraj(:,1) = aa(:,4);
    atraj(:,2:4) = aa(:,1:3);
    flights(end+1).traj = atraj;
    flights(end).team = 5;
    flights(end).id = INDEX_ARTILLERY;
    flights(end).pt0 = [x0;y0;z0];
    flights(end).pt1 = [x1;y1;z1];
    flights(end).theta = theta;
    flights(end).delay = atraj(1,4) - time_offset;
end

if b==1
    LEM_run_flights(airways,flights,1,1,'sim1_LBSD',...
        xmin,xmax,ymin,ymax,zmin,zmax);
end

%P_LBSD = LEM_performance(flights);

res.roads = roads;
res.airways = airways;
res.requests = requests;
res.reservations = reservations;
res.flights = flights;
failures_LBSD = zeros(num_flights,1);
for f = 1:num_flights
    if flights(f).type==FAILED
        failures_LBSD(f) = 1;
    end
end
res.failures_LBSD = failures_LBSD;
res.mp4 = 'sim1_LBSD.mp4';
res.P_LBSD = []; %P_LBSD;
res.P_cells = [];
