From e8f7e7444c472d809da75e10af2ebd0d496c32c7 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Wed, 16 Dec 2020 12:05:15 -0800 Subject: [PATCH] Treat vehicles waiting to spawn as active. #392 --- sim/src/mechanics/driving.rs | 23 ++++++++++++++++++++++- sim/src/sim/mod.rs | 9 +++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/sim/src/mechanics/driving.rs b/sim/src/mechanics/driving.rs index 0d5329519f..27e245ad65 100644 --- a/sim/src/mechanics/driving.rs +++ b/sim/src/mechanics/driving.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use abstutil::{deserialize_hashmap, serialize_hashmap, FixedMap, IndexableKey}; use geom::{Distance, Duration, PolyLine, Speed, Time}; -use map_model::{IntersectionID, LaneID, Map, Path, Traversable}; +use map_model::{IntersectionID, LaneID, Map, Path, Position, Traversable}; use crate::mechanics::car::{Car, CarState}; use crate::mechanics::Queue; @@ -40,6 +40,8 @@ pub(crate) struct DrivingSimState { queues: HashMap, events: Vec, + waiting_to_spawn: BTreeMap)>, + recalc_lanechanging: bool, handle_uber_turns: bool, @@ -58,6 +60,7 @@ impl DrivingSimState { events: Vec::new(), recalc_lanechanging: opts.recalc_lanechanging, handle_uber_turns: opts.handle_uber_turns, + waiting_to_spawn: BTreeMap::new(), time_to_unpark_onstreet: Duration::seconds(10.0), time_to_park_onstreet: Duration::seconds(15.0), @@ -177,12 +180,19 @@ impl DrivingSimState { // get_idx_to_insert_car does a more detailed check of the current space usage. queue.reserved_length += car.vehicle.length + FOLLOWING_DISTANCE; } + self.waiting_to_spawn.remove(&car.vehicle.id); self.cars.insert(car.vehicle.id, car); return None; } Some(params) } + /// If start_car_on_lane fails and a retry is scheduled, this is an idempotent way to mark the + /// vehicle as active, but waiting to spawn. + pub fn vehicle_waiting_to_spawn(&mut self, id: CarID, pos: Position, person: Option) { + self.waiting_to_spawn.insert(id, (pos, person)); + } + /// State transitions for this car: /// /// Crossing -> Queued or WaitingToAdvance @@ -620,6 +630,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 { + self.waiting_to_spawn.remove(&c); + let dists = self.queues[&self.cars[&c].router.head()].get_car_positions( now, &self.cars, @@ -921,6 +933,15 @@ impl DrivingSimState { } } + for (id, (pos, person)) in &self.waiting_to_spawn { + result.push(UnzoomedAgent { + id: AgentID::Car(*id), + pos: pos.pt(map), + person: *person, + parking: false, + }); + } + result } diff --git a/sim/src/sim/mod.rs b/sim/src/sim/mod.rs index 5a32073e2c..7d9282a3f5 100644 --- a/sim/src/sim/mod.rs +++ b/sim/src/sim/mod.rs @@ -503,6 +503,15 @@ impl Sim { { // Starting the car failed for some reason. if retry_if_no_room { + self.driving.vehicle_waiting_to_spawn( + id, + Position::new( + create_car.router.head().as_lane(), + create_car.start_dist, + ), + trip_and_person.map(|(_, p)| p), + ); + // TODO Record this in the trip log self.scheduler.push( self.time + BLIND_RETRY_TO_SPAWN,