pub(crate) mod bamboo;
pub(crate) mod canyon;
pub(crate) mod cave;
pub(crate) mod city;
pub(crate) mod countryside;
pub(crate) mod crystals;
pub(crate) mod desert;
pub(crate) mod details;
pub(crate) mod fjord;
pub(crate) mod forest;
pub(crate) mod great_tree;
pub(crate) mod greenhouse;
pub(crate) mod harbour;
pub(crate) mod interior;
pub(crate) mod islands;
pub(crate) mod lake;
pub mod landmark;
pub(crate) mod lantern_lake;
pub(crate) mod layers;
pub(crate) mod ledge;
pub(crate) mod lighthouse;
pub(crate) mod lunar;
pub(crate) mod meadow;
pub(crate) mod mountains;
pub(crate) mod mushrooms;
pub(crate) mod night_sky;
pub(crate) mod oasis;
pub(crate) mod observatory;
pub(crate) mod ruins;
pub(crate) mod savanna;
pub(crate) mod shore;
pub(crate) mod shrine;
pub(crate) mod snowfield;
pub(crate) mod standing_stones;
pub(crate) mod swamp;
pub(crate) mod viaduct;
pub(crate) mod volcano;
pub(crate) mod waterfall;
pub(crate) mod windmills;
pub(crate) mod wizard_tower;
pub(crate) mod zen_garden;
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; 37] = [
"canyon",
"mountains",
"desert",
"snowfield",
"forest",
"shore",
"city_rooftops",
"night_sky",
"cave",
"interior",
"meadow",
"lake",
"volcano",
"islands",
"lunar",
"bamboo",
"waterfall",
"countryside",
"ruins",
"savanna",
"swamp",
"mushrooms",
"crystals",
"great_tree",
"wizard_tower",
"standing_stones",
"lantern_lake",
"windmills",
"oasis",
"observatory",
"zen_garden",
"harbour",
"lighthouse",
"fjord",
"viaduct",
"shrine",
"greenhouse",
];
pub(crate) struct Journeyed {
pub sky_limit: Vec<f64>,
pub layers: Vec<crate::graph::ScrollLayer>,
pub surface: Vec<f64>,
}
pub(crate) fn journey(archetype: &str, ctx: &Ctx, cells: &mut [Cell]) -> Option<Journeyed> {
let landmark = landmark::paint(ctx, cells);
let mut j = match archetype {
"mountains" => mountains::journey(ctx, cells),
"snowfield" => snowfield::journey(ctx, cells),
"forest" => forest::journey(ctx, cells),
"desert" => desert::journey(ctx, cells),
_ => return None,
};
for (s, l) in j.sky_limit.iter_mut().zip(landmark) {
*s = s.min(l);
}
Some(j)
}
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,
pub unsheltered: std::cell::RefCell<Vec<Option<f64>>>,
}
impl Ctx<'_> {
#[inline]
pub fn idx(&self, col: u32, row: u32) -> usize {
(row * self.cols + col) as usize
}
pub fn row_scale(&self) -> f64 {
f64::from(self.rows) / f64::from(crate::graph::TARGET_ROWS)
}
pub fn scaled(&self, rows: f64) -> i64 {
(rows * self.row_scale()).round() as i64
}
pub fn land_line(&self, sky_limit: &[f64]) -> Vec<f64> {
let u = self.unsheltered.borrow();
sky_limit
.iter()
.enumerate()
.map(|(c, &s)| u.get(c).copied().flatten().map_or(s, |v| v.max(s)))
.collect()
}
}
pub(crate) fn sea_line(archetype: &str, ctx: &Ctx) -> Option<f64> {
match archetype {
"shore" => Some(shore::sea_top(ctx)),
"lighthouse" => Some(lighthouse::sea_top(ctx)),
"harbour" => Some(harbour::water_top(ctx)),
"fjord" => Some(fjord::sea_top(ctx)),
_ => None,
}
}
pub(crate) fn shelter(ctx: &Ctx, sky: &mut [f64], col: i64, top: f64) {
let Some(s) = usize::try_from(col).ok().and_then(|c| sky.get_mut(c)) else {
return;
};
if *s > top {
ctx.unsheltered.borrow_mut()[col as usize].get_or_insert(*s);
*s = top;
}
}
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),
"interior" => interior::background(ctx, cells),
"meadow" => meadow::background(ctx, cells),
"lake" => lake::background(ctx, cells),
"volcano" => volcano::background(ctx, cells),
"islands" => islands::background(ctx, cells),
"lunar" => lunar::background(ctx, cells),
"bamboo" => bamboo::background(ctx, cells),
"waterfall" => waterfall::background(ctx, cells),
"countryside" => countryside::background(ctx, cells),
"ruins" => ruins::background(ctx, cells),
"savanna" => savanna::background(ctx, cells),
"swamp" => swamp::background(ctx, cells),
"mushrooms" => mushrooms::background(ctx, cells),
"crystals" => crystals::background(ctx, cells),
"great_tree" => great_tree::background(ctx, cells),
"wizard_tower" => wizard_tower::background(ctx, cells),
"standing_stones" => standing_stones::background(ctx, cells),
"lantern_lake" => lantern_lake::background(ctx, cells),
"windmills" => windmills::background(ctx, cells),
"oasis" => oasis::background(ctx, cells),
"observatory" => observatory::background(ctx, cells),
"zen_garden" => zen_garden::background(ctx, cells),
"harbour" => harbour::background(ctx, cells),
"lighthouse" => lighthouse::background(ctx, cells),
"fjord" => fjord::background(ctx, cells),
"viaduct" => viaduct::background(ctx, cells),
"shrine" => shrine::background(ctx, cells),
"greenhouse" => greenhouse::background(ctx, cells),
other => unreachable!("archetype `{other}` is not painted"),
}
}
fn ledge_style(archetype: &str) -> &'static ledge::LedgeStyle {
match archetype {
"canyon" | "mountains" | "cave" | "volcano" | "lunar" | "waterfall" | "ruins"
| "crystals" | "wizard_tower" | "observatory" | "harbour" | "lighthouse" | "fjord"
| "shrine" => &ledge::ROCK,
"desert" | "islands" | "savanna" | "oasis" => &ledge::DUNE,
"snowfield" => &ledge::ICE,
"forest" | "meadow" | "lake" | "bamboo" | "countryside" | "swamp" | "mushrooms"
| "great_tree" | "standing_stones" | "lantern_lake" | "windmills" | "zen_garden"
| "viaduct" | "greenhouse" => &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),
"interior" => interior::floor(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" | "interior" => 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);
}
}
}