5 #include "signaltype.h"
15 Signal::Signal(Layout &l, const SignalType &t):
23 check_train_direction(false),
24 check_allocated_blocks(false),
27 layout.add_signal(*this);
29 layout.signal_block_reserved.connect(sigc::mem_fun(this, &Signal::block_reserved));
34 layout.remove_signal(*this);
37 void Signal::set_address(unsigned a)
41 if(layout.has_driver() && address)
42 layout.get_driver().add_signal(address, type);
45 void Signal::set_position(const Vector &p)
47 const set<Track *> &tracks = layout.get_tracks();
49 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
50 if(!(*i)->get_type().is_turnout())
52 TrackPoint n = (*i)->get_nearest_point(p);
53 float d = distance(p, n.pos);
66 void Signal::normalize_location()
68 block = &track->get_block();
70 unsigned n_endpoints = track->get_type().get_endpoints().size();
71 for(unsigned j=0; j<n_endpoints; ++j)
73 float a = track->get_endpoint_direction(j)-direction;
80 BlockIter biter = TrackIter(track, j).block_iter();
81 entry = biter.entry();
86 void Signal::set_direction(float d)
88 float a = direction-d;
100 normalize_location();
103 void Signal::tick(const Time::TimeDelta &)
105 if(check_train_direction)
107 int train_entry = train->get_entry_to_block(*block);
108 if(train_entry>=0 && static_cast<unsigned>(train_entry)==entry)
111 train_conn.disconnect();
112 train_conn = train->signal_advanced.connect(sigc::mem_fun(this, &Signal::train_advanced));
114 check_train_direction = false;
115 check_allocated_blocks = true;
118 if(check_allocated_blocks)
120 unsigned n_blocks = 0;
121 BlockIter iter(block, entry);
123 while(iter && iter->get_train()==train)
125 if(iter->get_sensor_id())
129 check_allocated_blocks = false;
131 const list<SignalType::Indication> &indications = type.get_indications();
132 unsigned aspect = indications.back().aspect;
133 for(list<SignalType::Indication>::const_iterator i=indications.begin(); i!=indications.end(); ++i)
134 if(n_blocks>=i->free_blocks)
140 layout.get_driver().set_signal(address, aspect);
144 void Signal::block_reserved(const Block &b, Train *t)
152 check_train_direction = true;
156 layout.get_driver().set_signal(address, type.get_indications().back().aspect);
160 else if(train && t==train)
161 check_allocated_blocks = true;
164 void Signal::train_advanced(Block &b)
168 else if(passing && b.get_sensor_id())
170 layout.get_driver().set_signal(address, type.get_indications().back().aspect);
179 train_conn.disconnect();
180 check_train_direction = false;
181 check_allocated_blocks = false;
184 void Signal::save(list<DataFile::Statement> &st) const
186 st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
187 st.push_back((DataFile::Statement("direction"), direction));
189 st.push_back((DataFile::Statement("address"), address));
193 Signal::Loader::Loader(Signal &s):
194 DataFile::ObjectLoader<Signal>(s)
196 add("address", &Loader::address);
197 add("direction", &Loader::direction);
198 add("position", &Loader::position);
201 void Signal::Loader::address(unsigned a)
206 void Signal::Loader::direction(float d)
208 obj.set_direction(d);
211 void Signal::Loader::position(float x, float y, float z)
213 obj.set_position(Vector(x, y, z));