libshimmer 0.1.0

Animated status lines for terminal UIs, paced by the host's work
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! Tetromino: a four-by-four Tetris well in two octant cells, played by a bot as quanta arrive.

use std::sync::LazyLock;

use crate::kit::AZURE;
use crate::kit::CORAL;
use crate::kit::CYAN;
use crate::kit::GOLD;
use crate::kit::LIME;
use crate::kit::OCTANTS;
use crate::kit::STEEL;
use crate::kit::SUN;
use crate::kit::TANGERINE;
use crate::kit::VIOLET;
use crate::kit::WHITE;
use crate::kit::ease;
use crate::kit::frac;
use crate::kit::hash;

use super::OPUS;
use crate::paint::{Ink, Palette, Rgb};
use crate::recipe::{Glyphs, Recipe};
use crate::stage::{Canvas, Shader, Stage};

pub(super) const RECIPE: Recipe = Recipe {
    name: "Tetromino",
    blurb: "a bot plays a tiny Tetris well",
    credit: OPUS,
    needs: Glyphs::OCTANTS,
    wide: true,
    summon: |_| Box::<Tetromino>::default(),
};

/// The well is four cells square; `Board` holds rows top down.
const SIDE: i8 = 4;
type Board = [Option<Piece>; 16];
type Cells = [(i8, i8); 4];
/// Quanta per row of fall; quanta beyond three pieces' worth are forgotten.
const FALL: u32 = 10;
const BACKLOG: u32 = 30;
/// Seconds a cleared line flashes before it goes, a feat glows, and a top-out flashes.
const FLASH: f64 = 0.2;
const FEAT: f64 = 1.6;
const TOP_OUT: f64 = 0.6;
/// Seconds the caption takes to crossfade to the last piece's tint.
const CROSSFADE: f64 = 0.4;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Piece {
    I,
    O,
    T,
    S,
    Z,
    J,
    L,
}

const PIECES: [Piece; 7] = [
    Piece::I,
    Piece::O,
    Piece::T,
    Piece::S,
    Piece::Z,
    Piece::J,
    Piece::L,
];

impl Piece {
    fn cells(self) -> Cells {
        match self {
            Piece::I => [(0, 0), (1, 0), (2, 0), (3, 0)],
            Piece::O => [(0, 0), (1, 0), (0, 1), (1, 1)],
            Piece::T => [(0, 0), (1, 0), (2, 0), (1, 1)],
            Piece::S => [(1, 0), (2, 0), (0, 1), (1, 1)],
            Piece::Z => [(0, 0), (1, 0), (1, 1), (2, 1)],
            Piece::J => [(0, 0), (0, 1), (1, 1), (2, 1)],
            Piece::L => [(2, 0), (0, 1), (1, 1), (2, 1)],
        }
    }

    fn color(self) -> Rgb {
        match self {
            Piece::I => CYAN,
            Piece::O => SUN,
            Piece::T => VIOLET,
            Piece::S => LIME,
            Piece::Z => CORAL,
            Piece::J => AZURE,
            Piece::L => TANGERINE,
        }
    }

    /// The piece's distinct rotations, each normalized to the top left and sorted row by row.
    fn orientations(self) -> &'static [Cells] {
        static ORIENTATIONS: LazyLock<Vec<Vec<Cells>>> = LazyLock::new(|| {
            PIECES
                .iter()
                .map(|piece| {
                    let mut found: Vec<Cells> = Vec::new();
                    let mut cells = piece.cells();
                    for _ in 0..4 {
                        let (x0, y0) = (
                            cells.iter().map(|c| c.0).min().unwrap_or_default(),
                            cells.iter().map(|c| c.1).min().unwrap_or_default(),
                        );
                        let mut normal = cells.map(|(x, y)| (x - x0, y - y0));
                        normal.sort_by_key(|&(x, y)| (y, x));
                        if !found.contains(&normal) {
                            found.push(normal);
                        }
                        cells = cells.map(|(x, y)| (y, -x));
                    }
                    found
                })
                .collect()
        });
        &ORIENTATIONS[self as usize]
    }

    /// The piece standing up in one octant cell, for the next-piece preview.
    fn upright(self) -> char {
        let Some(cells) = self
            .orientations()
            .iter()
            .find(|cells| cells.iter().all(|c| c.0 < 2))
        else {
            return '•';
        };
        let y0 = (3 - cells.iter().map(|c| c.1).max().unwrap_or_default()) >> 1;
        OCTANTS[cells
            .iter()
            .map(|&(x, y)| 1 << (2 * (y + y0) + x))
            .sum::<usize>()]
    }
}

