X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouter.cpp;h=01205fa834a3fc5575a334be2f67ed8a47109cf4;hb=ef70d27737dd8333aef142e4810dc223b7bc7696;hp=0885bd2861d1e8c81eff3cc8b1f6d415fa8cee22;hpb=d607c5454e63ac9be1384419a0ec5d561859c2a3;p=r2c2.git diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index 0885bd2..01205fa 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -91,6 +91,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()) { @@ -106,12 +109,16 @@ void TrainRouter::route_changed() 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); @@ -211,27 +218,25 @@ 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); - current_sequence = 0; - sequence_check_pending = false; - - 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; } if(sequence_check_pending) @@ -274,18 +279,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(); @@ -298,7 +302,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); @@ -306,6 +310,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(); @@ -320,11 +326,13 @@ 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; @@ -333,6 +341,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(); @@ -347,6 +356,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)) @@ -371,7 +381,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)) {