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
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

use serde::{Deserialize, Serialize};

use abstutil::{deserialize_btreemap, prettyprint_usize, serialize_btreemap, FixedMap};
use geom::{Duration, Time};
use map_model::{
    ControlStopSign, ControlTrafficSignal, Intersection, IntersectionID, LaneID, Map, StageType,
    Traversable, TurnID, TurnPriority, TurnType, UberTurn,
};

use crate::mechanics::car::{Car, CarState};
use crate::mechanics::Queue;
use crate::{
    AgentID, AlertLocation, CarID, Command, DelayCause, Event, Scheduler, SimOptions, Speed,
};

const WAIT_AT_STOP_SIGN: Duration = Duration::const_seconds(0.5);
const WAIT_BEFORE_YIELD_AT_TRAFFIC_SIGNAL: Duration = Duration::const_seconds(0.2);

/// Manages conflicts at intersections. When an agent has reached the end of a lane, they call
/// maybe_start_turn to make a Request. Based on the intersection type (stop sign, traffic signal,
/// or a "freeform policy"), the Request gets queued or immediately accepted. When agents finish
/// turns or when some time passes (for traffic signals), the intersection also gets a chance to
/// react, maybe granting one of the pending requests.
///
/// Most of the complexity comes from attempting to workaround
/// <https://a-b-street.github.io/docs/tech/trafficsim/gridlock.html>.
#[derive(Serialize, Deserialize, Clone)]
pub(crate) struct IntersectionSimState {
    state: BTreeMap<IntersectionID, State>,
    use_freeform_policy_everywhere: bool,
    dont_block_the_box: bool,
    break_turn_conflict_cycles: bool,
    handle_uber_turns: bool,
    disable_turn_conflicts: bool,
    // (x, y) means x is blocked by y. It's a many-to-many relationship. TODO Better data
    // structure.
    blocked_by: BTreeSet<(CarID, CarID)>,
    events: Vec<Event>,

    // Count how many calls to maybe_start_turn there are aside from the initial call. Break down
    // failures by those not allowed by the current intersection state vs those blocked by a
    // vehicle in the way in the target queue.
    total_repeat_requests: usize,
    not_allowed_requests: usize,
    blocked_by_someone_requests: usize,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct State {
    id: IntersectionID,
    // The in-progress turns which any potential new turns must not conflict with
    accepted: BTreeSet<Request>,
    // Track when a request is first made and if it's "urgent" (because the agent is overflowing a
    // short queue)
    #[serde(
        serialize_with = "serialize_btreemap",
        deserialize_with = "deserialize_btreemap"
    )]
    waiting: BTreeMap<Request, (Time, bool)>,
    // When a vehicle begins an uber-turn, reserve the future turns to ensure they're able to
    // complete the entire sequence. This is especially necessary since groups of traffic signals
    // are not yet configured as one.
    reserved: BTreeSet<Request>,
    // In some cases, a turn completing at one intersection may affect agents waiting to start an
    // uber-turn at nearby intersections.
    uber_turn_neighbors: Vec<IntersectionID>,

    // This is keyed by the lane the agent is approaching from. Note that:
    // 1) the turn in the request may change by the time the leader arrives -- they might decide to
    //    aim for a different destination lane
    // 2) If another agent pulls out on a driveway before this agent, they become the leader and
    //    overwrite this one
    #[serde(
        serialize_with = "serialize_btreemap",
        deserialize_with = "deserialize_btreemap"
    )]
    leader_eta: BTreeMap<LaneID, (Request, Time)>,

    signal: Option<SignalState>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct SignalState {
    // The current stage of the signal, zero based
    current_stage: usize,
    // The time when the signal is checked for advancing
    stage_ends_at: Time,
    // The number of times a variable signal has been extended during the current stage.
    extensions_count: usize,
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone, Debug)]
struct Request {
    agent: AgentID,
    turn: TurnID,
}

// Mutations
impl IntersectionSimState {
    pub fn new(map: &Map, scheduler: &mut Scheduler, opts: &SimOptions) -> IntersectionSimState {
        let mut sim = IntersectionSimState {
            state: BTreeMap::new(),
            use_freeform_policy_everywhere: opts.use_freeform_policy_everywhere,
            dont_block_the_box: !opts.allow_block_the_box,
            break_turn_conflict_cycles: !opts.dont_break_turn_conflict_cycles,
            handle_uber_turns: !opts.dont_handle_uber_turns,
            disable_turn_conflicts: opts.disable_turn_conflicts,
            blocked_by: BTreeSet::new(),
            events: Vec::new(),

            total_repeat_requests: 0,
            not_allowed_requests: 0,
            blocked_by_someone_requests: 0,
        };
        if sim.disable_turn_conflicts {
            sim.use_freeform_policy_everywhere = true;
        }

        for i in map.all_intersections() {
            let mut state = State {
                id: i.id,
                accepted: BTreeSet::new(),
                waiting: BTreeMap::new(),
                reserved: BTreeSet::new(),
                uber_turn_neighbors: Vec::new(),
                signal: None,
                leader_eta: BTreeMap::new(),
            };
            if i.is_traffic_signal() {
                state.signal = Some(SignalState::new(i.id, Time::START_OF_DAY, map, scheduler));
            }
            if let Some(mut set) = map_model::IntersectionCluster::autodetect(i.id, map) {
                set.remove(&i.id);
                state.uber_turn_neighbors.extend(set);
            }
            sim.state.insert(i.id, state);
        }
        sim
    }

