]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/train.cpp
Basic support for beam gate sensors
[r2c2.git] / source / libr2c2 / train.cpp
index 6cb3205eb175fad4f6e6d8b18997a725df2da7b5..aa2ba3f528627beb093dc936446b1f2b3bbd4d81 100644 (file)
@@ -53,6 +53,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p):
                speed_quantizer = new SpeedQuantizer(speed_steps);
 
        vehicles.push_back(new Vehicle(layout, loco_type));
+       vehicles.back()->set_train(this);
 
        layout.add_train(*this);
 
@@ -87,6 +88,8 @@ void Train::add_vehicle(const VehicleType &vt)
        Vehicle *veh = new Vehicle(layout, 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)
@@ -95,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
@@ -338,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;
 }
@@ -410,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)
                {
@@ -478,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
@@ -490,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());
@@ -561,6 +575,7 @@ void Train::Loader::vehicle(ArticleNumber art_nr)
        Vehicle *veh = new Vehicle(obj.layout, vtype);
        obj.vehicles.back()->attach_back(*veh);
        obj.vehicles.push_back(veh);
+       veh->set_train(&obj);
 }
 
 } // namespace R2C2