]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/train.cpp
Basic support for beam gate sensors
[r2c2.git] / source / libr2c2 / train.cpp
index 74bc50328ce8b6bb60f7f917c149b97989d5e7b7..aa2ba3f528627beb093dc936446b1f2b3bbd4d81 100644 (file)
@@ -89,6 +89,7 @@ void Train::add_vehicle(const VehicleType &vt)
        vehicles.back()->attach_back(*veh);
        vehicles.push_back(veh);
        veh->set_train(this);
+       signal_vehicle_added.emit(vehicles.size()-1, *veh);
 }
 
 void Train::remove_vehicle(unsigned i)
@@ -97,10 +98,17 @@ void Train::remove_vehicle(unsigned i)
                throw out_of_range("Train::remove_vehicle");
        if(i==0)
                throw logic_error("can't remove locomotive");
-       delete vehicles[i];
+
+       Vehicle *veh = vehicles[i];
        vehicles.erase(vehicles.begin()+i);
+       veh->detach_front();
        if(i<vehicles.size())
+       {
+               veh->detach_back();
                vehicles[i-1]->attach_back(*vehicles[i]);
+       }
+       signal_vehicle_removed.emit(i, *veh);
+       delete veh;
 }
 
 unsigned Train::get_n_vehicles() const
@@ -340,6 +348,8 @@ void Train::tick(const Time::TimeDelta &dt)
                        }
                }
        }
+       else if(intent_to_move && !allocator.is_active())
+               allocator.set_active(true);
        else if(allocator.is_active() && !intent_to_move && !stop_timeout)
                stop_timeout = 2*Time::sec;
 }
@@ -412,13 +422,14 @@ void Train::loco_func_event(unsigned addr, unsigned func, bool state)
 
 void Train::sensor_state_changed(Sensor &sensor, Sensor::State state)
 {
-       Block *block = 0;
-       if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
-               block = &tc->get_block();
-       else
+       if(state!=Sensor::MAYBE_ACTIVE)
                return;
 
-       if(block->get_train()==this && state==Sensor::MAYBE_ACTIVE)
+       Block *block = sensor.get_block();
+       if(!block || block->get_train()!=this)
+               return;
+
+       if(dynamic_cast<TrackCircuit *>(&sensor))
        {
                if(last_entry_block)
                {
@@ -480,8 +491,7 @@ float Train::get_reserved_distance_until(const Block *until_block) const
        if(!track)  // XXX Probably unnecessary
                return 0;
 
-       BlockIter block = track.block_iter();
-       if(&*block==until_block)
+       if(&track->get_block()==until_block)
                return 0;
 
        // Account for the vehicle's offset on its current track
@@ -492,6 +502,8 @@ float Train::get_reserved_distance_until(const Block *until_block) const
                result = track->get_type().get_path_length(track->get_active_path())-result;
        result -= veh.get_type().get_length()/2;
 
+       BlockIter block = track.block_iter();
+
        // Count remaining distance in the vehicle's current block
        for(track=track.next(); &track->get_block()==&*block; track=track.next())
                result += track->get_type().get_path_length(track->get_active_path());