better chunk unloading, world gen optimizations

This commit is contained in:
GameHerobrine 2023-07-01 03:30:45 +03:00
parent e785db588d
commit 5cdfedfc75
16 changed files with 104 additions and 109 deletions

View file

@ -141,10 +141,10 @@ class LevelAPI{
break;
case "HELL":
case "NETHER":
$generator = new HellGenerator();
$generator = new HellGenerator($options);
break;
case "END":
$generator = new EndGenerator();
$generator = new EndGenerator($options);
break;
default:
$generator = new NormalGenerator($options);

View file

@ -415,6 +415,7 @@ class Player{
}
}
asort($this->chunksOrder);
arsort($chunkToUnload);
if($this->level->generatorType != 0){
foreach($chunkToUnload as $chunk => $useless){
$chunkI = explode(":", $chunk);

View file

@ -167,7 +167,11 @@ class PMFLevel extends PMF{
}
return true;
}
public function getSeed(){
return $this->levelData["seed"];
}
public function getData($index){
if(!isset($this->levelData[$index])){
return false;
@ -432,15 +436,13 @@ class PMFLevel extends PMF{
$Z = $z >> 4;
$Y = $y >> 4;
$index = $this->getIndex($X, $Z);
if(!isset($this->chunks[$index]) || $this->chunks[$index] === false || ($this->chunks[$index][$Y] === false)){
return 0;
}
$aX = $x & 0xf;
$aZ = $z & 0xf;
$aY = $y & 0xf;
if(is_array($this->chunks) && isset($this->chunks[$index]) && is_array($this->chunks[$index]) && isset($this->chunks[$index][$Y]) && is_string($this->chunks[$index][$Y])){
$b = ord($this->chunks[$index][$Y][($aY + ($aX << 6) + ($aZ << 10))]);
}else{ //php8 fix
$b = 0;
}
$b = ord($this->chunks[$index][$Y][($aY + ($aX << 6) + ($aZ << 10))]);
return $b;
}
@ -478,14 +480,17 @@ class PMFLevel extends PMF{
$Z = $z >> 4;
$Y = $y >> 4;
$index = $this->getIndex($X, $Z);
if(!isset($this->chunks[$index]) || $this->chunks[$index] === false || ($this->chunks[$index][$Y] === false)){
return 0;
}
$aX = $x & 0xf;
$aZ = $z & 0xf;
$aY = $y & 0xf;
if(is_array($this->chunks) && isset($this->chunks[$index]) && is_array($this->chunks[$index]) && isset($this->chunks[$index][$Y]) && is_string($this->chunks[$index][$Y])){
$m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 6) + ($aZ << 10))]);
}else{ //php8 fix
$m = 0;
}
//if(is_array($this->chunks) && isset($this->chunks[$index]) && is_array($this->chunks[$index]) && isset($this->chunks[$index][$Y]) && is_string($this->chunks[$index][$Y])){
$m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 6) + ($aZ << 10))]);
//}else{ //php8 fix
// $m = 0;
//}
if(($y & 1) === 0){
$m = $m & 0x0F;
@ -540,20 +545,21 @@ class PMFLevel extends PMF{
if($this->loadChunk($X, $Z) === false){
return [AIR, 0];
}
}elseif($this->chunks[$index][$Y] === false){
}
if($this->chunks[$index][$Y] === false){
return [AIR, 0];
}
$aX = $x & 0xf;
$aZ = $z & 0xf;
$aY = $y & 0xf;
#Need to fix. But idk how.
if(is_array($this->chunks) && is_array($this->chunks[$index]) && is_string($this->chunks[$index][$Y])){ //PHP8 warn fix
//if(is_array($this->chunks) && is_array($this->chunks[$index]) && is_string($this->chunks[$index][$Y])){ //PHP8 warn fix
$b = ord($this->chunks[$index][$Y][($aY + ($aX << 6) + ($aZ << 10))]);
$m = ord($this->chunks[$index][$Y][(($aY >> 1) + 16 + ($aX << 6) + ($aZ << 10))]);
}else{
$b = 0;
$m = 0;
}
//}else{
// $b = 0;
// $m = 0;
//}
if(($y & 1) === 0){
$m = $m & 0x0F;

View file

@ -7,7 +7,7 @@ class Level{
* 1 - Infinite
* @var integer
*/
public $generatorType = 0;
public $generatorType = 1;
/**
* @var Config
@ -447,7 +447,6 @@ class Level{
if(!isset($this->level)){
return false;
}
if(strlen($data) != 16384) console(strlen($data));
$this->changedCount[$X . ":" . $Y . ":" . $Z] = 4096;
if(ADVANCED_CACHE == true){
Cache::remove("world:{$this->name}:$X:$Z");
@ -647,7 +646,7 @@ class Level{
if(!isset($this->level)){
return false;
}
return (int) $this->level->getData("seed");
return (int) $this->level->getSeed();
}
public function setSeed($seed){

View file

@ -17,7 +17,6 @@ class ExperimentalGenerator implements NewLevelGenerator{
private $genPopulators = array();
private $level;
private $random;
private $worldHeight = 65;
private $waterHeight = 63;
private $noiseHills;
private $noisePatches;
@ -56,10 +55,7 @@ class ExperimentalGenerator implements NewLevelGenerator{
public function init(Level $level, Random $random){
$this->level = $level;
$this->random = $random;
$this->random->setSeed($this->level->getSeed());
$this->noiseHills = new NoiseGeneratorPerlin($this->random, 10);
$this->noisePatches = new NoiseGeneratorSimplex($this->random, 2);
$this->noisePatchesSmall = new NoiseGeneratorSimplex($this->random, 2);
$this->random->setSeed($this->level->level->getSeed());
$this->noiseBase = new NoiseGeneratorPerlin($this->random, 4);
$this->selector = new BiomeSelector($this->random, BiomeSelector::$biomes[BIOME_PLAINS]);
$ores = new OrePopulator();
@ -88,11 +84,11 @@ class ExperimentalGenerator implements NewLevelGenerator{
$tallGrass = new TallGrassPopulator();
$tallGrass->setBaseAmount(5);
$tallGrass->setRandomAmount(0);
//$this->populators[] = $tallGrass;
$this->populators[] = $tallGrass;
}
public function pickBiome($x, $z){
$hash = $x * 2345803 ^ $z * 9236449 ^ $this->level->getSeed();
$hash = $x * 2345803 ^ $z * 9236449 ^ $this->level->level->getSeed();
$hash *= $hash + 223;
$xNoise = $hash >> 20 & 3;
$zNoise = $hash >> 22 & 3;
@ -106,7 +102,7 @@ class ExperimentalGenerator implements NewLevelGenerator{
}
public function generateChunk($chunkX, $chunkZ){
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
$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 = [];
@ -122,10 +118,10 @@ class ExperimentalGenerator implements NewLevelGenerator{
$weightSum = 0;
$biome = $this->pickBiome($chunkX * 16 + $x, $chunkZ * 16 + $z);
$this->level->level->setBiomeId(($chunkX << 4) + $x, ($chunkZ << 4) + $z, $biome->getID());
$this->level->level->setBiomeId(($chunkX << 4) + $x, ($chunkZ << 4) + $z, $biome->id);
for($sx = -ExperimentalGenerator::$SMOOTH_SIZE; $sx <= ExperimentalGenerator::$SMOOTH_SIZE; ++$sx){
for($sz = -ExperimentalGenerator::$SMOOTH_SIZE; $sz <= ExperimentalGenerator::$SMOOTH_SIZE; ++$sz){
$weight = ExperimentalGenerator::$GAUSSIAN_KERNEL[$sx + ExperimentalGenerator::$SMOOTH_SIZE][$sz + ExperimentalGenerator::$SMOOTH_SIZE];
$weight = ExperimentalGenerator::$GAUSSIAN_KERNEL[0][0];
if($sx === 0 and $sz === 0){
$adjacent = $biome;
@ -138,8 +134,8 @@ class ExperimentalGenerator implements NewLevelGenerator{
}
}
$minSum += ($adjacent->getMin() - 1) * $weight; //TODO biomes
$maxSum += $adjacent->getMax() * $weight;
$minSum += ($adjacent->minY - 1) * $weight;
$maxSum += $adjacent->maxY * $weight;
$weightSum += $weight;
}
@ -153,18 +149,17 @@ class ExperimentalGenerator implements NewLevelGenerator{
}
$noiseAdjustment = 2 * (($maxSum - $y) / ($maxSum - $minSum)) - 1;
$caveLevel = $minSum - 10;
$distAboveCaveLevel = max(0, $y - $caveLevel); // must be positive
$noiseAdjustment = min($noiseAdjustment, 0.4 + ($distAboveCaveLevel / 10));
$distAboveCaveLevel = $y - $caveLevel > 0 ? $y - $caveLevel : 0; //max(0, $y - $caveLevel); // must be positive, looks like max is slower
$noiseAdjustment = ($noiseAdjustment < (0.4 + ($distAboveCaveLevel / 10))) ? $noiseAdjustment : (0.4 + ($distAboveCaveLevel / 10)); //min($noiseAdjustment, 0.4 + ($distAboveCaveLevel / 10));
$noiseValue = $noiseArray[$x][$z][$y] + $noiseAdjustment;
if($noiseValue > 0){
$chunk .= (($noiseValue > 0) ? "\x01" : (($y <= $this->waterHeight) ? "\x09" : "\x00"));
/*if($noiseValue > 0){
$chunk .= "\x01";
}elseif($y <= $this->waterHeight){
$chunk .= "\x09";
}else{
$chunk .= "\x00";
}
}*/
}
$chunk .= "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
//$chunk .= str_repeat("\xff", 16);
@ -179,14 +174,11 @@ class ExperimentalGenerator implements NewLevelGenerator{
foreach($this->genPopulators as $pop){
$pop->populate($this->level, $chunkX, $chunkZ, $this->random);
}
}
public function populateChunk($chunkX, $chunkZ){
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->level->getSeed());
foreach($this->populators as $populator){
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
$populator->populate($this->level, $chunkX, $chunkZ, $this->random);
}
}
@ -218,19 +210,15 @@ class ExperimentalGenerator implements NewLevelGenerator{
$dy1 = (($nny - $yy) / ($nny - $ny));
$dy2 = (($yy - $ny) / ($nny - $ny));
$noiseArray[$xx][$zz][$yy] = (($nnz - $zz) / ($nnz - $nz)) * (
$dy1 * (
$dx1 * $noiseArray[$nx][$nz][$ny] + $dx2 * $noiseArray[$nnx][$nz][$ny]
) + $dy2 * (
$dx1 * $noiseArray[$nx][$nz][$nny] + $dx2 * $noiseArray[$nnx][$nz][$nny]
)
) + (($zz - $nz) / ($nnz - $nz)) * (
$dy1 * (
$dx1 * $noiseArray[$nx][$nnz][$ny] + $dx2 * $noiseArray[$nnx][$nnz][$ny]
) + $dy2 * (
$dx1 * $noiseArray[$nx][$nnz][$nny] + $dx2 * $noiseArray[$nnx][$nnz][$nny]
)
);
$noiseArray[$xx][$zz][$yy] = (($nnz - $zz) / ($nnz - $nz)) *
(
$dy1 * ($dx1 * $noiseArray[$nx][$nz][$ny] + $dx2 * $noiseArray[$nnx][$nz][$ny]) +
$dy2 * ($dx1 * $noiseArray[$nx][$nz][$nny] + $dx2 * $noiseArray[$nnx][$nz][$nny])
) +
(($zz - $nz) / ($nnz - $nz)) * (
$dy1 * ($dx1 * $noiseArray[$nx][$nnz][$ny] + $dx2 * $noiseArray[$nnx][$nnz][$ny]) +
$dy2 * ($dx1 * $noiseArray[$nx][$nnz][$nny] + $dx2 * $noiseArray[$nnx][$nnz][$nny])
);
}
}
}

