From: Mikko Rasa Date: Fri, 23 Apr 2010 17:21:00 +0000 (+0000) Subject: Release non-sensor blocks when the tail end of the train has passed far enough X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=3b3099eb9e6d14e2a0495ea78144a734a102112f;p=r2c2.git Release non-sensor blocks when the tail end of the train has passed far enough --- diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index 973cc07..44f9298 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -372,6 +372,52 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) } else if(end_of_route) set_route(0); + + if(!cur_blocks.empty() && !cur_blocks.front().block->get_sensor_id()) + { + Vehicle &veh = *(reverse ? vehicles.front() : vehicles.back()); + + list::iterator i = cur_blocks.begin(); + const Block::Endpoint &bep = i->block->get_endpoints()[i->entry]; + + Track *track = bep.track; + unsigned entry = bep.track_ep; + + bool found = false; + float dist = veh.get_offset()-veh.get_type().get_length()/2; + while(1) + { + if(track==veh.get_track()) + { + found = true; + break; + } + + if(i!=cur_blocks.begin()) + { + float path_len = track->get_type().get_path_length(track->get_active_path()); + dist += path_len; + } + + unsigned exit = track->traverse(entry); + Track *next = track->get_link(exit); + entry = next->get_endpoint_by_link(*track); + track = next; + + if(!i->block->get_tracks().count(track)) + { + ++i; + if(i==cur_blocks.end()) + break; + } + } + + if(found && i!=cur_blocks.begin() && dist>10*layout.get_catalogue().get_scale()) + { + cur_blocks.front().block->reserve(0); + cur_blocks.erase(cur_blocks.begin()); + } + } } void Train::save(list &st) const @@ -539,15 +585,15 @@ void Train::sensor_event(unsigned addr, bool state) if(layout.get_driver().get_sensor(i->block->get_sensor_id())) break; else + { end = i; + ++end; + } } if(end!=cur_blocks.begin()) - { // Free blocks up to the last inactive sensor - ++end; release_blocks(cur_blocks, cur_blocks.begin(), end); - } } }