]> 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 4708db5d40de6c79d26de28ef61af696afe6f48d..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)
@@ -344,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;