    pub fn turn_finished(
        &mut self,
        now: Time,
        agent: AgentID,
        turn: TurnID,
        scheduler: &mut Scheduler,
        map: &Map,
        handling_live_edits: bool,
    ) {
        let state = self.state.get_mut(&turn.parent).unwrap();
        assert!(state.accepted.remove(&Request { agent, turn }));

        state.reserved.remove(&Request { agent, turn });
        if !handling_live_edits && map.get_t(turn).turn_type != TurnType::SharedSidewalkCorner {
            self.wakeup_waiting(now, turn.parent, scheduler, map);
        }
        if self.break_turn_conflict_cycles {
            if let AgentID::Car(car) = agent {
                self.blocked_by.retain(|(_, c)| *c != car);
            }
        }

        // If this intersection has uber-turns going through it, then we may need to wake up agents
        // with a reserved uber-turn that're waiting at nearby intersections. They may have
        // reserved their sequence of turns but then gotten stuck by somebody filling up a queue a
        // few steps away.
        for id in &self.state[&turn.parent].uber_turn_neighbors {
            self.wakeup_waiting(now, *id, scheduler, map);
        }
    }

    /// For deleting cars
    pub fn cancel_request(&mut self, agent: AgentID, turn: TurnID) {
        let state = self.state.get_mut(&turn.parent).unwrap();
        state.waiting.remove(&Request { agent, turn });
        if self.break_turn_conflict_cycles {
            if let AgentID::Car(car) = agent {
                self.blocked_by.retain(|(c1, c2)| *c1 != car && *c2 != car);
            }
        }
    }

    pub fn space_freed(
        &mut self,
        now: Time,
        i: IntersectionID,
        scheduler: &mut Scheduler,
        map: &Map,
    ) {
        self.wakeup_waiting(now, i, scheduler, map);
    }

    /// Vanished at border, stopped biking, etc -- a vehicle disappeared, and didn't have one last
    /// turn.
    pub fn vehicle_gone(&mut self, car: CarID) {
        self.blocked_by.retain(|(c1, c2)| *c1 != car && *c2 != car);
    }

    pub fn agent_deleted_mid_turn(&mut self, agent: AgentID, turn: TurnID) {
        let state = self.state.get_mut(&turn.parent).unwrap();
        assert!(state.accepted.remove(&Request { agent, turn }));

        // This agent might have a few more nearby turns reserved, because they're part of an
        // uber-turn. It's a blunt response to just clear them all out, but it should be correct.
        for state in self.state.values_mut() {
            state.reserved.retain(|req| req.agent != agent);
        }
    }

    fn wakeup_waiting(&self, now: Time, i: IntersectionID, scheduler: &mut Scheduler, map: &Map) {
        let mut all: Vec<(Request, Time, bool)> = self.state[&i]
            .waiting
            .iter()
            .map(|(r, (t, urgent))| (r.clone(), *t, *urgent))
            .collect();
        // Sort by waiting time, so things like stop signs actually are first-come, first-served.
        // But with an override: if somebody is currently on a queue that's overflowing, they're
        // very likely to be part of a cycle causing gridlock. Let them go first.
        all.sort_by_key(|(_, t, urgent)| (!*urgent, *t));

        // Wake up Priority turns before Yield turns. Don't wake up Banned turns at all. This makes
        // sure priority vehicles should get the head-start, without blocking yield vehicles
        // unnecessarily.
        let mut protected = Vec::new();
        let mut yielding = Vec::new();

        if self.use_freeform_policy_everywhere {
            for (req, _, _) in all {
                protected.push(req);
            }
        } else if let Some(signal) = map.maybe_get_traffic_signal(i) {
            let current_stage = self.state[&i].signal.as_ref().unwrap().current_stage;
            let stage = &signal.stages[current_stage];
            let reserved = &self.state[&i].reserved;
            let i = map.get_i(i);
            for (req, _, _) in all {
                match stage.get_priority_of_turn(req.turn, i) {
                    TurnPriority::Protected => {
                        protected.push(req);
                    }
                    TurnPriority::Yield => {
                        yielding.push(req);
                    }
                    // No need to wake up unless it has reserved
                    TurnPriority::Banned => {
                        if reserved.contains(&req) {
                            protected.push(req);
                        }
                    }
                }
            }
        } else if let Some(sign) = map.maybe_get_stop_sign(i) {
            for (req, _, _) in all {
                match sign.get_priority(req.turn, map) {
                    TurnPriority::Protected => {
                        protected.push(req);
                    }
                    TurnPriority::Yield => {
                        yielding.push(req);
                    }
                    TurnPriority::Banned => unreachable!(),
                }
            }
        } else {
            // This could either be a border intersection or an intersection that was just closed
            // in the middle of simulation. In either case, there shouldn't be any other turns at
            // it.
            assert!(protected.is_empty());
            assert!(yielding.is_empty());
        };
        protected.extend(yielding);

        // We now have all the requests in the order that we want to wake them up. The scheduler
        // arbitrarily (but deterministically) orders commands with the same time, so preserve the
        // ordering by adding little epsilons.
        let mut delay = Duration::ZERO;
        for req in protected {
            // Use update because multiple agents could finish a turn at the same time, before the
            // waiting one has a chance to try again.
            scheduler.update(now + delay, Command::update_agent(req.agent));
            delay += Duration::EPSILON;
        }
    }

