]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/signal.cpp
a25f8eca4697fde499b993348a32a4de7f20e752
[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 void Signal::tick(const Time::TimeDelta &)
126 {
127         if(check_allocated_blocks)
128         {
129                 unsigned n_blocks = 0;
130                 BlockIter iter(block, entry);
131                 iter = iter.next();
132                 while(iter && iter->get_train()==train)
133                 {
134                         if(iter->get_sensor_id())
135                                 ++n_blocks;
136                         iter=iter.next();
137                 }
138                 check_allocated_blocks = false;
139
140                 const list<SignalType::Indication> &indications = type.get_indications();
141                 unsigned aspect = indications.back().aspect;
142                 for(list<SignalType::Indication>::const_iterator i=indications.begin(); i!=indications.end(); ++i)
143                         if(n_blocks>=i->free_blocks)
144                         {
145                                 aspect = i->aspect;
146                                 break;
147                         }
148
149                 layout.get_driver().set_signal(address, aspect);
150         }
151 }
152
153 void Signal::block_reserved(const Block &b, Train *t)
154 {
155         if(&b==block)
156         {
157                 if(t)
158                 {
159                         int train_entry = t->get_entry_to_block(*block);
160                         if(train_entry>=0 && static_cast<unsigned>(train_entry)==entry)
161                         {
162                                 if(train_conn)
163                                         train_conn.disconnect();
164                                 train = t;
165                                 passing = false;
166                                 train_conn = train->signal_advanced.connect(sigc::mem_fun(this, &Signal::train_advanced));
167                                 check_allocated_blocks = true;
168                         }
169                 }
170                 else
171                 {
172                         layout.get_driver().set_signal(address, type.get_indications().back().aspect);
173                         reset();
174                 }
175         }
176         else if(train && t==train)
177                 check_allocated_blocks = true;
178 }
179
180 void Signal::train_advanced(Block &b)
181 {
182         if(&b==block)
183                 passing = true;
184         else if(passing && b.get_sensor_id())
185         {
186                 layout.get_driver().set_signal(address, type.get_indications().back().aspect);
187                 reset();
188         }
189 }
190
191 void Signal::reset()
192 {
193         train = 0;
194         if(train_conn)
195                 train_conn.disconnect();
196         check_allocated_blocks = false;
197 }
198
199 void Signal::save(list<DataFile::Statement> &st) const
200 {
201         st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
202         st.push_back((DataFile::Statement("rotation"), rotation.radians()));
203         if(address)
204                 st.push_back((DataFile::Statement("address"), address));
205 }
206
207
208 Signal::Loader::Loader(Signal &s):
209         DataFile::ObjectLoader<Signal>(s)
210 {
211         add("address",  &Loader::address);
212         add("position", &Loader::position);
213         add("rotation", &Loader::rotation);
214 }
215
216 void Signal::Loader::address(unsigned a)
217 {
218         obj.set_address(a);
219 }
220
221 void Signal::Loader::position(float x, float y, float z)
222 {
223         obj.set_position(Vector(x, y, z));
224 }
225
226 void Signal::Loader::rotation(float d)
227 {
228         obj.set_rotation(Angle::from_radians(d));
229 }
230
231 } // namespace R2C2