TrainRouter::TrainRouter(Train &t):
TrainAI(t),
priority(0),
- arrival(ON_THE_WAY),
+ state(ON_THE_WAY),
waypoints_changed(false),
metrics_stale(false),
- current_sequence(0),
- sequence_check_pending(false)
+ current_sequence(0)
{
train.get_layout().signal_block_reserved.connect(sigc::mem_fun(this, &TrainRouter::block_reserved));
train.signal_advanced.connect(sigc::mem_fun(this, &TrainRouter::train_advanced));
waypoints.clear();
sequence_points.clear();
- current_sequence = 0;
- sequence_check_pending = false;
route_changed();
sequence_points = planner->get_sequence_for(train);
current_sequence = 0;
- sequence_check_pending = false;
route_changed();
}
{
BlockIter fncb = train.get_last_critical_block().next();
- arrival = ON_THE_WAY;
+ state = ON_THE_WAY;
reserving_route = routes.begin();
if(!routes.empty())
{
{
if(!advance_to_track(reserving_route, track))
{
- arrival = (allocator.is_block_current(track->get_block()) ? ADVANCED_TO_END : RESERVED_TO_END);
+ state = (allocator.is_block_current(track->get_block()) ? ADVANCED_TO_END : RESERVED_TO_END);
break;
}
if(&track->get_block()==fncb.block())
if(!sequence_points.empty())
{
const SequencePoint &sp = sequence_points.front();
- if(sp.block==fncb.block() && sp.preceding_train)
- {
- arrival = WAITING_FOR_SEQUENCE;
- sequence_check_pending = true;
- }
+ if(sp.block==fncb.block() && !sp.is_cleared())
+ state = SEQUENCE_CHECK_PENDING;
}
}
/* 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
+ route. Set stop marker first in case a stopping state was set within the
critical blocks. */
- if(arrival)
+ if(state!=ON_THE_WAY)
train.stop_at(&*fncb.flip());
train.refresh_blocks_from(*fncb);
- // If no arrival condition was found, clear a possible previous stop marker.
- if(!arrival)
+ // If we don't need to stop, clear a possible previous stop marker.
+ if(state==ON_THE_WAY)
train.stop_at(0);
const Route *route = get_route();
{
duration = max(duration+delay, Time::zero);
delay = Time::zero;
+ train.ai_message(Message("set-target-speed", train.get_maximum_speed()));
+ signal_departed.emit();
+ signal_event.emit(Message("departed"));
}
}
else if(duration)
duration = max(duration-dt, Time::zero);
}
- if(sequence_check_pending)
+ if(state==SEQUENCE_CHECK_PENDING)
{
if(sequence_points.front().is_cleared())
{
- arrival = ON_THE_WAY;
+ state = ON_THE_WAY;
train.stop_at(0);
}
- sequence_check_pending = false;
+ else
+ state = WAITING_FOR_SEQUENCE;
}
- if(arrival==ADVANCED_TO_END && !train.get_speed())
+ if(state==ADVANCED_TO_END && !train.get_speed())
{
signal_arrived.emit(waypoints.back().chain);
signal_event.emit(Message("arrived", waypoints.back().chain));
- arrival = ARRIVED;
+ state = ARRIVED;
}
- else if(arrival==ARRIVED && !train.get_block_allocator().is_active())
+ else if(state==ARRIVED && !train.get_block_allocator().is_active())
set_route(0);
}
return;
// Are we waiting for the other train to pass a sequence point?
- SequencePoint &sp = sequence_points.front();
- if(sp.preceding_train==t && sp.block==&block)
- /* The other train's router will advance its sequence on the same
- signal and may not have handled it yet. */
- sequence_check_pending = true;
+ if(state==WAITING_FOR_SEQUENCE)
+ {
+ SequencePoint &sp = sequence_points.front();
+ if(sp.preceding_train==t && sp.block==&block)
+ /* The other train's router will advance its sequence on the same
+ signal and may not have handled it yet. */
+ state = SEQUENCE_CHECK_PENDING;
+ }
return;
}
if(reserving_route==routes.end() || !(*reserving_route)->has_track(*track))
{
reserving_route = routes.begin();
- arrival = ON_THE_WAY;
+ state = ON_THE_WAY;
track = t->get_block_allocator().first().track_iter();
for(; track; track=track.next())
{
if(!advance_to_track(reserving_route, track))
{
// We've reached the end of the route. Stop here.
- arrival = RESERVED_TO_END;
+ state = RESERVED_TO_END;
train.stop_at(&block);
return;
}
SequencePoint &sp = sequence_points.front();
if(sp.block==&track->get_block() && !sp.is_cleared())
{
- arrival = WAITING_FOR_SEQUENCE;
+ state = SEQUENCE_CHECK_PENDING;
train.stop_at(&block);
}
}
if(waypoints.size()==1)
{
- if(arrival==RESERVED_TO_END)
- arrival = ADVANCED_TO_END;
+ if(state==RESERVED_TO_END)
+ state = ADVANCED_TO_END;
}
else
{
return false;
}
-void TrainRouter::get_routers(Layout &layout, vector<TrainRouter *> &routers)
+void TrainRouter::get_routers(Layout &layout, vector<TrainRouter *> &routers, TrainRoutePlanner *planner)
{
const map<unsigned, Train *> &trains = layout.get_trains();
routers.reserve(trains.size());
for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
if(TrainRouter *router = i->second->get_ai_of_type<TrainRouter>())
- routers.push_back(router);
+ if(!planner || router->planner.get()==planner)
+ routers.push_back(router);
}
void TrainRouter::start_planning(Layout &layout)
layout.emergency(0, "Route planning failed");
vector<TrainRouter *> routers;
- get_routers(layout, routers);
+ get_routers(layout, routers, &planner);
+ /* Clear sequence counters first to avoid inconsistent state while applying
+ the plan. */
for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
- if((*i)->planner.get()==&planner)
- {
- (*i)->use_planned_route();
- (*i)->planner = 0;
- }
+ (*i)->current_sequence = 0;
+
+ for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
+ {
+ (*i)->use_planned_route();
+ (*i)->planner = 0;
+ }
}