#include "layout.h"
#include "route.h"
#include "train.h"
+#include "trainroutemetric.h"
#include "trainrouteplanner.h"
#include "trainrouter.h"
#include "vehicle.h"
{
for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
{
- i->route = new Route(i->train->get_layout());
- i->route->set_name("Router");
- i->route->set_temporary(true);
+ Route *route = new Route(i->train->get_layout());
+ route->set_name("Router");
+ route->set_temporary(true);
+ i->routes.push_front(route);
+
+ for(unsigned j=0; j<3; ++j)
+ i->track_history[j] = 0;
}
for(const RoutingStep *i=&goal; i; i=i->prev)
{
for(vector<TrainRoutingState>::const_iterator j=i->trains.begin(); j!=i->trains.end(); ++j)
{
+ Track **history = j->info->track_history;
+ if(j->track.track()==history[0])
+ continue;
+
if(j->state==WAITING || j->state==BLOCKED)
j->info->waits.push_front(&*j);
- j->info->route->add_track(*j->track);
+
+ Route *route = j->info->routes.front();
+ if(route->has_track(*j->track))
+ {
+ route = new Route(j->info->train->get_layout());
+ route->set_name("Router");
+ route->set_temporary(true);
+ for(unsigned k=2; k>0; --k)
+ route->add_track(*history[k]);
+ j->info->routes.push_front(route);
+ }
+
+ if(history[0])
+ route->add_track(*history[0]);
+ for(unsigned k=2; k>0; --k)
+ history[k] = history[k-1];
+ history[0] = j->track.track();
}
}
for(vector<TrainRoutingInfo>::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i)
{
- i->router->set_route(i->route);
+ for(list<Route *>::iterator j=i->routes.begin(); j!=i->routes.end(); ++j)
+ i->router->add_route(**j);
const TrainRoutingState *current_wait = 0;
for(list<const TrainRoutingState *>::const_iterator j=i->waits.begin(); j!=i->waits.end(); ++j)
if(!current_wait || (*j)->track.track()!=current_wait->track.track())
TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t):
train(&t),
speed(train->get_maximum_speed()),
- router(train->get_ai_of_type<TrainRouter>()),
- route(0)
+ router(train->get_ai_of_type<TrainRouter>())
{
// If no maximum speed is specified, use a sensible default
if(!speed)
break;
iter = iter.next();
}
+
+ update_estimate();
}
TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState &other):
state(other.state),
delay(other.delay),
waypoint(other.waypoint),
+ distance_traveled(other.distance_traveled),
+ remaining_estimate(other.remaining_estimate),
+ wait_time(other.wait_time),
blocked_by(other.blocked_by)
{
++occupied_tracks->refcount;
}
--occupied_tracks->n_tracks;
}
+
+ distance_traveled += distance;
+ remaining_estimate -= distance;
+}
+
+void TrainRoutePlanner::TrainRoutingState::advance(const Time::TimeDelta &dt)
+{
+ if(delay>=dt)
+ {
+ delay -= dt;
+ return;
+ }
+
+ float secs = dt/Time::sec;
+ if(delay)
+ {
+ secs -= delay/Time::sec;
+ delay = Time::zero;
+ }
+
+ if(state==MOVING)
+ advance(info->speed*secs);
+ else if(state!=ARRIVED)
+ wait_time += secs*Time::sec;
}
void TrainRoutePlanner::TrainRoutingState::advance_track(unsigned next_path)
offset = 0;
}
+void TrainRoutePlanner::TrainRoutingState::update_estimate()
+{
+ TrackIter iter = track.reverse(path);
+ float distance = info->router->get_metric(waypoint).get_distance_from(*iter.track(), iter.entry());
+ distance += track->get_type().get_path_length(path)-offset;
+ remaining_estimate = distance;
+}
+
TrainRoutePlanner::RoutingStep::RoutingStep():
prev(0)
TrainRoutePlanner::RoutingStep::RoutingStep(const RoutingStep *p):
time(p->time),
+ cost_estimate(p->cost_estimate),
trains(p->trains),
prev(p)
{ }
if(next_entry_ep.has_path(i))
{
train.path = i;
+ train.update_estimate();
+ next.update_estimate();
if(next.is_viable())
new_steps.push_back(next);
}
{
time += dt;
for(vector<TrainRoutingState>::iterator i=trains.begin(); i!=trains.end(); ++i)
- {
- if(i->delay)
- {
- i->delay -= dt;
- if(i->delay>Time::zero)
- continue;
- i->delay = Time::zero;
- }
- else if(i->state==MOVING)
- i->advance(i->info->speed*(dt/Time::sec));
- }
+ i->advance(dt);
+}
+
+void TrainRoutePlanner::RoutingStep::update_estimate()
+{
+ cost_estimate = Time::zero;
+ 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;
}
bool TrainRoutePlanner::RoutingStep::is_viable() const
{
+ for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+ if(i->remaining_estimate<0)
+ return false;
+
for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
if(i->state==MOVING)
return true;
bool TrainRoutePlanner::RoutingStep::operator<(const RoutingStep &other) const
{
- return time<other.time;
+ return cost_estimate<other.cost_estimate;
}
} // namespace R2C2