]> 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 ed7d01831445e3745a4e7a297d687e5f3c3d7e00..7ce8127d433b7995a6b9ff91aa9f1f0f53909f60 100644 (file)
@@ -1,8 +1,11 @@
+#include <algorithm>
 #include <msp/strings/format.h>
+#include "aicontrol.h"
 #include "clock.h"
 #include "layout.h"
 #include "timetable.h"
 #include "train.h"
+#include "trainrouter.h"
 #include "zone.h"
 
 using namespace std;
@@ -13,8 +16,14 @@ 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);
+       if(!train.get_ai_of_type<TrainRouter>())
+               new TrainRouter(train);
+
        train.signal_ai_event.connect(sigc::mem_fun(this, &Timetable::event));
 }
 
@@ -115,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;
@@ -155,13 +183,25 @@ 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;
                        dt /= clock.get_rate();
                        train.ai_message(Message("set-departure-delay", dt));
                }
+               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()));
        }
 }
 
@@ -170,15 +210,27 @@ void Timetable::event(TrainAI &, const Message &msg)
        if(msg.type=="arrived")
        {
                if(current_row->type==ARRIVE)
+                       record_time();
+               update_pending = true;
+       }
+       else if(msg.type=="waypoint-reached")
+       {
+               const TrackChain *wp = msg.value.value<const TrackChain *>();
+               if(current_row->type==THROUGH && current_row->target==wp)
                {
-                       current_row->time = train.get_layout().get_clock().get_current_time();
-                       unsigned i = distance(rows.begin(), current_row);
-                       signal_row_modified.emit(i, *current_row);
+                       record_time();
+                       ++current_row;
                }
-               update_pending = true;
        }
 }
 
+void Timetable::record_time()
+{
+       current_row->time = train.get_layout().get_clock().get_current_time();
+       unsigned i = distance(rows.begin(), current_row);
+       signal_row_modified.emit(i, *current_row);
+}
+
 
 Timetable::Row::Row():
        type(ARRIVE),
@@ -241,6 +293,7 @@ void operator<<(LexicalConverter &conv, Timetable::RowType rt)
        {
        case Timetable::ARRIVE: conv.result("ARRIVE"); return;
        case Timetable::DEPART: conv.result("DEPART"); return;
+       case Timetable::THROUGH: conv.result("THROUGH"); return;
        default: throw lexical_error(format("conversion of RowType(%d) to string", rt));
        }
 }
@@ -251,6 +304,8 @@ void operator>>(const LexicalConverter &conv, Timetable::RowType &rt)
                rt = Timetable::ARRIVE;
        else if(conv.get()=="DEPART")
                rt = Timetable::DEPART;
+       else if(conv.get()=="THROUGH")
+               rt = Timetable::THROUGH;
        else
                throw lexical_error(format("conversion of '%s' to RowType", conv.get()));
 }