pinch-points 0.9.0

A fast, kid-friendly crab-routing game: route streams of crabs into your sandcastle before time runs out
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
//! The versus sidebars: on the left, score chips sorted by rank (the
//! leader's chip is biggest and wears the crown); on the right, the big
//! tide clock over a running log of the round's notable events, with the
//! tide event still running pinned at its top.

mod clock;
mod event_timer;
mod feed;

pub use clock::update_side_clock;
pub use event_timer::update_event_timer;
pub use feed::{EventLog, collect_chat, collect_log, reach_the_board, update_log};

use crate::app::seating::{Seating, beside_the_robot, robot_inline};
use crate::app::settings::GameSettings;
use crate::app::{Seats, Sim};
use crate::app::{menu_ui, palette};
use crate::sim::MAX_PLAYERS;
use bevy::ecs::system::SystemParam;
use bevy::prelude::*;

/// Width of each sidebar in px; `fit_camera` reserves this much on both
/// sides.
pub const SIDEBAR_W: f32 = 250.0;

/// How far above the window's foot the sidebars stop, clear of the prompt
/// line.
const SIDEBAR_FOOT: f32 = 44.0;

/// The sidebar root; despawning it takes everything with it.
#[derive(Component)]
pub struct SidePanelRoot;

/// A per-seat score chip inside the sidebar. Its vertical slot follows the
/// seat's current rank, so overtakes visibly reshuffle the column.
#[derive(Component)]
pub struct SidePanel(pub u8);

/// The score number inside a chip; `bump` animates on change.
#[derive(Component)]
pub struct SideScore {
    pub seat: u8,
    bump: f32,
}

/// One arrow slot on a chip, oldest first: a faint pip while the arrow is
/// in hand, and for an arrow standing an outline, filled and drained by
/// how much of its life is gone. One per arrow the rules allow, so the
/// row is the cap, read at a glance. Not an empty outline in hand: a row
/// of hollow boxes under a name reads as characters the font cannot draw.
#[derive(Component)]
pub struct ArrowDot {
    seat: u8,
    index: u8,
}

/// The fill inside an [`ArrowDot`]: its height is the arrow's life left.
#[derive(Component)]
pub struct ArrowFill {
    seat: u8,
    index: u8,
}

/// The most arrow slots a chip draws. The dial stops at six; a level file
/// can ask for more, and past this the row would run into the score.
const MAX_DOTS: u8 = 10;

/// The crown icon shown on the current leader's chip.
#[derive(Component)]
pub struct LeaderCrown(pub u8);

/// The round rank medal on a chip's left edge (colour by rank).
#[derive(Component)]
pub struct RankMedal(pub u8);

/// The digit inside the rank medal.
#[derive(Component)]
pub struct RankDigit(pub u8);

/// Text written on seat `.0`'s chip, in [`palette::ink_on`] its fill: the
/// score at full strength, the name and its tag (`.1`) a touch under.
#[derive(Component)]
pub struct ChipInk(pub u8, pub bool);

/// A chip's fill: the seat's own colour while it leads, a darker shade
/// while it trails, either one its text reads on.
pub fn chip_fill(seat: u8, leading: bool) -> Color {
    match leading {
        true => palette::leader_chip(seat),
        false => palette::seat_chip(seat),
    }
}

/// An arrow slot's ink on a chip in `fill`: the fill of an arrow standing,
/// and the outline of a free slot, in the chip's own [`palette::ink_on`].
fn pip_on(fill: Color) -> Color {
    palette::ink_on(fill).with_alpha(0.95)
}

fn pip_off(fill: Color) -> Color {
    palette::ink_on(fill).with_alpha(0.25)
}

/// The ink text `named` (a name, else the score) is written in on `fill`.
fn chip_ink(fill: Color, named: bool) -> Color {
    match named {
        true => palette::name_ink_on(fill),
        false => palette::ink_on(fill),
    }
}

