]> git.tdb.fi Git - r2c2.git/commitdiff
Use signal_advanced for train speed measurement and vehicle placement
authorMikko Rasa <tdb@tdb.fi>
Fri, 11 Apr 2014 17:24:30 +0000 (20:24 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 11 Apr 2014 19:35:10 +0000 (22:35 +0300)
It's stupid to try to reproduce the logic of checking whether this is the
correct sensor when BlockAllocator has that information already.

source/libr2c2/blockallocator.cpp
source/libr2c2/blockallocator.h
source/libr2c2/train.cpp
source/libr2c2/train.h

index ba07e94e76e96baa746edbab655b7b34b9233414..8e8726955c42cfddee688a7a0a1c775355857748 100644 (file)
@@ -337,7 +337,7 @@ void BlockAllocator::advance_front(const Block *block, bool inclusive)
        // Update cur_blocks_end first to keep things consistent.
        cur_blocks_end = end;
        for(; i!=end; ++i)
-               signal_advanced.emit(**i);
+               signal_advanced.emit(**i, (*i)->get_sensor());
 }
 
 void BlockAllocator::advance_front(const Sensor *sensor)
@@ -488,6 +488,8 @@ void BlockAllocator::sensor_state_changed(Sensor &sensor, Sensor::State state)
        {
                if(&sensor==next_sensor)
                {
+                       if(is_block_current(*block))
+                               signal_advanced.emit(*block, &sensor);
                        update_next_sensor(next_sensor);
                        advance_front(next_sensor);
 
index 6ba3cfbb7559ca7e718b2471cd9fca952fec44ce..33e9d5511654e41928527a5cc872d2735c7b4a29 100644 (file)
@@ -27,7 +27,7 @@ public:
                void hint(unsigned);
        };
 
-       sigc::signal<void, Block &> signal_advanced;
+       sigc::signal<void, Block &, Sensor *> signal_advanced;
 
 private:
        struct BlockMatch;
index b278948a83ed9c8d38d7694f293037a48ef99842..98553e3c611540aef90dc34477315ec3c8f6ea48 100644 (file)
@@ -63,13 +63,11 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p):
        layout.get_driver().signal_loco_speed.connect(sigc::mem_fun(this, &Train::loco_speed_event));
        layout.get_driver().signal_loco_function.connect(sigc::mem_fun(this, &Train::loco_func_event));
 
-       layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &Train::sensor_state_changed));
-
        layout.get_driver().signal_halt.connect(sigc::mem_fun(this, &Train::halt_event));
 
        controller->signal_control_changed.connect(sigc::mem_fun(this, &Train::control_changed));
 
-       allocator.signal_advanced.connect(signal_advanced);
+       allocator.signal_advanced.connect(sigc::mem_fun(this, &Train::advanced));
 }
 
 Train::~Train()
@@ -454,29 +452,21 @@ void Train::loco_func_event(unsigned id, unsigned func, bool state)
        }
 }
 
-void Train::sensor_state_changed(Sensor &sensor, Sensor::State state)
+void Train::advanced(Block &block, Sensor *sensor)
 {
-       if(!current_speed_step || state!=Sensor::MAYBE_ACTIVE)
-               return;
+       if(!sensor || sensor==block.get_sensor())
+               signal_advanced.emit(block);
 
-       Block *block = sensor.get_block();
-       if(!block || block->get_train()!=this)
+       if(!sensor)
                return;
 
-       if(last_entry_block && &*last_entry_block!=block)
-       {
-               for(BlockIter i=last_entry_block.next(); (i && &*i!=block); i=i.next())
-                       if(i->get_train()!=this || i->get_sensor_address())
-                               return;
-       }
-
-       if(dynamic_cast<TrackCircuit *>(&sensor))
+       if(sensor==block.get_sensor())
        {
                if(last_entry_block && pure_speed && speed_quantizer)
                {
                        float travel_distance = 0;
 
-                       for(BlockIter i=last_entry_block; &*i!=block; i=i.next())
+                       for(BlockIter i=last_entry_block; &*i!=&block; i=i.next())
                                travel_distance += i->get_path_length(i.entry());
 
                        if(travel_distance>0)
@@ -488,7 +478,7 @@ void Train::sensor_state_changed(Sensor &sensor, Sensor::State state)
                        }
                }
 
-               last_entry_block = allocator.iter_for(*block);
+               last_entry_block = allocator.iter_for(block);
                travel_time = Time::zero;
                if(!layout.get_driver().is_halted())
                {
@@ -509,12 +499,12 @@ void Train::sensor_state_changed(Sensor &sensor, Sensor::State state)
                                vehicles.front()->place(track, VehiclePlacement::FRONT_AXLE);
                }
        }
-       else if(BeamGate *gate = dynamic_cast<BeamGate *>(&sensor))
+       else if(BeamGate *gate = dynamic_cast<BeamGate *>(sensor))
        {
                if(!advancing && vehicles.front()->is_placed())
                {
-                       TrackIter track = allocator.iter_for(*block).track_iter();
-                       for(; (track && &track->get_block()==block); track=track.next())
+                       TrackIter track = allocator.iter_for(block).track_iter();
+                       for(; (track && &track->get_block()==&block); track=track.next())
                                if(track.track()==gate->get_track())
                                {
                                        if(reverse)
index 25392bd768db8f06493fe414fb6f9e038068cec1..3c4176d3e3cf6de5fe816e484b51205546a359bb 100644 (file)
@@ -132,7 +132,7 @@ private:
        void control_changed(const Controller::Control &);
        void loco_speed_event(unsigned, unsigned, bool);
        void loco_func_event(unsigned, unsigned, bool);
-       void sensor_state_changed(Sensor &, Sensor::State);
+       void advanced(Block &, Sensor *);
        void halt_event(bool);
        void block_reserved(const Block &, const Train *);
        float get_reserved_distance_until(const Block *) const;