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
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
use std::collections::{hash_map::Entry, BTreeMap, BTreeSet, BinaryHeap, HashMap};

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

use abstutil::{
    deserialize_btreemap, deserialize_multimap, retain_btreemap, serialize_btreemap,
    serialize_multimap, MultiMap,
};
use geom::{Distance, PolyLine, Pt2D};
use map_model::{
    BuildingID, Lane, LaneID, LaneType, Map, OffstreetParking, ParkingLotID, PathConstraints,
    PathStep, Position, Traversable, TurnID,
};

use crate::{CarID, CarStatus, DrawCarInput, Event, ParkedCar, ParkingSpot, PersonID, Vehicle};

/// Manages the state of parked cars. There are two implementations:
/// - NormalParkingSimState allows only one vehicle per ParkingSpot defined in the map
/// - InfiniteParkingSimState pretends every building has infinite capacity, and onstreet parking is
///   ignored
#[enum_dispatch(ParkingSimState)]
pub trait ParkingSim {
    /// Returns any cars that got very abruptly evicted from existence, and also cars actively
    /// moving into a deleted spot.
    fn handle_live_edits(&mut self, map: &Map) -> (Vec<ParkedCar>, Vec<CarID>);
    fn get_free_onstreet_spots(&self, l: LaneID) -> Vec<ParkingSpot>;
    fn get_free_offstreet_spots(&self, b: BuildingID) -> Vec<ParkingSpot>;
    fn get_free_lot_spots(&self, pl: ParkingLotID) -> Vec<ParkingSpot>;
    fn reserve_spot(&mut self, spot: ParkingSpot, car: CarID);
    /// Needed when abruptly deleting a car, in case they're being deleted during their last step.
    fn unreserve_spot(&mut self, car: CarID);
    fn remove_parked_car(&mut self, p: ParkedCar);
    fn add_parked_car(&mut self, p: ParkedCar);
    fn get_draw_cars(&self, id: LaneID, map: &Map) -> Vec<DrawCarInput>;
    fn get_draw_cars_in_lots(&self, id: LaneID, map: &Map) -> Vec<DrawCarInput>;
    fn get_draw_car(&self, id: CarID, map: &Map) -> Option<DrawCarInput>;
    /// There's no DrawCarInput for cars parked offstreet, so we need this.
    fn canonical_pt(&self, id: CarID, map: &Map) -> Option<Pt2D>;
    fn get_all_draw_cars(&self, map: &Map) -> Vec<DrawCarInput>;
    fn is_free(&self, spot: ParkingSpot) -> bool;
    fn get_car_at_spot(&self, spot: ParkingSpot) -> Option<&ParkedCar>;
    /// The vehicle's front is currently at the given driving_pos. Returns all valid spots and their
    /// driving position.
    fn get_all_free_spots(
        &self,
        driving_pos: Position,
        vehicle: &Vehicle,
        // Either the building where a seeded car starts or the target of a trip. For filtering
        // private spots.
        target: BuildingID,
        map: &Map,
    ) -> Vec<(ParkingSpot, Position)>;
    fn spot_to_driving_pos(&self, spot: ParkingSpot, vehicle: &Vehicle, map: &Map) -> Position;
    fn spot_to_sidewalk_pos(&self, spot: ParkingSpot, map: &Map) -> Position;
    fn get_owner_of_car(&self, id: CarID) -> Option<PersonID>;
    fn lookup_parked_car(&self, id: CarID) -> Option<&ParkedCar>;
    /// (Filled, available)
    fn get_all_parking_spots(&self) -> (Vec<ParkingSpot>, Vec<ParkingSpot>);
    /// Unrealistically assumes the driver has knowledge of currently free parking spots, even if
    /// they're far away. Since they don't reserve the spot in advance, somebody else can still beat
    /// them there, producing some nice, realistic churn if there's too much contention. But
    /// the implementation has some internal jitter between different vehicles, to discourage
    /// everybody near one spot from all competing for it.
    /// Note the first PathStep is the turn after start, NOT PathStep::Lane(start).
    fn path_to_free_parking_spot(
        &self,
        start: LaneID,
        vehicle: &Vehicle,
        target: BuildingID,
        map: &Map,
    ) -> Option<(Vec<PathStep>, ParkingSpot, Position)>;
    fn collect_events(&mut self) -> Vec<Event>;
    fn all_parked_car_positions(&self, map: &Map) -> Vec<(Position, PersonID)>;
    fn bldg_to_parked_cars(&self, b: BuildingID) -> Vec<CarID>;
}

#[enum_dispatch]
#[derive(Serialize, Deserialize, Clone)]
pub enum ParkingSimState {
    Normal(NormalParkingSimState),
    Infinite(InfiniteParkingSimState),
}

