From: Mikko Rasa Date: Fri, 6 Feb 2015 14:32:18 +0000 (+0200) Subject: Consider block deallocation when planning routes X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ace4d2175dce7364ff094b8dd44c53e47ba395b1;p=r2c2.git Consider block deallocation when planning routes Blocks are not released until a sensor is detriggered. Not taking that into account could cause situations where the planner tries to drive trains through a balloon loop which is actually too small for them. --- diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index a05e97e..5bffec1 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -375,20 +375,37 @@ void TrainRoutePlanner::TrainRoutingState::advance(float distance) offset += distance; back_offset += distance; - OccupiedTrack *last_occ = occupied_tracks; - for(unsigned n=occupied_tracks->n_tracks; n>1; --n) - last_occ = last_occ->next; + unsigned count_to_free = 0; + unsigned last_sensor_addr = 0; + float distance_after_sensor = 0; + OccupiedTrack *occ = occupied_tracks; + for(unsigned n=occupied_tracks->n_tracks; n>0; --n) + { + if(unsigned saddr = occ->track->get_sensor_address()) + { + if(saddr!=last_sensor_addr) + { + count_to_free = 0; + distance_after_sensor = 0; + } + last_sensor_addr = saddr; + } + + ++count_to_free; + distance_after_sensor += occ->path_length; + + occ = occ->next; + } - // XXX What if there's multiple tracks to remove? - if(back_offset>last_occ->path_length) + if(count_to_free && back_offset>distance_after_sensor) { - back_offset -= last_occ->path_length; + back_offset -= distance_after_sensor; if(occupied_tracks->refcount>1) { --occupied_tracks->refcount; occupied_tracks = new OccupiedTrack(*occupied_tracks); } - --occupied_tracks->n_tracks; + occupied_tracks->n_tracks -= count_to_free; } distance_traveled += distance;