| 1 | /* |
| 2 | Adapted from Hmbown/CodeWhale crates/tui/src/tui/ambient_life/native_poses.rs |
| 3 | at a79ce5c4d5ed1a5f7032185710c27343a900351c. Authored native dot poses. |
| 4 | |
| 5 | MIT License |
| 6 | |
| 7 | Copyright (c) 2024-2025 DeepSeek-TUI Contributors |
| 8 | |
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | of this software and associated documentation files (the "Software"), to deal |
| 11 | in the Software without restriction, including without limitation the rights |
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | copies of the Software, and to permit persons to whom the Software is |
| 14 | furnished to do so, subject to the following conditions: |
| 15 | |
| 16 | The above copyright notice and this permission notice shall be included in all |
| 17 | copies or substantial portions of the Software. |
| 18 | |
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | //! Bounded native replacements for the ASCII fish and jelly silhouettes. |
| 29 | //! These are authored dot poses, not another simulation. The habitat owns |
| 30 | //! travel, collision and lifetime from caller-supplied elapsed time. Two tiny immutable tables |
| 31 | //! retain their rows so a frame borrows glyphs without allocating strings. |
| 32 | //! Braille remains discrete: half-column / quarter-row motion is not pixels. |
| 33 | |
| 34 | use crate::whale::BITS as BRAILLE_BITS; |
| 35 | use std::sync::LazyLock; |
| 36 | |
| 37 | // Fish: 2 directions × 4 tail poses × 2 horizontal × 2 vertical dot offsets. |
| 38 | static FISH: LazyLock<Vec<String>> = LazyLock::new(|| { |
| 39 | (0..32) |
| 40 | .map(|index| { |
| 41 | let dy = index % 2; |
| 42 | let dx = index / 2 % 2; |
| 43 | let pose = index / 4 % 4; |
| 44 | let right = index / 16 != 0; |
| 45 | let mut dots = Vec::from([(2, 1), (3, 0), (4, 0), (5, 1), (6, 1), (3, 2), (4, 2)]); |
| 46 | // A small forked tail opens, folds and opens; no blinking eye or flash. |
| 47 | dots.extend(match pose { |
| 48 | 0 => [(0, 0), (0, 2), (1, 1)], |
| 49 | 1 => [(0, 0), (1, 1), (1, 2)], |
| 50 | 2 => [(1, 0), (0, 1), (1, 2)], |
| 51 | _ => [(1, 0), (1, 1), (0, 2)], |
| 52 | }); |
| 53 | if !right { |
| 54 | for point in &mut dots { |
| 55 | point.0 = 6 - point.0; |
| 56 | } |
| 57 | } |
| 58 | raster(&dots, 4, 1, dx, dy).remove(0) |
| 59 | }) |
| 60 | .collect() |
| 61 | }); |
| 62 | |
| 63 | // Jelly: 16 bell/tentacle poses × 2 horizontal × 4 vertical dot offsets. |
| 64 | // Eight-by-eight source dots plus offsets fit the existing 5×3-cell habitat. |
| 65 | static JELLY: LazyLock<Vec<[String; 3]>> = LazyLock::new(|| { |
| 66 | (0..128) |
| 67 | .map(|index| { |
| 68 | let dy = index % 4; |
| 69 | let dx = index / 4 % 2; |
| 70 | let phase = (index / 8) as f64 / 16.0 * std::f64::consts::TAU; |
| 71 | let contracted = (1.0 - phase.cos()) * 0.5; |
| 72 | let inset = usize::from(contracted > 0.55); |
| 73 | let mut dots = Vec::with_capacity(24); |
| 74 | // An open contour, never a solid luminous block. |
| 75 | dots.extend([ |
| 76 | (2, 0), |
| 77 | (3, 0), |
| 78 | (4, 0), |
| 79 | (5, 0), |
| 80 | (1 + inset, 1), |
| 81 | (6 - inset, 1), |
| 82 | ]); |
| 83 | for x in inset..8 - inset { |
| 84 | dots.push((x, 2)); |
| 85 | } |
| 86 | for y in 3..8 { |
| 87 | // Wave travels down both arms after the bell; columns are offset. |
| 88 | let lag = (y - 2) as f64 * 0.45 + 0.75; |
| 89 | let left = (2.0 + (phase - lag).sin() * 0.9).round() as usize; |
| 90 | let right = (5.0 + (phase - lag - 0.8).sin() * 0.9).round() as usize; |
| 91 | dots.extend([(left, y), (right, y)]); |
| 92 | } |
| 93 | let mut rows = raster(&dots, 5, 3, dx, dy).into_iter(); |
| 94 | std::array::from_fn(|_| rows.next().expect("three jelly rows")) |
| 95 | }) |
| 96 | .collect() |
| 97 | }); |
| 98 | |
| 99 | fn raster( |
| 100 | dots: &[(usize, usize)], |
| 101 | width: usize, |
| 102 | height: usize, |
| 103 | dx: usize, |
| 104 | dy: usize, |
| 105 | ) -> Vec<String> { |
| 106 | let mut cells = vec![0u8; width * height]; |
| 107 | for &(x, y) in dots { |
| 108 | let (x, y) = (x + dx, y + dy); |
| 109 | assert!( |
| 110 | x < width * 2 && y < height * 4, |
| 111 | "native pose escaped its habitat" |
| 112 | ); |
| 113 | cells[y / 4 * width + x / 2] |= BRAILLE_BITS[y % 4][x % 2]; |
| 114 | } |
| 115 | cells |
| 116 | .chunks_exact(width) |
| 117 | .map(|row| { |
| 118 | row.iter() |
| 119 | .map(|bits| { |
| 120 | if *bits == 0 { |
| 121 | ' ' |
| 122 | } else { |
| 123 | char::from_u32(0x2800 + u32::from(*bits)).expect("braille dot mask") |
| 124 | } |
| 125 | }) |
| 126 | .collect() |
| 127 | }) |
| 128 | .collect() |
| 129 | } |
| 130 | |
| 131 | pub(super) fn fish(right: bool, pose: usize, dx: usize, dy: usize) -> &'static str { |
| 132 | &FISH[usize::from(right) * 16 + pose % 4 * 4 + dx % 2 * 2 + dy % 2] |
| 133 | } |
| 134 | |
| 135 | pub(super) fn jelly(pose: usize, dx: usize, dy: usize) -> &'static [String; 3] { |
| 136 | &JELLY[pose % 16 * 8 + dx % 2 * 4 + dy % 4] |
| 137 | } |
| 138 |