impl ParkingSimState {
    /// Counterintuitive: any spots located in blackholes are just not represented here. If somebody
    /// tries to drive from a blackholed spot, they couldn't reach most places.
    pub fn new(map: &Map, infinite: bool) -> ParkingSimState {
        if infinite {
            ParkingSimState::Infinite(InfiniteParkingSimState::new(map))
        } else {
            ParkingSimState::Normal(NormalParkingSimState::new(map))
        }
    }

    pub fn is_infinite(&self) -> bool {
        match self {
            ParkingSimState::Normal(_) => false,
            ParkingSimState::Infinite(_) => true,
        }
    }
}

#[derive(Serialize, Deserialize, Clone)]
pub struct NormalParkingSimState {
    #[serde(
        serialize_with = "serialize_btreemap",
        deserialize_with = "deserialize_btreemap"
    )]
    parked_cars: BTreeMap<CarID, ParkedCar>,
    #[serde(
        serialize_with = "serialize_btreemap",
        deserialize_with = "deserialize_btreemap"
    )]
    occupants: BTreeMap<ParkingSpot, CarID>,
    #[serde(
        serialize_with = "serialize_btreemap",
        deserialize_with = "deserialize_btreemap"
    )]
    reserved_spots: BTreeMap<ParkingSpot, CarID>,

    // On-street
    onstreet_lanes: BTreeMap<LaneID, ParkingLane>,
    // TODO Really this could be 0, 1, or 2 lanes. Full MultiMap is overkill.
    #[serde(
        serialize_with = "serialize_multimap",
        deserialize_with = "deserialize_multimap"
    )]
    driving_to_parking_lanes: MultiMap<LaneID, LaneID>,

    // Off-street
    num_spots_per_offstreet: BTreeMap<BuildingID, usize>,
    // Cache dist_along
    #[serde(
        serialize_with = "serialize_multimap",
        deserialize_with = "deserialize_multimap"
    )]
    driving_to_offstreet: MultiMap<LaneID, (BuildingID, Distance)>,

    // Parking lots
    num_spots_per_lot: BTreeMap<ParkingLotID, usize>,
    #[serde(
        serialize_with = "serialize_multimap",
        deserialize_with = "deserialize_multimap"
    )]
    driving_to_lots: MultiMap<LaneID, ParkingLotID>,

    events: Vec<Event>,
}

impl NormalParkingSimState {
    fn new(map: &Map) -> NormalParkingSimState {
        let mut sim = NormalParkingSimState {
            parked_cars: BTreeMap::new(),
            occupants: BTreeMap::new(),
            reserved_spots: BTreeMap::new(),

            onstreet_lanes: BTreeMap::new(),
            driving_to_parking_lanes: MultiMap::new(),
            num_spots_per_offstreet: BTreeMap::new(),
            driving_to_offstreet: MultiMap::new(),
            num_spots_per_lot: BTreeMap::new(),
            driving_to_lots: MultiMap::new(),

            events: Vec::new(),
        };
        for l in map.all_lanes().values() {
            if let Some(lane) = ParkingLane::new(l, map) {
                sim.driving_to_parking_lanes.insert(lane.driving_lane, l.id);
                sim.onstreet_lanes.insert(lane.parking_lane, lane);
            }
        }
        for b in map.all_buildings() {
            if let Some((pos, _)) = b.driving_connection(map) {
                if !map.get_l(pos.lane()).driving_blackhole {
                    let num_spots = b.num_parking_spots();
                    if num_spots > 0 {
                        sim.num_spots_per_offstreet.insert(b.id, num_spots);
                        sim.driving_to_offstreet
                            .insert(pos.lane(), (b.id, pos.dist_along()));
                    }
                }
            }
        }
        for pl in map.all_parking_lots() {
            if !map.get_l(pl.driving_pos.lane()).driving_blackhole {
                sim.num_spots_per_lot.insert(pl.id, pl.capacity());
                sim.driving_to_lots.insert(pl.driving_pos.lane(), pl.id);
            }
        }

        sim
    }
}

impl ParkingSim for NormalParkingSimState {
    fn handle_live_edits(&mut self, map: &Map) -> (Vec<ParkedCar>, Vec<CarID>) {
        let (filled_before, _) = self.get_all_parking_spots();
        let new = NormalParkingSimState::new(map);
        let (_, avail_after) = new.get_all_parking_spots();
        let avail_after: BTreeSet<ParkingSpot> = avail_after.into_iter().collect();

        // Use the new spots
        self.onstreet_lanes = new.onstreet_lanes;
        self.driving_to_parking_lanes = new.driving_to_parking_lanes;
        self.num_spots_per_offstreet = new.num_spots_per_offstreet;
        self.driving_to_offstreet = new.driving_to_offstreet;
        self.num_spots_per_lot = new.num_spots_per_lot;
        self.driving_to_lots = new.driving_to_lots;

        // For every spot filled or reserved before, make sure that same spot still exists. If not,
        // evict that car.
        let mut evicted = Vec::new();
        for spot in filled_before {
            if !avail_after.contains(&spot) {
                // If the spot isn't occupied, it must be reserved; a car is in the process of
                // parking in it. That'll be handled below.
                if let Some(car) = self.occupants.remove(&spot) {
                    evicted.push(self.parked_cars.remove(&car).unwrap());
                }
            }
        }

        let mut moving_into_deleted_spot = Vec::new();
        retain_btreemap(&mut self.reserved_spots, |spot, car| {
            if avail_after.contains(spot) {
                true
            } else {
                moving_into_deleted_spot.push(*car);
                false
            }
        });

        (evicted, moving_into_deleted_spot)
    }