/// The piece `i`th in play: shuffled seven-bags.
#[expect(
    clippy::cast_possible_wrap,
    reason = "The bag number is i / 7, below i64::MAX, and piece indices are below seven."
)]
fn bag(i: u64) -> Piece {
    let mut bag: Vec<(f64, Piece)> = PIECES
        .iter()
        .enumerate()
        .map(|(j, &piece)| (hash(77 + 7 * (i / 7) as i64 + j as i64), piece))
        .collect();
    bag.sort_by(|a, b| a.0.total_cmp(&b.0));
    bag[(i % 7) as usize].1
}

#[expect(
    clippy::cast_sign_loss,
    reason = "Short-circuit bounds checks establish 0 <= px, py < SIDE before indexing the 16-cell board."
)]
fn fits(board: &Board, cells: Cells, x: i8, y: i8) -> bool {
    cells.iter().all(|&(cx, cy)| {
        let (px, py) = (cx + x, cy + y);
        (0..SIDE).contains(&px)
            && py < SIDE
            && (py < 0 || board[(SIDE * py + px) as usize].is_none())
    })
}

/// The board after locking a piece, full rows removed, and how many there were.
#[expect(
    clippy::cast_sign_loss,
    reason = "Only fully in-board placements established by fits and the caller's top-out check are locked."
)]
fn settle(board: &Board, cells: Cells, x: i8, y: i8, piece: Piece) -> (Board, usize) {
    let mut locked = *board;
    for (cx, cy) in cells {
        locked[(SIDE * (cy + y) + cx + x) as usize] = Some(piece);
    }
    let kept: Vec<&[Option<Piece>]> = locked
        .chunks(4)
        .filter(|row| row.iter().any(Option::is_none))
        .collect();
    let lines = 4 - kept.len();
    let mut settled = [None; 16];
    for (row, cells) in kept.iter().enumerate() {
        settled[4 * (lines + row)..4 * (lines + row + 1)].copy_from_slice(cells);
    }
    (settled, lines)
}

struct Placement {
    orientation: usize,
    cells: Cells,
    x: i8,
    height: i8,
    board: Board,
    score: f64,
}

/// Every rotation and column for `piece`, with the board each leaves and its worth.
#[expect(
    clippy::cast_possible_wrap,
    clippy::cast_precision_loss,
    reason = "Counts and the heuristic score come from a 4×4 board: at most 16 cells, four rows, and twelve adjacent height differences."
)]
fn placements(board: &Board, piece: Piece) -> Vec<Placement> {
    let mut all = Vec::new();
    for (orientation, &cells) in piece.orientations().iter().enumerate() {
        let width = 1 + cells.iter().map(|c| c.0).max().unwrap_or_default();
        let height = 1 + cells.iter().map(|c| c.1).max().unwrap_or_default();
        for x in 0..=SIDE - width {
            let mut y = -height;
            while fits(board, cells, x, y + 1) {
                y += 1;
            }
            if cells.iter().any(|c| c.1 + y < 0) {
                continue;
            }
            let (settled, lines) = settle(board, cells, x, y, piece);
            let tops: [usize; 4] = std::array::from_fn(|column| {
                (0..4)
                    .find(|row| settled[4 * row + column].is_some())
                    .map_or(0, |row| 4 - row)
            });
            let holes = (0..4)
                .map(|column| {
                    (0..4)
                        .filter(|row| settled[4 * row + column].is_none() && 4 - row < tops[column])
                        .count()
                })
                .sum::<usize>();
            let bumps = tops
                .windows(2)
                .map(|pair| pair[0].abs_diff(pair[1]))
                .sum::<usize>();
            let score = 3 * lines as i64
                - 4 * holes as i64
                - tops.iter().sum::<usize>() as i64
                - bumps as i64;
            all.push(Placement {
                orientation,
                cells,
                x,
                height,
                board: settled,
                score: score as f64,
            });
        }
    }
    all
}

