if(!sequence_points.empty())
{
const SequencePoint &sp = sequence_points.front();
- if(sp.block==fncb.block() && sp.preceding_train)
+ if(sp.block==fncb.block() && !sp.is_cleared())
state = SEQUENCE_CHECK_PENDING;
}
}
SequencePoint &sp = sequence_points.front();
if(sp.block==&track->get_block() && !sp.is_cleared())
{
- state = WAITING_FOR_SEQUENCE;
+ state = SEQUENCE_CHECK_PENDING;
train.stop_at(&block);
}
}
return false;
}
-void TrainRouter::get_routers(Layout &layout, vector<TrainRouter *> &routers)
+void TrainRouter::get_routers(Layout &layout, vector<TrainRouter *> &routers, TrainRoutePlanner *planner)
{
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);
+ if(!planner || router->planner.get()==planner)
+ routers.push_back(router);
}
void TrainRouter::start_planning(Layout &layout)
layout.emergency(0, "Route planning failed");
vector<TrainRouter *> routers;
- get_routers(layout, 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)
- if((*i)->planner.get()==&planner)
- {
- (*i)->use_planned_route();
- (*i)->planner = 0;
- }
+ (*i)->current_sequence = 0;
+
+ for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
+ {
+ (*i)->use_planned_route();
+ (*i)->planner = 0;
+ }
}
bool create_lead_route();
bool advance_to_track(RouteList::iterator &, const TrackIter &);
- static void get_routers(Layout &, std::vector<TrainRouter *> &);
+ static void get_routers(Layout &, std::vector<TrainRouter *> &, TrainRoutePlanner * = 0);
static void start_planning(Layout &);
static void apply_plan(Layout &, TrainRoutePlanner &);
};