From 848a39d9cf52c7158968ba3f9af4fd5d4e2b4391 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 3 Oct 2010 13:27:54 +0000 Subject: [PATCH] Prevent some undesirable recursive calls --- source/libmarklin/train.cpp | 26 ++++++++++++++++++++++---- source/libmarklin/train.h | 2 ++ source/libmarklin/vehicle.cpp | 3 ++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/source/libmarklin/train.cpp b/source/libmarklin/train.cpp index c3affef..96566a6 100644 --- a/source/libmarklin/train.cpp +++ b/source/libmarklin/train.cpp @@ -32,6 +32,8 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a): address(a), priority(0), pending_block(0), + reserving(false), + advancing(false), controller(new AIControl(*this, new SimpleController)), timetable(0), active(false), @@ -407,7 +409,11 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt) float d = get_real_speed(current_speed)*(dt/Time::sec); if(ok) + { + advancing = true; vehicle.advance(reverse ? -d : d); + advancing = false; + } else if(accurate_position) { overshoot_dist += d; @@ -592,7 +598,7 @@ void Train::sensor_event(unsigned addr, bool state) j->block->traverse(j->entry, &block_len); travel_dist += block_len; - if(j->block->get_sensor_id()==addr) + if(j->block->get_sensor_id()==addr && !advancing) { const Block::Endpoint &bep = j->block->get_endpoints()[j->entry]; if(reverse) @@ -664,7 +670,12 @@ void Train::turnout_event(unsigned addr, bool) unsigned pending_addr = pending_block->get_turnout_id(); bool double_addr = (*pending_block->get_tracks().begin())->get_type().is_double_address(); if(addr==pending_addr || (double_addr && addr==pending_addr+1)) - reserve_more(); + { + if(reserving) + pending_block = 0; + else + reserve_more(); + } } } @@ -717,6 +728,8 @@ unsigned Train::reserve_more() } } + reserving = true; + bool got_more = false; BlockRef *good = last; unsigned good_sens = nsens; @@ -805,10 +818,13 @@ unsigned Train::reserve_more() if(path!=static_cast(entry_ep.track->get_active_path())) { // The turnout is set to wrong path - switch and wait for it - link->reserve(0); pending_block = link; entry_ep.track->set_active_path(path); - break; + if(pending_block) + { + link->reserve(0); + break; + } } } @@ -830,6 +846,8 @@ unsigned Train::reserve_more() last = &rsv_blocks.back(); } + reserving = false; + // Make any sensorless blocks at the beginning immediately current list::iterator i; for(i=rsv_blocks.begin(); (i!=rsv_blocks.end() && !i->block->get_sensor_id()); ++i) ; diff --git a/source/libmarklin/train.h b/source/libmarklin/train.h index 319119b..3e7a56b 100644 --- a/source/libmarklin/train.h +++ b/source/libmarklin/train.h @@ -78,6 +78,8 @@ private: std::list cur_blocks; std::list rsv_blocks; Block *pending_block; + bool reserving; + bool advancing; Controller *controller; Timetable *timetable; bool active; diff --git a/source/libmarklin/vehicle.cpp b/source/libmarklin/vehicle.cpp index 8761bb6..ac03de1 100644 --- a/source/libmarklin/vehicle.cpp +++ b/source/libmarklin/vehicle.cpp @@ -213,6 +213,8 @@ void Vehicle::check_sensor(float offset, unsigned &sensor) { /* Sensor ID under axle has changed. Deduce movement direction by using the sensor ID under the midpoint of the vehicle. */ + /* XXX This depends on the simulation running fast enough. Something + more robust would be preferable. */ unsigned old = sensor; sensor = s; unsigned mid = track_pos.track->get_sensor_id(); @@ -220,7 +222,6 @@ void Vehicle::check_sensor(float offset, unsigned &sensor) if(s && s!=mid) /* There's a sensor and it's different from mid. We've just entered that sensor. */ - // XXX The Train will reset the vehicles to the start of the sensor, which is somewhat undesirable layout.get_driver().set_sensor(sensor, true); if(old && old!=mid) /* A sensor was under the axle and it was different from mid. We've -- 2.45.2