    /// This is only triggered for traffic signals.
    pub fn update_intersection(
        &mut self,
        now: Time,
        id: IntersectionID,
        map: &Map,
        scheduler: &mut Scheduler,
    ) {
        let i = map.get_i(id);

        // trivial function that advances the signal stage and returns duration
        fn advance(
            signal_state: &mut SignalState,
            signal: &ControlTrafficSignal,
            i: &Intersection,
            allow_crosswalk_skip: bool,
        ) -> Duration {
            signal_state.current_stage = (signal_state.current_stage + 1) % signal.stages.len();
            let stage = &signal.stages[signal_state.current_stage];
            // only skip for variable all-walk crosswalk
            if let StageType::Variable(_, _, _) = stage.stage_type {
                if allow_crosswalk_skip && stage.max_crosswalk_time(i).is_some() {
                    // we can skip this stage, as its all walk and we're allowed to skip (no
                    // pedestrian waiting).
                    signal_state.current_stage =
                        (signal_state.current_stage + 1) % signal.stages.len();
                }
            }
            signal.stages[signal_state.current_stage]
                .stage_type
                .simple_duration()
        }
        let state = self.state.get_mut(&id).unwrap();
        let signal_state = state.signal.as_mut().unwrap();
        let signal = map.get_traffic_signal(id);
        let ped_waiting = state.waiting.keys().any(|req| {
            if let AgentID::Pedestrian(_) = req.agent {
                return true;
            }
            false
        });
        let duration: Duration;
        // Switch to a new stage?
        assert_eq!(now, signal_state.stage_ends_at);
        let old_stage = &signal.stages[signal_state.current_stage];
        match old_stage.stage_type {
            StageType::Fixed(_) => {
                duration = advance(signal_state, signal, i, !ped_waiting);
            }
            StageType::Variable(min, delay, additional) => {
                // test if anyone is waiting in current stage, and if so, extend the signal cycle.
                // Filter out pedestrians, as they've had their chance and the delay
                // could be short enough to keep them on the curb.
                let delay = std::cmp::max(Duration::const_seconds(1.0), delay);
                // Only extend for the fixed additional time
                if signal_state.extensions_count as f64 * delay.inner_seconds()
                    >= additional.inner_seconds()
                {
                    self.events.push(Event::Alert(
                        AlertLocation::Intersection(id),
                        format!(
                            "exhausted a variable stage {},{},{},{}",
                            min, delay, additional, signal_state.extensions_count
                        ),
                    ));
                    duration = advance(signal_state, signal, i, !ped_waiting);
                    signal_state.extensions_count = 0;
                } else if state.waiting.keys().all(|req| {
                    if let AgentID::Pedestrian(_) = req.agent {
                        return true;
                    }
                    // Should we only allow protected to extend or any not banned?
                    // currently only the protected demand control extended.
                    old_stage.get_priority_of_turn(req.turn, i) != TurnPriority::Protected
                }) {
                    signal_state.extensions_count = 0;
                    duration = advance(signal_state, signal, i, !ped_waiting);
                } else {
                    signal_state.extensions_count += 1;
                    duration = delay;
                    self.events.push(Event::Alert(
                        AlertLocation::Intersection(id),
                        format!(
                            "Extending a variable stage {},{},{},{}",
                            min, delay, additional, signal_state.extensions_count
                        ),
                    ));
                }
            }
        }

        signal_state.stage_ends_at = now + duration;
        scheduler.push(signal_state.stage_ends_at, Command::UpdateIntersection(id));
        self.wakeup_waiting(now, id, scheduler, map);
    }

