]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/train.cpp
Add a maximum speed property for vehicle types
[r2c2.git] / source / libr2c2 / train.cpp
index 50ad137723d42ec2a8c10775767a3de92c91d745..845232fa4d9cf442fc5dddc692c5873291c35302 100644 (file)
@@ -58,7 +58,7 @@ Train::Train(Layout &l, const VehicleType &t, unsigned a, const string &p):
 
        layout.add_train(*this);
 
-       layout.get_driver().add_loco(address, protocol, loco_type);
+       loco_id = layout.get_driver().add_loco(address, protocol, loco_type);
        layout.get_driver().signal_loco_speed.connect(sigc::mem_fun(this, &Train::loco_speed_event));
        layout.get_driver().signal_loco_function.connect(sigc::mem_fun(this, &Train::loco_func_event));
 
@@ -140,7 +140,7 @@ void Train::set_function(unsigned func, bool state)
 {
        if(!loco_type.get_functions().count(func))
                throw invalid_argument("Train::set_function");
-       layout.get_driver().set_loco_function(address, func, state);
+       layout.get_driver().set_loco_function(loco_id, func, state);
 }
 
 float Train::get_control(const string &ctrl) const
@@ -161,6 +161,18 @@ float Train::get_quantized_speed() const
                return controller->get_speed();
 }
 
+float Train::get_maximum_speed() const
+{
+       float ms = 0;
+       for(vector<Vehicle *>::const_iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
+       {
+               float vms = (*i)->get_type().get_maximum_speed();
+               if(ms<=0 || (vms>0 && vms<ms))
+                       ms = vms;
+       }
+       return ms;
+}
+
 bool Train::get_function(unsigned func) const
 {
        return (functions>>func)&1;
@@ -317,7 +329,7 @@ void Train::tick(const Time::TimeDelta &dt)
                bool r = reverse;
                if(loco_type.get_swap_direction())
                        r = !r;
-               driver.set_loco_reverse(address, r);
+               driver.set_loco_reverse(loco_id, r);
 
                allocator.reverse();
                last_entry_block = BlockIter();
@@ -329,7 +341,7 @@ void Train::tick(const Time::TimeDelta &dt)
                if(speed_step!=current_speed_step && !speed_changing && !driver.is_halted() && driver.get_power())
                {
                        speed_changing = true;
-                       driver.set_loco_speed(address, speed_step);
+                       driver.set_loco_speed(loco_id, speed_step);
 
                        pure_speed = false;
                }
@@ -404,24 +416,24 @@ void Train::control_changed(const Controller::Control &ctrl)
        signal_control_changed.emit(ctrl.name, ctrl.value);
 }
 
-void Train::loco_speed_event(unsigned addr, unsigned speed, bool rev)
+void Train::loco_speed_event(unsigned id, unsigned speed, bool rev)
 {
-       if(addr==address)
+       if(id==loco_id)
        {
                current_speed_step = speed;
                bool r = reverse;
                if(loco_type.get_swap_direction())
                        r = !r;
                if(rev!=r)
-                       layout.get_driver().set_loco_reverse(address, r);
+                       layout.get_driver().set_loco_reverse(loco_id, r);
                speed_changing = false;
                pure_speed = false;
        }
 }
 
-void Train::loco_func_event(unsigned addr, unsigned func, bool state)
+void Train::loco_func_event(unsigned id, unsigned func, bool state)
 {
-       if(addr==address)
+       if(id==loco_id)
        {
                if(state)
                        functions |= 1<<func;