]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/signal.cpp
Make use of the mspmath library
[r2c2.git] / source / libr2c2 / signal.cpp
1 #include "blockiter.h"
2 #include "driver.h"
3 #include "layout.h"
4 #include "signal.h"
5 #include "signaltype.h"
6 #include "trackiter.h"
7 #include "tracktype.h"
8 #include "train.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 namespace R2C2 {
14
15 Signal::Signal(Layout &l, const SignalType &t):
16         Object(l),
17         type(t),
18         address(0),
19         track(0),
20         block(0),
21         entry(0),
22         train(0),
23         check_allocated_blocks(false),
24         passing(false)
25 {
26         layout.add_signal(*this);
27
28         layout.signal_block_reserved.connect(sigc::mem_fun(this, &Signal::block_reserved));
29 }
30
31 Signal::~Signal()
32 {
33         layout.remove_signal(*this);
34 }
35
36 Signal *Signal::clone(Layout *to_layout) const
37 {
38         Signal *sig = new Signal((to_layout ? *to_layout : layout), type);
39         sig->set_position(position);
40         sig->set_rotation(rotation);
41         return sig;
42 }
43
44 void Signal::set_address(unsigned a)
45 {
46         address = a;
47         
48         if(layout.has_driver() && address)
49                 layout.get_driver().add_signal(address, type);
50 }
51
52 void Signal::set_position(const Vector &p)
53 {
54         const set<Track *> &tracks = layout.get_tracks();
55         float dist = -1;
56         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
57                 if(!(*i)->get_type().is_turnout())
58                 {
59                         Snap sn;
60                         sn.position = p;
61                         sn.rotation = rotation;
62                         (*i)->snap(sn, 1000, SNAP_SEGMENT);
63                         float d = distance(p, sn.position);
64                         if(d<dist || dist<0)
65                         {
66                                 position = sn.position;
67                                 rotation = sn.rotation;
68                                 track = *i;
69                                 dist = d;
70                         }
71                 }
72
73         normalize_location();
74 }
75
76 void Signal::normalize_location()
77 {
78         block = &track->get_block();
79
80         unsigned n_endpoints = track->get_type().get_endpoints().size();
81         for(unsigned j=0; j<n_endpoints; ++j)
82         {
83                 Angle a = wrap_with_base(track->get_snap_node(j).rotation-rotation, -Angle::quarter_turn());
84                 if(a>=Angle::quarter_turn())
85                 {
86                         BlockIter biter = TrackIter(track, j).block_iter();
87                         entry = biter.entry();
88                 }
89         }
90 }
91
92 void Signal::set_rotation(const Angle &r)
93 {
94         Angle a = wrap_with_base(rotation-r, -Angle::quarter_turn());
95         if(a>=Angle::quarter_turn())
96                 rotation = wrap_positive(rotation+Angle::half_turn());
97
98         normalize_location();
99 }
100
101 unsigned Signal::get_n_snap_nodes() const
102 {
103         return 1;
104 }
105
106 Snap Signal::get_snap_node(unsigned i) const
107 {
108         if(i>=1)
109                 throw out_of_range("Signal::get_snap_node");
110
111         Snap sn;
112         sn.position = position;
113         sn.rotation = rotation;
114         return sn;
115 }
116
117 SnapType Signal::get_default_snap_type_to(const Object &other) const
118 {
119         if(dynamic_cast<const Track *>(&other))
120                 return SNAP_SEGMENT;
121
122         return NO_SNAP;
123 }
124
125 bool Signal::collide_ray(const Vector &start, const Vector &ray) const
126 {
127         // XXX Totally hardcoded stuff, should be replaced with a geometry system
128         Vector center = position+rotated_vector(Vector(0, -0.035, 0), rotation);
129         Vector span = center-start;
130         float x = (span.x*ray.x+span.y*ray.y)/(ray.x*ray.x+ray.y*ray.y);
131         Vector nearest = start+ray*x-center;
132         if(nearest.z<0 || nearest.z>0.12)
133                 return false;
134         nearest.z = 0;
135         return dot(nearest, nearest)<0.0001;
136 }
137
138 void Signal::tick(const Time::TimeDelta &)
139 {
140         if(check_allocated_blocks)
141         {
142                 unsigned n_blocks = 0;
143                 BlockIter iter(block, entry);
144                 iter = iter.next();
145                 while(iter && iter->get_train()==train)
146                 {
147                         if(iter->get_sensor_id())
148                                 ++n_blocks;
149                         iter=iter.next();
150                 }
151                 check_allocated_blocks = false;
152
153                 const list<SignalType::Indication> &indications = type.get_indications();
154                 unsigned aspect = indications.back().aspect;
155                 for(list<SignalType::Indication>::const_iterator i=indications.begin(); i!=indications.end(); ++i)
156                         if(n_blocks>=i->free_blocks)
157                         {
158                                 aspect = i->aspect;
159                                 break;
160                         }
161
162                 layout.get_driver().set_signal(address, aspect);
163         }
164 }
165
166 void Signal::block_reserved(const Block &b, Train *t)
167 {
168         if(&b==block)
169         {
170                 if(t)
171                 {
172                         int train_entry = t->get_entry_to_block(*block);
173                         if(train_entry>=0 && static_cast<unsigned>(train_entry)==entry)
174                         {
175                                 if(train_conn)
176                                         train_conn.disconnect();
177                                 train = t;
178                                 passing = false;
179                                 train_conn = train->signal_advanced.connect(sigc::mem_fun(this, &Signal::train_advanced));
180                                 check_allocated_blocks = true;
181                         }
182                 }
183                 else
184                 {
185                         layout.get_driver().set_signal(address, type.get_indications().back().aspect);
186                         reset();
187                 }
188         }
189         else if(train && t==train)
190                 check_allocated_blocks = true;
191 }
192
193 void Signal::train_advanced(Block &b)
194 {
195         if(&b==block)
196                 passing = true;
197         else if(passing && b.get_sensor_id())
198         {
199                 layout.get_driver().set_signal(address, type.get_indications().back().aspect);
200                 reset();
201         }
202 }
203
204 void Signal::reset()
205 {
206         train = 0;
207         if(train_conn)
208                 train_conn.disconnect();
209         check_allocated_blocks = false;
210 }
211
212 void Signal::save(list<DataFile::Statement> &st) const
213 {
214         st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
215         st.push_back((DataFile::Statement("rotation"), rotation.radians()));
216         if(address)
217                 st.push_back((DataFile::Statement("address"), address));
218 }
219
220
221 Signal::Loader::Loader(Signal &s):
222         DataFile::ObjectLoader<Signal>(s)
223 {
224         add("address",  &Loader::address);
225         add("position", &Loader::position);
226         add("rotation", &Loader::rotation);
227 }
228
229 void Signal::Loader::address(unsigned a)
230 {
231         obj.set_address(a);
232 }
233
234 void Signal::Loader::position(float x, float y, float z)
235 {
236         obj.set_position(Vector(x, y, z));
237 }
238
239 void Signal::Loader::rotation(float d)
240 {
241         obj.set_rotation(Angle::from_radians(d));
242 }
243
244 } // namespace R2C2