]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/aicontrol.cpp
Make AIControl states clearer
[r2c2.git] / source / libr2c2 / aicontrol.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include "aicontrol.h"
9 #include "catalogue.h"
10 #include "layout.h"
11 #include "train.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 namespace R2C2 {
17
18 AIControl::AIControl(Train &t, Controller *n):
19         train(t),
20         next_ctrl(n),
21         target_speed(Control::continuous("speed", 0, 1000)),
22         state(NORMAL)
23 {
24         target_speed.set(0);
25
26         train.signal_arrived.connect(sigc::mem_fun(this, &AIControl::arrived));
27         next_ctrl->signal_control_changed.connect(sigc::mem_fun(this, &AIControl::control_changed));
28 }
29
30 AIControl::~AIControl()
31 {
32         delete next_ctrl;
33 }
34
35 void AIControl::set_control(const string &n, float v)
36 {
37         if(n=="speed")
38         {
39                 if(v && !train.is_active())
40                         train.set_active(true);
41
42                 target_speed.set(v);
43                 if(state!=BLOCKED)
44                 {
45                         float approach_speed = 5*train.get_layout().get_catalogue().get_scale();
46                         if(state==APPROACH && target_speed.value>approach_speed)
47                                 next_ctrl->set_control("speed", approach_speed);
48                         else
49                                 next_ctrl->set_control("speed", target_speed.value);
50                 }
51
52                 signal_control_changed.emit(target_speed);
53         }
54         else
55                 next_ctrl->set_control(n, v);
56 }
57
58 const Controller::Control &AIControl::get_control(const string &n) const
59 {
60         if(n=="speed")
61                 return target_speed;
62         else
63                 return next_ctrl->get_control(n);
64 }
65
66 float AIControl::get_speed() const
67 {
68         return next_ctrl->get_speed();
69 }
70
71 bool AIControl::get_reverse() const
72 {
73         return next_ctrl->get_reverse();
74 }
75
76 float AIControl::get_braking_distance() const
77 {
78         return next_ctrl->get_braking_distance();
79 }
80
81 void AIControl::tick(const Time::TimeDelta &dt)
82 {
83         float scale = train.get_layout().get_catalogue().get_scale();
84         float rsv_dist = train.get_reserved_distance();
85         float brake_dist = next_ctrl->get_braking_distance();
86         float approach_margin = 50*scale;
87         float approach_speed = 5*scale;
88         float margin = 10*scale;
89
90         State old_state = state;
91
92         if(state==FOLLOW && !train.get_preceding_train())
93                 state = NORMAL;
94
95         if(rsv_dist<brake_dist+margin)
96                 state = BLOCKED;
97         else if(rsv_dist>brake_dist+margin*2 && rsv_dist<brake_dist*1.3+approach_margin)
98                 state = APPROACH;
99         else if(rsv_dist>brake_dist*1.3+approach_margin*2)
100                 state = NORMAL;
101
102         if(state==NORMAL && train.get_preceding_train())
103                 state = FOLLOW;
104
105         if(state!=old_state || state==FOLLOW)
106         {
107                 float speed_limit = -1;
108                 if(state==BLOCKED)
109                         speed_limit = 0;
110                 else if(state==APPROACH)
111                         speed_limit = approach_speed;
112                 else if(state==FOLLOW)
113                         speed_limit = train.get_preceding_train()->get_speed();
114
115                 if(speed_limit>=0 && target_speed.value>speed_limit)
116                         next_ctrl->set_control("speed", speed_limit);
117                 else
118                         next_ctrl->set_control("speed", target_speed.value);
119         }
120
121         next_ctrl->tick(dt);
122
123         if(!target_speed.value && !next_ctrl->get_speed() && train.is_active())
124                 train.set_active(false);
125 }
126
127 void AIControl::control_changed(const Control &ctrl)
128 {
129         if(ctrl.name!="speed")
130                 signal_control_changed.emit(ctrl);
131 }
132
133 void AIControl::arrived()
134 {
135         set_control("speed", 0);
136 }
137
138 } // namespace R2C2