return routes.front();
}
+void TrainRouter::use_planned_route()
+{
+ if(!planner || planner->get_result()!=TrainRoutePlanner::COMPLETE)
+ return;
+
+ const list<Route *> &planned_routes = planner->get_routes_for(train);
+
+ routes.clear();
+ routes.insert(routes.end(), planned_routes.begin(), planned_routes.end());
+ create_lead_route();
+
+ sequence_points = planner->get_sequence_for(train);
+ current_sequence = 0;
+ sequence_check_pending = false;
+
+ route_changed();
+}
+
void TrainRouter::route_changed()
{
BlockIter fncb = train.get_first_noncritical_block();
start_planning(train.get_layout());
if(planner && planner->check()!=TrainRoutePlanner::PENDING)
- {
- waypoints_changed = false;
- if(planner->get_result()==TrainRoutePlanner::COMPLETE)
- {
- const list<Route *> &planned_routes = planner->get_routes_for(train);
-
- routes.clear();
- routes.insert(routes.end(), planned_routes.begin(), planned_routes.end());
- create_lead_route();
-
- sequence_points = planner->get_sequence_for(train);
- current_sequence = 0;
- sequence_check_pending = false;
-
- route_changed();
- }
- planner = 0;
- }
+ apply_plan(train.get_layout(), *planner);
if(sequence_check_pending)
{
return true;
}
-void TrainRouter::start_planning(Layout &layout)
+void TrainRouter::get_routers(Layout &layout, vector<TrainRouter *> &routers)
{
- vector<TrainRouter *> routers;
const map<unsigned, Train *> &trains = layout.get_trains();
routers.reserve(trains.size());
for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
if(TrainRouter *router = i->second->get_ai_of_type<TrainRouter>())
routers.push_back(router);
+}
+
+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)
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)
+{
+ vector<TrainRouter *> routers;
+ get_routers(layout, routers);
+
+ for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
+ if((*i)->planner.get()==&planner)
+ {
+ (*i)->use_planned_route();
+ (*i)->planner = 0;
+ }
+}
+
TrainRouter::SequencePoint::SequencePoint(Block &b, unsigned o):
block(&b),
const Route *get_route() const;
unsigned get_current_sequence() const { return current_sequence; }
private:
+ void use_planned_route();
void route_changed();
public:
bool is_valid_for_track(const Route &, const TrackIter &) const;
bool advance_to_track(RouteList::iterator &, const TrackIter &);
+ static void get_routers(Layout &, std::vector<TrainRouter *> &);
static void start_planning(Layout &);
+ static void apply_plan(Layout &, TrainRoutePlanner &);
};
} // namespace R2C2