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
use crate::app::App;
use crate::common::ColorDiscrete;
use crate::game::{PopupMsg, State};
use abstutil::Timer;
use ezgui::{Color, EventCtx};
use map_model::{connectivity, EditCmd, LaneID, LaneType, Map, PathConstraints};
use std::collections::BTreeSet;
pub fn check_sidewalk_connectivity(
ctx: &mut EventCtx,
app: &mut App,
cmd: EditCmd,
) -> Option<Box<dyn State>> {
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;
}
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)]
pub fn check_blackholes(ctx: &mut EventCtx, app: &mut App, cmd: EditCmd) -> Option<Box<dyn State>> {
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;
}
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>> {
let orig_edits = map.get_edits().clone();
let mut edits = orig_edits.clone();
let cmd = EditCmd::ChangeLaneType {
id: l,
lt: new_lt,
orig_lt: map.get_l(l).lane_type,
};
edits.commands.push(cmd.clone());
map.try_apply_edits(edits, &mut Timer::throwaway());
let mut errors = Vec::new();
let r = map.get_parent(l);
let (fwd, back) = r.get_lane_types();
let all_types: BTreeSet<LaneType> = fwd.chain(back).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"
));
}
if !r.all_bus_stops(map).is_empty()
&& !r
.children(r.is_forwards(l))
.iter()
.any(|(_, lt)| *lt == LaneType::Driving || *lt == LaneType::Bus)
{
errors.push(format!("You need a driving or bus lane for the bus stop!"));
}
map.must_apply_edits(orig_edits, &mut Timer::throwaway());
if errors.is_empty() {
Ok(cmd)
} else {
Err(PopupMsg::new(ctx, "Error", errors))
}
}
pub fn try_reverse(ctx: &mut EventCtx, map: &Map, l: LaneID) -> Result<EditCmd, Box<dyn State>> {
let lane = map.get_l(l);
if map.get_r(lane.parent).dir_and_offset(l).1 != 0 {
Err(PopupMsg::new(
ctx,
"Error",
vec!["You can only reverse the lanes next to the road's yellow center line"],
))
} else {
Ok(EditCmd::ReverseLane {
l,
dst_i: lane.src_i,
})
}
}