+void TrainRouter::start_planning(Layout &layout)
+{
+ vector<TrainRouter *> routers;
+ get_routers(layout, routers);
+
+ for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
+ if((*i)->metrics_stale)
+ (*i)->create_metrics();
+
+ RefPtr<TrainRoutePlanner> planner = new TrainRoutePlanner(layout);
+ for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
+ {
+ (*i)->waypoints_changed = false;
+ (*i)->planner = planner;
+ }
+
+ planner->plan_async();
+}
+
+void TrainRouter::apply_plan(Layout &layout, TrainRoutePlanner &planner)
+{
+ if(planner.get_result()==TrainRoutePlanner::FAILED)
+ layout.emergency(0, "Route planning failed");
+
+ vector<TrainRouter *> routers;
+ get_routers(layout, routers, &planner);
+
+ /* Clear sequence counters first to avoid inconsistent state while applying
+ the plan. */
+ for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
+ (*i)->current_sequence = 0;
+
+ for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
+ {
+ (*i)->use_planned_route();
+ (*i)->planner = 0;
+ }