    /// For cars: The head car calls this when they're at the end of the lane WaitingToAdvance. If
    /// this returns true, then the head car MUST actually start this turn.
    /// For peds: Likewise -- only called when the ped is at the start of the turn. They must
    /// actually do the turn if this returns true.
    ///
    /// If this returns false, the agent should NOT retry. IntersectionSimState will schedule a
    /// retry event at some point.
    pub fn maybe_start_turn(
        &mut self,
        agent: AgentID,
        turn: TurnID,
        speed: Speed,
        now: Time,
        map: &Map,
        scheduler: &mut Scheduler,
        maybe_cars_and_queues: Option<(
            &Car,
            &FixedMap<CarID, Car>,
            &mut HashMap<Traversable, Queue>,
        )>,
    ) -> bool {
        #![allow(clippy::logic_bug)] // Remove once TODO below is taken care of
        let req = Request { agent, turn };

        if let Some(_eta) = self
            .state
            .get_mut(&turn.parent)
            .unwrap()
            .leader_eta
            .remove(&req.turn.src)
        {
            // When they're late, it's because of a slow laggy head. Conflicting turns would've
            // been blocked anyway. Uncomment to debug
            //info!("{} predicted ETA {}, actually {}", req.agent, eta, now);
        }

        let entry = self
            .state
            .get_mut(&turn.parent)
            .unwrap()
            .waiting
            .entry(req.clone());
        let repeat_request = match entry {
            std::collections::btree_map::Entry::Vacant(_) => false,
            std::collections::btree_map::Entry::Occupied(_) => true,
        };
        let urgent = if let Some((car, _, queues)) = maybe_cars_and_queues.as_ref() {
            queues[&car.router.head()].is_overflowing()
        } else {
            false
        };
        entry.or_insert((now, urgent));

        if repeat_request {
            self.total_repeat_requests += 1;
        }

        let shared_sidewalk_corner =
            map.get_t(req.turn).turn_type == TurnType::SharedSidewalkCorner;

        let readonly_pair = maybe_cars_and_queues.as_ref().map(|(_, c, q)| (*c, &**q));
        let started_uber_turn = |state: &Self, car: &Car| {
            state.handle_uber_turns && car.router.get_path().currently_inside_ut().is_some()
        };
        #[allow(clippy::if_same_then_else)]
        let allowed = if shared_sidewalk_corner {
            // SharedSidewalkCorner doesn't conflict with anything -- fastpath!
            true
        } else if !self.handle_accepted_conflicts(&req, map, readonly_pair, Some((now, scheduler)))
        {
            // It's never OK to perform a conflicting turn
            false
        } else if maybe_cars_and_queues
            .as_ref()
            .map(|(car, _, _)| started_uber_turn(self, *car))
            .unwrap_or(false)
        {
            // If we started an uber-turn, then finish it! But alert if we're running a red light.
            // TODO: Consider reenabling alert
            if let Some(signal) = map.maybe_get_traffic_signal(turn.parent) {
                // Don't pass in the scheduler, aka, don't pause before yielding.
                if !self.traffic_signal_policy(&req, map, signal, speed, now, None) && false {
                    self.events.push(Event::Alert(
                        AlertLocation::Intersection(req.turn.parent),
                        format!("Running a red light inside an uber-turn: {:?}", req),
                    ));
                }
            }

            true
        } else if self.use_freeform_policy_everywhere {
            // If we made it this far, we don't conflict with an accepted turn
            true
        } else if let Some(signal) = map.maybe_get_traffic_signal(turn.parent) {
            self.traffic_signal_policy(&req, map, signal, speed, now, Some(scheduler))
        } else if let Some(sign) = map.maybe_get_stop_sign(turn.parent) {
            self.stop_sign_policy(&req, map, sign, speed, now, scheduler)
        } else {
            unreachable!()
        };
        if !allowed {
            if repeat_request {
                self.not_allowed_requests += 1;
            }
            // remove the reservation if we're about to start a UT and can't move
            if self.handle_uber_turns {
                if let Some(ut) = maybe_cars_and_queues
                    .as_ref()
                    .and_then(|(car, _, _)| car.router.get_path().about_to_start_ut())
                {
                    for t in &ut.path {
                        self.state
                            .get_mut(&t.parent)
                            .unwrap()
                            .reserved
                            .remove(&Request { agent, turn: *t });
                    }
                }
            }
            return false;
        }

        // Lock the entire uber-turn.
        if self.handle_uber_turns {
            if let Some(ut) = maybe_cars_and_queues
                .as_ref()
                .and_then(|(car, _, _)| car.router.get_path().about_to_start_ut())
            {
                // If there's a problem up ahead, don't start.
                for t in &ut.path {
                    let req = Request { agent, turn: *t };
                    if !self.handle_accepted_conflicts(&req, map, readonly_pair, None) {
                        if repeat_request {
                            self.blocked_by_someone_requests += 1;
                        }
                        return false;
                    }
                }
                // If the way is clear, make sure it stays that way.
                for t in &ut.path {
                    self.state
                        .get_mut(&t.parent)
                        .unwrap()
                        .reserved
                        .insert(Request { agent, turn: *t });
                }
            }
        }

        // Don't block the box.
        if let Some((car, cars, queues)) = maybe_cars_and_queues {
            assert_eq!(agent, AgentID::Car(car.vehicle.id));
            let inside_ut = self.handle_uber_turns
                && (car.router.get_path().currently_inside_ut().is_some()
                    || car.router.get_path().about_to_start_ut().is_some());
            let queue = queues.get_mut(&Traversable::Lane(turn.dst)).unwrap();
            if !queue.try_to_reserve_entry(
                car,
                !self.dont_block_the_box
                    || allow_block_the_box(map.get_i(turn.parent))
                    || inside_ut,
            ) {
                let mut actually_did_reserve_entry = false;
                if self.break_turn_conflict_cycles {
                    if let Some(c) = queue.laggy_head {
                        self.blocked_by.insert((car.vehicle.id, c));
                    } else if let Some(c) = queue.get_active_cars().get(0) {
                        self.blocked_by.insert((car.vehicle.id, *c));
                    } else {
                        // try_to_reserve_entry must have failed because somebody has filled up
                        // reserved_length. That only happens while a turn is in progress, so this
                        // unwrap() must work.
                        let blocking_req = self.state[&turn.parent]
                            .accepted
                            .iter()
                            .find(|r| r.turn.dst == turn.dst)
                            .unwrap();
                        self.blocked_by
                            .insert((car.vehicle.id, blocking_req.agent.as_car()));
                    }

                    // Allow blocking the box if we're part of a cycle.
                    if self
                        .detect_conflict_cycle(car.vehicle.id, (cars, queues))
                        .is_some()
                    {
                        // Reborrow
                        let queue = queues.get_mut(&Traversable::Lane(turn.dst)).unwrap();
                        actually_did_reserve_entry = queue.try_to_reserve_entry(car, true);
                    }
                }

                if !actually_did_reserve_entry {
                    if repeat_request {
                        self.blocked_by_someone_requests += 1;
                    }
                    return false;
                }
            }
        }

        // TODO For now, we're only interested in signals, and there's too much raw data to store
        // for stop signs too.
        let state = self.state.get_mut(&turn.parent).unwrap();
        state.waiting.remove(&req).unwrap();
        state.accepted.insert(req);
        if self.break_turn_conflict_cycles {
            if let AgentID::Car(car) = agent {
                self.blocked_by.retain(|(c, _)| *c != car);
            }
        }
        true
    }

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

