Skip to content

Commit

Permalink
remove edits_name from SimFlags and refactor loading MapEdits by name…
Browse files Browse the repository at this point in the history
…. fixes bug where quitting an a/b test breaks loading other maps by retaining edits_name
  • Loading branch information
dabreegster committed Jun 8, 2019
1 parent 53d87af commit 8eb03b8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 72 deletions.
23 changes: 12 additions & 11 deletions editor/src/abtest/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,12 @@ fn launch_test(test: &ABTest, ui: &mut UI, ctx: &mut EventCtx) -> Mode {
timer.start("load primary");
ui.primary.current_flags.sim_flags.run_name =
Some(format!("{} with {}", test.test_name, test.edits1_name));
let edits1: MapEdits = if test.edits1_name == "no_edits" {
MapEdits::new(test.map_name.clone())
} else {
abstutil::read_json(&format!(
"../data/edits/{}/{}.json",
test.map_name, test.edits1_name,
))
.unwrap()
};
apply_map_edits(ui, ctx, edits1);
apply_map_edits(
&mut ui.primary,
&ui.cs,
ctx,
MapEdits::load(&test.map_name, &test.edits1_name),
);

let scenario: Scenario = abstutil::read_binary(load.to_str().unwrap(), &mut timer)
.expect("loading scenario failed");
Expand All @@ -136,14 +132,19 @@ fn launch_test(test: &ABTest, ui: &mut UI, ctx: &mut EventCtx) -> Mode {
load,
rng_seed: current_flags.sim_flags.rng_seed,
run_name: Some(format!("{} with {}", test.test_name, test.edits2_name)),
edits_name: test.edits2_name.clone(),
},
..current_flags.clone()
},
&ui.cs,
ctx,
&mut timer,
);
apply_map_edits(
&mut secondary,
&ui.cs,
ctx,
MapEdits::load(&test.map_name, &test.edits2_name),
);
secondary.sim.step(&secondary.map, Duration::seconds(0.1));
timer.stop("load secondary");
secondary
Expand Down
74 changes: 44 additions & 30 deletions editor/src/edit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ mod traffic_signals;
use crate::common::CommonState;
use crate::debug::DebugMode;
use crate::game::{GameState, Mode};
use crate::helpers::ID;
use crate::helpers::{ColorScheme, ID};
use crate::render::{
DrawCtx, DrawIntersection, DrawLane, DrawMap, DrawOptions, DrawTurn, Renderable,
MIN_ZOOM_FOR_DETAIL,
};
use crate::sandbox::SandboxMode;
use crate::ui::{ShowEverything, UI};
use crate::ui::{PerMapUI, ShowEverything, UI};
use abstutil::Timer;
use ezgui::{
hotkey, lctrl, Color, EventCtx, EventLoopMode, GfxCtx, Key, ModalMenu, Text, Wizard,
Expand Down Expand Up @@ -133,7 +133,12 @@ impl EditMode {
) {
let mut new_edits = orig_edits.clone();
new_edits.lane_overrides.insert(lane.id, new_type);
apply_map_edits(&mut state.ui, ctx, new_edits);
apply_map_edits(
&mut state.ui.primary,
&state.ui.cs,
ctx,
new_edits,
);
}
}
}
Expand All @@ -156,7 +161,12 @@ impl EditMode {
{
let mut new_edits = orig_edits.clone();
new_edits.lane_overrides.insert(lane.id, *lt);
apply_map_edits(&mut state.ui, ctx, new_edits);
apply_map_edits(
&mut state.ui.primary,
&state.ui.cs,
ctx,
new_edits,
);
break;
}
}
Expand All @@ -176,7 +186,7 @@ impl EditMode {
{
let mut new_edits = orig_edits.clone();
new_edits.lane_overrides.remove(&id);
apply_map_edits(&mut state.ui, ctx, new_edits);
apply_map_edits(&mut state.ui.primary, &state.ui.cs, ctx, new_edits);
}
}
if let Some(ID::Intersection(id)) = state.ui.primary.current_selection {
Expand All @@ -193,7 +203,7 @@ impl EditMode {
{
let mut new_edits = orig_edits.clone();
new_edits.stop_sign_overrides.remove(&id);
apply_map_edits(&mut state.ui, ctx, new_edits);
apply_map_edits(&mut state.ui.primary, &state.ui.cs, ctx, new_edits);
}
}
if state.ui.primary.map.maybe_get_traffic_signal(id).is_some() {
Expand All @@ -209,7 +219,7 @@ impl EditMode {
{
let mut new_edits = orig_edits.clone();
new_edits.traffic_signal_overrides.remove(&id);
apply_map_edits(&mut state.ui, ctx, new_edits);
apply_map_edits(&mut state.ui.primary, &state.ui.cs, ctx, new_edits);
}
}
}
Expand All @@ -229,7 +239,7 @@ impl EditMode {
&mut wizard.wrap(ctx),
"Load which map edits?",
) {
apply_map_edits(&mut state.ui, ctx, new_edits);
apply_map_edits(&mut state.ui.primary, &state.ui.cs, ctx, new_edits);
state.mode = Mode::Edit(EditMode::new(ctx, &mut state.ui));
} else if wizard.aborted() {
state.mode = Mode::Edit(EditMode::new(ctx, &mut state.ui));
Expand All @@ -248,7 +258,7 @@ impl EditMode {
Mode::Edit(EditMode::BulkEditLanes(r, ref mut wizard)) => {
ctx.canvas.handle_event(ctx.input);
if let Some(edits) = bulk_edit(r, &mut wizard.wrap(ctx), &state.ui.primary.map) {
apply_map_edits(&mut state.ui, ctx, edits);
apply_map_edits(&mut state.ui.primary, &state.ui.cs, ctx, edits);
state.mode = Mode::Edit(EditMode::new(ctx, &mut state.ui));
} else if wizard.aborted() {
state.mode = Mode::Edit(EditMode::new(ctx, &mut state.ui));
Expand Down Expand Up @@ -464,26 +474,30 @@ fn can_change_lane_type(r: &Road, l: &Lane, lt: LaneType, map: &Map) -> bool {
true
}

pub fn apply_map_edits(ui: &mut UI, ctx: &mut EventCtx, edits: MapEdits) {
pub fn apply_map_edits(
bundle: &mut PerMapUI,
cs: &ColorScheme,
ctx: &mut EventCtx,
edits: MapEdits,
) {
let mut timer = Timer::new("apply map edits");

ui.primary.current_flags.sim_flags.edits_name = edits.edits_name.clone();
let (lanes_changed, turns_deleted, turns_added) = ui.primary.map.apply_edits(edits, &mut timer);
let (lanes_changed, turns_deleted, turns_added) = bundle.map.apply_edits(edits, &mut timer);

for l in lanes_changed {
ui.primary.draw_map.lanes[l.0] = DrawLane::new(
ui.primary.map.get_l(l),
&ui.primary.map,
!ui.primary.current_flags.dont_draw_lane_markings,
&ui.cs,
bundle.draw_map.lanes[l.0] = DrawLane::new(
bundle.map.get_l(l),
&bundle.map,
!bundle.current_flags.dont_draw_lane_markings,
cs,
ctx.prerender,
&mut timer,
);
}
let mut modified_intersections: BTreeSet<IntersectionID> = BTreeSet::new();
let mut lanes_of_modified_turns: BTreeSet<LaneID> = BTreeSet::new();
for t in turns_deleted {
ui.primary.draw_map.turns.remove(&t);
bundle.draw_map.turns.remove(&t);
lanes_of_modified_turns.insert(t.src);
modified_intersections.insert(t.parent);
}
Expand All @@ -496,32 +510,32 @@ pub fn apply_map_edits(ui: &mut UI, ctx: &mut EventCtx, edits: MapEdits) {
for l in lanes_of_modified_turns {
DrawMap::compute_turn_to_lane_offset(
&mut turn_to_lane_offset,
ui.primary.map.get_l(l),
&ui.primary.map,
bundle.map.get_l(l),
&bundle.map,
);
}
for t in turns_added {
let turn = ui.primary.map.get_t(t);
let turn = bundle.map.get_t(t);
if turn.turn_type != TurnType::SharedSidewalkCorner {
ui.primary.draw_map.turns.insert(
t,
DrawTurn::new(&ui.primary.map, turn, turn_to_lane_offset[&t]),
);
bundle
.draw_map
.turns
.insert(t, DrawTurn::new(&bundle.map, turn, turn_to_lane_offset[&t]));
}
}

for i in modified_intersections {
ui.primary.draw_map.intersections[i.0] = DrawIntersection::new(
ui.primary.map.get_i(i),
&ui.primary.map,
&ui.cs,
bundle.draw_map.intersections[i.0] = DrawIntersection::new(
bundle.map.get_i(i),
&bundle.map,
cs,
ctx.prerender,
&mut timer,
);
}

// Do this after fixing up all the state above.
ui.primary.map.simplify_edits(&mut timer);
bundle.map.simplify_edits(&mut timer);
}

fn load_edits(map: &Map, wizard: &mut WrappedWizard, query: &str) -> Option<MapEdits> {
Expand Down
6 changes: 3 additions & 3 deletions editor/src/edit/stop_signs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,22 @@ impl StopSignEditor {
sign.change(t, next_priority, &ui.primary.map);
let mut new_edits = ui.primary.map.get_edits().clone();
new_edits.stop_sign_overrides.insert(self.id, sign);
apply_map_edits(ui, ctx, new_edits);
apply_map_edits(&mut ui.primary, &ui.cs, ctx, new_edits);
}
} else if let Some(r) = self.selected_sign {
if ctx.input.contextual_action(Key::Space, "toggle stop sign") {
let mut sign = ui.primary.map.get_stop_sign(self.id).clone();
sign.flip_sign(r, &ui.primary.map);
let mut new_edits = ui.primary.map.get_edits().clone();
new_edits.stop_sign_overrides.insert(self.id, sign);
apply_map_edits(ui, ctx, new_edits);
apply_map_edits(&mut ui.primary, &ui.cs, ctx, new_edits);
}
} else if self.menu.action("quit") {
return true;
} else if self.menu.action("reset to default") {
let mut new_edits = ui.primary.map.get_edits().clone();
new_edits.stop_sign_overrides.remove(&self.id);
apply_map_edits(ui, ctx, new_edits);
apply_map_edits(&mut ui.primary, &ui.cs, ctx, new_edits);
}
false
}
Expand Down
2 changes: 1 addition & 1 deletion editor/src/edit/traffic_signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl TrafficSignalEditor {
} else {
new_edits.traffic_signal_overrides.insert(self.i, signal);
}
apply_map_edits(ui, ctx, new_edits);
apply_map_edits(&mut ui.primary, &ui.cs, ctx, new_edits);
}

false
Expand Down
7 changes: 7 additions & 0 deletions map_model/src/edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ impl MapEdits {
}
}

pub fn load(map_name: &str, edits_name: &str) -> MapEdits {
if edits_name == "no_edits" {
return MapEdits::new(map_name.to_string());
}
abstutil::read_json(&format!("../data/edits/{}/{}.json", map_name, edits_name)).unwrap()
}

pub fn save(&self) {
abstutil::save_object("edits", &self.map_name, &self.edits_name, self);
}
Expand Down
31 changes: 4 additions & 27 deletions sim/src/make/load.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{Scenario, Sim};
use abstutil;
use abstutil::Timer;
use geom::Duration;
use map_model::{Map, MapEdits};
use rand::{FromEntropy, SeedableRng};
Expand All @@ -26,10 +25,6 @@ pub struct SimFlags {
/// Run name for savestating
#[structopt(long = "run_name")]
pub run_name: Option<String>,

/// Name of map edits. Shouldn't be a full path or have the ".json"
#[structopt(long = "edits_name", default_value = "no_edits")]
pub edits_name: String,
}

impl SimFlags {
Expand All @@ -43,7 +38,6 @@ impl SimFlags {
load: PathBuf::from(format!("../data/maps/{}.abst", map)),
rng_seed: Some(42),
run_name: Some(run_name.to_string()),
edits_name: "no_edits".to_string(),
}
}

Expand All @@ -64,7 +58,6 @@ impl SimFlags {
let mut rng = self.make_rng();

if self.load.starts_with(Path::new("../data/save/")) {
assert_eq!(self.edits_name, "no_edits");
timer.note(format!("Resuming from {}", self.load.display()));

timer.start("read sim savestate");
Expand All @@ -75,7 +68,7 @@ impl SimFlags {
let mut map: Map =
abstutil::read_binary(&format!("../data/maps/{}.abst", sim.map_name), timer)
.unwrap();
apply_edits(&mut map, &sim.edits_name, timer);
map.apply_edits(MapEdits::load(map.get_name(), &sim.edits_name), timer);

(map, sim, rng)
} else if self.load.starts_with(Path::new("../data/scenarios/")) {
Expand All @@ -87,10 +80,9 @@ impl SimFlags {
let scenario: Scenario = abstutil::read_binary(self.load.to_str().unwrap(), timer)
.expect("loading scenario failed");

let mut map: Map =
let map: Map =
abstutil::read_binary(&format!("../data/maps/{}.abst", scenario.map_name), timer)
.unwrap();
apply_edits(&mut map, &self.edits_name, timer);

let mut sim = Sim::new(
&map,
Expand All @@ -105,9 +97,8 @@ impl SimFlags {
} else if self.load.starts_with(Path::new("../data/raw_maps/")) {
timer.note(format!("Loading map {}", self.load.display()));

let mut map = Map::new(self.load.to_str().unwrap(), timer)
let map = Map::new(self.load.to_str().unwrap(), timer)
.expect(&format!("Couldn't load map from {}", self.load.display()));
apply_edits(&mut map, &self.edits_name, timer);

timer.start("create sim");
let sim = Sim::new(
Expand All @@ -123,9 +114,8 @@ impl SimFlags {
} else if self.load.starts_with(Path::new("../data/maps/")) {
timer.note(format!("Loading map {}", self.load.display()));

let mut map: Map = abstutil::read_binary(self.load.to_str().unwrap(), timer)
let map: Map = abstutil::read_binary(self.load.to_str().unwrap(), timer)
.expect(&format!("Couldn't load map from {}", self.load.display()));
apply_edits(&mut map, &self.edits_name, timer);

timer.start("create sim");
let sim = Sim::new(
Expand All @@ -143,16 +133,3 @@ impl SimFlags {
}
}
}

fn apply_edits(map: &mut Map, edits_name: &str, timer: &mut Timer) {
if edits_name == "no_edits" {
return;
}
let edits: MapEdits = abstutil::read_json(&format!(
"../data/edits/{}/{}.json",
map.get_name(),
edits_name
))
.unwrap();
map.apply_edits(edits, timer);
}

0 comments on commit 8eb03b8

Please sign in to comment.