    fn get_free_onstreet_spots(&self, l: LaneID) -> Vec<ParkingSpot> {
        let mut spots: Vec<ParkingSpot> = Vec::new();
        if let Some(lane) = self.onstreet_lanes.get(&l) {
            for spot in lane.spots() {
                if self.is_free(spot) {
                    spots.push(spot);
                }
            }
        }
        spots
    }

    fn get_free_offstreet_spots(&self, b: BuildingID) -> Vec<ParkingSpot> {
        let mut spots: Vec<ParkingSpot> = Vec::new();
        for idx in 0..self.num_spots_per_offstreet.get(&b).cloned().unwrap_or(0) {
            let spot = ParkingSpot::Offstreet(b, idx);
            if self.is_free(spot) {
                spots.push(spot);
            }
        }
        spots
    }

    fn get_free_lot_spots(&self, pl: ParkingLotID) -> Vec<ParkingSpot> {
        let mut spots: Vec<ParkingSpot> = Vec::new();
        for idx in 0..self.num_spots_per_lot.get(&pl).cloned().unwrap_or(0) {
            let spot = ParkingSpot::Lot(pl, idx);
            if self.is_free(spot) {
                spots.push(spot);
            }
        }
        spots
    }

    fn reserve_spot(&mut self, spot: ParkingSpot, car: CarID) {
        assert!(self.is_free(spot));
        self.reserved_spots.insert(spot, car);

        // Sanity check the spot exists
        match spot {
            ParkingSpot::Onstreet(l, idx) => {
                assert!(idx < self.onstreet_lanes[&l].spot_dist_along.len());
            }
            ParkingSpot::Offstreet(b, idx) => {
                assert!(idx < self.num_spots_per_offstreet[&b]);
            }
            ParkingSpot::Lot(pl, idx) => {
                assert!(idx < self.num_spots_per_lot[&pl]);
            }
        }
    }

    fn unreserve_spot(&mut self, car: CarID) {
        retain_btreemap(&mut self.reserved_spots, |_, c| car != *c);
    }

    fn remove_parked_car(&mut self, p: ParkedCar) {
        if self.parked_cars.remove(&p.vehicle.id).is_none() {
            panic!("remove_parked_car {:?} missing from parked_cars", p);
        }
        if self.occupants.remove(&p.spot).is_none() {
            panic!("remove_parked_car {:?} missing from occupants", p);
        }
        self.events
            .push(Event::CarLeftParkingSpot(p.vehicle.id, p.spot));
    }

    fn add_parked_car(&mut self, p: ParkedCar) {
        self.events
            .push(Event::CarReachedParkingSpot(p.vehicle.id, p.spot));

        assert_eq!(self.reserved_spots.remove(&p.spot), Some(p.vehicle.id));

        assert!(!self.occupants.contains_key(&p.spot));
        self.occupants.insert(p.spot, p.vehicle.id);

        assert!(!self.parked_cars.contains_key(&p.vehicle.id));
        self.parked_cars.insert(p.vehicle.id, p);
    }

    fn get_draw_cars(&self, id: LaneID, map: &Map) -> Vec<DrawCarInput> {
        let mut cars = Vec::new();
        if let Some(ref lane) = self.onstreet_lanes.get(&id) {
            for spot in lane.spots() {
                if let Some(car) = self.occupants.get(&spot) {
                    cars.push(self.get_draw_car(*car, map).unwrap());
                }
            }
        }
        cars
    }

    fn get_draw_cars_in_lots(&self, id: LaneID, map: &Map) -> Vec<DrawCarInput> {
        let mut cars = Vec::new();
        for pl in self.driving_to_lots.get(id) {
            for idx in 0..self.num_spots_per_lot[&pl] {
                if let Some(car) = self.occupants.get(&ParkingSpot::Lot(*pl, idx)) {
                    if let Some(d) = self.get_draw_car(*car, map) {
                        cars.push(d);
                    }
                }
            }
        }
        cars
    }

