NL
games/rob-revisited / src / Revisited / Ui / Screens.php · commit f9a1307
<?phpnamespace App\Revisited\Ui;use App\Revisited\I18n\Translator;use App\Revisited\Land\Chests;use App\Revisited\Land\Game;use App\Revisited\Land\Quest;use App\Revisited\Land\ZoneFactory;use App\Revisited\Sim\Body;use App\Revisited\Sim\Crafting;use App\Revisited\Sim\Inventory;use App\Revisited\Sim\Items;use JumPHP\Renderer\Sdl;/** * The tabbed screens over the game (paused behind): Inventaire, Artisanat, Corps, Carte, Journal. Keys / pad: arrows * to choose, Enter (A) to use / craft / treat, X to drop, 1-4 to put an item in a quick slot, PageUp / PageDown (LB / RB) * or the tab's own key to change tab, Escape (B) to close. Mouse and touch: click the tabs, cells, rows and buttons. */final class Screens{    use PhoneScreens;    public const TABS = ['inventory' => 'Inventaire', 'craft' => 'Artisanat', 'body' => 'Corps', 'talents' => 'Talents', 'scanner' => 'Scanner', 'map' => 'Carte', 'journal' => 'Journal'];    private const KEYS = ['inventory' => 'I', 'craft' => 'K', 'body' => 'B', 'talents' => 'L', 'scanner' => 'N', 'map' => 'M', 'journal' => 'J'];    private const KIND = [        'material' => 'Matériau', 'food' => 'Nourriture', 'drink' => 'Boisson', 'tool' => 'Outil', 'weapon' => 'Arme',        'clothing' => 'Vêtement', 'medical' => 'Soin', 'quest' => 'Objet de quête', 'trade' => 'Marchandise', 'misc' => 'Objet',    ];    private const KIND_ORDER = ['weapon' => 0, 'tool' => 1, 'clothing' => 2, 'food' => 3, 'drink' => 4, 'medical' => 5, 'quest' => 6, 'material' => 7, 'trade' => 8];    private const PARTS = BodyPanel::PARTS;    private const COLS = 6;    private const CELL = 74.0;    /** rows of cells the desktop inventory shows */    private const INV_ROWS = 6;    public string $tab = 'inventory';    public int $invSel = 0;    public int $craftSel = 0;    public int $part = 1;    public int $treatSel = 0;    /** the body tab's view: dressed | medical */    public string $bodyView = 'dressed';    /** the scanner tab's page: radar | gps */    public string $scanPage = 'radar';    private int $craftTop = 0;    /** the desktop inventory: its first row of cells shown */    public int $invTop = 0;    private Wheel $wheel;    private string $note = '';    private float $noteAge = 0.0;    /** @var list<string> */    private array $items = [];    /** @var list<array> */    private array $recipes = [];    private BodyPanel $bodyPanel;    public TalentsView $talentsView;    private ScannerView $scanner;    private float $time = 0.0;    /** the scanner has battery (it drains while its tab is open) */    private bool $scanOn = true;    /** touch screens: the scroll of a list by dragging */    public bool $touch = false;    public function __construct(private MapView $map, ?Sdl $sdl = null)    {        $this->bodyPanel = new BodyPanel($sdl);        $this->talentsView = new TalentsView();        $this->wheel = new Wheel();        $this->scanner = new ScannerView();    }    public function open(string $tab): void    {        $this->tab = isset(self::TABS[$tab]) ? $tab : 'inventory';        $this->note = '';        $this->pDetail = null;        $this->wheel->reset();    }    /** @return list<string> the bag's items, sorted by kind then name */    private function itemList(Game $g): array    {        $ids = array_keys($g->inv->all());        usort($ids, static function (string $a, string $b): int {            $ka = self::KIND_ORDER[Items::get($a)['kind']] ?? 9;            $kb = self::KIND_ORDER[Items::get($b)['kind']] ?? 9;            return $ka <=> $kb ?: strcmp(Items::name($a), Items::name($b));        });        return $ids;    }    /** @return list<array> the recipes, possible ones first */    private function recipeList(Game $g): array    {        $near = $g->nearFire();        $ok = [];        $no = [];        foreach (Crafting::recipes() as $r) {            if (self::can($r, $g, $near)) {                $ok[] = $r;            } else {                $no[] = $r;            }        }        return array_merge($ok, $no);    }    private static function can(array $r, Game $g, bool $near): bool    {        foreach ($r['needs'] as $id => $n) {            if (!$g->inv->has(Crafting::source($id, $n, $g->inv), $n)) {                return false;            }        }        return Crafting::hasTool($r, $g->inv) && (!$r['fire'] || $near);    }    private function say(string $text): void    {        $this->note = $text;        $this->noteAge = 0.0;    }    /** Returns false when the screens must close. */    public function update(UiInput $in, Game $g, HudData $d, float $dt): bool    {        $this->noteAge += $dt;        $this->time += $dt;        if ($this->tab === 'scanner') {            $this->scanOn = $g->useScanner($dt);        }        if ($this->touch) {            $this->items = $this->itemList($g);            return $this->phoneUpdate($in, $g, $d);        }        if ($in->back) {            return false;        }        $tabs = array_keys(self::TABS);        $at = (int) array_search($this->tab, $tabs, true);        if ($in->prev || $in->next) {            $this->open($tabs[($at + ($in->next ? 1 : -1) + count($tabs)) % count($tabs)]);        }        if ($in->click && $in->hit !== null) {            if (str_starts_with($in->hit, 'tab:')) {                $this->open(substr($in->hit, 4));                return true;            }            if ($in->hit === 'close') {                return false;            }            if ($this->gpsClick($in->hit, $g)) {                return true;            }        }        $this->items = $this->itemList($g);        return match ($this->tab) {            'inventory' => $this->updateInventory($in, $g, $d),            'craft' => $this->updateCraft($in, $g),            'body' => $this->updateBody($in, $g),            'talents' => $this->talentsView->update($in, $g, fn (string $t) => $this->say($t)),            'scanner' => $this->updateScanner($in),            default => true,        };    }    private function updateScanner(UiInput $in): bool    {        if ($in->left || $in->right || $in->ok) {            $this->scanPage = $this->scanPage === 'radar' ? 'gps' : 'radar';        }        return true;    }    /** The GPS buttons (scanner's GPS page, the journal, the inventory): true when the click was one of them. */    private function gpsClick(string $hit, Game $g): bool    {        if (str_starts_with($hit, 'scan:')) {            $this->scanPage = substr($hit, 5) === 'gps' ? 'gps' : 'radar';            return true;        }        if (str_starts_with($hit, 'gpslog:')) {            $this->say($g->insertLog(substr($hit, 7)));            return true;        }        if (str_starts_with($hit, 'wp:')) {            $id = substr($hit, 3);            $g->setActiveWaypoint($g->activeWaypoint === $id ? null : $id);            $this->say($g->activeWaypoint === null ? 'GPS : plus de point actif.' : 'GPS : la boussole montre « ' . $g->waypoints[$id][0] . ' ».');            return true;        }        if (str_starts_with($hit, 'wpdel:')) {            $g->removeWaypoint(substr($hit, 6));            $this->say('GPS : point effacé.');            return true;        }        if ($hit === 'mark') {            $this->say($g->markHere());            return true;        }        return false;    }    /** Opens the body tab in one of its views, on a part. */    public function openBody(string $view, ?string $part = null): void    {        $this->open('body');        $this->bodyView = isset(BodyPanel::VIEWS[$view]) ? $view : 'dressed';        if ($part !== null) {            $k = array_search($part, array_keys(self::PARTS), true);            $this->part = $k === false ? $this->part : (int) $k;            if ($this->touch && $k !== false) {                $this->pDetail = $part;      // phones: straight to the part's page            }        }    }    private function updateInventory(UiInput $in, Game $g, HudData $d): bool    {        $n = count($this->items);        if ($n === 0) {            return true;        }        $this->invSel = max(0, min($this->invSel, $n - 1));        if ($in->left) {            $this->invSel = max(0, $this->invSel - 1);        }        if ($in->right) {            $this->invSel = min($n - 1, $this->invSel + 1);        }        if ($in->up && $this->invSel >= self::COLS) {            $this->invSel -= self::COLS;        }        if ($in->down && $this->invSel + self::COLS < $n) {            $this->invSel += self::COLS;        }        $rows = (int) ceil($n / self::COLS);        $steps = $this->wheel->steps($in->wheel);        if ($steps !== 0) {            // the wheel scrolls the grid; the selection is pulled into the rows shown            $this->invTop = Wheel::clamp($this->invTop + $steps, $rows, self::INV_ROWS);            $row = max($this->invTop, min($this->invTop + self::INV_ROWS - 1, intdiv($this->invSel, self::COLS)));            $this->invSel = min($n - 1, $row * self::COLS + $this->invSel % self::COLS);        } elseif ($in->left || $in->right || $in->up || $in->down) {            $row = intdiv($this->invSel, self::COLS);            $this->invTop = $row < $this->invTop ? $row : ($row >= $this->invTop + self::INV_ROWS ? $row - self::INV_ROWS + 1 : $this->invTop);        }        $this->invTop = Wheel::clamp($this->invTop, $rows, self::INV_ROWS);        $use = $in->ok;        if ($in->click && $in->hit !== null) {            if (str_starts_with($in->hit, 'inv:')) {                // a click selects, a second click on the same cell uses it                $i = min($n - 1, (int) substr($in->hit, 4));                $use = $i === $this->invSel;                $this->invSel = $i;            } elseif ($in->hit === 'use') {                $use = true;            } elseif ($in->hit === 'drop') {                $in->drop = true;            } elseif (str_starts_with($in->hit, 'quick:')) {                $in->digit = (int) substr($in->hit, 6);            } elseif (str_starts_with($in->hit, 'unequip:')) {                $this->say($g->unequip(substr($in->hit, 8)));            } elseif (str_starts_with($in->hit, 'equip:')) {                $this->say($this->toggleClothes($g, substr($in->hit, 6)));            }        }        $id = $this->items[$this->invSel] ?? null;        if ($id === null) {            return true;        }        if ($in->digit >= 1 && $in->digit <= 4) {            foreach ($d->slots as $k => $s) {                if ($s === $id) {                    $d->slots[$k] = null;                }            }            $d->slots[$in->digit] = $id;            $this->say(Items::name($id) . ' → raccourci ' . $in->digit . '.');        }        if ($in->drop) {            if ($g->dropBag($id, 1) > 0) {                $g->say('Tu poses : ' . Items::name($id) . '.');                $this->say('Tu poses : ' . Items::name($id) . '.');                if (!$g->inv->has($id)) {                    foreach ($d->slots as $k => $s) {                        if ($s === $id) {                            $d->slots[$k] = null;      // nothing left of it: off the quick slots too                        }                    }                }            }        }        if ($use) {            $item = Items::get($id);            if (self::isLocalCare($id)) {                // a bandage, a splint, a poultice: choose the part on the body                $this->open('body');                $this->selectTreatment($g, $id);                return true;            }            if (in_array($item['kind'], ['material', 'trade'], true) && !isset($item['damage']) && $id !== 'sand') {                $this->say('Il n\'y a rien à faire de cela directement : c\'est pour l\'artisanat ou le troc.');                return true;            }            if ($id === 'scanner') {                $this->open('scanner');                return true;            }            if ($g->inv->heldSlot($id) !== null || $g->inv->isWorn($id) !== null) {                $this->say($g->unequip($g->inv->heldSlot($id) ?? (string) $g->inv->isWorn($id)));                return true;            }            $this->say($g->useItem($id));            if (in_array($id, ['campfire_kit', 'shelter_kit', 'trap_snare'], true)) {                return false;            }        }        return true;    }    public static function isLocalCare(string $id): bool    {        $heal = Items::get($id)['heal'] ?? [];        return isset($heal['bleed']) || isset($heal['fracture']) || isset($heal['infection']);    }    /** @return list<string> the care items in the bag */    private function cares(Game $g): array    {        $out = [];        foreach ($this->items as $id) {            if (Items::get($id)['kind'] === 'medical') {                $out[] = $id;            }        }        return $out;    }    private function selectTreatment(Game $g, string $id): void    {        $this->bodyView = 'medical';        $this->items = $this->itemList($g);        $k = array_search($id, $this->cares($g), true);        $this->treatSel = $k === false ? 0 : (int) $k;        // the worst injured part first        $worst = null;        foreach ($g->body->injuries() as $w) {            if (!$w['treated'] && ($worst === null || $w['severity'] > $worst['severity'])) {                $worst = $w;            }        }        if ($worst !== null) {            $this->part = (int) array_search($worst['part'], array_keys(self::PARTS), true);        }    }    private function updateCraft(UiInput $in, Game $g): bool    {        $this->recipes = $this->recipeList($g);        $n = count($this->recipes);        if ($in->up) {            $this->craftSel = max(0, $this->craftSel - 1);        }        if ($in->down) {            $this->craftSel = min($n - 1, $this->craftSel + 1);        }        $steps = $this->wheel->steps($in->wheel);        if ($steps !== 0) {            // the wheel moves the view; the selection follows into it (the view never snaps back to the selection)            $this->craftTop = Wheel::clamp($this->craftTop + $steps, $n, 12);            $this->craftSel = max($this->craftTop, min($this->craftTop + 11, $this->craftSel));        }        $make = $in->ok;        if ($in->click && $in->hit !== null) {            if (str_starts_with($in->hit, 'rec:')) {                $this->craftSel = (int) substr($in->hit, 4);            } elseif ($in->hit === 'craft') {                $make = true;            }        }        $this->craftSel = max(0, min($this->craftSel, $n - 1));        if ($this->craftSel < $this->craftTop) {            $this->craftTop = $this->craftSel;        } elseif ($this->craftSel >= $this->craftTop + 12) {            $this->craftTop = $this->craftSel - 11;        }        $this->craftTop = Wheel::clamp($this->craftTop, $n, 12);        if ($make && isset($this->recipes[$this->craftSel])) {            $id = $this->recipes[$this->craftSel]['id'];            $this->say($g->craft($id));            // keep the same recipe selected            $this->recipes = $this->recipeList($g);            foreach ($this->recipes as $i => $r) {                if ($r['id'] === $id) {                    $this->craftSel = $i;                }            }        }        return true;    }    private function updateBody(UiInput $in, Game $g): bool    {        $parts = array_keys(self::PARTS);        $cares = $this->cares($g);        if ($in->up) {            $this->part = ($this->part + 5) % 6;        }        if ($in->down) {            $this->part = ($this->part + 1) % 6;        }        if ($this->bodyView === 'medical' && $in->left && $cares !== []) {            $this->treatSel = ($this->treatSel - 1 + count($cares)) % count($cares);        }        if ($this->bodyView === 'medical' && $in->right && $cares !== []) {            $this->treatSel = ($this->treatSel + 1) % count($cares);        }        if ($this->bodyView === 'dressed' && ($in->left || $in->right)) {            $this->bodyView = 'medical';        } elseif ($in->drop) {            $this->bodyView = $this->bodyView === 'medical' ? 'dressed' : 'medical';        }        $apply = $in->ok && $this->bodyView === 'medical';        if ($in->click && $in->hit !== null) {            $hit = $in->hit;            if (str_starts_with($hit, 'part:')) {                $this->part = (int) array_search(substr($hit, 5), $parts, true);            } elseif (str_starts_with($hit, 'view:')) {                $this->bodyView = substr($hit, 5) === 'medical' ? 'medical' : 'dressed';            } elseif (str_starts_with($hit, 'care:')) {                $i = (int) substr($hit, 5);                $apply = $i === $this->treatSel;                $this->treatSel = $i;            } elseif ($hit === 'treat') {                $apply = true;            } elseif (str_starts_with($hit, 'cloth:')) {                $this->say($this->toggleClothes($g, substr($hit, 6)));                return true;            } elseif (str_starts_with($hit, 'wear:')) {                $this->say($g->useItem(substr($hit, 5)));                return true;            } elseif ($hit === 'mend') {                $this->say($g->useItem('patch'));                return true;            }        }        $this->treatSel = max(0, min($this->treatSel, count($cares) - 1));        if ($apply && $cares !== []) {            $this->say($g->useItem($cares[$this->treatSel], $parts[$this->part]));        }        return true;    }    /** A clothing slot clicked: takes off what is there, or puts on the warmest garment of that slot in the bag. */    private function toggleClothes(Game $g, string $slot): string    {        if (!in_array($slot, Inventory::CLOTH_SLOTS, true)) {            return '';        }        if ($g->inv->$slot !== null) {            return $g->unequip($slot);        }        $best = BodyPanel::spare($g, $slot);        return $best === null ? 'Aucun vêtement de ce genre dans ton sac.' : $g->useItem($best);    }    // ------------------------------------------------------------------------------------------ drawing    public function draw(Canvas $c, Game $g, HudData $d): void    {        if ($this->touch) {            $this->items = $this->itemList($g);            $this->phoneDraw($c, $g, $d);            return;        }        $c->rect(0, 0, Canvas::W, Canvas::H, [4, 8, 12, 120]);        $c->panel(36, 58, 888, 610, 214);        $x = 50.0;        foreach (self::TABS as $id => $label) {            $on = $id === $this->tab;            $w = $c->measure($label, 16) + 38;            if ($on) {                $c->rect($x, 68, $w, 34, [120, 190, 255, 40]);                $c->rect($x, 100, $w, 2, [150, 210, 255, 230]);            }            $c->text($label, $x + 12, 76, 16, $on ? [255, 255, 255] : [166, 180, 194]);            $c->text(self::KEYS[$id], $x + $w - 10, 78, 11, [120, 136, 150], 'right', false);            $c->hit($x, 66, $w, 38, 'tab:' . $id);            $x += $w + 4;        }        $c->line(892, 76, 908, 92, [210, 220, 230], 2);        $c->line(908, 76, 892, 92, [210, 220, 230], 2);        $c->hit(880, 64, 40, 40, 'close');        $c->rect(50, 106, 860, 1, [255, 255, 255, 40]);        $this->items = $this->itemList($g);        match ($this->tab) {            'inventory' => $this->drawInventory($c, $g, $d),            'craft' => $this->drawCraft($c, $g),            'body' => $this->drawBody($c, $g),            'talents' => $this->talentsView->draw($c, $g, $this->time),            'scanner' => $this->scanPage === 'gps' ? $this->scanner->drawGps($c, $g, $this->scanOn) : $this->scanner->draw($c, $g, $d, $this->time, $this->scanOn),            'map' => $this->drawMap($c, $g, $d),            default => $this->drawJournal($c, $g),        };        if ($this->note !== '' && $this->noteAge < 6) {            $lines = $c->wrap($this->note, 14, 820);            $y = 664 - 8 - 18 * count($lines);            $c->rect(50, $y - 4, 860, 18 * count($lines) + 8, [0, 0, 0, 110]);            foreach ($lines as $i => $line) {                $c->text($line, 60, $y + $i * 18, 14, [255, 226, 160]);            }        }        if ($this->tab === 'scanner' && $g->inv->has('scanner')) {            // its two pages: top left of the content (the tab row is full)            $x = 56.0;            foreach (['radar' => 'Radar', 'gps' => 'GPS'] as $id => $label) {                $on = $this->scanPage === $id;                $c->pill($x, 114, 80, 26, $on ? [120, 190, 255, 120] : [255, 255, 255, 22]);                $c->text($label, $x + 40, 118, 15, $on ? [255, 255, 255] : [176, 188, 200], 'center');                $c->hit($x, 112, 80, 30, 'scan:' . $id);                $x += 88;            }        }        $c->text($this->touch ? 'Touche les onglets · ✕ pour fermer' : 'Échap : fermer   ·   PgPréc / PgSuiv (LB / RB) : onglets', 480, 676, 12, [150, 164, 178], 'center');    }    private function drawInventory(Canvas $c, Game $g, HudData $d): void    {        $n = count($this->items);        $this->invSel = max(0, min($this->invSel, max(0, $n - 1)));        $gx = 56.0;        $gy = 120.0;        $rows = self::INV_ROWS;        // the selection is always among the rows shown (the wheel pulls it along): the view follows it        $selRow = intdiv($this->invSel, self::COLS);        $this->invTop = $selRow < $this->invTop ? $selRow : ($selRow >= $this->invTop + self::INV_ROWS ? $selRow - self::INV_ROWS + 1 : $this->invTop);        $this->invTop = Wheel::clamp($this->invTop, (int) ceil($n / self::COLS), self::INV_ROWS);        $first = $this->invTop * self::COLS;        for ($k = 0; $k < self::COLS * $rows; $k++) {            $i = $first + $k;            $cx = $gx + ($k % self::COLS) * self::CELL;            $cy = $gy + intdiv($k, self::COLS) * self::CELL;            $id = $this->items[$i] ?? null;            $on = $id !== null && $i === $this->invSel;            $c->rect($cx, $cy, self::CELL - 6, self::CELL - 6, $on ? [120, 190, 255, 60] : [255, 255, 255, 14]);            $c->border($cx, $cy, self::CELL - 6, self::CELL - 6, $on ? [160, 214, 255, 220] : [255, 255, 255, 34]);            if ($id === null) {                continue;            }            $c->icon($id, $cx + 13, $cy + 9, 42);            $cnt = $g->inv->count($id);            if ($cnt > 1) {                $c->text('×' . $cnt, $cx + self::CELL - 10, $cy + self::CELL - 25, 13, [255, 255, 255], 'right');            }            if ($g->inv->heldSlot($id) !== null || $g->inv->isWorn($id) !== null) {                $c->rect($cx + 4, $cy + 4, 8, 8, [255, 204, 110]);            }            foreach ($d->slots as $s => $sid) {                if ($sid === $id) {                    $c->text((string) $s, $cx + 6, $cy + self::CELL - 24, 12, [150, 210, 255], 'left');                }            }            $c->hit($cx, $cy, self::CELL - 6, self::CELL - 6, 'inv:' . $i);        }        if ($n === 0) {            $c->text('Ton sac est vide.', $gx + 220, 300, 18, [170, 180, 190], 'center');        }        // the load        $w = $g->inv->weight();        $cap = $g->inv->capacity();        $ratio = $cap > 0 ? min(1.0, $w / $cap) : 0.0;        $c->icon('v_weight', $gx, 576, 26);        $c->text(sprintf('Charge  %s / %s kg', self::num($w), self::num($cap)), $gx + 34, 574, 15, [220, 228, 236]);        $c->rect($gx + 34, 596, 400, 7, [255, 255, 255, 26]);        $c->rect($gx + 34, 596, 400 * $ratio, 7, $ratio > 0.9 ? [230, 80, 60] : ($ratio > 0.7 ? [230, 170, 60] : [110, 200, 140]));        // equipment        $ex = 520.0;        $c->text('ÉQUIPEMENT', $ex, 118, 13, [150, 166, 180]);        $slots = [['hand', 'Main D.', $g->inv->hand], ['handLeft', 'Main G.', $g->inv->handLeft]];        foreach (Inventory::CLOTH_SLOTS as $slot) {            $slots[] = [$slot, ['head' => 'Tête', 'under' => 'Dessous', 'worn' => 'Corps', 'legs' => 'Jambes', 'hands' => 'Mains', 'feet' => 'Pieds'][$slot], $g->inv->$slot];        }        // the slots share the column's width (388 units: the separator below), whatever their number        $gap = 4.0;        $sw = (388.0 - $gap * (count($slots) - 1)) / count($slots);        $icon = min(34.0, $sw - 8);        foreach ($slots as $k => [$slot, $label, $id]) {            $sx = $ex + $k * ($sw + $gap);            $c->rect($sx, 136, $sw, 60, [255, 255, 255, 14]);            $c->border($sx, 136, $sw, 60, [255, 255, 255, 34]);            $c->box($sx, 136, $sw, 60);            $c->text($label, $sx + $sw / 2, 138, 10, [150, 166, 180], 'center');            if ($id !== null && $g->inv->has($id)) {                $c->icon($id, $sx + ($sw - $icon) / 2, 154 + (34 - $icon) / 2, $icon);                $c->hit($sx, 136, $sw, 60, 'unequip:' . $slot);            } else {                $c->text('—', $sx + $sw / 2, 160, 14, [110, 120, 130], 'center', false);                if ($slot !== 'hand' && $slot !== 'handLeft') {                    $c->hit($sx, 136, $sw, 60, 'equip:' . $slot);                }            }        }        // the item chosen        $id = $this->items[$this->invSel] ?? null;        if ($id === null) {            return;        }        $it = Items::get($id);        $c->rect($ex, 206, 388, 1, [255, 255, 255, 30]);        $c->rect($ex, 220, 76, 76, [255, 255, 255, 16]);        $c->icon($id, $ex + 6, 226, 64);        $c->textBox($it['name'], $ex + 86, 222, 302, 20, [255, 255, 255]);        if ($it['kind'] === 'clothing') {            $cond = $g->inv->condition($id);            $word = $g->inv->conditionWord($id);            $c->rect($ex + 200, 278, 120, 5, [255, 255, 255, 26]);            $c->rect($ex + 200, 278, 120 * $cond / 100, 5, $cond < 20 ? [230, 70, 60] : ($cond < 50 ? [230, 170, 60] : [110, 200, 140]));            $c->text('état ' . (int) round($cond) . ' %' . ($word !== '' ? ' (' . $word . ')' : ''), $ex + 200, 286, 11, [200, 210, 220]);        }        $c->text((self::KIND[$it['kind']] ?? 'Objet') . ' · ' . self::num((float) $it['weight']) . ' kg', $ex + 90, 252, 13, [170, 186, 200]);        $c->text('× ' . $g->inv->count($id) . ' dans le sac', $ex + 90, 272, 13, [170, 186, 200]);        $y = 310.0;        foreach (array_slice($c->wrap($it['desc'], 15, 380), 0, 3) as $line) {            $c->text($line, $ex, $y, 15, [222, 228, 234]);            $y += 20;        }        $y += 6;        foreach (self::stats($it) as [$label, $value, $good]) {            $c->text($label, $ex, $y, 14, [170, 186, 200]);            $c->text($value, $ex + 380, $y, 14, $good ? [140, 230, 160] : [255, 150, 120], 'right');            $y += 19;        }        // buttons        $verb = match (true) {            self::isLocalCare($id) => 'Soigner…',            $g->inv->heldSlot($id) !== null || $g->inv->isWorn($id) !== null => $g->inv->isWorn($id) !== null ? 'Ôter' : 'Ranger',            in_array($it['kind'], ['weapon', 'tool'], true) && !in_array($id, ['campfire_kit', 'shelter_kit', 'trap_snare', ...array_keys(Chests::KINDS)], true) => 'Prendre en main',            $it['kind'] === 'clothing' => 'Enfiler',            $id === 'scanner' => 'Ouvrir le scanner',            $id === 'patch' => 'Rapiécer',            $it['kind'] === 'food' => 'Manger',            $it['kind'] === 'drink' => 'Boire',            $id === 'purification_tablets' => 'Purifier une gourde',            $id === 'water_filter' => 'Filtrer une gourde',            $id === 'earthworm' => 'Avaler (cru…)',            $id === 'sand' => null,            $it['kind'] === 'medical' => 'Prendre',            in_array($id, ['pod_log', 'colony_log'], true) => 'Lire',            in_array($id, ['campfire_kit', 'shelter_kit', 'trap_snare'], true) => 'Installer',            isset(Chests::KINDS[$id]) => 'Poser devant toi',            default => null,        };        $bx = $ex;        if ($verb !== null) {            $bx += $this->button($c, $bx, 556, $verb, 'Entrée', 'use') + 8;        }        $bx += $this->button($c, $bx, 556, 'Poser', 'X', 'drop') + 8;        if (in_array($id, ['pod_log', 'colony_log'], true)) {            $this->button($c, $ex, 512, 'Insérer dans le GPS', '', 'gpslog:' . $id);        }        $c->text('Raccourci :', $ex, 606, 13, [150, 166, 180]);        for ($s = 1; $s <= 4; $s++) {            $sx = $ex + 80 + ($s - 1) * 36;            $on = ($d->slots[$s] ?? null) === $id;            $c->rect($sx, 602, 30, 24, $on ? [150, 210, 255, 90] : [255, 255, 255, 20]);            $c->border($sx, 602, 30, 24, [255, 255, 255, 60]);            $c->text((string) $s, $sx + 15, 606, 13, [236, 240, 244], 'center', false);            $c->hit($sx, 602, 30, 24, 'quick:' . $s);        }    }    /** @return list<array{0: string, 1: string, 2: bool}> */    private static function stats(array $it): array    {        $out = [];        if (isset($it['food'])) {            $out[] = ['Nourriture', '+' . (int) $it['food'], true];        }        if (isset($it['water'])) {            $out[] = ['Eau', '+' . (int) $it['water'], true];        }        if (isset($it['damage'])) {            $out[] = ['Dégâts', (string) (int) $it['damage'], true];        }        if (isset($it['warmth'])) {            $out[] = ['Chaleur', '+' . (int) $it['warmth'] . ' °C', true];        }        if (isset($it['fuel'])) {            $out[] = ['Combustible', (int) $it['fuel'] . ' min de feu', true];        }        if (isset($it['risk'])) {            $out[] = ['Risque', isset($it['risk']['poison']) ? 'poison' : (isset($it['risk']['raw']) ? 'cru : à cuire' : 'eau sale : à bouillir'), false];        }        if (isset($it['heal'])) {            $what = [];            foreach (['bleed' => 'saignement', 'fracture' => 'fracture', 'infection' => 'infection', 'poison' => 'poison', 'fever' => 'fièvre', 'health' => 'santé'] as $k => $l) {                if (isset($it['heal'][$k])) {                    $what[] = $l;                }            }            $out[] = ['Soigne', implode(', ', $what), true];        }        return $out;    }    /** A button with its key; returns its width. */    private function button(Canvas $c, float $x, float $y, string $label, string $key, string $hit): float    {        $kw = $key === '' ? -8.0 : $c->measure($key, 12) + 12;        $w = $kw + $c->measure($label, 16) + 26;        $c->rect($x, $y, $w, 36, [120, 190, 255, 44]);        $c->border($x, $y, $w, 36, [150, 210, 255, 150]);        if ($key !== '') {            $c->pill($x + 8, $y + 9, $kw, 18, [235, 238, 242, 220]);            $c->text($key, $x + 8 + $kw / 2, $y + 11, 12, [20, 26, 34], 'center', false);        }        $c->text($label, $x + 16 + $kw, $y + 8, 16, [255, 255, 255]);        $c->hit($x, $y, $w, 36, $hit);        return $w;    }    /** A small button; returns its height. */    private function smallButton(Canvas $c, float $x, float $y, string $label, string $hit, bool $done): float    {        $w = $c->measure($label, 13) + 22;        $c->rect($x, $y, $w, 26, $done ? [110, 200, 140, 40] : [80, 220, 255, 50]);        $c->border($x, $y, $w, 26, $done ? [140, 230, 160, 140] : [120, 230, 255, 170]);        $c->text($label, $x + 11, $y + 5, 13, [255, 255, 255]);        $c->hit($x, $y, $w, 26, $hit);        return 26.0;    }    private static function num(float $v): string    {        return Translator::num($v, 1);    }    private function drawCraft(Canvas $c, Game $g): void    {        $this->recipes = $this->recipeList($g);        $near = $g->nearFire();        $n = count($this->recipes);        $this->craftSel = max(0, min($this->craftSel, $n - 1));        $x = 56.0;        $y = 118.0;        for ($i = $this->craftTop; $i < min($n, $this->craftTop + 12); $i++) {            $r = $this->recipes[$i];            $ok = self::can($r, $g, $near);            $on = $i === $this->craftSel;            $ry = $y + ($i - $this->craftTop) * 38;            $c->rect($x, $ry, 420, 34, $on ? [120, 190, 255, 56] : [255, 255, 255, $ok ? 16 : 6]);            if ($on) {                $c->rect($x, $ry, 3, 34, [150, 210, 255, 230]);            }            $c->icon((string) array_key_first($r['gives']), $x + 8, $ry + 4, 26);            $c->text($r['name'], $x + 42, $ry + 8, 15, $ok ? [240, 244, 248] : [130, 138, 146]);            $c->text($ok ? 'prêt' : 'verrouillé', $x + 410, $ry + 10, 12, $ok ? [140, 230, 160] : [150, 120, 110], 'right');            $c->hit($x, $ry, 420, 34, 'rec:' . $i);        }        if ($n > 12) {            $c->rect($x + 426, $y, 3, 12 * 38 - 4, [255, 255, 255, 20]);            $c->rect($x + 426, $y + (12 * 38 - 4) * $this->craftTop / $n, 3, (12 * 38 - 4) * 12 / $n, [150, 210, 255, 160]);        }        $r = $this->recipes[$this->craftSel] ?? null;        if ($r === null) {            return;        }        $dx = 510.0;        $gives = (string) array_key_first($r['gives']);        $c->rect($dx, 118, 76, 76, [255, 255, 255, 16]);        $c->icon($gives, $dx + 6, 124, 64);        $c->text($r['name'], $dx + 90, 122, 19, [255, 255, 255]);        $what = [];        foreach ($r['gives'] as $id => $k) {            $what[] = $k . ' × ' . Items::name($id);        }        $c->text('Donne : ' . implode(', ', $what), $dx + 90, 150, 14, [170, 186, 200]);        $c->text('Durée : ' . $r['minutes'] . ' min', $dx + 90, 170, 14, [170, 186, 200]);        $yy = 214.0;        $c->text('IL FAUT', $dx, $yy, 13, [150, 166, 180]);        $yy += 24;        foreach ($r['needs'] as $id => $k) {            $have = $g->inv->count($id);            $c->icon($id, $dx, $yy - 2, 26);            $c->text(Items::name($id), $dx + 34, $yy + 2, 16, [230, 236, 242]);            $c->text($have . ' / ' . $k, $dx + 390, $yy + 2, 16, $have >= $k ? [140, 230, 160] : [255, 130, 110], 'right');            $yy += 32;        }        $tool = Crafting::toolFor($r, $g->inv);        if ($tool !== null) {            $has = $g->inv->has($tool);            $c->icon($tool, $dx, $yy - 2, 26);            $c->text('Outil : ' . Items::name($tool), $dx + 34, $yy + 2, 16, [230, 236, 242]);            $c->text($has ? 'oui' : 'non', $dx + 390, $yy + 2, 16, $has ? [140, 230, 160] : [255, 130, 110], 'right');            $yy += 32;        }        if ($r['fire']) {            $c->icon('v_fire', $dx, $yy - 2, 26);            $c->text('Près d\'un feu', $dx + 34, $yy + 2, 16, [230, 236, 242]);            $c->text($near ? 'oui' : 'non', $dx + 390, $yy + 2, 16, $near ? [140, 230, 160] : [255, 130, 110], 'right');            $yy += 32;        }        $ok = self::can($r, $g, $near);        if ($ok) {            $this->button($c, $dx, 556, 'Fabriquer', 'Entrée', 'craft');        } else {            $c->text('Il manque des éléments pour cette recette.', $dx, 564, 15, [200, 150, 130]);        }    }    private function drawBody(Canvas $c, Game $g): void    {        $parts = array_keys(self::PARTS);        $part = $parts[$this->part] ?? 'torso';        $cares = $this->cares($g);        $this->treatSel = max(0, min($this->treatSel, max(0, count($cares) - 1)));        $this->bodyPanel->draw($c, $g, $this->bodyView, $part, $this->time, $cares, $this->treatSel);    }    private function drawMap(Canvas $c, Game $g, HudData $d): void    {        $z = $g->zone;        [$marks, $wt] = $this->drawMapAt($c, $g, $d, 56.0, 118.0, 536.0);        $this->drawLegend($c, $g, $d, $marks, $wt);    }    /** The map of the zone in a square, with its marks, Rob, the objective and the GPS points. @return array{0: list<array>, 1: ?array} the marks, the GPS target */    private function drawMapAt(Canvas $c, Game $g, HudData $d, float $mx, float $my, float $size): array    {        $z = $g->zone;        $tex = $this->map->texture($z);        $c->rect($mx - 2, $my - 2, $size + 4, $size + 4, [0, 0, 0, 160]);        if ($tex !== null) {            $c->image($tex, $mx, $my, $size, $size);        } else {            $c->rect($mx, $my, $size, $size, [40, 60, 40, 200]);        }        $fog = $this->map->fog($g);        if ($fog !== null) {            $c->image($fog, $mx, $my, $size, $size);        }        $c->border($mx, $my, $size, $size, [255, 255, 255, 60]);        $k = $size / $z->size;        $at = static fn (float $x, float $zz): array => [$mx + $x * $k, $my + $zz * $k];        // places        $marks = [];        foreach ($z->exits as $e) {            if ($g->isExplored($e['x'], $e['z'])) {                $marks[] = [$e['x'], $e['z'], [90, 220, 240], $e['label']];            }        }        if ($z->id === 'valley') {            $marks[] = [ZoneFactory::POD[0], ZoneFactory::POD[1], [220, 220, 230], 'Ta nacelle'];        }        if ($z->id === 'canyon') {            $v = $g->villageCentre();            if ($v !== null && $g->isExplored((float) $v[0], (float) $v[1])) {                $marks[] = [(float) $v[0], (float) $v[1], [140, 230, 160], 'Village haldi'];            }            if (Quest::done($g, 'meet_elder')) {                $cc = ZoneFactory::campCentre($g->seed);                $marks[] = [(float) $cc[0], (float) $cc[1], [240, 90, 80], 'Camp des Rôdeurs'];            }        }        if ($z->id === 'plateau' && ($g->isExplored(ZoneFactory::MAST[0], ZoneFactory::MAST[1]) || Quest::done($g, 'colony_log'))) {            $marks[] = [ZoneFactory::MAST[0], ZoneFactory::MAST[1], [255, 214, 120], 'Mât relais'];        }        // Rob's chests: always on the map (he put them there), named        foreach (Chests::inZone($g) as $t) {            $marks[] = [$t->x, $t->z, Chests::COLOR, Chests::label($t)];        }        foreach ($marks as [$x, $zz, $col]) {            [$px, $py] = $at($x, $zz);            $c->rect($px - 5, $py - 5, 10, 10, [0, 0, 0, 180]);            $c->rect($px - 4, $py - 4, 8, 8, $col);        }        foreach ($g->fires as $f) {            if ($f['minutes'] > 0) {                [$px, $py] = $at($f['x'], $f['z']);                $c->circle($px, $py, 4, [255, 140, 40]);            }        }        if ($d->target !== null) {            [$px, $py] = $at($d->target[0], $d->target[1]);            $c->triangle($px, $py - 9, $px - 7, $py, $px + 7, $py, [255, 196, 80]);            $c->triangle($px - 7, $py, $px + 7, $py, $px, $py + 9, [255, 196, 80]);        }        // the GPS points of this zone, the active one (or the door toward it) ringed        foreach ($g->waypoints as $wid => [$wname, $wzone, $wx, $wz]) {            if ($wzone !== $z->id) {                continue;            }            [$px, $py] = $at($wx, $wz);            $on = $wid === $g->activeWaypoint;            $c->circle($px, $py, $on ? 8 : 6, [0, 0, 0, 170]);            $c->circle($px, $py, $on ? 6 : 4.5, [80, 230, 255]);            $c->text($wname, $px + 10, $py - 8, 12, [170, 240, 255]);        }        $wt = $g->waypointTarget();        if ($wt !== null) {            [$px, $py] = $at($wt[0], $wt[1]);            $c->circle($px, $py, 11, [80, 230, 255, 70]);        }        // Rob: an arrow in his direction        [$px, $py] = $at($g->x, $g->z);        $fx = sin($g->yaw);        $fz = cos($g->yaw);        $c->triangle($px + $fx * 13, $py + $fz * 13, $px - $fx * 7 + $fz * 8, $py - $fz * 7 - $fx * 8, $px - $fx * 7 - $fz * 8, $py - $fz * 7 + $fx * 8, [0, 0, 0]);        $c->triangle($px + $fx * 11, $py + $fz * 11, $px - $fx * 5 + $fz * 6, $py - $fz * 5 - $fx * 6, $px - $fx * 5 - $fz * 6, $py - $fz * 5 + $fx * 6, [255, 255, 255]);        $c->text('N', $mx + $size / 2, $my + 4, 14, [255, 120, 100], 'center');        return [$marks, $wt];    }    private function drawLegend(Canvas $c, Game $g, HudData $d, array $marks, ?array $wt): void    {        $z = $g->zone;        $lx = 616.0;        $c->text($z->name, $lx, 118, 20, [255, 255, 255]);        $c->text(Game::coordText($z->id, $g->x, $g->z) . sprintf('  ·  alt. %d m', (int) $g->y), $lx, 146, 13, [170, 186, 200]);        $c->text(sprintf('Exploré : %d %% de la zone', (int) round(100 * $g->exploredShare())), $lx, 164, 13, [170, 186, 200]);        $ly = 182.0;        $legend = [[[255, 255, 255], 'Toi'], [[255, 196, 80], $d->target !== null ? 'Objectif : ' . $d->target[2] : 'Objectif']];        if ($wt !== null) {            $legend[] = [[80, 230, 255], 'GPS : ' . $wt[2] . ' · ' . (int) round($wt[3]) . ' m'];        }        foreach ($marks as [, , $col, $label]) {            $legend[] = [$col, $label];        }        if ($g->fires !== []) {            $legend[] = [[255, 140, 40], 'Feux'];        }        foreach (array_slice($legend, 0, 12) as [$col, $label]) {            $c->rect($lx, $ly + 4, 10, 10, $col);            foreach (array_slice($c->wrap($label, 14, 270), 0, 2) as $i => $line) {                $c->text($line, $lx + 20, $ly + $i * 17, 14, [222, 228, 234]);            }            $ly += count($c->wrap($label, 14, 270)) > 1 ? 40 : 24;        }    }    private function drawJournal(Canvas $c, Game $g): void    {        $q = $g->quest();        $x = 56.0;        $c->text('CHAPITRE ' . $q['chapter'], $x, 118, 13, [255, 204, 110]);        $c->text($q['title'], $x, 136, 24, [255, 255, 255]);        $y = 176.0;        foreach ($q['goals'] as $goal) {            $done = $goal['done'];            $c->rect($x, $y + 2, 16, 16, $done ? [110, 200, 140, 220] : [255, 255, 255, 20]);            $c->border($x, $y + 2, 16, 16, [255, 255, 255, 90]);            if ($done) {                $c->line($x + 3, $y + 10, $x + 7, $y + 14, [255, 255, 255], 2);                $c->line($x + 7, $y + 14, $x + 14, $y + 5, [255, 255, 255], 2);            }            $c->text($goal['text'], $x + 26, $y, 16, $done ? [150, 166, 180] : [240, 244, 248]);            $y += 28;        }        $y += 10;        $c->text('DÉJÀ ACCOMPLI', $x, $y, 13, [150, 166, 180]);        $y += 22;        $prev = array_values(array_diff($q['done'], array_column(array_filter($q['goals'], static fn ($gl) => $gl['done']), 'text')));        if ($prev === []) {            $c->text('Rien encore : tout commence ici.', $x, $y, 14, [150, 160, 170]);        }        foreach (array_slice($prev, -8) as $t) {            $c->text('• ' . $t, $x, $y, 14, [180, 192, 204]);            $y += 20;        }        $c->text(sprintf('Jour %d  ·  %d nuit(s)  ·  %d feu(x)  ·  %d bête(s) abattue(s)', $g->day(), (int) ($g->flags['nights'] ?? 0),            (int) ($g->flags['fires'] ?? 0), (int) ($g->flags['kills'] ?? 0)), $x, 620, 13, [150, 166, 180]);        // logs read        $lx = 500.0;        $c->rect($lx - 16, 118, 1, 500, [255, 255, 255, 30]);        $c->text('JOURNAUX LUS', $lx, 118, 13, [150, 166, 180]);        $ly = 140.0;        $logs = [];        if (Quest::done($g, 'pod_log')) {            $logs[] = ['Journal de la nacelle', $g->logText('pod_log'), 'pod_log'];        }        if (Quest::done($g, 'colony_log')) {            $logs[] = ['Journal de la mission Halde', $g->logText('colony_log'), 'colony_log'];        }        if ($logs === []) {            $c->text('Aucun journal pour l\'instant.', $lx, $ly, 14, [150, 160, 170]);        }        foreach ($logs as [$title, $text, $log]) {            $c->text($title, $lx, $ly, 16, [255, 214, 140]);            $ly += 22;            foreach ($c->wrap($text, 12, 400) as $line) {                $c->text($line, $lx, $ly, 12, [214, 220, 228]);                $ly += 15;            }            $in = true;            foreach (array_keys($g->logPlaces($log)) as $wid) {                $in = $in && isset($g->waypoints[$wid]);            }            $ly += 4;            $ly += ($in ? $this->smallButton($c, $lx, $ly, 'Dans le GPS (actif)', 'gpslog:' . $log, true) : $this->smallButton($c, $lx, $ly, 'Insérer dans le GPS', 'gpslog:' . $log, false)) + 10;        }    }}