]> git.tdb.fi Git - r2c2.git/commitdiff
Support routing through multiple waypoints
authorMikko Rasa <tdb@tdb.fi>
Wed, 26 Mar 2014 00:12:06 +0000 (02:12 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 26 Mar 2014 00:12:06 +0000 (02:12 +0200)
Currently the result must be valid as a single route.  No crossing one's
own path or doubling back from a reversing loop.

source/libr2c2/trainrouteplanner.cpp
source/libr2c2/trainrouteplanner.h
source/libr2c2/trainrouter.cpp
source/libr2c2/trainrouter.h

index a7bb13ee13fa3fc7c0c8852796e4e4ec8266c7d8..b98b37e7ee1b2c9b41d6b4a0c41a7f545052f03b 100644 (file)
@@ -121,13 +121,20 @@ void TrainRoutePlanner::add_steps(RoutingStep &step, unsigned train_index)
        RoutingStep next(&step);
        next.advance(dt);
        TrainRouter &router = *train.info->router;
-       if(router.is_destination(*train.track) && !router.is_destination(*next_track))
+       if(train.waypoint<0 && router.is_destination(*train.track) && !router.is_destination(*next_track))
        {
                next.trains[train_index].state = ARRIVED;
                new_steps.push_back(next);
        }
        else
        {
+               if(train.waypoint>=0 && router.is_waypoint(train.waypoint, *train.track) && !router.is_waypoint(train.waypoint, *next_track))
+               {
+                       ++next.trains[train_index].waypoint;
+                       if(next.trains[train_index].waypoint>=static_cast<int>(router.get_n_waypoints()))
+                               next.trains[train_index].waypoint = -1;
+               }
+
                next.trains[train_index].advance_track(0);
 
                const TrackType::Endpoint &next_entry_ep = next_track.endpoint();
@@ -225,7 +232,8 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
        info(&inf),
        occupied_tracks(0),
        state(MOVING),
-       delay(info->router->get_departure_delay())
+       delay(info->router->get_departure_delay()),
+       waypoint(info->router->get_n_waypoints() ? 0 : -1)
 {
        const Vehicle *veh = &info->train->get_vehicle(0);
        // TODO margins
@@ -256,7 +264,8 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState
        occupied_tracks(other.occupied_tracks),
        offset(other.offset),
        back_offset(other.back_offset),
-       state(other.state)
+       state(other.state),
+       waypoint(other.waypoint)
 {
        ++occupied_tracks->refcount;
 }
index 19fb329bade0a2745e30a6c995244035ad77ac96..21da3bbd2b16d770987da0267e4432c5bfcd6c5b 100644 (file)
@@ -60,6 +60,7 @@ private:
                float back_offset;
                TrainState state;
                Msp::Time::TimeDelta delay;
+               int waypoint;
 
                TrainRoutingState(TrainRoutingInfo &);
                TrainRoutingState(const TrainRoutingState &);
index b2993ee74ba47d0620234719016ff465f06b7aeb..da3b7d515fe253b79db3412ea0e98d2bd062d6ea 100644 (file)
@@ -61,6 +61,7 @@ bool TrainRouter::set_route(const Route *r)
        if(!r)
        {
                destination = 0;
+               waypoints.clear();
        }
 
        train.refresh_blocks_from(*fncb);
@@ -101,6 +102,20 @@ bool TrainRouter::is_destination(Track &track) const
                return false;
 }
 
+void TrainRouter::add_waypoint(const TrackChain &wp)
+{
+       waypoints.push_back(&wp);
+       update_pending = true;
+}
+
+bool TrainRouter::is_waypoint(unsigned index, Track &track) const
+{
+       if(index>=waypoints.size())
+               throw out_of_range("TrainRouter::is_waypoint");
+
+       return waypoints[index]->has_track(track);
+}
+
 void TrainRouter::set_departure_delay(const Time::TimeDelta &d)
 {
        delay = d;
@@ -125,6 +140,13 @@ void TrainRouter::message(const Message &msg)
                else
                        set_destination(*msg.value.value<const TrackChain *>());
        }
+       else if(msg.type=="add-waypoint")
+       {
+               if(msg.value.check_type<TrackChain *>())
+                       add_waypoint(*msg.value.value<TrackChain *>());
+               else
+                       add_waypoint(*msg.value.value<const TrackChain *>());
+       }
        else if(msg.type=="set-departure-delay")
                set_departure_delay(msg.value.value<Time::TimeDelta>());
 }
index c518522590bc4fdda11cf330b8a5532ffc4d7d56..a2a73aa94d9337d083346e6e6688f8e47dcb8b56 100644 (file)
@@ -41,6 +41,7 @@ private:
        RouteList routes;
        unsigned arriving;
        const TrackChain *destination;
+       std::vector<const TrackChain *> waypoints;
        std::list<Wait> waits;
        Msp::Time::TimeDelta delay;
 
@@ -59,6 +60,9 @@ public:
        void set_destination(const TrackChain &);
        const TrackChain *get_destination() const { return destination; }
        bool is_destination(Track &) const;
+       void add_waypoint(const TrackChain &);
+       unsigned get_n_waypoints() const { return waypoints.size(); }
+       bool is_waypoint(unsigned, Track &) const;
        void set_departure_delay(const Msp::Time::TimeDelta &);
        const Msp::Time::TimeDelta &get_departure_delay() const { return delay; }