Add coastline view; Fix get_slant

9ea0b448e682d0cf37015b975cc94fe53d58c473
This commit is contained in:
Tobias Berger 2022-11-13 11:52:10 +01:00
parent 616a7d0697
commit 9464a495ea
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
2 changed files with 84 additions and 17 deletions

View file

@ -596,38 +596,89 @@ impl World {
let mut west_altitude = 0.0; let mut west_altitude = 0.0;
let mut neighbor_count = 0u8; 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) { if let Some(neighbor) = neighbors.get(&CompassDirection::West) {
west_altitude = f32::max(west_altitude, neighbor.altitude); west_altitude = f32::max(west_altitude, neighbor.altitude);
neighbor_count += 1; 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); west_altitude /= f32::from(neighbor_count);
neighbor_count = 0; neighbor_count = 0;
let mut east_altitude = f32::MIN; 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) { if let Some(neighbor) = neighbors.get(&CompassDirection::North) {
east_altitude = f32::max(east_altitude, neighbor.altitude); east_altitude = f32::max(east_altitude, neighbor.altitude);
neighbor_count += 1; 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); east_altitude /= f32::from(neighbor_count);
west_altitude - east_altitude 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;
}
} }

View file

@ -4,7 +4,11 @@ use {
planet::{BiomeStats, TerrainCell, World, WorldManager}, planet::{BiomeStats, TerrainCell, World, WorldManager},
}; };
iterable_enum_stringify!(WorldView { Biomes, Topography }); iterable_enum_stringify!(WorldView {
Biomes,
Topography,
Coastlines
});
iterable_enum_stringify!(WorldOverlay { iterable_enum_stringify!(WorldOverlay {
Temperature, Temperature,
Rainfall Rainfall
@ -104,6 +108,17 @@ fn biome_color(world: &World, cell: &TerrainCell) -> Color {
blue *= slant_factor * altitude_factor; blue *= slant_factor * altitude_factor;
Color::rgb(red, green, blue) 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 { pub(crate) trait WorldRenderer {
fn map_color_bytes(&self, render_settings: &WorldRenderSettings) -> Vec<u8>; fn map_color_bytes(&self, render_settings: &WorldRenderSettings) -> Vec<u8>;
fn generate_color(&self, cell: &TerrainCell, render_settings: &WorldRenderSettings) -> Color; 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 { let base_color = match render_settings.view {
WorldView::Biomes => biome_color(&self.world(), cell), WorldView::Biomes => biome_color(&self.world(), cell),
WorldView::Topography => altitude_contour_color(cell.altitude), WorldView::Topography => altitude_contour_color(cell.altitude),
WorldView::Coastlines => coastline_color(&self.world(), cell),
}; };
let mut normalizer = 1.0; let mut normalizer = 1.0;