    fn get_draw_car(&self, id: CarID, map: &Map) -> Option<DrawCarInput> {
        let p = self.parked_cars.get(&id)?;
        match p.spot {
            ParkingSpot::Onstreet(lane, idx) => {
                let front_dist =
                    self.onstreet_lanes[&lane].dist_along_for_car(idx, &p.vehicle, map);
                Some(DrawCarInput {
                    id: p.vehicle.id,
                    waiting_for_turn: None,
                    status: CarStatus::Parked,
                    show_parking_intent: false,
                    on: Traversable::Lane(lane),
                    partly_on: Vec::new(),
                    label: None,
                    person: None,

                    body: map
                        .get_l(lane)
                        .lane_center_pts
                        .exact_slice(front_dist - p.vehicle.length, front_dist),
                })
            }
            ParkingSpot::Offstreet(_, _) => None,
            ParkingSpot::Lot(pl, idx) => {
                let pl = map.get_pl(pl);
                // Some cars might be in the unrenderable extra_spots.
                let (pt, angle) = pl.spots.get(idx)?;
                let buffer = Distance::meters(0.5);
                Some(DrawCarInput {
                    id: p.vehicle.id,
                    waiting_for_turn: None,
                    status: CarStatus::Parked,
                    show_parking_intent: false,
                    // Just used for z-order
                    on: Traversable::Lane(pl.driving_pos.lane()),
                    partly_on: Vec::new(),
                    label: None,
                    person: None,

                    body: PolyLine::must_new(vec![
                        pt.project_away(buffer, *angle),
                        pt.project_away(map_model::PARKING_LOT_SPOT_LENGTH - buffer, *angle),
                    ]),
                })
            }
        }
    }

    fn canonical_pt(&self, id: CarID, map: &Map) -> Option<Pt2D> {
        let p = self.parked_cars.get(&id)?;
        match p.spot {
            ParkingSpot::Onstreet(_, _) => Some(self.get_draw_car(id, map).unwrap().body.last_pt()),
            ParkingSpot::Lot(pl, _) => {
                if let Some(car) = self.get_draw_car(id, map) {
                    Some(car.body.last_pt())
                } else {
                    Some(map.get_pl(pl).polygon.center())
                }
            }
            ParkingSpot::Offstreet(b, _) => Some(map.get_b(b).label_center),
        }
    }

    fn get_all_draw_cars(&self, map: &Map) -> Vec<DrawCarInput> {
        self.parked_cars
            .keys()
            .filter_map(|id| self.get_draw_car(*id, map))
            .collect()
    }

    fn is_free(&self, spot: ParkingSpot) -> bool {
        !self.occupants.contains_key(&spot) && !self.reserved_spots.contains_key(&spot)
    }

    fn get_car_at_spot(&self, spot: ParkingSpot) -> Option<&ParkedCar> {
        let car = self.occupants.get(&spot)?;
        Some(&self.parked_cars[&car])
    }

    fn get_all_free_spots(
        &self,
        driving_pos: Position,
        vehicle: &Vehicle,
        // Either the building where a seeded car starts or the target of a trip. For filtering
        // private spots.
        target: BuildingID,
        map: &Map,
    ) -> Vec<(ParkingSpot, Position)> {
        let mut candidates = Vec::new();

        for l in self.driving_to_parking_lanes.get(driving_pos.lane()) {
            for spot in self.onstreet_lanes[l].spots() {
                if self.is_free(spot)
                    && driving_pos.dist_along()
                        < self.spot_to_driving_pos(spot, vehicle, map).dist_along()
                {
                    candidates.push(spot);
                }
            }
        }

        for (b, bldg_dist) in self.driving_to_offstreet.get(driving_pos.lane()) {
            if let OffstreetParking::Private(_, _) = map.get_b(*b).parking {
                if target != *b {
                    continue;
                }
            }
            if driving_pos.dist_along() < *bldg_dist {
                for idx in 0..self.num_spots_per_offstreet[b] {
                    let spot = ParkingSpot::Offstreet(*b, idx);
                    if self.is_free(spot) {
                        candidates.push(spot);
                    }
                }
            }
        }

        for pl in self.driving_to_lots.get(driving_pos.lane()) {
            let lot_dist = map.get_pl(*pl).driving_pos.dist_along();
            if driving_pos.dist_along() < lot_dist {
                for idx in 0..self.num_spots_per_lot[&pl] {
                    let spot = ParkingSpot::Lot(*pl, idx);
                    if self.is_free(spot) {
                        candidates.push(spot);
                    }
                }
            }
        }

        candidates
            .into_iter()
            .map(|spot| (spot, self.spot_to_driving_pos(spot, vehicle, map)))
            .collect()
    }

    fn spot_to_driving_pos(&self, spot: ParkingSpot, vehicle: &Vehicle, map: &Map) -> Position {
        match spot {
            ParkingSpot::Onstreet(l, idx) => {
                let lane = &self.onstreet_lanes[&l];
                Position::new(l, lane.dist_along_for_car(idx, vehicle, map))
                    .equiv_pos_for_long_object(lane.driving_lane, vehicle.length, map)
            }
            ParkingSpot::Offstreet(b, _) => map.get_b(b).driving_connection(map).unwrap().0,
            ParkingSpot::Lot(pl, _) => map.get_pl(pl).driving_pos,
        }
    }