    pub fn handle_live_edited_traffic_signals(
        &mut self,
        now: Time,
        map: &Map,
        scheduler: &mut Scheduler,
    ) {
        for state in self.state.values_mut() {
            match (
                map.maybe_get_traffic_signal(state.id),
                state.signal.as_mut(),
            ) {
                (Some(ts), Some(signal_state)) => {
                    if signal_state.current_stage >= ts.stages.len() {
                        // Just jump back to the first one. Shrug.
                        signal_state.current_stage = 0;
                        println!(
                            "WARNING: Traffic signal {} was live-edited in the middle of a stage, \
                             so jumping back to the first stage",
                            state.id
                        );
                    }
                }
                (Some(_), None) => {
                    state.signal = Some(SignalState::new(state.id, now, map, scheduler));
                }
                (None, Some(_)) => {
                    state.signal = None;
                    scheduler.cancel(Command::UpdateIntersection(state.id));
                }
                (None, None) => {}
            }

            // It's unlikely, but the player might create/destroy traffic signals close together and
            // change the uber-turns that exist. To be safe, recalculate everywhere.
            state.uber_turn_neighbors.clear();
            if let Some(mut set) = map_model::IntersectionCluster::autodetect(state.id, map) {
                set.remove(&state.id);
                state.uber_turn_neighbors.extend(set);
            }
        }
    }

    pub fn handle_live_edits(&self, map: &Map) {
        // Just sanity check that we don't have any references to deleted turns
        let mut errors = Vec::new();
        for state in self.state.values() {
            for req in &state.accepted {
                if map.maybe_get_t(req.turn).is_none() {
                    errors.push(format!("{} accepted for {}", req.agent, req.turn));
                }
            }
            for req in state.waiting.keys() {
                if map.maybe_get_t(req.turn).is_none() {
                    errors.push(format!("{} waiting for {}", req.agent, req.turn));
                }
            }
            for req in &state.reserved {
                if map.maybe_get_t(req.turn).is_none() {
                    errors.push(format!("{} has reserved {}", req.agent, req.turn));
                }
            }
        }
        if !errors.is_empty() {
            for x in errors {
                error!("{}", x);
            }
            panic!("After live map edits, intersection state refers to deleted turns!");
        }
    }

    // Not calling this for pedestrians right now.
    // This is "best effort". If we get something wrong, somebody might start a turn and cut off an
    // approaching vehicle.
    // And it's idempotent -- can call to update an ETA.
    pub fn approaching_leader(&mut self, agent: AgentID, turn: TurnID, eta: Time) {
        let state = self.state.get_mut(&turn.parent).unwrap();
        // If there was a previous entry here for turn.src, then this leader is spawning in front
        // of the previous leader on a driveway
        state
            .leader_eta
            .insert(turn.src, (Request { agent, turn }, eta));
    }
}

// Queries
impl IntersectionSimState {
    pub fn nobody_headed_towards(&self, lane: LaneID, i: IntersectionID) -> bool {
        let state = &self.state[&i];
        !state
            .accepted
            .iter()
            .chain(state.reserved.iter())
            .any(|req| req.turn.dst == lane)
    }

    pub fn debug_json(&self, id: IntersectionID, map: &Map) -> String {
        let json1 = abstutil::to_json(&self.state[&id]);
        let json2 = if let Some(ref sign) = map.maybe_get_stop_sign(id) {
            abstutil::to_json(sign)
        } else if let Some(ref signal) = map.maybe_get_traffic_signal(id) {
            abstutil::to_json(signal)
        } else {
            "\"Border\"".to_string()
        };
        format!("[{json1}, {json2}]")
    }

    pub fn get_accepted_agents(&self, id: IntersectionID) -> Vec<(AgentID, TurnID)> {
        self.state[&id]
            .accepted
            .iter()
            .map(|req| (req.agent, req.turn))
            .collect()
    }

    pub fn get_waiting_agents(&self, id: IntersectionID) -> Vec<(AgentID, TurnID, Time)> {
        self.state[&id]
            .waiting
            .iter()
            .map(|(req, (time, _))| (req.agent, req.turn, *time))
            .collect()
    }