/// The best total worth reachable by placing the next `depth` pieces from the `i`th.
fn outlook(board: &Board, i: u64, depth: u32) -> f64 {
    if depth == 0 {
        return 0.0;
    }
    placements(board, bag(i))
        .iter()
        .map(|p| p.score + outlook(&p.board, i + 1, depth - 1))
        .fold(-1e3, f64::max)
}

/// The bot: where the `i`th piece goes, looking three pieces ahead. With nowhere to go, it drops the
/// piece at the left and tops out.
#[expect(
    clippy::cast_possible_wrap,
    reason = "Drawn pieces consume at least FALL = 10 quanta each, so the count fits i64; orientation indices are below four."
)]
fn aim(board: &Board, i: u64) -> (Cells, i8, i8) {
    placements(board, bag(i))
        .into_iter()
        .map(|p| {
            let worth = p.score
                + outlook(&p.board, i + 1, /*depth*/ 3)
                + 0.01 * hash(64 * i as i64 + 4 * i64::from(p.x) + p.orientation as i64);
            (worth, p)
        })
        .reduce(|best, next| if next.0 > best.0 { next } else { best })
        .map_or_else(
            || {
                let cells = bag(i).orientations()[0];
                (
                    cells,
                    0,
                    1 + cells.iter().map(|c| c.1).max().unwrap_or_default(),
                )
            },
            |(_, p)| (p.cells, p.x, p.height),
        )
}

#[derive(Clone, Copy)]
struct Falling {
    piece: Piece,
    cells: Cells,
    x: i8,
    y: i8,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Feat {
    Double,
    Tetris,
    TopOut,
}

#[derive(Default)]
struct Tetromino {
    board: Board,
    falling: Option<Falling>,
    /// Pieces drawn from the bags so far.
    drawn: u64,
    /// Quanta owed to gravity.
    owed: u32,
    /// Full rows flashing before they go: when they filled, and the board without them.
    clearing: Option<(f64, Board)>,
    /// The last two pieces locked, newest first, and when the newest locked.
    locked: (Option<Piece>, Option<Piece>),
    locked_at: f64,
    feat: Option<(Feat, f64)>,
}

impl Tetromino {
    /// One row of fall: spawn, drop or lock the piece.
    #[expect(
        clippy::cast_sign_loss,
        reason = "The failed downward fits check locks the prior in-board placement only after every cell passes the top-out check."
    )]
    fn gravity(&mut self, t: f64) {
        let Some(falling) = self.falling else {
            let (cells, x, height) = aim(&self.board, self.drawn);
            self.falling = Some(Falling {
                piece: bag(self.drawn),
                cells,
                x,
                y: -height,
            });
            self.drawn += 1;
            return;
        };
        let Falling { piece, cells, x, y } = falling;
        if fits(&self.board, cells, x, y + 1) {
            self.falling = Some(Falling {
                y: y + 1,
                ..falling
            });
            return;
        }
        self.falling = None;
        if cells.iter().any(|c| c.1 + y < 0) {
            self.board = [None; 16];
            self.feat = Some((Feat::TopOut, t));
            return;
        }
        let (settled, lines) = settle(&self.board, cells, x, y, piece);
        self.locked = (Some(piece), self.locked.0);
        self.locked_at = t;
        if lines == 0 {
            self.board = settled;
            return;
        }
        // Full rows flash before they go.
        for &(cx, cy) in &cells {
            self.board[(SIDE * (cy + y) + cx + x) as usize] = Some(piece);
        }
        self.clearing = Some((t, settled));
        if lines > 1 {
            self.feat = Some((
                if lines == 4 {
                    Feat::Tetris
                } else {
                    Feat::Double
                },
                t,
            ));
        }
    }

    fn feat(&self, t: f64) -> Option<(Feat, f64)> {
        self.feat
            .filter(|&(feat, at)| t - at < if feat == Feat::TopOut { TOP_OUT } else { FEAT })
    }
}

