X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=inline;f=source%2Flibr2c2%2Ftrainrouteplanner.cpp;h=1c7d996c5773dac66228e21be3632c31e64620a7;hb=776b39dfb0896aa563a220bdbf0b044425629917;hp=a7bb13ee13fa3fc7c0c8852796e4e4ec8266c7d8;hpb=a7b4305930090851623da7e02e21650510ce7dae;p=r2c2.git diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index a7bb13e..1c7d996 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" @@ -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(); @@ -187,9 +198,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): @@ -225,7 +241,8 @@ TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf): info(&inf), occupied_tracks(0), state(MOVING), - delay(info->router->get_departure_delay()) + delay(info->router->get_departure_delay()), + waypoint(info->router->get_n_waypoints() ? 0 : -1) { const Vehicle *veh = &info->train->get_vehicle(0); // TODO margins @@ -256,7 +273,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; } @@ -269,8 +288,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+delay; + return ((track->get_type().get_path_length(path)-offset)/info->speed)*Time::sec+delay; } bool TrainRoutePlanner::TrainRoutingState::is_occupied(Track &trk) const @@ -338,10 +356,7 @@ void TrainRoutePlanner::RoutingStep::advance(const Time::TimeDelta &dt) i->delay = Time::zero; } else if(i->state==MOVING) - { - float distance = dt/Time::sec; - i->advance(distance); - } + i->advance(i->info->speed*(dt/Time::sec)); } }