From 72d0294efb58cbff678f2da46ef38d1373a3cd93 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 2 Oct 2018 16:00:53 -0700 Subject: [PATCH] load named map edits, not a single one --- editor/src/main.rs | 12 +++++++++++- editor/src/ui.rs | 7 +++++-- headless/src/main.rs | 5 +++++ sim/src/edits.rs | 2 +- sim/src/helpers.rs | 20 +++++++++++++++++--- sim/tests/completion.rs | 1 + sim/tests/determinism.rs | 3 +++ sim/tests/transit.rs | 2 ++ 8 files changed, 45 insertions(+), 7 deletions(-) diff --git a/editor/src/main.rs b/editor/src/main.rs index 102662d6e1..996f3c6e42 100644 --- a/editor/src/main.rs +++ b/editor/src/main.rs @@ -61,12 +61,22 @@ struct Flags { /// Scenario name for savestating #[structopt(long = "scenario_name", default_value = "editor")] scenario_name: String, + + /// Name of map edits + #[structopt(long = "edits_name", default_value = "no_edits")] + edits_name: String, } fn main() { let flags = Flags::from_args(); ezgui::run( - ui::UIWrapper::new(flags.load, flags.scenario_name, flags.rng_seed, flags.kml), + ui::UIWrapper::new( + flags.load, + flags.scenario_name, + flags.edits_name, + flags.rng_seed, + flags.kml, + ), "A/B Street", 1024, 768, diff --git a/editor/src/ui.rs b/editor/src/ui.rs index e4d36ac242..b7c8458d9b 100644 --- a/editor/src/ui.rs +++ b/editor/src/ui.rs @@ -68,6 +68,7 @@ impl UIWrapper { pub fn new( load: String, scenario_name: String, + edits_name: String, rng_seed: Option, kml: Option, ) -> UIWrapper { @@ -78,6 +79,7 @@ impl UIWrapper { let (map, control_map, sim) = sim::load( load, scenario_name, + edits_name, rng_seed, Some(sim::Tick::from_seconds(30)), ); @@ -384,10 +386,11 @@ impl UI { // TODO maybe make state line up with the map, so loading from a new map doesn't break abstutil::write_json("editor_state", &state).expect("Saving editor_state failed"); abstutil::write_json("color_scheme", &self.cs).expect("Saving color_scheme failed"); + // TODO do this from a plugin! abstutil::write_json( - "map_edits.json", + &format!("../data/edits/{}/ui.json", self.map.get_name()), &MapEdits { - edits_name: "nameless".to_string(), + edits_name: "ui".to_string(), map_name: self.map.get_name().to_string(), road_edits: self.road_editor.get_edits().clone(), stop_signs: self.control_map.get_stop_signs_savestate(), diff --git a/headless/src/main.rs b/headless/src/main.rs index 5da3c2b24d..77a604066d 100644 --- a/headless/src/main.rs +++ b/headless/src/main.rs @@ -36,6 +36,10 @@ struct Flags { /// Scenario name for savestating #[structopt(long = "scenario_name", default_value = "headless")] scenario_name: String, + + /// Name of map edits + #[structopt(long = "edits_name", default_value = "no_edits")] + edits_name: String, } fn main() { @@ -47,6 +51,7 @@ fn main() { let (map, control_map, mut sim) = sim::load( flags.load.clone(), flags.scenario_name, + flags.edits_name, flags.rng_seed, Some(sim::Tick::from_seconds(30)), ); diff --git a/sim/src/edits.rs b/sim/src/edits.rs index dccb822bc2..b36719c315 100644 --- a/sim/src/edits.rs +++ b/sim/src/edits.rs @@ -15,7 +15,7 @@ pub struct MapEdits { impl MapEdits { pub fn new() -> MapEdits { MapEdits { - edits_name: "unnamed".to_string(), + edits_name: "no_edits".to_string(), map_name: "TODO".to_string(), // TODO er road_edits: RoadEdits::new(), stop_signs: BTreeMap::new(), diff --git a/sim/src/helpers.rs b/sim/src/helpers.rs index 2f1ed12964..c0a7718d90 100644 --- a/sim/src/helpers.rs +++ b/sim/src/helpers.rs @@ -11,18 +11,20 @@ use {CarID, Event, MapEdits, PedestrianID, RouteID, Scenario, Sim, Tick}; pub fn load( input: String, scenario_name: String, + edits_name: String, rng_seed: Option, savestate_every: Option, ) -> (Map, ControlMap, Sim) { - // TODO read a specific one - let edits: MapEdits = abstutil::read_json("map_edits.json").unwrap_or(MapEdits::new()); - if input.contains("data/save/") { info!("Resuming from {}", input); flame::start("read sim savestate"); let sim: Sim = abstutil::read_json(&input).expect("loading sim state failed"); flame::end("read sim savestate"); // TODO assuming the relative path :( + let edits: MapEdits = abstutil::read_json(&format!( + "../data/edits/{}/{}.json", + sim.map_name, edits_name + )).unwrap_or(MapEdits::new()); let map_path = format!("../data/maps/{}.abst", sim.map_name); let map = Map::new(&map_path, edits.road_edits.clone()) .expect(&format!("Couldn't load map from {}", map_path)); @@ -31,6 +33,10 @@ pub fn load( } else if input.contains("data/scenarios/") { info!("Seeding the simulation from scenario {}", input); let scenario: Scenario = abstutil::read_json(&input).expect("loading scenario failed"); + let edits: MapEdits = abstutil::read_json(&format!( + "../data/edits/{}/{}.json", + scenario.map_name, edits_name + )).unwrap_or(MapEdits::new()); let map_path = format!("../data/maps/{}.abst", scenario.map_name); let map = Map::new(&map_path, edits.road_edits.clone()) .expect(&format!("Couldn't load map from {}", map_path)); @@ -44,7 +50,15 @@ pub fn load( scenario.instantiate(&mut sim, &map); (map, control_map, sim) } else { + // TODO relative dir is brittle; match more cautiously + let map_name = input + .trim_left_matches("../data/maps/") + .trim_right_matches(".abst") + .to_string(); info!("Loading map {}", input); + let edits: MapEdits = + abstutil::read_json(&format!("../data/edits/{}/{}.json", map_name, edits_name)) + .unwrap_or(MapEdits::new()); let map = Map::new(&input, edits.road_edits.clone()).expect("Couldn't load map"); let control_map = ControlMap::new(&map, &edits.stop_signs, &edits.traffic_signals); flame::start("create sim"); diff --git a/sim/tests/completion.rs b/sim/tests/completion.rs index 70aec26851..e245793378 100644 --- a/sim/tests/completion.rs +++ b/sim/tests/completion.rs @@ -8,6 +8,7 @@ fn aorta_model_completes() { let (map, control_map, mut sim) = sim::load( "../data/maps/small.abst".to_string(), "aorta_model_completes".to_string(), + "no_edits".to_string(), Some(42), Some(sim::Tick::from_seconds(30)), ); diff --git a/sim/tests/determinism.rs b/sim/tests/determinism.rs index 8c7e29aa1b..fc0a1eb768 100644 --- a/sim/tests/determinism.rs +++ b/sim/tests/determinism.rs @@ -8,6 +8,7 @@ fn serialization() { let (map, _, mut sim) = sim::load( "../data/maps/small.abst".to_string(), "serialization".to_string(), + "no_edits".to_string(), Some(42), None, ); @@ -25,6 +26,7 @@ fn from_scratch() { let (map, control_map, mut sim1) = sim::load( "../data/maps/small.abst".to_string(), "from_scratch_1".to_string(), + "no_edits".to_string(), Some(42), None, ); @@ -52,6 +54,7 @@ fn with_savestating() { let (map, control_map, mut sim1) = sim::load( "../data/maps/small.abst".to_string(), "with_savestating_1".to_string(), + "no_edits".to_string(), Some(42), None, ); diff --git a/sim/tests/transit.rs b/sim/tests/transit.rs index 4306e6b228..5a6212319f 100644 --- a/sim/tests/transit.rs +++ b/sim/tests/transit.rs @@ -8,6 +8,7 @@ fn bus_reaches_stops() { let (map, control_map, mut sim) = sim::load( "../data/maps/small.abst".to_string(), "bus_reaches_stops".to_string(), + "no_edits".to_string(), Some(42), Some(sim::Tick::from_seconds(30)), ); @@ -36,6 +37,7 @@ fn ped_uses_bus() { let (map, control_map, mut sim) = sim::load( "../data/maps/small.abst".to_string(), "bus_reaches_stops".to_string(), + "no_edits".to_string(), Some(42), Some(sim::Tick::from_seconds(30)), );