返回 CodeWhale
pet_cameo.rs
根目录 / crates / tui / src / tui / ambient_life / pet_cameo.rs
1 //! Replaces the `≈≈>` cameo with the shared dot-whale widget. The existing
2 //! ocean completion clock is the only trigger. This is an authored completion
3 //! gesture, not an event-v1 bucketer or an estimate of hidden agent activity.
4
5 use super::pet_sim::{ChannelId, PetSim, PetState};
6 use super::pet_widget::PetWidget;
7 use super::{
8 AmbientActivity, AmbientFrameStats, WhaleCameo, is_open_water, occupied_text_bounds, ocean,
9 };
10 use ratatui::{buffer::Buffer, layout::Rect, style::Color, text::Line, widgets::Widget};
11 use std::sync::LazyLock;
12
13 const WIDTH: u16 = 18;
14 const HEIGHT: u16 = 6;
15 pub(super) const DURATION_MS: u128 = 2_400;
16 const FPS: u128 = 30;
17 const FRAMES: usize = (DURATION_MS * FPS / 1_000) as usize;
18
19 struct CameoFrame {
20 buffer: Buffer,
21 marks: u32,
22 }
23
24 // A bounded raster cache, not mutable simulation history. Sampling backwards,
25 // skipping draws, or rendering another session cannot change a frame. Two
26 // fixed 72-frame tapes; no RNG beyond the core's existing particle stream.
27 static SOLO: LazyLock<Vec<CameoFrame>> = LazyLock::new(|| frames(ChannelId::Other));
28 static POD: LazyLock<Vec<CameoFrame>> = LazyLock::new(|| frames(ChannelId::Agent));
29
30 fn frames(channel: ChannelId) -> Vec<CameoFrame> {
31 let mut sim = PetSim::whale();
32 // A completion is observed, but it does not assert a model/tool category.
33 // The pod has three persistent slots here; it is not three more swarms.
34 let state = PetState {
35 channel,
36 activity: 0.12,
37 coherence: 0.95,
38 ..PetState::rest()
39 };
40 (0..FRAMES)
41 .map(|_| {
42 sim.step(1.0 / FPS as f64, &state, true, 1.0);
43 let mut buffer = Buffer::empty(Rect::new(0, 0, WIDTH, HEIGHT));
44 PetWidget {
45 sim: &sim,
46 state: &state,
47 }
48 .render(buffer.area, &mut buffer);
49 let marks = buffer.content.iter().filter(|c| c.symbol() != " ").count() as u32;
50 CameoFrame { buffer, marks }
51 })
52 .collect()
53 }
54
55 #[allow(clippy::too_many_arguments)]
56 pub(super) fn paint(
57 area: Rect,
58 buf: &mut Buffer,
59 ink: Color,
60 lines: &[Line<'_>],
61 presence: f32,
62 whale: WhaleCameo,
63 activity: AmbientActivity,
64 stats: &mut AmbientFrameStats,
65 ) {
66 let Some(age) = whale.elapsed_ms.filter(|age| *age < DURATION_MS) else {
67 return;
68 };
69 if presence <= 0.0 {
70 return;
71 }
72 let transcript_has_text = lines
73 .iter()
74 .any(|line| occupied_text_bounds(line).is_some());
75 let pod = activity == AmbientActivity::Subagents;
76 let tape = if pod { &*POD } else { &*SOLO };
77 let slots: &[i32] = if pod { &[-1, 0, 1] } else { &[0] };
78 for (index, slot) in slots.iter().enumerate() {
79 let age = age + index as u128 * 240;
80 if age >= DURATION_MS {
81 continue;
82 }
83 let frame = &tape[(age * FPS / 1_000) as usize];
84 let progress = age as f64 / DURATION_MS as f64;
85 let x = i32::from(whale.anchor_x) - i32::from(area.x) - i32::from(WIDTH / 2)
86 + slot * i32::from(WIDTH)
87 + (progress * 3.0).round() as i32;
88 let y = i32::from(whale.anchor_y) - i32::from(area.y) - i32::from(HEIGHT / 2);
89 stats.marks_built += frame.marks;
90 // Do not clamp off-screen pod members into one overlapping creature.
91 if x < 0
92 || y < 0
93 || x + i32::from(WIDTH) > i32::from(area.width)
94 || y + i32::from(HEIGHT) > i32::from(area.height)
95 {
96 stats.marks_clipped += frame.marks;
97 continue;
98 }
99 let (x, y) = (x as u16, y as u16);
100 let marks = || {
101 frame
102 .buffer
103 .content
104 .iter()
105 .enumerate()
106 .filter(|(_, c)| c.symbol() != " ")
107 };
108 // Atomically withhold body and label, and keep the cameo out of the
109 // transcript column entirely while any transcript text is on screen.
110 if transcript_has_text
111 || marks()
112 .any(|(i, _)| !is_open_water(lines, x + i as u16 % WIDTH, y + i as u16 / WIDTH, 1))
113 {
114 stats.marks_skipped_text += frame.marks;
115 continue;
116 }
117 let glow = (0.36 + 0.24 * (progress * std::f64::consts::PI).sin()) as f32;
118 for (i, source) in marks() {
119 let point = (area.x + x + i as u16 % WIDTH, area.y + y + i as u16 / WIDTH);
120 let Some(cell) = buf.cell_mut(point) else {
121 stats.marks_clipped += 1;
122 continue;
123 };
124 // The existing theme owns all habitat ink. No category colour,
125 // especially Failure red, is introduced into completion chrome.
126 // Ambient ink is already calibrated against the ocean. Applying
127 // the standalone canvas alpha again would dim it twice.
128 let alpha = (glow * presence).clamp(0.0, 1.0);
129 let fg = ocean::mix_colors(cell.bg, ink, alpha);
130 cell.set_symbol(source.symbol()).set_fg(fg);
131 stats.marks_painted += 1;
132 stats.cells_written += 1;
133 }
134 }
135 }
136
136 lines RUST