]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/timetable.cpp
Allow zones with no qualifier or no number
[r2c2.git] / source / libr2c2 / timetable.cpp
index 93f61c50608d0b56839bf6ba59af8ba6b4410c47..5fa36395825d0afdb6dfb92df83e76f97d3cd6cb 100644 (file)
@@ -16,7 +16,8 @@ namespace R2C2 {
 Timetable::Timetable(Train &t):
        TrainAI(t),
        current_row(rows.end()),
-       update_pending(false)
+       update_pending(false),
+       sync_to_clock(true)
 {
        if(!train.get_ai_of_type<AIControl>())
                new AIControl(train);
@@ -115,7 +116,7 @@ void Timetable::save(list<DataFile::Statement> &st) const
        }
 }
 
-void Timetable::check_update(list<Row>::const_iterator i)
+void Timetable::check_update(const list<Row>::const_iterator &i)
 {
        for(list<Row>::const_iterator j=current_row; (j!=rows.end() && j!=i); ++j)
                if(j->type==ARRIVE)
@@ -144,6 +145,20 @@ void Timetable::update_route()
        if(rows.empty())
                return;
 
+       const Clock &clock = train.get_layout().get_clock();
+
+       if(sync_to_clock)
+       {
+               sync_to_clock = false;
+               current_row = rows.begin();
+               for(list<Row>::iterator i=rows.begin(); i!=rows.end(); ++i)
+                       if(i->type==DEPART && i->time>=clock.get_current_time())
+                       {
+                               current_row = i;
+                               break;
+                       }
+       }
+
        list<Row>::iterator arrive;
        list<Row>::iterator depart = find_trip(current_row, &arrive);
        if(depart==rows.end())
@@ -158,26 +173,22 @@ void Timetable::update_route()
 
        train.ai_message(Message("clear-route"));
 
-       const Clock &clock = train.get_layout().get_clock();
-
        current_row = depart;
        for(list<Row>::const_iterator i=depart; i!=rows.end(); ++i)
        {
-               if(i->type==ARRIVE)
-               {
-                       train.ai_message(Message("set-destination", i->target));
-                       break;
-               }
-               else if(i->type==DEPART)
+               if(i->type==DEPART)
                {
                        Time::TimeDelta dt = i->time-clock.get_current_time();
                        while(dt<Time::zero)
                                dt += Time::day;
-                       dt /= clock.get_rate();
-                       train.ai_message(Message("set-departure-delay", dt));
+                       train.ai_message(Message("set-departure-delay", dt/clock.get_rate()));
+               }
+               else
+               {
+                       train.ai_message(Message("add-waypoint", TrainRouter::Waypoint(*i->target, i->direction)));
+                       if(i->type==ARRIVE)
+                               break;
                }
-               else if(i->type==THROUGH)
-                       train.ai_message(Message("add-waypoint", i->target));
        }
 
        list<Row>::iterator next_depart = find_trip(arrive, 0);
@@ -221,7 +232,8 @@ void Timetable::record_time()
 
 Timetable::Row::Row():
        type(ARRIVE),
-       target(0)
+       target(0),
+       direction(TrackChain::UNSPECIFIED)
 { }
 
 void Timetable::Row::save(list<DataFile::Statement> &st) const
@@ -229,6 +241,8 @@ void Timetable::Row::save(list<DataFile::Statement> &st) const
        st.push_back((DataFile::Statement("type"), type));
        st.push_back((DataFile::Statement("time"), time.raw()));
        st.push_back(target->save_reference());
+       if(direction)
+               st.push_back((DataFile::Statement("direction"), direction));
 }
 
 
@@ -253,9 +267,11 @@ Timetable::Row::Loader::Loader(Row &r, Layout &l):
        layout(l)
 {
        add("block", &Loader::block);
+       add("direction", &Row::direction);
        add("time", &Loader::time);
        add("type", &Row::type);
        add("zone", &Loader::zone);
+       add("zone", &Loader::zone_numbered);
 }
 
 void Timetable::Row::Loader::block(unsigned id)
@@ -268,7 +284,12 @@ void Timetable::Row::Loader::time(Time::RawTime t)
        obj.time = Time::TimeDelta(t);
 }
 
-void Timetable::Row::Loader::zone(const string &name, unsigned number)
+void Timetable::Row::Loader::zone(const string &name)
+{
+       zone_numbered(name, 0);
+}
+
+void Timetable::Row::Loader::zone_numbered(const string &name, unsigned number)
 {
        obj.target = &layout.get_zone(name, number);
 }