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 float AIControl::get_braking_distance() const
66 return next_model->get_braking_distance();
69 void AIControl::tick(const Time::TimeDelta &dt)
71 float rsv_dist = train.get_reserved_distance();
72 float brake_dist = next_model->get_braking_distance()*1.15;
73 float margin = 25*train.get_layout().get_catalogue().get_scale();
74 if(!blocked && rsv_dist<brake_dist+margin)
77 next_model->set_control("speed", 0);
79 else if(blocked && rsv_dist>brake_dist+margin*3)
82 next_model->set_control("speed", target_speed.value);
87 if(!target_speed.value && !next_model->get_speed() && train.is_active())
88 train.set_active(false);
91 void AIControl::control_changed(const string &n, float v)
94 signal_control_changed.emit(n, v);
97 void AIControl::arrived()
99 set_control("speed", 0);
102 } // namespace Marklin