($grid + 1)² ground heights, row by row (z then x) */ public array $heights = []; /** @var list caves: roof heights, same grid; empty outdoors */ public array $ceilings = []; /** @var list one biome per cell (grid²): grass jungle mud sand rock snow ice cave redrock */ public array $biomes = []; /** @var list one flag per cell (grid²): a wall / cliff the player cannot enter */ public array $solid = []; /** @var list */ public array $things = []; /** @var list */ public array $exits = []; public float $water = -1000.0; // water level (lakes), -1000: none public float $baseTemp = 24.0; // °C at noon (caves: constant) public bool $cave = false; public function __construct( public string $id, public string $name, public int $grid, public float $cell, public int $seed, ) { $this->size = $grid * $cell; } public float $size; /** Grows each time a thing is added, removed or changed (the renderer rebuilds what it caches). */ public int $version = 0; /** @var array>|null things by 16 m square, built on demand */ private ?array $buckets = null; /** @var array|null */ private ?array $byId = null; /** @var array> Solids circles by 4 m square (built for $solidVersion) */ private array $solidGrid = []; private int $solidVersion = -1; private int $solidCount = -1; /** * The solid thing (Solids::SHAPES) a body of radius $r standing at ($x, $z) would overlap: its circle [x, z, radius], * null: none. One look into a 4 m square (each circle is filed in every square it can touch). * @return array{0: float, 1: float, 2: float}|null */ public function solidHit(float $x, float $z, float $r): ?array { if ($this->solidVersion !== $this->version || $this->solidCount !== count($this->things)) { $this->solidGrid = []; foreach ($this->things as $t) { foreach (Solids::circles($t) as $c) { $reach = $c[2] + Solids::MAX_BODY; for ($bz = (int) floor(($c[1] - $reach) / 4); $bz <= (int) floor(($c[1] + $reach) / 4); $bz++) { for ($bx = (int) floor(($c[0] - $reach) / 4); $bx <= (int) floor(($c[0] + $reach) / 4); $bx++) { $this->solidGrid[$bz * 4096 + $bx][] = $c; } } } } $this->solidVersion = $this->version; $this->solidCount = count($this->things); } foreach ($this->solidGrid[(int) floor($z / 4) * 4096 + (int) floor($x / 4)] ?? [] as $c) { $dx = $x - $c[0]; $dz = $z - $c[1]; $rr = $c[2] + $r; if ($dx * $dx + $dz * $dz < $rr * $rr) { return $c; } } return null; } /** @return list the things within $r metres of a point (2D) */ public function thingsNear(float $x, float $z, float $r): array { if ($this->buckets === null) { $this->buckets = []; foreach ($this->things as $t) { $this->buckets[self::bucketKey($t->x, $t->z)][] = $t; } } $out = []; $r2 = $r * $r; for ($bz = (int) floor(($z - $r) / 16); $bz <= (int) floor(($z + $r) / 16); $bz++) { for ($bx = (int) floor(($x - $r) / 16); $bx <= (int) floor(($x + $r) / 16); $bx++) { foreach ($this->buckets[$bz * 1000 + $bx] ?? [] as $t) { $dx = $t->x - $x; $dz = $t->z - $z; if ($dx * $dx + $dz * $dz <= $r2) { $out[] = $t; } } } } return $out; } /** @var list|null the things whose solid shape reaches far from their centre (the shuttle, the wrecks): thingsAhead() */ private ?array $big = null; private int $bigCount = -1; /** @return list the solid things wider than 4 m, found from further than thingsNear() looks */ public function bigThings(): array { if ($this->big === null || $this->bigCount !== count($this->things)) { $this->big = []; foreach ($this->things as $t) { foreach (Solids::SHAPES[$t->kind] ?? [] as [$lx, $lz, $r]) { if (hypot($lx, $lz) + $r > 4.0) { $this->big[] = $t; break; } } } $this->bigCount = count($this->things); } return $this->big; } public function thing(int $id): ?Thing { if ($this->byId === null) { $this->byId = []; foreach ($this->things as $t) { $this->byId[$t->id] = $t; } } return $this->byId[$id] ?? null; } public function addThing(Thing $t): void { $this->things[] = $t; $this->buckets = null; $this->byId = null; $this->version++; } public function removeThing(Thing $t): void { $k = array_search($t, $this->things, true); if ($k !== false) { array_splice($this->things, $k, 1); } $this->buckets = null; $this->byId = null; $this->version++; } /** Is the point under the water of the zone's lakes? */ public function waterDepth(float $x, float $z): float { return $this->water - $this->heightAt($x, $z); } private static function bucketKey(float $x, float $z): int { return (int) floor($z / 16) * 1000 + (int) floor($x / 16); } public function heightAt(float $x, float $z): float { return self::bilinear($this->heights, $this->grid, $this->cell, $x, $z); } public function ceilingAt(float $x, float $z): ?float { return $this->ceilings === [] ? null : self::bilinear($this->ceilings, $this->grid, $this->cell, $x, $z); } public function biomeAt(float $x, float $z): string { return $this->biomes[$this->cellIndex($x, $z)] ?? 'grass'; } public function solidAt(float $x, float $z): bool { if ($x < 0 || $z < 0 || $x >= $this->size || $z >= $this->size) { return true; } return $this->solid[$this->cellIndex($x, $z)] ?? false; } public function cellIndex(float $x, float $z): int { $ix = max(0, min($this->grid - 1, (int) floor($x / $this->cell))); $iz = max(0, min($this->grid - 1, (int) floor($z / $this->cell))); return $iz * $this->grid + $ix; } /** @param list $grid */ private static function bilinear(array $grid, int $n, float $cell, float $x, float $z): float { if ($grid === []) { return 0.0; } $cx = max(0.0, min($n - 0.0001, $x / $cell)); $cz = max(0.0, min($n - 0.0001, $z / $cell)); $ix = (int) $cx; $iz = (int) $cz; $fx = $cx - $ix; $fz = $cz - $iz; $w = $n + 1; $a = $grid[$iz * $w + $ix] + ($grid[$iz * $w + $ix + 1] - $grid[$iz * $w + $ix]) * $fx; $b = $grid[($iz + 1) * $w + $ix] + ($grid[($iz + 1) * $w + $ix + 1] - $grid[($iz + 1) * $w + $ix]) * $fx; return $a + ($b - $a) * $fz; } }