/// Chip geometry by rank: the leader gets the tall card and the huge
/// number; the rest shrink with their standing, with a gap between cards.
/// Six of them still has to fit the column above the fold, so the tail is
/// tighter than the head.
///
/// Each chip holds two lines, the name over the arrows, which sets the
/// smallest: 48 is the two of them and the padding, with nothing spare.
///
/// There was a third, three castle-tier pips, gone because they said
/// again what the number beside them says and the castle on the beach
/// shows, and nobody could tell what they were for.
const CHIP_TOPS: [f32; MAX_PLAYERS] = [10.0, 106.0, 172.0, 234.0, 294.0, 352.0];
const CHIP_HEIGHTS: [f32; MAX_PLAYERS] = [88.0, 58.0, 54.0, 52.0, 50.0, 48.0];
const SCORE_PX: [f32; MAX_PLAYERS] = [46.0, 28.0, 24.0, 21.0, 19.0, 18.0];

/// Rank medal colours: gold, silver, bronze, then driftwood for the rest.
const MEDALS: [Color; MAX_PLAYERS] = [
    palette::MEDAL_GOLD,
    palette::MEDAL_SILVER,
    palette::MEDAL_BRONZE,
    palette::MEDAL_DRIFTWOOD,
    palette::MEDAL_DRIFTWOOD,
    palette::MEDAL_DRIFTWOOD,
];
/// What a rank medal's digit reads: the place, one-based. A table, so the
/// chips repaint without formatting anything.
const PLACES: [&str; MAX_PLAYERS] = ["1", "2", "3", "4", "5", "6"];
const LEADER_BORDER: Color = palette::GOLD;
/// Where the log card starts: under the clock card, with the same gap the
/// two have always had.
const LOG_TOP: f32 = clock::CLOCK_TOP + clock::CLOCK_H + 14.0;
/// The hairline round every sidebar card, which Bevy counts inside a
/// card's height.
const CARD_BORDER: f32 = 2.0;

/// Seats ranked by score (descending, seat number breaking ties), and each
/// seat's rank.
fn ranks(scores: &[u32; MAX_PLAYERS], seats: u8) -> [usize; MAX_PLAYERS] {
    let count = usize::from(seats.max(2));
    let mut order: Vec<usize> = (0..count).collect();
    order.sort_by_key(|&seat| (std::cmp::Reverse(scores[seat]), seat));
    let mut rank = [0usize; MAX_PLAYERS];
    for (place, &seat) in order.iter().enumerate() {
        rank[seat] = place;
    }
    rank
}

/// One sidebar's frame: full height, pinned to one edge, under the header
/// and above the prompt line.
fn sidebar(left: bool) -> (SidePanelRoot, Node) {
    (
        SidePanelRoot,
        Node {
            position_type: PositionType::Absolute,
            left: if left { Val::Px(0.0) } else { Val::Auto },
            right: if left { Val::Auto } else { Val::Px(0.0) },
            top: Val::Px(menu_ui::UNDER_HEADER),
            bottom: Val::Px(SIDEBAR_FOOT),
            width: Val::Px(SIDEBAR_W),
            ..default()
        },
    )
}

/// The card language both sidebars share: the menus' gold hairline and
/// drop shadow over the in-round panel's opaque fill, so the chrome of a
/// round is the same set of cards as the screens around it, standing on
/// the same beach. The fill stays opaque: the scores are read at a glance.
fn card(top: f32, height: Option<f32>) -> (Node, BorderColor, BackgroundColor, BoxShadow) {
    (
        Node {
            position_type: PositionType::Absolute,
            top: Val::Px(top),
            bottom: if height.is_some() {
                Val::Auto
            } else {
                Val::Px(0.0)
            },
            left: Val::Px(10.0),
            right: Val::Px(10.0),
            height: height.map_or(Val::Auto, Val::Px),
            align_items: AlignItems::Center,
            justify_content: JustifyContent::Center,
            border: UiRect::all(Val::Px(CARD_BORDER)),
            border_radius: BorderRadius::all(Val::Px(12.0)),
            ..default()
        },
        BorderColor::all(palette::CARD_EDGE),
        BackgroundColor(palette::CARD_BG),
        menu_ui::card_shadow(),
    )
}

