]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/train.cpp
Support trains with multiple vehicles
[r2c2.git] / source / libmarklin / train.cpp
index 24ccfa2448bf1abf814afc4d728da7c935a9908b..fad68605e7c70952faf21ac6931616a7114a7f70 100644 (file)
@@ -57,6 +57,8 @@ Train::Train(Layout &l, const LocoType &t, unsigned a):
        layout.signal_block_reserved.connect(sigc::mem_fun(this, &Train::block_reserved));
        layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Train::sensor_event));
        layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Train::turnout_event));
+
+       control->signal_control_changed.connect(signal_control_changed);
 }
 
 Train::~Train()
@@ -90,7 +92,6 @@ const Vehicle &Train::get_vehicle(unsigned i) const
 void Train::set_control(const string &n, float v)
 {
        control->set_control(n, v);
-       signal_control_changed.emit(n, control->get_control(n).value);
 }
 
 void Train::set_active(bool a)
@@ -335,7 +336,8 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
                if(!active)
                        set_active(true);
 
-               Track *track = vehicles[0]->get_track();
+               Vehicle &vehicle = *(reverse ? vehicles.back() : vehicles.front());
+               Track *track = vehicle.get_track();
 
                bool ok = false;
                for(list<BlockRef>::const_iterator i=cur_blocks.begin(); (!ok && i!=cur_blocks.end()); ++i)
@@ -344,7 +346,7 @@ void Train::tick(const Time::TimeStamp &t, const Time::TimeDelta &dt)
                if(ok)
                {
                        float d = get_real_speed(current_speed)*(dt/Time::sec);
-                       vehicles[0]->advance(reverse ? -d : d);
+                       vehicle.advance(reverse ? -d : d);
                }
        }
        else if(end_of_route)
@@ -784,6 +786,7 @@ Train::Loader::Loader(Train &t):
        add("real_speed",  &Loader::real_speed);
        add("route",       &Loader::route);
        add("timetable",   &Loader::timetable);
+       add("vehicle",     &Loader::vehicle);
 }
 
 void Train::Loader::block(unsigned id)
@@ -837,4 +840,12 @@ void Train::Loader::timetable()
        load_sub(*obj.timetable);
 }
 
+void Train::Loader::vehicle(unsigned n)
+{
+       const VehicleType &vtype = obj.layout.get_catalogue().get_vehicle(n);
+       Vehicle *veh = new Vehicle(obj.layout, vtype);
+       obj.vehicles.back()->attach_back(*veh);
+       obj.vehicles.push_back(veh);
+}
+
 } // namespace Marklin