From d2dfed1a38c5e8487532e9055fad464cf54efd83 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 9 Jun 2013 11:32:21 +0300 Subject: [PATCH] Remove the scripting-based timetable I'm going to implement a proper timetable soon, but some other changes need to happen first. --- source/engineer/timetabledialog.cpp | 72 ---- source/engineer/timetabledialog.h | 25 -- source/engineer/trainpanel.cpp | 17 - source/engineer/trainpanel.h | 1 - source/libr2c2/timetable.cpp | 498 ---------------------------- source/libr2c2/timetable.h | 112 ------- source/libr2c2/train.cpp | 14 - source/libr2c2/train.h | 1 - 8 files changed, 740 deletions(-) delete mode 100644 source/engineer/timetabledialog.cpp delete mode 100644 source/engineer/timetabledialog.h delete mode 100644 source/libr2c2/timetable.cpp delete mode 100644 source/libr2c2/timetable.h diff --git a/source/engineer/timetabledialog.cpp b/source/engineer/timetabledialog.cpp deleted file mode 100644 index 8a185cd..0000000 --- a/source/engineer/timetabledialog.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include -#include "timetabledialog.h" - -using namespace std; -using namespace Msp; -using namespace R2C2; - -TimetableDialog::TimetableDialog(Timetable &tt): - timetable(tt) -{ - set_size(250, 200); - - GLtk::Button *btn; - - add_button(*(btn = new GLtk::Button("Cncl")), 0); - btn->set_geometry(GLtk::Geometry(geom.w-80, 10, 30, 25)); - btn->set_style("red"); - - add_button(*(btn = new GLtk::Button("OK")), 1); - btn->set_geometry(GLtk::Geometry(geom.w-40, 10, 30, 25)); - btn->set_style("green"); - - add(*(tgl_enabled = new GLtk::Toggle("On"))); - tgl_enabled->set_geometry(GLtk::Geometry(10, 10, 40, 27)); - tgl_enabled->set_value(timetable.is_enabled()); - tgl_enabled->signal_toggled.connect(sigc::mem_fun(this, &TimetableDialog::enabled_toggled)); - - add(*(ent_timetable = new GLtk::Entry)); - ent_timetable->set_geometry(GLtk::Geometry(10, 45, geom.w-20, geom.h-55)); - ent_timetable->set_style("multiline"); - ent_timetable->set_multiline(true); - - string text; - for(unsigned i=0; iset_text(text); -} - -void TimetableDialog::enabled_toggled(bool value) -{ - timetable.set_enabled(value); - timetable.reset(); -} - -void TimetableDialog::on_response(int code) -{ - if(code) - { - vector lines = split(ent_timetable->get_text(), '\n'); - vector rows; - rows.reserve(lines.size()); - try - { - for(vector::const_iterator i=lines.begin(); i!=lines.end(); ++i) - rows.push_back(Timetable::Row::parse(*i)); - timetable.clear(); - for(vector::const_iterator i=rows.begin(); i!=rows.end(); ++i) - timetable.append(*i); - } - catch(const exception &e) - { - // XXX Need a better way to report errors. Also, should not let the dialog close. - IO::print("%s\n", e.what()); - } - } -} diff --git a/source/engineer/timetabledialog.h b/source/engineer/timetabledialog.h deleted file mode 100644 index 98afc20..0000000 --- a/source/engineer/timetabledialog.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef TIMETABLEDIALOG_H_ -#define TIMETABLEDIALOG_H_ - -#include -#include -#include -#include "libr2c2/timetable.h" - -class TimetableDialog: public Msp::GLtk::Dialog -{ -private: - R2C2::Timetable &timetable; - Msp::GLtk::Entry *ent_timetable; - Msp::GLtk::Toggle *tgl_enabled; - -public: - TimetableDialog(R2C2::Timetable &); - -private: - void enabled_toggled(bool); - - virtual void on_response(int); -}; - -#endif diff --git a/source/engineer/trainpanel.cpp b/source/engineer/trainpanel.cpp index e6a91a8..e239b1e 100644 --- a/source/engineer/trainpanel.cpp +++ b/source/engineer/trainpanel.cpp @@ -1,13 +1,11 @@ #include #include #include "libr2c2/aicontrol.h" -#include "libr2c2/timetable.h" #include "libr2c2/trackiter.h" #include "libr2c2/trainrouter.h" #include "libr2c2/vehicletype.h" #include "engineer.h" #include "routeselect.h" -#include "timetabledialog.h" #include "trainpanel.h" #include "trainproperties.h" #include "trainview.h" @@ -120,10 +118,6 @@ TrainPanel::TrainPanel(Engineer &e, Train &t): btn->set_geometry(GLtk::Geometry(100, 30, 36, 25)); btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::route_clicked)); - pnl_extra->add(*(btn = new GLtk::Button("TTbl"))); - btn->set_geometry(GLtk::Geometry(46, 30, 36, 25)); - btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::timetable_clicked)); - pnl_extra->add(*(btn = new GLtk::Button("View"))); btn->set_geometry(GLtk::Geometry(geom.w-46, 30, 36, 25)); btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::view_clicked)); @@ -210,17 +204,6 @@ void TrainPanel::goto_clicked() pick_conn = engineer.signal_pick_done.connect(sigc::mem_fun(this, &TrainPanel::go_to)); } -void TrainPanel::timetable_clicked() -{ - Timetable *timetable = train.get_ai_of_type(); - if(!timetable) - timetable = new Timetable(train); - - TimetableDialog *dialog = new TimetableDialog(*timetable); - engineer.get_root().add(*dialog); - dialog->set_position(geom.x+geom.w, geom.y+geom.h-dialog->get_geometry().h); -} - void TrainPanel::view_clicked() { TrainView *dialog = new TrainView(engineer, train); diff --git a/source/engineer/trainpanel.h b/source/engineer/trainpanel.h index 406e1a6..1881035 100644 --- a/source/engineer/trainpanel.h +++ b/source/engineer/trainpanel.h @@ -47,7 +47,6 @@ private: void edit_clicked(); void route_clicked(); void goto_clicked(); - void timetable_clicked(); void view_clicked(); void expand_clicked(); void speed_slider_changed(double); diff --git a/source/libr2c2/timetable.cpp b/source/libr2c2/timetable.cpp deleted file mode 100644 index 5be3a03..0000000 --- a/source/libr2c2/timetable.cpp +++ /dev/null @@ -1,498 +0,0 @@ -#include -#include -#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(0)))); - break; - case GOTO_ZONE: - arrived = false; - train.ai_message(Message("set-destination-zone", &get_zone(row.get_param(0)))); - break; - case TRAVEL_TO: - { - Block *block = &get_sensor(row.get_param(0)); - if(block->get_train()!=&train || block->get_sensor().get_state()(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; - break; - case WAIT_UNTIL: - { - unsigned unixtime = t.to_unixtime(); - unsigned mod = row.get_param(1); - unsigned secs = ((mod+row.get_param(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(0)); - Block *block = &get_sensor(row.get_param(1)); - if(block->get_train()!=other_train || block->get_sensor().get_state()(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(0)))); - break; - } - - if(executing) - current_row = (current_row+1)%rows.size(); - } -} - -void Timetable::save(list &st) const -{ - for(vector::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(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(&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 -Timetable::Row::Row(RowType t, const T &p): - type(t) -{ - params.push_back(p); -} - -template -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(); -} - -string Timetable::Row::str() const -{ - switch(type) - { - case GOTO_SENSOR: - return format("set route to sensor %d", get_param(0)); - case GOTO_ZONE: - return "set route to "+get_param(0); - 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: - return format("wait until %d mod %d seconds", get_param(0), get_param(1)); - case WAIT_TRAIN: - return format("wait for train %d at sensor %d", get_param(0), get_param(1)); - case ARRIVE: - return "travel until arrival"; - case SPEED: - return format("set speed %d km/h", get_param(0)); - case REVERSE: - return "reverse"; - case ROUTE: - return "set route "+get_param(0); - default: - return "invalid row"; - } -} - -DataFile::Statement Timetable::Row::save() const -{ - switch(type) - { - case GOTO_SENSOR: - return DataFile::Statement("goto_sensor"), get_param(0); - case GOTO_ZONE: - return DataFile::Statement("goto_zone"), 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: - return DataFile::Statement("wait_until"), get_param(0), get_param(1); - case WAIT_TRAIN: - return DataFile::Statement("wait_train"), get_param(0), get_param(1); - case ARRIVE: - return DataFile::Statement("arrive"); - case SPEED: - return DataFile::Statement("speed"), get_param(0); - case REVERSE: - return DataFile::Statement("reverse"); - case ROUTE: - return DataFile::Statement("route"), get_param(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(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); - } - else if(!s.compare(0, 9, "wait for ")) - { - 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 sensor ", 15); - if(at!=string::npos) - { - Row row(WAIT_TRAIN, lexical_cast(s.substr(15, at-15))); - row.params.push_back(lexical_cast(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.substr(11, mod-11)); - Row row(WAIT_UNTIL, time); - row.params.push_back(lexical_cast(s.substr(mod+5, nondigit-mod-5))); - return row; - } - else - { - unsigned time = lexical_cast(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.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(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(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(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 diff --git a/source/libr2c2/timetable.h b/source/libr2c2/timetable.h deleted file mode 100644 index f8fc8a0..0000000 --- a/source/libr2c2/timetable.h +++ /dev/null @@ -1,112 +0,0 @@ -#ifndef LIBR2C2_TIMETABLE_H_ -#define LIBR2C2_TIMETABLE_H_ - -#include -#include -#include -#include -#include "sensor.h" -#include "trainai.h" - -namespace R2C2 { - -class Block; -class Track; -class Train; -class Zone; - -class Timetable: public TrainAI, public sigc::trackable -{ -public: - class Loader: public Msp::DataFile::ObjectLoader - { - public: - Loader(Timetable &); - private: - void arrive(); - void goto_sensor(unsigned); - 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_to(unsigned); - void travel_past(unsigned); - void wait(unsigned); - void wait_train(unsigned, unsigned); - void wait_until(unsigned, unsigned); - }; - - enum RowType - { - GOTO_SENSOR, - GOTO_ZONE, - TRAVEL_TO, - TRAVEL_PAST, - WAIT_TIME, - WAIT_UNTIL, - WAIT_TRAIN, - ARRIVE, - SPEED, - REVERSE, - ROUTE - }; - - struct Row - { - RowType type; - std::vector params; - - Row(RowType); - - template - Row(RowType, const T &); - - template - const T &get_param(unsigned) const; - - std::string str() const; - - Msp::DataFile::Statement save() const; - - static Row parse(const std::string &); - }; - -private: - bool enabled; - std::vector rows; - unsigned current_row; - bool executing; - Block *pending_block; - Train *pending_train; - Msp::Time::TimeStamp wait_timeout; - bool arrived; - -public: - Timetable(Train &); - - void set_enabled(bool); - bool is_enabled() const { return enabled; } - void reset(); - - void clear(); - void append(const Row &); - void insert(unsigned, const Row &); - unsigned get_n_rows() const { return rows.size(); } - const Row &get_row(unsigned) const; - - void tick(const Msp::Time::TimeStamp &, const Msp::Time::TimeDelta &); - void save(std::list &) const; -private: - Block &get_sensor(unsigned); - Track &get_turnout(unsigned); - Zone &get_zone(const std::string &); - void sensor_state_changed(Sensor &, Sensor::State); - void block_reserved(Block &, Train *); - void train_advanced(Block &); - void event(TrainAI &, const Message &); -}; - -} // namespace R2C2 - -#endif diff --git a/source/libr2c2/train.cpp b/source/libr2c2/train.cpp index 7224247..d1fb4ed 100644 --- a/source/libr2c2/train.cpp +++ b/source/libr2c2/train.cpp @@ -13,7 +13,6 @@ #include "route.h" #include "simplecontroller.h" #include "speedquantizer.h" -#include "timetable.h" #include "trackcircuit.h" #include "trackiter.h" #include "tracktype.h" @@ -388,12 +387,6 @@ void Train::save(list &st) const router->save(ss.sub); st.push_back(ss); } - else if(Timetable *timetable = dynamic_cast(*i)) - { - DataFile::Statement ss("timetable"); - timetable->save(ss.sub); - st.push_back(ss); - } } } @@ -542,7 +535,6 @@ Train::Loader::Loader(Train &t): add("name", &Loader::name); add("quantized_speed", &Loader::quantized_speed); add("router", &Loader::router); - add("timetable", &Loader::timetable); add("vehicle", &Loader::vehicle); } @@ -578,12 +570,6 @@ void Train::Loader::router() load_sub(*rtr); } -void Train::Loader::timetable() -{ - Timetable *ttbl = new Timetable(obj); - load_sub(*ttbl); -} - void Train::Loader::vehicle(ArticleNumber art_nr) { const VehicleType &vtype = obj.layout.get_catalogue().get_vehicle(art_nr); diff --git a/source/libr2c2/train.h b/source/libr2c2/train.h index ece5b4b..e3eebe1 100644 --- a/source/libr2c2/train.h +++ b/source/libr2c2/train.h @@ -35,7 +35,6 @@ public: void name(const std::string &); void quantized_speed(); void router(); - void timetable(); void vehicle(ArticleNumber); }; -- 2.45.2