View file

@ -1,7 +1,7 @@
<?php
abstract class Biome{
protected $id, $topBlocks, $fillerBlock, $name, $minY, $maxY;
public $id, $topBlocks, $fillerBlock, $name, $minY, $maxY;
public function __construct($id, $name){
$this->name = $name;
$this->id = $id;
@ -45,6 +45,7 @@ abstract class Biome{
}
public static function init(){
ConsoleAPI::debug("Init Biomes");
BiomeSelector::registerBiome(new BiomeExtremeHills(BIOME_EXTREME_HILLS, "Extreme Hills"));
BiomeSelector::registerBiome(new BiomePlains(BIOME_PLAINS, "Plains"));
BiomeSelector::registerBiome(new BiomeExtremeHillsEdge(BIOME_EXTREME_HILLS_EDGE, "Extreme Hills Edge"));
@ -56,6 +57,7 @@ abstract class Biome{
BiomeSelector::registerBiome(new BiomeTaiga(BIOME_TAIGA, "Taiga"));
BiomeSelector::registerBiome(new BiomeForest(BIOME_FOREST, "Forest"));
BiomeSelector::registerBiome(new BiomeForest(BIOME_BIRCH_FOREST, "Birch Forest"));
}
private static $initialized = false;

View file

@ -25,8 +25,8 @@ class BiomeSelector
}
public static function registerBiome(Biome $b){
//ConsoleAPI::debug("Registered $b");
self::$biomes[$b->getID()] = $b;
ConsoleAPI::debug("Registered $b");
self::$biomes[$b->id] = $b;
}
/**
* @param int $id
@ -81,11 +81,11 @@ class BiomeSelector
}
public function getTemperature($x, $z){ //TODO cache
return ($this->temperature->noise2D($x * (1/512), $z * (1/512), 2, 1 / 16, true) + 1) / 2;
return ($this->temperature->noise2D($x * 0.001953125, $z * 0.001953125, 2, 0.0625, true) + 1) / 2;
}
public function getRainfall($x, $z){
return ($this->rainfall->noise2D($x * (1/512), $z * (1/512), 2, 1 / 16, true) + 1) / 2;
return ($this->rainfall->noise2D($x * 0.001953125, $z * 0.001953125, 2, 0.0625, true) + 1) / 2;
}
public function pickBiome($x, $z){

View file

@ -7,7 +7,7 @@ class BiomeTaiga extends BiomeWithSnow
$this->setMinMax(63, 81);
$this->setTopBlocks([
[PODZOL, 0],
[GRASS, 0],
[DIRT, 0],
[DIRT, 0],
[DIRT, 0],

View file

@ -26,10 +26,6 @@ abstract class NoiseGenerator{
protected $offsetY = 0;
protected $offsetZ = 0;
protected $octaves = 8;
public static function floor($x){
return $x >= 0 ? (int) $x : (int) ($x - 1);
}
public static function fade($x){
return $x * $x * $x * ($x * ($x * 6 - 15) + 10);

View file

@ -61,9 +61,9 @@ class NoiseGeneratorPerlin extends NoiseGenerator{
$y += $this->offsetY;
$z += $this->offsetZ;
$floorX = self::floor($x);
$floorY = self::floor($y);
$floorZ = self::floor($z);
$floorX = floor($x); //normal floor is faster
$floorY = floor($y);
$floorZ = floor($z);
$X = $floorX & 0xFF;
$Y = $floorY & 0xFF;
@ -86,14 +86,16 @@ class NoiseGeneratorPerlin extends NoiseGenerator{
$BA = $this->perm[$B] + $Z;
$BB = $this->perm[$B + 1] + $Z;
return self::lerp($fZ, self::lerp($fY, self::lerp($fX, self::grad($this->perm[$AA], $x, $y, $z),
self::grad($this->perm[$BA], $x - 1, $y, $z)),
self::lerp($fX, self::grad($this->perm[$AB], $x, $y - 1, $z),
self::grad($this->perm[$BB], $x - 1, $y - 1, $z))),
self::lerp($fY, self::lerp($fX, self::grad($this->perm[$AA + 1], $x, $y, $z - 1),
self::grad($this->perm[$BA + 1], $x - 1, $y, $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))));
return self::lerp($fZ,
self::lerp($fY,
self::lerp($fX, self::grad($this->perm[$AA], $x, $y, $z), self::grad($this->perm[$BA], $x - 1, $y, $z)),
self::lerp($fX, self::grad($this->perm[$AB], $x, $y - 1, $z), self::grad($this->perm[$BB], $x - 1, $y - 1, $z))
),
self::lerp($fY,
self::lerp($fX, self::grad($this->perm[$AA + 1], $x, $y, $z - 1), self::grad($this->perm[$BA + 1], $x - 1, $y, $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))
)
);
}
public function getNoise2D($x, $y){

View file

@ -81,9 +81,9 @@ class NoiseGeneratorSimplex extends NoiseGeneratorPerlin{
// Skew the input space to determine which simplex cell we're in
$s = ($x + $y + $z) * self::$F3; // Very nice and simple skew factor for 3D
$i = self::floor($x + $s);
$j = self::floor($y + $s);
$k = self::floor($z + $s);
$i = floor($x + $s);
$j = floor($y + $s);
$k = floor($z + $s);
$t = ($i + $j + $k) * self::$G3;
$X0 = $i - $t; // Unskew the cell origin back to (x,y,z) space
$Y0 = $j - $t;
@ -214,8 +214,8 @@ class NoiseGeneratorSimplex extends NoiseGeneratorPerlin{
// Skew the input space to determine which simplex cell we're in
$s = ($x + $y) * self::$F2; // Hairy factor for 2D
$i = self::floor($x + $s);
$j = self::floor($y + $s);
$i = floor($x + $s);
$j = floor($y + $s);
$t = ($i + $j) * self::$G2;
$X0 = $i - $t; // Unskew the cell origin back to (x,y) space
$Y0 = $j - $t;

View file

@ -18,16 +18,16 @@ class OreObject{
return ($level->level->getBlockID($x, $y, $z) != AIR);
}
public function placeObject(Level $level, Vector3 $pos){
public function placeObject(Level $level, $xVect, $yVect, $zVect){
$clusterSize = (int) $this->type->clusterSize;
$angle = $this->random->nextFloat() * M_PI;
$offset = VectorMath::getDirection2D($angle)->multiply($clusterSize)->divide(8);
$x1 = $pos->x + 8 + $offset->x;
$x2 = $pos->x + 8 - $offset->x;
$z1 = $pos->z + 8 + $offset->y;
$z2 = $pos->z + 8 - $offset->y;
$y1 = $pos->y + $this->random->nextRange(0, 3) + 2;
$y2 = $pos->y + $this->random->nextRange(0, 3) + 2;
$x1 = $xVect + 8 + $offset->x;
$x2 = $xVect + 8 - $offset->x;
$z1 = $zVect + 8 + $offset->y;
$z2 = $zVect + 8 - $offset->y;
$y1 = $yVect + $this->random->nextRange(0, 3) + 2;
$y2 = $yVect + $this->random->nextRange(0, 3) + 2;
for($count = 0; $count <= $clusterSize; ++$count){
$seedX = $x1 + ($x2 - $x1) * $count / $clusterSize;
$seedY = $y1 + ($y2 - $y1) * $count / $clusterSize;
@ -53,7 +53,7 @@ class OreObject{
$sizeZ = ($z + 0.5 - $seedZ) / $size;
$sizeZ *= $sizeZ;
if(($sizeX + $sizeY + $sizeZ) < 1 and $level->level->getBlockID($x, $y, $z) === STONE){
$level->setBlockRaw(new Vector3($x, $y, $z), $this->type->material);
$level->level->setBlock($x, $y, $z, $this->type->material->getID(), $this->type->material->getMetadata());
}
}
}

View file

@ -36,13 +36,15 @@ class GroundCover extends Populator
break;
}
if($y <= $waterHeight and $b->getID() == GRASS and $level->level->getBlockID($pcx, $y + 1, $pcz) == STILL_WATER){
$b = BlockAPI::get(DIRT);
}
if($b->getMetadata() === 0){
$level->level->setBlockId($pcx, $y, $pcz, $b->getID());
}else{
$level->level->setBlock($pcx, $y, $pcz, $b->getID(), $b->getMetadata());
//$b = BlockAPI::get(DIRT);
$level->level->setBlock($pcx, $y, $pcz, DIRT, 0);
continue;
}
//if($b->getMetadata() === 0){
// $level->level->setBlockId($pcx, $y, $pcz, $b->getID());
//}else{
$level->level->setBlock($pcx, $y, $pcz, $b->getID(), $b->getMetadata());
//}
}
}
}

View file

@ -12,7 +12,7 @@ class OrePopulator extends Populator{
$y = $random->nextRange($ore->type->minHeight, $ore->type->maxHeight);
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 16);
if($ore->canPlaceObject($level, $x, $y, $z)){
$ore->placeObject($level, new Vector3($x, $y, $z));
$ore->placeObject($level, $x, $y, $z);
}
}
}

View file

@ -42,12 +42,11 @@ class TallGrassPopulator extends Populator{
$xx = $x - 7 + $random->nextRange(0, 15);
$zz = $z - 7 + $random->nextRange(0, 15);
$yy = $this->getHighestWorkableBlock($xx, $zz);
$vector = new Vector3($xx, $yy, $zz);
if($yy !== -1 and $this->canTallGrassStay($this->level->getBlockRaw($vector))){
if($yy !== -1 and $this->canTallGrassStay($this->level->level->getBlockID($xx, $yy, $zz),$this->level->level->getBlockID($xx, $yy - 1, $zz))){
if(mt_rand(1,4) == 1){
$this->level->setBlockRaw($vector, new TallGrassBlock(2));
$this->level->level->setBlock($xx, $yy, $zz, TALL_GRASS, 2);
}else{
$this->level->setBlockRaw($vector, new TallGrassBlock(1));
$this->level->level->setBlock($xx, $yy, $zz, TALL_GRASS, 1);
}
}
@ -55,14 +54,14 @@ class TallGrassPopulator extends Populator{
}
}
private function canTallGrassStay(Block $block){
return $block->getID() === AIR and $block->getSide(0)->getID() === GRASS;
private function canTallGrassStay($id, $idbottom){
return $id === AIR and $idbottom === GRASS;
}
private function getHighestWorkableBlock($x, $z){
for($y = 128; $y > 0; --$y){
$b = $this->level->getBlockRaw(new Vector3($x, $y, $z));
if($b->getID() === AIR or $b->getID() === LEAVES){
$b = $this->level->level->getBlockID($x, $y, $z);
if($b !== DIRT and $b !== GRASS){
if(--$y <= 0){
return -1;
}

View file

@ -35,8 +35,8 @@ class TreePopulator extends Populator{
private function getHighestWorkableBlock($x, $z){
for($y = 128; $y > 0; --$y){
$b = $this->level->getBlockRaw(new Vector3($x, $y, $z));
if($b->getID() !== DIRT and $b->getID() !== GRASS){
$b = $this->level->level->getBlockID($x, $y, $z);
if($b !== DIRT and $b !== GRASS){
if(--$y <= 0){
return -1;
}