squeeze in vehicle type to CarID, for debugging convenience

This commit is contained in:
Dustin Carlino 2019-02-04 20:09:22 -08:00
parent acc298a6ab
commit 17a0b57644
7 changed files with 60 additions and 14 deletions

View File

@ -5,7 +5,7 @@ use abstutil::elapsed_seconds;
use ezgui::{EventLoopMode, GfxCtx, InputResult, TextBox};
use geom::{Line, Pt2D};
use map_model::{raw_data, AreaID, BuildingID, IntersectionID, LaneID, Map, ParcelID, RoadID};
use sim::{CarID, PedestrianID, Sim, TripID};
use sim::{PedestrianID, Sim, TripID};
use std::time::Instant;
use std::usize;
@ -102,7 +102,10 @@ fn warp_point(line: String, map: &Map, sim: &Sim, draw_map: &DrawMap) -> Option<
// TODO ideally "pa" prefix?
'e' => ID::Parcel(ParcelID(idx)),
'p' => ID::Pedestrian(PedestrianID(idx)),
'c' => ID::Car(CarID(idx)),
'c' => {
// This one gets more complicated. :)
ID::Car(sim.lookup_car_id(idx)?)
}
't' => ID::Trip(TripID(idx)),
// TODO "tu"?
'u' => {

View File

@ -498,8 +498,16 @@ impl SimQueue {
#[derive(Serialize, Deserialize, PartialEq)]
pub struct DrivingSimState {
// Using BTreeMap instead of HashMap so iteration is deterministic.
#[serde(
serialize_with = "serialize_btreemap",
deserialize_with = "deserialize_btreemap"
)]
cars: BTreeMap<CarID, Car>,
// Separate from cars so we can have different mutability in react()
#[serde(
serialize_with = "serialize_btreemap",
deserialize_with = "deserialize_btreemap"
)]
routers: BTreeMap<CarID, Router>,
// If there's no SimQueue for a Traversable, then there are currently no agents on it.
#[serde(
@ -736,8 +744,8 @@ impl DrivingSimState {
if start_dist < params.vehicle.length {
panic!(
"Can't start car at {} along {}; the vehicle is {}. Bad position passed in.",
start_dist, start_lane, params.vehicle.length
"Can't start {} at {} along {}; the vehicle is {}. Bad position passed in.",
params.car, start_dist, start_lane, params.vehicle.length
);
}

View File

@ -37,7 +37,7 @@ pub struct Vehicle {
pub max_speed: Option<Speed>,
}
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
pub enum VehicleType {
Car,
Bus,

View File

@ -39,12 +39,23 @@ use map_model::{BuildingID, LaneID};
use serde_derive::{Deserialize, Serialize};
use std::fmt;
// The VehicleType is only used for convenient debugging. The numeric ID itself must be sufficient.
// TODO Implement Eq, Hash, Ord manually to guarantee this.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct CarID(pub usize);
pub struct CarID(pub usize, VehicleType);
impl fmt::Display for CarID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "CarID({0})", self.0)
write!(
f,
"CarID({0} -- {1})",
self.0,
match self.1 {
VehicleType::Car => "car",
VehicleType::Bus => "bus",
VehicleType::Bike => "bike",
}
)
}
}

View File

@ -1,5 +1,6 @@
use crate::kinematics::Vehicle;
use crate::{CarID, CarState, DrawCarInput, ParkedCar, ParkingSpot, VehicleType};
use abstutil::{deserialize_btreemap, serialize_btreemap};
use geom::{Angle, Distance, Pt2D};
use map_model;
use map_model::{BuildingID, Lane, LaneID, LaneType, Map, Position, Traversable};
@ -9,6 +10,10 @@ use std::iter;
#[derive(Serialize, Deserialize, PartialEq)]
pub struct ParkingSimState {
#[serde(
serialize_with = "serialize_btreemap",
deserialize_with = "deserialize_btreemap"
)]
cars: BTreeMap<CarID, ParkedCar>,
// TODO hacky, but other types of lanes just mark 0 spots. :\
lanes: Vec<ParkingLane>,

View File

@ -9,7 +9,9 @@ use crate::transit::TransitSimState;
use crate::trips::TripManager;
use crate::view::WorldView;
use crate::walking::WalkingSimState;
use crate::{AgentID, CarID, Event, ParkedCar, PedestrianID, SimStats, Tick, TripID, TIMESTEP};
use crate::{
AgentID, CarID, Event, ParkedCar, PedestrianID, SimStats, Tick, TripID, VehicleType, TIMESTEP,
};
use abstutil;
use abstutil::Error;
use derivative::Derivative;
@ -389,6 +391,23 @@ impl Sim {
.or_else(|| self.parking_state.get_owner_of_car(id))
}
pub fn lookup_car_id(&self, idx: usize) -> Option<CarID> {
for vt in &[VehicleType::Car, VehicleType::Bike, VehicleType::Bus] {
let id = CarID(idx, *vt);
if self.driving_state.get_path(id).is_some() {
return Some(id);
}
}
let id = CarID(idx, VehicleType::Car);
// Only cars can be parked.
if self.parking_state.lookup_car(id).is_some() {
return Some(id);
}
None
}
pub fn get_accepted_agents(&self, id: IntersectionID) -> HashSet<AgentID> {
self.intersection_state.get_accepted_agents(id)
}

View File

@ -307,7 +307,7 @@ impl Spawner {
for (next_stop_idx, start_dist_along, path) in
transit_sim.get_route_starts(route.id, map).into_iter()
{
let id = CarID(self.car_id_counter);
let id = CarID(self.car_id_counter, VehicleType::Bus);
self.car_id_counter += 1;
let vehicle = Vehicle::generate_bus(id, rng);
let start = Position::new(
@ -359,7 +359,7 @@ impl Spawner {
) -> Vec<CarID> {
let mut results: Vec<CarID> = Vec::new();
for idx in spots.into_iter() {
let car = CarID(self.car_id_counter);
let car = CarID(self.car_id_counter, VehicleType::Car);
parking_sim.add_parked_car(ParkedCar::new(
car,
ParkingSpot::new(lane, idx),
@ -411,7 +411,7 @@ impl Spawner {
find_spot_near_building(*b, &mut open_spots_per_road, neighborhoods_roads, map)
{
new_cars += 1;
let car = CarID(self.car_id_counter);
let car = CarID(self.car_id_counter, VehicleType::Car);
// TODO since spawning applies during the next step, lots of stuff breaks without
// this :(
parking_sim.add_parked_car(ParkedCar::new(
@ -454,7 +454,7 @@ impl Spawner {
trips: &mut TripManager,
base_rng: &mut XorShiftRng,
) -> CarID {
let car_id = CarID(self.car_id_counter);
let car_id = CarID(self.car_id_counter, VehicleType::Car);
self.car_id_counter += 1;
let ped_id = PedestrianID(self.ped_id_counter);
self.ped_id_counter += 1;
@ -535,7 +535,7 @@ impl Spawner {
) {
let ped_id = PedestrianID(self.ped_id_counter);
self.ped_id_counter += 1;
let bike_id = CarID(self.car_id_counter);
let bike_id = CarID(self.car_id_counter, VehicleType::Bike);
self.car_id_counter += 1;
let first_spot = {
@ -603,7 +603,7 @@ impl Spawner {
trips: &mut TripManager,
base_rng: &mut XorShiftRng,
) {
let bike_id = CarID(self.car_id_counter);
let bike_id = CarID(self.car_id_counter, VehicleType::Bike);
self.car_id_counter += 1;
let ped_id = PedestrianID(self.ped_id_counter);
self.ped_id_counter += 1;