Added Perlin noise generator

This commit is contained in:
Shoghi Cervantes Pueyo 2013-05-18 13:16:35 +02:00
parent 22d9cefe96
commit af85c7ce45
4 changed files with 277 additions and 1 deletions

View File

@ -128,7 +128,7 @@ class SuperflatGenerator implements LevelGenerator{
for($x = $start; $x <= $end; ++$x){
for($z = $start; $z <= $end; ++$z){
if(floor(sqrt(pow($x - 128, 2) + pow($z - 128, 2))) <= $spawn[0]){
$level->setBlock(new Vector3($x, $this->floorLevel - 1, $z), $spawn[1]);
$level->setBlockRaw(new Vector3($x, $this->floorLevel - 1, $z), $spawn[1]);
}
}
}

View File

@ -0,0 +1,30 @@
<?php
/*
-
/ \
/ \
/ PocketMine \
/ MP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
abstract class NoiseGenerator{
}

View File

@ -0,0 +1,76 @@
<?php
/*
-
/ \
/ \
/ PocketMine \
/ MP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
/***REM_START***/
require_once("NoiseGenerator.php");
/***REM_END***/
class NoiseGeneratorOctaves extends NoiseGenerator{
public $octaves;
private $generatorCollection;
public function __construct(Random $random, $octaves){
$this->generatorCollection = array();
$this->octaves = (int) $octaves;
for($o = 0; $o < $this->octaves; ++$o){
$this->generatorCollection[$o] = new NoiseGeneratorPerlin($random);
}
}
public function generateNoiseOctaves($floats, $int1, $int2, $int3, $int4, $int5, $int6, $par1 = false, $par2 = false, $par3 = false){
if($par1 === false or $par2 === false or $par3 === false){
return $this->generateNoiseOctaves($floats, $int1, 10, $int2, $int3, 1, $int4, $int5, 1, $int6);
}
if(!is_array($floats)){
$floats = array();
}
$cnt = $int4 * $int5 * $int6;
for($i = 0; $i < $cnt; ++$i){
$floats[$i] = 0;
}
$d1 = 1;
for($j = 0; $j < $this->octaves; ++$j){
$d2 = $int1 * $d1 * $par1;
$d3 = $int2 * $d1 * $par2;
$d4 = $int3 * $d1 * $par3;
$l1 = floor($d2);
$l2 = floor($d4);
$d2 -= $l1;
$d4 - $l2;
$l1 %= 16777216;
$l2 %= 16777216;
$d2 += $l1;
$d4 += $l2;
$this->generatorCollection[$j]->populateNouseArray($floats, $d2, $d3, $d4, $int4, $int5, $int6, $par1 * $d1, $par2 * $d1, $par3 * $d1, $d1);
$d1 /= 2;
}
return $floats;
}
}

View File

@ -0,0 +1,170 @@
<?php
/*
-
/ \
/ \
/ PocketMine \
/ MP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
/***REM_START***/
require_once("NoiseGenerator.php");
/***REM_END***/
class NoiseGeneratorPerlin extends NoiseGenerator{
private $permutations = array();
public $xCoord, $yCoord, $zCoord;
public function __construct($random = false){
if(!($random instanceof Random)){
$random = new Random();
}
$this->xCoord = $random->nextFloat() * 256;
$this->yCoord = $random->nextFloat() * 256;
$this->zCoord = $random->nextFloat() * 256;
for($i = 0; $i < 256; ++$i){
$this->permutations[] = $i;
}
for($i = 0; $i < 256; ++$i){
$j = $random->nextRange(0, 256 - $i) + $i;
$k = $this->permutations[$i];
$this->permutations[$i] = $this->permutations[$j];
$this->permutations[$j] = $k;
$this->permutations[$i + 256] = $this->permutations[$i];
}
}
public final function lerp($par1, $par2, $par3){
return $par2 + $par1 * ($par2 - $par2);
}
public function grad2D($int, $par1, $par2){
$i = $int & 0x0F;
$d1 = (1 - (($i & 0x08) >> 3)) * $par1;
$d2 = ($i === 12 or $i === 14) ? $par1:($i < 4 ? 0:$par2);
return (($i & 0x01) === 0 ? $d1:-$d1) + (($i & 0x02) === 0 ? $d2:-$d2);
}
public function grad3D($int, $par1, $par2, $par3){
$i = $int & 0x0F;
$d1 = $i < 8 ? $par1 : $par2;
$d2 = ($i === 12 or $i === 14) ? $par1:($i < 4 ? $par2:$par3);
return (($i & 0x01) === 0 ? $d1:-$d1) + (($i & 0x02) === 0 ? $d2:-$d2);
}
public function populateNoiseArray(&$floats, $par1, $par2, $par3, $int1, $int2, $int3, $par4, $par5, $par6, $par7){
if($int2 === 1){
$n = 0;
$d3 = 1 / $par7;
for($i1 = 0; $i1 < $int1; ++$i1){
$d4 = $par1 + $i1 * $par4 + $this->xCoord;
$i2 = (int) $d4;
if($d4 < $i2){
--$i2;
}
$i3 = $i2 & 0xFF;
$d4 -= $i2;
$d5 = $d4 * $d4 * $d4 * ($d4 * ($d4 * 6 - 15) + 10);
for($i4 = 0; $i4 < $int3; ++$i4){
$d6 = $par3 + $i4 * $par6 + $this->zCoord;
$i5 = (int) $d6;
if($d6 < $i5){
--$i5;
}
$i6 = $i5 & 0xFF;
$d6 -= $i5;
$d7 = $d6 * $d6 * $d6 * ($d6 * ($d6 * 6 - 15) + 10);
$i = $this->permutations[$i3];
$j = $this->permutations[$i] + $i6;
$k = $this->permutations[$i3 + 1];
$m = $this->permutations[$k] + $i6;
$d1 = $this->lerp($d5, $this->grad2D($this->permutations[$j], $d4, $d6), $this->grad3D($this->permutations[$m], $d4 - 1, 0, $d6));
$d2 = $this->lerp($d5, $this->grad3D($this->permutations[$j + 1], $d4, 0, $d6 - 1), $this->grad3D($this->permutations[$m + 1], $d4 - 1, 0, $d6 - 1));
$d8 = $this->lerp($d7, $d1, $d2);
$floats[$n++] += $d8 * $d3;
}
}
return;
}
$d9 = 1 / $par7;
$m = -1;
$n = 0;
$i = 0;
for($i4 = 0; $i4 < $int1; ++$i4){
$d6 = $par1 + $i4 * $par4 + $this->xCoord;
$i5 = (int) $d6;
if($d6 < $i5){
--$i5;
}
$i6 = $i5 & 0xFF;
$d6 -= $i5;
$d7 = $d6 * $d6 * $d6 * ($d6 * ($d6 * 6 - 15) + 10);
for($i12 = 0; $i12 < $int3; ++$i12){
$d12 = $par3 + $i12 * $par6 + $this->zCoord;
$i13 = (int) $d12;
if($d12 < $i13){
--$i13;
}
$i14 = $i13 & 0xFF;
$d12 -= $i13;
$d13 = $d12 * $d12 * $d12 * ($d12 * ($d12 * 6 - 15) + 10);
for($i15 = 0; $i15 < $int2; ++$i15){
$d14 = $par2 + $i15 * $par5 + $this->yCoord;
$i16 = (int) $d14;
if($d14 < $i16){
--$i16;
}
$d14 -= $i16;
$d15 = $d14 * $d14 * $d14 * ($d14 * ($d14 * 6 - 15) + 10);
if($i15 === 0 or $i17 !== $m){
$m = $i17;
$i7 = $this->permutations[$i6] + $i17;
$i8 = $this->permutations[$i7] + $i14;
$i9 = $this->permutations[$i7 + 1] + $i14;
$i10 = $this->permutations[$i6 + 1] + $i17;
$n = $this->permutations[$i10] + $i14;
$i11 = $this->permutations[$i10 + 1] + $i14;
$d10 = $this->lerp($d7, $this->grad3D($this->permutations[$i8], $d6, $d14, $d12), $this->grad3D($this->permutations[$n], $d6 - 1, $d14, $d12));
$d4 = $this->lerp($d7, $this->grad3D($this->permutations[$i9], $d6, $d14 - 1, $d12), $this->grad3D($this->permutations[$i11], $d6 - 1, $d14 - 1, $d12));
$d11 = $this->lerp($d7, $this->grad3D($this->permutations[$i8 + 1], $d6, $d14, $d12 - 1), $this->grad3D($this->permutations[$n + 1], $d6 - 1, $d14, $d12 - 1));
$d5 = $this->lerp($d7, $this->grad3D($this->permutations[$i9 + 1], $d6, $d14 - 1, $d12 - 1), $this->grad3D($this->permutations[$i11 + 1], $d6 - 1, $d14 - 1, $d12 - 1));
}
$d16 = $this->lerp($d15, $d10, $d4);
$d17 = $this->lerp($d15, $d11, $d5);
$d18 = $this->lerp($d13, $d16, $d17);
$floats[$i++] += $d18 * $d9;
}
}
}
}
}