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

use map_gui::tools::{ColorDiscrete, PopupMsg};
use map_model::{connectivity, EditCmd, LaneID, LaneType, Map, PathConstraints};
use widgetry::{Color, EventCtx, State};

use crate::app::App;

// All 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);

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

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

    let mut c = ColorDiscrete::new(app, vec![("disconnected", Color::RED)]);
    let num = newly_disconnected.len();
    for l in newly_disconnected {
        c.add_l(*l, "disconnected");
    }
    let (unzoomed, zoomed, _) = c.build(ctx);

    Some(PopupMsg::also_draw(
        ctx,
        "Error",
        vec![format!(
            "Can't close this intersection; {} sidewalks disconnected",
            num
        )],
        unzoomed,
        zoomed,
    ))
}

#[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);

    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);

    if newly_disconnected.is_empty() {
        return None;
    }

    let mut c = ColorDiscrete::new(app, vec![("disconnected", Color::RED)]);
    let num = newly_disconnected.len();
    for l in newly_disconnected {
        c.add_l(l, "disconnected");
    }
    let (unzoomed, zoomed, _) = c.build(ctx);

    Some(PopupMsg::also_draw(
        ctx,
        "Error",
        vec![format!("{} lanes have been disconnected", num)],
        unzoomed,
        zoomed,
    ))
}

pub fn try_change_lt(
    ctx: &mut EventCtx,
    map: &mut Map,
    l: LaneID,
    new_lt: LaneType,
) -> Result<EditCmd, Box<dyn State<App>>> {
    let orig_edits = map.get_edits().clone();

    let mut edits = orig_edits.clone();
    let cmd = {
        let r = map.get_l(l).parent;
        map.edit_road_cmd(r, |new| {
            new.lanes_ltr[map.get_r(r).offset(l)].0 = new_lt;
        })
    };
    edits.commands.push(cmd.clone());
    map.try_apply_edits(edits);

    let mut errors = Vec::new();
    let r = map.get_parent(l);

    // TODO Ban two adjacent parking lanes (What about dppd though?)

    // A parking lane must have a driving lane somewhere on the road.
    let all_types: BTreeSet<LaneType> = r.lanes_ltr().into_iter().map(|(_, _, lt)| lt).collect();
    if all_types.contains(&LaneType::Parking) && !all_types.contains(&LaneType::Driving) {
        errors.push(format!(
            "A parking lane needs a driving lane somewhere on the same road"
        ));
    }

    // Don't let players orphan a bus stop.
    // TODO This allows a bus stop switching sides of the road. Really need to re-do bus matching
    // and make sure nothing's broken (https://github.com/dabreegster/abstreet/issues/93).
    if !r.all_bus_stops(map).is_empty()
        && !r
            .lanes_ltr()
            .into_iter()
            .any(|(l, _, _)| PathConstraints::Bus.can_use(map.get_l(l), map))
    {
        errors.push(format!("You need a driving or bus lane for the bus stop!"));
    }

    map.must_apply_edits(orig_edits);
    if errors.is_empty() {
        Ok(cmd)
    } else {
        Err(PopupMsg::new(ctx, "Error", errors))
    }
}