#include <msp/core/maputils.h>
+#include <msp/time/utils.h>
#include "catalogue.h"
#include "layout.h"
#include "route.h"
TrainRoutePlanner::TrainRoutePlanner(Layout &layout):
goal(0),
+ timeout(10*Time::sec),
result(PENDING),
thread(0)
{
}
}
+void TrainRoutePlanner::set_timeout(const Time::TimeDelta &t)
+{
+ timeout = t;
+}
+
TrainRoutePlanner::Result TrainRoutePlanner::plan()
{
prepare_plan();
void TrainRoutePlanner::create_plan()
{
+ Time::TimeStamp timeout_stamp = Time::now()+timeout;
+ unsigned count = 0;
while(!queue.empty())
{
const RoutingStep &step = get_step();
}
add_steps(step);
+
+ if(++count>=1000)
+ {
+ if(Time::now()>timeout_stamp)
+ break;
+ count = 0;
+ }
}
result = FAILED;
speed(train->get_maximum_speed()),
first_noncritical(train->get_last_critical_block().next().block()),
router(train->get_ai_of_type<TrainRouter>()),
- waypoints(router ? router->get_n_waypoints() : 0),
has_duration(false)
{
- if(!waypoints.empty())
+ if(unsigned n_wps = router->get_n_waypoints())
{
- metrics.resize(waypoints.size());
- for(unsigned i=0; i<waypoints.size(); ++i)
+ waypoints.reserve(n_wps),
+ metrics.reserve(n_wps);
+ for(unsigned i=0; i<n_wps; ++i)
{
- waypoints[i] = &router->get_waypoint(i);
- metrics[i] = &router->get_metric(i);
+ waypoints.push_back(router->get_waypoint(i));
+ metrics.push_back(&router->get_metric(i));
}
has_duration = router->get_trip_duration();
}
{
TrackIter next_track = track.next(path);
- const TrackChain *wp_chain = info->waypoints[waypoint];
- if(wp_chain->has_track(*track) && !wp_chain->has_track(*next_track))
- {
- if(waypoint+1<info->waypoints.size())
- ++waypoint;
- else
+ const TrainRouter::Waypoint &wp = info->waypoints[waypoint];
+ if(wp.chain->has_track(*track) && !wp.chain->has_track(*next_track))
+ if(wp.direction==TrackChain::UNSPECIFIED || track==wp.chain->iter_for(*track, wp.direction))
{
- state = ARRIVED;
- return true;
+ if(waypoint+1<info->waypoints.size())
+ ++waypoint;
+ else
+ {
+ state = ARRIVED;
+ return true;
+ }
}
- }
- if(info->first_noncritical->has_track(*track))
+ if(info->first_noncritical->has_track(*next_track))
critical = false;
return false;
void TrainRoutePlanner::RoutingStep::create_successors(list<RoutingStep> &new_steps) const
{
RoutingStep next(this);
- if(next.update_states())
- {
- if(next.check_deadlocks())
- return;
-
- new_steps.push_back(next);
+ if(next.update_states() && next.check_deadlocks())
return;
- }
int train_index = find_next_train();
if(train_index<0)
const TrackType::Endpoint &entry_ep = train.track.endpoint();
if(train.critical)
{
- train.path = train.track->get_type().coerce_path(train.track.entry(), train.track->get_active_path());
- train.update_estimate();
- next.update_estimate();
- if(next.is_viable())
- new_steps.push_back(next);
+ unsigned critical_path = train.track->get_type().coerce_path(train.track.entry(), train.track->get_active_path());
+ create_successor(next, train_index, critical_path, new_steps);
}
else
{
for(unsigned i=0; entry_ep.paths>>i; ++i)
if(entry_ep.has_path(i))
- {
- train.path = i;
- train.update_estimate();
- next.update_estimate();
- if(next.is_viable())
- new_steps.push_back(next);
- }
+ create_successor(next, train_index, i, new_steps);
}
new_steps.sort();
}
}
+void TrainRoutePlanner::RoutingStep::create_successor(RoutingStep &next, unsigned train_index, unsigned path, list<RoutingStep> &new_steps)
+{
+ TrainRoutingState &train = next.trains[train_index];
+
+ train.path = path;
+ train.update_estimate();
+ next.update_estimate();
+ if(next.is_viable())
+ new_steps.push_back(next);
+}
+
bool TrainRoutePlanner::RoutingStep::update_states()
{
bool changes = false;