TrainRoutePlanner::~TrainRoutePlanner()
{
- delete thread;
+ if(thread)
+ {
+ thread->join();
+ delete thread;
+ }
}
TrainRoutePlanner::Result TrainRoutePlanner::plan()
{
if(result==PENDING && goal)
{
+ if(thread)
+ {
+ thread->join();
+ delete thread;
+ thread = 0;
+ }
finalize_plan();
- delete thread;
- thread = 0;
}
return result;
if(router)
{
destination = router->get_destination();
- waypoints.resize(router->get_n_waypoints());
- metrics.resize(waypoints.size()+1);
- metrics[0] = &router->get_metric(-1);
- for(unsigned i=0; i<waypoints.size(); ++i)
+ if(destination)
{
- waypoints[i] = &router->get_waypoint(i);
- metrics[i+1] = &router->get_metric(i);
+ waypoints.resize(router->get_n_waypoints());
+ metrics.resize(waypoints.size()+1);
+ metrics[0] = &router->get_metric(-1);
+ for(unsigned i=0; i<waypoints.size(); ++i)
+ {
+ waypoints[i] = &router->get_waypoint(i);
+ metrics[i+1] = &router->get_metric(i);
+ }
+ has_duration = router->get_trip_duration();
}
- has_duration = router->get_trip_duration();
}
// If no maximum speed is specified, use a sensible default
TrainRoutePlanner::RoutingStep::RoutingStep(const RoutingStep *p):
time(p->time),
+ penalty(p->penalty),
cost_estimate(p->cost_estimate),
trains(p->trains),
prev(p)
new_steps.push_back(next);
}
+ new_steps.sort();
+ for(list<RoutingStep>::iterator i=new_steps.begin(); ++i!=new_steps.end(); )
+ {
+ i->penalty += 5*Time::sec;
+ i->update_estimate();
+ }
+
if(next_entry_ep.paths!=next_track->get_type().get_paths())
{
RoutingStep wait(this);
wait.advance(dt);
wait.trains[train_index].state = WAITING;
+ wait.penalty += 15*Time::sec;
+ wait.update_estimate();
if(wait.is_viable())
new_steps.push_back(wait);
}
void TrainRoutePlanner::RoutingStep::update_estimate()
{
- cost_estimate = Time::zero;
+ cost_estimate = penalty;
for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
if(i->remaining_estimate>=0)
cost_estimate += i->wait_time+((i->distance_traveled+i->remaining_estimate)/i->info->speed)*Time::sec;