cycle indices got out of sync easily. no need to store them at all.

This commit is contained in:
Dustin Carlino 2019-08-02 16:19:02 +02:00
parent 7b3a096a8e
commit 4a9a40c09f
6 changed files with 21 additions and 21 deletions

View File

@ -41,7 +41,7 @@ impl TurnCyclerState {
.contextual_action(Key::F, "show full traffic signal diagram")
{
ui.primary.current_selection = None;
let (cycle, _) =
let (idx, _, _) =
signal.current_cycle_and_remaining_time(ui.primary.sim.time());
return Some(Transition::Push(Box::new(ShowTrafficSignal {
menu: ModalMenu::new(
@ -55,7 +55,7 @@ impl TurnCyclerState {
],
ctx,
),
diagram: TrafficSignalDiagram::new(i, cycle.idx, &ui.primary.map, ctx),
diagram: TrafficSignalDiagram::new(i, idx, &ui.primary.map, ctx),
})));
}
}

View File

@ -178,15 +178,14 @@ impl State for TrafficSignalEditor {
ctx,
);
} else if self.menu.action("add a new empty cycle") {
signal.cycles.insert(
current_cycle,
Cycle::new(self.diagram.i, signal.cycles.len()),
);
signal
.cycles
.insert(current_cycle, Cycle::new(self.diagram.i));
change_traffic_signal(signal, self.diagram.i, ui, ctx);
self.diagram =
TrafficSignalDiagram::new(self.diagram.i, current_cycle, &ui.primary.map, ctx);
} else if has_sidewalks && self.menu.action("add a new pedestrian scramble cycle") {
let mut cycle = Cycle::new(self.diagram.i, signal.cycles.len());
let mut cycle = Cycle::new(self.diagram.i);
for t in ui.primary.map.get_turns_in_intersection(self.diagram.i) {
if t.between_sidewalks() {
cycle.edit_turn(t, TurnPriority::Priority);
@ -309,7 +308,7 @@ fn convert_to_ped_scramble(signal: &mut ControlTrafficSignal, i: IntersectionID,
}
}
let mut cycle = Cycle::new(i, signal.cycles.len());
let mut cycle = Cycle::new(i);
for t in map.get_turns_in_intersection(i) {
if t.between_sidewalks() {
cycle.edit_turn(t, TurnPriority::Priority);

View File

@ -146,7 +146,7 @@ impl Renderable for DrawIntersection {
.map(|(_, t)| *t != ctx.sim.time())
.unwrap_or(true);
if recalc {
let (cycle, t) = signal.current_cycle_and_remaining_time(ctx.sim.time());
let (_, cycle, t) = signal.current_cycle_and_remaining_time(ctx.sim.time());
let mut batch = GeomBatch::new();
draw_signal_cycle(cycle, Some(t), &mut batch, ctx);
*maybe_redraw = Some((g.prerender.upload(batch), ctx.sim.time()));

View File

@ -204,7 +204,10 @@ impl TrafficSignalDiagram {
let scroller = Scroller::new(
ScreenPt::new(0.0, 0.0),
cycles.iter().map(|cycle| (cycle.idx, item_dims)).collect(),
std::iter::repeat(item_dims)
.take(cycles.len())
.enumerate()
.collect(),
current_cycle,
ctx,
);

View File

@ -48,12 +48,12 @@ impl ControlTrafficSignal {
results
}
pub fn current_cycle_and_remaining_time(&self, time: Duration) -> (&Cycle, Duration) {
pub fn current_cycle_and_remaining_time(&self, time: Duration) -> (usize, &Cycle, Duration) {
let cycle_idx = (time / CYCLE_DURATION).floor() as usize;
let cycle = &self.cycles[cycle_idx % self.cycles.len()];
let next_cycle_time = CYCLE_DURATION * (cycle_idx + 1) as f64;
let remaining_cycle_time = next_cycle_time - time;
(cycle, remaining_cycle_time)
(cycle_idx, cycle, remaining_cycle_time)
}
fn validate(&self, map: &Map) -> Result<(), Error> {
@ -113,7 +113,7 @@ impl ControlTrafficSignal {
.iter()
.map(|t| t.id)
.collect();
let mut current_cycle = Cycle::new(intersection, cycles.len());
let mut current_cycle = Cycle::new(intersection);
loop {
let add_turn = remaining_turns
.iter()
@ -126,7 +126,7 @@ impl ControlTrafficSignal {
}
None => {
cycles.push(current_cycle);
current_cycle = Cycle::new(intersection, cycles.len());
current_cycle = Cycle::new(intersection);
if remaining_turns.is_empty() {
break;
}
@ -379,17 +379,15 @@ impl ControlTrafficSignal {
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Cycle {
pub parent: IntersectionID,
pub idx: usize,
pub priority_turns: BTreeSet<TurnID>,
pub yield_turns: BTreeSet<TurnID>,
pub duration: Duration,
}
impl Cycle {
pub fn new(parent: IntersectionID, idx: usize) -> Cycle {
pub fn new(parent: IntersectionID) -> Cycle {
Cycle {
parent,
idx,
priority_turns: BTreeSet::new(),
yield_turns: BTreeSet::new(),
duration: CYCLE_DURATION,
@ -459,8 +457,8 @@ fn make_cycles(
) -> Vec<Cycle> {
let mut cycles: Vec<Cycle> = Vec::new();
for (idx, specs) in cycle_specs.into_iter().enumerate() {
let mut cycle = Cycle::new(i, idx);
for specs in cycle_specs {
let mut cycle = Cycle::new(i);
for (roads, turn_type, protected) in specs.into_iter() {
for turn in map.get_turns_in_intersection(i) {

View File

@ -86,7 +86,7 @@ impl IntersectionSimState {
scheduler: &mut Scheduler,
) {
let state = &self.state[&id];
let (_, remaining) = map
let (_, _, remaining) = map
.get_traffic_signal(id)
.current_cycle_and_remaining_time(now);
@ -225,7 +225,7 @@ impl State {
time: Duration,
map: &Map,
) -> bool {
let (cycle, remaining_cycle_time) = signal.current_cycle_and_remaining_time(time);
let (_, cycle, remaining_cycle_time) = signal.current_cycle_and_remaining_time(time);
// Can't go at all this cycle.
if cycle.get_priority(new_req.turn) == TurnPriority::Banned {