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