pub(crate) mod canyon;
pub(crate) mod cave;
pub(crate) mod city;
pub(crate) mod desert;
pub(crate) mod details;
pub(crate) mod forest;
pub(crate) mod layers;
pub(crate) mod ledge;
pub(crate) mod mountains;
pub(crate) mod night_sky;
pub(crate) mod shore;
pub(crate) mod snowfield;
use crate::graph::{Cell, GridSpec};
use crate::hash::{hash3, unit_from_hash};
use crate::math::clamp01;
use crate::recipe::{SceneRecipe, TimeOfDay};
pub const PAINTED: [&str; 9] = [
"canyon",
"mountains",
"desert",
"snowfield",
"forest",
"shore",
"city_rooftops",
"night_sky",
"cave",
];
pub(crate) struct Coords {
pub g: GridSpec,
pub w: f64,
pub h: f64,
}
impl Coords {
pub fn nx(&self, col: u32) -> f64 {
(f64::from(self.g.origin_x) + (f64::from(col) + 0.5) * f64::from(self.g.cell_w)) / self.w
}
pub fn ny(&self, row: u32) -> f64 {
(f64::from(self.g.origin_y) + (f64::from(row) + 0.5) * f64::from(self.g.cell_h)) / self.h
}
pub fn col_of(&self, px: f64) -> i64 {
((px - f64::from(self.g.origin_x)) / f64::from(self.g.cell_w)).floor() as i64
}
}
pub(crate) struct Ctx<'a> {
pub recipe: &'a SceneRecipe,
pub co: Coords,
pub aspect: f64,
pub unit: f64,
pub cols: u32,
pub rows: u32,
pub horizon: f64,
pub platform_row: u32,
pub terrain: u64,
pub gx0: i32,
pub gx1: i32,
pub group_cx: f64,
pub tod: TimeOfDay,
}
impl Ctx<'_> {
#[inline]
pub fn idx(&self, col: u32, row: u32) -> usize {
(row * self.cols + col) as usize
}
}
pub(crate) fn shade_u8(s: f64) -> u8 {
(clamp01(s) * 255.0).round() as u8
}
pub(crate) fn family_glyph(
family: &[u8],
density: f64,
threshold: f64,
jitter_seed: u64,
col: u32,
row: u32,
) -> u8 {
let n = family.len() as f64;
let jitter = (unit_from_hash(hash3(jitter_seed, i64::from(col), i64::from(row))) - 0.5) * 1.2;
let t = (density - threshold) / (1.0 - threshold) * n + jitter;
family[t.floor().clamp(0.0, n - 1.0) as usize]
}
pub(crate) fn background(archetype: &str, ctx: &Ctx, cells: &mut [Cell]) -> Vec<f64> {
match archetype {
"canyon" => canyon::background(ctx, cells),
"mountains" => mountains::background(ctx, cells),
"desert" => desert::background(ctx, cells),
"snowfield" => snowfield::background(ctx, cells),
"forest" => forest::background(ctx, cells),
"shore" => shore::background(ctx, cells),
"city_rooftops" => city::background(ctx, cells),
"night_sky" => night_sky::background(ctx, cells),
"cave" => cave::background(ctx, cells),
other => unreachable!("archetype `{other}` is not painted"),
}
}
fn ledge_style(archetype: &str) -> &'static ledge::LedgeStyle {
match archetype {
"canyon" | "mountains" | "cave" => &ledge::ROCK,
"desert" => &ledge::DUNE,
"snowfield" => &ledge::ICE,
"forest" => &ledge::GRASS,
other => unreachable!("archetype `{other}` is not painted"),
}
}
pub(crate) fn platform(archetype: &str, ctx: &Ctx, cells: &mut [Cell]) {
match archetype {
"shore" => shore::pier(ctx, cells),
"city_rooftops" => city::rooftop(ctx, cells),
"night_sky" => night_sky::island(ctx, cells),
other => ledge::paint(ctx, cells, ledge_style(other)),
}
}
#[cfg(test)]
pub(crate) fn platform_top(archetype: &str) -> u8 {
match archetype {
"shore" | "city_rooftops" => crate::glyphs::id('='),
"night_sky" => crate::glyphs::id('"'),
other => ledge_style(other).top,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::archetype::ARCHETYPE_INFO;
#[test]
fn painted_matches_implemented_flags() {
for a in &ARCHETYPE_INFO {
assert_eq!(a.implemented, PAINTED.contains(&a.id), "{}", a.id);
}
}
}