Exemple #1
0
use MapMaker\HeightLayer;
use MapMaker\WaterlineGDVisualiser;
use MapMaker\WaterlineLayer;
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
header('Content-Type: text/html; charset=utf-8');
require __DIR__ . '/vendor/autoload.php';
function __autoload($class)
{
    $ds = DIRECTORY_SEPARATOR;
    $classPath = str_replace('\\', $ds, $class);
    require __DIR__ . $ds . 'src' . $ds . $classPath . '.php';
}
$map = new Map(new Grid(100, 100));
$map->setVisualiser(new GDVisualiser(GDVisualiser::TYPE_PNG));
// See https://github.com/A1essandro/Diamond-And-Square
$generator = new MapGenerator\PerlinNoiseGenerator();
$generator->setSize(100);
$generator->setPersistence(mt_rand(500, 800) / 1000);
$heightMap = $generator->generate();
$heightLayer = new HeightLayer($map, $heightMap);
$heightLayer->generate();
$map->attachLayer($heightLayer);
$waterLayer = new WaterlineLayer($map, $heightLayer);
$waterLayer->setWaterRatio(mt_rand(300, 700) / 1000);
$waterLayer->generate();
$map->attachLayer($waterLayer);
$waterlineVisualiser = new WaterlineGDVisualiser();
$waterlineVisualiser->setLayer($waterLayer);
$map->addLayerVisualiser($waterlineVisualiser);
echo $map->render();
<?php

ini_set('display_errors', 'On');
require_once __DIR__ . '/../src/PerlinNoiseGenerator.php';
$gen = new MapGenerator\PerlinNoiseGenerator();
$size = 250;
$gen->setPersistence(0.78);
$gen->setSize($size);
$gen->setMapSeed('asd');
$map = $gen->generate();
$image = imagecreatetruecolor($size, $size);
$max = 0;
$min = PHP_INT_MAX;
for ($iy = 0; $iy < $size; $iy++) {
    for ($ix = 0; $ix < $size; $ix++) {
        $h = $map[$iy][$ix];
        if ($min > $h) {
            $min = $h;
        }
        if ($max < $h) {
            $max = $h;
        }
    }
}
$diff = $max - $min;
for ($iy = 0; $iy < $size; $iy++) {
    for ($ix = 0; $ix < $size; $ix++) {
        $h = 255 * ($map[$iy][$ix] - $min) / $diff;
        $color = imagecolorallocate($image, $h, $h, $h);
        imagesetpixel($image, $ix, $iy, $color);
    }
<?php

$start = microtime(true);
ini_set('display_errors', 'On');
require_once __DIR__ . '/../src/PerlinNoiseGenerator.php';
$gen = new MapGenerator\PerlinNoiseGenerator();
$memStart = memory_get_usage();
$gen->setPersistence(0.5);
$gen->setSize(1025);
$gen->setMapSeed('seed');
$gen->generate();
echo sprintf('Memory Peak Usage: %sMB', round(memory_get_peak_usage() / 1024 / 1024, 2)) . PHP_EOL;
echo sprintf('Memory Usage: %sMB', round((memory_get_usage() - $memStart) / 1024 / 1024, 2)) . PHP_EOL;
echo sprintf('Time: %s', round(microtime(true) - $start, 3)) . PHP_EOL;