mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-11-24 09:24:26 +03:00
Handle agents deleted mid-turn. #312
This commit is contained in:
parent
035b8ee96f
commit
011ed8c7cf
@ -304,6 +304,13 @@ impl Path {
|
||||
pub fn next_step(&self) -> PathStep {
|
||||
self.steps[1]
|
||||
}
|
||||
pub fn maybe_next_step(&self) -> Option<PathStep> {
|
||||
if self.is_last_step() {
|
||||
None
|
||||
} else {
|
||||
Some(self.next_step())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn last_step(&self) -> PathStep {
|
||||
self.steps[self.steps.len() - 1]
|
||||
|
@ -612,6 +612,8 @@ impl DrivingSimState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Abruptly remove a vehicle from the simulation. They may be in any arbitrary state, like in
|
||||
/// the middle of a turn or parking.
|
||||
pub fn delete_car(&mut self, c: CarID, now: Time, ctx: &mut Ctx) -> Vehicle {
|
||||
let dists = self.queues[&self.cars[&c].router.head()].get_car_positions(
|
||||
now,
|
||||
@ -622,9 +624,10 @@ impl DrivingSimState {
|
||||
let mut car = self.cars.remove(&c).unwrap();
|
||||
|
||||
// Hacks to delete cars that're mid-turn
|
||||
if let Traversable::Turn(_) = car.router.head() {
|
||||
if let Traversable::Turn(t) = car.router.head() {
|
||||
let queue = self.queues.get_mut(&car.router.head()).unwrap();
|
||||
queue.reserved_length += car.vehicle.length + FOLLOWING_DISTANCE;
|
||||
ctx.intersections.agent_deleted_mid_turn(AgentID::Car(c), t);
|
||||
}
|
||||
if let Some(Traversable::Turn(t)) = car.router.maybe_next() {
|
||||
ctx.intersections.cancel_request(AgentID::Car(c), t);
|
||||
|
@ -163,6 +163,12 @@ impl IntersectionSimState {
|
||||
retain_btreeset(&mut self.blocked_by, |(c1, c2)| *c1 != car && *c2 != car);
|
||||
}
|
||||
|
||||
pub fn agent_deleted_mid_turn(&mut self, agent: AgentID, turn: TurnID) {
|
||||
let state = self.state.get_mut(&turn.parent).unwrap();
|
||||
assert!(state.accepted.remove(&Request { agent, turn }));
|
||||
state.reserved.remove(&Request { agent, turn });
|
||||
}
|
||||
|
||||
fn wakeup_waiting(&self, now: Time, i: IntersectionID, scheduler: &mut Scheduler, map: &Map) {
|
||||
let mut all: Vec<(Request, Time)> = self.state[&i]
|
||||
.waiting
|
||||
|
@ -310,11 +310,21 @@ impl WalkingSimState {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn delete_ped(&mut self, id: PedestrianID, scheduler: &mut Scheduler) {
|
||||
/// Abruptly remove a pedestrian from the simulation. They may be in any arbitrary state, like
|
||||
/// in the middle of a turn.
|
||||
pub fn delete_ped(&mut self, id: PedestrianID, ctx: &mut Ctx) {
|
||||
let ped = self.peds.remove(&id).unwrap();
|
||||
self.peds_per_traversable
|
||||
.remove(ped.path.current_step().as_traversable(), id);
|
||||
scheduler.cancel(Command::UpdatePed(id));
|
||||
ctx.scheduler.cancel(Command::UpdatePed(id));
|
||||
|
||||
if let PathStep::Turn(t) = ped.path.current_step() {
|
||||
ctx.intersections
|
||||
.agent_deleted_mid_turn(AgentID::Pedestrian(id), t);
|
||||
}
|
||||
if let Some(PathStep::Turn(t)) = ped.path.maybe_next_step() {
|
||||
ctx.intersections.cancel_request(AgentID::Pedestrian(id), t);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn debug_ped(&self, id: PedestrianID) {
|
||||
|
@ -120,11 +120,7 @@ impl Router {
|
||||
}
|
||||
|
||||
pub fn maybe_next(&self) -> Option<Traversable> {
|
||||
if self.last_step() {
|
||||
None
|
||||
} else {
|
||||
Some(self.path.next_step().as_traversable())
|
||||
}
|
||||
self.path.maybe_next_step().map(|s| s.as_traversable())
|
||||
}
|
||||
|
||||
pub fn last_step(&self) -> bool {
|
||||
|
@ -903,6 +903,7 @@ impl Sim {
|
||||
for (agent, trip) in affected {
|
||||
match agent {
|
||||
AgentID::Car(car) => {
|
||||
info!("Nuking {}", car);
|
||||
let vehicle = self.driving.delete_car(car, self.time, &mut ctx);
|
||||
// TODO Plumb more info about the reason
|
||||
self.trips.cancel_trip(
|
||||
@ -914,7 +915,7 @@ impl Sim {
|
||||
);
|
||||
}
|
||||
AgentID::Pedestrian(ped) => {
|
||||
self.walking.delete_ped(ped, ctx.scheduler);
|
||||
self.walking.delete_ped(ped, &mut ctx);
|
||||
self.trips.cancel_trip(
|
||||
self.time,
|
||||
trip,
|
||||
|
Loading…
Reference in New Issue
Block a user