    /// Returns intersections with travelers waiting for at least `threshold` since `now`, ordered
    /// so the longest delayed intersection is first.
    pub fn delayed_intersections(
        &self,
        now: Time,
        threshold: Duration,
    ) -> Vec<(IntersectionID, Time)> {
        let mut candidates = Vec::new();
        for state in self.state.values() {
            if let Some((earliest, _)) = state.waiting.values().min() {
                if now - *earliest >= threshold {
                    candidates.push((state.id, *earliest));
                }
            }
        }
        candidates.sort_by_key(|(_, t)| *t);
        candidates
    }

    pub fn current_stage_and_remaining_time(
        &self,
        now: Time,
        i: IntersectionID,
    ) -> (usize, Duration) {
        let state = &self.state[&i].signal.as_ref().unwrap();
        if now > state.stage_ends_at {
            panic!(
                "At {}, but {} should have advanced its stage at {}",
                now, i, state.stage_ends_at
            );
        }
        (state.current_stage, state.stage_ends_at - now)
    }

    pub fn describe_stats(&self) -> Vec<String> {
        vec![
            "intersection stats".to_string(),
            format!(
                "{} total turn requests repeated after the initial attempt",
                prettyprint_usize(self.total_repeat_requests)
            ),
            format!(
                "{} not allowed by intersection ({}%)",
                prettyprint_usize(self.not_allowed_requests),
                (100.0 * (self.not_allowed_requests as f64) / (self.total_repeat_requests as f64))
                    .round()
            ),
            format!(
                "{} blocked by someone in the way ({}%)",
                prettyprint_usize(self.blocked_by_someone_requests),
                (100.0 * (self.blocked_by_someone_requests as f64)
                    / (self.total_repeat_requests as f64))
                    .round()
            ),
        ]
    }

    pub fn populate_blocked_by(
        &self,
        now: Time,
        graph: &mut BTreeMap<AgentID, (Duration, DelayCause)>,
        map: &Map,
        cars: &FixedMap<CarID, Car>,
        queues: &HashMap<Traversable, Queue>,
    ) {
        // Don't use self.blocked_by -- that gets complicated with uber-turns and such.
        //
        // This also assumes default values for handle_uber_turns, disable_turn_conflicts, etc!
        for state in self.state.values() {
            for (req, (started_at, _)) in &state.waiting {
                let turn = map.get_t(req.turn);
                // In the absence of other explanations, the agent must be pausing at a stop sign
                // or before making an unprotected movement, aka, in the middle of
                // WAIT_AT_STOP_SIGN or WAIT_BEFORE_YIELD_AT_TRAFFIC_SIGNAL. Or they're waiting for
                // a signal to change.
                let mut cause = DelayCause::Intersection(state.id);
                if let Some(other) = state.accepted.iter().find(|other| {
                    turn.conflicts_with(map.get_t(other.turn)) || turn.id == other.turn
                }) {
                    cause = DelayCause::Agent(other.agent);
                } else if let AgentID::Car(car) = req.agent {
                    let queue = &queues[&Traversable::Lane(req.turn.dst)];
                    let car = cars.get(&car).unwrap();
                    if !queue.room_for_car(car) {
                        // TODO Or it's reserved due to an uber turn or something
                        let blocker = queue
                            .get_active_cars()
                            .last()
                            .cloned()
                            .or(queue.laggy_head)
                            .unwrap();
                        cause = DelayCause::Agent(AgentID::Car(blocker));
                    } else if let Some(ut) = car.router.get_path().about_to_start_ut() {
                        if let Some(blocker) = self.check_for_conflicts_before_uber_turn(ut, map) {
                            cause = DelayCause::Agent(blocker);
                        }
                    }
                }
                graph.insert(req.agent, (now - *started_at, cause));
            }
        }
    }

    /// See if any agent is currently performing a turn that conflicts with an uber-turn. Doesn't
    /// check for room on the queues.
    fn check_for_conflicts_before_uber_turn(&self, ut: &UberTurn, map: &Map) -> Option<AgentID> {
        for t in &ut.path {
            let turn = map.get_t(*t);
            let state = &self.state[&turn.id.parent];
            for other in state.accepted.iter().chain(state.reserved.iter()) {
                if map.get_t(other.turn).conflicts_with(turn) {
                    return Some(other.agent);
                }
            }
        }
        None
    }
}

