WIP instantiating a scenario

This commit is contained in:
Dustin Carlino 2018-09-27 11:35:38 -07:00
parent 3d2255d728
commit fa1d7e8182
4 changed files with 42 additions and 6 deletions

View File

@ -48,3 +48,9 @@ CarParking
- rename to ParkedCar - rename to ParkedCar
SidewalkSpot SidewalkSpot
- this should cache lane and distance. :) - this should cache lane and distance. :)
## Scenarios
Awkward to turn neighborhoods into buildings/streets; we kind of need the
quadtree and stuff for that, which is the Renderable layer right now.
Originally there was a separate geometry layer, probably for stuff like this.

View File

@ -5,7 +5,7 @@ use map_model::Map;
use objects::SIM_SETUP; use objects::SIM_SETUP;
use piston::input::Key; use piston::input::Key;
use plugins::Colorizer; use plugins::Colorizer;
use sim::{Neighborhood, Scenario, SeedParkedCars, SpawnOverTime, Tick}; use sim::{Neighborhood, Scenario, SeedParkedCars, SpawnOverTime, Tick, Sim};
pub enum ScenarioManager { pub enum ScenarioManager {
Inactive, Inactive,
@ -19,7 +19,7 @@ impl ScenarioManager {
ScenarioManager::Inactive ScenarioManager::Inactive
} }
pub fn event(&mut self, input: &mut UserInput, map: &Map) -> bool { pub fn event(&mut self, input: &mut UserInput, map: &Map, sim: &mut Sim) -> bool {
let mut new_state: Option<ScenarioManager> = None; let mut new_state: Option<ScenarioManager> = None;
match self { match self {
ScenarioManager::Inactive => { ScenarioManager::Inactive => {
@ -51,10 +51,11 @@ impl ScenarioManager {
scenario.clone(), scenario.clone(),
Wizard::new(), Wizard::new(),
)); ));
} else if input.key_pressed(Key::I, "instantiate this scenario") {
scenario.instantiate(sim);
} else if scroller.event(input) { } else if scroller.event(input) {
new_state = Some(ScenarioManager::Inactive); new_state = Some(ScenarioManager::Inactive);
} }
// TODO instantiate it
} }
ScenarioManager::EditScenario(ref mut scenario, ref mut wizard) => { ScenarioManager::EditScenario(ref mut scenario, ref mut wizard) => {
if let Some(()) = edit_scenario(map, scenario, wizard.wrap(input)) { if let Some(()) = edit_scenario(map, scenario, wizard.wrap(input)) {

View File

@ -249,7 +249,7 @@ impl UIWrapper {
Box::new(|ui, input, osd| { Box::new(|ui, input, osd| {
ui.draw_neighborhoods.event(input, &ui.canvas, &ui.map, osd) ui.draw_neighborhoods.event(input, &ui.canvas, &ui.map, osd)
}), }),
Box::new(|ui, input, _osd| ui.scenarios.event(input, &ui.map)), Box::new(|ui, input, _osd| ui.scenarios.event(input, &ui.map, &mut ui.sim)),
Box::new(|ui, input, _osd| ui.logs.event(input)), Box::new(|ui, input, _osd| ui.logs.event(input)),
], ],
} }

View File

@ -1,6 +1,8 @@
use abstutil; use abstutil;
use geom::Pt2D; use map_model::{Map, BuildingID};
use Tick; use geom::{Polygon, Pt2D};
use std::collections::HashMap;
use {Sim, Tick};
#[derive(Clone, Serialize, Deserialize, Debug)] #[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Scenario { pub struct Scenario {
@ -32,9 +34,25 @@ pub struct SeedParkedCars {
#[derive(Clone, Serialize, Deserialize, Debug)] #[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Neighborhood { pub struct Neighborhood {
pub name: String, pub name: String,
// TODO Polygon would be more natural
pub points: Vec<Pt2D>, pub points: Vec<Pt2D>,
} }
impl Neighborhood {
// TODO This should use quadtrees and/or not just match the center of each building.
fn find_matching_buildings(&self, map: &Map) -> Vec<BuildingID> {
let poly = Polygon::new(&self.points);
let mut results: Vec<BuildingID> = Vec::new();
for b in map.all_buildings() {
if poly.contains_pt(Pt2D::center(&b.points)) {
results.push(b.id);
}
}
results
}
}
impl Scenario { impl Scenario {
pub fn describe(&self) -> Vec<String> { pub fn describe(&self) -> Vec<String> {
abstutil::to_json(self) abstutil::to_json(self)
@ -42,4 +60,15 @@ impl Scenario {
.map(|s| s.to_string()) .map(|s| s.to_string())
.collect() .collect()
} }
pub fn instantiate(&self, sim: &mut Sim) {
info!("Instantiating {}", self.scenario_name);
let neighborhoods: HashMap<String, Neighborhood> = abstutil::load_all_objects("neighborhoods", &self.map_name).into_iter().collect();
for s in &self.seed_parked_cars {
//sim.seed_parked_cars_in_polygon(&Polygon::new(&neighborhoods[&s.neighborhood].points), s.percent_to_fill);
}
// TODO spawn o'er time
}
} }