impl Shader for Tetromino {
    fn tick(&mut self, stage: &Stage) {
        self.owed = (self.owed + stage.fresh()).min(BACKLOG);
        if let Some((at, settled)) = self.clearing
            && stage.t - at >= FLASH
        {
            self.board = settled;
            self.clearing = None;
        }
        while stage.busy() && self.clearing.is_none() && self.owed >= FALL {
            self.owed -= FALL;
            self.gravity(stage.t);
        }
    }

    #[expect(
        clippy::cast_sign_loss,
        reason = "The falling placement is horizontally in bounds and visible cells pass the nonnegative row check before board indexing."
    )]
    fn status(&self, stage: &Stage, palette: Palette, canvas: &mut Canvas) {
        let t = stage.t;
        let flashing = |row: usize| {
            self.clearing.is_some() && self.board[4 * row..4 * row + 4].iter().all(Option::is_some)
        };
        let mut pixels: [Option<Rgb>; 16] = std::array::from_fn(|j| {
            self.board[j].map(|piece| {
                if flashing(j / 4) {
                    WHITE
                } else {
                    piece.color()
                }
            })
        });
        let mut own = [false; 16];
        if let Some(falling) = &self.falling
            && (stage.busy() || frac(t) < 0.5)
        {
            for &(cx, cy) in &falling.cells {
                if cy + falling.y >= 0 {
                    let j = (SIDE * (cy + falling.y) + cx + falling.x) as usize;
                    pixels[j] = Some(falling.piece.color());
                    own[j] = true;
                }
            }
        }
        let feat = self.feat(t);
        let well = STEEL.blend(palette.bg, /*alpha*/ 0.12);
        let well = match feat {
            Some((Feat::Tetris, at)) => GOLD.blend(well, 0.35 * (1.0 - (t - at) / FEAT)),
            _ => well,
        };
        canvas.mark = (0..2)
            .map(|half| {
                if matches!(feat, Some((Feat::TopOut, _))) {
                    return Ink::of('█').fg(CORAL.blend(palette.bg, /*alpha*/ 0.6));
                }
                let (mut bits, mut fg) = (0, None);
                for y in 0..4 {
                    for x in 0..2 {
                        let j = 4 * y + 2 * half + x;
                        if let Some(color) = pixels[j] {
                            bits |= 1 << (2 * y + x);
                            if fg.is_none() || own[j] {
                                fg = Some(color);
                            }
                        }
                    }
                }
                Ink::of(OCTANTS[bits]).fg(fg.unwrap_or(palette.fg)).bg(well)
            })
            .collect();
        let tint = |piece: Option<Piece>| {
            piece.map_or(palette.fg, |piece| {
                piece.color().blend(palette.fg, /*alpha*/ 0.3)
            })
        };
        let fade = ease(((t - self.locked_at) / CROSSFADE).clamp(0.0, 1.0));
        let tone = if stage.busy() {
            tint(self.locked.0).blend(tint(self.locked.1), fade)
        } else {
            palette.grey(/*alpha*/ 0.6)
        };
        canvas.letters = stage.letters(|_, _| Ink::default().fg(tone));
    }

    fn bullet(&self, stage: &Stage, palette: Palette) -> Ink {
        let next = bag(self.drawn);
        let color = if stage.busy() {
            next.color()
        } else {
            next.color().blend(palette.bg, /*alpha*/ 0.6)
        };
        Ink::of(next.upright()).fg(color)
    }
}