/// One seat's score chip: rank medal, name over arrow dots, the big number,
/// and a crown that only the leader shows.
fn spawn_score_chip(
    root: &mut ChildSpawnerCommands,
    art: &crate::app::art::Art,
    seat: u8,
    (name, tag): (String, &'static str),
    arrows: u8,
    bot: bool,
) {
    root.spawn((
        SidePanel(seat),
        Node {
            position_type: PositionType::Absolute,
            top: Val::Px(CHIP_TOPS[seat as usize]),
            left: Val::Px(10.0),
            right: Val::Px(10.0),
            height: Val::Px(CHIP_HEIGHTS[seat as usize]),
            align_items: AlignItems::Center,
            column_gap: Val::Px(10.0),
            padding: UiRect::axes(Val::Px(12.0), Val::Px(6.0)),
            border: UiRect::all(Val::Px(2.0)),
            border_radius: BorderRadius::all(Val::Px(12.0)),
            ..default()
        },
        BorderColor::all(palette::seat_paint(seat)),
        BackgroundColor(chip_fill(seat, false)),
    ))
    .with_children(|chip| {
        // Rank medal on the left edge.
        chip.spawn((
            RankMedal(seat),
            Node {
                width: Val::Px(26.0),
                height: Val::Px(26.0),
                flex_shrink: 0.0,
                align_items: AlignItems::Center,
                justify_content: JustifyContent::Center,
                border_radius: BorderRadius::all(Val::Px(13.0)),
                ..default()
            },
            BackgroundColor(MEDALS[seat as usize]),
        ))
        .with_children(|medal| {
            medal.spawn((
                RankDigit(seat),
                Text::new(""),
                TextFont {
                    font_size: FontSize::Px(menu_ui::type_scale::BODY),
                    ..default()
                },
                TextColor(palette::ink_on(MEDALS[seat as usize])),
            ));
        });
        let fill = chip_fill(seat, false);
        // Name, and the arrows under it.
        chip.spawn(Node {
            flex_direction: FlexDirection::Column,
            flex_grow: 1.0,
            // Free to be narrower than the name, which clips: wrapped, a
            // long name and its tag took two lines and put the arrows on
            // the chip's edge.
            min_width: Val::Px(0.0),
            row_gap: Val::Px(3.0),
            ..default()
        })
        .with_children(|mid| {
            mid.spawn(Node {
                column_gap: Val::Px(5.0),
                align_items: AlignItems::Center,
                ..default()
            })
            .with_children(|line| {
                let font = TextFont {
                    font_size: FontSize::Px(menu_ui::type_scale::BODY),
                    ..default()
                };
                line.spawn(Node {
                    min_width: Val::Px(0.0),
                    overflow: Overflow::clip_x(),
                    ..default()
                })
                .with_children(|clip| {
                    // The robot rides in the line with the name, so a name
                    // too long for the chip clips at its end and the robot
                    // at its start stays where it is read first.
                    clip.spawn((
                        Text::new(""),
                        font.clone(),
                        TextLayout::no_wrap(),
                        ChipInk(seat, true),
                        TextColor(chip_ink(fill, true)),
                    ))
                    .with_children(|text| {
                        if bot {
                            text.spawn(robot_inline(art, seat, 18.0));
                        }
                        text.spawn((
                            TextSpan::new(beside_the_robot(bot, &name)),
                            font.clone(),
                            ChipInk(seat, true),
                            TextColor(chip_ink(fill, true)),
                        ));
                    });
                });
                if !tag.is_empty() {
                    line.spawn((
                        Text::new(tag.trim_start()),
                        font,
                        TextLayout::no_wrap(),
                        ChipInk(seat, true),
                        TextColor(chip_ink(fill, true)),
                        Node {
                            flex_shrink: 0.0,
                            ..default()
                        },
                    ));
                }
            });
            mid.spawn(Node {
                column_gap: Val::Px(3.0),
                ..default()
            })
            .with_children(|dots| {
                for index in 0..arrows.min(MAX_DOTS) {
                    dots.spawn((
                        ArrowDot { seat, index },
                        Node {
                            width: Val::Px(6.0),
                            height: Val::Px(9.0),
                            border: UiRect::all(Val::Px(1.0)),
                            border_radius: BorderRadius::all(Val::Px(2.0)),
                            flex_direction: FlexDirection::Column,
                            justify_content: JustifyContent::FlexEnd,
                            overflow: Overflow::clip(),
                            ..default()
                        },
                        BorderColor::all(Color::NONE),
                        BackgroundColor(pip_off(fill)),
                    ))
                    .with_children(|dot| {
                        dot.spawn((
                            ArrowFill { seat, index },
                            Node {
                                width: Val::Percent(100.0),
                                height: Val::Percent(0.0),
                                ..default()
                            },
                            BackgroundColor(pip_on(fill)),
                        ));
                    });
                }
            });
        });
        // The big number, right-aligned.
        chip.spawn((
            SideScore { seat, bump: 0.0 },
            Text::new("0"),
            menu_ui::display_font(SCORE_PX[seat as usize]),
            ChipInk(seat, false),
            TextColor(chip_ink(fill, false)),
        ));
        // The crown perches on the card's top-right corner.
        chip.spawn((
            LeaderCrown(seat),
            Visibility::Hidden,
            ImageNode::new(art.crown.clone()),
            Node {
                position_type: PositionType::Absolute,
                top: Val::Px(-13.0),
                right: Val::Px(6.0),
                width: Val::Px(24.0),
                height: Val::Px(24.0),
                ..default()
            },
        ));
    });
}

/// The right sidebar: the big tide clock over the event feed.
fn spawn_clock_and_feed(commands: &mut Commands) {
    commands.spawn(sidebar(false)).with_children(|root| {
        clock::spawn_clock(root);
        feed::spawn_feed(root);
    });
}

/// Spawn the sidebars after `load_versus` has set the seat count.
pub fn spawn_side_panels(
    mut commands: Commands,
    seating: Seating,
    kinds: Res<crate::app::SeatKinds>,
    settings: Res<GameSettings>,
    art: Res<crate::app::art::Art>,
    sim: Res<Sim>,
    mut log: ResMut<EventLog>,
) {
    log.0.clear();
    let tr = settings.tr();
    let labels: Vec<(String, &'static str)> = (0..seating.seats.0.max(2))
        .map(|seat| seating.name_and_tag(tr, seat))
        .collect();
    // The cap is the board's, set before the round and never during it.
    let arrows = sim.0.signpost_rule().0;
    commands.spawn(sidebar(true)).with_children(|root| {
        for (seat, label) in labels.into_iter().enumerate() {
            let seat = seat as u8;
            spawn_score_chip(root, &art, seat, label, arrows, kinds.bot(seat));
        }
    });
    spawn_clock_and_feed(&mut commands);
}

/// How strongly the next-to-go marks show, `secs` into the round: the
/// pulse the arrow on the beach and its dot on the chip share, so the eye
/// ties the two together. Steady at full under reduced motion.
pub fn next_to_go_pulse(secs: f32, reduced_motion: bool) -> f32 {
    if reduced_motion {
        return 1.0;
    }
    0.55 + 0.45 * (secs * std::f32::consts::TAU * 1.2).sin().abs()
}

/// A chip's card: where it sits in the column and how it is coloured.
type ChipFrame = (
    &'static SidePanel,
    &'static mut Node,
    &'static mut BackgroundColor,
    &'static mut BorderColor,
);

/// A chip's score number and how big it is drawn.
type ChipScore = (
    &'static mut SideScore,
    &'static mut Text,
    &'static mut TextFont,
    &'static mut UiTransform,
);

/// A chip's rank digit and its ink.
type ChipDigit = (
    &'static RankDigit,
    &'static mut Text,
    &'static mut TextColor,
);

/// Every part of the score chips that follows the standings.
#[derive(SystemParam)]
pub struct Chips<'w, 's> {
    frames: Query<'w, 's, ChipFrame>,
    scores: Query<'w, 's, ChipScore>,
    crowns: Query<'w, 's, (&'static LeaderCrown, &'static mut Visibility)>,
    medals: Query<'w, 's, (&'static RankMedal, &'static mut BackgroundColor), Without<SidePanel>>,
    digits: Query<'w, 's, ChipDigit, (Without<SideScore>, Without<ChipInk>)>,
    inks: Query<'w, 's, (&'static ChipInk, &'static mut TextColor)>,
}

/// Keep the chips sorted and sized by rank, the numbers current, and the
/// crown on the leader (all writes guarded).
pub fn update_side_panels(
    scoring: crate::app::teams::Scoring,
    sim: Res<Sim>,
    seats: Res<Seats>,
    time: Res<Time>,
    mut chips: Chips,
    mut value: Local<String>,
) {
    let Chips {
        frames: panels,
        scores,
        crowns,
        medals,
        digits,
        inks,
    } = &mut chips;
    let board_scores = sim.0.scores();
    let leaders = scoring.leaders(board_scores, seats.0);
    let rank = ranks(board_scores, seats.0);
    for (panel, mut node, mut bg, mut border) in panels {
        let seat = panel.0 as usize;
        let r = rank[seat];
        let (top, height) = (Val::Px(CHIP_TOPS[r]), Val::Px(CHIP_HEIGHTS[r]));
        if node.top != top {
            node.top = top;
        }
        if node.height != height {
            node.height = height;
        }
        menu_ui::set_bg(&mut bg, chip_fill(panel.0, leaders[seat]));
        let edge = if leaders[seat] {
            LEADER_BORDER
        } else {
            palette::seat_paint(panel.0)
        };
        let edge = BorderColor::all(edge);
        border.set_if_neq(edge);
    }
    for (medal, mut bg) in medals {
        menu_ui::set_bg(&mut bg, MEDALS[rank[medal.0 as usize]]);
    }
    for (digit, mut text, mut ink) in digits {
        let r = rank[digit.0 as usize];
        menu_ui::set_text(&mut text, PLACES[r]);
        menu_ui::set_color(&mut ink, palette::ink_on(MEDALS[r]));
    }
    for (chip, mut ink) in inks {
        let fill = chip_fill(chip.0, leaders[usize::from(chip.0)]);
        menu_ui::set_color(&mut ink, chip_ink(fill, chip.1));
    }
    for (mut chip, mut text, mut font, mut transform) in scores {
        use std::fmt::Write;
        value.clear();
        let _ = write!(&mut *value, "{}", board_scores[chip.seat as usize]);
        if text.0 != *value {
            text.0.clear();
            text.0.push_str(&value);
            // Pop on change, unless the player asked for less motion, in
            // which case the number simply changes.
            chip.bump = if scoring.local.settings.reduced_motion {
                0.0
            } else {
                1.0
            };
        }
        chip.bump = (chip.bump - time.delta_secs() * 4.0).max(0.0);
        // The size follows the seat's rank, and only that: six fixed values,
        // each rasterized into the glyph atlas once and reused forever.
        let base = FontSize::Px(SCORE_PX[rank[chip.seat as usize]]);
        if font.font_size != base {
            font.font_size = base;
        }
        // The pop is scale, not size: Bevy keys its glyph atlas by font
        // size, so animating the size allocates a fresh atlas and
        // re-rasterizes the digits on every frame of the bump, which came
        // to 4.4% of the game's whole CPU.
        let pop = Vec2::splat(1.0 + 0.22 * chip.bump);
        if transform.scale != pop {
            transform.scale = pop;
        }
    }
    for (crown, mut visibility) in crowns {
        let target = if leaders[crown.0 as usize] {
            Visibility::Inherited
        } else {
            Visibility::Hidden
        };
        visibility.set_if_neq(target);
    }
}

/// The arrow slots on the chips, and the chips' fills they are inked for.
#[derive(SystemParam)]
pub struct ArrowSlots<'w, 's> {
    chips: Query<'w, 's, ChipFill, (Without<ArrowFill>, Without<ArrowDot>)>,
    dots: Query<'w, 's, SlotDot, (Without<ArrowFill>, Without<SidePanel>)>,
    fills: Query<'w, 's, SlotFill, (Without<SidePanel>, Without<ArrowDot>)>,
}

/// A chip and its fill, which its slots are inked for.
type ChipFill = (&'static SidePanel, &'static BackgroundColor);

/// An arrow slot: its outline and its in-hand pip.
type SlotDot = (
    &'static ArrowDot,
    &'static mut BorderColor,
    &'static mut BackgroundColor,
);

/// An arrow slot's fill: how full it is, and its ink.
type SlotFill = (
    &'static ArrowFill,
    &'static mut Node,
    &'static mut BackgroundColor,
);

/// Keep every chip's arrow row current: a slot filled per arrow standing,
/// oldest on the left and drained by its age, and the oldest picked out
/// in gold, pulsing with its post on the beach, while planting another
/// would cost it. That last only for the seats played at this screen.
pub fn update_arrow_dots(
    sim: Res<Sim>,
    seating: Seating,
    settings: Res<GameSettings>,
    clock: Res<crate::app::round_state::RoundClock>,
    slots: ArrowSlots,
    mut lives: Local<[Vec<f32>; MAX_PLAYERS]>,
) {
    let ArrowSlots {
        chips,
        mut dots,
        mut fills,
    } = slots;
    let board = &sim.0;
    // Each chip's fill as it is painted, which its slots are inked for.
    let mut chip = [Color::WHITE; MAX_PLAYERS];
    for (panel, fill) in &chips {
        chip[usize::from(panel.0)] = fill.0;
    }
    let mut marked = [false; MAX_PLAYERS];
    for (seat, out) in lives.iter_mut().enumerate() {
        board.signpost_lives(seat as u8, out);
        marked[seat] = seating.played_here(seat as u8) && board.next_to_go(seat as u8).is_some();
    }
    // On the clock its post on the beach pulses to (`dress_signposts`).
    let pulse = next_to_go_pulse(clock.0, settings.reduced_motion);
    // The next to go is the oldest by definition, so the first slot.
    let is_next = |seat: u8, index: u8| index == 0 && marked[usize::from(seat)];
    for (dot, mut border, mut pip) in &mut dots {
        // The outline says an arrow stands there and the fill how long it
        // has left; an arrow in hand is a faint pip with no outline. Told
        // by the fill alone, an arrow about to run out was an empty slot
        // to look at, and three standing read as one.
        let ink = chip[usize::from(dot.seat)];
        let standing = usize::from(dot.index) < lives[usize::from(dot.seat)].len();
        let edge = BorderColor::all(if is_next(dot.seat, dot.index) {
            palette::GOLD
        } else if standing {
            pip_on(ink)
        } else {
            Color::NONE
        });
        border.set_if_neq(edge);
        menu_ui::set_bg(
            &mut pip,
            match standing {
                true => Color::NONE,
                false => pip_off(ink),
            },
        );
    }
    for (fill, mut node, mut bg) in &mut fills {
        let life = lives[usize::from(fill.seat)]
            .get(usize::from(fill.index))
            .copied()
            .unwrap_or(0.0);
        let height = Val::Percent(100.0 * life);
        if node.height != height {
            node.height = height;
        }
        let ink = match is_next(fill.seat, fill.index) {
            true => palette::GOLD.with_alpha(pulse),
            false => pip_on(chip[usize::from(fill.seat)]),
        };
        menu_ui::set_bg(&mut bg, ink);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Everything a score chip writes is drawn by the fonts it is written
    /// in, in every language: the tag after a name, the medal's digit and
    /// the score. The rows' face draws what DejaVu carries and the
    /// Japanese subset the rest; the score is the display face. Its arrow
    /// slots are drawn, not written, so no glyph there can go missing.
    #[test]
    fn every_character_a_chip_writes_is_in_its_font() {
        use crate::app::i18n::metrics::{characters_in_font, display_covers};
        let mut rows =
            characters_in_font(include_bytes!("../../../assets/fonts/DejaVuSansMono.ttf"));
        rows.extend(characters_in_font(include_bytes!(
            "../../../assets/fonts/NotoSansMonoCJKjp-Subset.otf"
        )));
        let display = display_covers();
        for lang in crate::app::i18n::ALL_LANGS {
            let tr = lang.tr();
            let mut written: String = [tr.tag_you, tr.tag_ai].concat();
            written.push_str(&crate::app::seat_label(tr, 5));
            written.push_str("0123456789");
            for ch in written.chars().filter(|ch| !ch.is_whitespace()) {
                assert!(rows.contains(&ch), "{lang:?}: {ch:?} has no glyph");
            }
        }
        for ch in "0123456789".chars() {
            assert!(display.contains(&ch), "{ch:?}");
        }
    }

    /// Every rank's medal digit reads on its disc, the driftwood of the
    /// lower places as well as the gold.
    #[test]
    fn every_medal_digit_reads() {
        for (rank, disc) in MEDALS.iter().enumerate() {
            let seen = palette::contrast(palette::ink_on(*disc), *disc);
            assert!(seen >= palette::TEXT_CONTRAST, "rank {rank}: {seen:.2}");
        }
    }

    /// The score chip pops by *scale*, never by font size.
    ///
    /// Animating the size instead re-rasterizes the digits every frame of
    /// the bump. The size here follows rank alone, six fixed values.
    #[test]
    fn the_score_pop_is_scale_and_never_a_new_font_size() {
        let mut app = App::new();
        app.init_resource::<Time>();
        app.init_resource::<crate::app::net::Online>();
        app.init_resource::<crate::app::Playback>();
        app.init_resource::<Seats>();
        app.insert_resource(GameSettings::default());
        app.init_resource::<crate::app::Daily>();
        app.insert_resource(Sim(crate::sim::Board::new(4, 4, 0)));
        app.world_mut().resource_mut::<Seats>().0 = 2;
        app.add_systems(Update, update_side_panels);
        let chip = app
            .world_mut()
            .spawn((
                SideScore { seat: 0, bump: 0.0 },
                Text::new("0"),
                TextFont {
                    font_size: FontSize::Px(SCORE_PX[0]),
                    ..default()
                },
                UiTransform::IDENTITY,
            ))
            .id();

        // A bank puts the chip mid-pop.
        app.world_mut().resource_mut::<Sim>().0.set_score(0, 7);
        app.update();
        let scale_at_peak = app.world().get::<UiTransform>(chip).expect("chip").scale;
        let size_at_peak = app.world().get::<TextFont>(chip).expect("chip").font_size;
        assert!(
            scale_at_peak.x > 1.0,
            "the pop should be scale, got {scale_at_peak:?}"
        );
        assert!(
            app.world().get::<SideScore>(chip).expect("chip").bump > 0.0,
            "the bump should be running"
        );

        // Several frames later it is settling, and the font size has not
        // moved once: one atlas entry for the whole animation.
        for _ in 0..5 {
            app.update();
        }
        let scale_later = app.world().get::<UiTransform>(chip).expect("chip").scale;
        let size_later = app.world().get::<TextFont>(chip).expect("chip").font_size;
        assert!(
            scale_later.x <= scale_at_peak.x,
            "the pop should decay: {scale_at_peak:?} then {scale_later:?}"
        );
        assert_eq!(size_at_peak, FontSize::Px(SCORE_PX[0]));
        assert_eq!(size_later, FontSize::Px(SCORE_PX[0]), "size never animates");
    }

    #[test]
    fn ranks_sort_by_score_then_seat() {
        // P3 leads, P1 and P2 tie (seat order breaks it), P4 trails.
        assert_eq!(ranks(&[4, 4, 9, 1, 0, 0], 4), [1, 2, 0, 3, 0, 0]);
        // Two seats only: the trailing slots keep rank 0 but are unused.
        assert_eq!(ranks(&[0, 7, 0, 0, 0, 0], 2), [1, 0, 0, 0, 0, 0]);
        // A full table of six ranks all of them.
        assert_eq!(ranks(&[1, 6, 2, 5, 3, 4], 6), [5, 0, 4, 1, 3, 2]);
    }
}