From: Mikko Rasa Date: Sat, 19 Feb 2011 06:44:49 +0000 (+0000) Subject: Add a timetable command to travel past a turnout X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=598631d530a20ba8f4710f45747a9742e149f604;p=r2c2.git Add a timetable command to travel past a turnout --- diff --git a/source/libr2c2/timetable.cpp b/source/libr2c2/timetable.cpp index 8b469b5..670114c 100644 --- a/source/libr2c2/timetable.cpp +++ b/source/libr2c2/timetable.cpp @@ -28,8 +28,11 @@ Timetable::Timetable(Train &t): pending_train(0), arrived(false) { + train.signal_advanced.connect(sigc::mem_fun(this, &Timetable::train_advanced)); 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)); + Layout &layout = train.get_layout(); + layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Timetable::sensor_event)); + layout.signal_block_reserved.connect(sigc::mem_fun(this, &Timetable::block_reserved)); } void Timetable::set_enabled(bool e) @@ -100,11 +103,16 @@ void Timetable::tick(const Time::TimeStamp &t) if(!train.go_to(get_zone(row.get_param(0)))) set_enabled(false); break; - case TRAVEL: + case TRAVEL_TO: pending_block = &get_sensor(row.get_param(0)).get_block(); pending_train = &train; executing = false; break; + case TRAVEL_PAST: + pending_block = &get_turnout(row.get_param(0)).get_block(); + pending_train = (pending_block->get_train()==&train ? 0 : &train); + executing = false; + break; case WAIT_TIME: wait_timeout = t+row.get_param(0)*Time::sec; executing = false; @@ -158,6 +166,12 @@ Track &Timetable::get_sensor(unsigned id) return **block.get_tracks().begin(); } +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(' '); @@ -177,6 +191,27 @@ void Timetable::sensor_event(unsigned addr, bool state) } } +void Timetable::block_reserved(Block &block, Train *trn) +{ + 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) +{ + Row &row = rows[current_row]; + if(row.type==TRAVEL_PAST && &block==pending_block && pending_train) + pending_train = 0; +} + void Timetable::train_arrived() { Row &row = rows[current_row]; @@ -217,8 +252,10 @@ string Timetable::Row::str() const return "set route to sensor "+get_param(0); case GOTO_ZONE: return "set route to "+get_param(0); - case TRAVEL: + case TRAVEL_TO: return format("travel to sensor %d", get_param(0)); + case TRAVEL_PAST: + return format("travel past turnout %d", get_param(0)); case WAIT_TIME: return format("wait for %d seconds", get_param(0)); case WAIT_UNTIL: @@ -246,8 +283,10 @@ DataFile::Statement Timetable::Row::save() const return DataFile::Statement("goto_sensor"), get_param(0); case GOTO_ZONE: return DataFile::Statement("goto_zone"), get_param(0); - case TRAVEL: - return DataFile::Statement("travel"), get_param(0); + case TRAVEL_TO: + return DataFile::Statement("travel_to"), get_param(0); + case TRAVEL_PAST: + return DataFile::Statement("travel_past"), get_param(0); case WAIT_TIME: return DataFile::Statement("wait"), get_param(0); case WAIT_UNTIL: @@ -272,7 +311,9 @@ Timetable::Row Timetable::Row::parse(const string &s) if(!s.compare(0, 7, "travel ")) { if(!s.compare(7, 10, "to sensor ")) - return Row(TRAVEL, lexical_cast(s.substr(17))); + return Row(TRAVEL_TO, lexical_cast(s.substr(17))); + else if(!s.compare(7, 13, "past turnout ")) + return Row(TRAVEL_PAST, lexical_cast(s.substr(20))); else if(!s.compare(7, string::npos, "until arrival")) return Row(ARRIVE); } @@ -346,18 +387,20 @@ Timetable::Loader::Loader(Timetable &tt): DataFile::ObjectLoader(tt) { add("arrive", &Loader::arrive); - add("goto_sensor", static_cast(&Loader::goto_sensor)); + 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", &Loader::travel); + 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", static_cast(&Loader::goto_sensor)); + add("goto", &Loader::goto_sensor_str); + add("travel", &Loader::travel_to); } void Timetable::Loader::arrive() @@ -370,7 +413,7 @@ void Timetable::Loader::goto_sensor(unsigned s) obj.rows.push_back(Row(GOTO_SENSOR, s)); } -void Timetable::Loader::goto_sensor(const string &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(s.substr(7)))); @@ -396,9 +439,14 @@ void Timetable::Loader::speed(unsigned s) obj.rows.push_back(Row(SPEED, s)); } -void Timetable::Loader::travel(unsigned 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, s)); + obj.rows.push_back(Row(TRAVEL_PAST, s)); } void Timetable::Loader::wait(unsigned t) diff --git a/source/libr2c2/timetable.h b/source/libr2c2/timetable.h index 8f64ad1..cbbb49b 100644 --- a/source/libr2c2/timetable.h +++ b/source/libr2c2/timetable.h @@ -31,12 +31,13 @@ public: private: void arrive(); void goto_sensor(unsigned); - void goto_sensor(const std::string &); + void goto_sensor_str(const std::string &); void goto_zone(const std::string &); void route(const std::string &); void reverse(); void speed(unsigned); - void travel(unsigned); + void travel_to(unsigned); + void travel_past(unsigned); void wait(unsigned); void wait_train(unsigned, unsigned); void wait_until(unsigned, unsigned); @@ -46,7 +47,8 @@ public: { GOTO_SENSOR, GOTO_ZONE, - TRAVEL, + TRAVEL_TO, + TRAVEL_PAST, WAIT_TIME, WAIT_UNTIL, WAIT_TRAIN, @@ -104,8 +106,11 @@ public: void save(std::list &) const; private: Track &get_sensor(unsigned); + Track &get_turnout(unsigned); Zone &get_zone(const std::string &); void sensor_event(unsigned, bool); + void block_reserved(Block &, Train *); + void train_advanced(Block &); void train_arrived(); }; diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index 122ea9b..1447ca5 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -782,6 +782,8 @@ void Train::sensor_event(unsigned addr, bool state) } // Move blocks up to the next sensor to our current blocks + for(BlockList::iterator j=cur_blocks_end; j!=end; ++j) + signal_advanced.emit(**j); cur_blocks_end = end; // Try to get more blocks if we're moving diff --git a/source/libr2c2/train.h b/source/libr2c2/train.h index 81f0e2a..48ee867 100644 --- a/source/libr2c2/train.h +++ b/source/libr2c2/train.h @@ -50,6 +50,7 @@ public: sigc::signal signal_control_changed; sigc::signal signal_function_changed; sigc::signal signal_route_changed; + sigc::signal signal_advanced; sigc::signal signal_arrived; sigc::signal signal_status_changed;