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,
            &BTreeMap<CarID, Car>,
            &mut BTreeMap<Traversable, Queue>,
        )>,
    ) -> bool {
        let req = Request { agent, turn };
        self.state
            .get_mut(&turn.parent)
            .unwrap()
            .waiting
            .entry(req.clone())
            .or_insert(now);

        let readonly_pair = maybe_cars_and_queues.as_ref().map(|(_, c, q)| (*c, &**q));
        let allowed = if self.use_freeform_policy_everywhere {
            self.freeform_policy(&req, map, readonly_pair)
        } else if let Some(ref signal) = map.maybe_get_traffic_signal(turn.parent) {
            self.traffic_signal_policy(&req, map, signal, speed, now, scheduler, readonly_pair)
        } else if let Some(ref sign) = map.maybe_get_stop_sign(turn.parent) {
            self.stop_sign_policy(&req, map, sign, now, scheduler, readonly_pair)
        } else {
            unreachable!()
        };
        if !allowed {
            return false;
        }

        // Don't block the box
        if let Some((car, _, queues)) = maybe_cars_and_queues {
            assert_eq!(agent, AgentID::Car(car.vehicle.id));
            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).orig_id.osm_node_id),
            ) {
                if self.break_turn_conflict_cycles {
                    // TODO Should we run the detector here?
                    if let Some(c) = queue.laggy_head {
                        self.blocked_by.insert((car.vehicle.id, c));
                    } else if let Some(c) = queue.cars.get(0) {
                        self.blocked_by.insert((car.vehicle.id, *c));
                    } else {
                        // Nobody's in the target lane, but there's somebody already in the
                        // intersection headed there, taking up all of the space.
                        self.blocked_by.insert((
                            car.vehicle.id,
                            self.state[&turn.parent]
                                .accepted
                                .iter()
                                .find(|r| r.turn.dst == turn.dst)
                                .unwrap()
                                .agent
                                .as_car(),
                        ));
                    }
                }

                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();
        let delay = now - state.waiting.remove(&req).unwrap();
        if map.maybe_get_traffic_signal(state.id).is_some() {
            self.events.push(Event::IntersectionDelayMeasured(
                turn.parent,
                delay,
                TripMode::from_agent(agent),
            ));
        }
        state.accepted.insert(req);
        if self.break_turn_conflict_cycles {
            if let AgentID::Car(car) = agent {
                retain_btreeset(&mut self.blocked_by, |(c, _)| *c != car);
            }
        }

        true
    }
