if(update_states(*i))
{
- int next_train = find_next_train(*i);
+ int next_train = i->find_next_train();
if(next_train>=0)
add_steps(*i, next_train);
}
bool TrainRoutePlanner::update_states(RoutingStep &step)
{
RoutingStep next(&step);
- bool changes = false;
- for(vector<TrainRoutingState>::iterator i=next.trains.begin(); i!=next.trains.end(); ++i)
- {
- if(i->state==ARRIVED)
- continue;
-
- TrainState old_state = i->state;
- if(i->state==BLOCKED)
- i->state = MOVING;
-
- TrackIter next_track = i->track.next(i->path);
- if(!next_track)
- return false;
-
- for(vector<TrainRoutingState>::iterator j=next.trains.begin(); j!=next.trains.end(); ++j)
- if(j!=i)
- {
- if(j->track.track()==next_track.track())
- {
- unsigned other_exit = j->track.reverse(j->path).entry();
- if(next_track.entry()==other_exit)
- return false;
- }
- else if(!j->is_occupied(*next_track))
- continue;
+ if(!next.update_states())
+ return true;
+ if(next.check_deadlocks())
+ return false;
- i->state = BLOCKED;
- }
-
- if(i->state!=old_state)
- changes = true;
- }
-
- if(changes)
- {
- list<RoutingStep>::iterator i;
- for(i=steps.begin(); (i!=steps.end() && !(next<*i)); ++i) ;
- steps.insert(i, next);
- }
-
- return !changes;
-}
+ list<RoutingStep>::iterator i;
+ for(i=steps.begin(); (i!=steps.end() && !(next<*i)); ++i) ;
+ steps.insert(i, next);
-int TrainRoutePlanner::find_next_train(RoutingStep &step)
-{
- Time::TimeDelta min_dt;
- int next_train = -1;
- for(unsigned i=0; i<step.trains.size(); ++i)
- if(step.trains[i].state==MOVING)
- {
- Time::TimeDelta dt = step.trains[i].get_time_to_next_track();
- if(dt<min_dt || next_train<0)
- {
- min_dt = dt;
- next_train = i;
- }
- }
-
- return next_train;
+ return false;
}
void TrainRoutePlanner::add_steps(RoutingStep &step, unsigned train_index)
occupied_tracks(0),
state(MOVING),
delay(info->router->get_departure_delay()),
- waypoint(info->router->get_n_waypoints() ? 0 : -1)
+ waypoint(info->router->get_n_waypoints() ? 0 : -1),
+ blocked_by(-1)
{
const Vehicle *veh = &info->train->get_vehicle(0);
// TODO margins
back_offset(other.back_offset),
state(other.state),
delay(other.delay),
- waypoint(other.waypoint)
+ waypoint(other.waypoint),
+ blocked_by(other.blocked_by)
{
++occupied_tracks->refcount;
}
prev(p)
{ }
+bool TrainRoutePlanner::RoutingStep::update_states()
+{
+ bool changes = false;
+ for(vector<TrainRoutingState>::iterator i=trains.begin(); i!=trains.end(); ++i)
+ {
+ if(i->state==ARRIVED)
+ continue;
+
+ TrainState old_state = i->state;
+
+ TrackIter next_track = i->track.next(i->path);
+ if(next_track)
+ {
+ i->blocked_by = get_occupant(*next_track);
+ if(i->blocked_by>=0)
+ i->state = BLOCKED;
+ else if(i->state==BLOCKED)
+ i->state = MOVING;
+ }
+ else
+ i->state = BLOCKED;
+
+ if(i->state!=old_state)
+ changes = true;
+ }
+
+ return changes;
+}
+
+bool TrainRoutePlanner::RoutingStep::check_deadlocks() const
+{
+ for(unsigned i=0; i<trains.size(); ++i)
+ {
+ const TrainRoutingState &train = trains[i];
+ if(train.state!=BLOCKED)
+ continue;
+
+ if(train.blocked_by<0)
+ return true;
+
+ if(trains[train.blocked_by].blocked_by==static_cast<int>(i))
+ return true;
+ }
+
+ return false;
+}
+
+int TrainRoutePlanner::RoutingStep::get_occupant(Track &track) const
+{
+ for(unsigned i=0; i<trains.size(); ++i)
+ if(trains[i].is_occupied(track))
+ return i;
+
+ return -1;
+}
+
+int TrainRoutePlanner::RoutingStep::find_next_train() const
+{
+ Time::TimeDelta min_dt;
+ int next_train = -1;
+ for(unsigned i=0; i<trains.size(); ++i)
+ if(trains[i].state==MOVING)
+ {
+ Time::TimeDelta dt = trains[i].get_time_to_next_track();
+ if(dt<min_dt || next_train<0)
+ {
+ min_dt = dt;
+ next_train = i;
+ }
+ }
+
+ return next_train;
+}
+
void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt)
{
time += dt;
TrainState state;
Msp::Time::TimeDelta delay;
int waypoint;
+ int blocked_by;
TrainRoutingState(TrainRoutingInfo &);
TrainRoutingState(const TrainRoutingState &);
RoutingStep();
RoutingStep(RoutingStep *);
+ bool update_states();
+ bool check_deadlocks() const;
+ int get_occupant(Track &) const;
+ int find_next_train() const;
void advance(const Msp::Time::TimeDelta &);
bool is_viable() const;
bool is_goal() const;
void plan();
private:
bool update_states(RoutingStep &);
- int find_next_train(RoutingStep &);
void add_steps(RoutingStep &, unsigned);
void add_waiting_step(RoutingStep &, unsigned);
void add_steps(RoutingStep &, TrainRoutingState &train);