From d52be43a30ebb55edb1a9a4869215aba3b1bed78 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Wed, 27 Feb 2019 09:06:32 -0800 Subject: [PATCH] lift owner into Vehicle --- editor/src/plugins/sim/even_simpler_model.rs | 8 +++--- .../sim/new_des_model/make/scenario.rs | 2 +- .../plugins/sim/new_des_model/make/spawn.rs | 15 ++++++----- .../sim/new_des_model/mechanics/driving.rs | 24 +++++++++++------- .../sim/new_des_model/mechanics/parking.rs | 14 ++++------- editor/src/plugins/sim/new_des_model/mod.rs | 14 +++-------- editor/src/plugins/sim/new_des_model/sim.rs | 25 +++++++++---------- 7 files changed, 49 insertions(+), 53 deletions(-) diff --git a/editor/src/plugins/sim/even_simpler_model.rs b/editor/src/plugins/sim/even_simpler_model.rs index d7ae3d5f09..2dbdbaa20f 100644 --- a/editor/src/plugins/sim/even_simpler_model.rs +++ b/editor/src/plugins/sim/even_simpler_model.rs @@ -149,11 +149,11 @@ fn seed_parked_cars_near( for l in map.get_parent(driving_lane).all_lanes() { if map.get_l(l).is_parking() { for spot in sim.get_free_spots(l) { - if rng.gen_bool(0.2) { - sim.seed_parked_car(rand_vehicle(rng), spot, None); + if let Some(start_bldg) = random_bldg_near(l, map, rng) { + if rng.gen_bool(0.2) { + sim.seed_parked_car(rand_vehicle(rng), spot, Some(start_bldg)); - if rng.gen_bool(0.3) { - if let Some(start_bldg) = random_bldg_near(l, map, rng) { + if rng.gen_bool(0.3) { sim.schedule_trip( Duration::seconds(5.0), new_des_model::TripSpec::UsingParkedCar( diff --git a/editor/src/plugins/sim/new_des_model/make/scenario.rs b/editor/src/plugins/sim/new_des_model/make/scenario.rs index 0deeab63ef..a700db0fa6 100644 --- a/editor/src/plugins/sim/new_des_model/make/scenario.rs +++ b/editor/src/plugins/sim/new_des_model/make/scenario.rs @@ -634,7 +634,7 @@ fn rand_bike(rng: &mut XorShiftRng) -> VehicleSpec { let length = rand_dist(rng, MIN_BIKE_LENGTH, MAX_BIKE_LENGTH); let max_speed = Some(Speed::miles_per_hour(10.0)); VehicleSpec { - vehicle_type: VehicleType::Bus, + vehicle_type: VehicleType::Bike, length, max_speed, } diff --git a/editor/src/plugins/sim/new_des_model/make/spawn.rs b/editor/src/plugins/sim/new_des_model/make/spawn.rs index 672acbec56..53d58e0cb5 100644 --- a/editor/src/plugins/sim/new_des_model/make/spawn.rs +++ b/editor/src/plugins/sim/new_des_model/make/spawn.rs @@ -1,6 +1,6 @@ use crate::plugins::sim::new_des_model::{ Command, CreateCar, CreatePedestrian, DrivingGoal, ParkingSimState, ParkingSpot, Router, - Scheduler, SidewalkSpot, TripLeg, TripManager, VehicleSpec, + Scheduler, SidewalkPOI, SidewalkSpot, TripLeg, TripManager, VehicleSpec, }; use abstutil::Timer; use geom::Duration; @@ -155,7 +155,7 @@ impl TripSpawner { scheduler.enqueue_command(Command::SpawnCar( start_time, CreateCar::for_appearing( - vehicle_spec.make(car_id), + vehicle_spec.make(car_id, None), start_pos, router, trip, @@ -165,14 +165,17 @@ impl TripSpawner { TripSpec::UsingParkedCar(start, spot, goal) => { let ped_id = PedestrianID::tmp_new(self.ped_id_counter); self.ped_id_counter += 1; - let car_id = parking.get_car_at_spot(spot).unwrap().vehicle.id; - //assert_eq!(parked.owner, Some(start_bldg)); + let vehicle = &parking.get_car_at_spot(spot).unwrap().vehicle; + match start.connection { + SidewalkPOI::Building(b) => assert_eq!(vehicle.owner, Some(b)), + _ => unreachable!(), + }; let parking_spot = SidewalkSpot::parking_spot(spot, map, parking); let mut legs = vec![ TripLeg::Walk(parking_spot.clone()), - TripLeg::Drive(car_id, goal.clone()), + TripLeg::Drive(vehicle.id, goal.clone()), ]; match goal { DrivingGoal::ParkNear(b) => { @@ -220,7 +223,7 @@ impl TripSpawner { let walk_to = SidewalkSpot::bike_rack(start.sidewalk_pos.lane(), map).unwrap(); let mut legs = vec![ TripLeg::Walk(walk_to.clone()), - TripLeg::Bike(vehicle.make(bike_id), goal.clone()), + TripLeg::Bike(vehicle.make(bike_id, None), goal.clone()), ]; match goal { DrivingGoal::ParkNear(b) => { diff --git a/editor/src/plugins/sim/new_des_model/mechanics/driving.rs b/editor/src/plugins/sim/new_des_model/mechanics/driving.rs index ef941a93ec..753986feb2 100644 --- a/editor/src/plugins/sim/new_des_model/mechanics/driving.rs +++ b/editor/src/plugins/sim/new_des_model/mechanics/driving.rs @@ -6,7 +6,7 @@ use crate::plugins::sim::new_des_model::{ }; use ezgui::{Color, GfxCtx}; use geom::{Distance, Duration}; -use map_model::{Map, Path, Trace, Traversable, LANE_THICKNESS}; +use map_model::{BuildingID, Map, Path, Trace, Traversable, LANE_THICKNESS}; use serde_derive::{Deserialize, Serialize}; use sim::{AgentID, CarID, DrawCarInput}; use std::collections::{BTreeMap, VecDeque}; @@ -263,11 +263,10 @@ impl DrivingSimState { CarState::Parking(_, spot, ref time_int) => { if time > time_int.end { delete_indices.push((idx, dist)); - parking.add_parked_car(ParkedCar::new( - car.vehicle.clone(), + parking.add_parked_car(ParkedCar { + vehicle: car.vehicle.clone(), spot, - None, - )); + }); trips.car_reached_parking_spot( time, car.vehicle.id, @@ -408,10 +407,12 @@ impl DrivingSimState { pub fn tooltip_lines(&self, id: CarID) -> Option> { let car = self.get_car(id)?; - Some(vec![ - // TODO Owner, path left... - format!("Car {:?}", id), - ]) + Some(vec![format!( + "Car {:?}, owned by {:?}, {} lanes left", + id, + car.vehicle.owner, + car.router.get_path().num_lanes() + )]) } pub fn get_path(&self, id: CarID) -> Option<&Path> { @@ -435,4 +436,9 @@ impl DrivingSimState { .1; car.router.get_path().trace(map, front, dist_ahead) } + + pub fn get_owner_of_car(&self, id: CarID) -> Option { + let car = self.get_car(id)?; + car.vehicle.owner + } } diff --git a/editor/src/plugins/sim/new_des_model/mechanics/parking.rs b/editor/src/plugins/sim/new_des_model/mechanics/parking.rs index 7c060245bd..970aae38c1 100644 --- a/editor/src/plugins/sim/new_des_model/mechanics/parking.rs +++ b/editor/src/plugins/sim/new_des_model/mechanics/parking.rs @@ -90,7 +90,7 @@ impl ParkingSimState { assert!(self.reserved_spots.remove(&p.spot)); assert_eq!(self.lanes[&spot.lane].occupants[spot.idx], None); self.lanes.get_mut(&spot.lane).unwrap().occupants[spot.idx] = Some(p.vehicle.id); - if let Some(b) = p.owner { + if let Some(b) = p.vehicle.owner { self.cars_per_building.insert(b, p.vehicle.id); } self.cars.insert(p.vehicle.id, p); @@ -146,10 +146,6 @@ impl ParkingSimState { .collect() } - /*pub fn lookup_car(&self, id: CarID) -> Option<&ParkedCar> { - self.cars.get(&id) - }*/ - pub fn is_free(&self, spot: ParkingSpot) -> bool { self.lanes[&spot.lane].occupants[spot.idx].is_none() && !self.reserved_spots.contains(&spot) } @@ -195,7 +191,7 @@ impl ParkingSimState { let c = self.cars.get(&id)?; Some(vec![format!( "{} is parked, owned by {:?}", - c.vehicle.id, c.owner + c.vehicle.id, c.vehicle.owner )]) } @@ -208,9 +204,9 @@ impl ParkingSimState { .collect() } - /*pub fn get_owner_of_car(&self, id: CarID) -> Option { - self.lookup_car(id).and_then(|p| p.owner) - }*/ + pub fn get_owner_of_car(&self, id: CarID) -> Option { + self.cars.get(&id).and_then(|p| p.vehicle.owner) + } } #[derive(Serialize, Deserialize, PartialEq)] diff --git a/editor/src/plugins/sim/new_des_model/mod.rs b/editor/src/plugins/sim/new_des_model/mod.rs index 834509d6ad..22693dffce 100644 --- a/editor/src/plugins/sim/new_des_model/mod.rs +++ b/editor/src/plugins/sim/new_des_model/mod.rs @@ -41,6 +41,7 @@ pub const FOLLOWING_DISTANCE: Distance = Distance::const_meters(1.0); #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct Vehicle { pub id: CarID, + pub owner: Option, pub vehicle_type: VehicleType, pub length: Distance, pub max_speed: Option, @@ -55,9 +56,10 @@ pub struct VehicleSpec { } impl VehicleSpec { - pub fn make(self, id: CarID) -> Vehicle { + pub fn make(self, id: CarID, owner: Option) -> Vehicle { Vehicle { id, + owner, vehicle_type: self.vehicle_type, length: self.length, max_speed: self.max_speed, @@ -81,19 +83,9 @@ impl ParkingSpot { pub struct ParkedCar { pub vehicle: Vehicle, pub spot: ParkingSpot, - pub owner: Option, } impl ParkedCar { - pub fn new(vehicle: Vehicle, spot: ParkingSpot, owner: Option) -> ParkedCar { - assert_eq!(vehicle.vehicle_type, VehicleType::Car); - ParkedCar { - vehicle, - spot, - owner, - } - } - pub fn get_driving_pos(&self, parking: &ParkingSimState, map: &Map) -> Position { parking.spot_to_driving_pos(self.spot, &self.vehicle, map) } diff --git a/editor/src/plugins/sim/new_des_model/sim.rs b/editor/src/plugins/sim/new_des_model/sim.rs index f39bcc3374..6dd9c22694 100644 --- a/editor/src/plugins/sim/new_des_model/sim.rs +++ b/editor/src/plugins/sim/new_des_model/sim.rs @@ -87,14 +87,13 @@ impl Sim { owner: Option, ) { self.parking.reserve_spot(spot); - self.parking.add_parked_car(ParkedCar::new( - vehicle.make(CarID::tmp_new( - self.spawner.car_id_counter, - VehicleType::Car, - )), + self.parking.add_parked_car(ParkedCar { + vehicle: vehicle.make( + CarID::tmp_new(self.spawner.car_id_counter, VehicleType::Car), + owner, + ), spot, - owner, - )); + }); self.spawner.car_id_counter += 1; } @@ -492,15 +491,15 @@ impl Sim { } } - /////////////////// TODO Port properly - - /*pub fn get_owner_of_car(&self, id: CarID) -> Option { - self.driving_state + pub fn get_owner_of_car(&self, id: CarID) -> Option { + self.driving .get_owner_of_car(id) - .or_else(|| self.parking_state.get_owner_of_car(id)) + .or_else(|| self.parking.get_owner_of_car(id)) } - pub fn get_accepted_agents(&self, id: IntersectionID) -> HashSet { + /////////////////// TODO Port properly + + /*pub fn get_accepted_agents(&self, id: IntersectionID) -> HashSet { self.intersection_state.get_accepted_agents(id) }