]> git.tdb.fi Git - r2c2.git/commitdiff
Prevent some undesirable recursive calls
authorMikko Rasa <tdb@tdb.fi>
Sun, 3 Oct 2010 13:27:54 +0000 (13:27 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 3 Oct 2010 13:27:54 +0000 (13:27 +0000)
source/libmarklin/train.cpp
source/libmarklin/train.h
source/libmarklin/vehicle.cpp

index c3affef1fdb48dbb0fbe18ff7d9adefda12a9b70..96566a63a5f48fe89049b5c1f7c3a2a601d46e66 100644 (file)
@@ -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<int>(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<BlockRef>::iterator i;
        for(i=rsv_blocks.begin(); (i!=rsv_blocks.end() && !i->block->get_sensor_id()); ++i) ;
index 319119b93828fd745639a7a37b8c1ae3d6a69794..3e7a56b7e423da7e9d4157d88138fc7e4e4ede85 100644 (file)
@@ -78,6 +78,8 @@ private:
        std::list<BlockRef> cur_blocks;
        std::list<BlockRef> rsv_blocks;
        Block *pending_block;
+       bool reserving;
+       bool advancing;
        Controller *controller;
        Timetable *timetable;
        bool active;
index 8761bb6f9f80bfc9a906da113ac3c7ff38adf8d7..ac03de16a4ce7ed46ff87e5b04bdc40e8d79f6aa 100644 (file)
@@ -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