返回 CodeWhale
habitat_poses.rs
根目录 / vendor / codewhale-ratatui / src / components / habitat_poses.rs
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 poses for the habitat: fish, minnow, jellyfish, whale and
29 //! a single-dot mote. The ASCII fallback keeps fish, jellyfish and bubbles.
30 //! These are authored dot poses, not another simulation. The habitat owns
31 //! travel, collision and lifetime from caller-supplied elapsed time. Five tiny
32 //! immutable tables retain their rows so a frame borrows glyphs without
33 //! allocating strings. Braille remains discrete: half-column / quarter-row
34 //! motion is not pixels.
35
36 use crate::whale::BITS as BRAILLE_BITS;
37 use std::sync::LazyLock;
38
39 // Fish: 2 directions × 4 tail poses × 2 horizontal × 2 vertical dot offsets.
40 static FISH: LazyLock<Vec<String>> = LazyLock::new(|| {
41 (0..32)
42 .map(|index| {
43 let dy = index % 2;
44 let dx = index / 2 % 2;
45 let pose = index / 4 % 4;
46 let right = index / 16 != 0;
47 let mut dots = Vec::from([(2, 1), (3, 0), (4, 0), (5, 1), (6, 1), (3, 2), (4, 2)]);
48 // A small forked tail opens, folds and opens; no blinking eye or flash.
49 dots.extend(match pose {
50 0 => [(0, 0), (0, 2), (1, 1)],
51 1 => [(0, 0), (1, 1), (1, 2)],
52 2 => [(1, 0), (0, 1), (1, 2)],
53 _ => [(1, 0), (1, 1), (0, 2)],
54 });
55 if !right {
56 for point in &mut dots {
57 point.0 = 6 - point.0;
58 }
59 }
60 raster(&dots, 4, 1, dx, dy).remove(0)
61 })
62 .collect()
63 });
64
65 // Jelly: 16 bell/tentacle poses × 2 horizontal × 4 vertical dot offsets.
66 // Eight-by-eight source dots plus offsets fit the existing 5×3-cell habitat.
67 static JELLY: LazyLock<Vec<[String; 3]>> = LazyLock::new(|| {
68 (0..128)
69 .map(|index| {
70 let dy = index % 4;
71 let dx = index / 4 % 2;
72 let phase = (index / 8) as f64 / 16.0 * std::f64::consts::TAU;
73 let contracted = (1.0 - phase.cos()) * 0.5;
74 let inset = usize::from(contracted > 0.55);
75 let mut dots = Vec::with_capacity(24);
76 // An open contour, never a solid luminous block.
77 dots.extend([
78 (2, 0),
79 (3, 0),
80 (4, 0),
81 (5, 0),
82 (1 + inset, 1),
83 (6 - inset, 1),
84 ]);
85 for x in inset..8 - inset {
86 dots.push((x, 2));
87 }
88 for y in 3..8 {
89 // Wave travels down both arms after the bell; columns are offset.
90 let lag = (y - 2) as f64 * 0.45 + 0.75;
91 let left = (2.0 + (phase - lag).sin() * 0.9).round() as usize;
92 let right = (5.0 + (phase - lag - 0.8).sin() * 0.9).round() as usize;
93 dots.extend([(left, y), (right, y)]);
94 }
95 let mut rows = raster(&dots, 5, 3, dx, dy).into_iter();
96 std::array::from_fn(|_| rows.next().expect("three jelly rows"))
97 })
98 .collect()
99 });
100
101 static MINNOW: LazyLock<Vec<String>> = LazyLock::new(|| {
102 (0..16)
103 .map(|index| {
104 let dy = index % 2;
105 let dx = index / 2 % 2;
106 let pose = index / 4 % 2;
107 let right = index / 8 != 0;
108 let mut dots = Vec::from([(2, 1), (3, 1), (4, 1), (3, 0), (3, 2)]);
109 dots.extend(if pose == 0 {
110 [(0, 0), (1, 1), (0, 2)]
111 } else {
112 [(1, 0), (0, 1), (1, 2)]
113 });
114 if !right {
115 for point in &mut dots {
116 point.0 = 4 - point.0;
117 }
118 }
119 raster(&dots, 3, 1, dx, dy).remove(0)
120 })
121 .collect()
122 });
123
124 static WHALE: LazyLock<Vec<[String; 3]>> = LazyLock::new(|| {
125 (0..64)
126 .map(|index| {
127 let dy = index % 4;
128 let dx = index / 4 % 2;
129 let pose = index / 8 % 4;
130 let right = index / 32 != 0;
131 let mut dots = Vec::from([
132 (8, 1),
133 (9, 1),
134 (10, 1),
135 (11, 1),
136 (12, 1),
137 (6, 2),
138 (7, 2),
139 (13, 2),
140 (14, 2),
141 (5, 3),
142 (15, 3),
143 (16, 4),
144 (17, 5),
145 (4, 4),
146 (16, 6),
147 (5, 6),
148 (6, 6),
149 (7, 6),
150 (8, 6),
151 (9, 6),
152 (10, 6),
153 (11, 6),
154 (12, 6),
155 (13, 6),
156 (14, 6),
157 (15, 6),
158 (10, 7),
159 (11, 7),
160 (3, 4),
161 (2, 4),
162 ]);
163 dots.extend(match pose {
164 0 => [(1, 3), (0, 2), (1, 2), (0, 1)],
165 1 => [(1, 4), (0, 3), (1, 3), (0, 4)],
166 2 => [(1, 5), (0, 5), (1, 6), (0, 6)],
167 _ => [(1, 4), (0, 4), (1, 5), (0, 5)],
168 });
169 if !right {
170 for point in &mut dots {
171 point.0 = 17 - point.0;
172 }
173 }
174 let mut rows = raster(&dots, 10, 3, dx, dy).into_iter();
175 std::array::from_fn(|_| rows.next().expect("three whale rows"))
176 })
177 .collect()
178 });
179
180 static MOTE: LazyLock<Vec<String>> = LazyLock::new(|| {
181 (0..8)
182 .map(|index| raster(&[(index % 2, index / 2)], 1, 1, 0, 0).remove(0))
183 .collect()
184 });
185
186 fn raster(
187 dots: &[(usize, usize)],
188 width: usize,
189 height: usize,
190 dx: usize,
191 dy: usize,
192 ) -> Vec<String> {
193 let mut cells = vec![0u8; width * height];
194 for &(x, y) in dots {
195 let (x, y) = (x + dx, y + dy);
196 assert!(
197 x < width * 2 && y < height * 4,
198 "native pose escaped its habitat"
199 );
200 cells[y / 4 * width + x / 2] |= BRAILLE_BITS[y % 4][x % 2];
201 }
202 cells
203 .chunks_exact(width)
204 .map(|row| {
205 row.iter()
206 .map(|bits| {
207 if *bits == 0 {
208 ' '
209 } else {
210 char::from_u32(0x2800 + u32::from(*bits)).expect("braille dot mask")
211 }
212 })
213 .collect()
214 })
215 .collect()
216 }
217
218 pub(super) fn fish(right: bool, pose: usize, dx: usize, dy: usize) -> &'static str {
219 &FISH[usize::from(right) * 16 + pose % 4 * 4 + dx % 2 * 2 + dy % 2]
220 }
221
222 pub(super) fn jelly(pose: usize, dx: usize, dy: usize) -> &'static [String; 3] {
223 &JELLY[pose % 16 * 8 + dx % 2 * 4 + dy % 4]
224 }
225
226 pub(super) fn minnow(right: bool, pose: usize, dx: usize, dy: usize) -> &'static str {
227 &MINNOW[usize::from(right) * 8 + pose % 2 * 4 + dx % 2 * 2 + dy % 2]
228 }
229
230 pub(super) fn whale(right: bool, pose: usize, dx: usize, dy: usize) -> &'static [String; 3] {
231 &WHALE[usize::from(right) * 32 + pose % 4 * 8 + dx % 2 * 4 + dy % 4]
232 }
233
234 pub(super) fn mote(dx: usize, dy: usize) -> &'static str {
235 &MOTE[dy % 4 * 2 + dx % 2]
236 }
237
237 lines RUST