TR
games/rob-revisited / src / Revisited / Ui / BodyFigure.php · commit f9a1307
<?phpnamespace App\Revisited\Ui;/** * The human figure of the "Corps" tab: Rob seen from the front (his right arm on the left of the screen), drawn with * GD in a 100 x 200 box at twice the size then smoothed down (anti-aliased). Two looks: * *  - "dressed": the clothes worn, opaque, each with its own cut, colours, seams and folds, worn out (holes showing the *    skin, frayed edges), dirty, darker when wet; the bare skin where nothing covers it; *  - "medical": the bare body (skin, in briefs) tinted by its state (pale from blood loss, flushed with fever, blue hands *    and feet in the cold), the wounds drawn where they are (cuts, bites, burns, bruises, broken bones as a bone line), *    infection glowing around them, bandages and splints where they were put. * * Made once per look (BodyPanel keeps the texture until the look changes); what moves (bleeding drops, the pulse of an * infection) is drawn over it by BodyPanel. */final class BodyFigure{    private const SS = 2;    /** where a part's wounds are drawn, in order (box units): the first wound of a part at the first spot... */    public const SPOTS = [        'head' => [[46.0, 10.0], [55.0, 21.0], [43.0, 22.0]],        'torso' => [[42.0, 55.0], [58.0, 70.0], [47.0, 86.0], [56.0, 46.0]],        'rarm' => [[24.0, 56.0], [19.0, 86.0], [22.5, 72.0]],        'larm' => [[76.0, 56.0], [81.0, 86.0], [77.5, 72.0]],        'rleg' => [[40.5, 120.0], [40.4, 160.0], [40.8, 140.0]],        'lleg' => [[59.5, 120.0], [59.6, 160.0], [59.2, 140.0]],    ];    /** kept for the older callers: the first spot of each part */    public const ANCHORS = [        'head' => [50.0, 13.0], 'torso' => [50.0, 60.0], 'rarm' => [19.5, 80.0], 'larm' => [80.5, 80.0],        'rleg' => [40.5, 128.0], 'lleg' => [59.5, 128.0],    ];    /** garment => [cloth, dark, trim, accent] */    private const PALETTE = [        'flight_suit_torn' => [[70, 92, 118], [40, 54, 72], [226, 132, 52], [196, 200, 206]],        'undershirt' => [[104, 112, 120], [70, 76, 84], [140, 150, 160], [90, 96, 104]],        'tunic_fiber' => [[176, 160, 112], [126, 112, 74], [140, 120, 70], [200, 186, 140]],        'rags' => [[134, 120, 96], [92, 82, 64], [110, 96, 74], [150, 136, 110]],        'jacket_hide' => [[140, 98, 62], [92, 62, 38], [70, 48, 30], [176, 134, 90]],        'coat_fur' => [[118, 88, 58], [80, 58, 38], [226, 214, 190], [190, 170, 140]],        'trousers_hide' => [[120, 86, 56], [80, 56, 36], [64, 44, 28], [150, 112, 76]],        'leggings_fur' => [[206, 196, 172], [150, 140, 118], [96, 70, 44], [232, 226, 210]],        'gloves_pilot' => [[38, 40, 44], [20, 22, 24], [120, 124, 130], [70, 74, 80]],        'gloves_hide' => [[150, 112, 74], [100, 72, 46], [80, 58, 36], [180, 140, 96]],        'mittens_fur' => [[214, 204, 180], [156, 146, 124], [110, 80, 50], [236, 230, 214]],        'pilot_boots_worn' => [[46, 44, 48], [26, 24, 28], [120, 120, 126], [80, 78, 84]],        'boots' => [[112, 82, 56], [72, 52, 34], [176, 146, 104], [140, 104, 70]],        'helmet_pilot' => [[214, 218, 222], [150, 156, 162], [226, 132, 52], [60, 80, 100]],        'cap_hide' => [[132, 94, 60], [88, 62, 40], [70, 50, 32], [160, 120, 80]],        'hat_fur' => [[198, 186, 160], [140, 128, 104], [92, 66, 40], [228, 220, 200]],    ];    private const SKIN = [222, 176, 144];    private \GdImage $im;    private float $k;    private function __construct(int $px)    {        $h = $px * self::SS;        $w = intdiv($h, 2);        $this->im = imagecreatetruecolor($w, $h);        imagealphablending($this->im, false);        imagefill($this->im, 0, 0, imagecolorallocatealpha($this->im, 0, 0, 0, 127));        imagealphablending($this->im, true);        $this->k = $h / 200;    }    /**     * @param array<string, mixed> $look view (dressed | medical), selected (a part or null), clothes (slot => [id, condition,     *   wet]), wounds (list of [part, kind, severity, treated, infection, bleeding, spot]), cold (hands, feet: skin °C), fever     *   0..1, pale 0..1     */    public static function image(array $look, int $px): \GdImage    {        $f = new self($px);        $f->draw($look);        $w = intdiv($px, 2);        $out = imagecreatetruecolor($w, $px);        imagealphablending($out, false);        imagefill($out, 0, 0, imagecolorallocatealpha($out, 0, 0, 0, 127));        imagesavealpha($out, true);        imagecopyresampled($out, $f->im, 0, 0, 0, 0, $w, $px, $w * self::SS, $px * self::SS);        return $out;    }    /** @return array{0: float, 1: float} where the n-th wound of a part is drawn (box units) */    public static function spot(string $part, int $n): array    {        $s = self::SPOTS[$part] ?? [[50.0, 60.0]];        return $s[$n % count($s)];    }    // ---- shapes of each part (box units) -----------------------------------------------------------    /** @return list<array{0: string, 1: array}> [type, args]: ell [cx, cy, rx, ry], poly [x, y, ...], cap [x1, y1, r1, x2, y2, r2] */    private static function shapes(string $part): array    {        $arm = [            ['cap', [27.0, 42.0, 6.4, 21.5, 71.0, 4.8]],            ['cap', [21.5, 71.0, 4.8, 17.0, 99.0, 3.4]],            ['ell', [16.0, 106.0, 4.0, 6.6]],        ];        $leg = [            ['cap', [41.0, 99.0, 9.6, 40.2, 142.0, 5.8]],            ['cap', [40.2, 142.0, 5.8, 40.6, 184.0, 3.6]],            ['ell', [39.0, 189.5, 6.2, 3.8]],        ];        return match ($part) {            'head' => [['ell', [50.0, 17.0, 10.8, 13.2]], ['ell', [39.2, 18.5, 2.2, 3.6]], ['ell', [60.8, 18.5, 2.2, 3.6]]],            'torso' => [                ['poly', [45.0, 27.0, 55.0, 27.0, 56.8, 37.0, 43.2, 37.0]],                ['poly', self::TORSO],            ],            'rarm' => $arm,            'larm' => self::mirror($arm),            'rleg' => $leg,            'lleg' => self::mirror($leg),            default => [],        };    }    private const TORSO = [43.0, 34.0, 57.0, 34.0, 66.0, 36.4, 71.6, 40.0, 72.4, 46.0, 70.0, 56.0, 67.0, 68.0, 65.0, 80.0, 66.6, 90.0,        67.4, 98.0, 58.0, 101.5, 50.0, 102.5, 42.0, 101.5, 32.6, 98.0, 33.4, 90.0, 35.0, 80.0, 33.0, 68.0, 30.0, 56.0, 27.6, 46.0, 28.4, 40.0, 34.0, 36.4];    /** @param list<array{0: string, 1: array}> $shapes @return list<array{0: string, 1: array}> */    private static function mirror(array $shapes): array    {        $out = [];        foreach ($shapes as [$type, $a]) {            if ($type === 'ell') {                $a[0] = 100 - $a[0];            } elseif ($type === 'cap') {                $a[0] = 100 - $a[0];                $a[3] = 100 - $a[3];            } else {                for ($i = 0; $i < count($a); $i += 2) {                    $a[$i] = 100 - $a[$i];                }            }            $out[] = [$type, $a];        }        return $out;    }    // ---- primitives ----------------------------------------------------------------------------------    private function col(array $c, int $alpha = 0): int    {        return imagecolorallocatealpha($this->im, max(0, min(255, (int) $c[0])), max(0, min(255, (int) $c[1])), max(0, min(255, (int) $c[2])), max(0, min(127, $alpha)));    }    private function shape(string $type, array $a, int $colour, float $grow = 0.0, float $dx = 0.0, float $dy = 0.0, float $narrow = 1.0): void    {        $k = $this->k;        if ($type === 'ell') {            imagefilledellipse($this->im, (int) (($a[0] + $dx) * $k), (int) (($a[1] + $dy) * $k), max(1, (int) (2 * ($a[2] * $narrow + $grow) * $k)), max(1, (int) (2 * ($a[3] + $grow) * $k)), $colour);            return;        }        if ($type === 'cap') {            $this->cap($a[0] + $dx, $a[1] + $dy, $a[2] * $narrow + $grow, $a[3] + $dx, $a[4] + $dy, $a[5] * $narrow + $grow, $colour);            return;        }        $n = intdiv(count($a), 2);        $cx = 0.0;        $cy = 0.0;        for ($i = 0; $i < $n; $i++) {            $cx += $a[2 * $i];            $cy += $a[2 * $i + 1];        }        $cx /= $n;        $cy /= $n;        $pts = [];        for ($i = 0; $i < $n; $i++) {            $px = $a[2 * $i] - $cx;            $py = $a[2 * $i + 1] - $cy;            $d = max(0.001, hypot($px, $py));            $pts[] = (int) (($cx + $px * $narrow + $px / $d * $grow + $dx) * $k);            $pts[] = (int) (($cy + $py + $py / $d * $grow + $dy) * $k);        }        imagefilledpolygon($this->im, $pts, $colour);    }    /** A rounded segment from (x1, y1) radius r1 to (x2, y2) radius r2. */    private function cap(float $x1, float $y1, float $r1, float $x2, float $y2, float $r2, int $colour): void    {        $k = $this->k;        $len = max(0.001, hypot($x2 - $x1, $y2 - $y1));        $nx = -($y2 - $y1) / $len;        $ny = ($x2 - $x1) / $len;        $pts = [($x1 + $nx * $r1) * $k, ($y1 + $ny * $r1) * $k, ($x2 + $nx * $r2) * $k, ($y2 + $ny * $r2) * $k,            ($x2 - $nx * $r2) * $k, ($y2 - $ny * $r2) * $k, ($x1 - $nx * $r1) * $k, ($y1 - $ny * $r1) * $k];        imagefilledpolygon($this->im, array_map('intval', $pts), $colour);        imagefilledellipse($this->im, (int) ($x1 * $k), (int) ($y1 * $k), max(1, (int) (2 * $r1 * $k)), max(1, (int) (2 * $r1 * $k)), $colour);        imagefilledellipse($this->im, (int) ($x2 * $k), (int) ($y2 * $k), max(1, (int) (2 * $r2 * $k)), max(1, (int) (2 * $r2 * $k)), $colour);    }    /** A segment only (flat ends), for cuffs and hems. */    private function band(float $x1, float $y1, float $r1, float $x2, float $y2, float $r2, int $colour): void    {        $k = $this->k;        $len = max(0.001, hypot($x2 - $x1, $y2 - $y1));        $nx = -($y2 - $y1) / $len;        $ny = ($x2 - $x1) / $len;        $pts = [($x1 + $nx * $r1) * $k, ($y1 + $ny * $r1) * $k, ($x2 + $nx * $r2) * $k, ($y2 + $ny * $r2) * $k,            ($x2 - $nx * $r2) * $k, ($y2 - $ny * $r2) * $k, ($x1 - $nx * $r1) * $k, ($y1 - $ny * $r1) * $k];        imagefilledpolygon($this->im, array_map('intval', $pts), $colour);    }    private function line(float $x1, float $y1, float $x2, float $y2, int $colour, float $thick = 0.7): void    {        imagesetthickness($this->im, max(1, (int) round($thick * $this->k)));        imageline($this->im, (int) ($x1 * $this->k), (int) ($y1 * $this->k), (int) ($x2 * $this->k), (int) ($y2 * $this->k), $colour);        imagesetthickness($this->im, 1);    }    private function arc(float $cx, float $cy, float $w, float $h, int $from, int $to, int $colour, float $thick = 0.7): void    {        imagesetthickness($this->im, max(1, (int) round($thick * $this->k)));        imagearc($this->im, (int) ($cx * $this->k), (int) ($cy * $this->k), max(1, (int) ($w * $this->k)), max(1, (int) ($h * $this->k)), $from, $to, $colour);        imagesetthickness($this->im, 1);    }    private function dot(float $x, float $y, float $r, int $colour): void    {        imagefilledellipse($this->im, (int) ($x * $this->k), (int) ($y * $this->k), max(1, (int) (2 * $r * $this->k)), max(1, (int) (2 * $r * $this->k)), $colour);    }    /** Deterministic 0..1 noise for a seed and an index. */    private static function h(int $seed, int $i): float    {        $x = sin($seed * 12.9898 + $i * 78.233) * 43758.5453;        return $x - floor($x);    }    // ---- the figure ---------------------------------------------------------------------------------    /** @param array<string, mixed> $look */    private function draw(array $look): void    {        $view = (string) ($look['view'] ?? 'dressed');        $selected = $look['selected'] ?? null;        $order = ['rleg', 'lleg', 'torso', 'rarm', 'larm', 'head'];        // shadow on the ground, dark outline, a white one around the chosen part        imagefilledellipse($this->im, (int) (50 * $this->k), (int) (193 * $this->k), (int) (48 * $this->k), (int) (7 * $this->k), $this->col([0, 0, 0], 95));        foreach ($order as $part) {            foreach (self::shapes($part) as [$type, $a]) {                $this->shape($type, $a, $this->col([10, 14, 20], 20), 1.4);            }        }        if (is_string($selected)) {            foreach (self::shapes($selected) as [$type, $a]) {                $this->shape($type, $a, $this->col([255, 255, 255], 0), 2.0);            }        }        $this->skin($look, $view === 'medical');        if ($view === 'medical') {            $this->briefs();            $this->wounds((array) ($look['wounds'] ?? []), (array) ($look['cold'] ?? []));        } else {            $this->clothes((array) ($look['clothes'] ?? []));        }        if (is_string($selected)) {            foreach (self::shapes($selected) as [$type, $a]) {                $this->shape($type, $a, $this->col([255, 255, 255], 116));            }        }    }    /** The bare body, shaded, with the face and the muscles; its tint follows the body's state in the medical view. */    private function skin(array $look, bool $medical): void    {        $pale = $medical ? max(0.0, min(1.0, (float) ($look['pale'] ?? 0))) : 0.0;        $fever = $medical ? max(0.0, min(1.0, (float) ($look['fever'] ?? 0))) : 0.0;        $cold = (array) ($look['cold'] ?? []);        $base = self::SKIN;        $base = [$base[0] + (232 - $base[0]) * $pale * 0.6, $base[1] + (214 - $base[1]) * $pale * 0.6, $base[2] + (204 - $base[2]) * $pale * 0.6];        $base = [$base[0] + 20 * $fever, $base[1] - 26 * $fever, $base[2] - 26 * $fever];        foreach (['rleg', 'lleg', 'torso', 'rarm', 'larm', 'head'] as $part) {            $c = $base;            $dark = [$c[0] * 0.7, $c[1] * 0.64, $c[2] * 0.66];            $light = [min(255, $c[0] * 1.08 + 12), min(255, $c[1] * 1.1 + 12), min(255, $c[2] * 1.1 + 12)];            foreach (self::shapes($part) as [$type, $a]) {                $this->shape($type, $a, $this->col($dark));            }            foreach (self::shapes($part) as [$type, $a]) {                $this->shape($type, $a, $this->col($c), -0.9, -0.5, -0.3);            }            foreach (self::shapes($part) as [$type, $a]) {                $this->shape($type, $a, $this->col($light, 92), -0.5, -1.4, -0.6, 0.5);            }        }        // cold hands and feet turn blue-white (medical view)        if ($medical) {            foreach (['hands' => [[16.0, 106.0, 4.4, 7.0], [84.0, 106.0, 4.4, 7.0]], 'feet' => [[39.0, 189.5, 6.6, 4.2], [61.0, 189.5, 6.6, 4.2]]] as $region => $ells) {                $t = (float) ($cold[$region] ?? 30.0);                if ($t < 22) {                    $k = min(1.0, (22 - $t) / 18);                    foreach ($ells as $e) {                        $this->shape('ell', $e, $this->col([120, 170, 255], (int) (120 - 90 * $k)), -0.3);                        if ($t < 8) {                            $this->shape('ell', $e, $this->col([236, 244, 255], 70), -1.6);                        }                    }                }            }        }        $this->anatomy($base, $medical);    }    /** Face, collarbones, chest, belly, knees, fingers: fine lines. */    private function anatomy(array $skin, bool $medical): void    {        $c = fn (float $f, int $alpha) => $this->col([$skin[0] * $f, $skin[1] * $f, $skin[2] * $f], $alpha);        // face: eyes (closed when very weak? no: open), brows, nose, mouth, hair, ears        $this->shape('ell', [45.5, 16.5, 1.6, 1.1], $this->col([250, 250, 250], 10));        $this->shape('ell', [54.5, 16.5, 1.6, 1.1], $this->col([250, 250, 250], 10));        $this->shape('ell', [45.7, 16.6, 0.8, 0.9], $this->col([60, 74, 92], 0));        $this->shape('ell', [54.7, 16.6, 0.8, 0.9], $this->col([60, 74, 92], 0));        $this->line(43.0, 13.6, 47.6, 13.0, $this->col([70, 52, 40], 20), 0.9);        $this->line(52.4, 13.0, 57.0, 13.6, $this->col([70, 52, 40], 20), 0.9);        $this->line(50.0, 17.5, 49.2, 21.4, $c(0.6, 50), 0.6);        $this->arc(50.0, 23.6, 5.6, 2.2, 20, 160, $this->col([150, 80, 72], 30), 0.7);        // hair: dark brown, short, a little untidy after the crash        $hair = $this->col([64, 46, 34], 0);        $this->arc(50.0, 15.5, 21.6, 21.0, 192, 348, $hair, 3.2);        $this->shape('poly', [40.0, 8.5, 46.0, 4.0, 54.0, 3.8, 60.0, 8.5, 57.0, 7.2, 50.0, 6.0, 43.0, 7.2], $hair);        // stubble        $this->arc(50.0, 22.0, 15.0, 10.0, 20, 160, $this->col([90, 70, 60], 100), 1.4);        $this->line(41.0, 38.5, 47.0, 37.0, $c(0.62, 60), 0.7);        $this->line(59.0, 38.5, 53.0, 37.0, $c(0.62, 60), 0.7);        if ($medical) {            $this->arc(41.5, 46.0, 17.0, 14.0, 10, 170, $c(0.66, 70), 0.8);            $this->arc(58.5, 46.0, 17.0, 14.0, 10, 170, $c(0.66, 70), 0.8);            $this->line(50.0, 40.0, 50.0, 90.0, $c(0.7, 80), 0.6);            foreach ([62.0, 70.0, 78.0] as $y) {                $this->line(44.0, $y, 56.0, $y, $c(0.7, 95), 0.5);            }            $this->shape('ell', [50.0, 85.5, 0.9, 1.1], $c(0.55, 50));        }        foreach (['rarm' => 21.5, 'larm' => 78.5] as $x) {            $this->arc($x, 71.0, 5.0, 3.0, 200, 340, $c(0.66, 80), 0.6);        }        foreach ([40.2, 59.8] as $x) {            $this->arc($x, 141.0, 8.0, 7.0, 0, 360, $c(0.68, 90), 0.6);        }        foreach ([16.0, 84.0] as $x) {            for ($i = -1; $i <= 1; $i++) {                $this->line($x + $i * 1.4, 107.5, $x + $i * 1.6, 112.0, $c(0.62, 70), 0.4);            }        }    }    private function briefs(): void    {        $this->shape('poly', [33.0, 92.0, 67.0, 92.0, 67.4, 98.0, 59.0, 108.0, 50.0, 103.0, 41.0, 108.0, 32.6, 98.0], $this->col([70, 80, 96]));        $this->line(33.0, 93.0, 67.0, 93.0, $this->col([110, 124, 140], 20), 1.0);    }    // ---- the medical marks ---------------------------------------------------------------------------    /** @param list<array{0: string, 1: string, 2: float, 3: bool, 4: float, 5: bool, 6: int}> $wounds */    private function wounds(array $wounds, array $cold): void    {        foreach ($wounds as [$part, $kind, $sev, $treated, $infection, $bleeding, $n]) {            [$x, $y] = self::spot($part, $n);            $s = 0.7 + 0.6 * min(1.0, $sev);            if ($infection > 0.1) {                // the hot, swollen ring of an infection                $g = min(1.0, $infection);                $this->dot($x, $y, 7.5 * $s, $this->col([214, 140, 60], (int) (110 - 50 * $g)));                $this->dot($x, $y, 5.2 * $s, $this->col([190, 90, 200], (int) (100 - 50 * $g)));            }            switch ($kind) {                case 'cut':                    $this->line($x - 3.2 * $s, $y - 1.6 * $s, $x + 3.2 * $s, $y + 1.6 * $s, $this->col([120, 10, 16], 0), 1.3);                    $this->line($x - 2.6 * $s, $y - 1.0 * $s, $x + 2.6 * $s, $y + 2.0 * $s, $this->col([220, 70, 70], 30), 0.5);                    break;                case 'bite':                    foreach ([-1, 1] as $side) {                        for ($i = -2; $i <= 2; $i++) {                            $this->dot($x + $i * 1.2 * $s, $y + $side * (2.0 - abs($i) * 0.35) * $s, 0.55 * $s, $this->col([110, 12, 18], 0));                        }                    }                    $this->dot($x, $y, 2.4 * $s, $this->col([200, 60, 60], 90));                    break;                case 'burn':                    $this->dot($x, $y, 3.6 * $s, $this->col([206, 90, 60], 40));                    $this->dot($x + 0.6, $y - 0.4, 2.2 * $s, $this->col([240, 200, 150], 50));                    break;                case 'bruise':                    $this->dot($x, $y, 4.0 * $s, $this->col([110, 70, 140], 60));                    $this->dot($x + 0.5, $y + 0.3, 2.4 * $s, $this->col([90, 50, 120], 50));                    break;                case 'fracture':                    $this->bone($part, $x, $y);                    break;            }            if ($treated) {                if ($kind === 'fracture') {                    $this->splint($part, $y);                } elseif ($kind !== 'bruise') {                    $this->bandage($part, $x, $y);                }            }            if ($bleeding && !$treated) {                $this->line($x, $y + 1.5, $x - 0.4, $y + 6.0, $this->col([170, 16, 22], 20), 1.0);            }        }    }    /** The broken bone seen through the limb: a pale bone line, the break as a red zigzag. */    private function bone(string $part, float $x, float $y): void    {        $leg = $part === 'lleg' || $part === 'rleg';        $bone = $this->col([246, 240, 220], 30);        $len = $leg ? 14.0 : 10.0;        $dx = $part === 'rarm' ? -0.18 : ($part === 'larm' ? 0.18 : 0.0);        $this->line($x - $dx * $len, $y - $len, $x - 0.6, $y - 0.8, $bone, 1.6);        $this->line($x + 0.6, $y + 0.8, $x + $dx * $len, $y + $len, $bone, 1.6);        $red = $this->col([220, 30, 30], 0);        $this->line($x - 2.4, $y - 1.2, $x - 0.6, $y + 0.8, $red, 0.7);        $this->line($x - 0.6, $y + 0.8, $x + 0.8, $y - 1.0, $red, 0.7);        $this->line($x + 0.8, $y - 1.0, $x + 2.4, $y + 1.0, $red, 0.7);    }    private function bandage(string $part, float $x, float $y): void    {        $white = $this->col([244, 242, 236], 0);        $shade = $this->col([180, 176, 168], 20);        $w = match ($part) { 'torso' => 15.0, 'head' => 11.0, 'rleg', 'lleg' => 7.5, default => 5.6 };        if ($part === 'head') {            $this->band($x - $w, $y - 1.0, 2.0, $x + $w, $y - 1.0, 2.0, $white);            $this->line($x - $w, $y - 0.2, $x + $w, $y - 0.2, $shade, 0.4);            return;        }        if ($part === 'torso') {            $this->band(34.0, $y - 2.6, 2.6, 66.0, $y + 1.4, 2.6, $white);            $this->line(34.0, $y - 1.0, 66.0, $y + 3.0, $shade, 0.4);            return;        }        foreach ([-2.2, 0.0, 2.2] as $i => $o) {            $this->band($x - $w * 0.5, $y + $o - 1.2, 1.3, $x + $w * 0.5, $y + $o + 0.4, 1.3, $white);            $this->line($x - $w * 0.5, $y + $o + 0.3, $x + $w * 0.5, $y + $o + 1.6, $shade, 0.35);        }        if (self::h((int) $y, 1) > 0.5) {            $this->dot($x + 0.8, $y, 1.0, $this->col([200, 60, 60], 70));        }    }    private function splint(string $part, float $y): void    {        $wood = $this->col([170, 126, 72], 0);        $dark = $this->col([100, 70, 36], 10);        $leg = $part === 'lleg' || $part === 'rleg';        $cx = self::spot($part, 0)[0];        $len = $leg ? 22.0 : 15.0;        foreach ([-1, 1] as $side) {            $x = $cx + $side * ($leg ? 6.6 : 4.6);            $this->band($x, $y - $len / 2, 0.9, $x + ($part === 'rarm' ? -2.0 : ($part === 'larm' ? 2.0 : 0.0)), $y + $len / 2, 0.9, $wood);            $this->line($x - 0.4, $y - $len / 2, $x - 0.4, $y + $len / 2, $dark, 0.3);        }        foreach ([-0.35, 0.0, 0.35] as $t) {            $yy = $y + $t * $len;            $this->band($cx - ($leg ? 7.6 : 5.6), $yy - 0.6, 0.9, $cx + ($leg ? 7.6 : 5.6), $yy + 0.6, 0.9, $this->col([236, 232, 222], 0));        }    }    // ---- the clothes ----------------------------------------------------------------------------------    /** @param array<string, array{0: string, 1: float, 2: float}> $clothes slot => [id, condition, wet] */    private function clothes(array $clothes): void    {        foreach (['under', 'legs_under', 'worn', 'legs', 'feet', 'hands', 'head'] as $slot) {            if ($slot === 'legs_under') {                // trousers under a coat go first; leggings over the suit after the suit                $g = $clothes['legs'] ?? null;                if ($g !== null && ($clothes['worn'][0] ?? '') === 'coat_fur') {                    $this->garment($g[0], $g[1], $g[2]);                    unset($clothes['legs']);                }                continue;            }            $g = $clothes[$slot] ?? null;            if ($g !== null) {                $this->garment($g[0], $g[1], $g[2]);            }        }        if (!isset($clothes['worn']) && !isset($clothes['under'])) {            $this->briefs();        } elseif (!isset($clothes['worn']) && !isset($clothes['legs'])) {            $this->briefs();        }    }    private function garment(string $id, float $cond, float $wet): void    {        [$cloth, $dark, $trim, $acc] = self::PALETTE[$id] ?? [[110, 100, 90], [70, 64, 56], [80, 70, 60], [140, 130, 120]];        // wet clothes are darker        $w = 1.0 - 0.28 * $wet;        $cloth = [$cloth[0] * $w, $cloth[1] * $w, $cloth[2] * $w];        $dark = [$dark[0] * $w, $dark[1] * $w, $dark[2] * $w];        $C = $this->col($cloth);        $D = $this->col($dark);        $L = $this->col([min(255, $cloth[0] * 1.25 + 18), min(255, $cloth[1] * 1.25 + 18), min(255, $cloth[2] * 1.25 + 18)], 80);        $T = $this->col($trim);        $A = $this->col($acc);        $fold = $this->col($dark, 40);        $seed = crc32($id) % 997;        $regions = [];        switch ($id) {            case 'flight_suit_torn':                $this->torso($C, $D, $L, 0.7, 103.0);                $this->sleeves($C, $D, $L, 97.0, 0.8);                $this->trouserLegs($C, $D, $L, 180.0, 0.9);                // zip, collar, belt, chest pocket, the mission patch, orange stripes, knee pads                $this->line(50.0, 31.0, 50.0, 100.0, $this->col([30, 34, 40], 10), 0.8);                $this->line(50.6, 31.0, 50.6, 100.0, $this->col([200, 204, 210], 60), 0.3);                $this->shape('poly', [44.0, 29.0, 50.0, 37.0, 46.0, 38.0, 41.0, 32.0], $D);                $this->shape('poly', [56.0, 29.0, 50.0, 37.0, 54.0, 38.0, 59.0, 32.0], $D);                $this->shape('poly', [32.8, 88.0, 67.2, 88.0, 67.6, 92.6, 32.4, 92.6], $this->col([34, 38, 44], 0));                $this->shape('poly', [48.0, 88.6, 52.0, 88.6, 52.0, 92.0, 48.0, 92.0], $this->col([190, 192, 196], 0));                $this->shape('poly', [36.0, 48.0, 44.5, 48.0, 44.5, 56.0, 36.0, 56.0], $D);                $this->line(36.0, 49.5, 44.5, 49.5, $fold, 0.5);                $this->dot(60.5, 47.0, 3.4, $T);                $this->dot(60.5, 47.0, 2.0, $this->col([40, 60, 110], 0));                $this->dot(60.9, 46.4, 0.7, $this->col([240, 240, 255], 0));                foreach ([[27.0, 46.0, 24.0, 60.0], [73.0, 46.0, 76.0, 60.0]] as [$x1, $y1, $x2, $y2]) {                    $this->line($x1, $y1, $x2, $y2, $T, 1.3);                }                foreach ([40.2, 59.8] as $x) {                    $this->shape('ell', [$x, 141.0, 5.4, 4.4], $D);                    $this->arc($x, 141.0, 9.0, 7.0, 200, 340, $L, 0.5);                }                $this->line(44.0, 104.0, 41.0, 112.0, $fold, 0.6);                $this->line(56.0, 104.0, 59.0, 112.0, $fold, 0.6);                $regions = [[38, 64], [62, 75], [24, 60], [45, 118], [57, 150], [76, 84], [40, 160], [60, 120], [30, 80]];                break;            case 'undershirt':            case 'tunic_fiber':                $this->torso($C, $D, $L, 0.3, $id === 'tunic_fiber' ? 104.0 : 96.0);                if ($id === 'undershirt') {                    $this->sleeves($C, $D, $L, 92.0, 0.4);                    foreach ([46.0, 58.0, 70.0, 82.0] as $y) {                        $this->line(35.0, $y, 65.0, $y, $fold, 0.3);                    }                } else {                    for ($i = 0; $i < 9; $i++) {                        $x = 36.0 + $i * 3.5;                        $this->line($x, 38.0, $x + 0.5, 102.0, $fold, 0.35);                    }                    $this->line(32.0, 96.0, 68.0, 96.0, $this->col([110, 80, 40], 0), 1.1);                }                $regions = [[42, 70], [58, 82]];                break;            case 'rags':                $this->torso($C, $D, $L, 0.5, 100.0);                $this->sleeves($C, $D, $L, 78.0, 0.6);                for ($i = 0; $i < 9; $i++) {                    $x = 33.0 + $i * 4.2;                    $this->shape('poly', [$x, 99.0, $x + 3.0, 99.0, $x + 1.4, 103.0 + 4.0 * self::h($seed, $i)], $C);                }                $this->line(36.0, 60.0, 64.0, 66.0, $fold, 0.6);                $regions = [[40, 58], [60, 80], [24, 62], [76, 66]];                break;            case 'jacket_hide':                $this->torso($C, $D, $L, 1.1, 104.0);                $this->sleeves($C, $D, $L, 90.0, 1.2);                // laced front, a collar, fringes, stitched seams                $this->line(50.0, 36.0, 50.0, 104.0, $D, 1.0);                for ($y = 44.0; $y < 100.0; $y += 7.0) {                    $this->line(47.6, $y, 52.4, $y + 3.0, $T, 0.5);                    $this->line(52.4, $y, 47.6, $y + 3.0, $T, 0.5);                }                $this->shape('poly', [42.0, 30.0, 50.0, 38.0, 44.0, 41.0, 38.0, 34.0], $D);                $this->shape('poly', [58.0, 30.0, 50.0, 38.0, 56.0, 41.0, 62.0, 34.0], $D);                for ($i = 0; $i < 12; $i++) {                    $x = 33.0 + $i * 3.0;                    $this->line($x, 103.0, $x - 0.3, 108.0, $A, 0.6);                }                $this->line(31.0, 50.0, 31.0, 96.0, $fold, 0.4);                $this->line(69.0, 50.0, 69.0, 96.0, $fold, 0.4);                $regions = [[38, 62], [62, 72], [24, 62], [76, 70]];                break;            case 'coat_fur':                $this->torso($C, $D, $L, 1.6, 104.0);                $this->sleeves($C, $D, $L, 96.0, 1.8);                // the skirt of the coat down to the knees                $this->shape('poly', [32.0, 96.0, 68.0, 96.0, 72.0, 138.0, 51.5, 140.0, 50.0, 130.0, 48.5, 140.0, 28.0, 138.0], $D, 0.0);                $this->shape('poly', [32.4, 96.0, 67.6, 96.0, 71.0, 137.0, 51.0, 139.0, 50.0, 129.0, 49.0, 139.0, 29.0, 137.0], $C, -0.3, -0.3);                $this->line(50.0, 36.0, 50.0, 130.0, $D, 1.0);                // fur: collar, cuffs and hem as tufts                $fur = $this->col($trim);                $furD = $this->col([$trim[0] * 0.8, $trim[1] * 0.8, $trim[2] * 0.8]);                for ($i = 0; $i < 13; $i++) {                    $a = M_PI * (0.08 + 0.84 * $i / 12);                    $this->dot(50.0 - cos($a) * 12.0, 33.0 + sin($a) * 4.0, 2.4, $i % 2 ? $fur : $furD);                }                foreach ([[17.0, 96.0], [83.0, 96.0]] as [$x, $y]) {                    for ($i = 0; $i < 4; $i++) {                        $this->dot($x - 3.0 + $i * 2.0, $y, 2.0, $i % 2 ? $fur : $furD);                    }                }                for ($i = 0; $i < 16; $i++) {                    $x = 29.0 + $i * 2.8;                    $this->dot($x, 138.0 + ($i % 3) * 0.6, 1.9, $i % 2 ? $fur : $furD);                }                foreach ([[40.0, 52.0], [60.0, 52.0]] as [$x, $y]) {                    $this->shape('ell', [$x, $y + 26.0, 3.0, 2.2], $D);                }                $regions = [[38, 60], [62, 78], [24, 62], [76, 66], [40, 120], [60, 128]];                break;            case 'trousers_hide':            case 'leggings_fur':                if ($id === 'trousers_hide') {                    $this->shape('poly', [33.0, 92.0, 67.0, 92.0, 67.4, 100.0, 50.0, 104.0, 32.6, 100.0], $C);                    $this->trouserLegs($C, $D, $L, 182.0, 1.1);                    $this->line(33.0, 93.0, 67.0, 93.0, $this->col([60, 40, 24], 0), 1.4);                    foreach ([40.2, 59.8] as $x) {                        $this->line($x - 3.4, 104.0, $x - 2.4, 178.0, $fold, 0.45);                    }                } else {                    foreach ([40.3, 59.7] as $x) {                        $this->cap($x, 146.0, 6.6, $x, 182.0, 4.8, $D);                        $this->cap($x - 0.3, 146.0, 6.0, $x - 0.3, 181.0, 4.2, $C);                        for ($y = 150.0; $y < 180.0; $y += 6.0) {                            $this->line($x - 5.0, $y, $x + 5.0, $y + 2.5, $T, 0.6);                        }                        for ($i = 0; $i < 6; $i++) {                            $this->dot($x - 5.0 + $i * 2.0, 146.0, 1.6, $A);                        }                    }                }                $regions = [[41, 120], [59, 160], [40, 150]];                break;            case 'gloves_pilot':            case 'gloves_hide':            case 'mittens_fur':                foreach ([16.0, 84.0] as $x) {                    $mit = $id === 'mittens_fur';                    $this->shape('ell', [$x, 106.0, $mit ? 5.4 : 4.6, $mit ? 7.6 : 7.0], $D, 0.6);                    $this->shape('ell', [$x, 106.0, $mit ? 5.0 : 4.2, $mit ? 7.2 : 6.6], $C);                    $this->band($x - ($x < 50 ? -0.4 : 0.4), 98.0, 3.8, $x, 101.0, 4.4, $T);                    if (!$mit) {                        for ($i = -1; $i <= 1; $i++) {                            $this->line($x + $i * 1.4, 108.0, $x + $i * 1.6, 112.0, $D, 0.4);                        }                    } else {                        $this->dot($x + ($x < 50 ? 3.2 : -3.2), 103.0, 1.8, $C);                    }                }                break;            case 'pilot_boots_worn':            case 'boots':                foreach ([40.6, 59.4] as $x) {                    $this->cap($x, 168.0, 4.9, $x, 184.0, 4.4, $D);                    $this->cap($x, 168.0, 4.5, $x, 184.0, 4.0, $C);                    $this->shape('ell', [$x < 50 ? $x - 1.6 : $x + 1.6, 189.5, 6.9, 4.5], $C);                    $this->line($x - 4.5, 168.0, $x + 4.5, 168.0, $T, 1.0);                    $this->line($x - 6.0, 192.6, $x + 6.0, 192.6, $this->col([18, 16, 14], 0), 1.2);                    $this->line($x - 3.0, 176.0, $x + 3.0, 177.0, $fold, 0.4);                    if ($id === 'pilot_boots_worn') {                        $this->line($x - 3.4, 172.0, $x + 3.4, 172.0, $A, 0.5);                    } else {                        for ($y = 171.0; $y < 184.0; $y += 4.0) {                            $this->line($x - 2.6, $y, $x + 2.6, $y + 1.5, $T, 0.5);                        }                    }                }                $regions = [[40, 178], [60, 182]];                break;            case 'helmet_pilot':                $this->shape('ell', [50.0, 13.5, 13.4, 13.0], $D);                $this->shape('ell', [49.6, 13.0, 12.8, 12.4], $C);                $this->shape('poly', [37.0, 14.0, 63.0, 14.0, 62.0, 20.0, 38.0, 20.0], $this->col([50, 70, 92], 30));                $this->line(38.0, 14.2, 62.0, 14.2, $T, 1.1);                $this->line(44.0, 3.0, 47.0, 8.0, $this->col([60, 60, 66], 0), 0.6);                $this->line(47.0, 8.0, 45.6, 11.0, $this->col([60, 60, 66], 0), 0.6);                $this->shape('ell', [45.0, 6.0, 3.0, 1.6], $L);                break;            case 'cap_hide':            case 'hat_fur':                if ($id === 'cap_hide') {                    $this->shape('ell', [50.0, 9.5, 12.0, 7.6], $D);                    $this->shape('ell', [49.8, 9.2, 11.4, 7.0], $C);                    $this->band(38.5, 12.0, 1.6, 61.5, 12.0, 1.6, $T);                    $this->line(50.0, 2.6, 50.0, 12.0, $fold, 0.5);                } else {                    $this->shape('ell', [50.0, 9.0, 13.4, 8.6], $D);                    $this->shape('ell', [49.8, 8.6, 12.8, 8.0], $C);                    foreach ([38.0, 62.0] as $x) {                        $this->shape('ell', [$x, 19.0, 3.0, 6.2], $C);                        $this->line($x, 24.0, $x, 28.0, $T, 0.5);                    }                    for ($i = 0; $i < 12; $i++) {                        $this->dot(37.0 + $i * 2.4, 13.0 + ($i % 2) * 0.5, 1.9, $this->col($acc));                    }                }                break;        }        $this->wear($regions, $cond, $seed, $cloth);        if ($wet > 0.3) {            // drops beading on a soaked garment            foreach ($regions as $i => [$x, $y]) {                if (self::h($seed, $i + 40) < $wet) {                    $this->dot($x + 2.0, $y + 4.0, 0.8, $this->col([200, 220, 240], 50));                }            }        }    }    /** The torso of a garment (grown by $grow) down to $bottom. */    private function torso(int $C, int $D, int $L, float $grow, float $bottom): void    {        $pts = self::TORSO;        for ($i = 1; $i < count($pts); $i += 2) {            $pts[$i] = min($pts[$i], $bottom);        }        $this->shape('poly', $pts, $D, $grow + 0.6);        $this->shape('poly', $pts, $C, $grow - 0.3, -0.3, -0.2);        $this->shape('poly', $pts, $L, $grow - 2.6, -1.6, -0.8, 0.45);    }    /** Both sleeves to $end (y of the wrist end). */    private function sleeves(int $C, int $D, int $L, float $end, float $grow): void    {        foreach ([1, -1] as $s) {            $x0 = $s > 0 ? 27.0 : 73.0;            $x1 = $s > 0 ? 21.5 : 78.5;            $x2 = $s > 0 ? 17.0 : 83.0;            $t = max(0.0, min(1.0, ($end - 71.0) / 28.0));            $xe = $x1 + ($x2 - $x1) * $t;            $re = 4.8 - 1.4 * $t;            foreach ([[$D, 0.6], [$C, 0.0]] as [$c, $g]) {                $this->cap($x0, 42.0, 6.4 + $grow + $g, $x1, 71.0, 4.8 + $grow + $g, $c);                $this->band($x1, 71.0, 4.8 + $grow + $g, $xe, $end, $re + $grow + $g, $c);            }            $this->line($x0 - $s * 1.5, 46.0, $x1 - $s * 1.8, 68.0, $L, 0.9);            // folds at the elbow, the cuff            $this->line($x1 - 3.0, 70.0, $x1 + 1.0, 72.5, $D, 0.45);            $this->line($x1 - 2.4, 73.5, $x1 + 2.0, 74.5, $D, 0.4);            $this->line($xe - $re - $grow, $end, $xe + $re + $grow, $end, $D, 1.0);        }    }    /** Both trouser legs from the hips to $end. */    private function trouserLegs(int $C, int $D, int $L, float $end, float $grow): void    {        foreach ([41.0, 59.0] as $x) {            $xk = $x < 50 ? 40.2 : 59.8;            $xa = $x < 50 ? 40.6 : 59.4;            $t = max(0.0, min(1.0, ($end - 142.0) / 42.0));            foreach ([[$D, 0.6], [$C, 0.0]] as [$c, $g]) {                $this->cap($x, 99.0, 9.6 + $grow + $g, $xk, 142.0, 5.8 + $grow + $g, $c);                $this->band($xk, 142.0, 5.8 + $grow + $g, $xk + ($xa - $xk) * $t, $end, 5.8 - 2.2 * $t + $grow + $g, $c);            }            $this->line($x - 4.0 * ($x < 50 ? 1 : -1), 104.0, $xk - 3.0 * ($x < 50 ? 1 : -1), 138.0, $L, 0.9);            $this->line($xk - 3.0, 145.0, $xk + 2.0, 147.0, $D, 0.4);        }        $this->shape('poly', [34.0, 94.0, 66.0, 94.0, 64.0, 103.0, 50.0, 106.0, 36.0, 103.0], $C);    }    /**     * Wear and tear: dirt stains, then holes with frayed edges that show the skin under them, more as the condition     * falls (good: none; worn: a few; in shreds: many).     * @param list<array{0: float, 1: float}> $regions     */    private function wear(array $regions, float $cond, int $seed, array $cloth): void    {        if ($regions === []) {            return;        }        $dirt = (int) round((100 - $cond) / 14);        for ($i = 0; $i < $dirt; $i++) {            [$x, $y] = $regions[$i % count($regions)];            $x += (self::h($seed, $i) - 0.5) * 8;            $y += (self::h($seed, $i + 9) - 0.5) * 10;            $this->dot($x, $y, 2.0 + 2.5 * self::h($seed, $i + 3), $this->col([88, 70, 46], 92));        }        $holes = $cond < 20 ? 7 : ($cond < 45 ? 4 : ($cond < 70 ? 2 : 0));        for ($i = 0; $i < min($holes, count($regions)); $i++) {            [$x, $y] = $regions[$i];            $r = 1.4 + 1.6 * self::h($seed, $i + 20) + ($cond < 20 ? 0.8 : 0.0);            $pts = [];            for ($k = 0; $k < 9; $k++) {                $a = $k / 9 * 6.2832;                $rr = $r * (0.6 + 0.7 * self::h($seed * 3 + $i, $k));                $pts[] = $x + cos($a) * $rr * 1.3;                $pts[] = $y + sin($a) * $rr;            }            $this->shape('poly', $pts, $this->col([30, 24, 20], 0), 0.5);            $this->shape('poly', $pts, $this->col([196, 150, 120], 0), -0.3);            // frayed threads            for ($k = 0; $k < 4; $k++) {                $a = self::h($seed, $i * 7 + $k) * 6.2832;                $this->line($x + cos($a) * $r * 1.2, $y + sin($a) * $r, $x + cos($a) * ($r * 1.2 + 1.6), $y + sin($a) * ($r + 1.4),                    $this->col([$cloth[0] * 1.2, $cloth[1] * 1.2, $cloth[2] * 1.2], 20), 0.35);            }        }    }}