3 This file is part of R²C²
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #include <msp/strings/formatter.h>
9 #include <msp/time/units.h>
11 #include "catalogue.h"
14 #include "timetable.h"
22 Timetable::Timetable(Train &t):
30 train.signal_arrived.connect(sigc::mem_fun(this, &Timetable::train_arrived));
31 train.get_layout().get_driver().signal_sensor.connect(sigc::mem_fun(this, &Timetable::sensor_event));
34 void Timetable::set_enabled(bool e)
39 void Timetable::reset()
42 wait_timeout = Time::TimeStamp();
47 void Timetable::clear()
53 void Timetable::append(const Row &row)
58 void Timetable::insert(unsigned i, const Row &row)
61 throw InvalidParameterValue("Insert position out of range");
63 rows.insert(rows.begin()+i, row);
68 const Timetable::Row &Timetable::get_row(unsigned i) const
71 throw InvalidParameterValue("Row index out of range");
75 void Timetable::tick(const Time::TimeStamp &t)
77 if(rows.empty() || !enabled)
80 if(wait_timeout && t>=wait_timeout)
82 wait_timeout = Time::TimeStamp();
83 current_row = (current_row+1)%rows.size();
89 Row &row = rows[current_row];
93 if(!train.go_to(get_sensor(row.get_param<unsigned>(0))))
97 if(!train.go_to(get_zone(row.get_param<string>(0))))
101 pending_block = &get_sensor(row.get_param<unsigned>(0)).get_block();
102 pending_train = &train;
106 wait_timeout = t+row.get_param<unsigned>(0)*Time::sec;
110 pending_train = &train.get_layout().get_train(row.get_param<unsigned>(0));
111 pending_block = &get_sensor(row.get_param<unsigned>(1)).get_block();
118 train.set_control("speed", row.get_param<unsigned>(0)/3.6*train.get_layout().get_catalogue().get_scale());
121 train.set_control("reverse", !train.get_control("reverse"));
124 if(!train.set_route(&train.get_layout().get_route(row.get_param<string>(0))))
130 current_row = (current_row+1)%rows.size();
134 void Timetable::save(list<DataFile::Statement> &st) const
136 for(vector<Row>::const_iterator i=rows.begin(); i!=rows.end(); ++i)
137 st.push_back(i->save());
140 Track &Timetable::get_sensor(unsigned id)
142 Block &block = train.get_layout().get_block(id|0x1000);
143 return **block.get_tracks().begin();
146 Zone &Timetable::get_zone(const string &name)
148 string::size_type space = name.rfind(' ');
149 if(space==string::npos || space==0)
150 throw InvalidParameterValue("Invalid zone name");
151 unsigned number = lexical_cast<unsigned>(name.substr(space+1));
152 return train.get_layout().get_zone(name.substr(0, space), number);
155 void Timetable::sensor_event(unsigned addr, bool state)
157 if(pending_block && pending_block->get_train()==pending_train && addr==pending_block->get_sensor_id() && state)
160 current_row = (current_row+1)%rows.size();
165 void Timetable::train_arrived()
167 Row &row = rows[current_row];
170 current_row = (current_row+1)%rows.size();
176 Timetable::Row::Row(RowType t):
181 Timetable::Row::Row(RowType t, const T &p):
188 const T &Timetable::Row::get_param(unsigned i) const
191 throw InvalidParameterValue("Parameter index out of range");
192 return params[i].value<T>();
195 string Timetable::Row::str() const
200 return "set route to sensor "+get_param<unsigned>(0);
202 return "set route to "+get_param<string>(0);
204 return "travel to "+get_param<string>(0);
206 return format("wait for %d seconds", get_param<unsigned>(0));
208 return format("wait for train %d at %s", get_param<unsigned>(0), get_param<string>(1));
210 return "travel until arrival";
212 return format("set speed %d km/h", get_param<unsigned>(0));
216 return "set route "+get_param<string>(0);
218 return "invalid row";
222 DataFile::Statement Timetable::Row::save() const
227 return DataFile::Statement("goto_sensor"), get_param<unsigned>(0);
229 return DataFile::Statement("goto_zone"), get_param<string>(0);
231 return DataFile::Statement("travel"), get_param<string>(0);
233 return DataFile::Statement("wait"), get_param<unsigned>(0);
235 return DataFile::Statement("wait_train"), get_param<unsigned>(0), get_param<unsigned>(1);
237 return DataFile::Statement("arrive");
239 return DataFile::Statement("speed"), get_param<unsigned>(0);
241 return DataFile::Statement("reverse");
243 return DataFile::Statement("route"), get_param<string>(0);
245 return DataFile::Statement();
249 Timetable::Row Timetable::Row::parse(const string &s)
251 if(!s.compare(0, 7, "travel "))
253 if(!s.compare(7, 10, "to sensor "))
254 return Row(TRAVEL, lexical_cast<unsigned>(s.substr(17)));
255 else if(!s.compare(7, string::npos, "until arrival"))
258 else if(!s.compare(0, 9, "wait for "))
262 unsigned nondigit = 10;
263 while(nondigit<s.size() && isdigit(s[nondigit]))
265 return Row(WAIT_TIME, lexical_cast<unsigned>(s.substr(9, nondigit-9)));
267 else if(!s.compare(9, 6, "train "))
269 string::size_type at = s.find(" at sensor ", 15);
272 Row row(WAIT_TRAIN, lexical_cast<unsigned>(s.substr(15, at-15)));
273 row.params.push_back(lexical_cast<unsigned>(s.substr(at+11)));
278 else if(!s.compare(0, 10, "set speed "))
280 unsigned nondigit = 11;
281 while(nondigit<s.size() && (isdigit(s[nondigit]) || s[nondigit]=='-'))
283 return Row(SPEED, lexical_cast<unsigned>(s.substr(10, nondigit-10)));
285 else if(s=="reverse")
287 else if(!s.compare(0, 10, "set route "))
289 if(!s.compare(10, 3, "to "))
291 if(!s.compare(13, 7, "sensor "))
292 return Row(GOTO_SENSOR, lexical_cast<unsigned>(s.substr(20)));
294 return Row(GOTO_ZONE, s.substr(13));
296 return Row(ROUTE, s.substr(10));
299 throw InvalidParameterValue("Invalid row");
303 Timetable::Loader::Loader(Timetable &tt):
304 DataFile::ObjectLoader<Timetable>(tt)
306 add("arrive", &Loader::arrive);
307 add("goto_sensor", static_cast<void (Loader::*)(unsigned)>(&Loader::goto_sensor));
308 add("goto_zone", &Loader::goto_zone);
309 add("route", &Loader::route);
310 add("speed", &Loader::speed);
311 add("reverse", &Loader::reverse);
312 add("travel", &Loader::travel);
313 add("wait", &Loader::wait);
314 add("wait_train", &Loader::wait_train);
317 add("goto", static_cast<void (Loader::*)(const string &)>(&Loader::goto_sensor));
320 void Timetable::Loader::arrive()
322 obj.rows.push_back(Row(ARRIVE));
325 void Timetable::Loader::goto_sensor(unsigned s)
327 obj.rows.push_back(Row(GOTO_SENSOR, s));
330 void Timetable::Loader::goto_sensor(const string &s)
332 if(!s.compare(0, 7, "sensor "))
333 obj.rows.push_back(Row(GOTO_SENSOR, lexical_cast<unsigned>(s.substr(7))));
336 void Timetable::Loader::goto_zone(const string &z)
338 obj.rows.push_back(Row(GOTO_ZONE, z));
341 void Timetable::Loader::route(const string &r)
343 obj.rows.push_back(Row(ROUTE, r));
346 void Timetable::Loader::reverse()
348 obj.rows.push_back(Row(REVERSE));
351 void Timetable::Loader::speed(unsigned s)
353 obj.rows.push_back(Row(SPEED, s));
356 void Timetable::Loader::travel(const string &t)
358 obj.rows.push_back(Row(TRAVEL, t));
361 void Timetable::Loader::wait(unsigned t)
363 obj.rows.push_back(Row(WAIT_TIME, t));
366 void Timetable::Loader::wait_train(unsigned t, unsigned s)
368 Row row(WAIT_TRAIN, t);
369 row.params.push_back(s);
370 obj.rows.push_back(row);