X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouter.cpp;h=9befe4788c4b03114cfe4939f6ea7c02eee0fe06;hb=83c6923396fcfa42e6adf949b9476663f322748b;hp=27fca7a56632c7bb1017844393f103192f1c634a;hpb=91131237203bffe734219ab8b82b9d17f5d7a87d;p=r2c2.git diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index 27fca7a..9befe47 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -83,22 +83,23 @@ void TrainRouter::use_planned_route() void TrainRouter::route_changed() { - BlockIter fncb = train.get_first_noncritical_block(); + BlockIter fncb = train.get_last_critical_block().next(); + arrival = ON_THE_WAY; 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(); + const BlockAllocator &allocator = train.get_block_allocator(); + TrackIter track = allocator.first().track_iter(); list::iterator seq_begin = sequence_points.begin(); for(; track; track=track.next()) { if(!advance_to_track(reserving_route, track)) { - already_at_end = true; + arrival = (allocator.is_block_current(track->get_block()) ? ADVANCED_TO_END : RESERVED_TO_END); break; } if(&track->get_block()==fncb.block()) @@ -106,30 +107,34 @@ void TrainRouter::route_changed() if(seq_begin!=sequence_points.end() && seq_begin->block==&track->get_block()) { + // Assume any sequence points within critical blocks to be cleared 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.refresh_blocks_from(*fncb); - if(!arrival) - train.stop_at(0); + if(!sequence_points.empty()) + { + const SequencePoint &sp = sequence_points.front(); + if(sp.block==fncb.block() && !sp.is_cleared()) + { + arrival = WAITING_FOR_SEQUENCE; + sequence_check_pending = true; + } + } } - else if(!arrival) - { - /* If arrival wasn't set before (perhaps because we weren't on a route), - set it now. */ - arrival = RESERVED_TO_END; + + /* 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 + critical blocks. */ + if(arrival) train.stop_at(&*fncb.flip()); - train.refresh_blocks_from(*fncb); - } + train.refresh_blocks_from(*fncb); + // If no arrival condition was found, clear a possible previous stop marker. + if(!arrival) + train.stop_at(0); const Route *route = get_route(); signal_route_changed.emit(route); @@ -241,11 +246,14 @@ void TrainRouter::tick(const Time::TimeDelta &dt) if(sequence_check_pending) { if(sequence_points.front().is_cleared()) + { + arrival = ON_THE_WAY; train.stop_at(0); + } sequence_check_pending = false; } - if(arrival==RESERVED_TO_END && !train.get_speed()) + if(arrival==ADVANCED_TO_END && !train.get_speed()) { signal_arrived.emit(waypoints.back()); signal_event.emit(Message("arrived", waypoints.back())); @@ -345,7 +353,10 @@ 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; train.stop_at(&block); + } } } @@ -353,7 +364,7 @@ void TrainRouter::train_advanced(Block &block) { BlockIter b_iter = train.get_block_allocator().iter_for(block); - if(waypoints.size()>1) + if(!waypoints.empty()) { // A waypoint is considered reached when the train has advanced through it. const TrackChain &wp = *waypoints.front(); @@ -364,10 +375,18 @@ 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)); + if(waypoints.size()==1) + { + if(arrival==RESERVED_TO_END) + arrival = ADVANCED_TO_END; + } + else + { + waypoints.erase(waypoints.begin()); + metrics_stale = true; + signal_waypoint_reached.emit(&wp); + signal_event.emit(Message("waypoint-reached", &wp)); + } break; } else if(!block.has_track(*t_iter)) @@ -419,23 +438,24 @@ bool TrainRouter::create_lead_route() if(routes.empty() || !train.is_placed()) return false; - BlockIter fncb = train.get_first_noncritical_block(); - TrackIter next_track = fncb.track_iter(); + BlockIter lcb = train.get_last_critical_block(); + TrackIter last_track_rev = lcb.reverse().track_iter(); unsigned count = 0; - for(TrackIter i=next_track.flip(); (i && i->get_block().get_train()==&train); i=i.next()) + for(TrackIter i=last_track_rev; (i && i->get_block().get_train()==&train); i=i.next()) { if(routes.front()->has_track(*i)) ++count; else if(count>0) { if(count==routes.front()->get_tracks().size()) - next_track = i.flip(); + last_track_rev = i; break; } } - if(!routes.front()->has_track(*next_track) && !routes.front()->has_track(*next_track.flip())) + TrackIter next_track = last_track_rev.flip(); + if(!routes.front()->has_track(*last_track_rev) && !routes.front()->has_track(*next_track)) { Route *pf = Route::find(next_track, *routes.front()); if(!pf) @@ -445,7 +465,7 @@ bool TrainRouter::create_lead_route() } Route *lead = 0; - for(TrackIter i=next_track.flip(); (i && i->get_block().get_train()==&train); i=i.next()) + for(TrackIter i=last_track_rev; (i && i->get_block().get_train()==&train); i=i.next()) { if(!lead && !routes.front()->has_track(*i)) { @@ -466,27 +486,25 @@ bool TrainRouter::create_lead_route() return true; } -bool TrainRouter::is_valid_for_track(const Route &route, const TrackIter &track) const -{ - if(!route.has_track(*track)) - return false; - 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, const TrackIter &track) { - if(!is_valid_for_track(**route, track)) + Track &prev_track = *track.flip(); + unsigned taddr = (track->get_type().is_turnout() ? track->get_turnout_address() : 0); + for(unsigned i=0; route!=routes.end(); ++i) { - ++route; - if(route==routes.end()) - return false; - if(!is_valid_for_track(**route, track)) + bool in_route = (*route)->has_track(*track); + bool prev_in_route = (*route)->has_track(prev_track); + bool known_path = (!taddr || (*route)->get_turnout(taddr)>=0); + + if(in_route && (known_path || !prev_in_route)) + return true; + else if(i==0 || prev_in_route) + ++route; + else throw logic_error("internal error (routes are not continuous)"); } - return true; + return false; } void TrainRouter::get_routers(Layout &layout, vector &routers)