void TrainRouter::route_changed()
{
- BlockIter fncb = train.get_first_noncritical_block();
+ BlockIter fncb = train.get_last_critical_block().next();
+ arrival = ON_THE_WAY;
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();
+ const BlockAllocator &allocator = train.get_block_allocator();
+ TrackIter track = 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;
+ arrival = (allocator.is_block_current(track->get_block()) ? ADVANCED_TO_END : RESERVED_TO_END);
break;
}
if(&track->get_block()==fncb.block())
if(seq_begin!=sequence_points.end() && seq_begin->block==&track->get_block())
{
+ // Assume any sequence points within critical blocks to be cleared
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);
+ if(!sequence_points.empty())
+ {
+ const SequencePoint &sp = sequence_points.front();
+ if(sp.block==fncb.block() && !sp.is_cleared())
+ {
+ arrival = WAITING_FOR_SEQUENCE;
+ sequence_check_pending = true;
+ }
+ }
}
- else if(!arrival)
- {
- /* If arrival wasn't set before (perhaps because we weren't on a route),
- set it now. */
- arrival = RESERVED_TO_END;
+
+ /* Refresh from the first non-critical block to pick up any changes in the
+ route. Set stop marker first in case an arrival condition was met within the
+ critical blocks. */
+ if(arrival)
train.stop_at(&*fncb.flip());
- train.refresh_blocks_from(*fncb);
- }
+ train.refresh_blocks_from(*fncb);
+ // If no arrival condition was found, clear a possible previous stop marker.
+ if(!arrival)
+ train.stop_at(0);
const Route *route = get_route();
signal_route_changed.emit(route);
if(sequence_check_pending)
{
if(sequence_points.front().is_cleared())
+ {
+ arrival = ON_THE_WAY;
train.stop_at(0);
+ }
sequence_check_pending = false;
}
- if(arrival==RESERVED_TO_END && !train.get_speed())
+ if(arrival==ADVANCED_TO_END && !train.get_speed())
{
signal_arrived.emit(waypoints.back());
signal_event.emit(Message("arrived", waypoints.back()));
{
SequencePoint &sp = sequence_points.front();
if(sp.block==&track->get_block() && !sp.is_cleared())
+ {
+ arrival = WAITING_FOR_SEQUENCE;
train.stop_at(&block);
+ }
}
}
{
BlockIter b_iter = train.get_block_allocator().iter_for(block);
- if(waypoints.size()>1)
+ if(!waypoints.empty())
{
// A waypoint is considered reached when the train has advanced through it.
const TrackChain &wp = *waypoints.front();
{
if(!wp.has_track(*t_iter))
{
- waypoints.erase(waypoints.begin());
- metrics_stale = true;
- signal_waypoint_reached.emit(&wp);
- signal_event.emit(Message("waypoint-reached", &wp));
+ if(waypoints.size()==1)
+ {
+ if(arrival==RESERVED_TO_END)
+ arrival = ADVANCED_TO_END;
+ }
+ else
+ {
+ waypoints.erase(waypoints.begin());
+ metrics_stale = true;
+ signal_waypoint_reached.emit(&wp);
+ signal_event.emit(Message("waypoint-reached", &wp));
+ }
break;
}
else if(!block.has_track(*t_iter))
if(routes.empty() || !train.is_placed())
return false;
- BlockIter fncb = train.get_first_noncritical_block();
- TrackIter next_track = fncb.track_iter();
+ BlockIter lcb = train.get_last_critical_block();
+ TrackIter last_track_rev = lcb.reverse().track_iter();
unsigned count = 0;
- for(TrackIter i=next_track.flip(); (i && i->get_block().get_train()==&train); i=i.next())
+ for(TrackIter i=last_track_rev; (i && i->get_block().get_train()==&train); i=i.next())
{
if(routes.front()->has_track(*i))
++count;
else if(count>0)
{
if(count==routes.front()->get_tracks().size())
- next_track = i.flip();
+ last_track_rev = i;
break;
}
}
- if(!routes.front()->has_track(*next_track) && !routes.front()->has_track(*next_track.flip()))
+ TrackIter next_track = last_track_rev.flip();
+ if(!routes.front()->has_track(*last_track_rev) && !routes.front()->has_track(*next_track))
{
Route *pf = Route::find(next_track, *routes.front());
if(!pf)
}
Route *lead = 0;
- for(TrackIter i=next_track.flip(); (i && i->get_block().get_train()==&train); i=i.next())
+ for(TrackIter i=last_track_rev; (i && i->get_block().get_train()==&train); i=i.next())
{
if(!lead && !routes.front()->has_track(*i))
{
return true;
}
-bool TrainRouter::is_valid_for_track(const Route &route, const TrackIter &track) const
-{
- if(!route.has_track(*track))
- return false;
- if(track->get_type().is_turnout() && route.get_turnout(track->get_turnout_address())<0 && route.has_track(*track.flip()))
- return false;
- return true;
-}
-
bool TrainRouter::advance_to_track(RouteList::iterator &route, const TrackIter &track)
{
- if(!is_valid_for_track(**route, track))
+ Track &prev_track = *track.flip();
+ unsigned taddr = (track->get_type().is_turnout() ? track->get_turnout_address() : 0);
+ for(unsigned i=0; route!=routes.end(); ++i)
{
- ++route;
- if(route==routes.end())
- return false;
- if(!is_valid_for_track(**route, track))
+ bool in_route = (*route)->has_track(*track);
+ bool prev_in_route = (*route)->has_track(prev_track);
+ bool known_path = (!taddr || (*route)->get_turnout(taddr)>=0);
+
+ if(in_route && (known_path || !prev_in_route))
+ return true;
+ else if(i==0 || prev_in_route)
+ ++route;
+ else
throw logic_error("internal error (routes are not continuous)");
}
- return true;
+ return false;
}
void TrainRouter::get_routers(Layout &layout, vector<TrainRouter *> &routers)
void TrainRouter::apply_plan(Layout &layout, TrainRoutePlanner &planner)
{
+ if(planner.get_result()==TrainRoutePlanner::FAILED)
+ layout.emergency(0, "Route planning failed");
+
vector<TrainRouter *> routers;
get_routers(layout, routers);