[92, 132, 64], 'jungle' => [46, 102, 52], 'jungle_dense' => [38, 90, 44], 'mud' => [104, 88, 60], 'marsh' => [84, 96, 64], 'sand' => [196, 178, 128], 'beach' => [70, 66, 70], 'rock' => [120, 114, 106], 'snow' => [226, 232, 238], 'ice' => [176, 208, 226], 'cave' => [86, 78, 70], 'redrock' => [168, 84, 52], 'basalt' => [60, 58, 62], ]; /** The 3D view in the window now, as PNG bytes; null without OpenGL (or when GD can not write a PNG). */ public static function capture(Renderer $r): ?string { if ($r->isSoftware() || !function_exists('glReadPixels') || !function_exists('imagecreatetruecolor')) { return null; } $r->flush(); [$lx, $ly, $lw, $lh] = $r->letterbox(); [, $oh] = $r->outputSize(); if ($lw < self::W || $lh < self::H) { return null; } if (function_exists('glPixelStorei')) { glPixelStorei(GL_PACK_ALIGNMENT, 1); } $img = imagecreatetruecolor(self::W, self::H); $sum = 0; for ($j = 0; $j < self::H; $j++) { $sy = $ly + (int) (($j + 0.5) * $lh / self::H); $row = (string) glReadPixels($lx, $oh - 1 - $sy, $lw, 1, GL_RGBA, GL_UNSIGNED_BYTE); if (strlen($row) < $lw * 4) { return null; } for ($i = 0; $i < self::W; $i++) { // two pixels side by side, averaged (a little less aliasing than one) $o = (int) (($i + 0.25) * $lw / self::W) * 4; $p = min($lw - 1, (int) (($i + 0.75) * $lw / self::W)) * 4; $cr = (ord($row[$o]) + ord($row[$p])) >> 1; $cg = (ord($row[$o + 1]) + ord($row[$p + 1])) >> 1; $cb = (ord($row[$o + 2]) + ord($row[$p + 2])) >> 1; $sum += $cr + $cg + $cb; imagesetpixel($img, $i, $j, ($cr << 16) | ($cg << 8) | $cb); } } if ($sum < self::W * self::H * 3) { return null; // all black: nothing was drawn there (no 3D this frame) } return self::png($img); } /** The land around Rob from above (north up), 2 m a pixel, tinted by the hour: PNG bytes, null without GD. */ public static function fromMap(Game $g): ?string { if (!function_exists('imagecreatetruecolor')) { return null; } $z = $g->zone; $img = imagecreatetruecolor(self::W, self::H); $h = $g->hour(); $day = $z->cave ? 0.8 : max(0.5, min(1.0, 0.5 + 0.5 * sin(max(0.0, min(1.0, ($h - 5.5) / 15.0)) * M_PI))); $night = $z->cave ? [1.0, 0.92, 0.78] : ($day < 0.6 ? [0.7, 0.8, 1.15] : [1.0, 1.0, 1.0]); $k = 2.0; $step = 2; // the picture's centre: Rob, kept far enough from the zone's edges for the picture to be all land $ox = max(self::W / 2 * $k, min($z->size - self::W / 2 * $k, $g->x)); $oz = max(self::H / 2 * $k, min($z->size - self::H / 2 * $k, $g->z)); for ($j = 0; $j < self::H; $j += $step) { for ($i = 0; $i < self::W; $i += $step) { $x = $ox + ($i - self::W / 2) * $k; $zz = $oz + ($j - self::H / 2) * $k; if ($x >= $z->size || $zz >= $z->size) { $c = [12, 14, 18]; } elseif ($z->waterDepth($x, $zz) > 0.05) { $d = min(1.0, $z->waterDepth($x, $zz) / 3); $c = [(int) (60 - 30 * $d), (int) (110 - 30 * $d), (int) (150 - 20 * $d)]; } else { $c = self::BIOME[$z->biomeAt($x, $zz)] ?? [100, 110, 80]; // light from the north-west: the slopes stand out $shade = 1.0 + max(-0.45, min(0.45, ($z->heightAt($x - 1.5, $zz - 1.5) - $z->heightAt($x + 1.5, $zz + 1.5)) * 0.12)); if ($z->solidAt($x, $zz)) { $shade *= $z->cave ? 0.35 : 0.6; // walls, cliffs } elseif ($z->cave) { $shade *= 1.5; // the cave's floor, lit } $c = [(int) ($c[0] * $shade), (int) ($c[1] * $shade), (int) ($c[2] * $shade)]; } $col = self::tint($c, $day, $night); imagefilledrectangle($img, $i, $j, $i + $step - 1, $j + $step - 1, $col); } } foreach ($z->thingsNear($ox, $oz, 110) as $t) { $px = (int) (self::W / 2 + ($t->x - $ox) / $k); $py = (int) (self::H / 2 + ($t->z - $oz) / $k); if ($px < 1 || $py < 1 || $px >= self::W - 1 || $py >= self::H - 1) { continue; } $c = match (true) { str_contains($t->kind, 'tree') || $t->kind === 'mangrove' || str_contains($t->kind, 'palm') => [28, 70, 34], Solids::solid($t->kind) => [150, 146, 140], default => null, }; if ($c !== null) { imagefilledrectangle($img, $px, $py, $px + 1, $py + 1, self::tint($c, $day, $night)); } } // Rob: a white arrow toward where he looks $cx = self::W / 2 + ($g->x - $ox) / $k; $cy = self::H / 2 + ($g->z - $oz) / $k; $fx = sin($g->yaw); $fz = cos($g->yaw); $white = 0xF4F6F8; imagefilledpolygon($img, [ (int) round($cx + $fx * 6), (int) round($cy + $fz * 6), (int) round($cx - $fx * 3 + $fz * 3.5), (int) round($cy - $fz * 3 - $fx * 3.5), (int) round($cx - $fx * 3 - $fz * 3.5), (int) round($cy - $fz * 3 + $fx * 3.5), ], $white); return self::png($img); } /** @param array{0: int, 1: int, 2: int} $c @param array{0: float, 1: float, 2: float} $tint */ private static function tint(array $c, float $day, array $tint): int { $r = max(0, min(255, (int) ($c[0] * $day * $tint[0]))); $g = max(0, min(255, (int) ($c[1] * $day * $tint[1]))); $b = max(0, min(255, (int) ($c[2] * $day * $tint[2]))); return ($r << 16) | ($g << 8) | $b; } private static function png(\GdImage $img): ?string { ob_start(); $ok = imagepng($img, null, 9); $png = (string) ob_get_clean(); return $ok && $png !== '' ? $png : null; } }