| 1 | //! The native marine habitat: a fish school, a far shoal of minnows, one |
| 2 | //! visiting jellyfish, marine snow, bubbles and a distant whale. |
| 3 | //! |
| 4 | //! This is the one ocean Codewhale draws, built for the Engine's Underwater |
| 5 | //! field through [`Habitat`]. These components own no clock, simulation, |
| 6 | //! input handler or runtime state. The host supplies elapsed time, motion |
| 7 | //! policy, an optional ink and a presence. Reduced and still motion remove |
| 8 | //! ambient life; a host may separately paint its existing [`crate::Whale`]. |
| 9 | //! |
| 10 | //! Every mark lerps from the water it sits on toward its ink at a slow, |
| 11 | //! quantised glow that stays well below text, so a frame changes only the |
| 12 | //! cells a creature moves through. Positions are pure functions of elapsed |
| 13 | //! time: a host may change ink or presence without moving anything. |
| 14 | //! |
| 15 | //! Paint content first, then habitat. Every creature needs open water: no |
| 16 | //! visible glyph within one row and two columns of its whole silhouette, and |
| 17 | //! no overlap with [`Habitat::protected`]. Multi-row creatures are withheld |
| 18 | //! as a unit rather than split or moved around text. No component clears its |
| 19 | //! area or repaints existing text. Painting into a fresh buffer each frame |
| 20 | //! lets creatures move without leaving trails. |
| 21 | //! |
| 22 | //! Known limits: creatures interact only through that open-water rule; the |
| 23 | //! distant whale is withheld for the frame when any part of it would touch |
| 24 | //! text; ASCII themes keep the fish, jellyfish and bubbles only; non-truecolor |
| 25 | //! themes, unknown grounds or non-RGB water or ink fall back to the plain role |
| 26 | //! colour without a glow. |
| 27 | |
| 28 | use std::time::Duration; |
| 29 | |
| 30 | use ratatui::{ |
| 31 | buffer::{Buffer, CellDiffOption}, |
| 32 | layout::Rect, |
| 33 | style::{Color, Style}, |
| 34 | }; |
| 35 | |
| 36 | use crate::{MotionMode, Paint, Role, Theme, color::ColorDepth}; |
| 37 | |
| 38 | #[path = "habitat_poses.rs"] |
| 39 | mod poses; |
| 40 | |
| 41 | /// A bounded marine population. Auto follows the Engine's terminal tiers. |
| 42 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] |
| 43 | pub enum HabitatDensity { |
| 44 | #[default] |
| 45 | Auto, |
| 46 | /// Three fish, one bubble stream, four motes, no shoal. |
| 47 | Sparse, |
| 48 | /// Five fish, three minnows, two bubble streams, seven motes. |
| 49 | Normal, |
| 50 | /// Six fish, five minnows, three bubble streams, ten motes. There is |
| 51 | /// still at most one jellyfish and one whale. |
| 52 | Rich, |
| 53 | } |
| 54 | |
| 55 | impl HabitatDensity { |
| 56 | #[must_use] |
| 57 | pub const fn for_area(self, area: Rect) -> Self { |
| 58 | match self { |
| 59 | Self::Auto if area.width < 56 || area.height < 12 => Self::Sparse, |
| 60 | Self::Auto if area.width < 88 || area.height < 20 => Self::Normal, |
| 61 | Self::Auto => Self::Rich, |
| 62 | other => other, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn fish(self) -> usize { |
| 67 | match self { |
| 68 | Self::Sparse => 3, |
| 69 | Self::Normal => 5, |
| 70 | Self::Rich | Self::Auto => 6, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn minnows(self) -> usize { |
| 75 | match self { |
| 76 | Self::Sparse => 0, |
| 77 | Self::Normal => 3, |
| 78 | Self::Rich | Self::Auto => 5, |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn bubbles(self) -> usize { |
| 83 | match self { |
| 84 | Self::Sparse => 1, |
| 85 | Self::Normal => 2, |
| 86 | Self::Rich | Self::Auto => 3, |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | fn motes(self) -> usize { |
| 91 | match self { |
| 92 | Self::Sparse => 4, |
| 93 | Self::Normal => 7, |
| 94 | Self::Rich | Self::Auto => 10, |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | const CLEAR_COLS: u16 = 2; |
| 100 | const CLEAR_ROWS: u16 = 1; |
| 101 | const GLOW_STEPS: f32 = 12.0; |
| 102 | |
| 103 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 104 | struct Brush { |
| 105 | ink: Option<Color>, |
| 106 | presence: u16, |
| 107 | } |
| 108 | |
| 109 | impl Brush { |
| 110 | fn visible(self) -> bool { |
| 111 | self.presence > 0 |
| 112 | } |
| 113 | |
| 114 | fn amount(self, glow: f32) -> f32 { |
| 115 | let amount = glow * f32::from(self.presence) / 1_000.0; |
| 116 | (amount.clamp(0.0, 1.0) * GLOW_STEPS).round() / GLOW_STEPS |
| 117 | } |
| 118 | |
| 119 | fn style(self, theme: &Theme, role: Role, glow: f32, water: Color) -> Option<Style> { |
| 120 | let amount = self.amount(glow); |
| 121 | if amount == 0.0 { |
| 122 | return None; |
| 123 | } |
| 124 | let can_glow = theme.paints_grounds() && theme.depth() == ColorDepth::TrueColor; |
| 125 | let ink = self.ink.unwrap_or_else(|| theme.token(role)); |
| 126 | Some(match crate::color::glow_over(ink, water, amount) { |
| 127 | Some(color) if can_glow => Style::default().fg(color), |
| 128 | _ => theme.fg(role), |
| 129 | }) |
| 130 | } |
| 131 | |
| 132 | fn put( |
| 133 | self, |
| 134 | buf: &mut Buffer, |
| 135 | at: Rect, |
| 136 | chars: impl IntoIterator<Item = char>, |
| 137 | theme: &Theme, |
| 138 | role: Role, |
| 139 | glow: f32, |
| 140 | ) -> bool { |
| 141 | if self.amount(glow) == 0.0 { |
| 142 | return false; |
| 143 | } |
| 144 | let mut painted = false; |
| 145 | for (offset, ch) in chars.into_iter().take(usize::from(at.width)).enumerate() { |
| 146 | if ch == ' ' { |
| 147 | continue; |
| 148 | } |
| 149 | let Some(cell) = buf.cell_mut((at.x + offset as u16, at.y)) else { |
| 150 | continue; |
| 151 | }; |
| 152 | let Some(style) = self.style(theme, role, glow, cell.bg) else { |
| 153 | continue; |
| 154 | }; |
| 155 | cell.set_char(ch).set_style(style); |
| 156 | painted = true; |
| 157 | } |
| 158 | painted |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | macro_rules! brush_builders { |
| 163 | () => { |
| 164 | /// Ink for every mark, lerped from the water by the creature's glow. |
| 165 | /// `None` (the default) uses the creature's role token. |
| 166 | #[must_use] |
| 167 | pub const fn ink(mut self, ink: Option<Color>) -> Self { |
| 168 | self.brush.ink = ink; |
| 169 | self |
| 170 | } |
| 171 | |
| 172 | /// Scales every glow, `0.0..=1.0`. Zero paints nothing; it lets a host |
| 173 | /// fade life in and out without moving it. |
| 174 | #[must_use] |
| 175 | pub fn presence(mut self, presence: f32) -> Self { |
| 176 | self.brush.presence = (presence.clamp(0.0, 1.0) * 1_000.0).round() as u16; |
| 177 | self |
| 178 | } |
| 179 | }; |
| 180 | } |
| 181 | |
| 182 | const WEDGE: &[(i32, u16)] = &[(0, 0), (-1, 4), (1, 6), (-1, 9), (1, 11), (0, 14)]; |
| 183 | const SCHOOL_CELL_MS: u128 = 760; |
| 184 | const FISH_WAVE_MS: u128 = 3_600; |
| 185 | const FISH_GLOW: (f32, f32) = (0.30, 0.62); |
| 186 | |
| 187 | /// One loose school near the floor, with four native tail poses that step |
| 188 | /// with half-cell travel. ASCII terminals keep an eyed lead and smaller |
| 189 | /// members. |
| 190 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 191 | pub struct FishSchool { |
| 192 | pub elapsed: Duration, |
| 193 | pub motion: MotionMode, |
| 194 | pub density: HabitatDensity, |
| 195 | brush: Brush, |
| 196 | } |
| 197 | |
| 198 | impl FishSchool { |
| 199 | #[must_use] |
| 200 | pub const fn new(elapsed: Duration, motion: MotionMode) -> Self { |
| 201 | Self { |
| 202 | elapsed, |
| 203 | motion, |
| 204 | density: HabitatDensity::Auto, |
| 205 | brush: Brush { |
| 206 | ink: None, |
| 207 | presence: 1_000, |
| 208 | }, |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | #[must_use] |
| 213 | pub const fn density(mut self, density: HabitatDensity) -> Self { |
| 214 | self.density = density; |
| 215 | self |
| 216 | } |
| 217 | |
| 218 | brush_builders!(); |
| 219 | } |
| 220 | |
| 221 | impl FishSchool { |
| 222 | fn paint_except(&self, area: Rect, buf: &mut Buffer, theme: &Theme, protected: &[Rect]) { |
| 223 | let area = area.intersection(buf.area); |
| 224 | if !self.motion.animates() || !self.brush.visible() || area.width < 4 || area.height < 4 { |
| 225 | return; |
| 226 | } |
| 227 | let count = self.density.for_area(area).fish(); |
| 228 | let span = WEDGE[count - 1].1 + 4; |
| 229 | let travel = u128::from(area.width) + u128::from(span); |
| 230 | let cycle_ms = travel * SCHOOL_CELL_MS; |
| 231 | let t = self.elapsed.as_millis(); |
| 232 | let clock = t.saturating_add(cycle_ms / 2); |
| 233 | let right = swims_right(clock / cycle_ms); |
| 234 | let step = ((clock % cycle_ms) * travel * 2 / cycle_ms) as i32; |
| 235 | let mut marks = Vec::with_capacity(count); |
| 236 | for (member, &(dy, behind)) in WEDGE.iter().take(count).enumerate() { |
| 237 | let bob = sine_bob(t, 3_000 + jitter(member as u128 + 41) % 2_600, 1); |
| 238 | let drift = i32::from(sine_bob(t, 5_200 + jitter(member as u128 + 617) % 3_400, 2)) - 1; |
| 239 | let dot_x = if right { |
| 240 | step - i32::from(behind) * 2 - 8 + drift |
| 241 | } else { |
| 242 | i32::from(area.width) * 2 - step + i32::from(behind) * 2 - drift |
| 243 | }; |
| 244 | let (body, width, x) = if theme.ascii() { |
| 245 | let body = match (right, member == 0) { |
| 246 | (true, true) => "><o>", |
| 247 | (true, false) => "><>", |
| 248 | (false, true) => "<o><", |
| 249 | (false, false) => "<><", |
| 250 | }; |
| 251 | let width = body.len() as u16; |
| 252 | let x = if right { |
| 253 | step / 2 - i32::from(behind) - i32::from(width) |
| 254 | } else { |
| 255 | i32::from(area.width) - step / 2 + i32::from(behind) |
| 256 | }; |
| 257 | (body, width, x) |
| 258 | } else { |
| 259 | let pose = (step.div_euclid(2) as usize + member) % 4; |
| 260 | ( |
| 261 | poses::fish(right, pose, dot_x.rem_euclid(2) as usize, usize::from(bob)), |
| 262 | 4, |
| 263 | dot_x.div_euclid(2), |
| 264 | ) |
| 265 | }; |
| 266 | let y = |
| 267 | i32::from(area.height) - 3 + dy + if theme.ascii() { i32::from(bob) } else { 0 }; |
| 268 | let Some(at) = local_rect(area, x, y, width, 1) else { |
| 269 | continue; |
| 270 | }; |
| 271 | if open_water(at, buf, protected) { |
| 272 | let glow = FISH_GLOW.0 |
| 273 | + (FISH_GLOW.1 - FISH_GLOW.0) * wave01(t, FISH_WAVE_MS, member as u128 * 320); |
| 274 | marks.push((at, body, glow)); |
| 275 | } |
| 276 | } |
| 277 | // All members see the host's original text, rather than mistaking a |
| 278 | // neighbouring member of their wedge for a word. |
| 279 | for (at, body, glow) in marks { |
| 280 | self.brush |
| 281 | .put(buf, at, body.chars(), theme, Role::Hint, glow); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | impl Paint for FishSchool { |
| 287 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 288 | self.paint_except(area, buf, theme, &[]); |
| 289 | } |
| 290 | fn height(&self, _width: u16, _theme: &Theme) -> u16 { |
| 291 | 6 |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | const SHOAL_CELL_MS: u128 = 1_150; |
| 296 | const SHOAL_GLOW: (f32, f32) = (0.16, 0.34); |
| 297 | |
| 298 | /// A distant shoal of minnows crossing mid-water against the school, slower |
| 299 | /// and dimmer. Native braille only; ASCII themes leave it out. |
| 300 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 301 | pub struct Shoal { |
| 302 | pub elapsed: Duration, |
| 303 | pub motion: MotionMode, |
| 304 | pub density: HabitatDensity, |
| 305 | brush: Brush, |
| 306 | } |
| 307 | |
| 308 | impl Shoal { |
| 309 | #[must_use] |
| 310 | pub const fn new(elapsed: Duration, motion: MotionMode) -> Self { |
| 311 | Self { |
| 312 | elapsed, |
| 313 | motion, |
| 314 | density: HabitatDensity::Auto, |
| 315 | brush: Brush { |
| 316 | ink: None, |
| 317 | presence: 1_000, |
| 318 | }, |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | #[must_use] |
| 323 | pub const fn density(mut self, density: HabitatDensity) -> Self { |
| 324 | self.density = density; |
| 325 | self |
| 326 | } |
| 327 | |
| 328 | brush_builders!(); |
| 329 | } |
| 330 | |
| 331 | impl Shoal { |
| 332 | fn paint_except(&self, area: Rect, buf: &mut Buffer, theme: &Theme, protected: &[Rect]) { |
| 333 | let area = area.intersection(buf.area); |
| 334 | let count = self.density.for_area(area).minnows(); |
| 335 | if !self.motion.animates() |
| 336 | || !self.brush.visible() |
| 337 | || theme.ascii() |
| 338 | || count == 0 |
| 339 | || area.width < 3 |
| 340 | || area.height < 6 |
| 341 | { |
| 342 | return; |
| 343 | } |
| 344 | let t = self.elapsed.as_millis(); |
| 345 | let span = (count as u16).saturating_mul(5).saturating_add(3); |
| 346 | let travel = u128::from(area.width.saturating_add(span).max(1)); |
| 347 | let cycle_ms = travel * SHOAL_CELL_MS; |
| 348 | let clock = t.saturating_add(cycle_ms / 3); |
| 349 | let right = !swims_right((clock / cycle_ms).wrapping_add(3)); |
| 350 | let step = ((clock % cycle_ms) * travel * 2 / cycle_ms) as i32; |
| 351 | let band = i32::from(area.height.saturating_mul(3) / 5) * 2; |
| 352 | let mut marks = Vec::with_capacity(count); |
| 353 | for member in 0..count { |
| 354 | let lag = member as i32 * 10 + (jitter(member as u128 + 211) % 4) as i32; |
| 355 | let dot_x = if right { |
| 356 | step - lag - 6 |
| 357 | } else { |
| 358 | i32::from(area.width) * 2 - step + lag |
| 359 | }; |
| 360 | let bob = i32::from(sine_bob(t, 4_300 + jitter(member as u128 + 97) % 2_900, 1)); |
| 361 | let half_row = band + [0, 3, 1, 4, 2][member % 5] - 2 + bob; |
| 362 | let Some(at) = local_rect(area, dot_x.div_euclid(2), half_row.div_euclid(2), 3, 1) |
| 363 | else { |
| 364 | continue; |
| 365 | }; |
| 366 | if open_water(at, buf, protected) { |
| 367 | let pose = (step.div_euclid(2) as usize + member) % 2; |
| 368 | let body = poses::minnow( |
| 369 | right, |
| 370 | pose, |
| 371 | dot_x.rem_euclid(2) as usize, |
| 372 | half_row.rem_euclid(2) as usize, |
| 373 | ); |
| 374 | let glow = SHOAL_GLOW.0 |
| 375 | + (SHOAL_GLOW.1 - SHOAL_GLOW.0) * wave01(t, 3_100, member as u128 * 530); |
| 376 | marks.push((at, body, glow)); |
| 377 | } |
| 378 | } |
| 379 | for (at, body, glow) in marks { |
| 380 | self.brush |
| 381 | .put(buf, at, body.chars(), theme, Role::Dim, glow); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | impl Paint for Shoal { |
| 387 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 388 | self.paint_except(area, buf, theme, &[]); |
| 389 | } |
| 390 | fn height(&self, _width: u16, _theme: &Theme) -> u16 { |
| 391 | 8 |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | const JELLY_ROW_MS: u128 = 9_400; |
| 396 | const JELLY_VISIT_ROWS: u128 = 6; |
| 397 | const JELLY_CYCLE_ROWS: u128 = 20; |
| 398 | const JELLY_PULSE_MS: u128 = 5_200; |
| 399 | const JELLY_ARM_LAG_MS: u128 = 620; |
| 400 | const JELLY_GLOW: (f32, f32) = (0.18, 0.44); |
| 401 | |
| 402 | /// One complete bell with two lagging arms. The standalone component is |
| 403 | /// always present under Full motion. `visitor(true)` gives it the Engine's |
| 404 | /// slow six-row rise and long absence between visits, suitable for a habitat. |
| 405 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 406 | pub struct Jellyfish { |
| 407 | pub elapsed: Duration, |
| 408 | pub motion: MotionMode, |
| 409 | pub visitor: bool, |
| 410 | /// Keep a visitor's rise cycling during the Engine's long absence. |
| 411 | /// Collision checks remain authoritative even for a held visit. |
| 412 | pub hold_visit: bool, |
| 413 | brush: Brush, |
| 414 | } |
| 415 | |
| 416 | impl Jellyfish { |
| 417 | #[must_use] |
| 418 | pub const fn new(elapsed: Duration, motion: MotionMode) -> Self { |
| 419 | Self { |
| 420 | elapsed, |
| 421 | motion, |
| 422 | visitor: false, |
| 423 | hold_visit: false, |
| 424 | brush: Brush { |
| 425 | ink: None, |
| 426 | presence: 1_000, |
| 427 | }, |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | #[must_use] |
| 432 | pub const fn visitor(mut self, visitor: bool) -> Self { |
| 433 | self.visitor = visitor; |
| 434 | self |
| 435 | } |
| 436 | |
| 437 | #[must_use] |
| 438 | pub const fn hold_visit(mut self, hold: bool) -> Self { |
| 439 | self.hold_visit = hold; |
| 440 | self |
| 441 | } |
| 442 | |
| 443 | brush_builders!(); |
| 444 | } |
| 445 | |
| 446 | impl Jellyfish { |
| 447 | fn paint_except(&self, area: Rect, buf: &mut Buffer, theme: &Theme, protected: &[Rect]) { |
| 448 | let area = area.intersection(buf.area); |
| 449 | if !self.motion.animates() || !self.brush.visible() || area.width < 7 || area.height < 5 { |
| 450 | return; |
| 451 | } |
| 452 | let t = self.elapsed.as_millis(); |
| 453 | let compact = theme.ascii() && area.width < 56; |
| 454 | let width = if compact { 3 } else { 5 }; |
| 455 | let wobble = sine_bob(t, 8_300, 2); |
| 456 | let (x, y, dy) = if self.visitor { |
| 457 | if area.height < 9 { |
| 458 | return; |
| 459 | } |
| 460 | let cycle = t.saturating_add(3_100) % (JELLY_ROW_MS * JELLY_CYCLE_ROWS); |
| 461 | if !self.hold_visit && cycle >= JELLY_ROW_MS * JELLY_VISIT_ROWS { |
| 462 | return; |
| 463 | } |
| 464 | let cycle = cycle % (JELLY_ROW_MS * JELLY_VISIT_ROWS); |
| 465 | let dots = i32::from(area.height) * 4 - 32 - (cycle * 4 / JELLY_ROW_MS) as i32; |
| 466 | let x = (u32::from(area.width) * 5 / 6) as u16; |
| 467 | ( |
| 468 | i32::from(x.min(area.width - width - 1)), |
| 469 | dots.div_euclid(4), |
| 470 | dots.rem_euclid(4) as usize, |
| 471 | ) |
| 472 | } else { |
| 473 | ( |
| 474 | i32::from((area.width - width) / 2), |
| 475 | i32::from((area.height - 3) / 2), |
| 476 | usize::from(sine_bob(t, 9_400, 3)), |
| 477 | ) |
| 478 | }; |
| 479 | let Some(target) = local_rect(area, x, y, width, 3) else { |
| 480 | return; |
| 481 | }; |
| 482 | if target.x == area.x |
| 483 | || target.y == area.y |
| 484 | || target.right() == area.right() |
| 485 | || target.bottom() == area.bottom() |
| 486 | || !open_water(target, buf, protected) |
| 487 | { |
| 488 | return; |
| 489 | } |
| 490 | let bell = JELLY_GLOW.0 + (JELLY_GLOW.1 - JELLY_GLOW.0) * wave01(t, JELLY_PULSE_MS, 0); |
| 491 | let arms = JELLY_GLOW.0 |
| 492 | + (JELLY_GLOW.1 - JELLY_GLOW.0) |
| 493 | * wave01(t.saturating_sub(JELLY_ARM_LAG_MS), JELLY_PULSE_MS, 0); |
| 494 | if self.brush.amount(bell) == 0.0 || self.brush.amount(arms) == 0.0 { |
| 495 | return; |
| 496 | } |
| 497 | let rows: [&str; 3] = if theme.ascii() { |
| 498 | let pulse = ((t % JELLY_PULSE_MS) * 2 / JELLY_PULSE_MS) as usize; |
| 499 | let sway = ((t.saturating_add(3_100) / 1_100) % 4) as usize; |
| 500 | let tops = if compact { |
| 501 | [".-.", "'-'"] |
| 502 | } else { |
| 503 | [".-~-.", ".'-'."] |
| 504 | }; |
| 505 | let skirts = if compact { |
| 506 | ["\\_/", "(_)"] |
| 507 | } else { |
| 508 | ["\\___/", "(___)"] |
| 509 | }; |
| 510 | let trails = if compact { |
| 511 | ["| |", "/ \\", "| /", "\\ |"] |
| 512 | } else { |
| 513 | [" | | ", " / \\ ", " | / ", " \\ | "] |
| 514 | }; |
| 515 | [tops[pulse], skirts[pulse], trails[sway]] |
| 516 | } else { |
| 517 | let pose = ((t % JELLY_PULSE_MS) * 8 / JELLY_PULSE_MS) as usize * 2; |
| 518 | let rows = poses::jelly(pose, usize::from(wobble % 2), dy); |
| 519 | [&rows[0], &rows[1], &rows[2]] |
| 520 | }; |
| 521 | for (row, body) in rows.into_iter().enumerate() { |
| 522 | let at = Rect { |
| 523 | y: target.y + row as u16, |
| 524 | height: 1, |
| 525 | ..target |
| 526 | }; |
| 527 | let (role, glow) = if row == 0 { |
| 528 | (Role::Hint, bell) |
| 529 | } else { |
| 530 | (Role::Dim, arms) |
| 531 | }; |
| 532 | self.brush.put(buf, at, body.chars(), theme, role, glow); |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | impl Paint for Jellyfish { |
| 538 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 539 | self.paint_except(area, buf, theme, &[]); |
| 540 | } |
| 541 | fn height(&self, _width: u16, _theme: &Theme) -> u16 { |
| 542 | 7 |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | const BUBBLE_RISE_ROWS: u128 = 5; |
| 547 | const BUBBLE_RISE_MS: u128 = 5_200; |
| 548 | const BUBBLE_GLOW: (f32, f32) = (0.34, 0.72); |
| 549 | |
| 550 | /// One, two or three irregular bubble streams. Their short five-row ascent |
| 551 | /// stays near the floor, fades as it rises and gives way to prose and other |
| 552 | /// marine silhouettes. |
| 553 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 554 | pub struct BubbleField { |
| 555 | pub elapsed: Duration, |
| 556 | pub motion: MotionMode, |
| 557 | pub density: HabitatDensity, |
| 558 | brush: Brush, |
| 559 | } |
| 560 | |
| 561 | impl BubbleField { |
| 562 | #[must_use] |
| 563 | pub const fn new(elapsed: Duration, motion: MotionMode) -> Self { |
| 564 | Self { |
| 565 | elapsed, |
| 566 | motion, |
| 567 | density: HabitatDensity::Auto, |
| 568 | brush: Brush { |
| 569 | ink: None, |
| 570 | presence: 1_000, |
| 571 | }, |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | #[must_use] |
| 576 | pub const fn density(mut self, density: HabitatDensity) -> Self { |
| 577 | self.density = density; |
| 578 | self |
| 579 | } |
| 580 | |
| 581 | brush_builders!(); |
| 582 | } |
| 583 | |
| 584 | impl BubbleField { |
| 585 | fn paint_except(&self, area: Rect, buf: &mut Buffer, theme: &Theme, protected: &[Rect]) { |
| 586 | let area = area.intersection(buf.area); |
| 587 | if !self.motion.animates() || !self.brush.visible() || area.width < 3 || area.height < 3 { |
| 588 | return; |
| 589 | } |
| 590 | let t = self.elapsed.as_millis(); |
| 591 | for stream in 0..self.density.for_area(area).bubbles() { |
| 592 | let seed = stream as u128; |
| 593 | let phase = jitter(seed) % 9_000; |
| 594 | let period = BUBBLE_RISE_MS + jitter(seed + 313) % 2_600; |
| 595 | let rise = (t.saturating_add(phase) % period) * BUBBLE_RISE_ROWS / period; |
| 596 | let lane = (jitter(seed + 977) % u128::from(area.width)) as i32; |
| 597 | let drift = i32::from(sine_bob(t.saturating_add(phase), 2_100, 2)) - 1; |
| 598 | let x = (lane + drift).clamp(0, i32::from(area.width) - 1); |
| 599 | let y = (i32::from(area.height) - 2 - rise as i32).max(0); |
| 600 | let Some(at) = local_rect(area, x, y, 1, 1) else { |
| 601 | continue; |
| 602 | }; |
| 603 | if !open_water(at, buf, protected) { |
| 604 | continue; |
| 605 | } |
| 606 | let glyph = match (theme.ascii(), rise) { |
| 607 | (true, 0..=1) => ".", |
| 608 | (true, 2..=3) => "o", |
| 609 | (true, _) => "O", |
| 610 | (false, 0..=1) => "·", |
| 611 | (false, 2..=3) => "˚", |
| 612 | (false, _) => "°", |
| 613 | }; |
| 614 | let fade = 1.0 - 0.75 * rise as f32 / BUBBLE_RISE_ROWS as f32; |
| 615 | let glow = glint(t, 2_600 + phase % 700, 600, BUBBLE_GLOW.0, phase) * BUBBLE_GLOW.1; |
| 616 | self.brush |
| 617 | .put(buf, at, glyph.chars(), theme, Role::Dim, glow * fade); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | impl Paint for BubbleField { |
| 623 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 624 | self.paint_except(area, buf, theme, &[]); |
| 625 | } |
| 626 | fn height(&self, _width: u16, _theme: &Theme) -> u16 { |
| 627 | 8 |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | const SNOW_DOT_MS: u128 = 1_700; |
| 632 | const SNOW_GLOW: (f32, f32) = (0.10, 0.30); |
| 633 | |
| 634 | /// Marine snow: single braille dots drifting down and sideways through open |
| 635 | /// water, one dot step at a time. Native braille only. |
| 636 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 637 | pub struct MarineSnow { |
| 638 | pub elapsed: Duration, |
| 639 | pub motion: MotionMode, |
| 640 | pub density: HabitatDensity, |
| 641 | brush: Brush, |
| 642 | } |
| 643 | |
| 644 | impl MarineSnow { |
| 645 | #[must_use] |
| 646 | pub const fn new(elapsed: Duration, motion: MotionMode) -> Self { |
| 647 | Self { |
| 648 | elapsed, |
| 649 | motion, |
| 650 | density: HabitatDensity::Auto, |
| 651 | brush: Brush { |
| 652 | ink: None, |
| 653 | presence: 1_000, |
| 654 | }, |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | #[must_use] |
| 659 | pub const fn density(mut self, density: HabitatDensity) -> Self { |
| 660 | self.density = density; |
| 661 | self |
| 662 | } |
| 663 | |
| 664 | brush_builders!(); |
| 665 | } |
| 666 | |
| 667 | impl MarineSnow { |
| 668 | fn paint_except(&self, area: Rect, buf: &mut Buffer, theme: &Theme, protected: &[Rect]) { |
| 669 | let area = area.intersection(buf.area); |
| 670 | if !self.motion.animates() || !self.brush.visible() || theme.ascii() || area.is_empty() { |
| 671 | return; |
| 672 | } |
| 673 | let t = self.elapsed.as_millis(); |
| 674 | let width_dots = i64::from(area.width) * 2; |
| 675 | let span = u128::from(area.height) * 4; |
| 676 | for mote in 0..self.density.for_area(area).motes() { |
| 677 | let seed = mote as u128; |
| 678 | let fall_ms = SNOW_DOT_MS + jitter(seed + 401) % 900; |
| 679 | let y_dots = (t.saturating_add(jitter(seed + 19) % (span * fall_ms)) / fall_ms) % span; |
| 680 | let drift_ms = 7_000 + jitter(seed + 59) % 5_000; |
| 681 | let sway_ms = 5_600.0 + (jitter(seed + 83) % 3_100) as f64; |
| 682 | let sway = ((t as f64 / sway_ms) * std::f64::consts::TAU).sin() * 1.4; |
| 683 | let base = (jitter(seed + 733) % width_dots as u128) as i64; |
| 684 | let x_dots = |
| 685 | (base + (t / drift_ms) as i64 + sway.round() as i64).rem_euclid(width_dots); |
| 686 | let Some(at) = local_rect(area, (x_dots / 2) as i32, (y_dots / 4) as i32, 1, 1) else { |
| 687 | continue; |
| 688 | }; |
| 689 | if !open_water(at, buf, protected) { |
| 690 | continue; |
| 691 | } |
| 692 | let glow = SNOW_GLOW.0 |
| 693 | + (SNOW_GLOW.1 - SNOW_GLOW.0) |
| 694 | * wave01(t, 6_700 + jitter(seed + 7) % 4_000, seed * 911); |
| 695 | let glyph = poses::mote((x_dots % 2) as usize, (y_dots % 4) as usize); |
| 696 | self.brush |
| 697 | .put(buf, at, glyph.chars(), theme, Role::Dim, glow); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | impl Paint for MarineSnow { |
| 703 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 704 | self.paint_except(area, buf, theme, &[]); |
| 705 | } |
| 706 | fn height(&self, _width: u16, _theme: &Theme) -> u16 { |
| 707 | 8 |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | const WHALE_CELL_MS: u128 = 900; |
| 712 | const WHALE_CYCLE_MS: u128 = 210_000; |
| 713 | const WHALE_ABSENCE_MS: u128 = 60_000; |
| 714 | const WHALE_FIRST_MS: u128 = 12_000; |
| 715 | const WHALE_COLS: i32 = 10; |
| 716 | const WHALE_FLOOR_GAP: u16 = 8; |
| 717 | const WHALE_FLUKES: [usize; 6] = [0, 1, 3, 2, 3, 1]; |
| 718 | const WHALE_GLOW: (f32, f32) = (0.16, 0.28); |
| 719 | |
| 720 | /// A distant whale that glides across the deep water every few minutes, |
| 721 | /// first about twelve seconds in, sliding in and out at the edges. Native |
| 722 | /// braille only. `hold_visit(true)` keeps it crossing for a preview. |
| 723 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 724 | pub struct DistantWhale { |
| 725 | pub elapsed: Duration, |
| 726 | pub motion: MotionMode, |
| 727 | pub hold_visit: bool, |
| 728 | brush: Brush, |
| 729 | } |
| 730 | |
| 731 | impl DistantWhale { |
| 732 | #[must_use] |
| 733 | pub const fn new(elapsed: Duration, motion: MotionMode) -> Self { |
| 734 | Self { |
| 735 | elapsed, |
| 736 | motion, |
| 737 | hold_visit: false, |
| 738 | brush: Brush { |
| 739 | ink: None, |
| 740 | presence: 1_000, |
| 741 | }, |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | #[must_use] |
| 746 | pub const fn hold_visit(mut self, hold: bool) -> Self { |
| 747 | self.hold_visit = hold; |
| 748 | self |
| 749 | } |
| 750 | |
| 751 | brush_builders!(); |
| 752 | |
| 753 | /// Whether the whale is crossing `area` at this moment, before any |
| 754 | /// collision check. A habitat keeps an unheld jellyfish away while the |
| 755 | /// whale is painted. |
| 756 | #[must_use] |
| 757 | pub fn visiting(&self, area: Rect, theme: &Theme) -> bool { |
| 758 | self.crossing(area, theme).is_some() |
| 759 | } |
| 760 | |
| 761 | fn glow_at(&self) -> f32 { |
| 762 | WHALE_GLOW.0 + (WHALE_GLOW.1 - WHALE_GLOW.0) * wave01(self.elapsed.as_millis(), 8_200, 0) |
| 763 | } |
| 764 | |
| 765 | fn crossing(&self, area: Rect, theme: &Theme) -> Option<(bool, i32)> { |
| 766 | if !self.motion.animates() |
| 767 | || theme.ascii() |
| 768 | || area.width == 0 |
| 769 | || area.height < WHALE_FLOOR_GAP + 2 |
| 770 | || self.brush.amount(self.glow_at()) == 0.0 |
| 771 | { |
| 772 | return None; |
| 773 | } |
| 774 | let t = self.elapsed.as_millis(); |
| 775 | let crossing_ms = (u128::from(area.width) + WHALE_COLS as u128 + 2) * WHALE_CELL_MS; |
| 776 | let cycle = WHALE_CYCLE_MS.max(crossing_ms.saturating_add(WHALE_ABSENCE_MS)); |
| 777 | let clock = t.saturating_add(cycle - WHALE_FIRST_MS.min(cycle)); |
| 778 | let period = if self.hold_visit { crossing_ms } else { cycle }; |
| 779 | let pos = clock % period; |
| 780 | if pos >= crossing_ms { |
| 781 | return None; |
| 782 | } |
| 783 | let right = swims_right((clock / period).wrapping_add(7)); |
| 784 | let step = (pos * 2 / WHALE_CELL_MS) as i32; |
| 785 | let dot_x = if right { |
| 786 | step - WHALE_COLS * 2 |
| 787 | } else { |
| 788 | i32::from(area.width) * 2 - step |
| 789 | }; |
| 790 | Some((right, dot_x)) |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | impl DistantWhale { |
| 795 | fn paint_except( |
| 796 | &self, |
| 797 | area: Rect, |
| 798 | buf: &mut Buffer, |
| 799 | theme: &Theme, |
| 800 | protected: &[Rect], |
| 801 | ) -> bool { |
| 802 | let area = area.intersection(buf.area); |
| 803 | let Some((right, dot_x)) = self.crossing(area, theme) else { |
| 804 | return false; |
| 805 | }; |
| 806 | let t = self.elapsed.as_millis(); |
| 807 | let dot_y = i32::from(area.height - WHALE_FLOOR_GAP) * 4 + i32::from(sine_bob(t, 6_400, 3)); |
| 808 | let pose = WHALE_FLUKES[((t / 1_100) % WHALE_FLUKES.len() as u128) as usize]; |
| 809 | let rows = poses::whale( |
| 810 | right, |
| 811 | pose, |
| 812 | dot_x.rem_euclid(2) as usize, |
| 813 | dot_y.rem_euclid(4) as usize, |
| 814 | ); |
| 815 | let left = dot_x.div_euclid(2); |
| 816 | let skip = usize::try_from(-left).unwrap_or(0); |
| 817 | let x = left.max(0); |
| 818 | let take = ((i32::from(area.width) - x).clamp(0, WHALE_COLS) as usize) |
| 819 | .min((WHALE_COLS as usize).saturating_sub(skip)); |
| 820 | let Some(target) = local_rect(area, x, dot_y.div_euclid(4), take as u16, 3) else { |
| 821 | return false; |
| 822 | }; |
| 823 | if take == 0 || !open_water(target, buf, protected) { |
| 824 | return false; |
| 825 | } |
| 826 | let glow = self.glow_at(); |
| 827 | let mut painted = false; |
| 828 | for (row, glyph) in rows.iter().enumerate() { |
| 829 | let at = Rect { |
| 830 | y: target.y + row as u16, |
| 831 | height: 1, |
| 832 | ..target |
| 833 | }; |
| 834 | painted |= self |
| 835 | .brush |
| 836 | .put(buf, at, glyph.chars().skip(skip), theme, Role::Dim, glow); |
| 837 | } |
| 838 | painted |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | impl Paint for DistantWhale { |
| 843 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 844 | self.paint_except(area, buf, theme, &[]); |
| 845 | } |
| 846 | fn height(&self, _width: u16, _theme: &Theme) -> u16 { |
| 847 | 10 |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | /// The whole ocean, painted over the host's existing content. The distant |
| 852 | /// whale is placed first and keeps the jellyfish away while it crosses; the |
| 853 | /// school owns the floor band, the shoal mid-water, and bubbles and marine |
| 854 | /// snow take whatever open water remains. |
| 855 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 856 | pub struct Habitat { |
| 857 | pub elapsed: Duration, |
| 858 | pub motion: MotionMode, |
| 859 | pub density: HabitatDensity, |
| 860 | pub fish: bool, |
| 861 | pub shoal: bool, |
| 862 | pub jellyfish: bool, |
| 863 | pub bubbles: bool, |
| 864 | pub snow: bool, |
| 865 | pub whale: bool, |
| 866 | pub hold_jellyfish_visit: bool, |
| 867 | /// Host-owned overlays and other areas that must remain undisturbed, |
| 868 | /// including their blank cells. Coordinates are absolute buffer positions. |
| 869 | pub protected: Vec<Rect>, |
| 870 | brush: Brush, |
| 871 | } |
| 872 | |
| 873 | impl Habitat { |
| 874 | #[must_use] |
| 875 | pub const fn new(elapsed: Duration, motion: MotionMode) -> Self { |
| 876 | Self { |
| 877 | elapsed, |
| 878 | motion, |
| 879 | density: HabitatDensity::Auto, |
| 880 | fish: true, |
| 881 | shoal: true, |
| 882 | jellyfish: true, |
| 883 | bubbles: true, |
| 884 | snow: true, |
| 885 | whale: true, |
| 886 | hold_jellyfish_visit: false, |
| 887 | protected: Vec::new(), |
| 888 | brush: Brush { |
| 889 | ink: None, |
| 890 | presence: 1_000, |
| 891 | }, |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | #[must_use] |
| 896 | pub const fn density(mut self, density: HabitatDensity) -> Self { |
| 897 | self.density = density; |
| 898 | self |
| 899 | } |
| 900 | #[must_use] |
| 901 | pub const fn fish(mut self, show: bool) -> Self { |
| 902 | self.fish = show; |
| 903 | self |
| 904 | } |
| 905 | #[must_use] |
| 906 | pub const fn shoal(mut self, show: bool) -> Self { |
| 907 | self.shoal = show; |
| 908 | self |
| 909 | } |
| 910 | #[must_use] |
| 911 | pub const fn jellyfish(mut self, show: bool) -> Self { |
| 912 | self.jellyfish = show; |
| 913 | self |
| 914 | } |
| 915 | #[must_use] |
| 916 | pub const fn bubbles(mut self, show: bool) -> Self { |
| 917 | self.bubbles = show; |
| 918 | self |
| 919 | } |
| 920 | #[must_use] |
| 921 | pub const fn snow(mut self, show: bool) -> Self { |
| 922 | self.snow = show; |
| 923 | self |
| 924 | } |
| 925 | #[must_use] |
| 926 | pub const fn whale(mut self, show: bool) -> Self { |
| 927 | self.whale = show; |
| 928 | self |
| 929 | } |
| 930 | |
| 931 | brush_builders!(); |
| 932 | |
| 933 | /// Protect filled blank overlays, focus surfaces or other host-owned |
| 934 | /// regions. A creature whose clearance touches a region is withheld as a |
| 935 | /// complete silhouette. |
| 936 | #[must_use] |
| 937 | pub fn protected(mut self, areas: impl Into<Vec<Rect>>) -> Self { |
| 938 | self.protected = areas.into(); |
| 939 | self |
| 940 | } |
| 941 | |
| 942 | #[must_use] |
| 943 | pub fn exclude_rect(mut self, area: Rect) -> Self { |
| 944 | self.protected.push(area); |
| 945 | self |
| 946 | } |
| 947 | |
| 948 | /// Keep the corner visitor present for a preview or dedicated marine |
| 949 | /// surface, even while the distant whale crosses. False (the default) |
| 950 | /// retains its long absence between visits. Full-silhouette collision |
| 951 | /// checks still give all existing text priority. |
| 952 | #[must_use] |
| 953 | pub const fn hold_jellyfish_visit(mut self, hold: bool) -> Self { |
| 954 | self.hold_jellyfish_visit = hold; |
| 955 | self |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | impl Paint for Habitat { |
| 960 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 961 | let (t, motion, density, brush) = (self.elapsed, self.motion, self.density, self.brush); |
| 962 | let protected = &self.protected; |
| 963 | let whale_painted = self.whale |
| 964 | && DistantWhale { |
| 965 | brush, |
| 966 | ..DistantWhale::new(t, motion) |
| 967 | } |
| 968 | .paint_except(area, buf, theme, protected); |
| 969 | if self.jellyfish && (self.hold_jellyfish_visit || !whale_painted) { |
| 970 | Jellyfish { |
| 971 | brush, |
| 972 | ..Jellyfish::new(t, motion) |
| 973 | .visitor(true) |
| 974 | .hold_visit(self.hold_jellyfish_visit) |
| 975 | } |
| 976 | .paint_except(area, buf, theme, protected); |
| 977 | } |
| 978 | if self.fish { |
| 979 | FishSchool { |
| 980 | brush, |
| 981 | ..FishSchool::new(t, motion).density(density) |
| 982 | } |
| 983 | .paint_except(area, buf, theme, protected); |
| 984 | } |
| 985 | if self.shoal { |
| 986 | Shoal { |
| 987 | brush, |
| 988 | ..Shoal::new(t, motion).density(density) |
| 989 | } |
| 990 | .paint_except(area, buf, theme, protected); |
| 991 | } |
| 992 | if self.bubbles { |
| 993 | BubbleField { |
| 994 | brush, |
| 995 | ..BubbleField::new(t, motion).density(density) |
| 996 | } |
| 997 | .paint_except(area, buf, theme, protected); |
| 998 | } |
| 999 | if self.snow { |
| 1000 | MarineSnow { |
| 1001 | brush, |
| 1002 | ..MarineSnow::new(t, motion).density(density) |
| 1003 | } |
| 1004 | .paint_except(area, buf, theme, protected); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | fn height(&self, _width: u16, _theme: &Theme) -> u16 { |
| 1009 | 16 |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | // Signed local coordinates make off-screen travel and saturated absolute |
| 1014 | // buffer edges ordinary clipping, rather than u16 wraparound. |
| 1015 | fn local_rect(area: Rect, x: i32, y: i32, width: u16, height: u16) -> Option<Rect> { |
| 1016 | if x < 0 |
| 1017 | || y < 0 |
| 1018 | || x + i32::from(width) > i32::from(area.width) |
| 1019 | || y + i32::from(height) > i32::from(area.height) |
| 1020 | { |
| 1021 | return None; |
| 1022 | } |
| 1023 | Some(Rect::new( |
| 1024 | area.x + x as u16, |
| 1025 | area.y + y as u16, |
| 1026 | width, |
| 1027 | height, |
| 1028 | )) |
| 1029 | } |
| 1030 | |
| 1031 | fn open_water(at: Rect, buf: &Buffer, protected: &[Rect]) -> bool { |
| 1032 | let x0 = at.x.saturating_sub(CLEAR_COLS).max(buf.area.x); |
| 1033 | let y0 = at.y.saturating_sub(CLEAR_ROWS).max(buf.area.y); |
| 1034 | let x1 = at.right().saturating_add(CLEAR_COLS).min(buf.area.right()); |
| 1035 | let y1 = at |
| 1036 | .bottom() |
| 1037 | .saturating_add(CLEAR_ROWS) |
| 1038 | .min(buf.area.bottom()); |
| 1039 | let clearance = Rect::new(x0, y0, x1 - x0, y1 - y0); |
| 1040 | if protected |
| 1041 | .iter() |
| 1042 | .any(|r| !clearance.intersection(*r).is_empty()) |
| 1043 | { |
| 1044 | return false; |
| 1045 | } |
| 1046 | (y0..y1).all(|y| { |
| 1047 | if x0 > buf.area.x && crate::text::width(buf[(x0 - 1, y)].symbol()) > 1 { |
| 1048 | return false; |
| 1049 | } |
| 1050 | (x0..x1).all(|x| { |
| 1051 | let cell = &buf[(x, y)]; |
| 1052 | cell.diff_option != CellDiffOption::Skip |
| 1053 | && cell.symbol().chars().all(char::is_whitespace) |
| 1054 | }) |
| 1055 | }) |
| 1056 | } |
| 1057 | |
| 1058 | fn swims_right(cycle: u128) -> bool { |
| 1059 | (cycle.wrapping_mul(0x9E37_79B9_7F4A_7C15) >> 7) & 1 == 0 |
| 1060 | } |
| 1061 | |
| 1062 | // Engine's deterministic phase spread; no RNG and no animation state. |
| 1063 | fn jitter(seed: u128) -> u128 { |
| 1064 | let mut hash = 0xcbf2_9ce4_8422_2325u64; |
| 1065 | for byte in seed.to_le_bytes() { |
| 1066 | hash ^= u64::from(byte); |
| 1067 | hash = hash.wrapping_mul(0x0000_0100_0000_01b3); |
| 1068 | } |
| 1069 | u128::from(hash) |
| 1070 | } |
| 1071 | |
| 1072 | fn sine_bob(elapsed: u128, period: u128, amplitude: u16) -> u16 { |
| 1073 | let fraction = (elapsed % period) as f64 / period as f64; |
| 1074 | (((fraction * std::f64::consts::TAU).sin() + 1.0) * 0.5 * f64::from(amplitude)).round() as u16 |
| 1075 | } |
| 1076 | |
| 1077 | fn wave01(elapsed: u128, period: u128, phase: u128) -> f32 { |
| 1078 | let fraction = (elapsed.saturating_add(phase) % period) as f64 / period as f64; |
| 1079 | let s = (fraction * std::f64::consts::PI).sin(); |
| 1080 | (s * s) as f32 |
| 1081 | } |
| 1082 | |
| 1083 | fn glint(elapsed: u128, period: u128, width: u128, floor: f32, phase: u128) -> f32 { |
| 1084 | let pos = elapsed.saturating_add(phase) % period; |
| 1085 | if pos >= width { |
| 1086 | return floor; |
| 1087 | } |
| 1088 | let bump = 0.5 * (1.0 - (pos as f64 / width as f64 * std::f64::consts::TAU).cos()); |
| 1089 | floor + (1.0 - floor) * bump as f32 |
| 1090 | } |
| 1091 |