lift owner into Vehicle

This commit is contained in:
Dustin Carlino 2019-02-27 09:06:32 -08:00
parent 874b3be519
commit d52be43a30
7 changed files with 49 additions and 53 deletions

View File

@ -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(

View File

@ -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,
}

View File

@ -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) => {

View File

@ -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<Vec<String>> {
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<BuildingID> {
let car = self.get_car(id)?;
car.vehicle.owner
}
}

View File

@ -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<BuildingID> {
self.lookup_car(id).and_then(|p| p.owner)
}*/
pub fn get_owner_of_car(&self, id: CarID) -> Option<BuildingID> {
self.cars.get(&id).and_then(|p| p.vehicle.owner)
}
}
#[derive(Serialize, Deserialize, PartialEq)]

View File

@ -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<BuildingID>,
pub vehicle_type: VehicleType,
pub length: Distance,
pub max_speed: Option<Speed>,
@ -55,9 +56,10 @@ pub struct VehicleSpec {
}
impl VehicleSpec {
pub fn make(self, id: CarID) -> Vehicle {
pub fn make(self, id: CarID, owner: Option<BuildingID>) -> 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<BuildingID>,
}
impl ParkedCar {
pub fn new(vehicle: Vehicle, spot: ParkingSpot, owner: Option<BuildingID>) -> 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)
}

View File

@ -87,14 +87,13 @@ impl Sim {
owner: Option<BuildingID>,
) {
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<BuildingID> {
self.driving_state
pub fn get_owner_of_car(&self, id: CarID) -> Option<BuildingID> {
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<AgentID> {
/////////////////// TODO Port properly
/*pub fn get_accepted_agents(&self, id: IntersectionID) -> HashSet<AgentID> {
self.intersection_state.get_accepted_agents(id)
}