diff --git a/sim/src/mechanics/driving.rs b/sim/src/mechanics/driving.rs index bd940781bd..66c8b2f35f 100644 --- a/sim/src/mechanics/driving.rs +++ b/sim/src/mechanics/driving.rs @@ -11,9 +11,9 @@ use crate::mechanics::Queue; use crate::sim::Ctx; use crate::{ ActionAtEnd, AgentID, AgentProperties, CarID, Command, CreateCar, DistanceInterval, - DrawCarInput, Event, IntersectionSimState, ParkedCar, ParkingSim, ParkingSimState, ParkingSpot, - PersonID, Scheduler, SimOptions, TimeInterval, TransitSimState, TripID, TripManager, - UnzoomedAgent, Vehicle, WalkingSimState, FOLLOWING_DISTANCE, + DrawCarInput, Event, IntersectionSimState, ParkedCar, ParkingSim, ParkingSpot, PersonID, + SimOptions, TimeInterval, TransitSimState, TripID, TripManager, UnzoomedAgent, Vehicle, + WalkingSimState, FOLLOWING_DISTANCE, }; const TIME_TO_WAIT_AT_BUS_STOP: Duration = Duration::const_seconds(10.0); @@ -89,14 +89,14 @@ impl DrivingSimState { &mut self, now: Time, mut params: CreateCar, - map: &Map, - intersections: &IntersectionSimState, - parking: &ParkingSimState, - scheduler: &mut Scheduler, + ctx: &mut Ctx, ) -> Option { let first_lane = params.router.head().as_lane(); - if !intersections.nobody_headed_towards(first_lane, map.get_l(first_lane).src_i) { + if !ctx + .intersections + .nobody_headed_towards(first_lane, ctx.map.get_l(first_lane).src_i) + { return Some(params); } if let Some(idx) = self.queues[&Traversable::Lane(first_lane)].get_idx_to_insert_car( @@ -134,8 +134,8 @@ impl DrivingSimState { match car.router.maybe_handle_end( params.start_dist, &car.vehicle, - parking, - map, + ctx.parking, + ctx.map, car.trip_and_person, &mut self.events, ) { @@ -165,9 +165,10 @@ impl DrivingSimState { } } - car.state = car.crossing_state(params.start_dist, now, map); + car.state = car.crossing_state(params.start_dist, now, ctx.map); } - scheduler.push(car.state.get_end_time(), Command::UpdateCar(car.vehicle.id)); + ctx.scheduler + .push(car.state.get_end_time(), Command::UpdateCar(car.vehicle.id)); { let queue = self.queues.get_mut(&Traversable::Lane(first_lane)).unwrap(); queue.cars.insert(idx, car.vehicle.id); @@ -180,6 +181,7 @@ impl DrivingSimState { } Some(params) } + /// State transitions for this car: /// /// Crossing -> Queued or WaitingToAdvance diff --git a/sim/src/sim/mod.rs b/sim/src/sim/mod.rs index 05a17605c7..ec8755c40b 100644 --- a/sim/src/sim/mod.rs +++ b/sim/src/sim/mod.rs @@ -475,14 +475,10 @@ impl Sim { let maybe_parked_car = create_car.maybe_parked_car.clone(); let req = create_car.req.clone(); - if let Some(create_car) = self.driving.start_car_on_lane( - self.time, - create_car, - map, - &self.intersections, - &self.parking, - &mut self.scheduler, - ) { + if let Some(create_car) = self + .driving + .start_car_on_lane(self.time, create_car, &mut ctx) + { // Starting the car failed for some reason. if retry_if_no_room { // TODO Record this in the trip log @@ -493,14 +489,6 @@ impl Sim { } else { // Buses don't use Command::SpawnCar, so this must exist. let (trip, person) = create_car.trip_and_person.unwrap(); - // Have to redeclare for the borrow checker - let mut ctx = Ctx { - parking: &mut self.parking, - intersections: &mut self.intersections, - cap: &mut self.cap, - scheduler: &mut self.scheduler, - map, - }; self.trips.cancel_trip( self.time, trip, diff --git a/sim/src/trips.rs b/sim/src/trips.rs index 9246b5500b..06632e52ea 100644 --- a/sim/src/trips.rs +++ b/sim/src/trips.rs @@ -372,11 +372,15 @@ impl TripManager { end, constraints: PathConstraints::Bike, }; - if let Some(router) = ctx - .map - .pathfind(req.clone()) - .map(|path| drive_to.make_router(bike, path, ctx.map)) - { + let maybe_router = if req.start == req.end { + // TODO Convert to a walking trip! + None + } else { + ctx.map + .pathfind(req.clone()) + .map(|path| drive_to.make_router(bike, path, ctx.map)) + }; + if let Some(router) = maybe_router { ctx.scheduler.push( now, Command::SpawnCar( diff --git a/traffic_seitan/src/main.rs b/traffic_seitan/src/main.rs index ab69cbefea..bac7cac6c9 100644 --- a/traffic_seitan/src/main.rs +++ b/traffic_seitan/src/main.rs @@ -6,7 +6,7 @@ use rand::seq::SliceRandom; use rand_xorshift::XorShiftRng; -use abstutil::{CmdArgs, Timer}; +use abstutil::{prettyprint_usize, CmdArgs, Timer}; use geom::Duration; use map_model::{LaneID, LaneType, Map, MapEdits}; use sim::{Sim, SimFlags}; @@ -60,6 +60,21 @@ fn run(map: &mut Map, sim: &mut Sim, rng: &mut XorShiftRng, timer: &mut Timer) { sim.handle_live_edited_traffic_signals(&map); sim.handle_live_edits(&map); } + + let mut finished = 0; + let mut cancelled = 0; + for (_, _, maybe_mode, _) in &sim.get_analytics().finished_trips { + if maybe_mode.is_some() { + finished += 1; + } else { + cancelled += 1; + } + } + println!( + "\nDone! {} finished trips, {} cancelled", + prettyprint_usize(finished), + prettyprint_usize(cancelled) + ); } fn alter_turn_destinations(sim: &Sim, map: &Map, rng: &mut XorShiftRng, edits: &mut MapEdits) { @@ -96,8 +111,6 @@ fn alter_turn_destinations(sim: &Sim, map: &Map, rng: &mut XorShiftRng, edits: & } } -// TODO This doesn't cause any interesting crash yet. Find somebody in the act of -// parking/unparking/going to a spot, and nuke that instead. fn nuke_random_parking(map: &Map, rng: &mut XorShiftRng, edits: &mut MapEdits) { let num_edits = 5;