2 #include <msp/strings/format.h>
8 #include "trainrouter.h"
16 Timetable::Timetable(Train &t):
18 current_row(rows.end()),
21 if(!train.get_ai_of_type<AIControl>())
23 if(!train.get_ai_of_type<TrainRouter>())
24 new TrainRouter(train);
26 train.signal_ai_event.connect(sigc::mem_fun(this, &Timetable::event));
29 void Timetable::append_row(const Row &r)
31 insert_row(rows.size(), r);
34 void Timetable::insert_row(unsigned i, const Row &r)
37 throw out_of_range("Timetable::insert_row");
39 list<Row>::iterator j = rows.begin();
41 j = rows.insert(j, r);
42 signal_row_added.emit(i, *j);
47 void Timetable::modify_row(unsigned i, const Row &r)
50 throw out_of_range("Timetable::remove_row");
52 list<Row>::iterator j = rows.begin();
55 signal_row_modified.emit(i, r);
60 void Timetable::remove_row(unsigned i)
63 throw out_of_range("Timetable::remove_row");
65 list<Row>::iterator j = rows.begin();
73 signal_row_removed.emit(i);
76 const Timetable::Row &Timetable::get_row(unsigned i) const
79 throw out_of_range("Timetable::get_row");
81 list<Row>::const_iterator j = rows.begin();
86 void Timetable::tick(const Time::TimeDelta &dt)
88 if(update_pending && !train.get_block_allocator().is_active())
91 if(current_row->type==DEPART)
93 const Clock &clock = train.get_layout().get_clock();
95 Time::TimeDelta t = clock.get_current_time();
96 if(t<current_row->time)
99 Time::TimeDelta b = t-dt*clock.get_rate();
100 if(b<current_row->time)
102 train.ai_message(Message("set-target-speed", train.get_maximum_speed()));
108 void Timetable::save(list<DataFile::Statement> &st) const
110 for(list<Row>::const_iterator i=rows.begin(); i!=rows.end(); ++i)
112 DataFile::Statement ss("row");
118 void Timetable::check_update(list<Row>::const_iterator i)
120 for(list<Row>::const_iterator j=current_row; (j!=rows.end() && j!=i); ++j)
123 update_pending = true;
126 list<Timetable::Row>::iterator Timetable::find_trip(const list<Row>::iterator &begin, list<Row>::iterator *arrive)
128 list<Row>::iterator i = find_if(begin, rows.end(), RowTypeMatch(DEPART));
132 list<Row>::iterator j = find_if(i, rows.end(), RowTypeMatch(ARRIVE));
141 void Timetable::update_route()
143 update_pending = false;
147 list<Row>::iterator arrive;
148 list<Row>::iterator depart = find_trip(current_row, &arrive);
149 if(depart==rows.end())
151 depart = find_trip(rows.begin(), &arrive);
152 if(depart==rows.end())
154 current_row = rows.end();
159 train.ai_message(Message("clear-route"));
161 const Clock &clock = train.get_layout().get_clock();
163 current_row = depart;
164 for(list<Row>::const_iterator i=depart; i!=rows.end(); ++i)
168 train.ai_message(Message("set-destination", i->target));
171 else if(i->type==DEPART)
173 Time::TimeDelta dt = i->time-clock.get_current_time();
176 dt /= clock.get_rate();
177 train.ai_message(Message("set-departure-delay", dt));
179 else if(i->type==THROUGH)
180 train.ai_message(Message("add-waypoint", i->target));
183 list<Row>::iterator next_depart = find_trip(arrive, 0);
184 if(next_depart==rows.end())
185 next_depart = find_trip(rows.begin(), 0);
186 if(next_depart!=rows.end())
188 Time::TimeDelta dt = next_depart->time-depart->time;
189 while(dt<=Time::zero)
191 train.ai_message(Message("set-trip-duration", dt/clock.get_rate()));
195 void Timetable::event(TrainAI &, const Message &msg)
197 if(msg.type=="arrived")
199 if(current_row->type==ARRIVE)
201 update_pending = true;
203 else if(msg.type=="waypoint-reached")
205 const TrackChain *wp = msg.value.value<const TrackChain *>();
206 if(current_row->type==THROUGH && current_row->target==wp)
214 void Timetable::record_time()
216 current_row->time = train.get_layout().get_clock().get_current_time();
217 unsigned i = distance(rows.begin(), current_row);
218 signal_row_modified.emit(i, *current_row);
222 Timetable::Row::Row():
227 void Timetable::Row::save(list<DataFile::Statement> &st) const
229 st.push_back((DataFile::Statement("type"), type));
230 st.push_back((DataFile::Statement("time"), time.raw()));
231 st.push_back(target->save_reference());
235 Timetable::Loader::Loader(Timetable &t, Layout &l):
236 DataFile::ObjectLoader<Timetable>(t),
239 add("row", &Loader::row);
242 void Timetable::Loader::row()
246 obj.rows.push_back(r);
247 obj.update_pending = true;
251 Timetable::Row::Loader::Loader(Row &r, Layout &l):
252 DataFile::ObjectLoader<Timetable::Row>(r),
255 add("block", &Loader::block);
256 add("time", &Loader::time);
257 add("type", &Row::type);
258 add("zone", &Loader::zone);
261 void Timetable::Row::Loader::block(unsigned id)
263 obj.target = &layout.get_block(id);
266 void Timetable::Row::Loader::time(Time::RawTime t)
268 obj.time = Time::TimeDelta(t);
271 void Timetable::Row::Loader::zone(const string &name, unsigned number)
273 obj.target = &layout.get_zone(name, number);
277 void operator<<(LexicalConverter &conv, Timetable::RowType rt)
281 case Timetable::ARRIVE: conv.result("ARRIVE"); return;
282 case Timetable::DEPART: conv.result("DEPART"); return;
283 case Timetable::THROUGH: conv.result("THROUGH"); return;
284 default: throw lexical_error(format("conversion of RowType(%d) to string", rt));
288 void operator>>(const LexicalConverter &conv, Timetable::RowType &rt)
290 if(conv.get()=="ARRIVE")
291 rt = Timetable::ARRIVE;
292 else if(conv.get()=="DEPART")
293 rt = Timetable::DEPART;
294 else if(conv.get()=="THROUGH")
295 rt = Timetable::THROUGH;
297 throw lexical_error(format("conversion of '%s' to RowType", conv.get()));