From 4f8501ff34fc0cb4f8f64c09a7a149421598c414 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 4 Nov 2021 14:14:55 +0000 Subject: [PATCH] Added a tool to visualise behaviour of ChunkSelector I actually intended to write a tool for debugging generation, but it turns out this, as an intermediary step, is also useful and a whole bunch of fun to play with. --- tools/simulate-chunk-selector.php | 171 ++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 tools/simulate-chunk-selector.php diff --git a/tools/simulate-chunk-selector.php b/tools/simulate-chunk-selector.php new file mode 100644 index 000000000..9ae7a1161 --- /dev/null +++ b/tools/simulate-chunk-selector.php @@ -0,0 +1,171 @@ +selectChunks($radius, $baseX, $baseZ); + + $middleOffsetX = $scale * ($radius + $offsetX); + $middleOffsetZ = $scale * ($radius + $offsetZ); + + $frame = 0; + $seen = []; + while($iterator->valid()){ + $frame++; + + $black = imagecolorallocate($image, 0, 0, 0); + $yellow = imagecolorallocate($image, 255, 255, 51); + $red = imagecolorallocate($image, 255, 0, 0); + if($black === false || $yellow === false || $red === false) throw new AssumptionFailedError(); + + for($i = 0; $i < $chunksPerStep; ++$i){ + $chunkHash = $iterator->current(); + if(!isset($seen[$chunkHash])){ + $color = $chunkColor; + $seen[$chunkHash] = true; + }else{ + $color = $yellow; + } + World::getXZ($chunkHash, $chunkX, $chunkZ); + $imageX = $middleOffsetX + (($chunkX - $baseX) * $scale); + $imageZ = $middleOffsetZ + (($chunkZ - $baseZ) * $scale); + + imagefilledrectangle($image, $imageX, $imageZ, $imageX + $scale, $imageZ + $scale, $color); + imagerectangle($image, $imageX, $imageZ, $imageX + $scale, $imageZ + $scale, $black); + + $iterator->next(); + if(!$iterator->valid()){ + break; + } + } + imagearc($image, $middleOffsetX, $middleOffsetZ, $radius * $scale * 2, $radius * $scale * 2, 0, 360, $red); + + imagepng($image, Path::join($outputFolder, "frame" . str_pad(strval($frame), 5, "0", STR_PAD_LEFT) . ".png")); + echo "\rRendered step $frame"; + } + echo "\n"; +} + +$radius = null; +$baseX = 10000 >> Chunk::COORD_BIT_SIZE; +$baseZ = 10000 >> Chunk::COORD_BIT_SIZE; + +$nChunksPerStep = 32; +$scale = 10; + +if(count(getopt("", ["help"])) !== 0){ + echo "Required parameters:\n"; + echo "--output=path/to/dir: Output folder to put the generated images into (will attempt to create if it doesn't exist)\n"; + echo "--radius=N: Radius of chunks to render (default $radius)\n"; + echo "\n"; + echo "Optional parameters:\n"; + echo "--baseX=N: Base X coordinate to use for simulation (default $baseX\n"; + echo "--baseZ=N: Base Z coordinate to use for simulation (default $baseZ)\n"; + echo "--scale=N: Height/width of square of pixels to use for each chunk (default $scale)\n"; + echo "--chunksPerStep=N: Number of chunks to process in each frame (default $nChunksPerStep)\n"; + exit(0); +} + +foreach(getopt("", ["radius:", "baseX:", "baseZ:", "scale:", "chunksPerStep:"]) as $name => $value){ + if(!is_string($value) || (string) ((int) $value) !== $value){ + fwrite(STDERR, "Value for --$name must be an integer\n"); + exit(1); + } + $value = (int) $value; + match($name){ + "radius" => ($radius = $value), + "baseX" => ($baseX = $value), + "baseZ" => ($baseZ = $value), + "scale" => ($scale = $value), + "chunksPerStep" => ($nChunksPerStep = $value), + default => throw new AssumptionFailedError("getopt() returned unknown option") + }; +} +if($radius === null){ + fwrite(STDERR, "Please specify a radius using --radius\n"); + exit(1); +} + +$outputDirectory = null; +foreach(getopt("", ["output:"]) as $name => $value){ + assert($name === "output"); + if(!is_string($value)){ + fwrite(STDERR, "Value for --$name must be a string\n"); + exit(1); + } + if(!@mkdir($value) && !is_dir($value)){ + fwrite(STDERR, "Output directory $value could not be created\n"); + exit(1); + } + $files = scandir($value, SCANDIR_SORT_NONE); + if($files !== false && count($files) > 2){ //always returns . and .. + fwrite(STDERR, "Output directory $value is not empty\n"); + exit(1); + } + $realPath = realpath($value); + if($realPath === false){ + throw new AssumptionFailedError(); + } + $outputDirectory = $realPath; +} +if($outputDirectory === null){ + fwrite(STDERR, "Please specify an output directory using --output\n"); + exit(1); +} +$image = newImage($scale, $radius); + +$black = imagecolorallocate($image, 0, 0, 0); +$green = imagecolorallocate($image, 0, 220, 0); +if($black === false || $green === false){ + throw new AssumptionFailedError(); +} +render($radius, $baseX, $baseZ, $nChunksPerStep, $scale, $image, $green, 0, 0, $outputDirectory);