From 98914ce208ecbde301ee46beb15759fba49787d5 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Wed, 17 Oct 2018 10:23:25 -0700 Subject: [PATCH] peds only use the car associated with their building --- docs/design/sim.md | 5 +++- editor/src/plugins/scenarios.rs | 1 - sim/src/scenario.rs | 44 ++++++++++----------------------- 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/docs/design/sim.md b/docs/design/sim.md index 5384678359..fde68bcaeb 100644 --- a/docs/design/sim.md +++ b/docs/design/sim.md @@ -173,7 +173,10 @@ steps for this change: = initially randomly assign a building in the neighborhood = start seeding parked cars per building instead of per spot = make sure stability is vaguely preserved -- make peds that'll use a car pick from a house with an available car += make peds that'll use a car pick from a house with an available car + = should we even specify this, or should they pick mode at the + beginning based on availability? leaning towards this option since + it's easier to implement right now and seems more natural ## Traces between worlds diff --git a/editor/src/plugins/scenarios.rs b/editor/src/plugins/scenarios.rs index 94c43f3a57..cb9ec524cc 100644 --- a/editor/src/plugins/scenarios.rs +++ b/editor/src/plugins/scenarios.rs @@ -126,7 +126,6 @@ fn edit_scenario(map: &Map, scenario: &mut Scenario, mut wizard: WrappedWizard) start_tick: input_tick(&mut wizard, "Start spawning when?")?, // TODO input interval, or otherwise enforce stop_tick > start_tick stop_tick: input_tick(&mut wizard, "Stop spawning when?")?, - percent_drive: wizard.input_percent("What percent should drive?")?, start_from_neighborhood: choose_neighborhood( map, &mut wizard, diff --git a/sim/src/scenario.rs b/sim/src/scenario.rs index d5ee20aa9f..6075009531 100644 --- a/sim/src/scenario.rs +++ b/sim/src/scenario.rs @@ -2,8 +2,8 @@ use abstutil; use geom::{Polygon, Pt2D}; use map_model::{BuildingID, LaneID, Map}; use rand::Rng; -use std::collections::{BTreeMap, HashMap}; -use {fork_rng, ParkedCar, Sim, Tick}; +use std::collections::{HashMap, HashSet}; +use {CarID, Sim, Tick}; #[derive(Clone, Serialize, Deserialize, Debug)] pub struct Scenario { @@ -20,8 +20,6 @@ pub struct SpawnOverTime { // TODO use https://docs.rs/rand/0.5.5/rand/distributions/struct.Normal.html pub start_tick: Tick, pub stop_tick: Tick, - // [0, 1]. The rest will walk, using transit if useful. - pub percent_drive: f64, pub start_from_neighborhood: String, pub go_to_neighborhood: String, } @@ -103,20 +101,8 @@ impl Scenario { ); } - let mut parked_cars_per_neighborhood: BTreeMap> = BTreeMap::new(); - for (name, neighborhood) in &neighborhoods { - parked_cars_per_neighborhood.insert( - name.to_string(), - sim.parking_state - .get_all_parked_cars(Some(&Polygon::new(&neighborhood.points))), - ); - } - // Shuffle the list of parked cars, but be sure to fork the RNG to be stable across map - // edits. - for cars in parked_cars_per_neighborhood.values_mut() { - fork_rng(&mut sim.rng).shuffle(cars); - } - + // Don't let two pedestrians starting from one building use the same car. + let mut reserved_cars: HashSet = HashSet::new(); for s in &self.spawn_over_time { for _ in 0..s.num_agents { // TODO normal distribution, not uniform @@ -132,22 +118,18 @@ impl Scenario { .choose(&bldgs_per_neighborhood[&s.go_to_neighborhood]) .unwrap(); - if sim.rng.gen_bool(s.percent_drive) { - // TODO Probably prefer parked cars close to from_bldg, unless the particular - // area is tight on parking. :) - let parked_car = parked_cars_per_neighborhood - .get_mut(&s.start_from_neighborhood) - .unwrap() - .pop() - .expect(&format!( - "{} has no parked cars; can't instantiate {}", - s.start_from_neighborhood, self.scenario_name - )); - + // Will they drive or not? + if let Some(parked_car) = sim + .parking_state + .get_parked_cars_by_owner(from_bldg) + .into_iter() + .find(|p| !reserved_cars.contains(&p.car)) + { + reserved_cars.insert(parked_car.car); sim.spawner.start_trip_using_parked_car( spawn_time, map, - parked_car, + parked_car.clone(), &sim.parking_state, from_bldg, to_bldg,