]> git.tdb.fi Git - r2c2.git/commitdiff
Add an absolute wait command to Timetable
authorMikko Rasa <tdb@tdb.fi>
Fri, 26 Nov 2010 14:09:55 +0000 (14:09 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 26 Nov 2010 14:09:55 +0000 (14:09 +0000)
source/libr2c2/timetable.cpp
source/libr2c2/timetable.h

index dd91d219ab21f22be20b156348c2c17bb6643875..c28cbccae245a6dcb7650eb4e0237feb85056ccf 100644 (file)
@@ -106,6 +106,15 @@ void Timetable::tick(const Time::TimeStamp &t)
                        wait_timeout = t+row.get_param<unsigned>(0)*Time::sec;
                        executing = false;
                        break;
+               case WAIT_UNTIL:
+                       {
+                               unsigned unixtime = t.to_unixtime();
+                               unsigned mod = row.get_param<unsigned>(1);
+                               unsigned secs = ((mod+row.get_param<unsigned>(0))-(unixtime%mod))%mod;
+                               wait_timeout = t+secs*Time::sec;
+                               executing = false;
+                       }
+                       break;
                case WAIT_TRAIN:
                        pending_train = &train.get_layout().get_train(row.get_param<unsigned>(0));
                        pending_block = &get_sensor(row.get_param<unsigned>(1)).get_block();
@@ -204,6 +213,8 @@ string Timetable::Row::str() const
                return "travel to "+get_param<string>(0);
        case WAIT_TIME:
                return format("wait for %d seconds", get_param<unsigned>(0));
+       case WAIT_UNTIL:
+               return format("wait until %d mod %d seconds", get_param<unsigned>(0), get_param<unsigned>(1));
        case WAIT_TRAIN:
                return format("wait for train %d at %s", get_param<unsigned>(0), get_param<string>(1));
        case ARRIVE:
@@ -231,6 +242,8 @@ DataFile::Statement Timetable::Row::save() const
                return DataFile::Statement("travel"), get_param<string>(0);
        case WAIT_TIME:
                return DataFile::Statement("wait"), get_param<unsigned>(0);
+       case WAIT_UNTIL:
+               return DataFile::Statement("wait_until"), get_param<unsigned>(0), get_param<unsigned>(1);
        case WAIT_TRAIN:
                return DataFile::Statement("wait_train"), get_param<unsigned>(0), get_param<unsigned>(1);
        case ARRIVE:
@@ -275,6 +288,27 @@ Timetable::Row Timetable::Row::parse(const string &s)
                        }
                }
        }
+       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.size() && isdigit(s[nondigit]))
+                       ++nondigit;
+               if(mod!=string::npos)
+               {
+                       unsigned time = lexical_cast<unsigned>(s.substr(11, mod-11));
+                       Row row(WAIT_UNTIL, time);
+                       row.params.push_back(lexical_cast<unsigned>(s.substr(mod+5, nondigit-mod-5)));
+                       return row;
+               }
+               else
+               {
+                       unsigned time = lexical_cast<unsigned>(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;
@@ -312,6 +346,7 @@ Timetable::Loader::Loader(Timetable &tt):
        add("travel",      &Loader::travel);
        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));
@@ -370,4 +405,11 @@ void Timetable::Loader::wait_train(unsigned t, unsigned 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
index 86b4ae2944c8e21ebb207d49e27e54a946d62dbf..ea3ff0b45e78aa79fd91bced272ec0498d73cbe6 100644 (file)
@@ -39,6 +39,7 @@ public:
                void travel(const std::string &);
                void wait(unsigned);
                void wait_train(unsigned, unsigned);
+               void wait_until(unsigned, unsigned);
        };
 
        enum RowType
@@ -47,6 +48,7 @@ public:
                GOTO_ZONE,
                TRAVEL,
                WAIT_TIME,
+               WAIT_UNTIL,
                WAIT_TRAIN,
                ARRIVE,
                SPEED,