]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/timetable.cpp
Sync timetable to clock on first tick
[r2c2.git] / source / libr2c2 / timetable.cpp
index 42414f63d0baf3ecfe14fcbda9afb4de8c3669e3..7ce8127d433b7995a6b9ff91aa9f1f0f53909f60 100644 (file)
@@ -1,3 +1,4 @@
+#include <algorithm>
 #include <msp/strings/format.h>
 #include "aicontrol.h"
 #include "clock.h"
@@ -15,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);
@@ -122,28 +124,47 @@ void Timetable::check_update(list<Row>::const_iterator i)
        update_pending = true;
 }
 
+list<Timetable::Row>::iterator Timetable::find_trip(const list<Row>::iterator &begin, list<Row>::iterator *arrive)
+{
+       list<Row>::iterator i = find_if(begin, rows.end(), RowTypeMatch(DEPART));
+       if(i==rows.end())
+               return i;
+
+       list<Row>::iterator j = find_if(i, rows.end(), RowTypeMatch(ARRIVE));
+       if(j==rows.end())
+               return j;
+
+       if(arrive)
+               *arrive = j;
+       return i;
+}
+
 void Timetable::update_route()
 {
        update_pending = false;
        if(rows.empty())
                return;
 
-       list<Row>::iterator depart = rows.end();
-       for(list<Row>::iterator i=current_row;; )
-       {
-               if(i==rows.end())
-               {
-                       i = rows.begin();
-                       depart = rows.end();
-               }
+       const Clock &clock = train.get_layout().get_clock();
 
-               if(i->type==DEPART)
-                       depart = i;
-               else if(depart!=rows.end() && i->type==ARRIVE)
-                       break;
+       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;
+                       }
+       }
 
-               ++i;
-               if(i==current_row)
+       list<Row>::iterator arrive;
+       list<Row>::iterator depart = find_trip(current_row, &arrive);
+       if(depart==rows.end())
+       {
+               depart = find_trip(rows.begin(), &arrive);
+               if(depart==rows.end())
                {
                        current_row = rows.end();
                        return;
@@ -162,7 +183,6 @@ void Timetable::update_route()
                }
                else if(i->type==DEPART)
                {
-                       const Clock &clock = train.get_layout().get_clock();
                        Time::TimeDelta dt = i->time-clock.get_current_time();
                        while(dt<Time::zero)
                                dt += Time::day;
@@ -172,6 +192,17 @@ void Timetable::update_route()
                else if(i->type==THROUGH)
                        train.ai_message(Message("add-waypoint", i->target));
        }
+
+       list<Row>::iterator next_depart = find_trip(arrive, 0);
+       if(next_depart==rows.end())
+               next_depart = find_trip(rows.begin(), 0);
+       if(next_depart!=rows.end())
+       {
+               Time::TimeDelta dt = next_depart->time-depart->time;
+               while(dt<=Time::zero)
+                       dt += Time::day;
+               train.ai_message(Message("set-trip-duration", dt/clock.get_rate()));
+       }
 }
 
 void Timetable::event(TrainAI &, const Message &msg)