New "Karte" tab: a tiled zoom/pan map of the game world with a category sidebar (groups, per-category marker counts, show all / hide all, search) and marker popups, plus tactic layers drawn on top of it. Markers are placed by clicking the map (map.manage) and live in freely editable two-level categories, each with its own emoji and colour. A tactic (map.tactics) is a separate drawing layer that can be toggled on and off: rally points, dashed routes, areas and text notes in any colour, finished by double click, an explicit button or Enter, removed by clicking them. Coordinates are stored as a fraction of the map's width/height rather than in pixels, so re-tiling the map at a different resolution never moves anything. The map data lives entirely in the website database - the Discord bot is not involved. tools/make_map_tiles.php slices a high-resolution image into the zoom pyramid locally; the resulting folder is uploaded by FTP and picked up automatically, one region per folder. Leaflet 1.9.4 is vendored under assets/vendor/ so there is still no CDN and no build step. New permissions map.view / map.manage / map.tactics and four new tables - existing installs have to add both once (see README). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
193 lines
6.4 KiB
PHP
193 lines
6.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Zerlegt ein grosses Kartenbild in eine Kachel-Pyramide fuer die Taktikkarte (map.php).
|
|
*
|
|
* Aufruf (lokal, nicht auf dem Webspace noetig):
|
|
*
|
|
* php tools/make_map_tiles.php <kartenschluessel> <bilddatei> [--name="Anzeigename"]
|
|
* [--tile-size=256] [--quality=82] [--out=<zielordner>]
|
|
*
|
|
* Beispiel:
|
|
*
|
|
* php -d memory_limit=2G tools/make_map_tiles.php verteron "C:\Bilder\verteron.png" --name="Verteron"
|
|
*
|
|
* Ergebnis landet in assets/uploads/maps/<kartenschluessel>/ und besteht aus einem Ordner
|
|
* je Zoomstufe plus einer tiles.json, ueber die sich die Karte selbst beschreibt. Diesen
|
|
* Ordner danach einfach per FTP hochladen - map.php findet ihn von allein.
|
|
*
|
|
* Die oberste Zoomstufe zeigt das Bild in Originalaufloesung, jede Stufe darunter ist halb
|
|
* so gross, bis die ganze Karte in eine einzige Kachel passt. Randkacheln werden auf die
|
|
* volle Kachelgroesse aufgefuellt (mit der Hintergrundfarbe der Website), damit Leaflet sie
|
|
* nicht verzerrt.
|
|
*/
|
|
|
|
if (PHP_SAPI !== 'cli') {
|
|
fwrite(STDERR, "Dieses Script laeuft nur auf der Kommandozeile.\n");
|
|
exit(1);
|
|
}
|
|
|
|
if (!extension_loaded('gd')) {
|
|
fwrite(STDERR, "Die PHP-Erweiterung GD wird benoetigt (php.ini: extension=gd).\n");
|
|
exit(1);
|
|
}
|
|
|
|
$args = array_slice($argv, 1);
|
|
$positional = [];
|
|
$options = ['name' => null, 'tile-size' => 256, 'quality' => 82, 'out' => null];
|
|
|
|
foreach ($args as $arg) {
|
|
if (str_starts_with($arg, '--')) {
|
|
[$key, $value] = array_pad(explode('=', substr($arg, 2), 2), 2, '');
|
|
if (!array_key_exists($key, $options)) {
|
|
fwrite(STDERR, "Unbekannte Option: --$key\n");
|
|
exit(1);
|
|
}
|
|
$options[$key] = $value;
|
|
} else {
|
|
$positional[] = $arg;
|
|
}
|
|
}
|
|
|
|
if (count($positional) < 2) {
|
|
fwrite(STDERR, "Aufruf: php tools/make_map_tiles.php <kartenschluessel> <bilddatei> [--name=\"Anzeigename\"]\n");
|
|
exit(1);
|
|
}
|
|
|
|
[$mapKey, $sourcePath] = $positional;
|
|
|
|
if (preg_match('/^[a-z0-9_-]+$/', $mapKey) !== 1) {
|
|
fwrite(STDERR, "Der Kartenschluessel darf nur Kleinbuchstaben, Ziffern, - und _ enthalten (z.B. 'verteron').\n");
|
|
exit(1);
|
|
}
|
|
if (!is_file($sourcePath)) {
|
|
fwrite(STDERR, "Bilddatei nicht gefunden: $sourcePath\n");
|
|
exit(1);
|
|
}
|
|
|
|
$tileSize = max(64, (int) $options['tile-size']);
|
|
$quality = min(100, max(40, (int) $options['quality']));
|
|
$displayName = $options['name'] !== null && $options['name'] !== '' ? (string) $options['name'] : ucfirst($mapKey);
|
|
$outputRoot = $options['out'] !== null && $options['out'] !== ''
|
|
? rtrim((string) $options['out'], '/\\')
|
|
: __DIR__ . '/../assets/uploads/maps/' . $mapKey;
|
|
|
|
echo "Lade Bild …\n";
|
|
$source = load_image($sourcePath);
|
|
$width = imagesx($source);
|
|
$height = imagesy($source);
|
|
echo "Quellbild: {$width} x {$height} Pixel\n";
|
|
|
|
// Hoechste Zoomstufe = Originalgroesse. Kleinste Stufe = das ganze Bild passt in eine Kachel.
|
|
$maxZoom = (int) max(0, ceil(log(max($width, $height) / $tileSize, 2)));
|
|
$minZoom = 0;
|
|
|
|
echo "Zoomstufen: {$minZoom} bis {$maxZoom} (Kachelgroesse {$tileSize}px)\n";
|
|
|
|
if (!is_dir($outputRoot) && !mkdir($outputRoot, 0775, true) && !is_dir($outputRoot)) {
|
|
fwrite(STDERR, "Zielordner konnte nicht angelegt werden: $outputRoot\n");
|
|
exit(1);
|
|
}
|
|
|
|
$totalTiles = 0;
|
|
|
|
for ($zoom = $maxZoom; $zoom >= $minZoom; $zoom--) {
|
|
$scale = 2 ** ($zoom - $maxZoom);
|
|
$levelWidth = max(1, (int) round($width * $scale));
|
|
$levelHeight = max(1, (int) round($height * $scale));
|
|
|
|
echo "Zoom {$zoom}: {$levelWidth} x {$levelHeight} … ";
|
|
|
|
$level = $zoom === $maxZoom ? $source : resize_image($source, $levelWidth, $levelHeight);
|
|
|
|
$cols = (int) ceil($levelWidth / $tileSize);
|
|
$rows = (int) ceil($levelHeight / $tileSize);
|
|
|
|
$zoomDir = $outputRoot . '/' . $zoom;
|
|
if (!is_dir($zoomDir) && !mkdir($zoomDir, 0775, true) && !is_dir($zoomDir)) {
|
|
fwrite(STDERR, "\nOrdner konnte nicht angelegt werden: $zoomDir\n");
|
|
exit(1);
|
|
}
|
|
|
|
// Kein imagedestroy() noetig: GD-Bilder raeumt PHP seit 8.0 selbst auf, und seit 8.5
|
|
// ist die Funktion als deprecated markiert.
|
|
for ($x = 0; $x < $cols; $x++) {
|
|
for ($y = 0; $y < $rows; $y++) {
|
|
$tile = imagecreatetruecolor($tileSize, $tileSize);
|
|
// Randkacheln auf volle Groesse auffuellen - in der Hintergrundfarbe der
|
|
// Website, damit der Uebergang am Kartenrand nicht auffaellt.
|
|
imagefill($tile, 0, 0, imagecolorallocate($tile, 0x14, 0x16, 0x1a));
|
|
|
|
$srcX = $x * $tileSize;
|
|
$srcY = $y * $tileSize;
|
|
$copyWidth = min($tileSize, $levelWidth - $srcX);
|
|
$copyHeight = min($tileSize, $levelHeight - $srcY);
|
|
|
|
imagecopy($tile, $level, 0, 0, $srcX, $srcY, $copyWidth, $copyHeight);
|
|
imagejpeg($tile, $zoomDir . '/' . $x . '_' . $y . '.jpg', $quality);
|
|
$totalTiles++;
|
|
}
|
|
}
|
|
|
|
echo $cols * $rows . " Kacheln\n";
|
|
}
|
|
|
|
$manifest = [
|
|
'name' => $displayName,
|
|
'width' => $width,
|
|
'height' => $height,
|
|
'tile_size' => $tileSize,
|
|
'min_zoom' => $minZoom,
|
|
'max_zoom' => $maxZoom,
|
|
'extension' => 'jpg',
|
|
'generated_at' => date('c'),
|
|
];
|
|
|
|
file_put_contents(
|
|
$outputRoot . '/tiles.json',
|
|
json_encode($manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
|
|
);
|
|
|
|
echo "\nFertig: {$totalTiles} Kacheln in {$outputRoot}\n";
|
|
echo "Diesen Ordner nach assets/uploads/maps/{$mapKey}/ auf den Webspace hochladen.\n";
|
|
|
|
/**
|
|
* Laedt JPEG, PNG oder WebP als GD-Bild.
|
|
*
|
|
* @return GdImage
|
|
*/
|
|
function load_image(string $path)
|
|
{
|
|
$info = @getimagesize($path);
|
|
if ($info === false) {
|
|
fwrite(STDERR, "Datei ist kein unterstuetztes Bild: $path\n");
|
|
exit(1);
|
|
}
|
|
|
|
$image = match ($info[2]) {
|
|
IMAGETYPE_JPEG => @imagecreatefromjpeg($path),
|
|
IMAGETYPE_PNG => @imagecreatefrompng($path),
|
|
IMAGETYPE_WEBP => @imagecreatefromwebp($path),
|
|
default => false,
|
|
};
|
|
|
|
if ($image === false) {
|
|
fwrite(STDERR, "Bild konnte nicht geladen werden (JPEG, PNG und WebP werden unterstuetzt).\n");
|
|
fwrite(STDERR, "Bei sehr grossen Bildern hilft ein hoeheres Speicherlimit: php -d memory_limit=2G ...\n");
|
|
exit(1);
|
|
}
|
|
|
|
return $image;
|
|
}
|
|
|
|
/**
|
|
* @param GdImage $source
|
|
* @return GdImage
|
|
*/
|
|
function resize_image($source, int $width, int $height)
|
|
{
|
|
$resized = imagecreatetruecolor($width, $height);
|
|
imagecopyresampled($resized, $source, 0, 0, 0, 0, $width, $height, imagesx($source), imagesy($source));
|
|
return $resized;
|
|
}
|