Better parameters ^^

This commit is contained in:
Shoghi Cervantes 2014-02-12 16:06:43 +01:00
parent 2d88739005
commit f91e2a5cfe
3 changed files with 38 additions and 22 deletions

View File

@ -30,15 +30,12 @@ class NormalGenerator implements LevelGenerator{
private $random; private $random;
private $worldHeight = 64; private $worldHeight = 64;
private $waterHeight = 60; private $waterHeight = 60;
private $blockLevels = array();
private $noiseGen1; private $noiseGen1;
private $noiseGen2; private $noiseGen2;
private $noiseGen3; private $noiseGen3;
private $noiseGen4; private $noiseGen4;
private $noiseGen5; private $noiseGen5;
private $noiseGen6; private $noiseGen6;
private $noiseArray = array();
private $noise;
public function __construct(array $options = array()){ public function __construct(array $options = array()){
@ -48,13 +45,8 @@ class NormalGenerator implements LevelGenerator{
$this->level = $level; $this->level = $level;
$this->random = $random; $this->random = $random;
$this->random->setSeed($this->level->getSeed()); $this->random->setSeed($this->level->getSeed());
$this->noise = new NoiseGeneratorPerlin($this->random); $this->noiseGen1 = new NoiseGeneratorPerlin($this->random, 16);
$this->noiseGen1 = new PerlinOctaveGenerator($this->random, 16); $this->noiseGen2 = new NoiseGeneratorPerlin($this->random, 4);
$this->noiseGen2 = new PerlinOctaveGenerator($this->random, 16);
$this->noiseGen3 = new PerlinOctaveGenerator($this->random, 8);
$this->noiseGen4 = new PerlinOctaveGenerator($this->random, 4);
$this->noiseGen5 = new PerlinOctaveGenerator($this->random, 10);
$this->noiseGen6 = new PerlinOctaveGenerator($this->random, 16);
$ores = new OrePopulator(); $ores = new OrePopulator();
$ores->setOreTypes(array( $ores->setOreTypes(array(
@ -73,17 +65,14 @@ class NormalGenerator implements LevelGenerator{
public function generateChunk($chunkX, $chunkZ){ public function generateChunk($chunkX, $chunkZ){
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
//$this->noiseArray = $this->initializeNoiseArray($chunkX * $byte0, 0, $chunkZ * $byte0, $byte0 + 1, ($this->worldHeight / 8) + 1, $byte0 + 1); $baseHeight = $this->worldHeight;// + $this->noiseGen1->noise2D($chunkX, $chunkZ, 2, 1/4, true) * 35;
$chunks = array();
for($chunkY = 0; $chunkY < 8; ++$chunkY){ for($chunkY = 0; $chunkY < 8; ++$chunkY){
$chunk = ""; $chunk = "";
$startY = $chunkY << 4; $startY = $chunkY << 4;
$endY = $startY + 16; $endY = $startY + 16;
for($z = 0; $z < 16; ++$z){ for($z = 0; $z < 16; ++$z){
for($x = 0; $x < 16; ++$x){ for($x = 0; $x < 16; ++$x){
$height = (int) ($this->worldHeight + $this->noise->noise3D($x + ($chunkX << 4), 0, $z + ($chunkZ << 4), 4, 0.5, 24, true)); $height = (int) ($baseHeight + $this->noiseGen2->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.3, 32, true) * 15);
for($y = $startY; $y < $endY; ++$y){ for($y = $startY; $y < $endY; ++$y){
$diff = $height - $y; $diff = $height - $y;
if($y <= 4 and ($y === 0 or $this->random->nextFloat() < 0.75)){ if($y <= 4 and ($y === 0 or $this->random->nextFloat() < 0.75)){

View File

@ -25,6 +25,7 @@ abstract class NoiseGenerator{
protected $offsetX = 0; protected $offsetX = 0;
protected $offsetY = 0; protected $offsetY = 0;
protected $offsetZ = 0; protected $offsetZ = 0;
protected $octaves = 8;
public static function floor($x){ public static function floor($x){
return $x >= 0 ? (int) $x : (int) ($x - 1); return $x >= 0 ? (int) $x : (int) ($x - 1);
@ -44,17 +45,38 @@ abstract class NoiseGenerator{
$v = $hash < 4 ? $y : (($hash === 12 or $hash === 14) ? $x : $z); $v = $hash < 4 ? $y : (($hash === 12 or $hash === 14) ? $x : $z);
return (($hash & 1) === 0 ? $u : -$u) + (($hash & 2) === 0 ? $v : -$v); return (($hash & 1) === 0 ? $u : -$u) + (($hash & 2) === 0 ? $v : -$v);
} }
abstract public function getNoise2D($x, $z);
abstract public function noise($x, $y, $z); abstract public function getNoise3D($x, $y, $z);
public function noise3D($x, $y, $z, $octaves, $frequency, $amplitude, $normalized = false){ public function noise2D($x, $z, $frequency, $amplitude, $normalized = false){
$result = 0; $result = 0;
$amp = 1; $amp = 1;
$freq = 1; $freq = 1;
$max = 0; $max = 0;
for($i = 0; $i < $octaves; ++$i){ for($i = 0; $i < $this->octaves; ++$i){
$result += $this->noise($x * $freq, $y * $freq, $z * $freq) * $amp; $result += $this->getNoise2D($x * $freq, $z * $freq) * $amp;
$max += $amp;
$freq *= $frequency;
$amp *= $amplitude;
}
if($normalized === true){
$result /= $max;
}
return $result;
}
public function noise3D($x, $y, $z, $frequency, $amplitude, $normalized = false){
$result = 0;
$amp = 1;
$freq = 1;
$max = 0;
for($i = 0; $i < $this->octaves; ++$i){
$result += $this->getNoise3D($x * $freq, $y * $freq, $z * $freq) * $amp;
$max += $amp; $max += $amp;
$freq *= $frequency; $freq *= $frequency;
$amp *= $amplitude; $amp *= $amplitude;

View File

@ -31,7 +31,8 @@ class NoiseGeneratorPerlin extends NoiseGenerator{
]; ];
public function __construct(Random $random){ public function __construct(Random $random, $octaves){
$this->octaves = $octaves;
$this->offsetX = $random->nextFloat() * 256; $this->offsetX = $random->nextFloat() * 256;
$this->offsetY = $random->nextFloat() * 256; $this->offsetY = $random->nextFloat() * 256;
$this->offsetZ = $random->nextFloat() * 256; $this->offsetZ = $random->nextFloat() * 256;
@ -55,7 +56,7 @@ class NoiseGeneratorPerlin extends NoiseGenerator{
} }
public function noise($x, $y, $z){ public function getNoise3D($x, $y, $z){
$x += $this->offsetX; $x += $this->offsetX;
$y += $this->offsetY; $y += $this->offsetY;
$z += $this->offsetZ; $z += $this->offsetZ;
@ -94,4 +95,8 @@ class NoiseGeneratorPerlin extends NoiseGenerator{
self::lerp($fX, self::grad($this->perm[$AB + 1], $x, $y - 1, $z - 1), self::lerp($fX, self::grad($this->perm[$AB + 1], $x, $y - 1, $z - 1),
self::grad($this->perm[$BB + 1], $x - 1, $y - 1, $z - 1)))); self::grad($this->perm[$BB + 1], $x - 1, $y - 1, $z - 1))));
} }
public function getNoise2D($x, $z){
return $this->getNoise3D($x, 0, $z);
}
} }