<?phpnamespace App\Revisited\Ui;use App\Revisited\Land\Game;use App\Revisited\Land\Zone;use JumPHP\Renderer\Sdl;/** * The zone's map as a texture, made once per zone: the biomes' colours, shaded by the slopes (light from the north-west), * the lakes, the cave walls. Pixel (i, j) = the cell (i, j): x to the right, z downward (north up on the compass). */final class MapView{ private const BIOME = [ 'grass' => [92, 132, 64], 'jungle' => [46, 102, 52], 'mud' => [104, 88, 60], 'sand' => [196, 178, 128], 'rock' => [120, 114, 106], 'snow' => [226, 232, 238], 'ice' => [176, 208, 226], 'cave' => [86, 78, 70], 'redrock' => [168, 84, 52], ]; private ?string $zoneId = null; private mixed $texture = null; private int $n = 0; public function __construct(private Sdl $sdl) { } /** The texture of the zone (built at the first call for that zone), null when it can not be made. */ public function texture(Zone $z): mixed { if ($this->zoneId === $z->id) { return $this->texture; } $this->zoneId = $z->id; $this->texture = null; try { $this->texture = $this->build($z); } catch (\Throwable) { $this->texture = null; } return $this->texture; } private function build(Zone $z): mixed { $n = $z->grid; $this->n = $n; $w = $n + 1; $h = $z->heights; $im = imagecreatetruecolor($n, $n); $cols = []; for ($j = 0; $j < $n; $j++) { for ($i = 0; $i < $n; $i++) { $k = $j * $n + $i; $a = $h[$j * $w + $i] ?? 0.0; $b = $h[$j * $w + $i + 1] ?? $a; $c = $h[($j + 1) * $w + $i] ?? $a; $height = ($a + $b + $c + ($h[($j + 1) * $w + $i + 1] ?? $a)) / 4; $solid = $z->solid[$k] ?? false; if ($solid) { $rgb = $z->cave ? [28, 24, 22] : [70, 64, 60]; } elseif ($z->water > -999 && $z->water - $height > 0.0) { $deep = min(1.0, ($z->water - $height) / 4); $rgb = [(int) (64 - 34 * $deep), (int) (128 - 46 * $deep), (int) (160 - 30 * $deep)]; } else { $rgb = self::BIOME[$z->biomes[$k] ?? 'grass'] ?? [100, 100, 100]; // hill shading: the slope facing the north-west light is brighter $shade = 1.0 + (($a - $b) + ($a - $c)) / ($z->cell * 2.2); $shade = max(0.55, min(1.35, $shade)); $rgb = [(int) min(255, $rgb[0] * $shade), (int) min(255, $rgb[1] * $shade), (int) min(255, $rgb[2] * $shade)]; } $key = ($rgb[0] >> 2) << 12 | ($rgb[1] >> 2) << 6 | ($rgb[2] >> 2); $col = $cols[$key] ??= imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]); imagesetpixel($im, $i, $j, $col); } } // smoothed up (the window shows it several times bigger than the grid) $big = imagescale($im, $n * 4, $n * 4, IMG_BILINEAR_FIXED); $slices = $this->sdl->createTextureFromGd('rv_map_' . $z->id, $big === false ? $im : $big); return $slices[0]['texture'] ?? null; } private ?string $fogZone = null; private int $fogVersion = -1; private mixed $fog = null; /** * The fog over the parts of the zone not explored yet (Game's FOG_CELLS² bits): a dark parchment, opaque, with * soft edges (made small then smoothed up). Rebuilt only when a cell was revealed. */ public function fog(Game $g): mixed { if ($this->fogZone === $g->zone->id && $this->fogVersion === $g->exploredVersion) { return $this->fog; } $this->fogZone = $g->zone->id; $this->fogVersion = $g->exploredVersion; try { $n = Game::FOG_CELLS; $bits = $g->exploredBits(); $im = imagecreatetruecolor($n + 2, $n + 2); imagealphablending($im, false); imagesavealpha($im, true); $clear = imagecolorallocatealpha($im, 30, 26, 22, 127); for ($j = -1; $j <= $n; $j++) { for ($i = -1; $i <= $n; $i++) { $seen = false; if ($i >= 0 && $j >= 0 && $i < $n && $j < $n) { $k = $j * $n + $i; $seen = (ord($bits[$k >> 3]) & (1 << ($k & 7))) !== 0; } if ($seen) { imagesetpixel($im, $i + 1, $j + 1, $clear); continue; } // parchment with a little grain $h = (($i * 73856093) ^ ($j * 19349663)) & 3; imagesetpixel($im, $i + 1, $j + 1, imagecolorallocatealpha($im, 34 + $h, 29 + $h, 22 + (int) ($h / 2), 0)); } } $size = $n * 6; $big = imagecreatetruecolor($size, $size); imagealphablending($big, false); imagesavealpha($big, true); imagefill($big, 0, 0, imagecolorallocatealpha($big, 0, 0, 0, 127)); // the border pixels are fog too: the copy starts half a cell in, so the edges blend smoothly imagecopyresampled($big, $im, 0, 0, 1, 1, $size, $size, $n, $n); $slices = $this->sdl->createTextureFromGd('rv_map_fog', $big); $this->fog = $slices[0]['texture'] ?? null; } catch (\Throwable) { $this->fog = null; } return $this->fog; } public function cells(): int { return $this->n; }}