11 AIControl::AIControl(Train &t):
15 pending_reverse(false),
19 train.signal_ai_event.connect(sigc::mem_fun(this, &AIControl::event));
22 void AIControl::set_target_speed(float s)
26 signal_event.emit(Message("target-speed-changed", target_speed));
29 void AIControl::set_reverse(bool r)
32 if(train.get_controller().get_speed())
37 train.set_control("reverse", reverse);
38 signal_event.emit(Message("reverse-changed", reverse));
42 void AIControl::message(const Message &msg)
44 if(msg.type=="set-target-speed")
45 set_target_speed(msg.value.value<float>());
46 else if(msg.type=="set-reverse")
47 set_reverse(msg.value.value<bool>());
48 else if(msg.type=="toggle-reverse")
49 set_reverse(!reverse);
52 void AIControl::tick(const Time::TimeDelta &)
54 float scale = train.get_layout().get_catalogue().get_scale();
55 float rsv_dist = train.get_reserved_distance();
56 float brake_dist = train.get_controller().get_braking_distance();
57 float approach_margin = 50*scale;
58 float approach_speed = 5*scale;
59 float margin = 1*scale;
61 State old_state = state;
63 if(state==FOLLOW && !train.get_preceding_train())
66 if(rsv_dist<brake_dist+margin)
68 else if(rsv_dist>brake_dist+margin*2 && rsv_dist<brake_dist*1.3+approach_margin)
70 else if(rsv_dist>brake_dist*1.3+approach_margin*2)
73 if(state==NORMAL && train.get_preceding_train())
76 if(state!=old_state || state==FOLLOW || need_update)
78 float speed_limit = -1;
81 else if(state==APPROACH)
82 speed_limit = approach_speed;
83 else if(state==FOLLOW && train.get_preceding_train()->get_block_allocator().is_active())
84 speed_limit = train.get_preceding_train()->get_speed();
86 if(speed_limit>=0 && target_speed>speed_limit)
87 train.set_control("speed", speed_limit);
89 train.set_control("speed", target_speed);
94 if(pending_reverse!=reverse && !train.get_controller().get_speed())
96 reverse = pending_reverse;
97 train.set_control("reverse", reverse);
101 bool AIControl::has_intent_to_move() const
106 void AIControl::event(TrainAI &, const Message &ev)
108 if(ev.type=="arrived")