]> git.tdb.fi Git - r2c2.git/commitdiff
Add a timetable command to travel past a turnout
authorMikko Rasa <tdb@tdb.fi>
Sat, 19 Feb 2011 06:44:49 +0000 (06:44 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sat, 19 Feb 2011 06:44:49 +0000 (06:44 +0000)
source/libr2c2/timetable.cpp
source/libr2c2/timetable.h
source/libr2c2/train.cpp
source/libr2c2/train.h

index 8b469b52fd848feb52f09c07346a521780c1d8ca..670114cdd4454fab36d1be60355a8b22bab2631e 100644 (file)
@@ -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<string>(0))))
                                set_enabled(false);
                        break;
-               case TRAVEL:
+               case TRAVEL_TO:
                        pending_block = &get_sensor(row.get_param<unsigned>(0)).get_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;
@@ -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<unsigned>(0);
        case GOTO_ZONE:
                return "set route to "+get_param<string>(0);
-       case TRAVEL:
+       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:
@@ -246,8 +283,10 @@ DataFile::Statement Timetable::Row::save() const
                return DataFile::Statement("goto_sensor"), get_param<unsigned>(0);
        case GOTO_ZONE:
                return DataFile::Statement("goto_zone"), get_param<string>(0);
-       case TRAVEL:
-               return DataFile::Statement("travel"), get_param<unsigned>(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:
@@ -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<unsigned>(s.substr(17)));
+                       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);
        }
@@ -346,18 +387,20 @@ Timetable::Loader::Loader(Timetable &tt):
        DataFile::ObjectLoader<Timetable>(tt)
 {
        add("arrive",      &Loader::arrive);
-       add("goto_sensor", static_cast<void (Loader::*)(unsigned)>(&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<void (Loader::*)(const string &)>(&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<unsigned>(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)
index 8f64ad1c1f1ec828c81f4a7691aff1cf40b6bbee..cbbb49b3d9e3d357fa9e24780f834c752ee69123 100644 (file)
@@ -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<Msp::DataFile::Statement> &) 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();
 };
 
index 122ea9b18795ffcc64f4de01d065bd5d1fb46369..1447ca58df834c86f7a14d27610e0959bb38c560 100644 (file)
@@ -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
index 81f0e2a9e86c700a92486fcdce88159fd8966da1..48ee867a597c7c22e56045dc88df27a7b29bf88c 100644 (file)
@@ -50,6 +50,7 @@ public:
        sigc::signal<void, const std::string &, float> signal_control_changed;
        sigc::signal<void, unsigned, bool> signal_function_changed;
        sigc::signal<void, const Route *> signal_route_changed;
+       sigc::signal<void, Block &> signal_advanced;
        sigc::signal<void> signal_arrived;
        sigc::signal<void, const std::string &> signal_status_changed;