X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouter.cpp;h=9245774f605309d597591fa01e95eb60f8b20c38;hb=e8d2abb48b5236cc3455a035628292ae7908240e;hp=c55105026e3710e859692c934a3a1353f3fb5662;hpb=d4c2cc962dc4c7b11c2db360fef7a80de3334c11;p=r2c2.git diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index c551050..9245774 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -15,7 +15,7 @@ namespace R2C2 { TrainRouter::TrainRouter(Train &t): TrainAI(t), priority(0), - arriving(0), + arrival(ON_THE_WAY), destination(0), destination_changed(false), metrics_stale(false), @@ -24,6 +24,7 @@ TrainRouter::TrainRouter(Train &t): { 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)); + train.signal_rear_advanced.connect(sigc::mem_fun(this, &TrainRouter::train_rear_advanced)); } TrainRouter::~TrainRouter() @@ -61,6 +62,7 @@ bool TrainRouter::set_route(const Route *r) routes.clear(); if(lead) routes.push_back(lead); + // TODO Check if eclipsed by lead route if(r) routes.push_back(r); @@ -90,6 +92,9 @@ void TrainRouter::route_changed() 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(); for(; track; track=track.next()) { @@ -105,7 +110,18 @@ void TrainRouter::route_changed() if(!already_at_end) { - train.stop_at(0); + // 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); } @@ -165,6 +181,11 @@ void TrainRouter::set_departure_delay(const Time::TimeDelta &d) destination_changed = true; } +void TrainRouter::set_trip_duration(const Time::TimeDelta &d) +{ + duration = d; +} + void TrainRouter::message(const Message &msg) { if(msg.type=="set-route") @@ -192,6 +213,8 @@ void TrainRouter::message(const Message &msg) } else if(msg.type=="set-departure-delay") set_departure_delay(msg.value.value()); + else if(msg.type=="set-trip-duration") + set_trip_duration(msg.value.value()); } void TrainRouter::tick(const Time::TimeDelta &dt) @@ -199,31 +222,48 @@ void TrainRouter::tick(const Time::TimeDelta &dt) if(delay) { delay -= dt; - if(delay<=Time::zero) + if(delaycheck()!=TrainRoutePlanner::PENDING) { - if(!planner) - start_planning(train.get_layout()); - else if(planner->get_result()!=TrainRoutePlanner::PENDING) + destination_changed = false; + if(planner->get_result()==TrainRoutePlanner::COMPLETE) { - destination_changed = false; - if(planner->get_result()==TrainRoutePlanner::COMPLETE) + const list &planned_routes = planner->get_routes_for(train); + + routes.clear(); + Route *lead = create_lead_route(0, planned_routes.front()); + routes.push_back(lead); + + list::const_iterator begin = planned_routes.begin(); + for(; begin!=planned_routes.end(); ++begin) { - const list &planned_routes = planner->get_routes_for(train); - routes.clear(); - routes.push_back(create_lead_route(0, planned_routes.front())); - routes.insert(routes.end(), planned_routes.begin(), planned_routes.end()); - sequence_points = planner->get_sequence_for(train); - current_sequence = 0; - sequence_check_pending = false; - - route_changed(); + const Route::TrackSet &tracks = (*begin)->get_tracks(); + bool eclipsed = true; + for(Route::TrackSet::const_iterator i=tracks.begin(); (eclipsed && i!=tracks.end()); ++i) + eclipsed = lead->has_track(**i); + if(!eclipsed) + break; } - planner = 0; + routes.insert(routes.end(), begin, planned_routes.end()); + + sequence_points = planner->get_sequence_for(train); + current_sequence = 0; + sequence_check_pending = false; + + route_changed(); } + planner = 0; } if(sequence_check_pending) @@ -233,13 +273,13 @@ void TrainRouter::tick(const Time::TimeDelta &dt) sequence_check_pending = false; } - if(arriving==1 && !train.get_speed()) + if(arrival==RESERVED_TO_END && !train.get_speed()) { signal_arrived.emit(destination); signal_event.emit(Message("arrived", destination)); - arriving = 2; + arrival = ARRIVED; } - else if(arriving==2 && !train.get_block_allocator().is_active()) + else if(arrival==ARRIVED && !train.get_block_allocator().is_active()) set_route(0); } @@ -266,18 +306,17 @@ void TrainRouter::block_reserved(Block &block, Train *t) if(!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) - { - if(sp.is_cleared()) - train.stop_at(0); - else - sequence_check_pending = true; - } + /* The other train's router will advance its sequence on the same + signal and may not have handled it yet. */ + sequence_check_pending = true; return; } + // Did we reach our next sequence point? if(!sequence_points.empty()) { SequencePoint &sp = sequence_points.front(); @@ -290,7 +329,7 @@ void TrainRouter::block_reserved(Block &block, Train *t) TrackIter track = train.get_block_allocator().iter_for(block).track_iter(); - // Is the block a turnout? If so, set it to the proper path. + // Is the block a turnout? If it is, set it to the correct path. if(unsigned taddr = block.get_turnout_address()) { int path = (*reserving_route)->get_turnout(taddr); @@ -298,9 +337,12 @@ void TrainRouter::block_reserved(Block &block, Train *t) track->set_active_path(path); } + /* If the allocator has released blocks from the front, we may need to + resync reserving_route. */ if(reserving_route==routes.end() || !(*reserving_route)->has_track(*track)) { reserving_route = routes.begin(); + arrival = ON_THE_WAY; track = t->get_block_allocator().first().track_iter(); for(; track; track=track.next()) { @@ -311,11 +353,14 @@ void TrainRouter::block_reserved(Block &block, Train *t) } } - // Do we need to move to the next route? + /* Keep reserving_route pointing to the route containing the block that is + expected to be allocated next. */ for(; track; track=track.next((*reserving_route)->get_path(*track))) { if(!advance_to_track(reserving_route, *track)) { + // We've reached the end of the route. Stop here. + arrival = RESERVED_TO_END; train.stop_at(&block); return; } @@ -323,6 +368,7 @@ void TrainRouter::block_reserved(Block &block, Train *t) break; } + // Do we need to wait for another train to pass? if(!sequence_points.empty()) { SequencePoint &sp = sequence_points.front(); @@ -335,29 +381,9 @@ void TrainRouter::train_advanced(Block &block) { BlockIter b_iter = train.get_block_allocator().iter_for(block); - // Check if we've reached the next route - if(routes.size()>1) - { - Track &track = *b_iter.endpoint().track; - const Route *route = get_route(); - bool change_route = false; - if(route->is_loop()) - change_route = (*++routes.begin())->has_track(track); - else - change_route = !route->has_track(track); - - if(change_route) - { - routes.pop_front(); - route = get_route(); - // XXX Exceptions? - signal_route_changed.emit(route); - signal_event.emit(Message("route-changed", route)); - } - } - if(!waypoints.empty()) { + // A waypoint is considered reached when the train has advanced through it. const TrackChain &wp = *waypoints.front(); TrackIter t_iter = b_iter.track_iter(); if(wp.has_track(*t_iter)) @@ -376,13 +402,25 @@ void TrainRouter::train_advanced(Block &block) } } } +} - if(!routes.empty()) - { - b_iter = b_iter.next(); - if(b_iter && !is_on_route(*b_iter)) - arriving = 1; - } +void TrainRouter::train_rear_advanced(Block &block) +{ + Track &track = *train.get_block_allocator().iter_for(block).endpoint().track; + + // Drop any routes that are now completely behind the train. + for(RouteList::iterator i=routes.begin(); i!=routes.end(); ++i) + if((*i)->has_track(track)) + { + if(i!=routes.begin()) + { + routes.erase(routes.begin(), i); + const Route *route = get_route(); + signal_route_changed.emit(route); + signal_event.emit(Message("route-changed", route)); + } + break; + } } void TrainRouter::create_metrics() @@ -411,16 +449,16 @@ Route *TrainRouter::create_lead_route(Route *lead, const Route *target) lead->set_temporary(true); } - bool target_reached = false; - for(TrackIter i=train.get_block_allocator().first().track_iter(); i; i=i.next()) + bool target_tracks = 0; + for(TrackIter i=train.get_block_allocator().first().track_iter(); (target_tracks<2 && i); i=i.next()) { if(i->get_block().get_train()!=&train) break; if(target) { if(target->has_track(*i)) - target_reached = true; - else if(target_reached) + ++target_tracks; + else if(target_tracks>0) break; } lead->add_track(*i); @@ -468,7 +506,7 @@ void TrainRouter::start_planning(Layout &layout) router->planner = planner; } - planner->plan(); + planner->plan_async(); }