    fn spot_to_sidewalk_pos(&self, spot: ParkingSpot, map: &Map) -> Position {
        match spot {
            ParkingSpot::Onstreet(l, idx) => {
                let lane = &self.onstreet_lanes[&l];
                // Always centered in the entire parking spot
                Position::new(
                    l,
                    lane.spot_dist_along[idx] - (map.get_config().street_parking_spot_length / 2.0),
                )
                .equiv_pos(lane.sidewalk, map)
            }
            ParkingSpot::Offstreet(b, _) => map.get_b(b).sidewalk_pos,
            ParkingSpot::Lot(pl, _) => map.get_pl(pl).sidewalk_pos,
        }
    }

    fn get_owner_of_car(&self, id: CarID) -> Option<PersonID> {
        self.parked_cars.get(&id).and_then(|p| p.vehicle.owner)
    }
    fn lookup_parked_car(&self, id: CarID) -> Option<&ParkedCar> {
        self.parked_cars.get(&id)
    }

    fn get_all_parking_spots(&self) -> (Vec<ParkingSpot>, Vec<ParkingSpot>) {
        let mut spots = Vec::new();
        for lane in self.onstreet_lanes.values() {
            spots.extend(lane.spots());
        }
        for (b, num_spots) in &self.num_spots_per_offstreet {
            for idx in 0..*num_spots {
                spots.push(ParkingSpot::Offstreet(*b, idx));
            }
        }
        for (pl, num_spots) in &self.num_spots_per_lot {
            for idx in 0..*num_spots {
                spots.push(ParkingSpot::Lot(*pl, idx));
            }
        }

        let mut filled = Vec::new();
        let mut available = Vec::new();
        for spot in spots {
            if self.is_free(spot) {
                available.push(spot);
            } else {
                filled.push(spot);
            }
        }
        (filled, available)
    }

    fn path_to_free_parking_spot(
        &self,
        start: LaneID,
        vehicle: &Vehicle,
        target: BuildingID,
        map: &Map,
    ) -> Option<(Vec<PathStep>, ParkingSpot, Position)> {
        let mut backrefs: HashMap<LaneID, TurnID> = HashMap::new();
        // Don't travel far.
        // This is a max-heap, so negate all distances. Tie breaker is lane ID, arbitrary but
        // deterministic.
        let mut queue: BinaryHeap<(Distance, LaneID)> = BinaryHeap::new();
        queue.push((Distance::ZERO, start));

        // We need a source of randomness between different cars, but it needs to be deterministic
        // across repeated runs of the exact same simulation. This also shouldn't be the same
        // starting seed for one vehicle across different decisions through the simulation, because
        // then they might always prefer the first or third turn the most or whatever.
        let mut rng = XorShiftRng::seed_from_u64((vehicle.id.id + start.0) as u64);

        while !queue.is_empty() {
            let (dist_so_far, current) = queue.pop().unwrap();
            // If the current lane has a spot open, we wouldn't be asking. This can happen if a spot
            // opens up on the 'start' lane, but behind the car.
            if current != start {
                // Pick the closest to the start of the lane, since that's closest to where we came
                // from
                if let Some((spot, pos)) = self
                    .get_all_free_spots(Position::start(current), vehicle, target, map)
                    .into_iter()
                    .min_by_key(|(_, pos)| pos.dist_along())
                {
                    let mut steps = vec![PathStep::Lane(current)];
                    let mut current = current;
                    loop {
                        if current == start {
                            // Don't include PathStep::Lane(start)
                            steps.pop();
                            steps.reverse();
                            return Some((steps, spot, pos));
                        }
                        let turn = backrefs[&current];
                        steps.push(PathStep::Turn(turn));
                        steps.push(PathStep::Lane(turn.src));
                        current = turn.src;
                    }
                }
            }
            for turn in map.get_turns_for(current, PathConstraints::Car) {
                if let Entry::Vacant(e) = backrefs.entry(turn.id.dst) {
                    let dist_this_step = turn.geom.length() + map.get_l(current).length();
                    // When vehicles search away from the first lane for a spot, don't all go in
                    // the same direction! Do this by jittering which turn they explore.
                    // At worst, they consider a route to be 10% of its true length, so somebody
                    // might go up to 10x farther than necessary. From some quick tests, these
                    // worst cases aren't happening -- because it'd be unlikely to roll a higher
                    // number here many times in a row, and if there are only a few lanes away, it
                    // doesn't matter that much anyway.
                    let jitter = rng.gen_range(0.1..0.9);
                    e.insert(turn.id);
                    // Remember, keep things negative
                    queue.push((dist_so_far - jitter * dist_this_step, turn.id.dst));
                }
            }
        }

        None
    }

