function res = LEM_sim2(t_max,del_t)
% LEM_sim2 - Jerome simulation prototype (3 Nov 2020)
% On input:
%     t_max (float): max sim time
%     del_t (float): time step
% On output:
%     res (results struct): results of simulation
%       .reservations (reservations struct):
%       .flights (flights struct):
% Call:
%     res = LEM_sim2(400,0.5);
% Author:
%     T. Henderson
%     UU
%     Fall 2020
%

T_START_OFFSET = 1;
T_END_OFFSET = 20;
PLANNED = 1;

roads = LEM_grid_roads(-100,100,-100,100,20,20);
airways = LEM_gen_airways(roads,[1:20],[101:120],2);
num_lanes = length(airways.lanes(:,1));
for k = 1:num_lanes
    reservations(k).flights = [];
end

[fp1,reservations] = LEM_reserve_fp(reservations,airways,3,3.2,5,963,985,2);
[fp2,reservations] = LEM_reserve_fp(reservations,airways,3,3.2,5,967,1002,2);

flights(1).type = PLANNED;
flights(1).plan = fp1;
flights(1).route = LEM_plan2route(fp1,airways);
lane_index = fp1(1,4);
lane_vertex1 = airways.lane_edges(lane_index,1);
pt1 = airways.lane_vertexes(lane_vertex1,:);
lane_vertex2 = airways.lane_edges(lane_index,2);
pt2 = airways.lane_vertexes(lane_vertex2,:);
dir1 = pt2 - pt1;
dir1 = dir1/norm(dir1);
flights(1).telemetry = [pt1 dir1 fp1(1,3)];

flights(2).type = PLANNED;
flights(2).plan = fp2;
flights(2).route = LEM_plan2route(fp2,airways);
lane_index = fp2(1,4);
lane_vertex1 = airways.lane_edges(lane_index,1);
pt1 = airways.lane_vertexes(lane_vertex1,:);
lane_vertex2 = airways.lane_edges(lane_index,2);
pt2 = airways.lane_vertexes(lane_vertex2,:);
dir1 = pt2 - pt1;
dir1 = dir1/norm(dir1);
flights(2).telemetry = [pt1 dir1 fp2(1,3)];

num_flights = 2;

t = 0;
wb = waitbar(0,'Flight Sim');
while t<t_max
    waitbar(t/t_max);
    t = t + del_t;
    [t,t_max];
    flights = LEM_advance_flights(flights,t,del_t,0);
end
close(wb);
res.roads = roads;
res.airways = airways;
res.reservations = reservations;
res.flights = flights;
