+++ /dev/null
-#include <msp/strings/format.h>
-#include <msp/time/units.h>
-#include "block.h"
-#include "catalogue.h"
-#include "driver.h"
-#include "layout.h"
-#include "timetable.h"
-#include "trackcircuit.h"
-#include "train.h"
-
-using namespace std;
-using namespace Msp;
-
-namespace R2C2 {
-
-Timetable::Timetable(Train &t):
- TrainAI(t),
- enabled(false),
- current_row(0),
- executing(true),
- pending_block(0),
- pending_train(0),
- arrived(false)
-{
- train.signal_advanced.connect(sigc::mem_fun(this, &Timetable::train_advanced));
- train.signal_ai_event.connect(sigc::mem_fun(this, &Timetable::event));
- Layout &layout = train.get_layout();
- layout.signal_sensor_state_changed.connect(sigc::mem_fun(this, &Timetable::sensor_state_changed));
- layout.signal_block_reserved.connect(sigc::mem_fun(this, &Timetable::block_reserved));
-}
-
-void Timetable::set_enabled(bool e)
-{
- enabled = e;
-}
-
-void Timetable::reset()
-{
- current_row = 0;
- wait_timeout = Time::TimeStamp();
- pending_block = 0;
- executing = true;
-}
-
-void Timetable::clear()
-{
- rows.clear();
- reset();
-}
-
-void Timetable::append(const Row &row)
-{
- rows.push_back(row);
-}
-
-void Timetable::insert(unsigned i, const Row &row)
-{
- if(i>rows.size())
- throw out_of_range("Timetable::insert");
-
- rows.insert(rows.begin()+i, row);
- if(i<=current_row)
- ++current_row;
-}
-
-const Timetable::Row &Timetable::get_row(unsigned i) const
-{
- if(i>=rows.size())
- throw out_of_range("Timetable::get_row");
- return rows[i];
-}
-
-void Timetable::tick(const Time::TimeStamp &t, const Time::TimeDelta &)
-{
- if(rows.empty() || !enabled)
- return;
-
- if(wait_timeout && t>=wait_timeout)
- {
- wait_timeout = Time::TimeStamp();
- current_row = (current_row+1)%rows.size();
- executing = true;
- }
-
- if(executing)
- {
- Row &row = rows[current_row];
- switch(row.type)
- {
- case GOTO_SENSOR:
- arrived = false;
- train.ai_message(Message("set-destination-block", &get_sensor(row.get_param<unsigned>(0))));
- break;
- case GOTO_ZONE:
- arrived = false;
- train.ai_message(Message("set-destination-zone", &get_zone(row.get_param<string>(0))));
- break;
- case TRAVEL_TO:
- {
- Block *block = &get_sensor(row.get_param<unsigned>(0));
- if(block->get_train()!=&train || block->get_sensor().get_state()<Sensor::MAYBE_ACTIVE)
- {
- pending_block = block;
- pending_train = &train;
- executing = false;
- }
- }
- break;
- case TRAVEL_PAST:
- pending_block = &get_turnout(row.get_param<unsigned>(0)).get_block();
- pending_train = (pending_block->get_train()==&train ? 0 : &train);
- executing = false;
- break;
- case WAIT_TIME:
- wait_timeout = t+row.get_param<unsigned>(0)*Time::sec;
- executing = false;
- break;
- case WAIT_UNTIL:
- {
- unsigned unixtime = t.to_unixtime();
- unsigned mod = row.get_param<unsigned>(1);
- unsigned secs = ((mod+row.get_param<unsigned>(0))-(unixtime%mod))%mod;
- wait_timeout = t+secs*Time::sec;
- executing = false;
- }
- break;
- case WAIT_TRAIN:
- {
- Train *other_train = &train.get_layout().get_train(row.get_param<unsigned>(0));
- Block *block = &get_sensor(row.get_param<unsigned>(1));
- if(block->get_train()!=other_train || block->get_sensor().get_state()<Sensor::MAYBE_ACTIVE)
- {
- pending_train = other_train;
- pending_block = block;
- executing = false;
- }
- }
- break;
- case ARRIVE:
- if(!arrived)
- executing = false;
- arrived = false;
- break;
- case SPEED:
- if(!arrived)
- {
- float speed = row.get_param<unsigned>(0)/3.6*train.get_layout().get_catalogue().get_scale();
- train.ai_message(Message("set-target-speed", speed));
- }
- break;
- case REVERSE:
- train.ai_message(Message("toggle-reverse"));
- break;
- case ROUTE:
- train.ai_message(Message("set-route", &train.get_layout().get_route(row.get_param<string>(0))));
- break;
- }
-
- if(executing)
- current_row = (current_row+1)%rows.size();
- }
-}
-
-void Timetable::save(list<DataFile::Statement> &st) const
-{
- for(vector<Row>::const_iterator i=rows.begin(); i!=rows.end(); ++i)
- st.push_back(i->save());
-}
-
-Block &Timetable::get_sensor(unsigned id)
-{
- return train.get_layout().get_block(id|0x1000);
-}
-
-Track &Timetable::get_turnout(unsigned id)
-{
- Block &block = train.get_layout().get_block(id|0x2000);
- return **block.get_tracks().begin();
-}
-
-Zone &Timetable::get_zone(const string &name)
-{
- string::size_type space = name.rfind(' ');
- if(space==string::npos || space==0)
- throw invalid_argument("Timetable::get_zone");
- unsigned number = lexical_cast<unsigned>(name.substr(space+1));
- return train.get_layout().get_zone(name.substr(0, space), number);
-}
-
-void Timetable::sensor_state_changed(Sensor &sensor, Sensor::State state)
-{
- if(rows.empty() || !enabled)
- return;
-
- Block *block = 0;
- if(TrackCircuit *tc = dynamic_cast<TrackCircuit *>(&sensor))
- block = &tc->get_block();
- else
- return;
-
- if(block==pending_block && block->get_train()==pending_train && state>=Sensor::MAYBE_ACTIVE)
- {
- pending_block = 0;
- current_row = (current_row+1)%rows.size();
- executing = true;
- }
-}
-
-void Timetable::block_reserved(Block &block, Train *trn)
-{
- if(rows.empty() || !enabled)
- return;
-
- if(&block==pending_block && trn==pending_train)
- {
- Row &row = rows[current_row];
- if(row.type==TRAVEL_PAST && !pending_train)
- {
- pending_block = 0;
- current_row = (current_row+1)%rows.size();
- executing = true;
- }
- }
-}
-
-void Timetable::train_advanced(Block &block)
-{
- if(rows.empty() || !enabled)
- return;
-
- Row &row = rows[current_row];
- if(row.type==TRAVEL_PAST && &block==pending_block && pending_train)
- pending_train = 0;
-}
-
-void Timetable::event(TrainAI &, const Message &ev)
-{
- if(ev.type=="arrived")
- {
- if(rows.empty() || !enabled)
- return;
-
- Row &row = rows[current_row];
- if(row.type==ARRIVE)
- {
- current_row = (current_row+1)%rows.size();
- executing = true;
- }
- else
- arrived = true;
- }
-}
-
-
-Timetable::Row::Row(RowType t):
- type(t)
-{ }
-
-template<typename T>
-Timetable::Row::Row(RowType t, const T &p):
- type(t)
-{
- params.push_back(p);
-}
-
-template<typename T>
-const T &Timetable::Row::get_param(unsigned i) const
-{
- if(i>=params.size())
- throw out_of_range("Timetable::Row::get_param");
- return params[i].value<T>();
-}
-
-string Timetable::Row::str() const
-{
- switch(type)
- {
- case GOTO_SENSOR:
- return format("set route to sensor %d", get_param<unsigned>(0));
- case GOTO_ZONE:
- return "set route to "+get_param<string>(0);
- case TRAVEL_TO:
- return format("travel to sensor %d", get_param<unsigned>(0));
- case TRAVEL_PAST:
- return format("travel past turnout %d", get_param<unsigned>(0));
- case WAIT_TIME:
- return format("wait for %d seconds", get_param<unsigned>(0));
- case WAIT_UNTIL:
- return format("wait until %d mod %d seconds", get_param<unsigned>(0), get_param<unsigned>(1));
- case WAIT_TRAIN:
- return format("wait for train %d at sensor %d", get_param<unsigned>(0), get_param<unsigned>(1));
- case ARRIVE:
- return "travel until arrival";
- case SPEED:
- return format("set speed %d km/h", get_param<unsigned>(0));
- case REVERSE:
- return "reverse";
- case ROUTE:
- return "set route "+get_param<string>(0);
- default:
- return "invalid row";
- }
-}
-
-DataFile::Statement Timetable::Row::save() const
-{
- switch(type)
- {
- case GOTO_SENSOR:
- return DataFile::Statement("goto_sensor"), get_param<unsigned>(0);
- case GOTO_ZONE:
- return DataFile::Statement("goto_zone"), get_param<string>(0);
- case TRAVEL_TO:
- return DataFile::Statement("travel_to"), get_param<unsigned>(0);
- case TRAVEL_PAST:
- return DataFile::Statement("travel_past"), get_param<unsigned>(0);
- case WAIT_TIME:
- return DataFile::Statement("wait"), get_param<unsigned>(0);
- case WAIT_UNTIL:
- return DataFile::Statement("wait_until"), get_param<unsigned>(0), get_param<unsigned>(1);
- case WAIT_TRAIN:
- return DataFile::Statement("wait_train"), get_param<unsigned>(0), get_param<unsigned>(1);
- case ARRIVE:
- return DataFile::Statement("arrive");
- case SPEED:
- return DataFile::Statement("speed"), get_param<unsigned>(0);
- case REVERSE:
- return DataFile::Statement("reverse");
- case ROUTE:
- return DataFile::Statement("route"), get_param<string>(0);
- default:
- return DataFile::Statement();
- }
-}
-
-Timetable::Row Timetable::Row::parse(const string &s)
-{
- if(!s.compare(0, 7, "travel "))
- {
- if(!s.compare(7, 10, "to sensor "))
- return Row(TRAVEL_TO, lexical_cast<unsigned>(s.substr(17)));
- else if(!s.compare(7, 13, "past turnout "))
- return Row(TRAVEL_PAST, lexical_cast<unsigned>(s.substr(20)));
- else if(!s.compare(7, string::npos, "until arrival"))
- return Row(ARRIVE);
- }
- else if(!s.compare(0, 9, "wait for "))
- {
- if(isdigit(s[9]))
- {
- unsigned nondigit = 10;
- while(nondigit<s.size() && isdigit(s[nondigit]))
- ++nondigit;
- return Row(WAIT_TIME, lexical_cast<unsigned>(s.substr(9, nondigit-9)));
- }
- else if(!s.compare(9, 6, "train "))
- {
- string::size_type at = s.find(" at sensor ", 15);
- if(at!=string::npos)
- {
- Row row(WAIT_TRAIN, lexical_cast<unsigned>(s.substr(15, at-15)));
- row.params.push_back(lexical_cast<unsigned>(s.substr(at+11)));
- return row;
- }
- }
- }
- else if(!s.compare(0, 11, "wait until "))
- {
- string::size_type mod = s.find(" mod ", 11);
- unsigned nondigit = (mod!=string::npos ? mod+5 : 11);
- while(nondigit<s.size() && isdigit(s[nondigit]))
- ++nondigit;
- if(mod!=string::npos)
- {
- unsigned time = lexical_cast<unsigned>(s.substr(11, mod-11));
- Row row(WAIT_UNTIL, time);
- row.params.push_back(lexical_cast<unsigned>(s.substr(mod+5, nondigit-mod-5)));
- return row;
- }
- else
- {
- unsigned time = lexical_cast<unsigned>(s.substr(11, nondigit-11));
- Row row(WAIT_UNTIL, time);
- row.params.push_back(3600);
- return row;
- }
- }
- else if(!s.compare(0, 10, "set speed "))
- {
- unsigned nondigit = 11;
- while(nondigit<s.size() && (isdigit(s[nondigit]) || s[nondigit]=='-'))
- ++nondigit;
- return Row(SPEED, lexical_cast<unsigned>(s.substr(10, nondigit-10)));
- }
- else if(s=="reverse")
- return Row(REVERSE);
- else if(!s.compare(0, 10, "set route "))
- {
- if(!s.compare(10, 3, "to "))
- {
- if(!s.compare(13, 7, "sensor "))
- return Row(GOTO_SENSOR, lexical_cast<unsigned>(s.substr(20)));
- else
- return Row(GOTO_ZONE, s.substr(13));
- }
- return Row(ROUTE, s.substr(10));
- }
-
- throw invalid_argument("Timetable::Row::parse");
-}
-
-
-Timetable::Loader::Loader(Timetable &tt):
- DataFile::ObjectLoader<Timetable>(tt)
-{
- add("arrive", &Loader::arrive);
- add("goto_sensor", &Loader::goto_sensor);
- add("goto_zone", &Loader::goto_zone);
- add("route", &Loader::route);
- add("speed", &Loader::speed);
- add("reverse", &Loader::reverse);
- add("travel_to", &Loader::travel_to);
- add("travel_past", &Loader::travel_past);
- add("wait", &Loader::wait);
- add("wait_train", &Loader::wait_train);
- add("wait_until", &Loader::wait_until);
-
- // Deprecated alias
- add("goto", &Loader::goto_sensor_str);
- add("travel", &Loader::travel_to);
-}
-
-void Timetable::Loader::arrive()
-{
- obj.rows.push_back(Row(ARRIVE));
-}
-
-void Timetable::Loader::goto_sensor(unsigned s)
-{
- obj.rows.push_back(Row(GOTO_SENSOR, s));
-}
-
-void Timetable::Loader::goto_sensor_str(const string &s)
-{
- if(!s.compare(0, 7, "sensor "))
- obj.rows.push_back(Row(GOTO_SENSOR, lexical_cast<unsigned>(s.substr(7))));
-}
-
-void Timetable::Loader::goto_zone(const string &z)
-{
- obj.rows.push_back(Row(GOTO_ZONE, z));
-}
-
-void Timetable::Loader::route(const string &r)
-{
- obj.rows.push_back(Row(ROUTE, r));
-}
-
-void Timetable::Loader::reverse()
-{
- obj.rows.push_back(Row(REVERSE));
-}
-
-void Timetable::Loader::speed(unsigned s)
-{
- obj.rows.push_back(Row(SPEED, s));
-}
-
-void Timetable::Loader::travel_to(unsigned s)
-{
- obj.rows.push_back(Row(TRAVEL_TO, s));
-}
-
-void Timetable::Loader::travel_past(unsigned s)
-{
- obj.rows.push_back(Row(TRAVEL_PAST, s));
-}
-
-void Timetable::Loader::wait(unsigned t)
-{
- obj.rows.push_back(Row(WAIT_TIME, t));
-}
-
-void Timetable::Loader::wait_train(unsigned t, unsigned s)
-{
- Row row(WAIT_TRAIN, t);
- row.params.push_back(s);
- obj.rows.push_back(row);
-}
-
-void Timetable::Loader::wait_until(unsigned t, unsigned m)
-{
- Row row(WAIT_UNTIL, t);
- row.params.push_back(m);
- obj.rows.push_back(row);
-}
-
-} // namespace R2C2