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);
46 next_model->set_control(n, v);
49 const TrainControl &AIControl::get_control(const string &n) const
54 return next_model->get_control(n);
57 float AIControl::get_speed() const
59 return next_model->get_speed();
62 float AIControl::get_braking_distance() const
64 return next_model->get_braking_distance();
67 void AIControl::tick(const Time::TimeDelta &dt)
69 float rsv_dist = train.get_reserved_distance();
70 float brake_dist = next_model->get_braking_distance()*1.15;
71 float margin = 25*train.get_layout().get_catalogue().get_scale();
72 if(!blocked && rsv_dist<brake_dist+margin)
75 next_model->set_control("speed", 0);
77 else if(blocked && rsv_dist>brake_dist+margin*3)
80 next_model->set_control("speed", target_speed.value);
85 if(!target_speed.value && !next_model->get_speed() && train.is_active())
86 train.set_active(false);
89 void AIControl::arrived()
91 set_control("speed", 0);
94 } // namespace Marklin