From: Mikko Rasa Date: Thu, 21 Oct 2010 10:56:16 +0000 (+0000) Subject: Add a timetable row type to wait for another train X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=25c26706d400228110e14a4a9a9765cb1298372d;p=r2c2.git Add a timetable row type to wait for another train --- diff --git a/source/libmarklin/timetable.cpp b/source/libmarklin/timetable.cpp index f3d49c4..afe83ff 100644 --- a/source/libmarklin/timetable.cpp +++ b/source/libmarklin/timetable.cpp @@ -24,7 +24,8 @@ Timetable::Timetable(Train &t): enabled(false), current_row(0), executing(true), - pending_block(0) + pending_block(0), + pending_train(0) { train.signal_arrived.connect(sigc::mem_fun(this, &Timetable::train_arrived)); train.get_layout().get_driver().signal_sensor.connect(sigc::mem_fun(this, &Timetable::sensor_event)); @@ -93,12 +94,18 @@ void Timetable::tick(const Time::TimeStamp &t) break; case TRAVEL: pending_block = &parse_location(row.get_param(0)); + pending_train = &train; executing = false; break; - case WAIT: + case WAIT_TIME: wait_timeout = t+row.get_param(0)*Time::sec; executing = false; break; + case WAIT_TRAIN: + pending_train = &train.get_layout().get_train(row.get_param(0)); + pending_block = &parse_location(row.get_param(1)); + executing = false; + break; case ARRIVE: executing = false; break; @@ -133,7 +140,7 @@ Block &Timetable::parse_location(const string &loc) void Timetable::sensor_event(unsigned addr, bool state) { - if(pending_block && pending_block->get_train()==&train && addr==pending_block->get_sensor_id() && state) + if(pending_block && pending_block->get_train()==pending_train && addr==pending_block->get_sensor_id() && state) { pending_block = 0; current_row = (current_row+1)%rows.size(); @@ -179,8 +186,10 @@ string Timetable::Row::str() const return "set route to "+get_param(0); case TRAVEL: return "travel to "+get_param(0); - case WAIT: + case WAIT_TIME: return format("wait for %d seconds", get_param(0)); + case WAIT_TRAIN: + return format("wait for train %d at %s", get_param(0), get_param(1)); case ARRIVE: return "travel until arrival"; case SPEED: @@ -202,8 +211,10 @@ DataFile::Statement Timetable::Row::save() const return DataFile::Statement("goto"), get_param(0); case TRAVEL: return DataFile::Statement("travel"), get_param(0); - case WAIT: + case WAIT_TIME: return DataFile::Statement("wait"), get_param(0); + case WAIT_TRAIN: + return DataFile::Statement("wait_train"), get_param(0), get_param(1); case ARRIVE: return DataFile::Statement("arrive"); case SPEED: @@ -226,12 +237,25 @@ Timetable::Row Timetable::Row::parse(const string &s) else if(!s.compare(7, string::npos, "until arrival")) return Row(ARRIVE); } - else if(!s.compare(0, 9, "wait for ") && isdigit(s[9])) + else if(!s.compare(0, 9, "wait for ")) { - unsigned nondigit = 10; - while(nondigit(s.substr(9, nondigit-9))); + if(isdigit(s[9])) + { + unsigned nondigit = 10; + while(nondigit(s.substr(9, nondigit-9))); + } + else if(!s.compare(9, 6, "train ")) + { + string::size_type at = s.find(" at ", 15); + if(at!=string::npos) + { + Row row(WAIT_TRAIN, lexical_cast(s.substr(15, at-15))); + row.params.push_back(s.substr(at+4)); + return row; + } + } } else if(!s.compare(0, 10, "set speed ")) { @@ -263,6 +287,7 @@ Timetable::Loader::Loader(Timetable &tt): add("reverse", &Loader::reverse); add("travel", &Loader::travel); add("wait", &Loader::wait); + add("wait_train", &Loader::wait_train); } void Timetable::Loader::arrive() @@ -297,7 +322,14 @@ void Timetable::Loader::travel(const string &t) void Timetable::Loader::wait(unsigned t) { - obj.rows.push_back(Row(WAIT, t)); + obj.rows.push_back(Row(WAIT_TIME, t)); +} + +void Timetable::Loader::wait_train(unsigned t, const string &b) +{ + Row row(WAIT_TRAIN, t); + row.params.push_back(b); + obj.rows.push_back(row); } } // namespace Marklin diff --git a/source/libmarklin/timetable.h b/source/libmarklin/timetable.h index 72e12d3..b4f9cc1 100644 --- a/source/libmarklin/timetable.h +++ b/source/libmarklin/timetable.h @@ -34,13 +34,15 @@ public: void speed(unsigned); void travel(const std::string &); void wait(unsigned); + void wait_train(unsigned, const std::string &); }; enum RowType { GOTO, TRAVEL, - WAIT, + WAIT_TIME, + WAIT_TRAIN, ARRIVE, SPEED, REVERSE, @@ -74,6 +76,7 @@ private: unsigned current_row; bool executing; Block *pending_block; + Train *pending_train; Msp::Time::TimeStamp wait_timeout; public: