the gauge whose actions help */ public const COND_KEY = [ 'bleeding' => 'blood', 'shock' => 'blood', 'infection' => 'infection', 'fever' => 'fever', 'poison' => 'poison', 'dysentery' => 'thirst', 'hypothermia' => 'temperature', 'heatstroke' => 'temperature', 'exhaustion' => 'fatigue', 'starvation' => 'hunger', 'dehydration' => 'thirst', 'concussion' => 'fatigue', 'cold_hands' => 'temperature', 'cold_feet' => 'temperature', ]; public ?string $key = null; public string $title = ''; public string $info = ''; /** @var list label, action (null: a note), detail */ public array $rows = []; public int $sel = 0; public float $x = 26.0; /** what the last action said */ public string $said = ''; /** The explanation of a gauge or a condition (tooltips, the menu's head). */ public static function tooltip(Game $g, string $key): string { $v = $g->body->vitals(); $x = $g->body->extremityTemps(); return match ($key) { 'health' => sprintf('Santé %d / 100. Les blessures, le froid, la faim, la soif et le poison la font baisser ; elle remonte doucement au repos, nourri et désaltéré.', (int) round($v['health'])), 'blood' => sprintf('Sang %s L sur 5,5. Une plaie qui saigne le vide ; sous 3,6 L, c\'est le choc. Un bandage arrête le saignement.', Translator::num($v['blood'], 1)), 'temperature' => sprintf('Température du corps %s °C (normale 37). Air %s °C, habits +%s °C. Mains %s °C, pieds %s °C.', Translator::num($v['temperature'], 1), Translator::num($g->airTemp(), 0), Translator::num($g->inv->warmth(), 1), (int) round($x['hands']), (int) round($x['feet'])), 'hunger' => sprintf('Satiété %d / 100. Baisse plus vite quand tu as froid, que tu cours ou que tu es malade. Cuite, la nourriture nourrit mieux.', (int) round($v['hunger'])), 'thirst' => sprintf('Hydratation %d / 100. Baisse vite à la chaleur et à l\'effort. L\'eau de lac donne la dysenterie : bouillie, filtrée, de source ou de cascade.', (int) round($v['thirst'])), 'fatigue' => sprintf('Repos %d / 100. Dormir le remonte, mieux à l\'abri et près d\'un feu ; épuisé, tu es lent et maladroit.', (int) round($v['fatigue'])), 'morale' => sprintf('Moral %d / 100. La douleur, la faim, la soif et le froid l\'usent ; un bon repas, un feu, un abri, une parole amie le relèvent.', (int) round($v['morale'])), 'infection' => 'Infection : une plaie sale s\'infecte avec le temps. Un cataplasme ou la trousse la nettoie.', 'fever' => 'Fièvre : la tisane de fièvre la fait tomber ; dors au chaud.', 'poison' => 'Poison dans le sang : un antidote, vite.', default => '', }; } /** * Opens the menu of a gauge ("health"...) or of a condition ("cond:bleeding"). * @param array $hooks body(view, part), sleep(), rest(), point(x, z, label) */ public function open(Game $g, string $hit, float $x, array $hooks): void { $cond = str_starts_with($hit, 'cond:') ? substr($hit, 5) : null; $key = $cond !== null ? (self::COND_KEY[$cond] ?? 'health') : $hit; $this->key = $hit; $this->sel = 0; $this->said = ''; $this->x = max(16.0, min(Canvas::W - 376, $x)); $label = Hud::VITALS[$key][0] ?? ucfirst($key); if ($cond !== null) { foreach ($g->body->conditions() as $c) { if ($c['id'] === $cond) { $label = $c['label']; } } } $this->title = $label; $this->info = self::tooltip($g, $key); $this->rows = $this->rowsFor($g, $key, $cond, $hooks); if ($this->rows === []) { $this->rows[] = ['Rien à faire pour l\'instant.', null, '']; } } public function close(): void { $this->key = null; } /** @return list */ private function rowsFor(Game $g, string $key, ?string $cond, array $hooks): array { $rows = []; $v = $g->body->vitals(); $add = static function (string $label, ?\Closure $run, string $detail = '') use (&$rows): void { $rows[] = [$label, $run, $detail]; }; $partName = BodyPanel::PARTS; switch ($key) { case 'health': case 'blood': case 'infection': foreach ($g->body->injuries() as $w) { $bleeds = str_contains($w['label'], 'saigne'); $infected = str_contains($w['label'], 'infectée'); $p = $w['part']; if ($bleeds) { $item = $g->inv->has('bandage') ? 'bandage' : ($g->inv->has('medkit') ? 'medkit' : null); $add($item !== null ? 'Bander tout de suite : ' . mb_strtolower($partName[$p]) : 'Saigne (' . mb_strtolower($partName[$p]) . ') : pas de bandage !', $item !== null ? fn () => $g->useItem($item, $p) : null, $item !== null ? Items::name($item) : 'artisanat : tissu ou fibres'); } elseif ($infected) { $item = $g->inv->has('poultice') ? 'poultice' : ($g->inv->has('medkit') ? 'medkit' : null); $add($item !== null ? 'Soigner l\'infection : ' . mb_strtolower($partName[$p]) : 'Plaie infectée : il faut un cataplasme', $item !== null ? fn () => $g->useItem($item, $p) : null, $item !== null ? Items::name($item) : 'artisanat : herbes cicatrisantes'); } elseif ($w['kind'] === 'fracture' && !$w['treated']) { $item = $g->inv->has('splint') ? 'splint' : ($g->inv->has('medkit') ? 'medkit' : null); $add($item !== null ? 'Poser une attelle : ' . mb_strtolower($partName[$p]) : 'Fracture : il faut une attelle', $item !== null ? fn () => $g->useItem($item, $p) : null, $item !== null ? Items::name($item) : 'artisanat : bâtons et corde'); } } $worst = BodyPanel::worstPart($g); $add('Ouvrir le corps (vue médicale)', isset($hooks['body']) ? fn () => ($hooks['body'])('medical', $worst) : null, $partName[$worst]); if ($v['health'] < 85 && $g->inv->has('herb_heal')) { $add('Mâcher une herbe cicatrisante', fn () => $g->useItem('herb_heal'), '+2 santé'); } if ($key !== 'infection') { $add('Dormir pour récupérer…', $hooks['sleep'] ?? null, 'le corps guérit mieux en dormant'); } break; case 'fever': if ($g->inv->has('tea_fever')) { $add('Boire la tisane de fièvre', fn () => $g->useItem('tea_fever'), ''); } else { $add('Pas de tisane : herbe de fièvre + eau sûre + pot, au feu', null, ''); } $add('Dormir au chaud…', $hooks['sleep'] ?? null, ''); break; case 'poison': foreach (['antidote', 'herb_antidote'] as $id) { if ($g->inv->has($id) && $id === 'antidote') { $add('Prendre l\'antidote', fn () => $g->useItem($id), ''); } } if (!$g->inv->has('antidote')) { $add('Pas d\'antidote : herbe à venin + eau sûre + pot, au feu', null, ''); } break; case 'temperature': $hot = $v['temperature'] > 38 && $v['fever'] < 0.2; if ($hot) { foreach (['worn', 'head', 'legs', 'hands'] as $slot) { if ($g->inv->$slot !== null) { $add('Ôter : ' . Items::name((string) $g->inv->$slot), fn () => $g->unequip($slot), ''); } } $add('Boire, se mettre à l\'ombre et au repos', null, ''); break; } foreach (Inventory::CLOTH_SLOTS as $slot) { $spare = $g->inv->$slot === null ? BodyPanel::spare($g, $slot) : null; if ($spare !== null) { $add('Enfiler : ' . Items::name($spare), fn () => $g->useItem($spare), '+' . Translator::num($g->inv->garmentWarmth($spare), 1) . ' °C'); } } if ($g->nearFire()) { $add('Tu es près du feu : reste au chaud', isset($hooks['rest']) ? $hooks['rest'] : null, 'se reposer 20 min'); if ($this->option($g, 'fuel')) { $add('Remettre du bois dans le feu', fn () => (string) $g->act('fuel'), ''); } } else { if ($this->option($g, 'fire')) { $add('Allumer un feu ici', fn () => (string) $g->act('fire'), 'outil à feu en main + bois'); } elseif ($g->fireInHand() === null && $g->inv->has('fire_drill')) { // the drill stays in the bag: "Allumer un feu" only comes with it in hand $add('Prendre le foret à feu en main', fn () => $g->useItem('fire_drill'), 'puis : allumer un feu'); } elseif ($g->fireInHand() !== null) { $add('Allumer un feu : il te faut du bois', null, 'une bûche ou trois bâtons'); } else { $add('Allumer un feu : il faut un foret à feu', null, 'artisanat : 2 bâtons, fibres'); } $f = self::nearestFire($g); if ($f !== null && isset($hooks['point'])) { $add(sprintf('Aller au feu (%d m)', (int) round($f[2])), fn () => ($hooks['point'])($f[0], $f[1], 'Feu'), 'la boussole le montre'); } } if ($g->inv->has('shelter_kit') && !$g->sheltered()) { $add('Monter ton abri', fn () => $g->useItem('shelter_kit'), 'coupe le vent et la pluie'); } if ($g->inv->wet > 0.3) { $add(sprintf('Habits mouillés à %d %% : sèche-les près d\'un feu', (int) round($g->inv->wet * 100)), null, ''); } break; case 'hunger': foreach (self::foods($g) as [$id, $score]) { $d = Items::get($id); $add('Manger : ' . Items::name($id), fn () => $g->useItem($id), '+' . (int) ($d['food'] ?? 0) . (isset($d['risk']) ? ' · risqué' : '')); if (count($rows) >= 5) { break; } } if ($g->nearFire() && $g->inv->has('meat_raw')) { $add('Griller la viande au feu', fn () => $g->craft('meat_cooked'), '15 min'); } if ($g->nearFire() && $g->inv->count('earthworm') >= 4) { $add('Griller des vers', fn () => $g->craft('worms_cooked'), '8 min'); } if ($rows === []) { $add('Rien à manger dans le sac', null, ''); } foreach (['worms' => 'Fouiller la terre (vers)', 'roots' => 'Creuser pour des racines', 'fish' => 'Pêcher'] as $id => $label) { if ($this->option($g, $id)) { $add($label, fn () => (string) $g->act($id), ''); } } break; case 'thirst': foreach (self::drinks($g) as $id) { $d = Items::get($id); $add('Boire : ' . Items::name($id), fn () => $g->useItem($id), '+' . (int) ($d['water'] ?? 0) . (isset($d['risk']['dirty']) ? ' · risqué' : ' · sûre')); } foreach (['drink' => null, 'fill' => null, 'boil' => null] as $id => $_) { $o = $this->label($g, $id); if ($o !== null) { $add($o, fn () => (string) $g->act($id), ''); } } if ($g->inv->has('gourd_dirty') && $g->inv->has('water_filter')) { $add('Filtrer une gourde d\'eau trouble', fn () => $g->filterWater(), '10 min'); } if (($g->inv->has('gourd_dirty') || $g->inv->has('gourd_filtered')) && $g->inv->has('purification_tablets')) { $add('Purifier une gourde (pastille)', fn () => $g->purifyWater(), '30 min'); } $w = self::nearestWater($g); if ($w !== null && isset($hooks['point']) && $this->label($g, 'drink') === null) { $add(sprintf('Aller à l\'eau : %s (%d m)', $w[2], (int) round($w[3])), fn () => ($hooks['point'])($w[0], $w[1], $w[2]), 'la boussole la montre'); } if ($cond === 'dysentery') { $add('Dysenterie : ne bois que de l\'eau sûre, repose-toi', $hooks['rest'] ?? null, 'se reposer 20 min'); } break; case 'fatigue': $add('Dormir…', $hooks['sleep'] ?? null, $g->sleepInfo()['label']); $add('Se reposer 20 min (assis)', $hooks['rest'] ?? null, 'reprendre son souffle'); break; case 'morale': foreach (self::foods($g) as [$id, $score]) { if ((float) (Items::get($id)['morale'] ?? 0) > 0) { $add('Manger : ' . Items::name($id), fn () => $g->useItem($id), 'un bon repas remonte le moral'); break; } } foreach ($g->npcs as $n) { if ($n->faction === 'haldi' && hypot($n->x - $g->x, $n->z - $g->z) < 60) { if ($this->option($g, 'talk')) { $add('Parler à ' . $n->name, fn () => (string) $g->act('talk'), ''); } elseif (isset($hooks['point'])) { $add('Aller parler à ' . $n->name, fn () => ($hooks['point'])($n->x, $n->z, $n->name), 'la boussole le montre'); } break; } } if ($g->nearFire()) { $add('Se reposer près du feu', $hooks['rest'] ?? null, '20 min'); } $add('Dormir…', $hooks['sleep'] ?? null, ''); break; } return $rows; } /** Is a context action (Game::actions()) possible here? */ private function option(Game $g, string $id): bool { return $this->label($g, $id) !== null; } private function label(Game $g, string $id): ?string { foreach ($g->actions() as $a) { if ($a['id'] === $id) { return $a['label']; } } return null; } /** @return list the foods of the bag, best first (cooked, safe, filling) */ public static function foods(Game $g): array { $out = []; foreach ($g->inv->all() as $id => $n) { $d = Items::get($id); if ($d['kind'] !== 'food') { continue; } $score = (float) ($d['food'] ?? 0) + 0.5 * (float) ($d['morale'] ?? 0) - (isset($d['risk']['poison']) ? 80 : 0) - (isset($d['risk']['raw']) ? 18 : 0); $out[] = [(string) $id, $score]; } usort($out, static fn ($a, $b) => $b[1] <=> $a[1]); return $out; } /** @return list the drinks of the bag, the safest first */ public static function drinks(Game $g): array { $order = ['gourd_clean' => 0, 'gourd_boiled' => 1, 'gourd_purified' => 2, 'gourd_filtered' => 3, 'gourd_dirty' => 4]; $out = []; foreach ($g->inv->all() as $id => $n) { if (Items::get($id)['kind'] === 'drink') { $out[] = (string) $id; } } usort($out, static fn ($a, $b) => ($order[$a] ?? 5) <=> ($order[$b] ?? 5)); return $out; } /** @return array{0: float, 1: float, 2: float}|null x, z, distance of the nearest fire burning */ public static function nearestFire(Game $g): ?array { $best = null; foreach ($g->fires as $f) { $d = hypot($f['x'] - $g->x, $f['z'] - $g->z); if ($f['minutes'] > 0 && $d < 150 && ($best === null || $d < $best[2])) { $best = [(float) $f['x'], (float) $f['z'], $d]; } } return $best; } /** @return array{0: float, 1: float, 2: string, 3: float}|null the nearest water: a spring or a waterfall first, else the lake */ public static function nearestWater(Game $g): ?array { $best = null; foreach ($g->zone->thingsNear($g->x, $g->z, 200.0) as $t) { if ($t->kind === 'water_spring' || $t->kind === 'waterfall') { $x = $t->kind === 'waterfall' ? $t->x + sin($t->yaw) * 8 : $t->x; $z = $t->kind === 'waterfall' ? $t->z + cos($t->yaw) * 8 : $t->z; $d = hypot($x - $g->x, $z - $g->z) * 0.7; // clean water is worth a longer walk if ($best === null || $d < $best[3]) { $best = [$x, $z, $t->kind === 'waterfall' ? 'cascade' : 'source', $d]; } } } $z = $g->zone; if ($z->water > -999) { for ($r = 6.0; $r <= 120; $r += 6) { for ($k = 0; $k < 16; $k++) { $a = $k * M_PI / 8; $x = $g->x + sin($a) * $r; $zz = $g->z + cos($a) * $r; if ($z->waterDepth($x, $zz) > 0.2) { if ($best === null || $r < $best[3]) { $best = [$x, $zz, $z->cave ? 'bassin' : 'lac', $r]; } break 2; } } } } if ($best !== null) { $best[3] = hypot($best[0] - $g->x, $best[1] - $g->z); } return $best; } /** Returns false when the menu closes. A row's action is run and its text kept in $said. */ public function update(UiInput $in, Game $g): bool { if ($this->key === null || $in->back) { return false; } $n = count($this->rows); if ($in->up) { $this->sel = ($this->sel - 1 + $n) % $n; } if ($in->down) { $this->sel = ($this->sel + 1) % $n; } if ($in->moved && $in->hover !== null && str_starts_with($in->hover, 'vrow:')) { $this->sel = (int) substr($in->hover, 5); } $pick = null; if ($in->ok) { $pick = $this->sel; } elseif ($in->digit >= 1 && $in->digit <= $n) { $pick = $in->digit - 1; } elseif ($in->click) { if ($in->hit !== null && str_starts_with($in->hit, 'vrow:')) { $pick = (int) substr($in->hit, 5); } elseif ($in->hit === null || !str_starts_with($in->hit, 'vmenu')) { return false; // a click elsewhere closes it } } if ($pick !== null && isset($this->rows[$pick])) { $run = $this->rows[$pick][1]; if ($run === null) { return true; } $text = $run(); $this->said = is_string($text) ? $text : ''; if ($this->said !== '') { $g->say($this->said); } return false; } return true; } public function draw(Canvas $c): void { if ($this->key === null) { return; } $w = 360.0; $info = $c->wrap($this->info, 12, $w - 28); $h = 44 + 15 * count($info) + 34 * count($this->rows) + 10; $y = 626 - $h; $x = $this->x; $c->panel($x, $y, $w, $h, 232, [120, 190, 255, 220]); $c->hit($x, $y, $w, $h, 'vmenu'); $c->text($this->title, $x + 14, $y + 10, 17, [255, 255, 255]); $c->text('Échap : fermer', $x + $w - 12, $y + 13, 11, [130, 146, 160], 'right'); foreach ($info as $i => $line) { $c->text($line, $x + 14, $y + 36 + $i * 15, 12, [176, 192, 206]); } $ry = $y + 44 + 15 * count($info); foreach ($this->rows as $i => [$label, $run, $detail]) { $on = $i === $this->sel && $run !== null; if ($on) { $c->rect($x + 6, $ry, $w - 12, 30, [120, 190, 255, 60]); $c->rect($x + 6, $ry, 3, 30, [150, 210, 255, 230]); } $c->text(($i + 1) . '.', $x + 16, $ry + 7, 12, $run !== null ? [150, 210, 255] : [110, 120, 130]); $c->text($c->wrap($label, 14, 210)[0], $x + 36, $ry + 6, 14, $run !== null ? [255, 255, 255] : [160, 168, 176]); if ($detail !== '') { $c->text($c->wrap($detail, 11, 108)[0], $x + $w - 14, $ry + 8, 11, [150, 200, 170], 'right'); } $c->hit($x + 6, $ry, $w - 12, 30, 'vrow:' . $i); $ry += 34; } } }