NL
games/rob-revisited / src / Revisited / Ui / BodyPanel.php · commit f9a1307
<?phpnamespace App\Revisited\Ui;use App\Revisited\I18n\Translator;use App\Revisited\Land\Game;use App\Revisited\Sim\Body;use App\Revisited\Sim\Inventory;use App\Revisited\Sim\Items;use JumPHP\Renderer\Sdl;/** * The "Corps" tab drawn, two views (buttons above the figure): * *  - Habillé: Rob dressed (BodyFigure), the six clothing slots (click: take off / put on the warmest garment of that *    slot), the warmth on each region of the body with the skin temperature of the hands and the feet, and for the part *    chosen: what covers it, how much it protects, what else could be put on there, mending; *  - Médical: the bare body with the wounds where they are, bandages and splints, the bleeding (animated drops), the *    heart and breathing traces, the vitals with a note each, the conditions with advice, the wounds of the part chosen *    and the cares to apply to it. * * Screens handles the input (hits: view:*, part:*, cloth:<slot>, wear:<id>, mend, care:<i>, treat). */final class BodyPanel{    public const PARTS = ['head' => 'Tête', 'torso' => 'Torse', 'rarm' => 'Bras droit', 'larm' => 'Bras gauche', 'rleg' => 'Jambe droite', 'lleg' => 'Jambe gauche'];    public const VIEWS = ['dressed' => 'Habillé', 'medical' => 'Médical'];    /** state => [colour, legend] (the part's state, shown in the medical view's legend and the wound markers) */    public const STATES = [        'ok' => [[86, 168, 118], 'Sain'],        'cold' => [[92, 150, 232], 'Froid'],        'bruise' => [[214, 190, 70], 'Contusion'],        'wound' => [[230, 132, 50], 'Plaie'],        'bleed' => [[214, 50, 50], 'Saigne'],        'infected' => [[164, 92, 214], 'Infecté'],        'treated' => [[96, 178, 176], 'Soigné'],    ];    /** part => the regions of the clothes over it */    public const REGIONS_OF = ['head' => ['head'], 'torso' => ['torso'], 'rarm' => ['arms', 'hands'], 'larm' => ['arms', 'hands'],        'rleg' => ['legs', 'feet'], 'lleg' => ['legs', 'feet']];    private const REGION_LABEL = ['head' => 'Tête', 'torso' => 'Torse', 'arms' => 'Bras', 'hands' => 'Mains', 'legs' => 'Jambes', 'feet' => 'Pieds'];    /** the figure's box on screen (units) */    private const FX = 60.0;    private const FY = 150.0;    private const FS = 1.9;    /** hit boxes per part in figure units, in this order (the last one wins) */    private const HITS = [        'torso' => [28, 27, 44, 76], 'rleg' => [30, 100, 21, 95], 'lleg' => [49, 100, 21, 95],        'rarm' => [9, 36, 22, 78], 'larm' => [69, 36, 22, 78], 'head' => [38, 3, 24, 28],    ];    private mixed $texture = null;    private string $key = '';    public function __construct(private ?Sdl $sdl)    {    }    /** An estimate of the heart rate (beats per minute) from the body's state and what Rob is doing. */    public static function pulse(Game $g): int    {        $v = $g->body->vitals();        $bpm = 64.0;        $bpm += max(0.0, Body::BLOOD_MAX - $v['blood']) * 24;        $bpm += max(0.0, $v['temperature'] - 37.0) * 9 + $v['fever'] * 18;        $bpm += $g->body->pain() * 22;        $bpm += $g->running ? 46 : ($g->moving ? 16 : 0);        $bpm += max(0.0, 30 - $v['thirst']) * 0.4 + max(0.0, 40 - $v['morale']) * 0.2;        $bpm -= max(0.0, 35.5 - $v['temperature']) * 7;        return (int) round(max(38.0, min(178.0, $bpm)));    }    /** An estimate of the breathing rate (breaths per minute). */    public static function breaths(Game $g): int    {        $v = $g->body->vitals();        $r = 13.0 + ($g->running ? 14 : ($g->moving ? 4 : 0)) + $g->body->pain() * 6 + $v['fever'] * 7            + ($v['blood'] < Body::BLOOD_SHOCK ? 8 : 0) - max(0.0, 35 - $v['temperature']) * 2;        return (int) round(max(8.0, min(40.0, $r)));    }    /** @return string the state of a part: ok cold bruise wound bleed infected treated */    public static function state(Game $g, string $part): string    {        $worst = null;        $rank = ['treated' => 1, 'bruise' => 2, 'wound' => 3, 'bleed' => 4, 'infected' => 5];        foreach ($g->body->injuries() as $w) {            if ($w['part'] !== $part) {                continue;            }            $s = match (true) {                str_contains($w['label'], 'infectée') => 'infected',                $w['treated'] => 'treated',                str_contains($w['label'], 'saigne') => 'bleed',                $w['kind'] === 'bruise' && $w['severity'] < 0.5 => 'bruise',                default => 'wound',            };            if ($worst === null || $rank[$s] > $rank[$worst]) {                $worst = $s;            }        }        if ($worst !== null) {            return $worst;        }        $t = $g->body->vitals()['temperature'];        $x = $g->body->extremityTemps();        $limbCold = ($part === 'rarm' || $part === 'larm') ? $x['hands'] < 15 : (($part === 'rleg' || $part === 'lleg') ? $x['feet'] < 15 : false);        return $t < 35.6 || $limbCold ? 'cold' : 'ok';    }    /** The worst part (bleeding, infected, wounded...), for opening the medical view on it. */    public static function worstPart(Game $g): string    {        $rank = ['ok' => 0, 'cold' => 1, 'treated' => 2, 'bruise' => 3, 'wound' => 4, 'bleed' => 6, 'infected' => 5];        $best = 'torso';        $score = -1;        foreach (array_keys(self::PARTS) as $p) {            $s = $rank[self::state($g, $p)];            if ($s > $score) {                $score = $s;                $best = $p;            }        }        return $best;    }    /** @param list<string> $cares the medical items of the bag */    public function draw(Canvas $c, Game $g, string $view, string $selected, float $time, array $cares, int $treatSel): void    {        $x = self::FX;        foreach (self::VIEWS as $id => $label) {            $on = $id === $view;            $w = $c->measure($label, 15) + 30;            $c->pill($x, 114, $w, 26, $on ? [120, 190, 255, 120] : [255, 255, 255, 22]);            $c->text($label, $x + $w / 2, 118, 15, $on ? [255, 255, 255] : [176, 188, 200], 'center');            $c->hit($x, 112, $w, 30, 'view:' . $id);            $x += $w + 8;        }        $this->figure($c, $g, $view, $selected, $time);        if ($view === 'medical') {            $this->heart($c, $g, 284.0, 114.0, $time);            $this->vitals($c, $g, 284.0, 246.0);            $this->status($c, $g, 612.0, 114.0);            $this->partMedical($c, $g, $selected, 612.0, 330.0, $cares, $treatSel);        } else {            $this->slots($c, $g, 284.0, 114.0);            $this->warmth($c, $g, 284.0, 372.0);            $this->partDressed($c, $g, $selected, 612.0, 114.0);        }    }    /** @return array<string, mixed> what the figure shows (its texture is rebuilt only when this changes) */    public static function look(Game $g, string $view, string $selected): array    {        $clothes = [];        foreach ($g->inv->wornClothes() as $slot => $id) {            $clothes[$slot] = [$id, (float) (10 * (int) ($g->inv->condition($id) / 10)), round($g->inv->wet, 1)];        }        $wounds = [];        $n = [];        foreach ($g->body->injuries() as $w) {            $p = $w['part'];            $k = $n[$p] = ($n[$p] ?? -1) + 1;            $wounds[] = [$p, $w['kind'], round($w['severity'], 1), $w['treated'], str_contains($w['label'], 'infectée') ? 0.6 : 0.0, str_contains($w['label'], 'saigne'), $k];        }        $v = $g->body->vitals();        $x = $g->body->extremityTemps();        return ['view' => $view, 'selected' => $selected, 'clothes' => $view === 'dressed' ? $clothes : [], 'wounds' => $view === 'medical' ? $wounds : [],            'cold' => ['hands' => (float) (2 * (int) ($x['hands'] / 2)), 'feet' => (float) (2 * (int) ($x['feet'] / 2))],            'fever' => round($v['fever'], 1), 'pale' => round(max(0.0, min(1.0, (Body::BLOOD_MAX - $v['blood']) / 2.0)), 1)];    }    private function figure(Canvas $c, Game $g, string $view, string $selected, float $time): void    {        $w = 100 * self::FS;        $h = 200 * self::FS;        $c->rect(self::FX - 8, self::FY - 4, $w + 16, $h + 10, [255, 255, 255, 6]);        $px = max(64, (int) round($h * $c->s));        $look = self::look($g, $view, $selected);        $key = (string) json_encode([$look, $px]);        if ($key !== $this->key && $this->sdl !== null) {            $this->key = $key;            try {                $slices = $this->sdl->createTextureFromGd('rv_body_figure', BodyFigure::image($look, $px));                $this->texture = $slices[0]['texture'] ?? null;            } catch (\Throwable) {                $this->texture = null;            }        }        if ($this->texture !== null) {            $c->image($this->texture, self::FX, self::FY, $w, $h);        }        foreach (self::HITS as $part => [$hx, $hy, $hw, $hh]) {            $c->hit(self::FX + $hx * self::FS, self::FY + $hy * self::FS, $hw * self::FS, $hh * self::FS, 'part:' . $part);        }        if ($view === 'medical') {            // what moves: the drops of a bleeding wound, the pulse of an infection, a marker on each wound            $n = [];            foreach ($g->body->injuries() as $inj) {                $p = $inj['part'];                $k = $n[$p] = ($n[$p] ?? -1) + 1;                [$ax, $ay] = BodyFigure::spot($p, $k);                $x = self::FX + $ax * self::FS;                $y = self::FY + $ay * self::FS;                if (str_contains($inj['label'], 'infectée')) {                    $r = 13 + 4 * sin($time * 3);                    $c->circle($x, $y, $r, [200, 110, 255, 40]);                }                if (str_contains($inj['label'], 'saigne')) {                    for ($d = 0; $d < 3; $d++) {                        $ph = fmod($time * 0.9 + $d / 3, 1.0);                        $c->circle($x - 1 + $d, $y + 6 + $ph * 26, 2.6 * (1 - $ph * 0.4), [200, 20, 28, (int) (230 * (1 - $ph))]);                    }                }                $col = str_contains($inj['label'], 'infectée') ? [200, 120, 255] : (str_contains($inj['label'], 'saigne') ? [255, 70, 70]                    : ($inj['treated'] ? [140, 220, 220] : [255, 170, 70]));                $mx = $ax < 50 ? $x - 26 : $x + 26;                $c->line($x, $y, $mx, $y, [255, 255, 255, 90], 1);                $c->circle($mx, $y, 8, [0, 0, 0, 150]);                $c->circle($mx, $y, 6.5, $col);                $sym = ['cut' => '/', 'bite' => 'M', 'burn' => '~', 'bruise' => 'o', 'fracture' => 'X'][$inj['kind']] ?? '!';                $c->text($sym, $mx, $y - 6, 10, [30, 16, 16], 'center', false);            }        }        $c->text(self::PARTS[$selected] ?? '', self::FX + $w / 2, self::FY + $h + 8, 16, [255, 255, 255], 'center');        $c->text($view === 'medical' ? 'Clique une partie (↑ ↓) pour la soigner' : 'Clique une partie : ce qui la couvre', self::FX + $w / 2, self::FY + $h + 28, 11, [150, 166, 180], 'center');        if ($view === 'medical') {            $i = 0;            foreach (['cut' => '/ coupure', 'bite' => 'M morsure', 'burn' => '~ brûlure', 'bruise' => 'o contusion', 'fracture' => 'X fracture'] as $label) {                $c->text($label, 56 + ($i % 3) * 74, 618 + intdiv($i, 3) * 16, 11, [190, 200, 210]);                $i++;            }        }    }    private function heart(Canvas $c, Game $g, float $x, float $y, float $time): void    {        $bpm = self::pulse($g);        $rpm = self::breaths($g);        $v = $g->body->vitals();        $w = 312.0;        $c->rect($x, $y, $w, 122, [6, 18, 14, 220]);        $c->border($x, $y, $w, 122, [80, 200, 140, 90]);        for ($gx = $x + 13; $gx < $x + $w; $gx += 26) {            $c->rect($gx, $y + 1, 1, 120, [80, 200, 140, 18]);        }        // the heart trace, scrolling left        $period = 60.0 / max(30, $bpm);        $base = $y + 50;        $prev = null;        $amp = $v['blood'] < Body::BLOOD_SHOCK ? 0.6 : 1.0;        for ($sx = 0.0; $sx <= 200; $sx += 2) {            $tt = $time - (200 - $sx) / 96.0;            $ph = fmod($tt, $period) / $period;            $ph = $ph < 0 ? $ph + 1 : $ph;            $pt = [$x + 8 + $sx, $base - self::ecg($ph) * 28 * $amp];            if ($prev !== null) {                $c->line($prev[0], $prev[1], $pt[0], $pt[1], [110, 255, 170, (int) (60 + 195 * $sx / 200)], 1.6);            }            $prev = $pt;        }        // the breathing: a slow wave        $bp = 60.0 / max(6, $rpm);        $prev = null;        for ($sx = 0.0; $sx <= 200; $sx += 3) {            $tt = $time - (200 - $sx) / 40.0;            $pt = [$x + 8 + $sx, $y + 100 - 9 * sin(6.2832 * $tt / $bp)];            if ($prev !== null) {                $c->line($prev[0], $prev[1], $pt[0], $pt[1], [120, 190, 255, (int) (50 + 170 * $sx / 200)], 1.3);            }            $prev = $pt;        }        $beat = fmod($time, $period) / $period < 0.12;        $hc = $beat ? [255, 90, 90] : [170, 60, 60];        $hs = $beat ? 1.15 : 1.0;        $c->circle($x + 230 - 3.4 * $hs, $y + 16, 4.4 * $hs, $hc);        $c->circle($x + 230 + 3.4 * $hs, $y + 16, 4.4 * $hs, $hc);        $c->triangle($x + 230 - 7.8 * $hs, $y + 17.5, $x + 230 + 7.8 * $hs, $y + 17.5, $x + 230, $y + 17 + 9 * $hs, $hc);        $c->text((string) $bpm, $x + 302, $y + 2, 28, [140, 255, 190], 'right');        $c->text('bpm', $x + 302, $y + 36, 11, [100, 190, 150], 'right');        $c->text($rpm . ' resp/min', $x + 302, $y + 82, 13, [150, 200, 255], 'right');        $t = $v['temperature'];        $tc = $t < 35.5 ? [120, 180, 255] : ($t > 37.8 ? [255, 150, 90] : [220, 240, 230]);        $c->text(Translator::num($t, 1) . ' °C', $x + 302, $y + 54, 15, $tc, 'right');        $c->text(self::pulseNote($bpm), $x + 8, $y + 4, 11, [110, 200, 160]);        $c->text($rpm > 22 ? 'Respiration rapide' : ($rpm < 10 ? 'Respiration lente' : 'Respiration calme'), $x + 8, $y + 108 - 4, 11, [130, 180, 230]);    }    /** One beat (0..1 of a period): P, QRS, T waves. */    private static function ecg(float $p): float    {        $g = static fn (float $c, float $w) => exp(-(($p - $c) / $w) ** 2);        return 0.12 * $g(0.18, 0.035) - 0.12 * $g(0.30, 0.012) + 1.0 * $g(0.33, 0.012) - 0.25 * $g(0.36, 0.014) + 0.22 * $g(0.58, 0.06);    }    private static function pulseNote(int $bpm): string    {        return match (true) {            $bpm < 50 => 'Pouls faible : le corps ralentit',            $bpm <= 90 => 'Pouls normal (60–90 au repos)',            $bpm <= 120 => 'Pouls rapide',            default => 'Pouls très rapide : effort, douleur ou sang perdu',        };    }    /** @return list<array{0: string, 1: string, 2: float, 3: string}> key, label, value, note */    public static function vitalRows(Game $g): array    {        $v = $g->body->vitals();        return [            ['health', 'Santé', $v['health'], self::note($v['health'], ['Au bord de la mort', 'Très affaibli', 'Affaibli', 'En forme'])],            ['blood', 'Sang', $v['blood'], $v['blood'] < Body::BLOOD_SHOCK ? 'Choc : sous 3,6 L, la santé fond' : ($v['bleeding'] > 0 ? 'Saigne : ' . Translator::num($v['bleeding'], 3) . ' L/min' : 'Normal : 5,5 L')],            ['temperature', 'Température', $v['temperature'], $v['temperature'] < 35 ? 'Hypothermie (< 35 °C)' : ($v['temperature'] > 38 ? 'Fièvre (> 38 °C)' : 'Normale 36,5–37,5')],            ['hunger', 'Satiété', $v['hunger'], self::note($v['hunger'], ['Affamé', 'Faim', 'Un creux', 'Rassasié'])],            ['thirst', 'Hydratation', $v['thirst'], self::note($v['thirst'], ['Déshydraté', 'Assoiffé', 'Soif', 'Hydraté'])],            ['fatigue', 'Repos', $v['fatigue'], self::note($v['fatigue'], ['Épuisé', 'Fatigué', 'Las', 'Reposé'])],            ['morale', 'Moral', $v['morale'], self::note($v['morale'], ['Désespéré', 'Abattu', 'Inquiet', 'Confiant'])],        ];    }    private function vitals(Canvas $c, Game $g, float $x, float $y): void    {        $c->text('CONSTANTES', $x, $y, 12, [150, 166, 180]);        $y += 17;        foreach (self::vitalRows($g) as [$key, $label, $val, $note]) {            [$fill, $text] = Hud::vital($key, (float) $val);            $col = Hud::VITALS[$key][2];            $c->icon(Hud::VITALS[$key][1], $x, $y + 1, 18);            $c->text($label, $x + 24, $y, 13, [214, 222, 230]);            $c->text($text, $x + 196, $y, 13, $fill < 0.25 ? [255, 130, 110] : [244, 246, 250], 'right');            $c->rect($x + 206, $y + 5, 104, 5, [255, 255, 255, 24]);            $c->rect($x + 206, $y + 5, 104 * $fill, 5, $fill < 0.25 ? [230, 70, 60] : $col);            $c->text($note, $x + 24, $y + 15, 10, [140, 156, 170]);            $y += 31;        }        $pain = $g->body->pain();        $c->text('Douleur', $x + 24, $y, 13, [214, 222, 230]);        $c->text($pain < 0.05 ? 'Aucune' : ($pain < 0.35 ? 'Légère' : ($pain < 0.7 ? 'Forte' : 'Insupportable')), $x + 196, $y, 13, $pain > 0.5 ? [255, 150, 120] : [244, 246, 250], 'right');        $c->rect($x + 206, $y + 5, 104, 5, [255, 255, 255, 24]);        $c->rect($x + 206, $y + 5, 104 * $pain, 5, [230, 110, 80]);        $y += 22;        $e = $g->body->extremityTemps();        $c->text(sprintf('Mains %s · Pieds %s', self::deg($e['hands']), self::deg($e['feet'])), $x + 24, $y, 12,            min($e['hands'], $e['feet']) < 15 ? [140, 190, 255] : [190, 200, 210]);        $this->fitness($c, $g, $x, $y + 24);    }    /**     * The long-term fitness: "Endurance +12 %", "Force −4 %", the change over 7 days as an arrow and a number.     * @return list<array{0: string, 1: float, 2: float, 3: string, 4: string}> label, percent, 7-day change, value text, trend text     */    public static function fitnessRows(Game $g): array    {        $f = $g->body->fitness();        $rows = [];        foreach ([['Endurance', $f['endurance'], $f['enduranceTrend']], ['Force', $f['force'], $f['forceTrend']]] as [$label, $v, $t]) {            $arrow = $t > 0.5 ? '↑' : ($t < -0.5 ? '↓' : '→');            $rows[] = [$label, $v, $t, self::signed($v) . ' %', sprintf('%s %s %% en 7 j', $arrow, self::signed($t))];        }        return $rows;    }    /** "+12", "−4" (a true minus sign), "0" */    public static function signed(float $v): string    {        $n = (int) round($v);        return $n > 0 ? '+' . $n : ($n < 0 ? '−' . abs($n) : '0');    }    private function fitness(Canvas $c, Game $g, float $x, float $y, float $indent = 24.0): void    {        $c->text('FORME PHYSIQUE', $x, $y, 12, [150, 166, 180]);        $y += 17;        foreach (self::fitnessRows($g) as [$label, $v, $t, $value, $trend]) {            $c->text($label, $x + $indent, $y, 13, [214, 222, 230]);            $c->text($value, $x + 196, $y, 13, $v > 0.5 ? [150, 230, 160] : ($v < -0.5 ? [255, 150, 120] : [244, 246, 250]), 'right');            $c->text($trend, $x + 312, $y + 1, 11, $t > 0.5 ? [140, 210, 160] : ($t < -0.5 ? [240, 160, 130] : [160, 172, 184]), 'right');            $y += 18;        }        // the level (experience): Talents tab, L        [$level, $xp, $fill] = TalentsView::levelLine($g);        $c->text($level, $x + $indent, $y, 13, [255, 214, 140]);        $c->rect($x + 104, $y + 5, 92, 5, [255, 255, 255, 24]);        $c->rect($x + 104, $y + 5, 92 * $fill, 5, [255, 196, 90]);        $pts = $g->talents->points();        $c->text($pts > 0 ? $xp . ' · ' . $pts . ' pt' . ($pts > 1 ? 's' : '') : $xp, $x + 312, $y + 1, 11, $pts > 0 ? [140, 235, 160] : [200, 210, 220], 'right');        $c->hit($x, $y - 2, 316, 18, 'tab:talents');    }    private static function deg(float $t): string    {        return Translator::num($t, 0) . ' °C';    }    /** @param array{0: string, 1: string, 2: string, 3: string} $words worst .. best */    private static function note(float $v, array $words): string    {        return $v < 10 ? $words[0] : ($v < 30 ? $words[1] : ($v < 60 ? $words[2] : $words[3]));    }    /** The conditions, each with what to do. */    private function status(Canvas $c, Game $g, float $x, float $y): void    {        $c->text('ÉTAT ET CONSEILS', $x, $y, 12, [150, 166, 180]);        $y += 18;        $advice = ScannerView::advice($g);        foreach (array_slice($advice, 0, 5) as [$sev, $text]) {            $col = $sev > 0.6 ? [255, 120, 110] : ($sev > 0.3 ? [255, 180, 110] : ($sev > 0.05 ? [240, 220, 140] : [140, 210, 160]));            $lines = $c->wrap($text, 12, 286);            $c->rect($x, $y + 3, 4, 4 + 14 * (count($lines) - 1), $col);            foreach (array_slice($lines, 0, 3) as $i => $line) {                $c->text($line, $x + 10, $y + $i * 14, 12, [222, 228, 234]);            }            $y += 14 * min(3, count($lines)) + 6;            if ($y > 316) {                break;            }        }    }    /** @param list<string> $cares */    private function partMedical(Canvas $c, Game $g, string $part, float $x, float $y, array $cares, int $treatSel): void    {        $c->rect($x, $y - 6, 296, 1, [255, 255, 255, 30]);        $c->text('BLESSURES · ' . mb_strtoupper(self::PARTS[$part] ?? ''), $x, $y, 12, [150, 166, 180]);        $y += 18;        $any = false;        foreach ($g->body->injuries() as $w) {            if ($w['part'] !== $part) {                continue;            }            $any = true;            $s = $w['severity'];            $c->text($w['label'], $x, $y, 12, $w['treated'] ? [150, 200, 220] : [244, 224, 214]);            $c->rect($x + 240, $y + 5, 56, 5, [255, 255, 255, 26]);            $c->rect($x + 240, $y + 5, 56 * min(1.0, $s), 5, $s > 0.6 ? [220, 60, 60] : ($s > 0.3 ? [230, 140, 50] : [220, 200, 70]));            $y += 18;            if ($y > 420) {                break;            }        }        if (!$any) {            $c->text(self::state($g, $part) === 'cold' ? 'Pas de blessure, mais ce membre a froid.' : 'Aucune blessure ici.', $x, $y, 12, [140, 210, 160]);            $y += 18;        }        $y = max($y + 6, 440.0);        $c->text('SOIGNER : ' . mb_strtoupper(self::PARTS[$part] ?? ''), $x, $y, 12, [150, 166, 180]);        if ($cares === []) {            foreach ($c->wrap('Aucun soin dans ton sac (bandage, attelle, cataplasme, herbes…).', 12, 290) as $i => $line) {                $c->text($line, $x, $y + 20 + $i * 15, 12, [200, 160, 140]);            }            return;        }        $useful = self::usefulCares($g, $part);        $treatSel = max(0, min($treatSel, count($cares) - 1));        foreach (array_slice($cares, 0, 10) as $i => $id) {            $cx = $x + ($i % 5) * 59;            $cy = $y + 18 + intdiv($i, 5) * 56;            $on = $i === $treatSel;            $c->rect($cx, $cy, 52, 52, $on ? [120, 190, 255, 60] : [255, 255, 255, in_array($id, $useful, true) ? 26 : 10]);            $c->border($cx, $cy, 52, 52, $on ? [160, 214, 255, 220] : (in_array($id, $useful, true) ? [140, 230, 160, 160] : [255, 255, 255, 30]));            $c->icon($id, $cx + 8, $cy + 6, 36);            $n = $g->inv->count($id);            if ($n > 1) {                $c->text('×' . $n, $cx + 48, $cy + 34, 11, [255, 255, 255], 'right');            }            $c->hit($cx, $cy, 52, 52, 'care:' . $i);        }        $sel = $cares[$treatSel];        $dy = $y + 18 + (count($cares) > 5 ? 112 : 56) + 2;        $c->text(Items::name($sel) . (in_array($sel, $useful, true) ? ' — utile ici' : ''), $x, $dy, 12, in_array($sel, $useful, true) ? [150, 230, 170] : [210, 218, 226]);        $this->button($c, $x, 610, 'Appliquer : ' . mb_strtolower(self::PARTS[$part] ?? ''), 'Entrée', 'treat');    }    /** @return list<string> the cares of the bag that do something for this part now */    public static function usefulCares(Game $g, string $part): array    {        $need = [];        foreach ($g->body->injuries() as $w) {            if ($w['part'] !== $part) {                continue;            }            if (in_array($w['kind'], ['cut', 'bite', 'burn'], true) && !$w['treated']) {                $need['bleed'] = true;            }            if ($w['kind'] === 'fracture' && !$w['treated']) {                $need['fracture'] = true;            }            if ($w['kind'] !== 'fracture' && $w['kind'] !== 'bruise') {                $need['infection'] = true;            }        }        $v = $g->body->vitals();        $out = [];        foreach (array_keys($g->inv->all()) as $id) {            $heal = Items::get($id)['heal'] ?? [];            foreach ($heal as $k => $_) {                if (isset($need[$k]) || ($k === 'health' && $v['health'] < 90) || ($k === 'fever' && $v['fever'] > 0.1)) {                    $out[] = $id;                    break;                }            }        }        return $out;    }    // ---- the dressed view -------------------------------------------------------------------------------    private function slots(Canvas $c, Game $g, float $x, float $y): void    {        $c->text('VÊTEMENTS', $x, $y + 4, 12, [150, 166, 180]);        $c->text('clic : ôter / enfiler', $x + 312, $y + 4, 11, [120, 136, 150], 'right');        $y += 22;        foreach (Inventory::CLOTH_SLOTS as $k => $slot) {            $sx = $x + ($k % 2) * 160;            $sy = $y + intdiv($k, 2) * 80;            $id = $g->inv->$slot;            $c->rect($sx, $sy, 152, 74, [255, 255, 255, 14]);            $c->border($sx, $sy, 152, 74, [255, 255, 255, 40]);            $c->text(Inventory::SLOT_LABEL[$slot], $sx + 8, $sy + 4, 11, [150, 166, 180]);            if ($id !== null && $g->inv->has($id)) {                $c->icon($id, $sx + 6, $sy + 20, 34);                foreach (array_slice($c->wrap(Items::name($id), 12, 100), 0, 2) as $i => $line) {                    $c->text($line, $sx + 46, $sy + 20 + $i * 14, 12, [236, 240, 244]);                }                $cond = $g->inv->condition($id);                $c->rect($sx + 8, $sy + 62, 136, 5, [255, 255, 255, 26]);                $c->rect($sx + 8, $sy + 62, 136 * $cond / 100, 5, $cond < 20 ? [230, 70, 60] : ($cond < 50 ? [230, 170, 60] : [110, 200, 140]));                $c->text((int) round($cond) . ' %', $sx + 144, $sy + 4, 11, [200, 210, 220], 'right');            } else {                $spare = self::spare($g, $slot);                $c->text('— rien —', $sx + 76, $sy + 24, 13, [120, 130, 140], 'center');                if ($spare !== null) {                    $c->text('enfiler : ' . Items::name($spare), $sx + 76, $sy + 44, 10, [150, 210, 255], 'center');                }            }            $c->hit($sx, $sy, 152, 74, 'cloth:' . $slot);        }    }    /** The warmest garment of a slot in the bag (not worn), null: none. */    public static function spare(Game $g, string $slot): ?string    {        $best = null;        foreach ($g->inv->all() as $id => $n) {            if (Items::get($id)['kind'] !== 'clothing' || Inventory::slotOf($id) !== $slot || $g->inv->isWorn($id) !== null) {                continue;            }            if ($best === null || (Items::get($id)['warmth'] ?? 0) * $g->inv->condition($id) > (Items::get($best)['warmth'] ?? 0) * $g->inv->condition($best)) {                $best = $id;            }        }        return $best;    }    private function warmth(Canvas $c, Game $g, float $x, float $y): void    {        $c->text('CHALEUR DES HABITS PAR ZONE', $x, $y, 12, [150, 166, 180]);        $y += 20;        $e = $g->body->extremityTemps();        foreach (Inventory::REGIONS as $r) {            $w = $g->inv->regionWarmth($r);            $c->text(self::REGION_LABEL[$r], $x, $y, 13, [214, 222, 230]);            $c->rect($x + 64, $y + 5, 120, 6, [255, 255, 255, 24]);            $c->rect($x + 64, $y + 5, 120 * min(1.0, $w / 10), 6, $w < 1 ? [230, 120, 90] : [240, 170, 90]);            $c->text($w < 0.3 ? 'nu' : '+' . Translator::num($w, 1) . ' °C', $x + 192, $y, 12, $w < 0.3 ? [255, 150, 120] : [230, 236, 242]);            if ($r === 'hands' || $r === 'feet') {                $t = $e[$r];                $c->text('peau ' . self::deg($t), $x + 312, $y, 11, $t < 15 ? [140, 190, 255] : [170, 180, 190], 'right');            }            $y += 22;        }        $wet = $g->inv->wet;        $c->text(sprintf('Total : +%s °C', Translator::num($g->inv->warmth(), 1)), $x, $y + 4, 13, [255, 214, 150]);        if ($wet > 0.05) {            $c->text(sprintf('Mouillés à %d %% : sèche-les près d\'un feu', (int) round($wet * 100)), $x + 312, $y + 4, 11, [120, 180, 255], 'right');        }        $this->fitness($c, $g, $x, $y + 30, 0.0);    }    private function partDressed(Canvas $c, Game $g, string $part, float $x, float $y): void    {        $c->text('SUR ' . mb_strtoupper(self::PARTS[$part] ?? ''), $x, $y + 4, 12, [150, 166, 180]);        $y += 24;        $covering = [];        foreach ($g->inv->wornClothes() as $slot => $id) {            if (array_intersect(Inventory::coverOf($id), self::REGIONS_OF[$part] ?? []) !== []) {                $covering[$slot] = $id;            }        }        if ($covering === []) {            $c->text('Rien ne couvre cette partie : elle est nue.', $x, $y, 13, [255, 170, 140]);            $y += 22;        }        foreach ($covering as $slot => $id) {            $c->icon($id, $x, $y, 28);            $c->text($g->inv->label($id), $x + 34, $y, 13, [236, 240, 244]);            $c->text(sprintf('chaleur +%s °C · protège %d %%', Translator::num($g->inv->garmentWarmth($id), 1),                (int) round(100 * (float) (Items::get($id)['protect'] ?? 0) * (0.3 + 0.7 * $g->inv->condition($id) / 100))), $x + 34, $y + 15, 11, [170, 186, 200]);            $y += 36;        }        $c->text(sprintf('Protection contre les coups : %d %%', (int) round(100 * $g->inv->protection($part))), $x, $y + 2, 12, [200, 210, 220]);        $y += 30;        // what could be put on here        $c->text('DANS LE SAC', $x, $y, 12, [150, 166, 180]);        $y += 20;        $n = 0;        foreach ($g->inv->all() as $id => $cnt) {            $d = Items::get($id);            if ($d['kind'] !== 'clothing' || $g->inv->isWorn($id) !== null || array_intersect(Inventory::coverOf($id), self::REGIONS_OF[$part] ?? []) === []) {                continue;            }            $this->button($c, $x, $y, 'Enfiler : ' . Items::name($id), '', 'wear:' . $id, 13);            $y += 40;            if (++$n >= 4) {                break;            }        }        if ($n === 0) {            foreach ($c->wrap('Aucun autre vêtement pour cette partie. Artisanat : bonnet, toque, gants, moufles, pantalon, jambières, bottes…', 12, 290) as $i => $line) {                $c->text($line, $x, $y + $i * 15, 12, [160, 170, 180]);            }            $y += 50;        }        if ($g->inv->has('patch')) {            $this->button($c, $x, max($y + 6, 560.0), 'Rapiécer le plus usé', '', 'mend', 13);        }    }    /** A button with an optional key; returns its width. */    private function button(Canvas $c, float $x, float $y, string $label, string $key, string $hit, float $size = 16): float    {        $kw = $key === '' ? 0.0 : $c->measure($key, 12) + 12;        $w = $kw + $c->measure($label, $size) + ($key === '' ? 20 : 26);        $h = $size > 14 ? 36.0 : 32.0;        $c->rect($x, $y, $w, $h, [120, 190, 255, 44]);        $c->border($x, $y, $w, $h, [150, 210, 255, 150]);        if ($key !== '') {            $c->pill($x + 8, $y + ($h - 18) / 2, $kw, 18, [235, 238, 242, 220]);            $c->text($key, $x + 8 + $kw / 2, $y + ($h - 18) / 2 + 2, 12, [20, 26, 34], 'center', false);        }        $c->text($label, $x + ($key === '' ? 10 : 16 + $kw), $y + ($h - $size) / 2 - 1, $size, [255, 255, 255]);        $c->hit($x, $y, $w, $h, $hit);        return $w;    }}