]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/timetable.cpp
0d68a7be8aea5c88f515a60192e4aac62d6267ce
[r2c2.git] / source / libr2c2 / timetable.cpp
1 #include <algorithm>
2 #include <msp/strings/format.h>
3 #include "aicontrol.h"
4 #include "clock.h"
5 #include "layout.h"
6 #include "timetable.h"
7 #include "train.h"
8 #include "trainrouter.h"
9 #include "zone.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 namespace R2C2 {
15
16 Timetable::Timetable(Train &t):
17         TrainAI(t),
18         current_row(rows.end()),
19         update_pending(false),
20         sync_to_clock(true)
21 {
22         if(!train.get_ai_of_type<AIControl>())
23                 new AIControl(train);
24         if(!train.get_ai_of_type<TrainRouter>())
25                 new TrainRouter(train);
26
27         train.signal_ai_event.connect(sigc::mem_fun(this, &Timetable::event));
28 }
29
30 void Timetable::append_row(const Row &r)
31 {
32         insert_row(rows.size(), r);
33 }
34
35 void Timetable::insert_row(unsigned i, const Row &r)
36 {
37         if(i>rows.size())
38                 throw out_of_range("Timetable::insert_row");
39
40         list<Row>::iterator j = rows.begin();
41         advance(j, i);
42         j = rows.insert(j, r);
43         signal_row_added.emit(i, *j);
44
45         check_update(j);
46 }
47
48 void Timetable::modify_row(unsigned i, const Row &r)
49 {
50         if(i>=rows.size())
51                 throw out_of_range("Timetable::remove_row");
52
53         list<Row>::iterator j = rows.begin();
54         advance(j, i);
55         *j = r;
56         signal_row_modified.emit(i, r);
57
58         check_update(j);
59 }
60
61 void Timetable::remove_row(unsigned i)
62 {
63         if(i>=rows.size())
64                 throw out_of_range("Timetable::remove_row");
65
66         list<Row>::iterator j = rows.begin();
67         advance(j, i);
68
69         check_update(j);
70         if(j==current_row)
71                 --current_row;
72
73         rows.erase(j);
74         signal_row_removed.emit(i);
75 }
76
77 const Timetable::Row &Timetable::get_row(unsigned i) const
78 {
79         if(i>=rows.size())
80                 throw out_of_range("Timetable::get_row");
81
82         list<Row>::const_iterator j = rows.begin();
83         advance(j, i);
84         return *j;
85 }
86
87 void Timetable::tick(const Time::TimeDelta &dt)
88 {
89         if(update_pending && !train.get_block_allocator().is_active())
90                 update_route();
91
92         if(current_row->type==DEPART)
93         {
94                 const Clock &clock = train.get_layout().get_clock();
95
96                 Time::TimeDelta t = clock.get_current_time();
97                 if(t<current_row->time)
98                         t += Time::day;
99
100                 Time::TimeDelta b = t-dt*clock.get_rate();
101                 if(b<current_row->time)
102                 {
103                         train.ai_message(Message("set-target-speed", train.get_maximum_speed()));
104                         ++current_row;
105                 }
106         }
107 }
108
109 void Timetable::save(list<DataFile::Statement> &st) const
110 {
111         for(list<Row>::const_iterator i=rows.begin(); i!=rows.end(); ++i)
112         {
113                 DataFile::Statement ss("row");
114                 i->save(ss.sub);
115                 st.push_back(ss);
116         }
117 }
118
119 void Timetable::check_update(const list<Row>::const_iterator &i)
120 {
121         for(list<Row>::const_iterator j=current_row; (j!=rows.end() && j!=i); ++j)
122                 if(j->type==ARRIVE)
123                         return;
124         update_pending = true;
125 }
126
127 list<Timetable::Row>::iterator Timetable::find_trip(const list<Row>::iterator &begin, list<Row>::iterator *arrive)
128 {
129         list<Row>::iterator i = find_if(begin, rows.end(), RowTypeMatch(DEPART));
130         if(i==rows.end())
131                 return i;
132
133         list<Row>::iterator j = find_if(i, rows.end(), RowTypeMatch(ARRIVE));
134         if(j==rows.end())
135                 return j;
136
137         if(arrive)
138                 *arrive = j;
139         return i;
140 }
141
142 void Timetable::update_route()
143 {
144         update_pending = false;
145         if(rows.empty())
146                 return;
147
148         const Clock &clock = train.get_layout().get_clock();
149
150         if(sync_to_clock)
151         {
152                 sync_to_clock = false;
153                 current_row = rows.begin();
154                 for(list<Row>::iterator i=rows.begin(); i!=rows.end(); ++i)
155                         if(i->type==DEPART && i->time>=clock.get_current_time())
156                         {
157                                 current_row = i;
158                                 break;
159                         }
160         }
161
162         list<Row>::iterator arrive;
163         list<Row>::iterator depart = find_trip(current_row, &arrive);
164         if(depart==rows.end())
165         {
166                 depart = find_trip(rows.begin(), &arrive);
167                 if(depart==rows.end())
168                 {
169                         current_row = rows.end();
170                         return;
171                 }
172         }
173
174         train.ai_message(Message("clear-route"));
175
176         current_row = depart;
177         for(list<Row>::const_iterator i=depart; i!=rows.end(); ++i)
178         {
179                 if(i->type==ARRIVE)
180                 {
181                         train.ai_message(Message("add-waypoint", i->target));
182                         break;
183                 }
184                 else if(i->type==DEPART)
185                 {
186                         Time::TimeDelta dt = i->time-clock.get_current_time();
187                         while(dt<Time::zero)
188                                 dt += Time::day;
189                         train.ai_message(Message("set-departure-delay", dt/clock.get_rate()));
190                 }
191                 else if(i->type==THROUGH)
192                         train.ai_message(Message("add-waypoint", i->target));
193         }
194
195         list<Row>::iterator next_depart = find_trip(arrive, 0);
196         if(next_depart==rows.end())
197                 next_depart = find_trip(rows.begin(), 0);
198         if(next_depart!=rows.end())
199         {
200                 Time::TimeDelta dt = next_depart->time-depart->time;
201                 while(dt<=Time::zero)
202                         dt += Time::day;
203                 train.ai_message(Message("set-trip-duration", dt/clock.get_rate()));
204         }
205 }
206
207 void Timetable::event(TrainAI &, const Message &msg)
208 {
209         if(msg.type=="arrived")
210         {
211                 if(current_row->type==ARRIVE)
212                         record_time();
213                 update_pending = true;
214         }
215         else if(msg.type=="waypoint-reached")
216         {
217                 const TrackChain *wp = msg.value.value<const TrackChain *>();
218                 if(current_row->type==THROUGH && current_row->target==wp)
219                 {
220                         record_time();
221                         ++current_row;
222                 }
223         }
224 }
225
226 void Timetable::record_time()
227 {
228         current_row->time = train.get_layout().get_clock().get_current_time();
229         unsigned i = distance(rows.begin(), current_row);
230         signal_row_modified.emit(i, *current_row);
231 }
232
233
234 Timetable::Row::Row():
235         type(ARRIVE),
236         target(0)
237 { }
238
239 void Timetable::Row::save(list<DataFile::Statement> &st) const
240 {
241         st.push_back((DataFile::Statement("type"), type));
242         st.push_back((DataFile::Statement("time"), time.raw()));
243         st.push_back(target->save_reference());
244 }
245
246
247 Timetable::Loader::Loader(Timetable &t, Layout &l):
248         DataFile::ObjectLoader<Timetable>(t),
249         layout(l)
250 {
251         add("row", &Loader::row);
252 }
253
254 void Timetable::Loader::row()
255 {
256         Row r;
257         load_sub(r, layout);
258         obj.rows.push_back(r);
259         obj.update_pending = true;
260 }
261
262
263 Timetable::Row::Loader::Loader(Row &r, Layout &l):
264         DataFile::ObjectLoader<Timetable::Row>(r),
265         layout(l)
266 {
267         add("block", &Loader::block);
268         add("time", &Loader::time);
269         add("type", &Row::type);
270         add("zone", &Loader::zone);
271 }
272
273 void Timetable::Row::Loader::block(unsigned id)
274 {
275         obj.target = &layout.get_block(id);
276 }
277
278 void Timetable::Row::Loader::time(Time::RawTime t)
279 {
280         obj.time = Time::TimeDelta(t);
281 }
282
283 void Timetable::Row::Loader::zone(const string &name, unsigned number)
284 {
285         obj.target = &layout.get_zone(name, number);
286 }
287
288
289 void operator<<(LexicalConverter &conv, Timetable::RowType rt)
290 {
291         switch(rt)
292         {
293         case Timetable::ARRIVE: conv.result("ARRIVE"); return;
294         case Timetable::DEPART: conv.result("DEPART"); return;
295         case Timetable::THROUGH: conv.result("THROUGH"); return;
296         default: throw lexical_error(format("conversion of RowType(%d) to string", rt));
297         }
298 }
299
300 void operator>>(const LexicalConverter &conv, Timetable::RowType &rt)
301 {
302         if(conv.get()=="ARRIVE")
303                 rt = Timetable::ARRIVE;
304         else if(conv.get()=="DEPART")
305                 rt = Timetable::DEPART;
306         else if(conv.get()=="THROUGH")
307                 rt = Timetable::THROUGH;
308         else
309                 throw lexical_error(format("conversion of '%s' to RowType", conv.get()));
310 }
311
312 } // namespace R2C2