]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/train.cpp
Basic support for beam gate sensors
[r2c2.git] / source / libr2c2 / train.cpp
index 4f6c2046d7d9e0528158bc5d5c6af560816e3cd0..aa2ba3f528627beb093dc936446b1f2b3bbd4d81 100644 (file)
@@ -422,13 +422,14 @@ void Train::loco_func_event(unsigned addr, unsigned func, bool state)
 
 void Train::sensor_state_changed(Sensor &sensor, Sensor::State state)
 {
-       Block *block = 0;
-       if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
-               block = &tc->get_block();
-       else
+       if(state!=Sensor::MAYBE_ACTIVE)
+               return;
+
+       Block *block = sensor.get_block();
+       if(!block || block->get_train()!=this)
                return;
 
-       if(block->get_train()==this && state==Sensor::MAYBE_ACTIVE)
+       if(dynamic_cast<TrackCircuit *>(&sensor))
        {
                if(last_entry_block)
                {
@@ -490,8 +491,7 @@ float Train::get_reserved_distance_until(const Block *until_block) const
        if(!track)  // XXX Probably unnecessary
                return 0;
 
-       BlockIter block = track.block_iter();
-       if(&*block==until_block)
+       if(&track->get_block()==until_block)
                return 0;
 
        // Account for the vehicle's offset on its current track
@@ -502,6 +502,8 @@ float Train::get_reserved_distance_until(const Block *until_block) const
                result = track->get_type().get_path_length(track->get_active_path())-result;
        result -= veh.get_type().get_length()/2;
 
+       BlockIter block = track.block_iter();
+
        // Count remaining distance in the vehicle's current block
        for(track=track.next(); &track->get_block()==&*block; track=track.next())
                result += track->get_type().get_path_length(track->get_active_path());