// Stuff to support maybe_start_turn
impl IntersectionSimState {
    fn stop_sign_policy(
        &mut self,
        req: &Request,
        map: &Map,
        sign: &ControlStopSign,
        speed: Speed,
        now: Time,
        scheduler: &mut Scheduler,
    ) -> bool {
        let our_priority = sign.get_priority(req.turn, map);
        assert!(our_priority != TurnPriority::Banned);
        let (our_time, _) = self.state[&req.turn.parent].waiting[req];

        if our_priority == TurnPriority::Yield && now < our_time + WAIT_AT_STOP_SIGN {
            // Since we have "ownership" of scheduling for req.agent, don't need to use
            // scheduler.update.
            scheduler.push(
                our_time + WAIT_AT_STOP_SIGN,
                Command::update_agent(req.agent),
            );
            return false;
        }

        // Once upon a time, we'd make sure that this request doesn't conflict with another in
        // self.waiting:
        // 1) Higher-ranking turns get to go first.
        // 2) Equal-ranking turns that started waiting before us get to go first.
        // But the exceptions started stacking -- if the other agent is blocked or the turns don't
        // even conflict, then allow it. Except determining if the other agent is blocked or not is
        // tough and kind of recursive.
        //
        // So instead, don't do any of that! The WAIT_AT_STOP_SIGN scheduling above and the fact
        // that events are processed in time order mean that case #2 is magically handled anyway.
        // If a case #1 could've started by now, then they would have. Since they didn't, they must
        // be blocked.

        // TODO Make sure we can optimistically finish this turn before an approaching
        // higher-priority vehicle wants to begin.

        // If a pedestrian is going to cut off a car, check how long the car has been waiting and
        // maybe yield (regardless of stop sign priority). This is a very rough start to more
        // realistic "batching" of pedestrians to cross a street. Without this, if there's one
        // pedestrian almost clear of a crosswalk, cars are totally stopped for them, and so a new
        // pedestrian arriving will win.
        if req.agent.is_pedestrian() {
            let our_turn = map.get_t(req.turn);
            let time_to_cross = our_turn.geom.length() / speed;
            for (other_req, (other_time, _)) in &self.state[&req.turn.parent].waiting {
                if matches!(other_req.agent, AgentID::Car(_)) {
                    if our_turn.conflicts_with(map.get_t(other_req.turn)) {
                        let our_waiting = now - our_time;
                        let other_waiting = now - *other_time;
                        // We can't tell if a car has been waiting for a while due to pedestrians
                        // crossing, or due to a blockage in their destination queue. Always let
                        // pedestrians muscle their way in eventually.
                        if our_waiting > other_waiting {
                            continue;
                        }
                        // Intuition: another pedestrian trying to enter a crosswalk has a 3s
                        // buffer to "join" the first pedestrian who started crossing and caused
                        // cars to stop. We're using the time for _this_ pedestrian to cross _this_
                        // turn, so it's a very rough definition.
                        if other_waiting > time_to_cross + Duration::seconds(3.0) {
                            return false;
                        }
                    }
                }
            }
        }

        true
    }

    fn traffic_signal_policy(
        &mut self,
        req: &Request,
        map: &Map,
        signal: &ControlTrafficSignal,
        speed: Speed,
        now: Time,
        scheduler: Option<&mut Scheduler>,
    ) -> bool {
        let turn = map.get_t(req.turn);

        let state = &self.state[&req.turn.parent];
        let signal_state = state.signal.as_ref().unwrap();
        let stage = &signal.stages[signal_state.current_stage];
        let full_stage_duration = stage.stage_type.simple_duration();
        let remaining_stage_time = signal_state.stage_ends_at - now;
        let (our_time, _) = state.waiting[req];

        // Can't go at all this stage.
        let our_priority = stage.get_priority_of_turn(req.turn, map.get_i(state.id));
        if our_priority == TurnPriority::Banned {
            return false;
        }

        if our_priority == TurnPriority::Yield
            && now < our_time + WAIT_BEFORE_YIELD_AT_TRAFFIC_SIGNAL
        {
            // Since we have "ownership" of scheduling for req.agent, don't need to use
            // scheduler.update.
            if let Some(s) = scheduler {
                s.push(
                    our_time + WAIT_BEFORE_YIELD_AT_TRAFFIC_SIGNAL,
                    Command::update_agent(req.agent),
                );
            }
            return false;
        }

        // Previously: A yield loses to a conflicting Priority turn.
        // But similar to the description in stop_sign_policy, this caused unnecessary gridlock.
        // Priority vehicles getting scheduled first just requires a little tweak in
        // update_intersection.

        // TODO Make sure we can optimistically finish this turn before an approaching
        // higher-priority vehicle wants to begin.

        // Optimistically if nobody else is in the way, this is how long it'll take to finish the
        // turn. Don't start the turn if we won't finish by the time the light changes. If we get
        // it wrong, that's fine -- block the box a bit.
        let time_to_cross = turn.geom.length() / speed;
        if time_to_cross > remaining_stage_time {
            // Signals enforce a minimum crosswalk time, but some pedestrians are configured to
            // walk very slowly. In that case, allow them to go anyway and wind up in the crosswalk
            // during a red. This matches reality reasonably.
            if time_to_cross <= full_stage_duration {
                return false;
            }
        }

        true
    }

