5 #include "signaltype.h"
13 #include <msp/io/print.h>
17 Signal::Signal(Layout &l, const SignalType &t):
25 check_train_direction(false),
26 check_allocated_blocks(false),
29 layout.add_signal(*this);
31 layout.signal_block_reserved.connect(sigc::mem_fun(this, &Signal::block_reserved));
36 layout.remove_signal(*this);
39 void Signal::set_address(unsigned a)
43 if(layout.has_driver() && address)
44 layout.get_driver().add_signal(address, type);
47 void Signal::set_position(const Vector &p)
49 const set<Track *> &tracks = layout.get_tracks();
51 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
52 if(!(*i)->get_type().is_turnout())
54 TrackPoint n = (*i)->get_nearest_point(p);
55 float d = distance(p, n.pos);
68 void Signal::normalize_location()
70 block = &track->get_block();
72 unsigned n_endpoints = track->get_type().get_endpoints().size();
73 for(unsigned j=0; j<n_endpoints; ++j)
75 float a = track->get_endpoint_direction(j)-direction;
82 BlockIter biter = TrackIter(track, j).block_iter();
83 entry = biter.entry();
88 void Signal::set_direction(float d)
90 float a = direction-d;
102 normalize_location();
105 void Signal::tick(const Time::TimeDelta &)
107 if(check_train_direction)
109 int train_entry = train->get_entry_to_block(*block);
110 if(train_entry>=0 && static_cast<unsigned>(train_entry)==entry)
112 IO::print("Train passing in the proper direction\n");
114 train_conn.disconnect();
115 train_conn = train->signal_advanced.connect(sigc::mem_fun(this, &Signal::train_advanced));
118 IO::print("Train passing in the wrong direction (entry=%d, train_entry=%d)\n", entry, train_entry);
119 check_train_direction = false;
120 check_allocated_blocks = true;
123 if(check_allocated_blocks)
125 unsigned n_blocks = 0;
126 BlockIter iter(block, entry);
128 while(iter && iter->get_train()==train)
130 if(iter->get_sensor_id())
134 IO::print("%d sensor blocks allocated\n", n_blocks);
135 check_allocated_blocks = false;
137 const list<SignalType::Indication> &indications = type.get_indications();
138 unsigned aspect = indications.back().aspect;
139 for(list<SignalType::Indication>::const_iterator i=indications.begin(); i!=indications.end(); ++i)
140 if(n_blocks>=i->free_blocks)
146 IO::print("Setting signal %d to aspect %d\n", address, aspect);
147 layout.get_driver().set_signal(address, aspect);
151 void Signal::block_reserved(const Block &b, Train *t)
159 check_train_direction = true;
163 layout.get_driver().set_signal(address, type.get_indications().back().aspect);
167 else if(train && t==train)
168 check_allocated_blocks = true;
171 void Signal::train_advanced(Block &b)
175 IO::print("Train is passing the signal now\n");
178 else if(passing && b.get_sensor_id())
180 IO::print("Train has passed the signal\n");
181 layout.get_driver().set_signal(address, type.get_indications().back().aspect);
190 train_conn.disconnect();
191 check_train_direction = false;
192 check_allocated_blocks = false;
195 void Signal::save(list<DataFile::Statement> &st) const
197 st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
198 st.push_back((DataFile::Statement("direction"), direction));
200 st.push_back((DataFile::Statement("address"), address));
204 Signal::Loader::Loader(Signal &s):
205 DataFile::ObjectLoader<Signal>(s)
207 add("address", &Loader::address);
208 add("direction", &Loader::direction);
209 add("position", &Loader::position);
212 void Signal::Loader::address(unsigned a)
217 void Signal::Loader::direction(float d)
219 obj.set_direction(d);
222 void Signal::Loader::position(float x, float y, float z)
224 obj.set_position(Vector(x, y, z));