]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/blockallocator.cpp
Handle sensors in a separate class
[r2c2.git] / source / libr2c2 / blockallocator.cpp
index fc414cc99825d25b94140438923b9ed9f46bfff7..acb59aeae808beab4a212b245435bf7f9c181c93 100644 (file)
@@ -1,9 +1,11 @@
 #include <msp/core/maputils.h>
 #include <msp/core/raii.h>
 #include "blockallocator.h"
+#include "block.h"
 #include "catalogue.h"
 #include "driver.h"
 #include "layout.h"
+#include "trackcircuit.h"
 #include "trackiter.h"
 #include "train.h"
 #include "vehicle.h"
@@ -22,7 +24,7 @@ BlockAllocator::BlockAllocator(Train &t):
 {
        Layout &layout = train.get_layout();
        layout.signal_block_reserved.connect(sigc::mem_fun(this, &BlockAllocator::block_reserved));
-       layout.signal_block_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::block_state_changed));
+       layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &BlockAllocator::sensor_state_changed));
 
        const set<Track *> &tracks = layout.get_tracks();
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
@@ -304,12 +306,18 @@ void BlockAllocator::block_reserved(Block &block, const Train *tr)
                reserve_more();
 }
 
-void BlockAllocator::block_state_changed(Block &block, Block::State state)
+void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
 {
-       if(block.get_train()!=&train)
+       Block *block = 0;
+       if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
+               block = &tc->get_block();
+       else
                return;
 
-       if(state==Block::MAYBE_ACTIVE)
+       if(block->get_train()!=&train)
+               return;
+
+       if(state==Sensor::MAYBE_ACTIVE)
        {
                // Find the first sensor block from our reserved blocks that isn't this sensor
                BlockList::iterator end;
@@ -317,7 +325,7 @@ void BlockAllocator::block_state_changed(Block &block, Block::State state)
                for(end=cur_blocks_end; end!=blocks.end(); ++end)
                        if((*end)->get_sensor_id())
                        {
-                               if(&**end!=&block)
+                               if(&**end!=block)
                                {
                                        if(result==0)
                                                result = 2;
@@ -340,7 +348,7 @@ void BlockAllocator::block_state_changed(Block &block, Block::State state)
                else if(result==3)
                        train.get_layout().emergency("Sensor for "+train.get_name()+" triggered out of order");
        }
-       else if(state==Block::INACTIVE)
+       else if(state==Sensor::INACTIVE)
        {
                const Vehicle &veh = train.get_vehicle(train.get_controller().get_reverse() ? 0 : train.get_n_vehicles()-1);
                const Block &veh_block = veh.get_track()->get_block();