    // If true, the request can go.
    fn handle_accepted_conflicts(
        &mut self,
        req: &Request,
        map: &Map,
        maybe_cars_and_queues: Option<(&FixedMap<CarID, Car>, &HashMap<Traversable, Queue>)>,
        wakeup_stuck_cycle: Option<(Time, &mut Scheduler)>,
    ) -> bool {
        let turn = map.get_t(req.turn);
        let mut cycle_detected = false;
        let mut ok = true;
        for other in self.state[&req.turn.parent]
            .accepted
            .iter()
            .chain(self.state[&req.turn.parent].reserved.iter())
        {
            // Never short-circuit; always record all of the dependencies; it might help someone
            // else unstick things.
            if map.get_t(other.turn).conflicts_with(turn) {
                if self.break_turn_conflict_cycles {
                    if let AgentID::Car(c) = req.agent {
                        if let AgentID::Car(c2) = other.agent {
                            self.blocked_by.insert((c, c2));
                        }
                        if !cycle_detected {
                            if let Some(cycle) =
                                self.detect_conflict_cycle(c, maybe_cars_and_queues.unwrap())
                            {
                                // Allow the conflicting turn!
                                self.events.push(Event::Alert(
                                    AlertLocation::Intersection(req.turn.parent),
                                    format!(
                                        "{} found turn conflict cycle involving {:?}",
                                        req.agent, cycle
                                    ),
                                ));
                                cycle_detected = true;
                            }
                        }
                    }
                }

                if !cycle_detected && !self.disable_turn_conflicts {
                    ok = false;
                }

                // It's never safe for two vehicles to go for the same lane.
                // TODO I'm questioning this now. If the source is the same, then queueing will
                // work normally. If not, then... maybe we need to allow concurrent turns from
                // different lanes into the same lane, and somehow make the queueing work out.
                if turn.id.dst == other.turn.dst {
                    if let (Some((now, scheduler)), AgentID::Car(blocker), Some((cars, _))) = (
                        wakeup_stuck_cycle,
                        other.agent,
                        maybe_cars_and_queues.as_ref(),
                    ) {
                        // Sometimes the vehicle blocking us is actually queued in the turn;
                        // don't wake them up in that case.
                        if cycle_detected
                            && matches!(cars[&blocker].state, CarState::WaitingToAdvance { .. })
                        {
                            self.events.push(Event::Alert(
                                AlertLocation::Intersection(req.turn.parent),
                                format!(
                                    "{} waking up {}, who's blocking it as part of a cycle",
                                    req.agent, other.agent
                                ),
                            ));
                            scheduler.update(
                                now + Duration::EPSILON,
                                Command::update_agent(other.agent),
                            );
                        }
                    }
                    return false;
                }
            }
        }
        ok
    }

    fn detect_conflict_cycle(
        &self,
        car: CarID,
        pair: (&FixedMap<CarID, Car>, &HashMap<Traversable, Queue>),
    ) -> Option<HashSet<CarID>> {
        let (cars, queues) = pair;

        let mut queue = vec![car];
        let mut seen = HashSet::new();
        while !queue.is_empty() {
            let current = queue.pop().unwrap();
            // Might not actually be a cycle. Insist on seeing the original req.agent
            // again.
            if !seen.is_empty() && current == car {
                return Some(seen);
            }
            if !seen.contains(&current) {
                seen.insert(current);

                for (c1, c2) in &self.blocked_by {
                    if *c1 == current {
                        queue.push(*c2);
                    }
                }

                // If this car isn't the head of its queue, add that dependency. (Except for
                // the original car, which we already know is the head of its queue)
                // TODO Maybe store this in blocked_by?
                if current != car {
                    let q = &queues[&cars[&current].router.head()];
                    let head = if let Some(c) = q.laggy_head {
                        c
                    } else {
                        q.get_active_cars()[0]
                    };
                    if current != head {
                        queue.push(head);
                    }
                }
            }
        }
        None
    }
}

impl SignalState {
    fn new(id: IntersectionID, now: Time, map: &Map, scheduler: &mut Scheduler) -> SignalState {
        let mut state = SignalState {
            current_stage: 0,
            stage_ends_at: now,
            extensions_count: 0,
        };

        let signal = map.get_traffic_signal(id);
        // What stage are we starting with?
        let mut offset = (now - Time::START_OF_DAY) + signal.offset;
        loop {
            let dt = signal.stages[state.current_stage]
                .stage_type
                .simple_duration();
            if offset >= dt {
                offset -= dt;
                state.current_stage += 1;
                if state.current_stage == signal.stages.len() {
                    state.current_stage = 0;
                }
            } else {
                state.stage_ends_at = now + dt - offset;
                break;
            }
        }
        scheduler.push(state.stage_ends_at, Command::UpdateIntersection(id));
        state
    }
}

fn allow_block_the_box(i: &Intersection) -> bool {
    // Degenerate intersections are often just artifacts of how roads are split up in OSM. Allow
    // vehicles to get stuck in them, since the only possible thing they could block is pedestrians
    // from using the crosswalk. Those crosswalks usually don't exist in reality, so this behavior
    // is more realistic.
    if i.roads.len() == 2 {
        return true;
    }

    // TODO Sometimes a traffic signal is surrounded by tiny lanes with almost no capacity.
    // Workaround for now.
    //
    // When adding new cases:
    // 1) Organize by which map the intersection fixes
    // 2) Ensure a prebaked scenario covers this, to track regressions and make sure it actually
    //    helps.
    let id = i.orig_id.0;
    // lakeslice
    if id == 53211693
        || id == 53214134
        || id == 53214133
        || id == 987334546
        || id == 848817336
        || id == 1726088131
        || id == 1726088130
        || id == 53217946
        || id == 53223864
        || id == 53211694
        || id == 5440360144
        || id == 246768814
    {
        return true;
    }
    // poundbury
    if id == 18030505 || id == 2124133018 || id == 30024649 {
        return true;
    }
    false
}