]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/signal.cpp
Add a shortcut for getting the gauge of a TrackType
[r2c2.git] / source / libr2c2 / signal.cpp
1 #include "blockiter.h"
2 #include "catalogue.h"
3 #include "driver.h"
4 #include "layout.h"
5 #include "signal.h"
6 #include "signaltype.h"
7 #include "trackiter.h"
8 #include "tracktype.h"
9 #include "train.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 namespace R2C2 {
15
16 Signal::Signal(Layout &l, const SignalType &t):
17         TrackAttachment(l),
18         type(t),
19         address(0),
20         train(0),
21         check_allocated_blocks(false),
22         passing(false)
23 {
24         layout.add(*this);
25
26         layout.signal_block_reserved.connect(sigc::mem_fun(this, &Signal::block_reserved));
27 }
28
29 Signal::~Signal()
30 {
31         if(layout.has_driver() && address)
32                 layout.get_driver().remove_sensor(address);
33         layout.remove(*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         Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0);
47
48         if(driver && address)
49                 driver->remove_signal(address);
50         address = a;
51         if(driver && address)
52                 driver->add_signal(address, type);
53 }
54
55 void Signal::set_position(const Vector &p)
56 {
57         position = p;
58
59         update_attachment();
60         signal_moved.emit();
61 }
62
63 void Signal::set_rotation(const Angle &r)
64 {
65         rotation = r;
66
67         update_attachment();
68         signal_moved.emit();
69 }
70
71 void Signal::update_attachment()
72 {
73         attach_to_closest(2);
74
75         if(track)
76                 block = track.block_iter();
77         else
78                 block = BlockIter();
79 }
80
81 unsigned Signal::get_n_snap_nodes() const
82 {
83         return 1;
84 }
85
86 Snap Signal::get_snap_node(unsigned i) const
87 {
88         if(i>=1)
89                 throw out_of_range("Signal::get_snap_node");
90
91         Snap sn;
92         sn.position = position;
93         sn.rotation = rotation;
94         return sn;
95 }
96
97 SnapType Signal::get_default_snap_type_to(const Object &other) const
98 {
99         if(dynamic_cast<const Track *>(&other))
100                 return SNAP_SEGMENT;
101
102         return NO_SNAP;
103 }
104
105 void Signal::tick(const Time::TimeDelta &)
106 {
107         if(check_allocated_blocks)
108         {
109                 unsigned n_blocks = 0;
110                 BlockIter iter = block.next();
111                 while(iter && iter->get_train()==train)
112                 {
113                         if(iter->get_sensor_address())
114                                 ++n_blocks;
115                         iter=iter.next();
116                 }
117                 check_allocated_blocks = false;
118
119                 const list<SignalType::Indication> &indications = type.get_indications();
120                 unsigned aspect = indications.back().aspect;
121                 for(list<SignalType::Indication>::const_iterator i=indications.begin(); i!=indications.end(); ++i)
122                         if(n_blocks>=i->free_blocks)
123                         {
124                                 aspect = i->aspect;
125                                 break;
126                         }
127
128                 layout.get_driver().set_signal(address, aspect);
129         }
130 }
131
132 void Signal::block_reserved(const Block &b, Train *t)
133 {
134         if(&b==block.block())
135         {
136                 if(t)
137                 {
138                         const BlockIter &b_iter = t->get_block_allocator().iter_for(b);
139                         if(b_iter && b_iter.entry()==block.entry())
140                         {
141                                 if(train_conn)
142                                         train_conn.disconnect();
143                                 train = t;
144                                 passing = false;
145                                 train_conn = train->signal_advanced.connect(sigc::mem_fun(this, &Signal::train_advanced));
146                                 check_allocated_blocks = true;
147                         }
148                 }
149                 else
150                 {
151                         layout.get_driver().set_signal(address, type.get_indications().back().aspect);
152                         reset();
153                 }
154         }
155         else if(train && t==train)
156                 check_allocated_blocks = true;
157 }
158
159 void Signal::train_advanced(Block &b)
160 {
161         if(&b==block.block())
162                 passing = true;
163         else if(passing && b.get_sensor_address())
164         {
165                 layout.get_driver().set_signal(address, type.get_indications().back().aspect);
166                 reset();
167         }
168 }
169
170 void Signal::reset()
171 {
172         train = 0;
173         if(train_conn)
174                 train_conn.disconnect();
175         check_allocated_blocks = false;
176 }
177
178 void Signal::save(list<DataFile::Statement> &st) const
179 {
180         st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
181         st.push_back((DataFile::Statement("rotation"), rotation.radians()));
182         if(address)
183                 st.push_back((DataFile::Statement("address"), address));
184 }
185
186
187 Signal::Loader::Loader(Signal &s):
188         DataFile::ObjectLoader<Signal>(s)
189 {
190         add("address",  &Loader::address);
191         add("position", &Loader::position);
192         add("rotation", &Loader::rotation);
193 }
194
195 void Signal::Loader::address(unsigned a)
196 {
197         obj.set_address(a);
198 }
199
200 void Signal::Loader::position(float x, float y, float z)
201 {
202         obj.set_position(Vector(x, y, z));
203 }
204
205 void Signal::Loader::rotation(float d)
206 {
207         obj.set_rotation(Angle::from_radians(d));
208 }
209
210 } // namespace R2C2