X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouter.cpp;h=1ada62104cee5724ac208be1e1e57cf7dd18946c;hb=2fb2e58685c521ea76aab9f6d7cffe1186c9814a;hp=122b91a3bd195837f90455cc3da66289d37ea453;hpb=68877070660f2806cc2a7f0ccb69153059ef6ce7;p=r2c2.git diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index 122b91a..1ada621 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -16,11 +16,10 @@ namespace R2C2 { 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)); @@ -49,8 +48,6 @@ bool TrainRouter::set_route(const Route *r) waypoints.clear(); sequence_points.clear(); - current_sequence = 0; - sequence_check_pending = false; route_changed(); @@ -79,7 +76,6 @@ void TrainRouter::use_planned_route() sequence_points = planner->get_sequence_for(train); current_sequence = 0; - sequence_check_pending = false; route_changed(); } @@ -88,7 +84,7 @@ void TrainRouter::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()) { @@ -102,7 +98,7 @@ void TrainRouter::route_changed() { 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()) @@ -121,22 +117,19 @@ void TrainRouter::route_changed() 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(); @@ -147,26 +140,26 @@ void TrainRouter::route_changed() void TrainRouter::set_destination(const TrackChain &d) { if(waypoints.empty()) - waypoints.push_back(&d); + waypoints.push_back(Waypoint(d)); else - waypoints.back() = &d; + waypoints.back() = Waypoint(d); waypoints_changed = true; metrics_stale = true; } -void TrainRouter::add_waypoint(const TrackChain &wp) +void TrainRouter::add_waypoint(const TrackChain &chain, TrackChain::Direction dir) { - waypoints.push_back(&wp); + waypoints.push_back(Waypoint(chain, dir)); waypoints_changed = true; metrics_stale = true; } -const TrackChain &TrainRouter::get_waypoint(unsigned index) const +const TrainRouter::Waypoint &TrainRouter::get_waypoint(unsigned index) const { if(index>=waypoints.size()) throw out_of_range("TrainRouter::is_waypoint"); - return *waypoints[index]; + return waypoints[index]; } const TrainRouteMetric &TrainRouter::get_metric(int index) const @@ -215,7 +208,12 @@ void TrainRouter::message(const Message &msg) } else if(msg.type=="add-waypoint") { - if(msg.value.check_type()) + if(msg.value.check_type()) + { + Waypoint wp = msg.value.value(); + add_waypoint(*wp.chain, wp.direction); + } + else if(msg.value.check_type()) add_waypoint(*msg.value.value()); else add_waypoint(*msg.value.value()); @@ -250,23 +248,24 @@ void TrainRouter::tick(const Time::TimeDelta &dt) 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()); - signal_event.emit(Message("arrived", waypoints.back())); - arrival = ARRIVED; + signal_arrived.emit(waypoints.back().chain); + signal_event.emit(Message("arrived", waypoints.back().chain)); + 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); } @@ -294,11 +293,14 @@ void TrainRouter::block_reserved(Block &block, Train *t) 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; } @@ -329,7 +331,7 @@ void TrainRouter::block_reserved(Block &block, Train *t) 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()) { @@ -347,7 +349,7 @@ void TrainRouter::block_reserved(Block &block, Train *t) 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; } @@ -361,7 +363,7 @@ void TrainRouter::block_reserved(Block &block, Train *t) 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); } } @@ -369,35 +371,45 @@ void TrainRouter::block_reserved(Block &block, Train *t) void TrainRouter::train_advanced(Block &block) { - BlockIter b_iter = train.get_block_allocator().iter_for(block); - if(!waypoints.empty()) { // A waypoint is considered reached when the train has advanced through it. - const TrackChain &wp = *waypoints.front(); + BlockIter b_iter = train.get_block_allocator().iter_for(block); + const Waypoint &wp = waypoints.front(); TrackIter t_iter = b_iter.track_iter(); - if(wp.has_track(*t_iter)) + if(wp.chain->has_track(*t_iter)) { - for(; t_iter; t_iter=t_iter.next()) + while(1) { - if(!wp.has_track(*t_iter)) + TrackIter next = t_iter.next(); + if(!next) + break; + + if(!wp.chain->has_track(*next)) { + if(wp.direction!=TrackChain::UNSPECIFIED) + if(t_iter!=wp.chain->iter_for(*t_iter, wp.direction)) + break; + if(waypoints.size()==1) { - if(arrival==RESERVED_TO_END) - arrival = ADVANCED_TO_END; + if(state==RESERVED_TO_END) + state = ADVANCED_TO_END; } else { + const TrackChain *chain = wp.chain; waypoints.erase(waypoints.begin()); metrics_stale = true; - signal_waypoint_reached.emit(&wp); - signal_event.emit(Message("waypoint-reached", &wp)); + signal_waypoint_reached.emit(chain); + signal_event.emit(Message("waypoint-reached", chain)); } break; } - else if(!block.has_track(*t_iter)) + else if(!block.has_track(*next)) break; + + t_iter = next; } } } @@ -433,8 +445,8 @@ void TrainRouter::create_metrics() if(waypoints.empty()) return; - for(vector::const_iterator i=waypoints.begin(); i!=waypoints.end(); ++i) - metrics.push_back(new TrainRouteMetric(**i)); + for(vector::const_iterator i=waypoints.begin(); i!=waypoints.end(); ++i) + metrics.push_back(new TrainRouteMetric(*i->chain, i->direction)); for(unsigned i=metrics.size()-1; i-->0; ) metrics[i]->chain_to(*metrics[i+1]); @@ -514,13 +526,14 @@ bool TrainRouter::advance_to_track(RouteList::iterator &route, const TrackIter & return false; } -void TrainRouter::get_routers(Layout &layout, vector &routers) +void TrainRouter::get_routers(Layout &layout, vector &routers, TrainRoutePlanner *planner) { const map &trains = layout.get_trains(); routers.reserve(trains.size()); for(map::const_iterator i=trains.begin(); i!=trains.end(); ++i) if(TrainRouter *router = i->second->get_ai_of_type()) - routers.push_back(router); + if(!planner || router->planner.get()==planner) + routers.push_back(router); } void TrainRouter::start_planning(Layout &layout) @@ -548,17 +561,27 @@ void TrainRouter::apply_plan(Layout &layout, TrainRoutePlanner &planner) layout.emergency(0, "Route planning failed"); vector routers; - get_routers(layout, routers); + get_routers(layout, routers, &planner); + /* Clear sequence counters first to avoid inconsistent state while applying + the plan. */ for(vector::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::const_iterator i=routers.begin(); i!=routers.end(); ++i) + { + (*i)->use_planned_route(); + (*i)->planner = 0; + } } +TrainRouter::Waypoint::Waypoint(const TrackChain &c, TrackChain::Direction d): + chain(&c), + direction(d) +{ } + + TrainRouter::SequencePoint::SequencePoint(Block &b, unsigned o): block(&b), preceding_train(0),