use super::layers::{self, Layer};
use super::{Ctx, details, family_glyph, shade_u8};
use crate::glyphs::{self, EMPTY, id};
use crate::graph::Cell;
use crate::hash::{hash_label, hash3};
use crate::math::{clamp01, pm};
use crate::noise::fbm2;
const SEA: [u8; 4] = [id('.'), id('-'), id('~'), id('=')];
const SEA_LINE: f64 = -0.07;
const ISLANDS: Layer = Layer {
family: &glyphs::DISTANT,
hatch: None,
base: SEA_LINE + 0.025,
amp: 0.11,
freq: 1.2,
ridged: false,
plateau: None,
fill: (26.0, 18.0),
threshold: 0.45,
shade: 0.3,
outline: Some(0.45),
snow: None,
label: "shore_islands",
};
pub(crate) fn sea_top(ctx: &Ctx) -> f64 {
ctx.horizon + SEA_LINE
}
pub(crate) fn background(ctx: &Ctx, cells: &mut [Cell]) -> Vec<f64> {
let mut sky = layers::paint(ctx, cells, &[ISLANDS]);
let sea_top = sea_top(ctx);
let seed = hash_label(ctx.terrain, "shore_sea");
let first = (0..ctx.rows)
.find(|&r| ctx.co.ny(r) >= sea_top)
.unwrap_or(ctx.rows);
let span = f64::from(ctx.rows - first).max(1.0);
for row in first..ctx.rows {
let t = f64::from(row - first) / span;
let ny = ctx.co.ny(row);
for col in 0..ctx.cols {
let nx = ctx.co.nx(col);
let idx = ctx.idx(col, row);
if row == first {
cells[idx] = Cell {
glyph: id('_'),
shade: shade_u8(0.42),
};
continue;
}
let n = fbm2(
seed,
nx * ctx.aspect * (5.0 + 14.0 * t),
ny * (90.0 + 40.0 * t),
3,
);
let d = clamp01((n - 0.5) * 2.2 + 0.5);
let threshold = 0.70 - 0.22 * t;
cells[idx] = if d < threshold {
Cell::default()
} else {
let glyph = family_glyph(&SEA, d, threshold, seed, col, row);
let mut shade = 0.3 + 0.35 * t;
if row > ctx.platform_row {
shade *= 0.8;
}
Cell {
glyph,
shade: shade_u8(shade),
}
};
}
}
for s in &mut sky {
*s = s.min(sea_top);
}
if details::enabled(ctx) {
details::shore_life(ctx, cells, &mut sky, sea_top);
}
sky
}
pub(crate) fn open_water(ctx: &Ctx) -> (i32, i32) {
let co = &ctx.co;
let comp = &ctx.recipe.composition;
let margin_px = pm(comp.platform_margin_pm) * ctx.unit + 2.0 * f64::from(co.g.cell_w);
let from_left = ctx.group_cx < co.w * 0.5;
let (end, out) = if from_left {
(f64::from(ctx.gx1) + margin_px, 1.0)
} else {
(f64::from(ctx.gx0) - margin_px, -1.0)
};
let x = end + out * 3.0 * f64::from(co.g.cell_w);
let water_row = ctx.platform_row + 2;
let y = co.g.origin_y + water_row * co.g.cell_h + co.g.cell_h / 2;
(x.round() as i32, y as i32)
}
pub(crate) fn pier(ctx: &Ctx, cells: &mut [Cell]) {
let co = &ctx.co;
let comp = &ctx.recipe.composition;
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).max(0);
let pc1 = co
.col_of(f64::from(ctx.gx1) + margin_px)
.min(i64::from(ctx.cols) - 1);
let from_left = ctx.group_cx < co.w * 0.5;
let (c0, c1, sea_end) = if from_left {
(0, pc1, pc1)
} else {
(pc0, i64::from(ctx.cols) - 1, pc0)
};
let deck = i64::from(ctx.platform_row);
let water = deck + 2;
let depth = 4;
let seed = hash_label(ctx.terrain, "pier");
for c in c0..=c1 {
let k = hash3(seed, c, 0);
let worn = k.is_multiple_of(11) && !(pc0..=pc1).contains(&c);
let glyph = if worn { id('-') } else { id('=') };
layers::put(ctx, cells, c, deck, glyph, 0.85);
for r in deck + 1..water {
layers::put(ctx, cells, c, r, EMPTY, 0.0);
}
}
let step = if from_left { -6 } else { 6 };
let mut c = sea_end;
while (c0..=c1).contains(&c) {
for r in deck + 1..water + depth {
let glyph = if r == water { id('#') } else { id('|') };
layers::put(ctx, cells, c, r, glyph, 0.6);
}
for dc in [-1, 1] {
layers::put(ctx, cells, c + dc, water, id('~'), 0.5);
}
c += step;
}
layers::put(ctx, cells, sea_end, deck - 1, id('|'), 0.7);
layers::put(ctx, cells, sea_end, deck - 2, id('o'), 0.7);
}