X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrainrouteplanner.cpp;h=c5519e45d7db1c581932b7a48e4efe8d5aba8181;hb=524cce124fd8857483c28571c65f0a446d7be865;hp=e75cec9cc6f9921f248dd76f89ecc8af80dc6067;hpb=0443ca05cbd306487131f5f5bd3b91181ae4a733;p=r2c2.git diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index e75cec9..c5519e4 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -1,3 +1,4 @@ +#include "catalogue.h" #include "layout.h" #include "route.h" #include "train.h" @@ -16,7 +17,7 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout) for(map::const_iterator i=trains.begin(); i!=trains.end(); ++i) { TrainRoutingInfo info(*i->second); - if(info.router && info.router->has_destination()) + if(info.router && info.router->get_destination()) routed_trains.push_back(info); } @@ -55,6 +56,9 @@ bool TrainRoutePlanner::update_states(RoutingStep &step) bool changes = false; for(vector::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; @@ -121,13 +125,20 @@ void TrainRoutePlanner::add_steps(RoutingStep &step, unsigned train_index) RoutingStep next(&step); next.advance(dt); TrainRouter &router = *train.info->router; - if(router.is_destination(*train.track) && !router.is_destination(*next_track)) + if(train.waypoint<0 && router.is_destination(*train.track) && !router.is_destination(*next_track)) { next.trains[train_index].state = ARRIVED; new_steps.push_back(next); } else { + if(train.waypoint>=0 && router.is_waypoint(train.waypoint, *train.track) && !router.is_waypoint(train.waypoint, *next_track)) + { + ++next.trains[train_index].waypoint; + if(next.trains[train_index].waypoint>=static_cast(router.get_n_waypoints())) + next.trains[train_index].waypoint = -1; + } + next.trains[train_index].advance_track(0); const TrackType::Endpoint &next_entry_ep = next_track.endpoint(); @@ -135,7 +146,8 @@ void TrainRoutePlanner::add_steps(RoutingStep &step, unsigned train_index) if(next_entry_ep.has_path(i)) { next.trains[train_index].path = i; - new_steps.push_back(next); + if(next.is_viable()) + new_steps.push_back(next); } if(next_entry_ep.paths!=next_track->get_type().get_paths()) @@ -143,7 +155,8 @@ void TrainRoutePlanner::add_steps(RoutingStep &step, unsigned train_index) RoutingStep wait(&step); wait.advance(dt); wait.trains[train_index].state = WAITING; - new_steps.push_back(wait); + if(wait.is_viable()) + new_steps.push_back(wait); } } @@ -157,6 +170,7 @@ void TrainRoutePlanner::create_routes(RoutingStep &goal) { i->route = new Route(i->train->get_layout()); i->route->set_name("Router"); + i->route->set_temporary(true); } for(RoutingStep *i=&goal; i; i=i->prev) @@ -186,9 +200,14 @@ void TrainRoutePlanner::create_routes(RoutingStep &goal) TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t): train(&t), + speed(train->get_maximum_speed()), router(train->get_ai_of_type()), route(0) -{ } +{ + // If no maximum speed is specified, use a sensible default + if(!speed) + speed = 20*train->get_layout().get_catalogue().get_scale(); +} TrainRoutePlanner::OccupiedTrack::OccupiedTrack(Track &t, unsigned p, OccupiedTrack *n): @@ -223,19 +242,20 @@ TrainRoutePlanner::OccupiedTrack::~OccupiedTrack() TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf): info(&inf), occupied_tracks(0), - state(MOVING) + state(MOVING), + delay(info->router->get_departure_delay()), + waypoint(info->router->get_n_waypoints() ? 0 : -1) { const Vehicle *veh = &info->train->get_vehicle(0); // TODO margins - float half_length = veh->get_type().get_length()/2; - TrackOffsetIter track_and_offs = veh->get_track_iter().advance(half_length); + TrackOffsetIter track_and_offs = veh->get_placement().get_position(VehiclePlacement::FRONT_BUFFER); track = track_and_offs.track_iter(); offset = track_and_offs.offset(); path = track->get_active_path(); while(Vehicle *next = veh->get_link(1)) veh = next; - track_and_offs = veh->get_track_iter().advance(-half_length); + track_and_offs = veh->get_placement().get_position(VehiclePlacement::BACK_BUFFER); back_offset = track_and_offs.offset(); TrackIter iter = track_and_offs.track_iter(); @@ -255,7 +275,9 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(const TrainRoutingState occupied_tracks(other.occupied_tracks), offset(other.offset), back_offset(other.back_offset), - state(other.state) + state(other.state), + delay(other.delay), + waypoint(other.waypoint) { ++occupied_tracks->refcount; } @@ -268,8 +290,7 @@ TrainRoutePlanner::TrainRoutingState::~TrainRoutingState() Time::TimeDelta TrainRoutePlanner::TrainRoutingState::get_time_to_next_track() const { - // TODO Consider the speed of the train - return (track->get_type().get_path_length(path)-offset)*Time::sec; + return ((track->get_type().get_path_length(path)-offset)/info->speed)*Time::sec+delay; } bool TrainRoutePlanner::TrainRoutingState::is_occupied(Track &trk) const @@ -328,11 +349,26 @@ void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt) { time += dt; for(vector::iterator i=trains.begin(); i!=trains.end(); ++i) - if(i->state==MOVING) + { + if(i->delay) { - float distance = dt/Time::sec; - i->advance(distance); + 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)); + } +} + +bool TrainRoutePlanner::RoutingStep::is_viable() const +{ + for(vector::const_iterator i=trains.begin(); i!=trains.end(); ++i) + if(i->state==MOVING) + return true; + + return false; } bool TrainRoutePlanner::RoutingStep::is_goal() const