fixed CaveGenerator, currently disabled in NormalGenerator

This commit is contained in:
GameHerobrine 2023-11-30 07:08:42 +03:00
parent 894530e4fb
commit 807662dcb0
7 changed files with 188 additions and 127 deletions

View file

@ -600,17 +600,15 @@ class PMFLevel extends PMF{
$Z = $z >> 4;
$Y = $y >> 4;
$block &= 0xFF;
$index = $this->getIndex($X, $Z);
$index = $this->getIndex($X, $Z);
if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){
if($this->loadChunk($X, $Z, false) === false){
$this->createUnpopulatedChunk($X, $Z);
}
}
//if($this->chunks[$index][$Y] === false){
// $this->fillMiniChunk($X, $Z, $Y);
//}
if($this->chunks[$index][$Y] === false){
$this->fillMiniChunk($X, $Z, $Y);
}
$aX = $x & 0xf;
$aZ = $z & 0xf;

56
src/utils/MTRandom.php Normal file
View file

@ -0,0 +1,56 @@
<?php
/**
* Fatser implementation of Mersenne Twister
*/
class MTRandom
{
const MAG = [0, 0x9908b0df];
public $mt;
public $index = 0;
public function __construct($seed = null){
$this->setSeed($seed == null ? (int)microtime(1) : $seed);
}
public function setSeed($seed){
$this->mt[0] = $seed & 0xffffffff;
for($this->index = 1; $this->index < 624; ++$this->index){
$this->mt[$this->index] = (0x6c078965 * ($this->mt[$this->index-1] >> 30 ^ $this->mt[$this->index - 1]) + $this->index) & 0xffffffff;
}
}
public function genRandInt(){
if($this->index >= 624 || $this->index < 0){
if($this->index >= 625 || $this->index < 0) $this->setSeed(4357);
for($kk = 0; $kk < 227; ++$kk){
$y = ($this->mt[$kk] & 0x80000000) | ($this->mt[$kk + 1] & 0x7fffffff);
$this->mt[$kk] = $this->mt[$kk+397] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1];
}
for(;$kk < 623; ++$kk){
$y = ($this->mt[$kk] & 0x80000000) | ($this->mt[$kk + 1] & 0x7fffffff);
$this->mt[$kk] = $this->mt[$kk-227] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1];
}
$y = ($this->mt[623] & 0x80000000) | ($this->mt[0] & 0x7fffffff);
$this->mt[623] = $this->mt[396] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1];
$this->index = 0;
}
$y = $this->mt[$this->index++];
$y ^= ($y >> 11);
$y ^= ($y << 7) & 0x9d2c5680;
$y ^= ($y << 15) & 0xefc60000;
$y ^= ($y >> 18);
return $y;
}
public function nextInt($bound = null){
return $bound == null ? $this->genRandInt() >> 1 : $this->genRandInt() % $bound;
}
public function nextFloat(){
return $this->genRandInt() / 0xffffffff;
}
}

View file

