From 14b71235fbf3406ed88fab9125ea646eb6a9b702 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 10 Jul 2013 20:31:22 +0300 Subject: [PATCH] Refactor and fix vehicle positioning logic --- source/libr2c2/train.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index aa2ba3f..b5f600e 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -193,6 +193,7 @@ void Train::place(const BlockIter &block) allocator.start_from(block); accurate_position = false; + last_entry_block = BlockIter(); if(reverse) vehicles.front()->place(block.reverse().track_iter(), 0, Vehicle::FRONT_BUFFER); @@ -207,6 +208,7 @@ void Train::unplace() allocator.clear(); accurate_position = false; + last_entry_block = BlockIter(); for(vector::iterator i=vehicles.begin(); i!=vehicles.end(); ++i) (*i)->unplace(); @@ -309,6 +311,7 @@ void Train::tick(const Time::TimeDelta &dt) driver.set_loco_reverse(address, r); allocator.reverse(); + last_entry_block = BlockIter(); } if(speed_quantizer) @@ -422,28 +425,28 @@ void Train::loco_func_event(unsigned addr, unsigned func, bool state) void Train::sensor_state_changed(Sensor &sensor, Sensor::State state) { - if(state!=Sensor::MAYBE_ACTIVE) + if(!current_speed_step || state!=Sensor::MAYBE_ACTIVE) return; Block *block = sensor.get_block(); if(!block || block->get_train()!=this) 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_id()) + return; + } + if(dynamic_cast(&sensor)) { - if(last_entry_block) + if(last_entry_block && pure_speed && speed_quantizer) { - float travel_distance = -1; - if(pure_speed && speed_quantizer && current_speed_step>0) - travel_distance = 0; + float travel_distance = 0; for(BlockIter i=last_entry_block; &*i!=block; i=i.next()) - { - if(i->get_sensor_id()) - return; - if(travel_distance>=0) - travel_distance += i->get_path_length(i.entry()); - } + travel_distance += i->get_path_length(i.entry()); if(travel_distance>0) { -- 2.45.2