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
use std::collections::BTreeSet;

use abstutil::Timer;
use map_model::{connectivity, Direction, DrivingSide, EditCmd, Map, PathConstraints};
use widgetry::tools::PopupMsg;
use widgetry::{EventCtx, State};

use crate::app::App;

// Some of these take a candidate EditCmd to do, then see if it's valid. If they return None, it's
// fine. They always leave the map in the original state without the new EditCmd.

// Could be caused by closing intersections
pub fn check_sidewalk_connectivity(
    ctx: &mut EventCtx,
    app: &mut App,
    cmd: EditCmd,
) -> Option<Box<dyn State<App>>> {
    let orig_edits = app.primary.map.get_edits().clone();
    let (_, disconnected_before) =
        connectivity::find_scc(&app.primary.map, PathConstraints::Pedestrian);

    let mut edits = orig_edits.clone();
    edits.commands.push(cmd);
    app.primary
        .map
        .try_apply_edits(edits, &mut Timer::throwaway());

    let (_, disconnected_after) =
        connectivity::find_scc(&app.primary.map, PathConstraints::Pedestrian);
    app.primary
        .map
        .must_apply_edits(orig_edits, &mut Timer::throwaway());

    let newly_disconnected = disconnected_after
        .difference(&disconnected_before)
        .collect::<Vec<_>>();
    if newly_disconnected.is_empty() {
        return None;
    }

    // TODO Think through a proper UI for showing editing errors to the user and letting them
    // understand the problem. We used to just draw problems in red and mostly cover it up with the
    // popup.
    Some(PopupMsg::new_state(
        ctx,
        "Error",
        vec![format!(
            "Can't close this intersection; {} sidewalks disconnected",
            newly_disconnected.len()
        )],
    ))
}

#[allow(unused)]
// Could be caused by closing intersections, changing lane types, or reversing lanes
pub fn check_blackholes(
    ctx: &mut EventCtx,
    app: &mut App,
    cmd: EditCmd,
) -> Option<Box<dyn State<App>>> {
    let orig_edits = app.primary.map.get_edits().clone();
    let mut driving_ok_originally = BTreeSet::new();
    let mut biking_ok_originally = BTreeSet::new();
    for l in app.primary.map.all_lanes() {
        if !l.driving_blackhole {
            driving_ok_originally.insert(l.id);
        }
        if !l.biking_blackhole {
            biking_ok_originally.insert(l.id);
        }
    }

    let mut edits = orig_edits.clone();
    edits.commands.push(cmd);
    app.primary
        .map
        .try_apply_edits(edits, &mut Timer::throwaway());

    let mut newly_disconnected = BTreeSet::new();
    for l in connectivity::find_scc(&app.primary.map, PathConstraints::Car).1 {
        if driving_ok_originally.contains(&l) {
            newly_disconnected.insert(l);
        }
    }
    for l in connectivity::find_scc(&app.primary.map, PathConstraints::Bike).1 {
        if biking_ok_originally.contains(&l) {
            newly_disconnected.insert(l);
        }
    }
    app.primary
        .map
        .must_apply_edits(orig_edits, &mut Timer::throwaway());

    if newly_disconnected.is_empty() {
        return None;
    }

    Some(PopupMsg::new_state(
        ctx,
        "Error",
        vec![format!(
            "{} lanes have been disconnected",
            newly_disconnected.len()
        )],
    ))
}

/// Looks at all changed roads and makes sure sidewalk directions are correct -- this is easy for
/// the user to mix up. Returns a list of new fixes to apply on top of the original edits.
pub fn fix_sidewalk_direction(map: &Map) -> Vec<EditCmd> {
    let mut fixes = Vec::new();
    for cmd in &map.get_edits().commands {
        if let EditCmd::ChangeRoad { r, new, .. } = cmd {
            let mut fixed = new.clone();
            if fixed.lanes_ltr[0].lt.is_walkable() {
                fixed.lanes_ltr[0].dir = if map.get_config().driving_side == DrivingSide::Right {
                    Direction::Back
                } else {
                    Direction::Fwd
                };
            }
            if fixed.lanes_ltr.len() > 1 {
                let last = fixed.lanes_ltr.last_mut().unwrap();
                if last.lt.is_walkable() {
                    last.dir = if map.get_config().driving_side == DrivingSide::Right {
                        Direction::Fwd
                    } else {
                        Direction::Back
                    };
                }
            }
            if &fixed != new {
                fixes.push(EditCmd::ChangeRoad {
                    r: *r,
                    old: new.clone(),
                    new: fixed,
                });
            }
        }
    }
    fixes
}