]> git.tdb.fi Git - r2c2.git/commitdiff
Handle the case of a train being late
authorMikko Rasa <tdb@tdb.fi>
Tue, 31 Mar 2015 21:49:03 +0000 (00:49 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 31 Mar 2015 21:49:03 +0000 (00:49 +0300)
Send it on its next trip after a short fixed delay instead of waiting
for the next day.

source/libr2c2/timetable.cpp
source/libr2c2/timetable.h

index da01b599876d03c7f18d0e1bae05eb2951e9bff5..5cd4b646bc6e198a0a908e35b8d651529d315d36 100644 (file)
@@ -17,7 +17,9 @@ Timetable::Timetable(Train &t):
        TrainAI(t),
        current_row(rows.end()),
        update_pending(false),
-       sync_to_clock(true)
+       sync_to_clock(true),
+       late_arrival(false),
+       next_depart(rows.end())
 {
        if(!train.get_ai_of_type<AIControl>())
                new AIControl(train);
@@ -89,6 +91,21 @@ void Timetable::tick(const Time::TimeDelta &dt)
 {
        if(update_pending && !train.get_block_allocator().is_active())
                update_route();
+
+       if(next_depart!=rows.end() && next_depart!=current_row && passed_row(*next_depart, dt))
+               late_arrival = true;
+}
+
+bool Timetable::passed_row(const Row &row, const Time::TimeDelta &dt) const
+{
+       const Clock &clock = train.get_layout().get_clock();
+
+       Time::TimeDelta t = clock.get_current_time();
+       if(t<row.time)
+               t += Time::day;
+
+       Time::TimeDelta b = t-dt*clock.get_rate();
+       return b<row.time;
 }
 
 void Timetable::save(list<DataFile::Statement> &st) const
@@ -163,9 +180,15 @@ void Timetable::update_route()
        {
                if(i->type==DEPART)
                {
-                       Time::TimeDelta dt = i->time-clock.get_current_time();
-                       while(dt<Time::zero)
-                               dt += Time::day;
+                       Time::TimeDelta dt;
+                       if(late_arrival)
+                               dt = Time::min;
+                       else
+                       {
+                               dt = i->time-clock.get_current_time();
+                               while(dt<Time::zero)
+                                       dt += Time::day;
+                       }
                        train.ai_message(Message("set-departure-delay", dt/clock.get_rate()));
                }
                else
@@ -176,7 +199,7 @@ void Timetable::update_route()
                }
        }
 
-       list<Row>::iterator next_depart = find_trip(arrive, 0);
+       next_depart = find_trip(arrive, 0);
        if(next_depart==rows.end())
                next_depart = find_trip(rows.begin(), 0);
        if(next_depart!=rows.end())
index 75572490665ae80d66a670cc6f42131cdb68dcf6..67e5ad4ecb8d8684006bf5a1a31d15eaf6711424 100644 (file)
@@ -76,6 +76,8 @@ private:
        std::list<Row>::iterator current_row;
        bool update_pending;
        bool sync_to_clock;
+       bool late_arrival;
+       std::list<Row>::iterator next_depart;
 
 public:
        Timetable(Train &);
@@ -89,7 +91,10 @@ public:
        const Row &get_row(unsigned) const;
 
        virtual void tick(const Msp::Time::TimeDelta &);
+private:
+       bool passed_row(const Row &, const Msp::Time::TimeDelta &) const;
 
+public:
        void save(std::list<Msp::DataFile::Statement> &) const;
 
 private: