]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/track.cpp
Don't collide rays with unplaced vehicles
[r2c2.git] / source / libr2c2 / track.cpp
index 8ecb9b9bac00833eaa70190982fcb1933b43e9ae..5da34f692b370c9f5711536e2d0838da78e9f8cb 100644 (file)
@@ -53,6 +53,8 @@ Track::Track(Layout &l, const TrackType &t):
 Track::~Track()
 {
        break_links();
+       if(layout.has_driver() && turnout_id)
+               layout.get_driver().remove_turnout(turnout_id);
        layout.remove(*this);
 }
 
@@ -155,11 +157,15 @@ void Track::set_turnout_id(unsigned i)
        if(!i)
                throw invalid_argument("Track::set_turnout_id");
 
+       Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0);
+
+       if(driver && turnout_id)
+               driver->remove_turnout(turnout_id);
        turnout_id = i;
        layout.create_blocks(*this);
        layout.update_routes();
-       if(layout.has_driver() && turnout_id)
-               layout.get_driver().add_turnout(turnout_id, type);
+       if(driver && turnout_id)
+               driver->add_turnout(turnout_id, type);
 }
 
 void Track::set_sensor_id(unsigned i)
@@ -183,6 +189,13 @@ void Track::set_active_path(unsigned p)
        layout.get_driver().set_turnout(turnout_id, p);
 }
 
+float Track::get_path_length(int p) const
+{
+       if(p<0)
+               p = active_path;
+       return type.get_path_length(p);
+}
+
 OrientedPoint Track::get_point(unsigned epi, unsigned path, float d) const
 {
        OrientedPoint p = type.get_point(epi, path, d);
@@ -337,9 +350,14 @@ bool Track::break_link(unsigned i)
                return false;
 
        links[i] = 0;
-       other->break_link(*this);
-       // XXX Creates the blocks twice, because the other track calls this too
-       layout.create_blocks(*this);
+       if(!other->break_link(*this))
+       {
+               /* If the call doesn't succeed, it means that the other track already
+               broke the link and is calling us right now.  Recreate blocks in the inner
+               call so it occurs before any signals are emitted. */
+               layout.create_blocks(*this);
+       }
+
        signal_link_changed.emit(i, 0);
 
        return true;