use super::{Ctx, family_glyph, shade_u8};
use crate::glyphs;
use crate::graph::Cell;
use crate::hash::hash_label;
use crate::math::{clamp01, lerp, pm, smoothstep};
use crate::noise::{fbm1, fbm2};
pub(crate) struct LedgeStyle {
pub family: &'static [u8],
pub top: u8,
pub top_shade: f64,
pub shade: (f64, f64),
pub threshold: f64,
pub bias: f64,
pub widen: (f64, f64, f64),
pub roughness: f64,
pub freq: (f64, f64),
}
pub(crate) const ROCK: LedgeStyle = LedgeStyle {
family: &glyphs::PLATFORM,
top: glyphs::PLATFORM_TOP,
top_shade: 0.85,
shade: (0.80, 0.40),
threshold: 0.20,
bias: 0.42,
widen: (0.6, 1.8, 0.25),
roughness: 9.0,
freq: (9.0, 38.0),
};
pub(crate) const ICE: LedgeStyle = LedgeStyle {
family: &glyphs::ICE,
top: glyphs::id('='),
top_shade: 0.9,
shade: (0.75, 0.35),
threshold: 0.15,
bias: 0.38,
widen: (0.8, 2.4, 0.4),
roughness: 4.0,
freq: (6.0, 20.0),
};
pub(crate) const DUNE: LedgeStyle = LedgeStyle {
family: &glyphs::SAND,
top: glyphs::id('~'),
top_shade: 0.85,
shade: (0.75, 0.40),
threshold: 0.22,
bias: 0.40,
widen: (1.6, 4.8, 1.2),
roughness: 5.0,
freq: (3.0, 45.0),
};
pub(crate) const GRASS: LedgeStyle = LedgeStyle {
family: &glyphs::EARTH,
top: glyphs::id('"'),
top_shade: 0.85,
shade: (0.7, 0.35),
threshold: 0.25,
bias: 0.42,
widen: (0.8, 2.4, 0.5),
roughness: 6.0,
freq: (8.0, 30.0),
};
pub(crate) fn paint(ctx: &Ctx, cells: &mut [Cell], style: &LedgeStyle) {
let co = &ctx.co;
let comp = &ctx.recipe.composition;
let platform_seed = hash_label(ctx.terrain, "platform");
let margin_px = pm(comp.platform_margin_pm) * ctx.unit + 2.0 * f64::from(co.g.cell_w);
let pc0 = co.col_of(f64::from(ctx.gx0) - margin_px);
let pc1 = co.col_of(f64::from(ctx.gx1) + margin_px);
let span_rows = f64::from(ctx.rows - ctx.platform_row);
let (cols, platform_row) = (ctx.cols, ctx.platform_row);
for row in platform_row..ctx.rows {
let k = f64::from(row - platform_row);
let widen = if k < 3.0 {
style.widen.0 * k
} else {
style.widen.1 + style.widen.2 * (k - 3.0)
};
let rough = style.roughness * (k / 3.0).min(1.0);
let el = (fbm1(hash_label(platform_seed, "left"), k * 0.22, 3) - 0.5) * rough;
let er = (fbm1(hash_label(platform_seed, "right"), k * 0.22, 3) - 0.5) * rough;
let c0 = (pc0 as f64 - widen - el).round() as i64;
let c1 = (pc1 as f64 + widen + er).round() as i64;
for c in [c0 - 1, c1 + 1] {
if (0..i64::from(cols)).contains(&c) {
cells[ctx.idx(c as u32, row)] = Cell::default();
}
}
let fade = smoothstep(0.0, 1.0, k / span_rows);
for c in c0.max(0)..=c1.min(i64::from(cols) - 1) {
let col = c as u32;
let idx = ctx.idx(col, row);
if k == 0.0 {
cells[idx] = Cell {
glyph: style.top,
shade: shade_u8(style.top_shade),
};
continue;
}
let n = fbm2(
platform_seed,
co.nx(col) * ctx.aspect * style.freq.0,
co.ny(row) * style.freq.1,
3,
);
let d = clamp01((n - 0.5) * 1.8 + style.bias);
if d < style.threshold && k > 1.0 {
cells[idx] = Cell::default();
continue;
}
let glyph = family_glyph(
style.family,
d.max(style.threshold),
style.threshold,
platform_seed,
col,
row,
);
cells[idx] = Cell {
glyph,
shade: shade_u8(lerp(style.shade.0, style.shade.1, fade)),
};
}
}
}