JA
games/rob-revisited / src / Revisited / Ui / ScannerView.php · commit f9a1307
<?phpnamespace App\Revisited\Ui;use App\Revisited\I18n\Translator;use App\Revisited\Land\Chests;use App\Revisited\Land\Game;/** * The pod's survey scanner (item "scanner"): a round radar around Rob (his heading up, 60 m outdoors, 30 m in the * caves) with water, food, materials, loot, creatures (danger red, prey green), people, doors and the objective, a * sweeping beam that lights the echoes as it passes; beside it the battery and the bio-scan: the vitals and an * analysis of what is wrong with advice. The battery drains while the screen is open (Game::useScanner()). */final class ScannerView{    /** category => [colour, legend] */    public const CATS = [        'water' => [[80, 170, 255], 'Eau'],        'food' => [[130, 230, 120], 'Nourriture, herbes'],        'toxic' => [[200, 110, 230], 'Toxique'],        'material' => [[220, 190, 120], 'Bois, silex, pierre'],        'loot' => [[240, 240, 250], 'À fouiller'],        'danger' => [[255, 70, 60], 'Bête dangereuse'],        'prey' => [[120, 255, 160], 'Gibier'],        'friend' => [[110, 210, 255], 'Haldi'],        'foe' => [[255, 110, 40], 'Rôdeur'],        'exit' => [[90, 240, 230], 'Passage'],        'fire' => [[255, 150, 50], 'Feu'],        'goal' => [[255, 200, 80], 'Objectif'],        'gps' => [[80, 230, 255], 'Point GPS'],        'stash' => [Chests::COLOR, 'Tes coffres'],    ];    private const HOSTILE = ['stalker', 'crawler', 'burrower', 'glacier_brute', 'swarm'];    private const THINGS = [        'water_spring' => 'water', 'bush_berry' => 'food', 'mushroom' => 'food', 'herb_heal' => 'food', 'herb_fever' => 'food',        'bush_red' => 'toxic', 'flint' => 'material', 'stick' => 'material', 'stone_small' => 'material', 'wood' => 'material',        'tree_dead' => 'material', 'reed' => 'material', 'vine' => 'material', 'crate' => 'loot', 'wreck_pod' => 'loot',        'wreck_colony' => 'loot', 'bones' => 'loot', 'carcass' => 'loot', 'snare' => 'loot', 'campfire' => 'fire',    ];    private const ADVICE = [        'bleeding' => 'Hémorragie : un bandage sur la plaie qui saigne (onglet Corps).',        'shock' => 'Trop de sang perdu : arrêter le saignement, boire, se reposer au chaud.',        'infection' => 'Infection : appliquer un cataplasme sur la plaie, la garder propre.',        'fever' => 'Fièvre : tisane de fièvre, dormir près d\'un feu.',        'poison' => 'Poison dans le sang : antidote ou herbe à venin, vite.',        'dysentery' => 'Dysenterie : ne boire que de l\'eau bouillie, se reposer.',        'hypothermia' => 'Hypothermie : un feu, un abri, des vêtements secs.',        'heatstroke' => 'Coup de chaleur : de l\'ombre, de l\'eau, du repos.',        'exhaustion' => 'Épuisement : dormir (T), de préférence à l\'abri.',        'starvation' => 'Inanition : manger sans attendre.',        'dehydration' => 'Déshydratation : boire, une source d\'eau claire de préférence.',        'concussion' => 'Commotion : repos, éviter les combats.',        'cold_hands' => 'Mains gelées : des gants, ou les réchauffer près d\'un feu.',        'cold_feet' => 'Pieds gelés : des bottes sèches, des jambières, un feu.',    ];    public static function range(Game $g): float    {        return $g->zone->cave ? Game::SCAN_RANGE / 2 : Game::SCAN_RANGE;    }    /** @return list<array{0: float, 1: float, 2: string, 3: string}> x, z, category, label: what the scanner finds */    public static function echoes(Game $g, ?array $target): array    {        $r = self::range($g);        $out = [];        foreach ($g->zone->thingsNear($g->x, $g->z, $r) as $t) {            $cat = self::THINGS[$t->kind] ?? null;            if ($t->kind === 'tree_palm' && (int) ($t->data['fruit'] ?? 2) > 0) {                $cat = 'food';            }            if ($cat === null || ($t->kind === 'bush_berry' && $t->left <= 0)) {                continue;            }            if (($cat === 'loot') && empty($t->data['loot']) && $t->kind !== 'carcass' && $t->kind !== 'snare') {                continue;            }            $out[] = [$t->x, $t->z, $cat, (string) ($t->data['label'] ?? '')];        }        // Rob's chests, wherever they are in the zone (at the radar's edge when far)        foreach (Chests::inZone($g) as $t) {            $out[] = [$t->x, $t->z, 'stash', Chests::label($t)];        }        // the lake, sampled        $z = $g->zone;        if ($z->water > -999) {            $step = $z->cave ? 4.0 : 7.0;            for ($dz = -$r; $dz <= $r; $dz += $step) {                for ($dx = -$r; $dx <= $r; $dx += $step) {                    if ($dx * $dx + $dz * $dz > $r * $r) {                        continue;                    }                    $x = floor(($g->x + $dx) / $step) * $step;                    $zz = floor(($g->z + $dz) / $step) * $step;                    if ($z->waterDepth($x, $zz) > 0.25) {                        $out[] = [$x, $zz, 'water', ''];                    }                }            }        }        foreach ($g->creatures as $c) {            if ($c->state === 'dead' || $c->species === 'bird' || hypot($c->x - $g->x, $c->z - $g->z) > $r) {                continue;            }            $out[] = [$c->x, $c->z, in_array($c->species, self::HOSTILE, true) ? 'danger' : 'prey', ''];        }        foreach ($g->npcs as $n) {            if (hypot($n->x - $g->x, $n->z - $g->z) <= $r) {                $out[] = [$n->x, $n->z, $n->hostile || $n->faction === 'rodeur' ? 'foe' : 'friend', $n->name];            }        }        foreach ($g->fires as $f) {            if ($f['minutes'] > 0 && hypot($f['x'] - $g->x, $f['z'] - $g->z) <= $r) {                $out[] = [(float) $f['x'], (float) $f['z'], 'fire', ''];            }        }        foreach ($z->exits as $e) {            $out[] = [(float) $e['x'], (float) $e['z'], 'exit', (string) $e['label']];        }        if ($target !== null) {            $out[] = [$target[0], $target[1], 'goal', $target[2]];        }        $wp = $g->waypointTarget();        if ($wp !== null) {            $out[] = [$wp[0], $wp[1], 'gps', $wp[2]];        }        return $out;    }    /** Draws the scanner screen (inside the Screens' frame). */    public function draw(Canvas $c, Game $g, HudData $d, float $time, bool $on): void    {        $cx = 300.0;        $cy = 384.0;        $R = 252.0;        $range = self::range($g);        // the screen of the device        $c->circle($cx, $cy, $R + 10, [30, 38, 44, 255]);        $c->circle($cx, $cy, $R + 6, [70, 84, 92, 255]);        $c->circle($cx, $cy, $R, [4, 22, 18, 255]);        if (!$g->inv->has('scanner')) {            $c->text('Pas de scanner', $cx, $cy - 20, 22, [150, 170, 160], 'center');            $c->text('Le scanner de la nacelle s\'est perdu.', $cx, $cy + 14, 14, [120, 140, 130], 'center');            $this->bio($c, $g, 590.0);            return;        }        if (!$on) {            $c->text('BATTERIE VIDE', $cx, $cy - 20, 22, [255, 120, 100], 'center');            $c->text('Recharge au soleil, près de la nacelle ou avec la cellule d\'énergie.', $cx, $cy + 14, 13, [170, 190, 180], 'center');            $this->bio($c, $g, 590.0);            return;        }        // rings and cross        foreach ([0.25, 0.5, 0.75, 1.0] as $i => $f) {            $this->ring($c, $cx, $cy, $R * $f, [60, 200, 140, $i === 3 ? 120 : 60]);            $c->text((int) round($range * $f) . ' m', $cx + 4, $cy - $R * $f + 2, 11, [90, 200, 150], 'left', false);        }        $c->rect($cx - $R, $cy, 2 * $R, 1, [60, 200, 140, 50]);        $c->rect($cx, $cy - $R, 1, 2 * $R, [60, 200, 140, 50]);        // the beam and its trail        $sweep = fmod($time * 1.6, 2 * M_PI);        // the trail: thin wedges, fading behind the beam        for ($k = 0; $k < 16; $k++) {            $a0 = $sweep - $k * 0.04;            $a1 = $a0 - 0.042;            $c->triangle($cx, $cy, $cx + sin($a0) * $R, $cy - cos($a0) * $R, $cx + sin($a1) * $R, $cy - cos($a1) * $R,                [60, 230, 150, (int) (70 * (1 - $k / 16) ** 2)]);        }        $c->line($cx, $cy, $cx + sin($sweep) * $R, $cy - cos($sweep) * $R, [150, 255, 200, 220], 2.0);        // the echoes: heading up        $right = [-cos($g->yaw), sin($g->yaw)];        $fwd = [sin($g->yaw), cos($g->yaw)];        $k = $R / $range;        $labels = [];        foreach (self::echoes($g, $d->target) as [$x, $z, $cat, $label]) {            $dx = $x - $g->x;            $dz = $z - $g->z;            $u = $dx * $right[0] + $dz * $right[1];            $v = $dx * $fwd[0] + $dz * $fwd[1];            $dist = hypot($u, $v);            $edge = false;            if ($dist > $range) {                if ($cat !== 'goal' && $cat !== 'exit' && $cat !== 'gps' && $cat !== 'stash') {                    continue;                }                $u *= $range / $dist;                $v *= $range / $dist;                $edge = true;            }            $px = $cx + $u * $k;            $py = $cy - $v * $k;            // brightest just after the beam passed            $ang = atan2($u, $v);            $since = fmod($sweep - $ang + 4 * M_PI, 2 * M_PI);            $glow = max(0.35, 1 - $since / (2 * M_PI));            $col = self::CATS[$cat][0];            $a = (int) (255 * $glow);            if ($cat === 'water') {                $c->rect($px - 3, $py - 3, 6, 6, [$col[0], $col[1], $col[2], (int) (90 * $glow)]);                continue;            }            $size = in_array($cat, ['danger', 'foe', 'goal', 'exit', 'gps', 'stash'], true) ? 6.0 : 4.2;            if ($cat === 'gps') {                $c->circle($px, $py, $size + 4, [$col[0], $col[1], $col[2], 90]);                $c->circle($px, $py, $size, [$col[0], $col[1], $col[2], 255]);                if ($edge) {                    $c->line($px, $py, $px - $u * $k * 0.08, $py + $v * $k * 0.08, [$col[0], $col[1], $col[2], 220], 2.0);                }            } elseif ($cat === 'stash') {                // a little chest: a square with its lid line                $c->rect($px - $size - 1, $py - $size + 1, 2 * $size + 2, 2 * $size - 1, [0, 0, 0, (int) (160 * $glow)]);                $c->rect($px - $size, $py - $size + 2, 2 * $size, 2 * $size - 3, [$col[0], $col[1], $col[2], $a]);                $c->rect($px - $size, $py - 1, 2 * $size, 1, [60, 40, 20, $a]);            } elseif ($cat === 'goal' || $cat === 'exit') {                $c->triangle($px, $py - $size - 2, $px - $size - 2, $py, $px + $size + 2, $py, [$col[0], $col[1], $col[2], $a]);                $c->triangle($px - $size - 2, $py, $px + $size + 2, $py, $px, $py + $size + 2, [$col[0], $col[1], $col[2], $a]);            } else {                $c->circle($px, $py, $size + 2.5, [$col[0], $col[1], $col[2], (int) (60 * $glow)]);                $c->circle($px, $py, $size, [$col[0], $col[1], $col[2], $a]);            }            if ($label !== '' && ($cat === 'goal' || $cat === 'exit' || $cat === 'friend' || $cat === 'gps' || $cat === 'stash') && count($labels) < 6) {                $labels[] = [$px, $py, $label . ($edge ? ' ›' : ''), $col];            }        }        foreach ($labels as [$px, $py, $label, $col]) {            // two lines at most, kept inside the radar's square            $lx = max($cx - $R, min($cx + $R - 160, $px - 80));            $c->textBox($label, $lx, $py + 10, 160, 12, $col, 'center', 2, 1.15);        }        // Rob        $c->triangle($cx, $cy - 10, $cx - 7, $cy + 7, $cx + 7, $cy + 7, [255, 255, 255]);        // north (-z) on the rim        $c->text('N', $cx - sin($g->yaw) * ($R - 16), $cy + cos($g->yaw) * ($R - 16) - 8, 14, [255, 120, 100], 'center');        $this->bio($c, $g, 590.0);        // the legend        $ly = 512.0;        $i = 0;        foreach (self::CATS as [$col, $label]) {            $lx = 590 + ($i % 2) * 160;            $y = $ly + intdiv($i, 2) * 19;            $c->circle($lx + 5, $y + 8, 4.5, $col);            $c->text($label, $lx + 15, $y + 1, 12, [200, 212, 220]);            $i++;        }        $c->text(sprintf('Portée %d m · %d %% de la zone cartographiée', (int) $range, (int) round(100 * $g->exploredShare())), 50, 624, 12, [120, 180, 150]);    }    /** The GPS page: where Rob is on Ysor's grid, the waypoints (activate, erase), a mark here, the logs to insert. */    public function drawGps(Canvas $c, Game $g, bool $on): void    {        $x = 56.0;        $c->rect($x, 150, 500, 120, [4, 22, 18, 240]);        $c->border($x, 150, 500, 120, [60, 200, 140, 120]);        if (!$g->inv->has('scanner') || !$on) {            $c->text(!$g->inv->has('scanner') ? 'Pas de scanner' : 'BATTERIE VIDE', $x + 250, 196, 20, [255, 130, 110], 'center');            return;        }        $c->text('POSITION', $x + 14, 160, 12, [90, 200, 150]);        $c->text(Game::coordText($g->zone->id, $g->x, $g->z), $x + 14, 180, 26, [150, 255, 200]);        $c->text(sprintf('%s · altitude %d m · cap %03d°', $g->zone->name, (int) round($g->y), (int) round(Hud::bearing(sin($g->yaw), cos($g->yaw)))), $x + 14, 216, 13, [110, 200, 160]);        $c->text('Grille d\'Ysor (relevé du Meridian)', $x + 14, 240, 11, [80, 160, 130]);        $c->text('POINTS ENREGISTRÉS', $x, 286, 12, [150, 166, 180]);        $y = 306.0;        if ($g->waypoints === []) {            foreach ($c->wrap('Aucun point. Les journaux de bord contiennent des coordonnées : Journal ou Inventaire → « Insérer dans le GPS ». Tu peux aussi marquer l\'endroit où tu es.', 13, 490) as $i => $line) {                $c->text($line, $x, $y + $i * 17, 13, [170, 186, 200]);            }            $y += 60;        }        foreach ($g->waypoints as $id => [$name, $zone, $wx, $wz]) {            $active = $id === $g->activeWaypoint;            $c->rect($x, $y, 500, 44, $active ? [80, 230, 255, 40] : [255, 255, 255, 12]);            $c->border($x, $y, 500, 44, $active ? [120, 230, 255, 200] : [255, 255, 255, 34]);            $c->circle($x + 16, $y + 22, 6, $active ? [80, 230, 255] : [90, 120, 130]);            $c->text($name, $x + 30, $y + 4, 15, [240, 246, 250]);            $c->text(Game::coordText($zone, $wx, $wz) . ' · ' . \App\Revisited\Land\ZoneFactory::NAMES[$zone] . ' · ' . self::km($g->waypointDistance((string) $id)), $x + 30, $y + 24, 11, [150, 190, 200]);            $c->hit($x, $y, 440, 44, 'wp:' . $id);            $c->line($x + 472, $y + 16, $x + 484, $y + 28, [220, 150, 140], 2);            $c->line($x + 484, $y + 16, $x + 472, $y + 28, [220, 150, 140], 2);            $c->hit($x + 456, $y, 44, 44, 'wpdel:' . $id);            $y += 50;            if ($y > 560) {                break;            }        }        $c->rect($x, 592, 200, 34, [80, 220, 255, 50]);        $c->border($x, 592, 200, 34, [120, 230, 255, 170]);        $c->text('Marquer ici', $x + 100, 599, 15, [255, 255, 255], 'center');        $c->hit($x, 592, 200, 34, 'mark');        // the right column: the active point and the logs        $rx = 590.0;        $c->text('POINT ACTIF', $rx, 150, 12, [150, 166, 180]);        $wt = $g->waypointTarget();        if ($wt === null) {            $c->text($g->activeWaypoint === null ? 'Aucun : clique un point pour le suivre.' : 'Pas de chemin connu d\'ici.', $rx, 170, 13, [170, 186, 200]);        } else {            foreach (array_slice($c->wrap($wt[2], 15, 300), 0, 2) as $i => $line) {                $c->text($line, $rx, 170 + $i * 19, 15, [150, 240, 255]);            }            $c->text(sprintf('%d m · cap %03d°', (int) round($wt[3]), (int) round(Hud::bearing($wt[0] - $g->x, $wt[1] - $g->z))), $rx, 212, 22, [255, 255, 255]);            $c->text($wt[4] ? 'La boussole et la carte le montrent.' : 'Dans une autre zone : la boussole montre le passage.', $rx, 244, 12, [150, 190, 200]);        }        $c->text('JOURNAUX', $rx, 286, 12, [150, 166, 180]);        $y = 306.0;        foreach (['pod_log' => 'Journal de la nacelle', 'colony_log' => 'Journal de la colonie'] as $log => $title) {            if (!$g->inv->has($log)) {                continue;            }            $all = true;            foreach (array_keys($g->logPlaces($log)) as $id) {                $all = $all && isset($g->waypoints[$id]);            }            $c->text($title, $rx, $y, 14, [255, 214, 140]);            $label = $all ? 'Coordonnées insérées' : 'Insérer dans le GPS';            $w = $c->measure($label, 13) + 22;            $c->rect($rx, $y + 20, $w, 26, $all ? [110, 200, 140, 40] : [80, 220, 255, 50]);            $c->border($rx, $y + 20, $w, 26, $all ? [140, 230, 160, 140] : [120, 230, 255, 170]);            $c->text($label, $rx + 11, $y + 25, 13, [255, 255, 255]);            $c->hit($rx, $y + 20, $w, 26, 'gpslog:' . $log);            $y += 60;        }        if ($y === 306.0) {            $c->text('Aucun journal dans le sac.', $rx, $y, 13, [150, 160, 170]);        }    }    private static function km(float $m): string    {        return $m >= 1000 ? Translator::num($m / 1000, 1) . ' km' : sprintf('%d m', (int) round($m));    }    private function ring(Canvas $c, float $cx, float $cy, float $r, array $col): void    {        $n = max(24, (int) ($r / 3));        for ($i = 0; $i < $n; $i++) {            $a0 = 2 * M_PI * $i / $n;            $a1 = 2 * M_PI * ($i + 1) / $n;            $c->line($cx + cos($a0) * $r, $cy + sin($a0) * $r, $cx + cos($a1) * $r, $cy + sin($a1) * $r, $col);        }    }    /** The right column: battery, vitals in brief, the analysis with advice. */    private function bio(Canvas $c, Game $g, float $x): void    {        $bat = $g->scanBattery();        $c->text('BATTERIE', $x, 118, 12, [150, 166, 180]);        $c->rect($x, 136, 280, 14, [255, 255, 255, 20]);        $c->border($x, 136, 280, 14, [255, 255, 255, 60]);        $c->rect($x + 2, 138, 276 * $bat / 100, 10, $bat < 20 ? [230, 80, 60] : ($bat < 50 ? [230, 180, 60] : [90, 220, 150]));        $c->text((int) round($bat) . ' %', $x + 318, 134, 14, [230, 240, 236], 'right');        $c->text('BIO-SCAN', $x, 164, 12, [150, 166, 180]);        $v = $g->body->vitals();        $y = 184.0;        foreach ([['Pouls', BodyPanel::pulse($g) . ' bpm'], ['Température', Translator::num($v['temperature'], 1) . ' °C'],            ['Sang', Translator::num($v['blood'], 1) . ' L'], ['Santé', (int) round($v['health']) . ' %']] as $i => [$l, $val]) {            $lx = $x + ($i % 2) * 160;            $ly = $y + intdiv($i, 2) * 22;            $c->text($l, $lx, $ly, 13, [170, 186, 200]);            $c->text($val, $lx + 150, $ly, 13, [240, 246, 244], 'right');        }        $y += 54;        $c->text('ANALYSE', $x, $y, 12, [150, 166, 180]);        $y += 20;        foreach (array_slice(self::advice($g), 0, 5) as [$sev, $text]) {            $col = $sev > 0.6 ? [255, 120, 100] : ($sev > 0.3 ? [255, 190, 110] : ($sev > 0 ? [230, 220, 140] : [140, 230, 170]));            $lines = array_slice($c->wrap($text, 13, 300), 0, 3);            $c->rect($x, $y + 2, 3, 15 * count($lines) - 2, $col);            foreach ($lines as $k => $line) {                $c->text($line, $x + 10, $y + $k * 15, 13, $col);            }            $y += 15 * count($lines) + 6;            if ($y > 488) {                break;            }        }    }    /** @return list<array{0: float, 1: string}> severity, advice (French), worst first */    public static function advice(Game $g): array    {        $out = [];        foreach ($g->body->injuries() as $w) {            if ($w['treated'] && !str_contains($w['label'], 'infectée')) {                continue;            }            $what = str_contains($w['label'], 'infectée') ? 'appliquer un cataplasme'                : (str_contains($w['label'], 'saigne') ? 'poser un bandage'                : ($w['kind'] === 'fracture' ? 'poser une attelle' : 'surveiller, garder propre'));            $out[] = [$w['severity'] + (str_contains($w['label'], 'infectée') ? 0.3 : 0), $w['label'] . ' : ' . $what . '.'];        }        foreach ($g->body->conditions() as $cd) {            if (isset(self::ADVICE[$cd['id']])) {                $out[] = [$cd['severity'], self::ADVICE[$cd['id']]];            }        }        foreach ($g->inv->wornClothes() as $id) {            if ($g->inv->condition($id) < 35) {                $out[] = [0.2, $g->inv->label($id) . ' : à rapiécer (pièce + aiguille d\'os).'];            }        }        if ($g->inv->wet > 0.5) {            $out[] = [0.25, 'Vêtements trempés : ils te tiennent moins chaud. Sèche-les près d\'un feu.'];        }        usort($out, static fn ($a, $b) => $b[0] <=> $a[0]);        if ($out === []) {            $out[] = [0.0, 'Aucune anomalie détectée. Garde un œil sur l\'eau, la nourriture et le froid.'];        }        return $out;    }}