From: Mikko Rasa Date: Fri, 11 Apr 2014 17:24:30 +0000 (+0300) Subject: Use signal_advanced for train speed measurement and vehicle placement X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=42a5c6606e82b1a27cb6d4d7afa630c75c869f97;p=r2c2.git Use signal_advanced for train speed measurement and vehicle placement It's stupid to try to reproduce the logic of checking whether this is the correct sensor when BlockAllocator has that information already. --- diff --git a/source/libr2c2/blockallocator.cpp b/source/libr2c2/blockallocator.cpp index ba07e94..8e87269 100644 --- a/source/libr2c2/blockallocator.cpp +++ b/source/libr2c2/blockallocator.cpp @@ -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); diff --git a/source/libr2c2/blockallocator.h b/source/libr2c2/blockallocator.h index 6ba3cfb..33e9d55 100644 --- a/source/libr2c2/blockallocator.h +++ b/source/libr2c2/blockallocator.h @@ -27,7 +27,7 @@ public: void hint(unsigned); }; - sigc::signal signal_advanced; + sigc::signal signal_advanced; private: struct BlockMatch; diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index b278948..98553e3 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -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(&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!=█ 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(&sensor)) + else if(BeamGate *gate = dynamic_cast(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) diff --git a/source/libr2c2/train.h b/source/libr2c2/train.h index 25392bd..3c4176d 100644 --- a/source/libr2c2/train.h +++ b/source/libr2c2/train.h @@ -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;