1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
use std::collections::{BTreeMap, BTreeSet, HashSet, VecDeque};
use std::fmt;

use rand::seq::SliceRandom;
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;
use serde::{Deserialize, Serialize};

use abstutil::{prettyprint_usize, Counter, Timer};
use geom::{Distance, Duration, LonLat, Speed, Time};
use map_model::{
    BuildingID, BusRouteID, BusStopID, DirectedRoadID, Map, OffstreetParking, PathConstraints,
    Position, RoadID,
};

use crate::make::fork_rng;
use crate::{
    CarID, DrivingGoal, OrigPersonID, ParkingSpot, PersonID, SidewalkPOI, SidewalkSpot, Sim,
    TripEndpoint, TripMode, TripSpec, Vehicle, VehicleSpec, VehicleType, BIKE_LENGTH,
    MAX_CAR_LENGTH, MIN_CAR_LENGTH, SPAWN_DIST,
};

/// A Scenario describes all the input to a simulation. Usually a scenario covers one day.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Scenario {
    pub scenario_name: String,
    pub map_name: String,

    pub people: Vec<PersonSpec>,
    /// None means seed all buses. Otherwise the route name must be present here.
    pub only_seed_buses: Option<BTreeSet<String>>,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct PersonSpec {
    pub id: PersonID,
    /// Just used for debugging
    pub orig_id: Option<OrigPersonID>,
    pub trips: Vec<IndividTrip>,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct IndividTrip {
    pub depart: Time,
    pub trip: SpawnTrip,
    pub purpose: TripPurpose,
    pub cancelled: bool,
    /// Did a ScenarioModifier affect this?
    pub modified: bool,
}

impl IndividTrip {
    pub fn new(depart: Time, purpose: TripPurpose, trip: SpawnTrip) -> IndividTrip {
        IndividTrip {
            depart,
            trip,
            purpose,
            cancelled: false,
            modified: false,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum SpawnTrip {
    /// Only for interactive / debug trips
    VehicleAppearing {
        start: Position,
        goal: DrivingGoal,
        is_bike: bool,
    },
    FromBorder {
        dr: DirectedRoadID,
        goal: DrivingGoal,
        /// For bikes starting at a border, use FromBorder. UsingBike implies a walk->bike trip.
        is_bike: bool,
        origin: Option<OffMapLocation>,
    },
    UsingParkedCar(BuildingID, DrivingGoal),
    UsingBike(BuildingID, DrivingGoal),
    JustWalking(SidewalkSpot, SidewalkSpot),
    UsingTransit(
        SidewalkSpot,
        SidewalkSpot,
        BusRouteID,
        BusStopID,
        Option<BusStopID>,
    ),
    /// Completely off-map trip. Don't really simulate much of it.
    Remote {
        from: OffMapLocation,
        to: OffMapLocation,
        trip_time: Duration,
        mode: TripMode,
    },
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct OffMapLocation {
    pub parcel_id: usize,
    pub gps: LonLat,
}

/// Lifted from Seattle's Soundcast model, but seems general enough to use anyhere.
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum TripPurpose {
    Home,
    Work,
    School,
    Escort,
    PersonalBusiness,
    Shopping,
    Meal,
    Social,
    Recreation,
    Medical,
    ParkAndRideTransfer,
}

impl fmt::Display for TripPurpose {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                TripPurpose::Home => "home",
                TripPurpose::Work => "work",
                TripPurpose::School => "school",
                // Is this like a parent escorting a child to school?
                TripPurpose::Escort => "escort",
                TripPurpose::PersonalBusiness => "personal business",
                TripPurpose::Shopping => "shopping",
                TripPurpose::Meal => "eating",
                TripPurpose::Social => "social",
                TripPurpose::Recreation => "recreation",
                TripPurpose::Medical => "medical",
                TripPurpose::ParkAndRideTransfer => "park-and-ride transfer",
            }
        )
    }
}

impl Scenario {
    /// Any case where map edits could change the calls to the RNG, we have to fork.
    pub fn instantiate(&self, sim: &mut Sim, map: &Map, rng: &mut XorShiftRng, timer: &mut Timer) {
        sim.set_name(self.scenario_name.clone());

        timer.start(format!("Instantiating {}", self.scenario_name));

        if let Some(ref routes) = self.only_seed_buses {
            for route in map.all_bus_routes() {
                if routes.contains(&route.full_name) {
                    sim.seed_bus_route(route);
                }
            }
        } else {
            // All of them
            for route in map.all_bus_routes() {
                sim.seed_bus_route(route);
            }
        }

        timer.start_iter("trips for People", self.people.len());
        let mut spawner = sim.make_spawner();
        let mut parked_cars: Vec<(Vehicle, BuildingID)> = Vec::new();
        for p in &self.people {
            timer.next();

            if let Err(err) = p.check_schedule(map) {
                panic!("{}", err);
            }

            let (vehicle_specs, cars_initially_parked_at, vehicle_foreach_trip) =
                p.get_vehicles(rng);
            sim.new_person(
                p.id,
                p.orig_id,
                Scenario::rand_ped_speed(rng),
                vehicle_specs,
            );
            let person = sim.get_person(p.id);
            for (idx, b) in cars_initially_parked_at {
                parked_cars.push((person.vehicles[idx].clone(), b));
            }
            for (t, maybe_idx) in p.trips.iter().zip(vehicle_foreach_trip) {
                // The RNG call might change over edits for picking the spawning lane from a border
                // with multiple choices for a vehicle type.
                let mut tmp_rng = fork_rng(rng);
                let spec = t.trip.clone().to_trip_spec(
                    maybe_idx.map(|idx| person.vehicles[idx].id),
                    &mut tmp_rng,
                    map,
                );
                spawner.schedule_trip(
                    person,
                    t.depart,
                    spec,
                    t.trip.start(map),
                    t.purpose,
                    t.cancelled,
                    t.modified,
                    map,
                );
            }
        }

        // parked_cars is stable over map edits, so don't fork.
        parked_cars.shuffle(rng);
        seed_parked_cars(parked_cars, sim, map, rng, timer);

        sim.flush_spawner(spawner, map, timer);
        timer.stop(format!("Instantiating {}", self.scenario_name));
    }

    pub fn save(&self) {
        abstutil::write_binary(
            abstutil::path_scenario(&self.map_name, &self.scenario_name),
            self,
        );
    }

    pub fn empty(map: &Map, name: &str) -> Scenario {
        Scenario {
            scenario_name: name.to_string(),
            map_name: map.get_name().to_string(),
            people: Vec::new(),
            only_seed_buses: Some(BTreeSet::new()),
        }
    }

    pub fn rand_car(rng: &mut XorShiftRng) -> VehicleSpec {
        let length = Scenario::rand_dist(rng, MIN_CAR_LENGTH, MAX_CAR_LENGTH);
        VehicleSpec {
            vehicle_type: VehicleType::Car,
            length,
            max_speed: None,
        }
    }

    pub fn rand_bike(rng: &mut XorShiftRng) -> VehicleSpec {
        let max_speed = Some(Scenario::rand_speed(
            rng,
            Speed::miles_per_hour(8.0),
            Scenario::max_bike_speed(),
        ));
        VehicleSpec {
            vehicle_type: VehicleType::Bike,
            length: BIKE_LENGTH,
            max_speed,
        }
    }
    pub fn max_bike_speed() -> Speed {
        Speed::miles_per_hour(10.0)
    }

    pub fn rand_dist(rng: &mut XorShiftRng, low: Distance, high: Distance) -> Distance {
        assert!(high > low);
        Distance::meters(rng.gen_range(low.inner_meters(), high.inner_meters()))
    }

    fn rand_speed(rng: &mut XorShiftRng, low: Speed, high: Speed) -> Speed {
        assert!(high > low);
        Speed::meters_per_second(rng.gen_range(
            low.inner_meters_per_second(),
            high.inner_meters_per_second(),
        ))
    }

    pub fn rand_ped_speed(rng: &mut XorShiftRng) -> Speed {
        Scenario::rand_speed(rng, Speed::miles_per_hour(2.0), Speed::miles_per_hour(3.0))
    }
    pub fn max_ped_speed() -> Speed {
        Speed::miles_per_hour(3.0)
    }

    pub fn count_parked_cars_per_bldg(&self) -> Counter<BuildingID> {
        let mut per_bldg = Counter::new();
        // Pass in a dummy RNG
        let mut rng = XorShiftRng::from_seed([0; 16]);
        for p in &self.people {
            let (_, cars_initially_parked_at, _) = p.get_vehicles(&mut rng);
            for (_, b) in cars_initially_parked_at {
                per_bldg.inc(b);
            }
        }
        per_bldg
    }

    pub fn remove_weird_schedules(mut self, map: &Map) -> Scenario {
        let orig = self.people.len();
        self.people
            .retain(|person| match person.check_schedule(map) {
                Ok(()) => true,
                Err(err) => {
                    println!("{}", err);
                    false
                }
            });
        println!(
            "{} of {} people have nonsense schedules",
            prettyprint_usize(orig - self.people.len()),
            prettyprint_usize(orig)
        );
        // Fix up IDs
        for (idx, person) in self.people.iter_mut().enumerate() {
            person.id = PersonID(idx);
        }
        self
    }
}

fn seed_parked_cars(
    parked_cars: Vec<(Vehicle, BuildingID)>,
    sim: &mut Sim,
    map: &Map,
    base_rng: &mut XorShiftRng,
    timer: &mut Timer,
) {
    if sim.infinite_parking() {
        let mut blackholed = 0;
        for (vehicle, b) in parked_cars {
            if let Some(spot) = sim.get_free_offstreet_spots(b).pop() {
                sim.seed_parked_car(vehicle, spot);
            } else {
                blackholed += 1;
            }
        }
        if blackholed > 0 {
            timer.warn(format!(
                "{} parked cars weren't seeded, due to blackholed buildings",
                prettyprint_usize(blackholed)
            ));
        }
        return;
    }

    let mut open_spots_per_road: BTreeMap<RoadID, Vec<(ParkingSpot, Option<BuildingID>)>> =
        BTreeMap::new();
    for spot in sim.get_all_parking_spots().1 {
        let (r, restriction) = match spot {
            ParkingSpot::Onstreet(l, _) => (map.get_l(l).parent, None),
            ParkingSpot::Offstreet(b, _) => (
                map.get_l(map.get_b(b).sidewalk()).parent,
                match map.get_b(b).parking {
                    OffstreetParking::PublicGarage(_, _) => None,
                    OffstreetParking::Private(_, _) => Some(b),
                },
            ),
            ParkingSpot::Lot(pl, _) => (map.get_l(map.get_pl(pl).driving_pos.lane()).parent, None),
        };
        open_spots_per_road
            .entry(r)
            .or_insert_with(Vec::new)
            .push((spot, restriction));
    }
    // Changing parking on one road shouldn't affect far-off roads. Fork carefully.
    for r in map.all_roads() {
        let mut tmp_rng = fork_rng(base_rng);
        if let Some(ref mut spots) = open_spots_per_road.get_mut(&r.id) {
            spots.shuffle(&mut tmp_rng);
        }
    }

    timer.start_iter("seed parked cars", parked_cars.len());
    let mut ok = true;
    let total_cars = parked_cars.len();
    let mut seeded = 0;
    for (vehicle, b) in parked_cars {
        timer.next();
        if !ok {
            continue;
        }
        if let Some(spot) = find_spot_near_building(b, &mut open_spots_per_road, map) {
            seeded += 1;
            sim.seed_parked_car(vehicle, spot);
        } else {
            timer.warn(format!(
                "Not enough room to seed parked cars. Only found spots for {} of {}",
                prettyprint_usize(seeded),
                prettyprint_usize(total_cars)
            ));
            ok = false;
        }
    }
}

// Pick a parking spot for this building. If the building's road has a free spot, use it. If not,
// start BFSing out from the road in a deterministic way until finding a nearby road with an open
// spot.
fn find_spot_near_building(
    b: BuildingID,
    open_spots_per_road: &mut BTreeMap<RoadID, Vec<(ParkingSpot, Option<BuildingID>)>>,
    map: &Map,
) -> Option<ParkingSpot> {
    let mut roads_queue: VecDeque<RoadID> = VecDeque::new();
    let mut visited: HashSet<RoadID> = HashSet::new();
    {
        let start = map.building_to_road(b).id;
        roads_queue.push_back(start);
        visited.insert(start);
    }

    loop {
        let r = roads_queue.pop_front()?;
        if let Some(spots) = open_spots_per_road.get_mut(&r) {
            // Fill in all private parking first before
            // TODO With some probability, skip this available spot and park farther away
            if let Some(idx) = spots
                .iter()
                .position(|(_, restriction)| restriction == &Some(b))
            {
                return Some(spots.remove(idx).0);
            }
            if let Some(idx) = spots
                .iter()
                .position(|(_, restriction)| restriction.is_none())
            {
                return Some(spots.remove(idx).0);
            }
        }

        for next_r in map.get_next_roads(r).into_iter() {
            if !visited.contains(&next_r) {
                roads_queue.push_back(next_r);
                visited.insert(next_r);
            }
        }
    }
}

impl SpawnTrip {
    fn to_trip_spec(
        self,
        use_vehicle: Option<CarID>,
        rng: &mut XorShiftRng,
        map: &Map,
    ) -> TripSpec {
        match self {
            SpawnTrip::VehicleAppearing { start, goal, .. } => TripSpec::VehicleAppearing {
                start_pos: start,
                goal,
                use_vehicle: use_vehicle.unwrap(),
                retry_if_no_room: true,
                origin: None,
            },
            SpawnTrip::FromBorder {
                dr,
                goal,
                is_bike,
                origin,
            } => {
                let constraints = if is_bike {
                    PathConstraints::Bike
                } else {
                    PathConstraints::Car
                };
                if let Some(l) = dr.lanes(constraints, map).choose(rng) {
                    TripSpec::VehicleAppearing {
                        start_pos: Position::new(*l, SPAWN_DIST),
                        goal,
                        use_vehicle: use_vehicle.unwrap(),
                        retry_if_no_room: true,
                        origin,
                    }
                } else {
                    TripSpec::NoRoomToSpawn {
                        i: dr.src_i(map),
                        goal,
                        use_vehicle: use_vehicle.unwrap(),
                        origin,
                        error: format!("{} has no lanes to spawn a {:?}", dr.id, constraints),
                    }
                }
            }
            SpawnTrip::UsingParkedCar(start_bldg, goal) => TripSpec::UsingParkedCar {
                start_bldg,
                goal,
                car: use_vehicle.unwrap(),
            },
            SpawnTrip::UsingBike(start, goal) => TripSpec::UsingBike {
                bike: use_vehicle.unwrap(),
                start,
                goal,
            },
            SpawnTrip::JustWalking(start, goal) => TripSpec::JustWalking { start, goal },
            SpawnTrip::UsingTransit(start, goal, route, stop1, maybe_stop2) => {
                TripSpec::UsingTransit {
                    start,
                    goal,
                    route,
                    stop1,
                    maybe_stop2,
                }
            }
            SpawnTrip::Remote {
                from,
                to,
                trip_time,
                mode,
            } => TripSpec::Remote {
                from,
                to,
                trip_time,
                mode,
            },
        }
    }

    // TODO Why do I feel like this code is sitting somewhere else already
    pub fn mode(&self) -> TripMode {
        match self {
            SpawnTrip::VehicleAppearing { is_bike, .. } => {
                if *is_bike {
                    TripMode::Bike
                } else {
                    TripMode::Drive
                }
            }
            SpawnTrip::FromBorder { is_bike, .. } => {
                if *is_bike {
                    TripMode::Bike
                } else {
                    TripMode::Drive
                }
            }
            SpawnTrip::UsingParkedCar(_, _) => TripMode::Drive,
            SpawnTrip::UsingBike(_, _) => TripMode::Bike,
            SpawnTrip::JustWalking(_, _) => TripMode::Walk,
            SpawnTrip::UsingTransit(_, _, _, _, _) => TripMode::Transit,
            // TODO Uh...
            SpawnTrip::Remote { .. } => TripMode::Drive,
        }
    }

    pub fn start(&self, map: &Map) -> TripEndpoint {
        match self {
            SpawnTrip::VehicleAppearing { ref start, .. } => {
                TripEndpoint::Border(map.get_l(start.lane()).src_i, None)
            }
            SpawnTrip::FromBorder { dr, ref origin, .. } => {
                TripEndpoint::Border(dr.src_i(map), origin.clone())
            }
            SpawnTrip::UsingParkedCar(b, _) => TripEndpoint::Bldg(*b),
            SpawnTrip::UsingBike(b, _) => TripEndpoint::Bldg(*b),
            SpawnTrip::JustWalking(ref spot, _) | SpawnTrip::UsingTransit(ref spot, _, _, _, _) => {
                match spot.connection {
                    SidewalkPOI::Building(b) => TripEndpoint::Bldg(b),
                    SidewalkPOI::Border(i, ref loc) => TripEndpoint::Border(i, loc.clone()),
                    SidewalkPOI::SuddenlyAppear => {
                        TripEndpoint::Border(map.get_l(spot.sidewalk_pos.lane()).src_i, None)
                    }
                    _ => unreachable!(),
                }
            }
            // Pick an arbitrary border
            SpawnTrip::Remote { ref from, .. } => {
                TripEndpoint::Border(map.all_outgoing_borders()[0].id, Some(from.clone()))
            }
        }
    }

    pub fn end(&self, map: &Map) -> TripEndpoint {
        match self {
            SpawnTrip::VehicleAppearing { ref goal, .. }
            | SpawnTrip::FromBorder { ref goal, .. }
            | SpawnTrip::UsingParkedCar(_, ref goal)
            | SpawnTrip::UsingBike(_, ref goal) => match goal {
                DrivingGoal::ParkNear(b) => TripEndpoint::Bldg(*b),
                DrivingGoal::Border(i, _, ref loc) => TripEndpoint::Border(*i, loc.clone()),
            },
            SpawnTrip::JustWalking(_, ref spot) | SpawnTrip::UsingTransit(_, ref spot, _, _, _) => {
                match spot.connection {
                    SidewalkPOI::Building(b) => TripEndpoint::Bldg(b),
                    SidewalkPOI::Border(i, ref loc) => TripEndpoint::Border(i, loc.clone()),
                    _ => unreachable!(),
                }
            }
            // Pick an arbitrary border
            SpawnTrip::Remote { ref to, .. } => {
                TripEndpoint::Border(map.all_incoming_borders()[0].id, Some(to.clone()))
            }
        }
    }

    pub fn new(
        from: TripEndpoint,
        to: TripEndpoint,
        mode: TripMode,
        map: &Map,
    ) -> Option<SpawnTrip> {
        Some(match mode {
            TripMode::Drive => match from {
                TripEndpoint::Bldg(b) => {
                    SpawnTrip::UsingParkedCar(b, to.driving_goal(PathConstraints::Car, map)?)
                }
                TripEndpoint::Border(i, ref origin) => SpawnTrip::FromBorder {
                    dr: map.get_i(i).some_outgoing_road(map)?,
                    goal: to.driving_goal(PathConstraints::Car, map)?,
                    is_bike: false,
                    origin: origin.clone(),
                },
            },
            TripMode::Bike => match from {
                TripEndpoint::Bldg(b) => {
                    SpawnTrip::UsingBike(b, to.driving_goal(PathConstraints::Bike, map)?)
                }
                TripEndpoint::Border(i, ref origin) => SpawnTrip::FromBorder {
                    dr: map.get_i(i).some_outgoing_road(map)?,
                    goal: to.driving_goal(PathConstraints::Bike, map)?,
                    is_bike: true,
                    origin: origin.clone(),
                },
            },
            TripMode::Walk => {
                SpawnTrip::JustWalking(from.start_sidewalk_spot(map)?, to.end_sidewalk_spot(map)?)
            }
            TripMode::Transit => {
                let start = from.start_sidewalk_spot(map)?;
                let goal = to.end_sidewalk_spot(map)?;
                if let Some((stop1, maybe_stop2, route)) =
                    map.should_use_transit(start.sidewalk_pos, goal.sidewalk_pos)
                {
                    SpawnTrip::UsingTransit(start, goal, route, stop1, maybe_stop2)
                } else {
                    //timer.warn(format!("{:?} not actually using transit, because pathfinding
                    // didn't find any useful route", trip));
                    SpawnTrip::JustWalking(start, goal)
                }
            }
        })
    }
}

impl PersonSpec {
    // Verify that the trip start/endpoints of the person match up
    fn check_schedule(&self, map: &Map) -> Result<(), String> {
        for pair in self.trips.iter().zip(self.trips.iter().skip(1)) {
            if pair.0.depart >= pair.1.depart {
                return Err(format!(
                    "{} {:?} starts two trips in the wrong order: {} then {}",
                    self.id, self.orig_id, pair.0.depart, pair.1.depart
                ));
            }

            // Once off-map, re-enter via any border node.
            let end_bldg = match pair.0.trip.end(map) {
                TripEndpoint::Bldg(b) => Some(b),
                TripEndpoint::Border(_, _) => None,
            };
            let start_bldg = match pair.1.trip.start(map) {
                TripEndpoint::Bldg(b) => Some(b),
                TripEndpoint::Border(_, _) => None,
            };

            if end_bldg != start_bldg {
                return Err(format!(
                    "At {}, {} {:?} warps between some trips, from {:?} to {:?}",
                    pair.1.depart, self.id, self.orig_id, end_bldg, start_bldg
                ));
            }

            // But actually, make sure pairs of remote trips match up.
            if let (SpawnTrip::Remote { ref to, .. }, SpawnTrip::Remote { ref from, .. }) =
                (&pair.0.trip, &pair.1.trip)
            {
                if to != from {
                    return Err(format!(
                        "At {}, {} {:?} warps between some trips, from {:?} to {:?}",
                        pair.1.depart, self.id, self.orig_id, to, from
                    ));
                }
            }
        }
        Ok(())
    }

    fn get_vehicles(
        &self,
        rng: &mut XorShiftRng,
    ) -> (
        Vec<VehicleSpec>,
        Vec<(usize, BuildingID)>,
        Vec<Option<usize>>,
    ) {
        let mut vehicle_specs = Vec::new();
        let mut cars_initially_parked_at = Vec::new();
        let mut vehicle_foreach_trip = Vec::new();

        let mut bike_idx = None;
        // For each indexed car, is it parked somewhere, or off-map?
        let mut car_locations: Vec<(usize, Option<BuildingID>)> = Vec::new();

        // TODO If the trip is cancelled, this should be affected...
        for trip in &self.trips {
            let use_for_trip = match trip.trip {
                SpawnTrip::VehicleAppearing {
                    is_bike, ref goal, ..
                }
                | SpawnTrip::FromBorder {
                    is_bike, ref goal, ..
                } => {
                    if is_bike {
                        if bike_idx.is_none() {
                            bike_idx = Some(vehicle_specs.len());
                            vehicle_specs.push(Scenario::rand_bike(rng));
                        }
                        bike_idx
                    } else {
                        // Any available cars off-map?
                        let idx = if let Some(idx) = car_locations
                            .iter()
                            .find(|(_, parked_at)| parked_at.is_none())
                            .map(|(idx, _)| *idx)
                        {
                            idx
                        } else {
                            // Need a new car, starting off-map
                            let idx = vehicle_specs.len();
                            vehicle_specs.push(Scenario::rand_car(rng));
                            idx
                        };

                        // Where does this car wind up?
                        car_locations.retain(|(i, _)| idx != *i);
                        match goal {
                            DrivingGoal::ParkNear(b) => {
                                car_locations.push((idx, Some(*b)));
                            }
                            DrivingGoal::Border(_, _, _) => {
                                car_locations.push((idx, None));
                            }
                        }

                        Some(idx)
                    }
                }
                SpawnTrip::UsingParkedCar(b, ref goal) => {
                    // Is there already a car parked here?
                    let idx = if let Some(idx) = car_locations
                        .iter()
                        .find(|(_, parked_at)| *parked_at == Some(b))
                        .map(|(idx, _)| *idx)
                    {
                        idx
                    } else {
                        // Need a new car, starting at this building
                        let idx = vehicle_specs.len();
                        vehicle_specs.push(Scenario::rand_car(rng));
                        cars_initially_parked_at.push((idx, b));
                        idx
                    };

                    // Where does this car wind up?
                    car_locations.retain(|(i, _)| idx != *i);
                    match goal {
                        DrivingGoal::ParkNear(b) => {
                            car_locations.push((idx, Some(*b)));
                        }
                        DrivingGoal::Border(_, _, _) => {
                            car_locations.push((idx, None));
                        }
                    }

                    Some(idx)
                }
                SpawnTrip::UsingBike(_, _) => {
                    if bike_idx.is_none() {
                        bike_idx = Some(vehicle_specs.len());
                        vehicle_specs.push(Scenario::rand_bike(rng));
                    }
                    bike_idx
                }
                SpawnTrip::JustWalking(_, _) | SpawnTrip::UsingTransit(_, _, _, _, _) => None,
                SpawnTrip::Remote { .. } => None,
            };
            vehicle_foreach_trip.push(use_for_trip);
        }

        // For debugging
        if false {
            let mut n = vehicle_specs.len();
            if bike_idx.is_some() {
                n -= 1;
            }
            if n > 1 {
                println!("{} needs {} cars", self.id, n);
            }
        }

        (
            vehicle_specs,
            cars_initially_parked_at,
            vehicle_foreach_trip,
        )
    }
}