3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
18 AIControl::AIControl(Train &t, ControlModel *n):
21 target_speed(TrainControl::continuous("speed", -1000, 1000)),
26 train.signal_arrived.connect(sigc::mem_fun(this, &AIControl::arrived));
29 AIControl::~AIControl()
34 void AIControl::set_control(const string &n, float v)
38 if(v && !train.is_active())
39 train.set_active(true);
43 next_model->set_control("speed", target_speed.value);
45 signal_control_changed.emit(n, target_speed.value);
48 next_model->set_control(n, v);
51 const TrainControl &AIControl::get_control(const string &n) const
56 return next_model->get_control(n);
59 float AIControl::get_speed() const
61 return next_model->get_speed();
64 bool AIControl::get_reverse() const
66 if(float ns = next_model->get_speed())
69 return target_speed.value<0;
72 float AIControl::get_braking_distance() const
74 return next_model->get_braking_distance();
77 void AIControl::tick(const Time::TimeDelta &dt)
79 float rsv_dist = train.get_reserved_distance();
80 float brake_dist = next_model->get_braking_distance()*1.15;
81 float margin = 25*train.get_layout().get_catalogue().get_scale();
82 if(!blocked && rsv_dist<brake_dist+margin)
85 next_model->set_control("speed", 0);
87 else if(blocked && rsv_dist>brake_dist+margin*3)
90 next_model->set_control("speed", target_speed.value);
95 if(!target_speed.value && !next_model->get_speed() && train.is_active())
96 train.set_active(false);
99 void AIControl::control_changed(const string &n, float v)
102 signal_control_changed.emit(n, v);
105 void AIControl::arrived()
107 set_control("speed", 0);
110 } // namespace Marklin