+ delay -= dt;
+ if(delay<Time::zero)
+ {
+ duration = max(duration+delay, Time::zero);
+ delay = Time::zero;
+ }
+ }
+ else if(duration)
+ duration = max(duration-dt, Time::zero);
+
+ if(waypoints_changed && !planner)
+ start_planning(train.get_layout());
+
+ if(planner && planner->check()!=TrainRoutePlanner::PENDING)
+ {
+ waypoints_changed = false;
+ if(planner->get_result()==TrainRoutePlanner::COMPLETE)
+ {
+ const list<Route *> &planned_routes = planner->get_routes_for(train);
+
+ routes.clear();
+ routes.insert(routes.end(), planned_routes.begin(), planned_routes.end());
+ create_lead_route();
+
+ sequence_points = planner->get_sequence_for(train);
+ current_sequence = 0;
+ sequence_check_pending = false;
+
+ route_changed();
+ }
+ planner = 0;
+ }
+
+ if(sequence_check_pending)
+ {
+ if(sequence_points.front().is_cleared())
+ train.stop_at(0);
+ sequence_check_pending = false;
+ }
+
+ if(arrival==RESERVED_TO_END && !train.get_speed())
+ {
+ signal_arrived.emit(waypoints.back());
+ signal_event.emit(Message("arrived", waypoints.back()));
+ arrival = ARRIVED;