From 9464a495ea26b39331b766c6d91bf9d258c37389 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Sun, 13 Nov 2022 11:52:10 +0100 Subject: [PATCH] Add coastline view; Fix get_slant 9ea0b448e682d0cf37015b975cc94fe53d58c473 --- planet/src/world.rs | 83 ++++++++++++++++++++++++++++++++++-------- src/planet_renderer.rs | 18 ++++++++- 2 files changed, 84 insertions(+), 17 deletions(-) diff --git a/planet/src/world.rs b/planet/src/world.rs index ace7375..2202f1f 100644 --- a/planet/src/world.rs +++ b/planet/src/world.rs @@ -596,38 +596,89 @@ impl World { let mut west_altitude = 0.0; let mut neighbor_count = 0u8; - if let Some(neighbor) = neighbors.get(&CompassDirection::North) { - west_altitude = f32::max(west_altitude, neighbor.altitude); - neighbor_count += 1; - } - if let Some(neighbor) = neighbors.get(&CompassDirection::NorthWest) { - west_altitude = f32::max(west_altitude, neighbor.altitude); - neighbor_count += 1; - } if let Some(neighbor) = neighbors.get(&CompassDirection::West) { west_altitude = f32::max(west_altitude, neighbor.altitude); neighbor_count += 1; } + if let Some(neighbor) = neighbors.get(&CompassDirection::SouthWest) { + west_altitude = f32::max(west_altitude, neighbor.altitude); + neighbor_count += 1; + } + if let Some(neighbor) = neighbors.get(&CompassDirection::South) { + west_altitude = f32::max(west_altitude, neighbor.altitude); + neighbor_count += 1; + } west_altitude /= f32::from(neighbor_count); neighbor_count = 0; let mut east_altitude = f32::MIN; + if let Some(neighbor) = neighbors.get(&CompassDirection::East) { + east_altitude = f32::max(east_altitude, neighbor.altitude); + neighbor_count += 1; + } + if let Some(neighbor) = neighbors.get(&CompassDirection::NorthEast) { + east_altitude = f32::max(east_altitude, neighbor.altitude); + neighbor_count += 1; + } if let Some(neighbor) = neighbors.get(&CompassDirection::North) { east_altitude = f32::max(east_altitude, neighbor.altitude); neighbor_count += 1; } - if let Some(neighbor) = neighbors.get(&CompassDirection::NorthWest) { - east_altitude = f32::max(east_altitude, neighbor.altitude); - neighbor_count += 1; - } - if let Some(neighbor) = neighbors.get(&CompassDirection::West) { - east_altitude = f32::max(east_altitude, neighbor.altitude); - neighbor_count += 1; - } east_altitude /= f32::from(neighbor_count); west_altitude - east_altitude } + + #[must_use] + pub fn is_cell_coastline(&self, cell: &TerrainCell) -> bool { + if cell.altitude <= 0.0 { + return false; + } + + let neighbors = self.cell_neighbors(cell.x, cell.y); + + if let Some(neighbor) = neighbors.get(&CompassDirection::West) { + if neighbor.altitude <= 0.0 { + return true; + } + } + if let Some(neighbor) = neighbors.get(&CompassDirection::NorthWest) { + if neighbor.altitude <= 0.0 { + return true; + } + } + if let Some(neighbor) = neighbors.get(&CompassDirection::North) { + if neighbor.altitude <= 0.0 { + return true; + } + } + if let Some(neighbor) = neighbors.get(&CompassDirection::NorthEast) { + if neighbor.altitude <= 0.0 { + return true; + } + } + if let Some(neighbor) = neighbors.get(&CompassDirection::East) { + if neighbor.altitude <= 0.0 { + return true; + } + } + if let Some(neighbor) = neighbors.get(&CompassDirection::SouthEast) { + if neighbor.altitude <= 0.0 { + return true; + } + } + if let Some(neighbor) = neighbors.get(&CompassDirection::South) { + if neighbor.altitude <= 0.0 { + return true; + } + } + if let Some(neighbor) = neighbors.get(&CompassDirection::SouthWest) { + if neighbor.altitude <= 0.0 { + return true; + } + } + return false; + } } diff --git a/src/planet_renderer.rs b/src/planet_renderer.rs index ceee40c..f54abdf 100644 --- a/src/planet_renderer.rs +++ b/src/planet_renderer.rs @@ -4,7 +4,11 @@ use { planet::{BiomeStats, TerrainCell, World, WorldManager}, }; -iterable_enum_stringify!(WorldView { Biomes, Topography }); +iterable_enum_stringify!(WorldView { + Biomes, + Topography, + Coastlines +}); iterable_enum_stringify!(WorldOverlay { Temperature, Rainfall @@ -104,6 +108,17 @@ fn biome_color(world: &World, cell: &TerrainCell) -> Color { blue *= slant_factor * altitude_factor; Color::rgb(red, green, blue) } + +#[must_use] +fn coastline_color(world: &World, cell: &TerrainCell) -> Color { + if world.is_cell_coastline(cell) { + Color::BLACK + } else if cell.altitude > 0.0 { + Color::rgb(0.75, 0.75, 0.75) + } else { + Color::ANTIQUE_WHITE + } +} pub(crate) trait WorldRenderer { fn map_color_bytes(&self, render_settings: &WorldRenderSettings) -> Vec; fn generate_color(&self, cell: &TerrainCell, render_settings: &WorldRenderSettings) -> Color; @@ -131,6 +146,7 @@ impl WorldRenderer for WorldManager { let base_color = match render_settings.view { WorldView::Biomes => biome_color(&self.world(), cell), WorldView::Topography => altitude_contour_color(cell.altitude), + WorldView::Coastlines => coastline_color(&self.world(), cell), }; let mut normalizer = 1.0;