@ -44,29 +44,22 @@ define("MASK32", $val);
class MersenneTwister{
public $mt;
public $mti = 0;
static $MAG_01 = [0, MATRIX_A];
const N = N;
//the class constant is not used anywhere in this namespace,
//but it makes the API cleaner.
//^^ u probably wanted to say makes code look weirder
function __construct(){
$this->bits32 = PHP_INT_MAX == 2147483647;
if(func_num_args() == 1){
$this->init_with_integer(func_get_arg(0));
}
public function __construct($seed = null){
$this->init_with_integer($seed == null ? ((int)microtime(1) & 0xffffffff) : $seed);
}
function init_with_integer($integer_seed){
$integer_seed = force_32_bit_int($integer_seed);
$this->mt = array_fill(0, N, 0);
$this->mt[0] = $integer_seed;
for($this->mti = 1; $this->mti < N; ++$this->mti){
for($this->mti = 1; $this->mti < 624; ++$this->mti){
$this->mt[$this->mti] = add_2(mul(1812433253, ($this->mt[$this->mti - 1] ^ (($this->mt[$this->mti - 1] >> 30) & 3))), $this->mti);
}
}

View file

@ -49,7 +49,14 @@ class Random{
public function nextFloat(){
return $this->nextInt() / 0x7FFFFFFF;
}
public function nextLong($n = -1){
if($n > 0){
return Utils::readLong($this->nextBytes(8)) & 0x7FFFFFFF % $n;
}
return Utils::readLong($this->nextBytes(8)) & 0x7FFFFFFF;
}
public function nextInt($n = -1){
if($n > 0){
return Utils::readInt($this->nextBytes(4)) & 0x7FFFFFFF % $n;

View file

@ -105,7 +105,6 @@ class ExperimentalGenerator implements NewLevelGenerator{
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->level->getSeed());
$noiseArray = ExperimentalGenerator::getFastNoise3D($this->noiseBase, 16, 128, 16, 4, 8, 4, $chunkX * 16, 0, $chunkZ * 16);
$biomeCache = [];
$t = microtime(1);
for($chunkY = 0; $chunkY < 8; ++$chunkY){
$chunk = "";
$startY = $chunkY << 4;
@ -162,13 +161,12 @@ class ExperimentalGenerator implements NewLevelGenerator{
}
$this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk);
}
$t = microtime(1) - $t;
console("gen:".$t);
foreach($this->genPopulators as $pop){
$pop->populate($this->level, $chunkX, $chunkZ, $this->random);
}
$this->caveGenerator->generate($this->level, $chunkX, $chunkZ);
}
public function populateChunk($chunkX, $chunkZ){

View file

@ -152,9 +152,7 @@ class NormalGenerator implements NewLevelGenerator{
$this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, $biomes);
$this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk);
}
//$this->caveGenerator->curChunkX = $chunkX;
//$this->caveGenerator->curChunkZ = $chunkZ;
//$this->caveGenerator->generate($this->level, $chunkX, $chunkZ);
//$this->caveGenerator->generate($this->level, $chunkX, $chunkZ); TODO speedup
}
public function populateChunk($chunkX, $chunkZ){

View file

@ -4,174 +4,185 @@ class CaveGenerator
{
public $range = 8;
public $rand;
public $curChunkX, $curChunkZ;
public function __construct($seed){
$this->rand = new Random($seed);
$this->rand = new MTRandom($seed);
}
public function generate(Level $level, $xCenter, $zCenter){
$i = $this->range;
public function generate(Level $level, $chunkX, $chunkZ){
$range = $this->range;
$this->rand->setSeed($level->getSeed());
$seedInt1 = ((int)($this->rand->nextInt() / 2) * 2) + 1;
$seedInt2 = ((int)($this->rand->nextInt() / 2) * 2) + 1;
$seedInt1 = $this->rand->nextInt();
$seedInt2 = $this->rand->nextInt();
for($x = $xCenter - $i; $x <= $xCenter + $i; ++$x){
for($z = $zCenter - $i; $z <= $zCenter + $i; ++$z){
for($x = $chunkX - $range; $x <= $chunkX + $range; ++$x){
for($z = $chunkZ - $range; $z <= $chunkZ + $range; ++$z){
$this->rand->setSeed((($x * $seedInt1) + ($z * $seedInt2)) ^ $level->getSeed());
$this->generate2($level, $x, $z, $xCenter, $zCenter);
$this->recursiveGenerate($level, $x, $z, $chunkX, $chunkZ);
}
}
}
public function generate2(Level $level, $x, $z, $xCenter, $zCenter){
public function recursiveGenerate(Level $level, $chunkXoffsetted, $chunkZoffsetted, $chunkX, $chunkZ){
$nextInt = $this->rand->nextInt($this->rand->nextInt($this->rand->nextInt(40) + 1) + 1);
if($this->rand->nextInt(15) != 0){
$nextInt = 0;
}
for($i = 0; $i < $nextInt; ++$i){
$randX = ($x * 16) + $this->rand->nextInt(16);
$randX = ($chunkXoffsetted * 16) + $this->rand->nextInt(16);
$randY = $this->rand->nextInt($this->rand->nextInt(120) + 8);
$randZ = ($z * 16) + $this->rand->nextInt(16);
$randZ = ($chunkZoffsetted * 16) + $this->rand->nextInt(16);
$i2 = 1;
if($this->rand->nextInt(4) == 0){
$this->generate3($level, $xCenter, $zCenter, $randX, $randY, $randZ);
$this->generateLargeCaveNode($level, $chunkX, $chunkZ, $randX, $randY, $randZ);
$i2 = 1 + $this->rand->nextInt(4);
}
for($i3 = 0; $i3 < $i2; ++$i3){
//generate(xCenter, zCenter, tileIdArray, randX, randY, randZ, (this.rand.nextFloat() * 2.0f) + this.rand.nextFloat(), this.rand.nextFloat() * 3.1415927f * 2.0f, ((this.rand.nextFloat() - 0.5f) * 2.0f) / 8.0f, 0, 0, 1.0d);
$this->generate4($level, $xCenter, $zCenter, $randX, $randY, $randZ, ($this->rand->nextFloat() * 2) + $this->rand->nextFloat(), $this->rand->nextFloat() * M_PI * 2, (($this->rand->nextFloat() - 0.5) * 2) / 8, 0, 0, 1);
$v17 = $this->rand->nextFloat() * M_PI * 2;
$v18 = ($this->rand->nextFloat() - 0.5) * 2 / 8;
$v19 = $this->rand->nextFloat() * 2 + $this->rand->nextFloat();
if($this->rand->nextInt(10) == 0) {
$v19 *= $this->rand->nextFloat() * $this->rand->nextFloat() * 3 + 1;
}
$this->generateCaveNode($level, $chunkX, $chunkZ, $randX, $randY, $randZ, $v19, $v17, $v18, 0, 0, 1);
}
}
}
public function generate3(Level $level, $xCenter, $zCenter, $x, $y, $z){
public function generateLargeCaveNode(Level $level, $xCenter, $zCenter, $x, $y, $z){
//generate(xCenter, zCenter, tileIdArray, x, y, z, 1.0f + (this.rand.nextFloat() * 6.0f), 0.0f, 0.0f, -1, -1, 0.5d); gen4
$this->generate4($level, $xCenter, $zCenter, $x, $y, $z, 1 + ($this->rand->nextFloat() * 6), 0, 0, -1, -1, 0.5);
$this->generateCaveNode($level, $xCenter, $zCenter, $x, $y, $z, 1 + ($this->rand->nextFloat() * 6), 0, 0, -1, -1, 0.5);
}
public function generate4(Level $level, $xCenter, $zCenter, $x, $y, $z, $randFloat, $f1, $f2, $k, $i1, $d3){
$d = $xCenter*16 + 8;
$d2 = $zCenter*16 + 8;
public function generateCaveNode(Level $level, $chunkX, $chunkZ, $x, $y, $z, $randFloat, $f1, $f2, $unk_1, $unk_2, $d3){
$chunkCenterX = $chunkX*16 + 8;
$chunkCenterZ = $chunkZ*16 + 8;
$f = $f3 = 0;
$random = new Random($this->rand->nextInt());
$random = new MTRandom($this->rand->nextInt());
if($i1 <= 0){
if($unk_2 <= 0){
$i = ($this->range * 16) - 16;
$i1 = $i - $random->nextInt((int)($i / 4));
$unk_2 = $i - $random->nextInt((int)($i / 4));
}
$z2 = false;
if($k == -1){
$k = $i1 / 2;
if($unk_1 == -1){
$unk_1 = (int)($unk_2 / 2);
$z2 = true;
}
$nextInt = $random->nextInt((int)($i1 / 2));
$z3 = $random->nextInt(6) == 0;
while($k < $i1){
$sin = 1.5 + (sin(($k * M_PI) / $i1) * $randFloat * 1);
$d4 = $sin * $d3;
$cos = cos($f2);
$x += cos($f1) * $cos;
$y += sin($f2);
$z += sin($f1) * $cos;
$var27 = $random->nextInt((int)($unk_2 / 2)) + ($unk_2 / 4);
for($var28 = ($random->nextInt(6) == 0); $unk_1 < $unk_2; ++$unk_1){
$var29 = 1.5 + sin($unk_1 * M_PI / $unk_2) * $randFloat /* * 1 */;
$var31 = $var29 * $d3;
$var33 = cos($f2);
$var34 = sin($f2);
$x += cos($f1) * $var33;
$y += $var34;
$z += sin($f1) * $var33;
$f22 = $z3 ? ($f2 * 0.92) : ($f2 * 0.7);
$f2 = $f22 + ($f3 * 0.1);
$f2 = $var28 ? ($f2 * 0.92) : ($f2 * 0.7);
$f2 += $f3 * 0.1;
$f1 += $f * 0.1;
$f3 = ($f3 * 0.9) + (($random->nextFloat() - $random->nextFloat()) * $random->nextFloat() * 2);
$f = ($f * 0.75) + (($random->nextFloat() - $random->nextFloat()) * $random->nextFloat() * 4);
if(!$z2 && $k == $nextInt && $randFloat > 1){
$this->generate4($level, $xCenter, $zCenter, $x, $y, $z, ($random->nextFloat() * 0.5) + 0.5, $f1 - M_PI_2, $f2 / 3, $k, $i1, 1);
$this->generate4($level, $xCenter, $zCenter, $x, $y, $z, ($random->nextFloat() * 0.5) + 0.5, $f1 + M_PI_2, $f2 / 3, $k, $i1, 1);
$f3 *= 0.9;
$f *= 0.75;
$f3 += ($random->nextFloat() - $random->nextFloat()) * $random->nextFloat() * 2;
$f += ($random->nextFloat() - $random->nextFloat()) * $random->nextFloat() * 2;
if(!$z2 && $unk_1 == $var27 && $randFloat > 1 && $unk_2 > 0){
//this.generateCaveNode(random.nextLong(), chunkX, chunkZ, par5ArrayOfByte, x, y, z, random.nextFloat() * 0.5F + 0.5F, f1 - ((float)Math.PI / 2F), f2 / 3.0F, unk_1, unk_2, 1.0D);
//this.generateCaveNode(random.nextLong(), chunkX, chunkZ, par5ArrayOfByte, x, y, z, random.nextFloat() * 0.5F + 0.5F, f1 + ((float)Math.PI / 2F), f2 / 3.0F, unk_1, unk_2, 1.0D);
$this->generateCaveNode($level, $chunkX, $chunkZ, $x, $y, $z, $random->nextFloat() * 0.5 + 0.5, $f1 - (M_PI / 2), $f2 / 3, $unk_1, $unk_2, 1);
$this->generateCaveNode($level, $chunkX, $chunkZ, $x, $y, $z, $random->nextFloat() * 0.5 + 0.5, $f1 + (M_PI / 2), $f2 / 3, $unk_1, $unk_2, 1);
return;
}
if($z2 || $random->nextInt(4) != 0){
$d5 = $x - $d;
$d6 = $z - $d2;
$d7 = $i1 - $k;
$d8 = $randFloat + 2 + 16;
if((($d5 * $d5) + ($d6*$d6) - ($d7*$d7)) > $d8*$d8){
$distFromCenterX = $x - $chunkCenterX;
$distFromCenterZ = $z - $chunkCenterZ;
$var39 = ($unk_2 - $unk_1);
$var41 = $randFloat + /*2 + 16*/ 18;
if($distFromCenterX*$distFromCenterX + $distFromCenterZ*$distFromCenterZ - $var39*$var39 > $var41*$var41){
return;
}
if($x >= (($d - 16) - ($sin * 2)) && $z >= (($d2 - 16) - ($sin * 2)) && $x <= ($d + 16 + ($sin * 2)) && $z <= ($d2 + 16 + ($sin * 2))){
$floor = (floor($x - $sin) - ($xCenter * 16)) - 1;
$floor2 = (floor($x + $sin) - ($xCenter * 16)) + 1;
$floor3 = floor($y - $d4) - 1;
$floor4 = floor($y + $d4) + 1;
$floor5 = (floor($z - $sin) - ($zCenter * 16)) - 1;
$floor6 = (floor($z + $sin) - ($zCenter * 16)) + 1;
//if (x >= chunkCenterX - 16.0D - var29 * 2.0D && z >= chunkCenterZ - 16.0D - var29 * 2.0D && x <= chunkCenterX + 16.0D + var29 * 2.0D && z <= chunkCenterZ + 16.0D + var29 * 2.0D)
if($x >= $chunkCenterX - 16 - $var29*2 && $z >= $chunkCenterZ - 16 - $var29*2 && $x <= $chunkCenterX + 16 + $var29*2 && $z <= $chunkCenterZ + 16 + $var29*2){
$minX = floor($x - $var29) - $chunkX*16 - 1;
$maxX = floor($x + $var29) - $chunkX*16 + 1;
$minY = floor($y - $var31) - 1;
$maxY = floor($y + $var31) + 1;
$minZ = floor($z - $var29) - $chunkZ*16 - 1;
$maxZ = floor($z + $var29) - $chunkZ*16 + 1;
if($floor < 0) $floor = 0;
if($floor2 > 16) $floor2 = 16;
if($floor3 < 1) $floor3 = 1;
if($floor4 > 120) $floor4 = 120;
if($floor5 < 0) $floor5 = 0;
if($floor6 > 16) $floor6 = 16;
if($minX < 0) $minX = 0;
if($maxX > 16) $maxX = 16;
$z4 = false;
for($i2 = $floor; !$z4 && $i2 < $floor2; ++$i2){ //x
for($i3 = $floor5; !$z4 && $i3 < $floor6; ++$i3){ //z
$i4 = $floor4 + 1; //y
while(!$z4 && $i4 >= ($floor3 - 1)){
if($i4 >= 0 && $i4 < 128){
$id = $level->level->getBlockID($this->curChunkX*16 + $x, $y, $this->curChunkZ*16 + $z);
$z4 = $id == WATER || $id == STILL_WATER;
if($i4 != $floor3 - 1 && $i2 != $floor && $i2 != $floor2 - 1 && $i3 != $floor5 && $i3 != $floor6 - 1){
$i4 = $floor3;
if($minY < 1) $minY = 1;
if($maxY > 120) $maxY = 120;
if($minZ < 0) $minZ = 0;
if($maxZ > 16) $maxZ = 16;
$hasWater = false;
for($blockX = $minX; !$hasWater && $blockX < $maxX; ++$blockX){
for($blockZ = $minZ; !$hasWater && $blockZ < $maxZ; ++$blockZ){
for($blockY = $maxY + 1; !$hasWater && $blockY >= $minY - 1; --$blockY){
if($blockY >= 0 && $blockY < 128){
$id = $level->level->getBlockID($blockX + $chunkX*16, $blockY, $blockZ + $chunkZ * 16);
$hasWater = $id == WATER || $id == STILL_WATER;
//if (blockY != minY - 1 && blockX != minX && blockX != maxX - 1 && blockZ != minZ && blockZ != maxZ - 1)
if($blockY != $minY - 1 && $blockX != $minX && $blockX != $maxX - 1 && $blockZ != $minZ && $blockZ != $maxZ - 1){
$blockY = $minY;
}
}
--$i4;
}
}
}
if($z4){
continue;
}else{
for($i6 = $floor; $i6 < $floor2; ++$i6){ //x
$d9 = ((($i6 + ($xCenter * 16)) + 0.5) - $x) / $sin;
for($i7 = $floor5; $i7 < $floor6; ++$i7){ //z
$d10 = ((($i7 + ($zCenter * 16)) + 0.5) - $z) / $sin;
$i8 = ((($i6 * $i6) + $i7) * 128) + $floor4;
$z5 = false;
if(($d9 * $d9) + ($d10 * $d10) < 1){
for($i9 = $floor4 - 1; $i9 >= $floor3; --$i9){
$d11 = (($i9 + 0.5) - $y) / $d4;
if($d11 > -0.7 && ($d9*$d9)+($d11*$d11)+($d10*$d10) < 1){
$b = $level->level->getBlockID($this->curChunkX*16 + $i6, $floor4, $this->curChunkZ*16 + $i7);
if($b == GRASS) $z5 = true;
if($b == STONE || $b == DIRT || $b == GRASS){
if($i9 < 10){
$level->level->setBlockID($this->curChunkX*16 + $i6, $floor4, $this->curChunkZ*16 + $i7, LAVA);
if(!$hasWater){
for($blockX = $minX; $blockX < $maxX; ++$blockX){
$var59 = (($blockX + $chunkX*16) + 0.5 - $x) / $var29;
for($blockZ = $minZ; $blockZ < $maxZ; ++$blockZ){
$var46 = (($blockZ + $chunkZ*16) + 0.5 - $z) / $var29;
$yPosition = $maxY;
$hasGrass = false;
if($var59*$var59 + $var46*$var46 < 1){
for($blockY = $maxY - 1; $blockY >= $minY; --$blockY){
$var51 = ($blockY + 0.5 - $y) / $var31;
if($var51 > -0.7 && $var59*$var59 + $var51*$var51 + $var46*$var46 < 1){
$blockID = $level->level->getBlockID($blockX + $chunkX*16, $yPosition, $blockZ + $chunkZ * 16);
$hasGrass = $blockID == GRASS;
if($blockID == STONE || $blockID == DIRT || $hasGrass){
if($blockY < 10){
$level->level->setBlockID($blockX + $chunkX*16, $yPosition, $blockZ + $chunkZ * 16, LAVA);
}else{
//ConsoleAPI::info("place air at ".($this->curChunkX*16 + $i6).":$floor4:".($this->curChunkZ*16 + $i7));
$level->level->setBlockID($this->curChunkX*16 + $i6, $floor4, $this->curChunkZ*16 + $i7, 0, true);
if($z5){
$belowID = $level->level->getBlockID($this->curChunkX*16 + $i6, $floor4 - 1, $this->curChunkZ*16 + $i7);
if($belowID == DIRT){
ConsoleAPI::info("place grass at ".($this->curChunkX*16 + $i6).":$floor4:".($this->curChunkZ*16 + $i7));
$level->level->setBlockID($this->curChunkX*16 + $i6, $floor4 - 1, $this->curChunkZ*16 + $i7, GRASS);
}
$level->level->setBlockID($blockX + $chunkX*16, $yPosition, $blockZ + $chunkZ * 16, 0);
if($hasGrass && $level->level->getBlockID($blockX + $chunkX*16, $yPosition - 1, $blockZ + $chunkZ * 16) == DIRT){
//ConsoleAPI::debug("place grass at ".($blockX + $chunkX*16).":".($yPosition - 1).":".($blockZ + $chunkZ * 16));
$level->level->setBlockID($blockX + $chunkX*16, $yPosition - 1, $blockZ + $chunkZ * 16, GRASS);
}
}
}
}
--$i8;
--$yPosition;
}
}
}
}
if($z2) return;
}
}
}
}
}
}