X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouter.cpp;h=38027dbe896942f6b232b1abcd565393a99d9ca5;hb=193220dca4f520379b23160787ec60315903d49c;hp=5b2c5ee2f768e962cbbe669fb6f46425f6185d9e;hpb=e5c244baa4058dbfc0c5caee64e6c83523751924;p=r2c2.git diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index 5b2c5ee..38027db 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -40,30 +40,12 @@ void TrainRouter::set_priority(int p) bool TrainRouter::set_route(const Route *r) { - BlockIter fncb = train.get_first_noncritical_block(); - - Route *lead = 0; - if(r && train.is_placed()) - { - const BlockAllocator &allocator = train.get_block_allocator(); - TrackIter first = allocator.first().track_iter(); - TrackIter next = fncb.track_iter(); - if(!r->has_track(*next)) - { - lead = Route::find(next, *r); - if(!lead) - return false; - create_lead_route(lead, lead); - } - else if(!r->has_track(*first)) - lead = create_lead_route(0, r); - } - routes.clear(); - if(lead) - routes.push_back(lead); if(r) + { routes.push_back(r); + create_lead_route(); + } destination = 0; waypoints.clear(); @@ -91,23 +73,45 @@ 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(); + list::iterator seq_begin = sequence_points.begin(); for(; track; track=track.next()) { - if(!advance_to_track(reserving_route, *track)) + if(!advance_to_track(reserving_route, track)) { already_at_end = true; break; } if(&track->get_block()==fncb.block()) break; + + if(seq_begin!=sequence_points.end() && seq_begin->block==&track->get_block()) + { + 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.stop_at(0); + 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); } @@ -123,14 +127,6 @@ void TrainRouter::set_destination(const TrackChain &d) metrics_stale = true; } -bool TrainRouter::is_destination(Track &track) const -{ - if(destination) - return destination->has_track(track); - else - return false; -} - void TrainRouter::add_waypoint(const TrackChain &wp) { waypoints.push_back(&wp); @@ -138,12 +134,12 @@ void TrainRouter::add_waypoint(const TrackChain &wp) metrics_stale = true; } -bool TrainRouter::is_waypoint(unsigned index, Track &track) const +const TrackChain &TrainRouter::get_waypoint(unsigned index) const { if(index>=waypoints.size()) throw out_of_range("TrainRouter::is_waypoint"); - return waypoints[index]->has_track(track); + return *waypoints[index]; } const TrainRouteMetric &TrainRouter::get_metric(int index) const @@ -167,6 +163,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") @@ -194,6 +195,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) @@ -201,31 +204,36 @@ 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(); - 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; + const list &planned_routes = planner->get_routes_for(train); + + routes.clear(); + routes.insert(routes.end(), planned_routes.begin(), planned_routes.end()); + create_lead_route(); + + sequence_points = planner->get_sequence_for(train); + current_sequence = 0; + sequence_check_pending = false; + + route_changed(); } + planner = 0; } if(sequence_check_pending) @@ -268,18 +276,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(); @@ -292,7 +299,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); @@ -300,6 +307,8 @@ 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(); @@ -307,18 +316,20 @@ void TrainRouter::block_reserved(Block &block, Train *t) track = t->get_block_allocator().first().track_iter(); for(; track; track=track.next()) { - if(!advance_to_track(reserving_route, *track)) + if(!advance_to_track(reserving_route, track)) throw logic_error("internal error (reservation outside of route)"); else if(&track->get_block()==&block) break; } } - // 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)) + 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; @@ -327,6 +338,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(); @@ -341,6 +353,7 @@ void TrainRouter::train_advanced(Block &block) 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)) @@ -350,6 +363,7 @@ void TrainRouter::train_advanced(Block &block) 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)); break; @@ -365,7 +379,7 @@ void TrainRouter::train_rear_advanced(Block &block) { Track &track = *train.get_block_allocator().iter_for(block).endpoint().track; - // Have we left some routes completely behind? + // 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)) { @@ -386,6 +400,8 @@ void TrainRouter::create_metrics() delete *i; metrics.clear(); + metrics_stale = false; + if(!destination) return; @@ -397,43 +413,57 @@ void TrainRouter::create_metrics() metrics[i]->chain_to(*metrics[(i+1)%metrics.size()]); } -Route *TrainRouter::create_lead_route(Route *lead, const Route *target) +bool TrainRouter::create_lead_route() { - if(!lead) + if(routes.empty() || !train.is_placed()) + return false; + + BlockIter fncb = train.get_first_noncritical_block(); + TrackIter next_track = fncb.track_iter(); + if(!routes.front()->has_track(*next_track.flip())) { - lead = new Route(train.get_layout()); - lead->set_name("Lead"); - lead->set_temporary(true); + Route *pf = Route::find(next_track, *routes.front()); + if(!pf) + return false; + + routes.push_front(pf); } - bool target_reached = false; - for(TrackIter i=train.get_block_allocator().first().track_iter(); i; i=i.next()) + TrackIter first_track = train.get_block_allocator().first().track_iter(); + if(!routes.front()->has_track(*first_track)) { - if(i->get_block().get_train()!=&train) - break; - if(target) + Route *lead = new Route(train.get_layout()); + lead->set_name("Lead"); + lead->set_temporary(true); + + unsigned target_tracks = 0; + for(TrackIter i=first_track; (target_tracks<2 && i); i=i.next()) { - if(target->has_track(*i)) - target_reached = true; - else if(target_reached) + if(i->get_block().get_train()!=&train) break; + if(routes.front()->has_track(*i)) + ++target_tracks; + else if(target_tracks>0) + break; + lead->add_track(*i); } - lead->add_track(*i); + + routes.push_front(lead); } - return lead; + return true; } -bool TrainRouter::is_valid_for_track(const Route &route, Track &track) const +bool TrainRouter::is_valid_for_track(const Route &route, const TrackIter &track) const { - if(!route.has_track(track)) + if(!route.has_track(*track)) return false; - if(track.get_type().is_turnout() && route.get_turnout(track.get_turnout_address())<0) + 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, Track &track) +bool TrainRouter::advance_to_track(RouteList::iterator &route, const TrackIter &track) { if(!is_valid_for_track(**route, track)) { @@ -449,21 +479,22 @@ bool TrainRouter::advance_to_track(RouteList::iterator &route, Track &track) void TrainRouter::start_planning(Layout &layout) { - RefPtr planner = new TrainRoutePlanner(layout); - + vector routers; 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()) - { - if(router->metrics_stale) - { - router->create_metrics(); - router->metrics_stale = false; - } - router->planner = planner; - } + routers.push_back(router); + + for(vector::const_iterator i=routers.begin(); i!=routers.end(); ++i) + if((*i)->metrics_stale) + (*i)->create_metrics(); + + RefPtr planner = new TrainRoutePlanner(layout); + for(vector::const_iterator i=routers.begin(); i!=routers.end(); ++i) + (*i)->planner = planner; - planner->plan(); + planner->plan_async(); }