function res = LEM_sim5(t_max,del_t,f_prob,sigma)
% LEM_sim5 - simulation with radar
% On input:
%     t_max (float): max sim time
%     del_t (float): time step
%     f_prob (float): probability of new flight
%     sigma (float): variance on position
% On output:
%     res (results struct): results of simulation
%       .reservations (reservations struct): reservation info
%       .flights (flights struct): flight info
%       .trace (trace struct): radar targets
% Call:
%     res = LEM_sim1(400,0.5,0.2,0.1);
% Author:
%     T. Henderson
%     UU
%     Fall 2020
%

roads = LEM_grid_roads(-20,20,-20,20,10,10);
launch = randperm(length(roads.vertexes));
launch = launch(1:10);
land = randperm(length(roads.vertexes));
land = land(1:10);
airways = LEM_gen_airways(roads,launch,land,1);
num_lanes = length(airways.lanes(:,1));
for k = 1:num_lanes
    reservations(k).flights = [];
end
flights = [];
num_flights = 0;

radars(1).x = -10;
radars(1).y = -10;
radars(1).z = 0;
radars(1).dx = 0;
radars(1).dy = 0;
radars(1).dz = 1;
radars(1).angle = 5*pi/180;
radars(1).max_range = 1000;
radars(1).id = 1;
radars(1).pos_noise = 0.1*eye(3);
 radars(2).x = 10;
 radars(2).y = -10;
 radars(2).z = 0;
 radars(2).dx = 0;
 radars(2).dy = 0;
 radars(2).dz = 1;
 radars(2).angle = 5*pi/180;
 radars(2).max_range = 1000;
 radars(2).id = 2;
 radars(2).pos_noise = 0.1*eye(3);
% radars(3).x = -10;
% radars(3).y = 10;
% radars(3).z = 0;
% radars(3).dx = 0;
% radars(3).dy = 0;
% radars(3).dz = 1;
% radars(3).angle = 45*pi/180;
% radars(3).max_range = 1000;
% radars(3).id = 3;
% radars(3).pos_noise = 0.001*eye(3);
% radars(4).x = 10;
% radars(4).y = 10;
% radars(4).z = 0;
% radars(4).dx = 0;
% radars(4).dy = 0;
% radars(4).dz = 1;
% radars(4).angle = 45*pi/180;
% radars(4).max_range = 1000;
% radars(4).id = 4;
% radars(4).pos_noise = 0.001*eye(3);

t = 0;
count = 0;
wb = waitbar(0,'Flight Sim');
while t<t_max
    count = count + 1;
    waitbar(t/t_max);
    t = t + del_t;
    [t,t_max];
    new_flights = [];
    if rand<f_prob
        [reservations,new_flights] = LEM_gen_reservations(airways,...
            reservations,t,1);
    end
%    if isempty(flights)&~isempty(new_flights)
%        flights(1).type = new_flights(1).type;
%        flights(1).plan = new_flights(1).plan;
%        flights(1).route = new_flights(1).route;
%        flights(1).telemetry = new_flights(1).telemetry;
%    end
     if ~isempty(new_flights)
         num_new_flights = length(new_flights);
         for k = 1:num_new_flights
             flights(end+1).type = new_flights(k).type;
             flights(end).plan = new_flights(k).plan;
             flights(end).route = new_flights(k).route;
             flights(end).telemetry = new_flights(k).telemetry;
         end
     end
    new_flights = LEM_gen_rogue_flights(airways,flights,t);
    flights = LEM_advance_flights(flights,t,del_t,sigma);
    objects = LEM_flights2objects(flights);
    targets = LEM_radar(radars,objects,t);
    trace(count).targets = targets;
end
close(wb);
res.roads = roads;
res.airways = airways;
res.reservations = reservations;
res.flights = flights;
res.trace = trace;