    fn collect_events(&mut self) -> Vec<Event> {
        std::mem::take(&mut self.events)
    }

    fn all_parked_car_positions(&self, map: &Map) -> Vec<(Position, PersonID)> {
        self.parked_cars
            .values()
            .map(|p| {
                (
                    self.spot_to_sidewalk_pos(p.spot, map),
                    p.vehicle.owner.unwrap(),
                )
            })
            .collect()
    }

    fn bldg_to_parked_cars(&self, b: BuildingID) -> Vec<CarID> {
        let mut cars = Vec::new();
        for idx in 0..self.num_spots_per_offstreet.get(&b).cloned().unwrap_or(0) {
            let spot = ParkingSpot::Offstreet(b, idx);
            if let Some(car) = self.occupants.get(&spot) {
                cars.push(*car);
            }
        }
        cars
    }
}

#[derive(Serialize, Deserialize, Clone)]
struct ParkingLane {
    parking_lane: LaneID,
    driving_lane: LaneID,
    sidewalk: LaneID,
    // The front of the parking spot (farthest along the lane)
    spot_dist_along: Vec<Distance>,
}

impl ParkingLane {
    fn new(lane: &Lane, map: &Map) -> Option<ParkingLane> {
        if lane.lane_type != LaneType::Parking {
            return None;
        }

        let driving_lane = if let Some(l) = map.get_parent(lane.id).parking_to_driving(lane.id, map)
        {
            l
        } else {
            // Serious enough to blow up loudly.
            panic!("Parking lane {} has no driving lane!", lane.id);
        };
        if map.get_l(driving_lane).driving_blackhole {
            return None;
        }
        let sidewalk = if let Some(l) =
            map.get_parent(lane.id)
                .find_closest_lane(lane.id, |l| l.is_walkable(), map)
        {
            l
        } else {
            warn!("Parking lane {} has no sidewalk!", lane.id);
            return None;
        };

        Some(ParkingLane {
            parking_lane: lane.id,
            driving_lane,
            sidewalk,
            spot_dist_along: (0..lane.number_parking_spots(map.get_config()))
                .map(|idx| map.get_config().street_parking_spot_length * (2.0 + idx as f64))
                .collect(),
        })
    }

    fn dist_along_for_car(&self, spot_idx: usize, vehicle: &Vehicle, map: &Map) -> Distance {
        // Find the offset to center this particular car in the parking spot
        self.spot_dist_along[spot_idx]
            - (map.get_config().street_parking_spot_length - vehicle.length) / 2.0
    }

    fn spots(&self) -> Vec<ParkingSpot> {
        let mut spots = Vec::new();
        for idx in 0..self.spot_dist_along.len() {
            spots.push(ParkingSpot::Onstreet(self.parking_lane, idx));
        }
        spots
    }
}

/// This assigns infinite private parking to all buildings and none anywhere else. This effectively
/// disables the simulation of parking entirely, making driving trips just go directly between
/// buildings. Useful for maps without good parking data (which is currently all of them) and
/// experiments where parking contention skews results and just gets in the way.
//
// TODO Reconsider this split implementation. There's lots of copied code. We can maybe just use
// NormalParkingSimState with an 'infinite: bool' and rethinking num_spots_per_offstreet.
#[derive(Serialize, Deserialize, Clone)]
pub struct InfiniteParkingSimState {
    #[serde(
        serialize_with = "serialize_btreemap",
        deserialize_with = "deserialize_btreemap"
    )]
    parked_cars: BTreeMap<CarID, ParkedCar>,
    #[serde(
        serialize_with = "serialize_btreemap",
        deserialize_with = "deserialize_btreemap"
    )]
    occupants: BTreeMap<ParkingSpot, CarID>,
    #[serde(
        serialize_with = "serialize_btreemap",
        deserialize_with = "deserialize_btreemap"
    )]
    reserved_spots: BTreeMap<ParkingSpot, CarID>,

    // Cache dist_along
    #[serde(
        serialize_with = "serialize_multimap",
        deserialize_with = "deserialize_multimap"
    )]
    driving_to_offstreet: MultiMap<LaneID, (BuildingID, Distance)>,
    // For an entry b1 -> b2, b1 is blackholed, so instead go park at b2 and walk the rest of the
    // way.
    blackholed_building_redirects: BTreeMap<BuildingID, BuildingID>,

    events: Vec<Event>,
}

