]> git.tdb.fi Git - r2c2.git/commitdiff
Release non-sensor blocks when the tail end of the train has passed far enough
authorMikko Rasa <tdb@tdb.fi>
Fri, 23 Apr 2010 17:21:00 +0000 (17:21 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 23 Apr 2010 17:21:00 +0000 (17:21 +0000)
source/libmarklin/train.cpp

index 973cc07e8c5a234c9d0e11094b810585ba693344..44f9298d142daf72682594148476699a0ce89554 100644 (file)
@@ -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<BlockRef>::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<DataFile::Statement> &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);
-               }
        }
 }