1use crate::palette::{PaletteFamily, Theme};
6use crate::particles::{Emitter, Particle};
7use crate::placeholder::{
8 CAMP_STOVE, CAMPFIRE, CLAWD_PLACEHOLDER, COFFEE_MUG, FALLEN_HAT, LANTERN, LOG_SEAT, PixelMap,
9 SPELLBOOK, TELESCOPE,
10};
11use crate::recipe::TimeOfDay;
12
13pub const ADVANCE_RATIO: f64 = 0.6;
15pub const FONT_TO_CELL: f64 = 0.9;
17pub const TARGET_ROWS: u32 = 108;
19pub const MIN_CELL_W: u32 = 6;
20pub const MIN_CELL_H: u32 = 11;
21
22pub const CHARACTER_BOX: (u32, u32) = (19, 13);
25
26pub const BANDS: u8 = 8;
28const DITHER_WIDTH: f64 = 0.4;
30const BAYER4: [[u8; 4]; 4] = [[0, 8, 2, 10], [12, 4, 14, 6], [3, 11, 1, 9], [15, 7, 13, 5]];
32
33#[derive(Clone, Copy, Debug, PartialEq)]
35pub struct GridSpec {
36 pub cols: u32,
37 pub rows: u32,
38 pub cell_w: u32,
39 pub cell_h: u32,
40 pub origin_x: u32,
42 pub origin_y: u32,
43 pub font_px: f64,
45}
46
47pub fn grid_spec(width: u32, height: u32) -> GridSpec {
49 let cell_h = ((f64::from(height) / f64::from(TARGET_ROWS)).round() as u32).max(MIN_CELL_H);
50 let font_px = f64::from(cell_h) * FONT_TO_CELL;
51 let cell_w = ((font_px * ADVANCE_RATIO).round() as u32).max(MIN_CELL_W);
52 let cols = width / cell_w;
53 let rows = height / cell_h;
54 GridSpec {
55 cols,
56 rows,
57 cell_w,
58 cell_h,
59 origin_x: (width - cols * cell_w) / 2,
60 origin_y: (height - rows * cell_h) / 2,
61 font_px,
62 }
63}
64
65#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
67pub struct Cell {
68 pub glyph: u8,
69 pub shade: u8,
70}
71
72#[derive(Clone, Copy, Debug, PartialEq)]
74pub struct Glow {
75 pub cx: f64,
76 pub cy: f64,
77 pub radius: f64,
78}
79
80#[derive(Clone, Copy, Debug, PartialEq)]
87pub struct Lighting {
88 pub vignette_cx: f64,
89 pub vignette_cy: f64,
90 pub vignette_rx: f64,
91 pub vignette_ry: f64,
92 pub glow: Option<Glow>,
93}
94
95#[derive(Clone, Copy, Debug)]
97pub struct RowTerms {
98 vy2: f64,
99 gy2: f64,
100 bayer_row: usize,
101}
102
103impl Lighting {
104 #[inline]
105 pub fn row_terms(&self, py: u32) -> RowTerms {
106 let y = f64::from(py) + 0.5;
107 let vy = (y - self.vignette_cy) / self.vignette_ry;
108 let gy2 = match self.glow {
109 Some(g) => {
110 let d = y - g.cy;
111 d * d
112 }
113 None => 0.0,
114 };
115 RowTerms {
116 vy2: vy * vy,
117 gy2,
118 bayer_row: (py & 3) as usize,
119 }
120 }
121
122 #[inline]
124 pub fn band_in_row(&self, rt: &RowTerms, px: u32) -> u8 {
125 let x = f64::from(px) + 0.5;
126 let vx = (x - self.vignette_cx) / self.vignette_rx;
127 let q = (vx * vx + rt.vy2).min(1.0);
128 let v = 1.0 - q;
129 let cv = 3.999 * v * v;
130 let c = match self.glow {
131 Some(g) => {
132 let dx = x - g.cx;
133 let r2 = (dx * dx + rt.gy2) / (g.radius * g.radius);
134 if r2 < 1.0 {
135 let t = 1.0 - r2;
136 cv + t * t * (7.999 - cv)
137 } else {
138 cv
139 }
140 }
141 None => cv,
142 };
143 let b = BAYER4[rt.bayer_row][(px & 3) as usize];
144 let d = ((f64::from(b) + 0.5) / 16.0 - 0.5) * DITHER_WIDTH;
145 (c + d).floor().clamp(0.0, f64::from(BANDS - 1)) as u8
146 }
147
148 pub fn band_at(&self, px: u32, py: u32) -> u8 {
150 self.band_in_row(&self.row_terms(py), px)
151 }
152
153 pub fn fill_row(&self, py: u32, out: &mut [u8]) {
155 let rt = self.row_terms(py);
156 for (px, o) in out.iter_mut().enumerate() {
157 *o = self.band_in_row(&rt, px as u32);
158 }
159 }
160}
161
162#[derive(Clone, Copy, Debug, PartialEq, Eq)]
164pub enum SpriteKind {
165 Character,
167 Campfire,
168 LogSeat,
170 Telescope,
171 Lantern,
172 Spellbook,
173 CampStove,
174 CoffeeMug,
175 FallenHat,
176}
177
178impl SpriteKind {
179 pub const ALL: [SpriteKind; 9] = [
181 SpriteKind::Character,
182 SpriteKind::Campfire,
183 SpriteKind::LogSeat,
184 SpriteKind::Telescope,
185 SpriteKind::Lantern,
186 SpriteKind::Spellbook,
187 SpriteKind::CampStove,
188 SpriteKind::CoffeeMug,
189 SpriteKind::FallenHat,
190 ];
191
192 pub fn box_size(self) -> (u32, u32) {
195 match self {
196 SpriteKind::Character => CHARACTER_BOX,
197 other => (other.map().width, other.map().height),
198 }
199 }
200
201 pub fn map(self) -> &'static PixelMap {
202 match self {
203 SpriteKind::Character => &CLAWD_PLACEHOLDER,
204 SpriteKind::Campfire => &CAMPFIRE,
205 SpriteKind::LogSeat => &LOG_SEAT,
206 SpriteKind::Telescope => &TELESCOPE,
207 SpriteKind::Lantern => &LANTERN,
208 SpriteKind::Spellbook => &SPELLBOOK,
209 SpriteKind::CampStove => &CAMP_STOVE,
210 SpriteKind::CoffeeMug => &COFFEE_MUG,
211 SpriteKind::FallenHat => &FALLEN_HAT,
212 }
213 }
214}
215
216#[derive(Clone, Copy, Debug, PartialEq, Eq)]
218pub enum Motion {
219 Stationary { x: i32 },
221 Pace {
223 x_min: i32,
224 x_max: i32,
225 period_ms: u64,
226 },
227}
228
229#[derive(Clone, Copy, Debug, PartialEq, Eq)]
231pub struct CharacterTrack {
232 pub y: i32,
234 pub scale: u32,
235 pub motion: Motion,
236 pub face_left: bool,
238}
239
240impl CharacterTrack {
241 pub fn at(&self, t_ms: u64) -> SpriteInstance {
243 let (x, mirror) = match self.motion {
244 Motion::Stationary { x } => (x, self.face_left),
245 Motion::Pace {
246 x_min,
247 x_max,
248 period_ms,
249 } => {
250 let span = i64::from(x_max - x_min);
251 let half = (period_ms / 2).max(1) as i64;
252 let ph = (t_ms % period_ms.max(1)) as i64;
253 if ph < half {
254 ((i64::from(x_min) + span * ph / half) as i32, false)
255 } else {
256 ((i64::from(x_max) - span * (ph - half) / half) as i32, true)
257 }
258 }
259 };
260 SpriteInstance {
261 kind: SpriteKind::Character,
262 x,
263 y: self.y,
264 scale: self.scale,
265 mirror,
266 }
267 }
268
269 pub fn x_range(&self) -> (i32, i32) {
271 match self.motion {
272 Motion::Stationary { x } => (x, x),
273 Motion::Pace { x_min, x_max, .. } => (x_min, x_max),
274 }
275 }
276}
277
278#[derive(Clone, Copy, Debug, PartialEq, Eq)]
280pub struct SpriteInstance {
281 pub kind: SpriteKind,
282 pub x: i32,
284 pub y: i32,
285 pub scale: u32,
286 pub mirror: bool,
287}
288
289impl SpriteInstance {
290 pub fn width(&self) -> u32 {
291 self.kind.box_size().0 * self.scale
292 }
293 pub fn height(&self) -> u32 {
294 self.kind.box_size().1 * self.scale
295 }
296}
297
298#[derive(Clone, Debug)]
300pub struct SceneGraph {
301 pub width: u32,
302 pub height: u32,
303 pub theme: Theme,
304 pub tod: TimeOfDay,
305 pub buddy: bool,
306 pub seed: u64,
308 pub family: PaletteFamily,
309 pub grid: GridSpec,
310 pub cells: Vec<Cell>,
312 pub lighting: Lighting,
313 pub activity: Option<String>,
315 pub props: Vec<SpriteInstance>,
317 pub unattended: Vec<SpriteInstance>,
319 pub character: Option<CharacterTrack>,
321 pub emitters: Vec<Emitter>,
322 pub ground_y: i32,
324 pub t_hero_ms: u64,
326 pub loop_ms: u64,
328}
329
330impl SceneGraph {
331 #[inline]
332 pub fn cell(&self, col: u32, row: u32) -> Cell {
333 self.cells[(row * self.grid.cols + col) as usize]
334 }
335
336 pub fn sprites_at(&self, t_ms: u64) -> Vec<SpriteInstance> {
339 let mut out = self.props.clone();
340 if self.buddy {
341 out.extend(self.character.map(|c| c.at(t_ms)));
342 } else {
343 out.extend_from_slice(&self.unattended);
344 }
345 out
346 }
347
348 pub fn particles_at(&self, t_ms: u64) -> Vec<(u32, u32, Particle)> {
350 let g = &self.grid;
351 let mut out = Vec::new();
352 for e in &self.emitters {
353 for p in e.sample(t_ms) {
354 let cx = (p.x - f64::from(g.origin_x)) / f64::from(g.cell_w);
355 let cy = (p.y - f64::from(g.origin_y)) / f64::from(g.cell_h);
356 if cx < 0.0 || cy < 0.0 {
357 continue;
358 }
359 let (col, row) = (cx.floor() as u32, cy.floor() as u32);
360 if col < g.cols && row < g.rows {
361 out.push((col, row, p));
362 }
363 }
364 }
365 out
366 }
367}