impl InfiniteParkingSimState {
    fn new(map: &Map) -> InfiniteParkingSimState {
        let mut sim = InfiniteParkingSimState {
            parked_cars: BTreeMap::new(),
            occupants: BTreeMap::new(),
            reserved_spots: BTreeMap::new(),

            driving_to_offstreet: MultiMap::new(),
            blackholed_building_redirects: BTreeMap::new(),

            events: Vec::new(),
        };
        let mut blackholes = Vec::new();
        for b in map.all_buildings() {
            if let Some((pos, _)) = b.driving_connection(map) {
                if !map.get_l(pos.lane()).driving_blackhole {
                    sim.driving_to_offstreet
                        .insert(pos.lane(), (b.id, pos.dist_along()));
                    continue;
                }
            }
            blackholes.push(b.id);
        }

        // For every blackholed building, find a nearby building that isn't blackholed
        for b in blackholes {
            // TODO This is a simple DFS. Could sort by distance.
            let mut queue = vec![map.find_driving_lane_near_building(b)];
            let mut seen = BTreeSet::new();
            loop {
                let current = queue.pop().unwrap();
                if seen.contains(&current) {
                    continue;
                }
                seen.insert(current);
                if let Some((redirect, _)) = sim.driving_to_offstreet.get(current).iter().next() {
                    sim.blackholed_building_redirects.insert(b, *redirect);
                    break;
                }
                for turn in map.get_turns_for(current, PathConstraints::Car) {
                    queue.push(turn.id.dst);
                }
            }
        }

        sim
    }

    fn get_free_bldg_spot(&self, b: BuildingID) -> ParkingSpot {
        if let Some(redirect) = self.blackholed_building_redirects.get(&b) {
            // This won't recurse endlessly; the redirect is not a key in
            // blackholed_building_redirects.
            return self.get_free_bldg_spot(*redirect);
        }

        let mut i = 0;
        loop {
            let spot = ParkingSpot::Offstreet(b, i);
            if self.is_free(spot) {
                return spot;
            }
            i += 1;
        }
    }
}

impl ParkingSim for InfiniteParkingSimState {
    fn handle_live_edits(&mut self, map: &Map) -> (Vec<ParkedCar>, Vec<CarID>) {
        // Can live edits possibly affect anything?
        let new = InfiniteParkingSimState::new(map);
        self.driving_to_offstreet = new.driving_to_offstreet;
        self.blackholed_building_redirects = new.blackholed_building_redirects;

        (Vec::new(), Vec::new())
    }

    fn get_free_onstreet_spots(&self, _: LaneID) -> Vec<ParkingSpot> {
        Vec::new()
    }

    fn get_free_offstreet_spots(&self, b: BuildingID) -> Vec<ParkingSpot> {
        // Just returns the next free spot
        vec![self.get_free_bldg_spot(b)]
    }

    fn get_free_lot_spots(&self, _: ParkingLotID) -> Vec<ParkingSpot> {
        Vec::new()
    }

    fn reserve_spot(&mut self, spot: ParkingSpot, car: CarID) {
        assert!(self.is_free(spot));
        self.reserved_spots.insert(spot, car);
    }

    fn unreserve_spot(&mut self, car: CarID) {
        retain_btreemap(&mut self.reserved_spots, |_, c| car != *c);
    }

    fn remove_parked_car(&mut self, p: ParkedCar) {
        self.parked_cars
            .remove(&p.vehicle.id)
            .expect("remove_parked_car missing from parked_cars");
        self.occupants
            .remove(&p.spot)
            .expect("remove_parked_car missing from occupants");
        self.events
            .push(Event::CarLeftParkingSpot(p.vehicle.id, p.spot));
    }

    fn add_parked_car(&mut self, p: ParkedCar) {
        self.events
            .push(Event::CarReachedParkingSpot(p.vehicle.id, p.spot));

        assert_eq!(self.reserved_spots.remove(&p.spot), Some(p.vehicle.id));

        assert!(!self.occupants.contains_key(&p.spot));
        self.occupants.insert(p.spot, p.vehicle.id);

        assert!(!self.parked_cars.contains_key(&p.vehicle.id));
        self.parked_cars.insert(p.vehicle.id, p);
    }

    fn get_draw_cars(&self, _: LaneID, _: &Map) -> Vec<DrawCarInput> {
        Vec::new()
    }

    fn get_draw_cars_in_lots(&self, _: LaneID, _: &Map) -> Vec<DrawCarInput> {
        Vec::new()
    }

    fn get_draw_car(&self, _: CarID, _: &Map) -> Option<DrawCarInput> {
        None
    }

    fn canonical_pt(&self, id: CarID, map: &Map) -> Option<Pt2D> {
        let p = self.parked_cars.get(&id)?;
        match p.spot {
            ParkingSpot::Offstreet(b, _) => Some(map.get_b(b).label_center),
            _ => unreachable!(),
        }
    }

    fn get_all_draw_cars(&self, _: &Map) -> Vec<DrawCarInput> {
        Vec::new()
    }

    fn is_free(&self, spot: ParkingSpot) -> bool {
        !self.occupants.contains_key(&spot) && !self.reserved_spots.contains_key(&spot)
    }

    fn get_car_at_spot(&self, spot: ParkingSpot) -> Option<&ParkedCar> {
        let car = self.occupants.get(&spot)?;
        Some(&self.parked_cars[&car])
    }

