ZH
games/alis / src / Revisited / World.php · commit 9c722cf
<?phpnamespace App\Revisited;/** * "Experience - Rob revisited" (an original survival prototype, not a remake): the planet's land around the crash * site. A height field of SIZE x SIZE cells of CELL metres (value noise, seeded), a lake, trees, fruit bushes, * dead wood, the wreck. Plain data: the scene draws it, the tests read it. */final class World{    public const SIZE = 96;          // cells per side    public const CELL = 4.0;         // metres per cell    public const WATER = 1.2;        // the lake's level (metres)    /** @var list<float> heights, row by row (SIZE + 1 points per side) */    public array $height = [];    /** @var list<array{x: float, z: float, kind: string, left: int}> things on the ground: tree, bush (fruit), wood, wreck */    public array $things = [];    public function __construct(public readonly int $seed = 7)    {        mt_srand($seed);        $n = self::SIZE + 1;        $lattice = [];        for ($i = 0; $i < 13 * 13; $i++) {            $lattice[] = mt_rand() / mt_getrandmax();        }        for ($z = 0; $z < $n; $z++) {            for ($x = 0; $x < $n; $x++) {                $u = $x / self::SIZE;                $v = $z / self::SIZE;                $h = 16 * self::noise($lattice, $u * 3, $v * 3) + 5 * self::noise($lattice, $u * 9 + 3, $v * 9 + 5)                    + 1.2 * self::noise($lattice, $u * 27, $v * 27);                // mountains at the edges (the valley of the crash in the middle)                $edge = max(abs($u - 0.5), abs($v - 0.5)) * 2;                $h += 30 * max(0, $edge - 0.6) ** 2 * 6;                $this->height[] = $h - 6;            }        }        $centre = self::SIZE * self::CELL / 2;        $this->things[] = ['x' => $centre + 6, 'z' => $centre + 4, 'kind' => 'wreck', 'left' => 0];        for ($i = 0; $i < 900 && count($this->things) < 320; $i++) {            $x = mt_rand(8, self::SIZE * 4 - 8) + mt_rand() / mt_getrandmax();            $z = mt_rand(8, self::SIZE * 4 - 8) + mt_rand() / mt_getrandmax();            $h = $this->heightAt($x, $z);            if ($h < self::WATER + 0.4 || $h > 18) {                continue;            }            $r = mt_rand(0, 99);            $kind = $r < 50 ? 'tree' : ($r < 68 ? 'bush' : ($r < 84 ? 'wood' : 'rock'));            $this->things[] = ['x' => $x, 'z' => $z, 'kind' => $kind, 'left' => $kind === 'bush' ? 3 : ($kind === 'wood' ? 1 : 0)];        }    }    private static function noise(array $l, float $x, float $y): float    {        $xi = (int) floor($x);        $yi = (int) floor($y);        $fx = $x - $xi;        $fy = $y - $yi;        $fx = $fx * $fx * (3 - 2 * $fx);        $fy = $fy * $fy * (3 - 2 * $fy);        $g = static fn (int $i, int $j): float => $l[(($i % 13 + 13) % 13) * 13 + (($j % 13 + 13) % 13)];        $a = $g($xi, $yi) + ($g($xi + 1, $yi) - $g($xi, $yi)) * $fx;        $b = $g($xi, $yi + 1) + ($g($xi + 1, $yi + 1) - $g($xi, $yi + 1)) * $fx;        return $a + ($b - $a) * $fy;    }    /** The ground's height at a point (metres), bilinear. */    public function heightAt(float $x, float $z): float    {        $cx = max(0.0, min(self::SIZE - 0.001, $x / self::CELL));        $cz = max(0.0, min(self::SIZE - 0.001, $z / self::CELL));        $ix = (int) $cx;        $iz = (int) $cz;        $fx = $cx - $ix;        $fz = $cz - $iz;        $n = self::SIZE + 1;        $h = $this->height;        $a = $h[$iz * $n + $ix] + ($h[$iz * $n + $ix + 1] - $h[$iz * $n + $ix]) * $fx;        $b = $h[($iz + 1) * $n + $ix] + ($h[($iz + 1) * $n + $ix + 1] - $h[($iz + 1) * $n + $ix]) * $fx;        return $a + ($b - $a) * $fz;    }    public function extent(): float    {        return self::SIZE * self::CELL;    }    /** The nearest thing of these kinds within $reach metres (its index), or null. */    public function nearest(float $x, float $z, float $reach, array $kinds): ?int    {        $best = null;        $bestD = $reach * $reach;        foreach ($this->things as $i => $t) {            if (!in_array($t['kind'], $kinds, true)) {                continue;            }            $d = ($t['x'] - $x) ** 2 + ($t['z'] - $z) ** 2;            if ($d < $bestD) {                $best = $i;                $bestD = $d;            }        }        return $best;    }}