]> git.tdb.fi Git - r2c2.git/commitdiff
Unoccupy destination during planning when the train has departed again
authorMikko Rasa <tdb@tdb.fi>
Tue, 3 Feb 2015 23:25:32 +0000 (01:25 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 4 Feb 2015 09:14:29 +0000 (11:14 +0200)
source/libr2c2/timetable.cpp
source/libr2c2/timetable.h
source/libr2c2/trainrouteplanner.cpp
source/libr2c2/trainrouteplanner.h
source/libr2c2/trainrouter.cpp
source/libr2c2/trainrouter.h

index 42414f63d0baf3ecfe14fcbda9afb4de8c3669e3..93f61c50608d0b56839bf6ba59af8ba6b4410c47 100644 (file)
@@ -1,3 +1,4 @@
+#include <algorithm>
 #include <msp/strings/format.h>
 #include "aicontrol.h"
 #include "clock.h"
@@ -122,28 +123,33 @@ 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;; )
+       list<Row>::iterator arrive;
+       list<Row>::iterator depart = find_trip(current_row, &arrive);
+       if(depart==rows.end())
        {
-               if(i==rows.end())
-               {
-                       i = rows.begin();
-                       depart = rows.end();
-               }
-
-               if(i->type==DEPART)
-                       depart = i;
-               else if(depart!=rows.end() && i->type==ARRIVE)
-                       break;
-
-               ++i;
-               if(i==current_row)
+               depart = find_trip(rows.begin(), &arrive);
+               if(depart==rows.end())
                {
                        current_row = rows.end();
                        return;
@@ -152,6 +158,8 @@ 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)
        {
@@ -162,7 +170,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 +179,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)
index da16d3c0d488c23cffc7d3879fcf2c367e611141..f3d62399e09b11a1e3d03843470229e957bb1e6b 100644 (file)
@@ -56,6 +56,15 @@ public:
                void save(std::list<Msp::DataFile::Statement> &) const;
        };
 
+       struct RowTypeMatch
+       {
+               RowType type;
+
+               RowTypeMatch(RowType t): type(t) { }
+
+               bool operator()(const Row &r) const { return r.type==type; }
+       };
+
        sigc::signal<void, unsigned, const Row &> signal_row_added;
        sigc::signal<void, unsigned, const Row &> signal_row_modified;
        sigc::signal<void, unsigned> signal_row_removed;
@@ -82,6 +91,7 @@ public:
 
 private:
        void check_update(std::list<Row>::const_iterator);
+       std::list<Row>::iterator find_trip(const std::list<Row>::iterator &, std::list<Row>::iterator *);
        void update_route();
        void event(TrainAI &, const Message &);
        void record_time();
index 30d9f5e482ee054aa4a5dee7df770abbddd743a7..99700699c452a21a81ef51bdad8dd25b5cab8e5e 100644 (file)
@@ -248,6 +248,7 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
        occupied_tracks(0),
        state(MOVING),
        delay(info->router->get_departure_delay()),
+       duration(info->router->get_trip_duration()),
        waypoint(info->router->get_n_waypoints() ? 0 : -1),
        blocked_by(-1)
 {
@@ -284,6 +285,7 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState
        back_offset(other.back_offset),
        state(other.state),
        delay(other.delay),
+       duration(other.duration),
        waypoint(other.waypoint),
        distance_traveled(other.distance_traveled),
        remaining_estimate(other.remaining_estimate),
@@ -306,6 +308,9 @@ Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_next_track() c
 
 bool TrainRoutePlanner::TrainRoutingState::is_occupying(Track &trk) const
 {
+       if(state==ARRIVED && !duration && info->router->get_trip_duration())
+               return false;
+
        OccupiedTrack *occ = occupied_tracks;
        for(unsigned n=occ->n_tracks; n>0; --n, occ=occ->next)
                if(occ->track==&trk)
@@ -373,6 +378,9 @@ void TrainRoutePlanner::TrainRoutingState::advance(const Time::TimeDelta &dt)
                delay = Time::zero;
        }
 
+       if(duration)
+               duration = max(duration-secs*Time::sec, Time::zero);
+
        if(state==MOVING)
                advance(info->speed*secs);
        else if(state!=ARRIVED)
index 153eda0532dfc369d48f34aabc77fff068a3ec51..c4850d46a61c00185f9630b8259885d0582b5f5e 100644 (file)
@@ -71,6 +71,7 @@ private:
                float back_offset;
                TrainState state;
                Msp::Time::TimeDelta delay;
+               Msp::Time::TimeDelta duration;
                int waypoint;
                float distance_traveled;
                float remaining_estimate;
index bbb87eae116eb56326c911900eec1f0d09fe9303..9245774f605309d597591fa01e95eb60f8b20c38 100644 (file)
@@ -181,6 +181,11 @@ void TrainRouter::set_departure_delay(const Time::TimeDelta &d)
        destination_changed = true;
 }
 
+void TrainRouter::set_trip_duration(const Time::TimeDelta &d)
+{
+       duration = d;
+}
+
 void TrainRouter::message(const Message &msg)
 {
        if(msg.type=="set-route")
@@ -208,6 +213,8 @@ void TrainRouter::message(const Message &msg)
        }
        else if(msg.type=="set-departure-delay")
                set_departure_delay(msg.value.value<Time::TimeDelta>());
+       else if(msg.type=="set-trip-duration")
+               set_trip_duration(msg.value.value<Time::TimeDelta>());
 }
 
 void TrainRouter::tick(const Time::TimeDelta &dt)
@@ -215,9 +222,14 @@ void TrainRouter::tick(const Time::TimeDelta &dt)
        if(delay)
        {
                delay -= dt;
-               if(delay<=Time::zero)
+               if(delay<Time::zero)
+               {
+                       duration = max(duration+delay, Time::zero);
                        delay = Time::zero;
+               }
        }
+       else if(duration)
+               duration = max(duration-dt, Time::zero);
 
        if(destination_changed && !planner)
                start_planning(train.get_layout());
index 4f51b3e107878b07e37cd86fa38d7509d8527208..91b7bac1a4e0730afb2e7b278911f4d74b4c5334 100644 (file)
@@ -66,6 +66,7 @@ private:
        unsigned current_sequence;
        bool sequence_check_pending;
        Msp::Time::TimeDelta delay;
+       Msp::Time::TimeDelta duration;
        Msp::RefPtr<TrainRoutePlanner> planner;
 
 public:
@@ -91,6 +92,8 @@ public:
        const TrainRouteMetric &get_metric(int = -1) const;
        void set_departure_delay(const Msp::Time::TimeDelta &);
        const Msp::Time::TimeDelta &get_departure_delay() const { return delay; }
+       void set_trip_duration(const Msp::Time::TimeDelta &);
+       const Msp::Time::TimeDelta &get_trip_duration() const { return duration; }
 
        virtual void message(const Message &);
        virtual void tick(const Msp::Time::TimeDelta &);