    fn get_all_free_spots(
        &self,
        driving_pos: Position,
        vehicle: &Vehicle,
        target: BuildingID,
        map: &Map,
    ) -> Vec<(ParkingSpot, Position)> {
        // The target building may be blackholed, so fallback to a building on one of the
        // penultimate lanes, when the search begins.
        let mut bldg: Option<BuildingID> = None;
        for (b, bldg_dist) in self.driving_to_offstreet.get(driving_pos.lane()) {
            if driving_pos.dist_along() >= *bldg_dist {
                continue;
            }
            if target == *b {
                bldg = Some(target);
                break;
            } else if bldg.is_none() {
                // Backup option
                bldg = Some(*b);
            }
        }
        if let Some(b) = bldg {
            let spot = self.get_free_bldg_spot(b);
            vec![(spot, self.spot_to_driving_pos(spot, vehicle, map))]
        } else {
            Vec::new()
        }
    }

    fn spot_to_driving_pos(&self, spot: ParkingSpot, _: &Vehicle, map: &Map) -> Position {
        match spot {
            ParkingSpot::Offstreet(b, _) => map.get_b(b).driving_connection(map).unwrap().0,
            _ => unreachable!(),
        }
    }

    fn spot_to_sidewalk_pos(&self, spot: ParkingSpot, map: &Map) -> Position {
        match spot {
            ParkingSpot::Offstreet(b, _) => map.get_b(b).sidewalk_pos,
            _ => unreachable!(),
        }
    }

    fn get_owner_of_car(&self, id: CarID) -> Option<PersonID> {
        self.parked_cars.get(&id).and_then(|p| p.vehicle.owner)
    }
    fn lookup_parked_car(&self, id: CarID) -> Option<&ParkedCar> {
        self.parked_cars.get(&id)
    }

    fn get_all_parking_spots(&self) -> (Vec<ParkingSpot>, Vec<ParkingSpot>) {
        unreachable!()
    }

    fn path_to_free_parking_spot(
        &self,
        start: LaneID,
        vehicle: &Vehicle,
        target: BuildingID,
        map: &Map,
    ) -> Option<(Vec<PathStep>, ParkingSpot, Position)> {
        // TODO This impl is copied from NormalParkingSimState. Instead, we already know the
        // redirect... could just path to it.
        let mut backrefs: HashMap<LaneID, TurnID> = HashMap::new();
        // Don't travel far.
        // This is a max-heap, so negate all distances. Tie breaker is lane ID, arbitrary but
        // deterministic.
        let mut queue: BinaryHeap<(Distance, LaneID)> = BinaryHeap::new();
        queue.push((Distance::ZERO, start));

        while !queue.is_empty() {
            let (dist_so_far, current) = queue.pop().unwrap();
            // If the current lane has a spot open, we wouldn't be asking. This can happen if a spot
            // opens up on the 'start' lane, but behind the car.
            if current != start {
                // Pick the closest to the start of the lane, since that's closest to where we came
                // from
                if let Some((spot, pos)) = self
                    .get_all_free_spots(Position::start(current), vehicle, target, map)
                    .into_iter()
                    .min_by_key(|(_, pos)| pos.dist_along())
                {
                    let mut steps = vec![PathStep::Lane(current)];
                    let mut current = current;
                    loop {
                        if current == start {
                            // Don't include PathStep::Lane(start)
                            steps.pop();
                            steps.reverse();
                            return Some((steps, spot, pos));
                        }
                        let turn = backrefs[&current];
                        steps.push(PathStep::Turn(turn));
                        steps.push(PathStep::Lane(turn.src));
                        current = turn.src;
                    }
                }
            }
            for turn in map.get_turns_for(current, PathConstraints::Car) {
                if let Entry::Vacant(e) = backrefs.entry(turn.id.dst) {
                    let dist_this_step = turn.geom.length() + map.get_l(current).length();
                    e.insert(turn.id);
                    // Remember, keep things negative
                    queue.push((dist_so_far - dist_this_step, turn.id.dst));
                }
            }
        }

        None
    }

    fn collect_events(&mut self) -> Vec<Event> {
        std::mem::take(&mut self.events)
    }

    fn all_parked_car_positions(&self, map: &Map) -> Vec<(Position, PersonID)> {
        self.parked_cars
            .values()
            .map(|p| {
                (
                    self.spot_to_sidewalk_pos(p.spot, map),
                    p.vehicle.owner.unwrap(),
                )
            })
            .collect()
    }

    fn bldg_to_parked_cars(&self, b: BuildingID) -> Vec<CarID> {
        // TODO This is a very inefficient impl
        let mut cars = Vec::new();
        for (spot, car) in &self.occupants {
            if let ParkingSpot::Offstreet(bldg, _) = spot {
                if b == *bldg {
                    cars.push(*car);
                }
            }
        }
        cars
    }
}