From 1d000cbc6b7ffd6c131b137964e04af28fcf0801 Mon Sep 17 00:00:00 2001 From: Paris DOUADY Date: Mon, 18 Dec 2023 19:07:20 +0100 Subject: [PATCH] rename Terrain -> Environment in simulation as this struct encapsulates the Terrain + Trees + ... (cliffs? water? wildlife?..) --- native_app/src/audio/ambient.rs | 2 +- native_app/src/game_loop.rs | 8 +++--- native_app/src/gui/roadbuild.rs | 4 +-- native_app/src/gui/windows/debug.rs | 3 +- .../src/rendering/map_rendering/map_mesh.rs | 20 ++++++------- native_app/src/rendering/map_rendering/mod.rs | 2 +- .../src/rendering/map_rendering/terrain.rs | 4 +-- .../src/rendering/map_rendering/trees.rs | 8 +++--- simulation/src/map/map.rs | 28 +++++++++---------- simulation/src/map/objects/building.rs | 6 ++-- simulation/src/map/objects/lot.rs | 2 +- simulation/src/map/objects/road.rs | 8 +++--- simulation/src/map/serializing.rs | 10 +++---- simulation/src/map/terrain.rs | 22 +++++++-------- simulation/src/world_command.rs | 6 ++-- 15 files changed, 67 insertions(+), 66 deletions(-) diff --git a/native_app/src/audio/ambient.rs b/native_app/src/audio/ambient.rs index 6eca8913..ee87315c 100644 --- a/native_app/src/audio/ambient.rs +++ b/native_app/src/audio/ambient.rs @@ -56,7 +56,7 @@ impl Ambient { if volume > 0.0 { let mut matches = 0; - for _ in map.terrain.trees.query(bbox.ll, bbox.ur) { + for _ in map.environment.trees.query(bbox.ll, bbox.ur) { matches += 1; if matches > 50 { break; diff --git a/native_app/src/game_loop.rs b/native_app/src/game_loop.rs index 21291eaf..440c052b 100644 --- a/native_app/src/game_loop.rs +++ b/native_app/src/game_loop.rs @@ -122,7 +122,7 @@ impl engine::framework::State for State { self.uiw.write::().ray = ray; if let Some(ray) = ray { - let cast = map.terrain.raycast(ray); + let cast = map.environment.raycast(ray); self.uiw.write::().unprojected = cast.map(|x| x.0); self.uiw.write::().unprojected_normal = cast.map(|x| x.1); @@ -291,8 +291,8 @@ impl State { ctx.delta, &self.uiw.read::(), &self.uiw.read::(), - map.terrain.bounds(), - |p| map.terrain.height(p), + map.environment.bounds(), + |p| map.environment.height(p), ); *self.uiw.write::() = self.uiw.read::().camera; @@ -322,7 +322,7 @@ impl State { unsafe { for v in &geom::DEBUG_OBBS { immediate - .obb(*v, map.terrain.height(v.center()).unwrap_or(0.0) + 8.0) + .obb(*v, map.environment.height(v.center()).unwrap_or(0.0) + 8.0) .color(col); } for v in &geom::DEBUG_SPLINES { diff --git a/native_app/src/gui/roadbuild.rs b/native_app/src/gui/roadbuild.rs index 6d611896..f4f1aec6 100644 --- a/native_app/src/gui/roadbuild.rs +++ b/native_app/src/gui/roadbuild.rs @@ -56,7 +56,7 @@ pub fn roadbuild(sim: &Simulation, uiworld: &mut UiWorld) { let grid_size = 20.0; let mousepos = if state.snap_to_grid { let v = unproj.xy().snap(grid_size, grid_size); - v.z(unwrap_ret!(map.terrain.height(v)) + 0.3 + state.height_offset) + v.z(unwrap_ret!(map.environment.height(v)) + 0.3 + state.height_offset) } else { unproj.up(0.3 + state.height_offset) }; @@ -438,7 +438,7 @@ impl RoadBuildResource { terrain_height, pos, .. - } in simulation::map::Road::pylons_positions(&p, &map.terrain) + } in simulation::map::Road::pylons_positions(&p, &map.environment) { immdraw .circle(pos.xy().z(terrain_height + 0.1), patwidth * 0.5) diff --git a/native_app/src/gui/windows/debug.rs b/native_app/src/gui/windows/debug.rs index 2f892706..244715ca 100644 --- a/native_app/src/gui/windows/debug.rs +++ b/native_app/src/gui/windows/debug.rs @@ -621,7 +621,8 @@ pub fn debug_spatialmap(tess: &mut Tesselator, sim: &Simulation, _: &UiWor for r in map.spatial_map().debug_grid() { tess.set_color(LinearColor::BLUE.a(0.1)); tess.draw_rect_cos_sin( - r.center().z(map.terrain.height(r.center()).unwrap_or(0.0)), + r.center() + .z(map.environment.height(r.center()).unwrap_or(0.0)), r.w(), r.h(), Vec2::X, diff --git a/native_app/src/rendering/map_rendering/map_mesh.rs b/native_app/src/rendering/map_rendering/map_mesh.rs index 68943cef..16d486dd 100644 --- a/native_app/src/rendering/map_rendering/map_mesh.rs +++ b/native_app/src/rendering/map_rendering/map_mesh.rs @@ -8,9 +8,9 @@ use engine::{ }; use geom::{minmax, vec2, vec3, Color, LinearColor, PolyLine3, Polygon, Radians, Vec2, Vec3}; use simulation::map::{ - Building, BuildingKind, CanonicalPosition, Intersection, LaneKind, Lanes, LotKind, Map, - MapSubscriber, ProjectFilter, ProjectKind, PylonPosition, Road, Roads, SubscriberChunkID, - Terrain, Turn, TurnKind, UpdateType, CROSSWALK_WIDTH, + Building, BuildingKind, CanonicalPosition, Environment, Intersection, LaneKind, Lanes, LotKind, + Map, MapSubscriber, ProjectFilter, ProjectKind, PylonPosition, Road, Roads, SubscriberChunkID, + Turn, TurnKind, UpdateType, CROSSWALK_WIDTH, }; use simulation::souls::goods_company::GoodsCompanyRegistry; use simulation::Simulation; @@ -586,7 +586,7 @@ impl MapBuilders { let lanes = map.lanes(); let roads = map.roads(); let lots = map.lots(); - let terrain = &map.terrain; + let env = &map.environment; for road in chunk_roads { let road = &roads[road]; @@ -597,7 +597,7 @@ impl MapBuilders { let first_dir = unwrap_cont!(cut.first_dir()); let last_dir = unwrap_cont!(cut.last_dir()); - road_pylons(&mut self.tess_map.meshbuilder, terrain, road); + road_pylons(&mut self.tess_map.meshbuilder, env, road); self.tess_map.normal.z = -1.0; self.tess_map.draw_polyline_full( @@ -674,7 +674,7 @@ impl MapBuilders { self.crosswalks(inter, lanes); - inter_pylon(&mut self.tess_map.meshbuilder, terrain, inter, roads); + inter_pylon(&mut self.tess_map.meshbuilder, env, inter, roads); intersection_mesh(&mut self.tess_map, &hig_col, inter, roads); // Walking corners @@ -819,19 +819,19 @@ fn add_polyon( quad(3, 0, 7, 4, d2p); } -fn road_pylons(meshb: &mut MeshBuilder, terrain: &Terrain, road: &Road) { - for pylon in Road::pylons_positions(road.interfaced_points(), terrain) { +fn road_pylons(meshb: &mut MeshBuilder, env: &Environment, road: &Road) { + for pylon in Road::pylons_positions(road.interfaced_points(), env) { add_polyon(meshb, road.width * 0.5, pylon); } } fn inter_pylon( meshb: &mut MeshBuilder, - terrain: &Terrain, + env: &Environment, inter: &Intersection, roads: &Roads, ) { - let h = unwrap_ret!(terrain.height(inter.pos.xy())); + let h = unwrap_ret!(env.height(inter.pos.xy())); if (h - inter.pos.z).abs() <= 2.0 { return; } diff --git a/native_app/src/rendering/map_rendering/mod.rs b/native_app/src/rendering/map_rendering/mod.rs index 5c2a1ad7..c8f190f6 100644 --- a/native_app/src/rendering/map_rendering/mod.rs +++ b/native_app/src/rendering/map_rendering/mod.rs @@ -35,7 +35,7 @@ impl MapRenderer { meshb: MapMeshHandler::new(gfx, sim), trees: TreesRender::new(gfx, &sim.map()), terrain: TerrainRender::new(gfx, sim), - water: Water::new(gfx, sim.map().terrain.bounds()), + water: Water::new(gfx, sim.map().environment.bounds()), lamps: LampsRender::new(&sim.map()), } } diff --git a/native_app/src/rendering/map_rendering/terrain.rs b/native_app/src/rendering/map_rendering/terrain.rs index b304aaad..98ed8dd9 100644 --- a/native_app/src/rendering/map_rendering/terrain.rs +++ b/native_app/src/rendering/map_rendering/terrain.rs @@ -14,7 +14,7 @@ pub struct TerrainRender { impl TerrainRender { pub fn new(gfx: &mut GfxContext, sim: &Simulation) -> Self { - let (w, h) = sim.map().terrain.size(); + let (w, h) = sim.map().environment.size(); let terrain = EngineTerrainRender::new(gfx, w as u32, h as u32); @@ -29,7 +29,7 @@ impl TerrainRender { } pub fn update(&mut self, ctx: &mut Context, map: &Map) { - let ter = &map.terrain; + let ter = &map.environment; if self.terrain_sub.take_cleared() { for (chunk_id, chunk) in ter.chunks() { diff --git a/native_app/src/rendering/map_rendering/trees.rs b/native_app/src/rendering/map_rendering/trees.rs index 5c38a5e7..5b756c7d 100644 --- a/native_app/src/rendering/map_rendering/trees.rs +++ b/native_app/src/rendering/map_rendering/trees.rs @@ -35,14 +35,14 @@ impl TreesRender { self.tree_builder.instances.clear(); let aabb = chunkid.bbox(); - map.terrain + map.environment .trees .query_aabb_visitor(aabb.ll, aabb.ur, |obj| { - let Some((_, t)) = map.terrain.trees.get(obj.0) else { + let Some((_, t)) = map.environment.trees.get(obj.0) else { return; }; self.tree_builder.instances.push(MeshInstance { - pos: t.pos.z(map.terrain.height(t.pos).unwrap_or_default()), + pos: t.pos.z(map.environment.height(t.pos).unwrap_or_default()), dir: t.dir.z0() * t.size * 0.2, tint: ((1.0 - t.size * 0.05) * t.col * LinearColor::WHITE).a(1.0), }); @@ -109,7 +109,7 @@ impl TreesRender { let chunkcenter = cid.center().z0(); let max_height = cid .convert() - .filter_map(|c| map.terrain.get_chunk(c)) + .filter_map(|c| map.environment.get_chunk(c)) .map(HeightmapChunk::max_height) .fold(0.0, f32::max); diff --git a/simulation/src/map/map.rs b/simulation/src/map/map.rs index 1bd4c853..6734b6cf 100644 --- a/simulation/src/map/map.rs +++ b/simulation/src/map/map.rs @@ -1,9 +1,9 @@ use crate::map::serializing::SerializedMap; use crate::map::{ - Building, BuildingID, BuildingKind, Intersection, IntersectionID, Lane, LaneID, LaneKind, - LanePattern, Lot, LotID, LotKind, MapSubscriber, MapSubscribers, ParkingSpotID, ParkingSpots, - ProjectFilter, ProjectKind, Road, RoadID, RoadSegmentKind, SpatialMap, SubscriberChunkID, - TerraformKind, Terrain, UpdateType, Zone, + Building, BuildingID, BuildingKind, Environment, Intersection, IntersectionID, Lane, LaneID, + LaneKind, LanePattern, Lot, LotID, LotKind, MapSubscriber, MapSubscribers, ParkingSpotID, + ParkingSpots, ProjectFilter, ProjectKind, Road, RoadID, RoadSegmentKind, SpatialMap, + SubscriberChunkID, TerraformKind, UpdateType, Zone, }; use crate::utils::time::Tick; use common::descriptions::BuildingGen; @@ -34,7 +34,7 @@ pub struct Map { pub(crate) lots: Lots, pub(crate) spatial_map: SpatialMap, pub(crate) bkinds: BTreeMap>, - pub terrain: Terrain, + pub environment: Environment, pub parking: ParkingSpots, pub subscribers: MapSubscribers, } @@ -57,7 +57,7 @@ impl Map { parking: ParkingSpots::default(), buildings: Buildings::default(), lots: Lots::default(), - terrain: Terrain::default(), + environment: Environment::default(), spatial_map: SpatialMap::default(), bkinds: Default::default(), subscribers: Default::default(), @@ -99,7 +99,7 @@ impl Map { pub fn dispatch_all(&self) { self.subscribers.dispatch_clear(); - let bounds = self.terrain.bounds(); + let bounds = self.environment.bounds(); let ll = SubscriberChunkID::new(bounds.ll); let ur = SubscriberChunkID::new(bounds.ur); for x in ll.0..ur.0 { @@ -177,7 +177,7 @@ impl Map { }; f(z); - self.terrain.remove_trees_near(&z.poly, |tree_chunk| { + self.environment.remove_trees_near(&z.poly, |tree_chunk| { self.subscribers .dispatch_chunk(UpdateType::Terrain, tree_chunk) }); @@ -214,7 +214,7 @@ impl Map { self.clean_lots_inner(self.spatial_map.query(obb, ProjectFilter::LOT).collect()); - self.terrain + self.environment .remove_trees_near(obb.expand(10.0), |tree_chunk| { self.subscribers .dispatch_chunk(UpdateType::Terrain, tree_chunk) @@ -223,7 +223,7 @@ impl Map { let v = Building::make( &mut self.buildings, &mut self.spatial_map, - &self.terrain, + &self.environment, *obb, kind, gen, @@ -255,7 +255,7 @@ impl Map { let v = Building::make( &mut self.buildings, &mut self.spatial_map, - &self.terrain, + &self.environment, lot.shape, BuildingKind::House, BuildingGen::House, @@ -338,7 +338,7 @@ impl Map { slope: Option<(Vec3, Vec3)>, ) { let modified = self - .terrain + .environment .terraform(tick, kind, center, radius, amount, level, slope); for id in modified { @@ -349,7 +349,7 @@ impl Map { pub fn clear(&mut self) { info!("clear"); let before = std::mem::replace(self, Self::empty()); - self.terrain = before.terrain; + self.environment = before.environment; self.subscribers.dispatch_clear(); self.check_invariants(); @@ -564,7 +564,7 @@ impl Map { let r = &self.roads[id]; let mut b = r.boldline(); b.expand(40.0); - self.terrain.remove_trees_near(&b, |tree_chunk| { + self.environment.remove_trees_near(&b, |tree_chunk| { self.subscribers .dispatch_chunk(UpdateType::Terrain, tree_chunk) }); diff --git a/simulation/src/map/objects/building.rs b/simulation/src/map/objects/building.rs index cf4923e9..552dce35 100644 --- a/simulation/src/map/objects/building.rs +++ b/simulation/src/map/objects/building.rs @@ -1,5 +1,5 @@ use crate::map::procgen::{gen_exterior_farm, gen_exterior_house, ColoredMesh}; -use crate::map::{Buildings, LanePattern, SpatialMap, Terrain}; +use crate::map::{Buildings, Environment, LanePattern, SpatialMap}; use crate::souls::goods_company::GoodsCompanyID; use common::descriptions::BuildingGen; use egui_inspect::debug_inspect_impl; @@ -84,13 +84,13 @@ impl Building { pub fn make( buildings: &mut Buildings, spatial_map: &mut SpatialMap, - terrain: &Terrain, + env: &Environment, obb: OBB, kind: BuildingKind, gen: BuildingGen, zone: Option, ) -> Option { - let at = obb.center().z(terrain.height(obb.center())?); + let at = obb.center().z(env.height(obb.center())?); let axis = (obb.corners[1] - obb.corners[0]).normalize(); let size = obb.corners[0].distance(obb.corners[1]); diff --git a/simulation/src/map/objects/lot.rs b/simulation/src/map/objects/lot.rs index aa10bac2..7af38cc9 100644 --- a/simulation/src/map/objects/lot.rs +++ b/simulation/src/map/objects/lot.rs @@ -33,7 +33,7 @@ impl Lot { axis: Vec2, size: f32, ) -> Option { - let height = map.terrain.height(at.xy())?; + let height = map.environment.height(at.xy())?; if (height - at.z).abs() > 1.0 { return None; } diff --git a/simulation/src/map/objects/road.rs b/simulation/src/map/objects/road.rs index 7eb9b36c..a13d5cb8 100644 --- a/simulation/src/map/objects/road.rs +++ b/simulation/src/map/objects/road.rs @@ -1,6 +1,6 @@ use crate::map::{ - Intersection, IntersectionID, Lane, LaneDirection, LaneID, LaneKind, LanePattern, Lanes, - ParkingSpots, Roads, SpatialMap, Terrain, + Environment, Intersection, IntersectionID, Lane, LaneDirection, LaneID, LaneKind, LanePattern, + Lanes, ParkingSpots, Roads, SpatialMap, }; use geom::Spline3; use geom::{BoldLine, PolyLine3}; @@ -238,12 +238,12 @@ impl Road { pub fn pylons_positions<'a>( interfaced_points: &'a PolyLine3, - terrain: &'a Terrain, + env: &'a Environment, ) -> impl Iterator + 'a { interfaced_points .equipoints_dir(80.0, true) .filter_map(move |(pos, dir)| { - let h = terrain.height(pos.xy())?; + let h = env.height(pos.xy())?; if (h - pos.z).abs() <= 2.0 { return None; } diff --git a/simulation/src/map/serializing.rs b/simulation/src/map/serializing.rs index 7a0fdf67..9ae96cec 100644 --- a/simulation/src/map/serializing.rs +++ b/simulation/src/map/serializing.rs @@ -1,6 +1,6 @@ use crate::map::{ - BuildingID, Buildings, Intersections, Lanes, Lots, Map, ParkingSpots, Roads, SpatialMap, - Terrain, + BuildingID, Buildings, Environment, Intersections, Lanes, Lots, Map, ParkingSpots, Roads, + SpatialMap, }; use crate::BuildingKind; use serde::{Deserialize, Serialize}; @@ -14,7 +14,7 @@ pub(crate) struct SerializedMap { pub lanes: Lanes, pub parking: ParkingSpots, pub lots: Lots, - pub terrain: Terrain, + pub environment: Environment, pub bkinds: BTreeMap>, } @@ -27,7 +27,7 @@ impl From<&Map> for SerializedMap { lanes: m.lanes.clone(), parking: m.parking.clone(), lots: m.lots.clone(), - terrain: m.terrain.clone(), + environment: m.environment.clone(), bkinds: m.bkinds.clone(), } } @@ -44,7 +44,7 @@ impl From for Map { spatial_map, lots: sel.lots, parking: sel.parking, - terrain: sel.terrain, + environment: sel.environment, bkinds: sel.bkinds, subscribers: Default::default(), } diff --git a/simulation/src/map/terrain.rs b/simulation/src/map/terrain.rs index e0d9e4ce..c0f0d754 100644 --- a/simulation/src/map/terrain.rs +++ b/simulation/src/map/terrain.rs @@ -29,7 +29,7 @@ pub struct Tree { } #[derive(Clone)] -pub struct Terrain { +pub struct Environment { heightmap: Heightmap, pub trees: Grid, } @@ -43,15 +43,15 @@ pub enum TerraformKind { Erode, } -defer_serialize!(Terrain, SerializedTerrain); +defer_serialize!(Environment, SerializedEnvironment); -impl Default for Terrain { +impl Default for Environment { fn default() -> Self { Self::new(0, 0) } } -impl Terrain { +impl Environment { pub fn new(w: u16, h: u16) -> Self { let mut me = Self { heightmap: Heightmap::new(w, h), @@ -315,14 +315,14 @@ pub fn to_pos(encoded: SmolTree, chunk: (u32, u32)) -> Vec2 { } #[derive(Serialize, Deserialize)] -struct SerializedTerrain { +struct SerializedEnvironment { h: Heightmap, trees: Vec<((u32, u32), Vec)>, } -impl From for Terrain { - fn from(ser: SerializedTerrain) -> Self { - let mut terrain = Terrain { +impl From for Environment { + fn from(ser: SerializedEnvironment) -> Self { + let mut terrain = Environment { heightmap: ser.h, ..Self::default() }; @@ -337,9 +337,9 @@ impl From for Terrain { } } -impl From<&Terrain> for SerializedTerrain { - fn from(ter: &Terrain) -> Self { - let mut t = SerializedTerrain { +impl From<&Environment> for SerializedEnvironment { + fn from(ter: &Environment) -> Self { + let mut t = SerializedEnvironment { h: ter.heightmap.clone(), trees: Vec::new(), }; diff --git a/simulation/src/world_command.rs b/simulation/src/world_command.rs index 28d920d3..0da6639c 100644 --- a/simulation/src/world_command.rs +++ b/simulation/src/world_command.rs @@ -10,8 +10,8 @@ use WorldCommand::*; use crate::economy::Government; use crate::map::procgen::{load_parismap, load_testfield}; use crate::map::{ - BuildingID, BuildingKind, IntersectionID, LaneID, LanePattern, LanePatternBuilder, LightPolicy, - LotID, Map, MapProject, ProjectKind, RoadID, TerraformKind, Terrain, TurnPolicy, Zone, + BuildingID, BuildingKind, Environment, IntersectionID, LaneID, LanePattern, LanePatternBuilder, + LightPolicy, LotID, Map, MapProject, ProjectKind, RoadID, TerraformKind, TurnPolicy, Zone, }; use crate::map_dynamic::{BuildingInfos, ParkingManagement}; use crate::multiplayer::chat::Message; @@ -363,7 +363,7 @@ fn generate_terrain(sim: &mut Simulation, size: u16) { info!("generating terrain.."); let t = Instant::now(); - sim.map_mut().terrain = Terrain::new(size, size); + sim.map_mut().environment = Environment::new(size, size); info!("took {}s", t.elapsed().as_secs_f32()); let c = vec3(3000.0 + 72.2 / 2.0, 200.0 / 2.0 + 1.0, 0.3);