RU
games/rob-revisited / src / Revisited / Ui / Guide.php · commit f9a1307
<?phpnamespace App\Revisited\Ui;use App\Revisited\Land\Game;use App\Revisited\Land\Quest;use App\Revisited\Land\ZoneFactory;/** * Where the current objective is, for the compass and the map: the first goal not done of the chapter, found in the * zone Rob is in (a thing, a person, a door), else the door toward the zone where it lies. Null: nothing to point at * (drink, eat, a fire: anywhere). */final class Guide{    /** goal => [zone where it lies, how to find it: thing:<kind> | npc:<name> | exit:<zone> | pos:<x>,<z> | water] */    private const GOALS = [        'drink' => ['valley', 'water'],        'pod_log' => ['valley', 'thing:wreck_pod'],        'enter_caves' => ['valley', 'exit:caves1'],        'keycard' => ['caves1', 'note:colonist'],        'reach_caves2' => ['caves1', 'exit:caves2'],        'colony_log' => ['caves2', 'thing:wreck_colony'],        'power_cell' => ['caves2', 'thing:wreck_colony'],        'reach_canyon' => ['caves2', 'exit:canyon'],        'meet_elder' => ['canyon', 'npc:Maëva'],        'tuner' => ['canyon', 'camp'],        'return_tuner' => ['canyon', 'npc:Maëva'],        'reach_plateau' => ['canyon', 'exit:plateau'],        'reach_mast' => ['plateau', 'mast'],        'fit_cell' => ['plateau', 'mast'],        'fit_tuner' => ['plateau', 'mast'],        'repair_antenna' => ['plateau', 'mast'],        'transmit' => ['plateau', 'mast'],        'nav_core' => ['canyon', 'npc:Maëva'],        'reach_jungle' => ['valley', 'exit:jungle'],        'meet_ouvi' => ['jungle', 'npc:Aïka'],        'raft' => ['jungle', 'water'],        'heat_plates' => ['jungle', 'npc:Aïka'],        'reach_caves3' => ['jungle', 'exit:caves3'],        'rescue_jonas' => ['caves3', 'note:jonas'],        'reach_marsh' => ['caves3', 'exit:marsh'],        'launch_codes' => ['marsh', 'npc:Orsolo'],        'ignition_key' => ['canyon', 'camp'],        'reach_coast' => ['caves3', 'exit:coast'],        'parts_fitted' => ['coast', 'thing:shuttle'],        'fuel' => ['coast', 'thing:shuttle'],        'repaired' => ['coast', 'npc:Jonas Merle'],        'launch' => ['coast', 'thing:shuttle'],    ];    /** @var array<string, array{0: float, 1: float}|null> water points per zone */    private array $water = [];    /** @return array{0: float, 1: float, 2: string}|null x, z, what it is (for the map's legend) */    public function target(Game $g): ?array    {        $goal = null;        foreach (array_keys(Quest::CHAPTERS[Quest::chapter($g)]['goals'] ?? []) as $id) {            if (!Quest::done($g, $id) && isset(self::GOALS[$id])) {                $goal = $id;                break;            }        }        if ($goal === null) {            return null;        }        [$zone, $how] = self::GOALS[$goal];        if ($zone !== $g->zone->id) {            $toward = self::nextZone($g->zone->id, $zone);            return $toward === null ? null : $this->find($g, 'exit:' . $toward);        }        return $this->find($g, $how);    }    /** The zone next door on the shortest way between two zones (the doors' graph), null: none. */    public static function nextZone(string $from, string $to): ?string    {        $prev = [$from => null];        $queue = [$from];        while ($queue !== []) {            $z = array_shift($queue);            if ($z === $to) {                break;            }            foreach (ZoneFactory::exits($z) as $e) {                if (!array_key_exists($e['to'], $prev)) {                    $prev[$e['to']] = $z;                    $queue[] = $e['to'];                }            }        }        if (!array_key_exists($to, $prev)) {            return null;        }        $step = $to;        while ($prev[$step] !== null && $prev[$step] !== $from) {            $step = $prev[$step];        }        return $step;    }    /** @return array{0: float, 1: float, 2: string}|null */    private function find(Game $g, string $how): ?array    {        $z = $g->zone;        if (str_starts_with($how, 'exit:')) {            $to = substr($how, 5);            foreach ($z->exits as $e) {                if ($e['to'] === $to) {                    return [$e['x'], $e['z'], $e['label']];                }            }            return null;        }        if (str_starts_with($how, 'thing:') || str_starts_with($how, 'note:')) {            $kind = substr($how, strpos($how, ':') + 1);            $note = str_starts_with($how, 'note:');            foreach ($z->things as $t) {                if ($note ? (($t->data['note'] ?? '') === $kind) : $t->kind === $kind) {                    return [$t->x, $t->z, (string) ($t->data['label'] ?? $kind)];                }            }            return null;        }        if (str_starts_with($how, 'npc:')) {            foreach ($g->npcs as $n) {                if ($n->name === substr($how, 4)) {                    return [$n->x, $n->z, $n->name];                }            }            $c = $g->villageCentre();            return $c === null ? null : [(float) $c[0], (float) $c[1], 'Village haldi'];        }        if ($how === 'camp' && $z->id !== 'canyon') {            return null;        }        if ($how === 'camp') {            $c = ZoneFactory::campCentre($g->seed);            return [(float) $c[0], (float) $c[1], 'Camp des Rôdeurs'];        }        if ($how === 'mast') {            return [ZoneFactory::MAST[0], ZoneFactory::MAST[1], 'Mât relais'];        }        if ($how === 'water') {            if (!array_key_exists($z->id, $this->water)) {                $this->water[$z->id] = $this->nearestWater($g);            }            $w = $this->water[$z->id];            return $w === null ? null : [$w[0], $w[1], 'Eau'];        }        return null;    }    /** @return array{0: float, 1: float}|null the water closest to the crash site (a coarse scan, once per zone) */    private function nearestWater(Game $g): ?array    {        $z = $g->zone;        $best = null;        $step = $z->cell * 2;        for ($pz = $step / 2; $pz < $z->size; $pz += $step) {            for ($px = $step / 2; $px < $z->size; $px += $step) {                if ($z->waterDepth($px, $pz) > 0.3) {                    $d = hypot($px - $g->x, $pz - $g->z);                    if ($best === null || $d < $best[2]) {                        $best = [$px, $pz, $d];                    }                }            }        }        foreach ($z->things as $t) {            if ($t->kind === 'water_spring') {                $d = hypot($t->x - $g->x, $t->z - $g->z);                if ($best === null || $d < $best[2]) {                    $best = [$t->x, $t->z, $d];                }            }        }        return $best === null ? null : [$best[0], $best[1]];    }}