use crate::palette::{PaletteFamily, Theme};
use crate::particles::{Emitter, Particle};
use crate::placeholder::{CAMPFIRE, CLAWD_PLACEHOLDER, LANTERN, LOG_SEAT, PixelMap, TELESCOPE};
use crate::recipe::TimeOfDay;
pub const ADVANCE_RATIO: f64 = 0.6;
pub const FONT_TO_CELL: f64 = 0.9;
pub const TARGET_ROWS: u32 = 108;
pub const MIN_CELL_W: u32 = 6;
pub const MIN_CELL_H: u32 = 11;
pub const CHARACTER_BOX: (u32, u32) = (19, 13);
pub const BANDS: u8 = 8;
const DITHER_WIDTH: f64 = 0.4;
const BAYER4: [[u8; 4]; 4] = [[0, 8, 2, 10], [12, 4, 14, 6], [3, 11, 1, 9], [15, 7, 13, 5]];
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GridSpec {
pub cols: u32,
pub rows: u32,
pub cell_w: u32,
pub cell_h: u32,
pub origin_x: u32,
pub origin_y: u32,
pub font_px: f64,
}
pub fn grid_spec(width: u32, height: u32) -> GridSpec {
let cell_h = ((f64::from(height) / f64::from(TARGET_ROWS)).round() as u32).max(MIN_CELL_H);
let font_px = f64::from(cell_h) * FONT_TO_CELL;
let cell_w = ((font_px * ADVANCE_RATIO).round() as u32).max(MIN_CELL_W);
let cols = width / cell_w;
let rows = height / cell_h;
GridSpec {
cols,
rows,
cell_w,
cell_h,
origin_x: (width - cols * cell_w) / 2,
origin_y: (height - rows * cell_h) / 2,
font_px,
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Cell {
pub glyph: u8,
pub shade: u8,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Glow {
pub cx: f64,
pub cy: f64,
pub radius: f64,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Lighting {
pub vignette_cx: f64,
pub vignette_cy: f64,
pub vignette_rx: f64,
pub vignette_ry: f64,
pub glow: Option<Glow>,
}
#[derive(Clone, Copy, Debug)]
pub struct RowTerms {
vy2: f64,
gy2: f64,
bayer_row: usize,
}
impl Lighting {
#[inline]
pub fn row_terms(&self, py: u32) -> RowTerms {
let y = f64::from(py) + 0.5;
let vy = (y - self.vignette_cy) / self.vignette_ry;
let gy2 = match self.glow {
Some(g) => {
let d = y - g.cy;
d * d
}
None => 0.0,
};
RowTerms {
vy2: vy * vy,
gy2,
bayer_row: (py & 3) as usize,
}
}
#[inline]
pub fn band_in_row(&self, rt: &RowTerms, px: u32) -> u8 {
let x = f64::from(px) + 0.5;
let vx = (x - self.vignette_cx) / self.vignette_rx;
let q = (vx * vx + rt.vy2).min(1.0);
let v = 1.0 - q;
let cv = 3.999 * v * v;
let c = match self.glow {
Some(g) => {
let dx = x - g.cx;
let r2 = (dx * dx + rt.gy2) / (g.radius * g.radius);
if r2 < 1.0 {
let t = 1.0 - r2;
cv + t * t * (7.999 - cv)
} else {
cv
}
}
None => cv,
};
let b = BAYER4[rt.bayer_row][(px & 3) as usize];
let d = ((f64::from(b) + 0.5) / 16.0 - 0.5) * DITHER_WIDTH;
(c + d).floor().clamp(0.0, f64::from(BANDS - 1)) as u8
}
pub fn band_at(&self, px: u32, py: u32) -> u8 {
self.band_in_row(&self.row_terms(py), px)
}
pub fn fill_row(&self, py: u32, out: &mut [u8]) {
let rt = self.row_terms(py);
for (px, o) in out.iter_mut().enumerate() {
*o = self.band_in_row(&rt, px as u32);
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SpriteKind {
Character,
Campfire,
LogSeat,
Telescope,
Lantern,
}
impl SpriteKind {
pub const ALL: [SpriteKind; 5] = [
SpriteKind::Character,
SpriteKind::Campfire,
SpriteKind::LogSeat,
SpriteKind::Telescope,
SpriteKind::Lantern,
];
pub fn box_size(self) -> (u32, u32) {
match self {
SpriteKind::Character => CHARACTER_BOX,
other => (other.map().width, other.map().height),
}
}
pub fn map(self) -> &'static PixelMap {
match self {
SpriteKind::Character => &CLAWD_PLACEHOLDER,
SpriteKind::Campfire => &CAMPFIRE,
SpriteKind::LogSeat => &LOG_SEAT,
SpriteKind::Telescope => &TELESCOPE,
SpriteKind::Lantern => &LANTERN,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Motion {
Stationary { x: i32 },
Pace {
x_min: i32,
x_max: i32,
period_ms: u64,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CharacterTrack {
pub y: i32,
pub scale: u32,
pub motion: Motion,
pub face_left: bool,
}
impl CharacterTrack {
pub fn at(&self, t_ms: u64) -> SpriteInstance {
let (x, mirror) = match self.motion {
Motion::Stationary { x } => (x, self.face_left),
Motion::Pace {
x_min,
x_max,
period_ms,
} => {
let span = i64::from(x_max - x_min);
let half = (period_ms / 2).max(1) as i64;
let ph = (t_ms % period_ms.max(1)) as i64;
if ph < half {
((i64::from(x_min) + span * ph / half) as i32, false)
} else {
((i64::from(x_max) - span * (ph - half) / half) as i32, true)
}
}
};
SpriteInstance {
kind: SpriteKind::Character,
x,
y: self.y,
scale: self.scale,
mirror,
}
}
pub fn x_range(&self) -> (i32, i32) {
match self.motion {
Motion::Stationary { x } => (x, x),
Motion::Pace { x_min, x_max, .. } => (x_min, x_max),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SpriteInstance {
pub kind: SpriteKind,
pub x: i32,
pub y: i32,
pub scale: u32,
pub mirror: bool,
}
impl SpriteInstance {
pub fn width(&self) -> u32 {
self.kind.box_size().0 * self.scale
}
pub fn height(&self) -> u32 {
self.kind.box_size().1 * self.scale
}
}
#[derive(Clone, Debug)]
pub struct SceneGraph {
pub width: u32,
pub height: u32,
pub theme: Theme,
pub tod: TimeOfDay,
pub buddy: bool,
pub seed: u64,
pub family: PaletteFamily,
pub grid: GridSpec,
pub cells: Vec<Cell>,
pub lighting: Lighting,
pub activity: Option<String>,
pub props: Vec<SpriteInstance>,
pub unattended: Vec<SpriteInstance>,
pub character: Option<CharacterTrack>,
pub emitters: Vec<Emitter>,
pub ground_y: i32,
pub t_hero_ms: u64,
pub loop_ms: u64,
}
impl SceneGraph {
#[inline]
pub fn cell(&self, col: u32, row: u32) -> Cell {
self.cells[(row * self.grid.cols + col) as usize]
}
pub fn sprites_at(&self, t_ms: u64) -> Vec<SpriteInstance> {
let mut out = self.props.clone();
if self.buddy {
out.extend(self.character.map(|c| c.at(t_ms)));
} else {
out.extend_from_slice(&self.unattended);
}
out
}
pub fn particles_at(&self, t_ms: u64) -> Vec<(u32, u32, Particle)> {
let g = &self.grid;
let mut out = Vec::new();
for e in &self.emitters {
for p in e.sample(t_ms) {
let cx = (p.x - f64::from(g.origin_x)) / f64::from(g.cell_w);
let cy = (p.y - f64::from(g.origin_y)) / f64::from(g.cell_h);
if cx < 0.0 || cy < 0.0 {
continue;
}
let (col, row) = (cx.floor() as u32, cy.floor() as u32);
if col < g.cols && row < g.rows {
out.push((col, row, p));
}
}
}
out
}
}