X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouter.cpp;h=01205fa834a3fc5575a334be2f67ed8a47109cf4;hb=ef70d27737dd8333aef142e4810dc223b7bc7696;hp=2fac984f03eba6ba23ee1c1ffad0c735d755aaf1;hpb=3de7ae761b9a1fd1c1cd40457cc7067f4cf57c36;p=r2c2.git diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index 2fac984..01205fa 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -15,13 +15,16 @@ namespace R2C2 { TrainRouter::TrainRouter(Train &t): TrainAI(t), priority(0), - arriving(0), + arrival(ON_THE_WAY), destination(0), destination_changed(false), - metrics_stale(false) + metrics_stale(false), + current_sequence(0), + sequence_check_pending(false) { 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() @@ -65,8 +68,8 @@ bool TrainRouter::set_route(const Route *r) destination = 0; waypoints.clear(); sequence_points.clear(); - pending_sequence_checks.clear(); current_sequence = 0; + sequence_check_pending = false; route_changed(); @@ -82,10 +85,44 @@ const Route *TrainRouter::get_route() const void TrainRouter::route_changed() { - train.stop_at(0); - arriving = 0; BlockIter fncb = train.get_first_noncritical_block(); - train.refresh_blocks_from(*fncb); + + 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(); + 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(!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); @@ -181,45 +218,41 @@ void TrainRouter::tick(const Time::TimeDelta &dt) delay = Time::zero; } - if(destination_changed) + if(destination_changed && !planner) + start_planning(train.get_layout()); + + if(planner && planner->get_result()!=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(); - 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); - pending_sequence_checks.clear(); - current_sequence = 0; - - route_changed(); - } - planner = 0; + 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(); } + planner = 0; } - for(list::iterator i=pending_sequence_checks.begin(); i!=pending_sequence_checks.end(); ++i) - if((*i)->preceding_train->get_ai_of_type()->get_current_sequence()>=(*i)->sequence_in) - { - (*i)->preceding_train = 0; - if(*i==&sequence_points.front()) - train.stop_at(0); - } - pending_sequence_checks.clear(); + if(sequence_check_pending) + { + if(sequence_points.front().is_cleared()) + train.stop_at(0); + 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); } @@ -238,30 +271,25 @@ void TrainRouter::save(list &st) const void TrainRouter::block_reserved(Block &block, Train *t) { + if(routes.empty()) + return; + if(t!=&train) { if(!t) return; - TrainRouter *other_router = 0; - for(list::iterator i=sequence_points.begin(); i!=sequence_points.end(); ++i) - if(i->block==&block && i->preceding_train==t) - { - if(!other_router) - other_router = t->get_ai_of_type(); - if(other_router->get_current_sequence()>=i->sequence_in) - { - i->preceding_train = 0; - if(i==sequence_points.begin()) - train.stop_at(0); - } - else - pending_sequence_checks.push_back(&*i); - } + // 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; return; } + // Did we reach our next sequence point? if(!sequence_points.empty()) { SequencePoint &sp = sequence_points.front(); @@ -272,35 +300,53 @@ void TrainRouter::block_reserved(Block &block, Train *t) } } - BlockIter b_iter = t->get_block_allocator().iter_for(block); + TrackIter track = train.get_block_allocator().iter_for(block).track_iter(); - RouteList::iterator route = routes.begin(); - if(advance_route(route, block)) + // Is the block a turnout? If it is, set it to the correct path. + if(unsigned taddr = block.get_turnout_address()) { - // Check if the block is a turnout and set it to proper path - if(unsigned taddr = block.get_turnout_address()) + int path = (*reserving_route)->get_turnout(taddr); + if(path>=0) + 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()) { - int path = (*route)->get_turnout(taddr); - if(path>=0) - b_iter.track_iter()->set_active_path(path); + if(!advance_to_track(reserving_route, *track)) + throw logic_error("internal error (reservation outside of route)"); + else if(&track->get_block()==&block) + break; } + } - // Check if the next block is still part of the designated route - BlockIter b_iter_next = b_iter.next(*route); - - RouteList::iterator next_route = route; - if(!advance_route(next_route, *b_iter_next)) + /* 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; } + if(&track->get_block()!=&block) + break; + } - if(!sequence_points.empty()) - { - SequencePoint &sp = sequence_points.front(); - if(sp.preceding_train && sp.block==b_iter_next.block()) - train.stop_at(&block); - } + // Do we need to wait for another train to pass? + if(!sequence_points.empty()) + { + SequencePoint &sp = sequence_points.front(); + if(sp.block==&track->get_block() && !sp.is_cleared()) + train.stop_at(&block); } } @@ -308,29 +354,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)) @@ -349,24 +375,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; - } } -const Route *TrainRouter::get_route_for_block(const Block &block) const +void TrainRouter::train_rear_advanced(Block &block) { - const set &tracks = block.get_tracks(); - for(RouteList::const_iterator i=routes.begin(); i!=routes.end(); ++i) - for(set::const_iterator j=tracks.begin(); j!=tracks.end(); ++j) - if((*i)->has_track(**j)) - return *i; + Track &track = *train.get_block_allocator().iter_for(block).endpoint().track; - return 0; + // 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() @@ -413,26 +440,27 @@ Route *TrainRouter::create_lead_route(Route *lead, const Route *target) return lead; } -bool TrainRouter::advance_route(RouteList::iterator &iter, const Block &block) +bool TrainRouter::is_valid_for_track(const Route &route, Track &track) const { - const set &tracks = block.get_tracks(); - unsigned turnout_addr = block.get_turnout_address(); - for(; iter!=routes.end(); ++iter) - { - if(turnout_addr && (*iter)->get_turnout(turnout_addr)<0) - continue; - for(set::const_iterator j=tracks.begin(); j!=tracks.end(); ++j) - if((*iter)->has_track(**j)) - return true; - } - - return false; + if(!route.has_track(track)) + return false; + if(track.get_type().is_turnout() && route.get_turnout(track.get_turnout_address())<0) + return false; + return true; } -bool TrainRouter::is_on_route(const Block &block) +bool TrainRouter::advance_to_track(RouteList::iterator &route, Track &track) { - RouteList::iterator iter = routes.begin(); - return advance_route(iter, block); + if(!is_valid_for_track(**route, track)) + { + ++route; + if(route==routes.end()) + return false; + if(!is_valid_for_track(**route, track)) + throw logic_error("internal error (routes are not continuous)"); + } + + return true; } void TrainRouter::start_planning(Layout &layout) @@ -462,6 +490,15 @@ TrainRouter::SequencePoint::SequencePoint(Block &b, unsigned o): sequence_out(o) { } +bool TrainRouter::SequencePoint::is_cleared() const +{ + if(!preceding_train) + return true; + + TrainRouter *router = preceding_train->get_ai_of_type(); + return router->get_current_sequence()>=sequence_in; +} + TrainRouter::Loader::Loader(TrainRouter &r): DataFile::ObjectLoader(r)