mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-29 17:34:58 +03:00
make cars end at borders properly by slightly extending the protocol for routers
This commit is contained in:
parent
7214cd43d2
commit
62182e6736
@ -75,7 +75,6 @@ pub enum Action {
|
||||
Continue(Acceleration, Vec<Request>),
|
||||
// TODO Get rid of this one
|
||||
VanishAtDeadEnd,
|
||||
VanishAtBorder,
|
||||
}
|
||||
|
||||
impl Car {
|
||||
@ -204,6 +203,10 @@ impl Car {
|
||||
if dist_to_lookahead >= dist_from_stop {
|
||||
let should_stop = if maybe_stop_early.is_some() {
|
||||
true
|
||||
} else if current_router.should_vanish_at_border() {
|
||||
// Don't limit acceleration, but also don't vanish before physically
|
||||
// reaching the border.
|
||||
break;
|
||||
} else {
|
||||
let req =
|
||||
Request::for_car(self.id, current_router.next_step_as_turn().unwrap());
|
||||
@ -259,6 +262,7 @@ impl Car {
|
||||
Ok(Action::Continue(safe_accel, requests))
|
||||
}
|
||||
|
||||
// If true, vanish at the border
|
||||
fn step_continue(
|
||||
&mut self,
|
||||
events: &mut Vec<Event>,
|
||||
@ -266,7 +270,7 @@ impl Car {
|
||||
accel: Acceleration,
|
||||
map: &Map,
|
||||
intersections: &mut IntersectionSimState,
|
||||
) -> Result<(), Error> {
|
||||
) -> Result<bool, Error> {
|
||||
let (dist, new_speed) = kinematics::results_of_accel_for_one_tick(self.speed, accel);
|
||||
self.dist_along += dist;
|
||||
self.speed = new_speed;
|
||||
@ -308,6 +312,9 @@ impl Car {
|
||||
self.on,
|
||||
));
|
||||
|
||||
if router.should_vanish_at_border() {
|
||||
return Ok(true);
|
||||
}
|
||||
match router.finished_step(self.on) {
|
||||
PathStep::Lane(id) => {
|
||||
self.on = Traversable::Lane(id);
|
||||
@ -336,7 +343,7 @@ impl Car {
|
||||
self.on,
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
@ -633,31 +640,34 @@ impl DrivingSimState {
|
||||
}
|
||||
}
|
||||
Action::Continue(accel, ref requests) => {
|
||||
let c = self.cars.get_mut(&id).unwrap();
|
||||
c.step_continue(
|
||||
events,
|
||||
self.routers.get_mut(&id).unwrap(),
|
||||
accel,
|
||||
map,
|
||||
intersections,
|
||||
)?;
|
||||
// TODO maybe just return TurnID
|
||||
for req in requests {
|
||||
// Note this is idempotent and does NOT grant the request.
|
||||
// TODO should we check that the car is currently the lead vehicle?
|
||||
// intersection is assuming that! or relax that assumption.
|
||||
intersections.submit_request(req.clone());
|
||||
let done = {
|
||||
let c = self.cars.get_mut(&id).unwrap();
|
||||
c.step_continue(
|
||||
events,
|
||||
self.routers.get_mut(&id).unwrap(),
|
||||
accel,
|
||||
map,
|
||||
intersections,
|
||||
)?
|
||||
};
|
||||
if done {
|
||||
self.cars.remove(&id);
|
||||
self.routers.remove(&id);
|
||||
vanished_at_border.push(*id);
|
||||
} else {
|
||||
// TODO maybe just return TurnID
|
||||
for req in requests {
|
||||
// Note this is idempotent and does NOT grant the request.
|
||||
// TODO should we check that the car is currently the lead vehicle?
|
||||
// intersection is assuming that! or relax that assumption.
|
||||
intersections.submit_request(req.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
Action::VanishAtDeadEnd => {
|
||||
self.cars.remove(&id);
|
||||
self.routers.remove(&id);
|
||||
}
|
||||
Action::VanishAtBorder => {
|
||||
self.cars.remove(&id);
|
||||
self.routers.remove(&id);
|
||||
vanished_at_border.push(*id);
|
||||
}
|
||||
}
|
||||
}
|
||||
*current_agent = None;
|
||||
|
@ -65,17 +65,7 @@ impl Router {
|
||||
transit_sim: &mut TransitSimState,
|
||||
rng: &mut XorShiftRng,
|
||||
) -> Option<Action> {
|
||||
if self.path.isnt_last_step() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// TODO Hack. Makes cars vanish too early. Hard to express early termination in lookahead.
|
||||
if self.goal == Goal::EndAtBorder {
|
||||
return Some(Action::VanishAtBorder);
|
||||
}
|
||||
|
||||
// Not at the end yet?
|
||||
if view.speed > kinematics::EPSILON_SPEED {
|
||||
if self.path.isnt_last_step() || view.speed > kinematics::EPSILON_SPEED {
|
||||
return None;
|
||||
}
|
||||
|
||||
@ -103,7 +93,7 @@ impl Router {
|
||||
return Some(Action::Continue(0.0 * si::MPS2, Vec::new()));
|
||||
}
|
||||
}
|
||||
// Handled earlier
|
||||
// Don't stop at the border node; plow through
|
||||
Goal::EndAtBorder => {}
|
||||
}
|
||||
None
|
||||
@ -153,6 +143,11 @@ impl Router {
|
||||
self.path.current_step()
|
||||
}
|
||||
|
||||
// Called when lookahead reaches an intersection
|
||||
pub fn should_vanish_at_border(&self) -> bool {
|
||||
self.path.is_last_step() && self.goal == Goal::EndAtBorder
|
||||
}
|
||||
|
||||
pub fn next_step_as_turn(&self) -> Option<TurnID> {
|
||||
if self.path.is_last_step() {
|
||||
return None;
|
||||
|
Loading…
Reference in New Issue
Block a user