// 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.
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.
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,
            &BTreeMap<CarID, Car>,
            &mut BTreeMap<Traversable, Queue>,
        )>,
    ) -> bool {
        let req = Request { agent, turn };
        self.state
            .get_mut(&turn.parent)
            .unwrap()
            .waiting
            .entry(req.clone())
            .or_insert(now);
        let readonly_pair = maybe_cars_and_queues.as_ref().map(|(_, c, q)| (*c, &**q));
        let allowed = if self.use_freeform_policy_everywhere {
            self.freeform_policy(&req, map, readonly_pair)
        } else if let Some(ref signal) = map.maybe_get_traffic_signal(turn.parent) {
            self.traffic_signal_policy(&req, map, signal, speed, now, scheduler, readonly_pair)
        } else if let Some(ref sign) = map.maybe_get_stop_sign(turn.parent) {
            self.stop_sign_policy(&req, map, sign, now, scheduler, readonly_pair)
        } else {
            unreachable!()
        };
        if !allowed {
            return false;
        }
        // Don't block the box
        if let Some((car, _, queues)) = maybe_cars_and_queues {
            assert_eq!(agent, AgentID::Car(car.vehicle.id));
            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).orig_id.osm_node_id),
            ) {
                if self.break_turn_conflict_cycles {
                    // TODO Should we run the detector here?
                    if let Some(c) = queue.laggy_head {
                        self.blocked_by.insert((car.vehicle.id, c));
                    } else if let Some(c) = queue.cars.get(0) {
                        self.blocked_by.insert((car.vehicle.id, *c));
                    } else {
                        // Nobody's in the target lane, but there's somebody already in the
                        // intersection headed there, taking up all of the space.
                        self.blocked_by.insert((
                            car.vehicle.id,
                            self.state[&turn.parent]
                                .accepted
                                .iter()
                                .find(|r| r.turn.dst == turn.dst)
                                .unwrap()
                                .agent
                                .as_car(),
                        ));
                    }
                }
                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();
        let delay = now - state.waiting.remove(&req).unwrap();
        if map.maybe_get_traffic_signal(state.id).is_some() {
            self.events.push(Event::IntersectionDelayMeasured(
                turn.parent,
                delay,
                TripMode::from_agent(agent),
            ));
        }
        state.accepted.insert(req);
        if self.break_turn_conflict_cycles {
            if let AgentID::Car(car) = agent {
                retain_btreeset(&mut self.blocked_by, |(c, _)| *c != car);
            }
        }
        true
    }
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 , & BTreeMap < CarID , Car > , & mut BTreeMap < Traversable , Queue > , ) > , ) -> bool { let req = Request { agent , turn } ; self . state . get_mut ( & turn . parent ) . unwrap ( ) . waiting . entry ( req . clone ( ) ) . or_insert ( now ) ; let readonly_pair = maybe_cars_and_queues . as_ref ( ) . map ( | ( _ , c , q ) | ( * c , & * * q ) ) ; let allowed = if self . use_freeform_policy_everywhere { self . freeform_policy ( & req , map , readonly_pair ) } else if let Some ( ref signal ) = map . maybe_get_traffic_signal ( turn . parent ) { self . traffic_signal_policy ( & req , map , signal , speed , now , scheduler , readonly_pair ) } else if let Some ( ref sign ) = map . maybe_get_stop_sign ( turn . parent ) { self . stop_sign_policy ( & req , map , sign , now , scheduler , readonly_pair ) } else { unreachable ! ( ) } ; if ! allowed { return false ; } // Don't block the box if let Some ( ( car , _ , queues ) ) = maybe_cars_and_queues { assert_eq ! ( agent AgentID Car ( car vehicle id ) ) ; 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 ) . orig_id . osm_node_id ) , ) { if self . break_turn_conflict_cycles { // TODO Should we run the detector here? if let Some ( c ) = queue . laggy_head { self . blocked_by . insert ( ( car . vehicle . id , c ) ) ; } else if let Some ( c ) = queue . cars . get ( 0 ) { self . blocked_by . insert ( ( car . vehicle . id , * c ) ) ; } else { // Nobody's in the target lane, but there's somebody already in the // intersection headed there, taking up all of the space. self . blocked_by . insert ( ( car . vehicle . id , self . state [ & turn . parent ] . accepted . iter ( ) . find ( | r | r . turn . dst == turn . dst ) . unwrap ( ) . agent . as_car ( ) , ) ) ; } } 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 ( ) ; let delay = now - state . waiting . remove ( & req ) . unwrap ( ) ; if map . maybe_get_traffic_signal ( state . id ) . is_some ( ) { self . events . push ( Event :: IntersectionDelayMeasured ( turn . parent , delay , TripMode :: from_agent ( agent ) , ) ) ; } state . accepted . insert ( req ) ; if self . break_turn_conflict_cycles { if let AgentID :: Car ( car ) = agent { retain_btreeset ( & mut self . blocked_by , | ( c , _ ) | * c != car ) ; } } true }
For cars: The head car calls this when they're at the end of the lane WaitingToAdvance.
For cars : The head car calls this when they ' re at the end of the lane WaitingToAdvance .
&mut self
agent
AgentID
turn
TurnID
speed
Speed
now
Time
map
&Map
scheduler
&mut Scheduler
maybe_cars_and_queues
Option<(
            &Car,
            &BTreeMap<CarID, Car>,
            &mut BTreeMap<Traversable, Queue>,
        )>
&mut self
agent
AgentID
turn
TurnID
speed
Speed
now
Time
map
&Map
scheduler
&mut Scheduler
maybe_cars_and_queues
Option<(
            &Car,
            &BTreeMap<CarID, Car>,
            &mut BTreeMap<Traversable, Queue>,
        )>