Stop hardcoding chunk dimensions everywhere (#4443)

This commit is contained in:
Dylan T
2021-09-10 16:13:25 +01:00
committed by GitHub
parent 9d5a86fe53
commit 4111d92b98
24 changed files with 166 additions and 140 deletions

View File

@ -28,6 +28,7 @@ use pocketmine\item\LegacyStringToItemParser;
use pocketmine\item\LegacyStringToItemParserException;
use pocketmine\world\ChunkManager;
use pocketmine\world\format\Chunk;
use pocketmine\world\format\SubChunk;
use pocketmine\world\generator\object\OreType;
use pocketmine\world\generator\populator\Ore;
use pocketmine\world\generator\populator\Populator;
@ -141,20 +142,20 @@ class Flat extends Generator{
protected function generateBaseChunk() : void{
$this->chunk = new Chunk();
for($Z = 0; $Z < 16; ++$Z){
for($X = 0; $X < 16; ++$X){
for($Z = 0; $Z < Chunk::EDGE_LENGTH; ++$Z){
for($X = 0; $X < Chunk::EDGE_LENGTH; ++$X){
$this->chunk->setBiomeId($X, $Z, $this->biome);
}
}
$count = count($this->structure);
for($sy = 0; $sy < $count; $sy += 16){
$subchunk = $this->chunk->getSubChunk($sy >> 4);
for($y = 0; $y < 16 and isset($this->structure[$y | $sy]); ++$y){
for($sy = 0; $sy < $count; $sy += SubChunk::EDGE_LENGTH){
$subchunk = $this->chunk->getSubChunk($sy >> SubChunk::COORD_BIT_SIZE);
for($y = 0; $y < SubChunk::EDGE_LENGTH and isset($this->structure[$y | $sy]); ++$y){
$id = $this->structure[$y | $sy];
for($Z = 0; $Z < 16; ++$Z){
for($X = 0; $X < 16; ++$X){
for($Z = 0; $Z < SubChunk::EDGE_LENGTH; ++$Z){
for($X = 0; $X < SubChunk::EDGE_LENGTH; ++$X){
$subchunk->setFullBlock($X, $y, $Z, $id);
}
}

View File

@ -27,6 +27,7 @@ use pocketmine\block\VanillaBlocks;
use pocketmine\data\bedrock\BiomeIds;
use pocketmine\world\biome\BiomeRegistry;
use pocketmine\world\ChunkManager;
use pocketmine\world\format\Chunk;
use pocketmine\world\generator\Generator;
use pocketmine\world\generator\InvalidGeneratorOptionsException;
use pocketmine\world\generator\noise\Simplex;
@ -72,7 +73,7 @@ class Nether extends Generator{
public function generateChunk(ChunkManager $world, int $chunkX, int $chunkZ) : void{
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->seed);
$noise = $this->noiseBase->getFastNoise3D(16, 128, 16, 4, 8, 4, $chunkX * 16, 0, $chunkZ * 16);
$noise = $this->noiseBase->getFastNoise3D(Chunk::EDGE_LENGTH, 128, Chunk::EDGE_LENGTH, 4, 8, 4, $chunkX * Chunk::EDGE_LENGTH, 0, $chunkZ * Chunk::EDGE_LENGTH);
$chunk = $world->getChunk($chunkX, $chunkZ);
@ -80,8 +81,8 @@ class Nether extends Generator{
$netherrack = VanillaBlocks::NETHERRACK()->getFullId();
$stillLava = VanillaBlocks::LAVA()->getFullId();
for($x = 0; $x < 16; ++$x){
for($z = 0; $z < 16; ++$z){
for($x = 0; $x < Chunk::EDGE_LENGTH; ++$x){
for($z = 0; $z < Chunk::EDGE_LENGTH; ++$z){
$chunk->setBiomeId($x, $z, BiomeIds::HELL);
for($y = 0; $y < 128; ++$y){

View File

@ -28,6 +28,7 @@ use pocketmine\data\bedrock\BiomeIds;
use pocketmine\world\biome\Biome;
use pocketmine\world\biome\BiomeRegistry;
use pocketmine\world\ChunkManager;
use pocketmine\world\format\Chunk;
use pocketmine\world\generator\biome\BiomeSelector;
use pocketmine\world\generator\Gaussian;
use pocketmine\world\generator\Generator;
@ -144,7 +145,7 @@ class Normal extends Generator{
public function generateChunk(ChunkManager $world, int $chunkX, int $chunkZ) : void{
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->seed);
$noise = $this->noiseBase->getFastNoise3D(16, 128, 16, 4, 8, 4, $chunkX * 16, 0, $chunkZ * 16);
$noise = $this->noiseBase->getFastNoise3D(Chunk::EDGE_LENGTH, 128, Chunk::EDGE_LENGTH, 4, 8, 4, $chunkX * Chunk::EDGE_LENGTH, 0, $chunkZ * Chunk::EDGE_LENGTH);
$chunk = $world->getChunk($chunkX, $chunkZ);
@ -154,11 +155,11 @@ class Normal extends Generator{
$stillWater = VanillaBlocks::WATER()->getFullId();
$stone = VanillaBlocks::STONE()->getFullId();
$baseX = $chunkX * 16;
$baseZ = $chunkZ * 16;
for($x = 0; $x < 16; ++$x){
$baseX = $chunkX * Chunk::EDGE_LENGTH;
$baseZ = $chunkZ * Chunk::EDGE_LENGTH;
for($x = 0; $x < Chunk::EDGE_LENGTH; ++$x){
$absoluteX = $baseX + $x;
for($z = 0; $z < 16; ++$z){
for($z = 0; $z < Chunk::EDGE_LENGTH; ++$z){
$absoluteZ = $baseZ + $z;
$minSum = 0;
$maxSum = 0;

View File

@ -29,6 +29,7 @@ use pocketmine\block\Liquid;
use pocketmine\utils\Random;
use pocketmine\world\biome\BiomeRegistry;
use pocketmine\world\ChunkManager;
use pocketmine\world\format\Chunk;
use function count;
use function min;
@ -38,8 +39,8 @@ class GroundCover implements Populator{
$chunk = $world->getChunk($chunkX, $chunkZ);
$factory = BlockFactory::getInstance();
$biomeRegistry = BiomeRegistry::getInstance();
for($x = 0; $x < 16; ++$x){
for($z = 0; $z < 16; ++$z){
for($x = 0; $x < Chunk::EDGE_LENGTH; ++$x){
for($z = 0; $z < Chunk::EDGE_LENGTH; ++$z){
$biome = $biomeRegistry->getBiome($chunk->getBiomeId($x, $z));
$cover = $biome->getGroundCover();
if(count($cover) > 0){

View File

@ -25,6 +25,7 @@ namespace pocketmine\world\generator\populator;
use pocketmine\utils\Random;
use pocketmine\world\ChunkManager;
use pocketmine\world\format\Chunk;
use pocketmine\world\generator\object\Ore as ObjectOre;
use pocketmine\world\generator\object\OreType;
@ -36,9 +37,9 @@ class Ore implements Populator{
foreach($this->oreTypes as $type){
$ore = new ObjectOre($random, $type);
for($i = 0; $i < $ore->type->clusterCount; ++$i){
$x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15);
$x = $random->nextRange($chunkX << Chunk::COORD_BIT_SIZE, ($chunkX << Chunk::COORD_BIT_SIZE) + Chunk::EDGE_LENGTH - 1);
$y = $random->nextRange($ore->type->minHeight, $ore->type->maxHeight);
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15);
$z = $random->nextRange($chunkZ << Chunk::COORD_BIT_SIZE, ($chunkZ << Chunk::COORD_BIT_SIZE) + Chunk::EDGE_LENGTH - 1);
if($ore->canPlaceObject($world, $x, $y, $z)){
$ore->placeObject($world, $x, $y, $z);
}

View File

@ -27,6 +27,7 @@ use pocketmine\block\BlockLegacyIds;
use pocketmine\block\VanillaBlocks;
use pocketmine\utils\Random;
use pocketmine\world\ChunkManager;
use pocketmine\world\format\Chunk;
class TallGrass implements Populator{
/** @var int */
@ -47,8 +48,8 @@ class TallGrass implements Populator{
$block = VanillaBlocks::TALL_GRASS();
for($i = 0; $i < $amount; ++$i){
$x = $random->nextRange($chunkX * 16, $chunkX * 16 + 15);
$z = $random->nextRange($chunkZ * 16, $chunkZ * 16 + 15);
$x = $random->nextRange($chunkX * Chunk::EDGE_LENGTH, $chunkX * Chunk::EDGE_LENGTH + (Chunk::EDGE_LENGTH - 1));
$z = $random->nextRange($chunkZ * Chunk::EDGE_LENGTH, $chunkZ * Chunk::EDGE_LENGTH + (Chunk::EDGE_LENGTH - 1));
$y = $this->getHighestWorkableBlock($world, $x, $z);
if($y !== -1 and $this->canTallGrassStay($world, $x, $y, $z)){

View File

@ -27,6 +27,7 @@ use pocketmine\block\BlockLegacyIds;
use pocketmine\block\utils\TreeType;
use pocketmine\utils\Random;
use pocketmine\world\ChunkManager;
use pocketmine\world\format\Chunk;
use pocketmine\world\generator\object\TreeFactory;
class Tree implements Populator{
@ -56,8 +57,8 @@ class Tree implements Populator{
public function populate(ChunkManager $world, int $chunkX, int $chunkZ, Random $random) : void{
$amount = $random->nextRange(0, $this->randomAmount) + $this->baseAmount;
for($i = 0; $i < $amount; ++$i){
$x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15);
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15);
$x = $random->nextRange($chunkX << Chunk::COORD_BIT_SIZE, ($chunkX << Chunk::COORD_BIT_SIZE) + Chunk::EDGE_LENGTH);
$z = $random->nextRange($chunkZ << Chunk::COORD_BIT_SIZE, ($chunkZ << Chunk::COORD_BIT_SIZE) + Chunk::EDGE_LENGTH);
$y = $this->getHighestWorkableBlock($world, $x, $z);
if($y === -1){
continue;