1 #include <msp/strings/format.h>
13 Timetable::Timetable(Train &t):
15 current_row(rows.end()),
18 train.signal_ai_event.connect(sigc::mem_fun(this, &Timetable::event));
21 void Timetable::append_row(const Row &r)
23 insert_row(rows.size(), r);
26 void Timetable::insert_row(unsigned i, const Row &r)
29 throw out_of_range("Timetable::insert_row");
31 list<Row>::iterator j = rows.begin();
33 j = rows.insert(j, r);
34 signal_row_added.emit(i, *j);
39 void Timetable::modify_row(unsigned i, const Row &r)
42 throw out_of_range("Timetable::remove_row");
44 list<Row>::iterator j = rows.begin();
47 signal_row_modified.emit(i, r);
52 void Timetable::remove_row(unsigned i)
55 throw out_of_range("Timetable::remove_row");
57 list<Row>::iterator j = rows.begin();
65 signal_row_removed.emit(i);
68 const Timetable::Row &Timetable::get_row(unsigned i) const
71 throw out_of_range("Timetable::get_row");
73 list<Row>::const_iterator j = rows.begin();
78 void Timetable::tick(const Time::TimeDelta &dt)
80 if(update_pending && !train.get_block_allocator().is_active())
83 if(current_row->type==DEPART)
85 const Clock &clock = train.get_layout().get_clock();
87 Time::TimeDelta t = clock.get_current_time();
88 if(t<current_row->time)
91 Time::TimeDelta b = t-dt*clock.get_rate();
92 if(b<current_row->time)
94 train.ai_message(Message("set-target-speed", train.get_maximum_speed()));
100 void Timetable::save(list<DataFile::Statement> &st) const
102 for(list<Row>::const_iterator i=rows.begin(); i!=rows.end(); ++i)
104 DataFile::Statement ss("row");
110 void Timetable::check_update(list<Row>::const_iterator i)
112 for(list<Row>::const_iterator j=current_row; (j!=rows.end() && j!=i); ++j)
115 update_pending = true;
118 void Timetable::update_route()
120 update_pending = false;
124 list<Row>::iterator depart = rows.end();
125 for(list<Row>::iterator i=current_row;; )
135 else if(depart!=rows.end() && i->type==ARRIVE)
141 current_row = rows.end();
146 train.ai_message(Message("clear-route"));
148 current_row = depart;
149 for(list<Row>::const_iterator i=depart; i!=rows.end(); ++i)
153 train.ai_message(Message("set-destination-zone", i->zone));
156 else if(i->type==DEPART)
158 const Clock &clock = train.get_layout().get_clock();
159 Time::TimeDelta dt = i->time-clock.get_current_time();
162 dt /= clock.get_rate();
163 train.ai_message(Message("set-departure-delay", dt));
168 void Timetable::event(TrainAI &, const Message &msg)
170 if(msg.type=="arrived")
172 if(current_row->type==ARRIVE)
174 current_row->time = train.get_layout().get_clock().get_current_time();
175 unsigned i = distance(rows.begin(), current_row);
176 signal_row_modified.emit(i, *current_row);
178 update_pending = true;
183 Timetable::Row::Row():
188 void Timetable::Row::save(list<DataFile::Statement> &st) const
190 st.push_back((DataFile::Statement("type"), type));
191 st.push_back((DataFile::Statement("time"), time.raw()));
192 st.push_back((DataFile::Statement("zone"), zone->get_group(), zone->get_number()));
196 Timetable::Loader::Loader(Timetable &t, Layout &l):
197 DataFile::ObjectLoader<Timetable>(t),
200 add("row", &Loader::row);
203 void Timetable::Loader::row()
207 obj.rows.push_back(r);
208 obj.update_pending = true;
212 Timetable::Row::Loader::Loader(Row &r, Layout &l):
213 DataFile::ObjectLoader<Timetable::Row>(r),
216 add("zone", &Loader::zone);
217 add("time", &Loader::time);
218 add("type", &Row::type);
221 void Timetable::Row::Loader::zone(const string &name, unsigned number)
223 obj.zone = &layout.get_zone(name, number);
226 void Timetable::Row::Loader::time(Time::RawTime t)
228 obj.time = Time::TimeDelta(t);
232 void operator<<(LexicalConverter &conv, Timetable::RowType rt)
236 case Timetable::ARRIVE: conv.result("ARRIVE"); return;
237 case Timetable::DEPART: conv.result("DEPART"); return;
238 default: throw lexical_error(format("conversion of RowType(%d) to string", rt));
242 void operator>>(const LexicalConverter &conv, Timetable::RowType &rt)
244 if(conv.get()=="ARRIVE")
245 rt = Timetable::ARRIVE;
246 else if(conv.get()=="DEPART")
247 rt = Timetable::DEPART;
249 throw lexical_error(format("conversion of '%s' to RowType", conv.get()));