]> git.tdb.fi Git - r2c2.git/commitdiff
Add a timetable row type to wait for another train
authorMikko Rasa <tdb@tdb.fi>
Thu, 21 Oct 2010 10:56:16 +0000 (10:56 +0000)
committerMikko Rasa <tdb@tdb.fi>
Thu, 21 Oct 2010 10:56:16 +0000 (10:56 +0000)
source/libmarklin/timetable.cpp
source/libmarklin/timetable.h

index f3d49c48fa1d44f0b38f8efe94d903c9dc664720..afe83ff867376004753b75c60408b904f5f36c48 100644 (file)
@@ -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<string>(0));
+                       pending_train = &train;
                        executing = false;
                        break;
-               case WAIT:
+               case WAIT_TIME:
                        wait_timeout = t+row.get_param<unsigned>(0)*Time::sec;
                        executing = false;
                        break;
+               case WAIT_TRAIN:
+                       pending_train = &train.get_layout().get_train(row.get_param<unsigned>(0));
+                       pending_block = &parse_location(row.get_param<string>(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<string>(0);
        case TRAVEL:
                return "travel to "+get_param<string>(0);
-       case WAIT:
+       case WAIT_TIME:
                return format("wait for %d seconds", get_param<unsigned>(0));
+       case WAIT_TRAIN:
+               return format("wait for train %d at %s", get_param<unsigned>(0), get_param<string>(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<string>(0);
        case TRAVEL:
                return DataFile::Statement("travel"), get_param<string>(0);
-       case WAIT:
+       case WAIT_TIME:
                return DataFile::Statement("wait"), get_param<unsigned>(0);
+       case WAIT_TRAIN:
+               return DataFile::Statement("wait_train"), get_param<unsigned>(0), get_param<string>(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.size() && isdigit(s[nondigit]))
-                       ++nondigit;
-               return Row(WAIT, lexical_cast<unsigned>(s.substr(9, nondigit-9)));
+               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 ", 15);
+                       if(at!=string::npos)
+                       {
+                               Row row(WAIT_TRAIN, lexical_cast<unsigned>(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
index 72e12d3d9980f5ca1a66732678c88abfd17cf20e..b4f9cc1b2fd2972ed097926e54c31a45824101f5 100644 (file)
@@ -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: