+ if(!planner || planner->get_result()!=TrainRoutePlanner::COMPLETE)
+ return;
+
+ 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();
+}
+
+void TrainRouter::route_changed()
+{
+ BlockIter fncb = train.get_first_noncritical_block();
+
+ reserving_route = routes.begin();
+ bool already_at_end = false;
+ if(!routes.empty())
+ {
+ /* Find the route that should be used for the next allocated block. We
+ can't rely on the resync code in block_reserved since we may need to
+ clear the stop marker to continue allocation. */
+ TrackIter track = train.get_block_allocator().first().track_iter();
+ list<SequencePoint>::iterator seq_begin = sequence_points.begin();
+ for(; track; track=track.next())
+ {
+ if(!advance_to_track(reserving_route, track))
+ {
+ already_at_end = true;
+ break;
+ }
+ if(&track->get_block()==fncb.block())
+ break;
+
+ if(seq_begin!=sequence_points.end() && seq_begin->block==&track->get_block())
+ {
+ current_sequence = seq_begin->sequence_out;
+ ++seq_begin;
+ }
+ }
+
+ sequence_points.erase(sequence_points.begin(), seq_begin);
+ }
+
+ if(!already_at_end)
+ {
+ // We are not at the end of the route now, but might have been before.
+ arrival = ON_THE_WAY;
+ train.refresh_blocks_from(*fncb);
+ if(!arrival)
+ train.stop_at(0);
+ }
+ else if(!arrival)
+ {
+ /* If arrival wasn't set before (perhaps because we weren't on a route),
+ set it now. */
+ arrival = RESERVED_TO_END;
+ train.stop_at(&*fncb.flip());
+ train.refresh_blocks_from(*fncb);
+ }
+
+ const Route *route = get_route();
+ signal_route_changed.emit(route);
+ signal_event.emit(Message("route-changed", route));