From bb045abb09a4df61cf7118ac19c63e45382b7b5a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 28 Nov 2023 06:58:04 +0300 Subject: [PATCH] better generator --- src/constants/BlockIDs.php | 1 + src/dependencies.php | 9 ++++ src/world/generator/NormalGenerator.php | 36 +++++++++++---- src/world/generator/biome/BiomeSelector.php | 1 - .../biome/NormalGeneratorBiomeSelector.php | 24 ++++++++++ .../populator/BiomeBasedTreePopulator.php | 46 +++++++++++++++++++ .../populator/FlowerPatchPopulator.php | 45 ++++++++++++++++++ .../generator/populator/TreePopulator.php | 8 ++-- 8 files changed, 157 insertions(+), 13 deletions(-) create mode 100644 src/world/generator/biome/NormalGeneratorBiomeSelector.php create mode 100644 src/world/generator/populator/BiomeBasedTreePopulator.php create mode 100644 src/world/generator/populator/FlowerPatchPopulator.php diff --git a/src/constants/BlockIDs.php b/src/constants/BlockIDs.php index 8b8c4797..79e694cd 100755 --- a/src/constants/BlockIDs.php +++ b/src/constants/BlockIDs.php @@ -86,6 +86,7 @@ define("BLACK_WOOL", "35:15"); define("DANDELION", 37); define("YELLOW_FLOWER", 37); define("ROSE", 38); +define("MULTIFLOWER", 38); define("CYAN_FLOWER", 38); //NO define("RED_FLOWER", 38); define("BLUE_ORCHID", "38:1"); diff --git a/src/dependencies.php b/src/dependencies.php index 5258e999..f881fdf5 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -123,6 +123,8 @@ require_once(FILE_PATH . "/src/material/item/base/ItemSword.php"); require_once(FILE_PATH . "/src/material/item/armor/ArmorItem.php"); require_once(FILE_PATH . "/src/structure/Structure.php"); +require_once(FILE_PATH . "/src/world/generator/Populator.php"); +require_once(FILE_PATH . "/src/world/generator/LevelGenerator.php"); require_once(FILE_PATH . "/src/plugin/Plugin.php"); require_once(FILE_PATH . "/src/plugin/OtherPluginRequirement.php"); @@ -134,11 +136,18 @@ require_once(FILE_PATH . "/src/world/generator/LevelGenerator.php"); require_once(FILE_PATH . "/src/world/generator/NewLevelGenerator.php"); require_once(FILE_PATH . "/src/world/generator/biome/Biome.php"); require_once(FILE_PATH . "/src/world/generator/biome/BiomeSelector.php"); +require_once(FILE_PATH . "/src/world/generator/biome/NormalGeneratorBiomeSelector.php"); require_once(FILE_PATH . "/src/world/generator/biome/biomes/BiomeWithGrass.php"); require_once(FILE_PATH . "/src/world/generator/biome/biomes/BiomeWithSand.php"); require_once(FILE_PATH . "/src/world/generator/biome/biomes/BiomeWithSnow.php"); require_once(FILE_PATH . "/src/world/generator/biome/biomes/BiomeExtremeHills.php"); + +require_once(FILE_PATH . "/src/world/generator/populator/TreePopulator.php"); + + + + require_all(FILE_PATH . "src/"); diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index 510a6963..76bb1d9f 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -14,7 +14,7 @@ class NormalGenerator implements NewLevelGenerator{ private $noisePatches; private $noisePatchesSmall; private $noiseBase; - + private $biomeSelector; public function __construct(array $options = array()){ } @@ -31,6 +31,7 @@ class NormalGenerator implements NewLevelGenerator{ $this->noisePatches = new NoiseGeneratorPerlin($this->random, 2); $this->noisePatchesSmall = new NoiseGeneratorPerlin($this->random, 2); $this->noiseBase = new NoiseGeneratorPerlin($this->random, 16); + $this->biomeSelector = new NormalGeneratorBiomeSelector($this->random, BiomeSelector::$biomes[BIOME_PLAINS]); $ores = new OrePopulator(); $ores->setOreTypes(array( @@ -50,23 +51,42 @@ class NormalGenerator implements NewLevelGenerator{ )); $this->populators[] = $ores; - $trees = new TreePopulator(); - $trees->setBaseAmount(3); - $trees->setRandomAmount(0); + $trees = new BiomeBasedTreePopulator(); + $trees->setBaseAmount(8); + $trees->setRandomAmount(2); $this->populators[] = $trees; + $this->populators[] = new FlowerPatchPopulator(); + $tallGrass = new TallGrassPopulator(); $tallGrass->setBaseAmount(5); $tallGrass->setRandomAmount(0); $this->populators[] = $tallGrass; + + } + + public function pickBiome(int $x, int $z){ + $hash = $x * 2345803 ^ $z * 9236449 ^ $this->level->level->getSeed(); + $hash *= $hash + 223; + $xNoise = ((int)$hash) >> 20 & 3; //why dont u have types for local variables?? + $zNoise = ((int)$hash) >> 22 & 3; + if($xNoise == 3){ + $xNoise = 1; + } + if($zNoise == 3){ + $zNoise = 1; + } + return $this->biomeSelector->pickBiome($x + $xNoise - 1, $z + $zNoise - 1); } public function generateChunk($chunkX, $chunkZ){ $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); $hills = array(); $base = array(); + $biomes = str_repeat(chr(BIOME_PLAINS), 256); for($z = 0; $z < 16; ++$z){ for($x = 0; $x < 16; ++$x){ + $biomes[($z << 4) + $x] = chr($this->pickBiome($chunkX * 16 + $x, $chunkZ * 16 + $z)->id); $i = ($z << 4) + $x; $hills[$i] = $this->noiseHills->noise3D($x + ($chunkX << 4), 0, $z + ($chunkZ << 4), 0.11, 12, true); $patches[$i] = $this->noisePatches->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.03, 16, true); @@ -86,7 +106,7 @@ class NormalGenerator implements NewLevelGenerator{ $i = ($z << 4) + $x; $height = $this->worldHeight + $hills[$i] * 14 + $base[$i] * 7; $height = (int) $height; - + $biomeID = ord($biomes[$i]); for($y = $startY; $y < $endY; ++$y){ $diff = $height - $y; if($y <= 4 and ($y === 0 or $this->random->nextFloat() < 0.33)){ @@ -99,7 +119,7 @@ class NormalGenerator implements NewLevelGenerator{ }elseif($patches[$i] < -0.8){ $chunk .= "\x0d"; //gravel }else{ - $chunk .= "\x03"; //dirt + $chunk .= $biomeID === BIOME_DESERT ? chr(SANDSTONE) : chr(DIRT); } }elseif($y <= $this->waterHeight){ if(($this->waterHeight - $y) <= 1 and $diff === 0){ @@ -121,7 +141,7 @@ class NormalGenerator implements NewLevelGenerator{ }elseif($patches[$i] < -0.8){ $chunk .= "\x0d"; //gravel }else{ - $chunk .= "\x02"; //grass + $chunk .= $biomeID === BIOME_DESERT ? chr(SAND) : chr(GRASS); //grass } }else{ $chunk .= "\x00"; @@ -132,7 +152,7 @@ class NormalGenerator implements NewLevelGenerator{ $chunk .= "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; //skylight/light } } - $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, str_repeat(chr(BIOME_PLAINS), 256)); + $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, $biomes); $this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk); } } diff --git a/src/world/generator/biome/BiomeSelector.php b/src/world/generator/biome/BiomeSelector.php index 0de1f1a2..072e52b1 100644 --- a/src/world/generator/biome/BiomeSelector.php +++ b/src/world/generator/biome/BiomeSelector.php @@ -91,7 +91,6 @@ class BiomeSelector public function pickBiome($x, $z){ $temperature = (int) ($this->getTemperature($x, $z) * 63); $rainfall = (int) ($this->getRainfall($x, $z) * 63); - $biomeId = $this->map[$temperature + ($rainfall << 6)] ?? -1; return self::$biomes[$biomeId] ?? $this->fallback; } diff --git a/src/world/generator/biome/NormalGeneratorBiomeSelector.php b/src/world/generator/biome/NormalGeneratorBiomeSelector.php new file mode 100644 index 00000000..9a6ed368 --- /dev/null +++ b/src/world/generator/biome/NormalGeneratorBiomeSelector.php @@ -0,0 +1,24 @@ +level = $level; + $amount = $random->nextRange(0, $this->randomAmount + 1) + $this->baseAmount; + for($i = 0; $i < $amount; ++$i){ + $x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15); + $z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15); + $biomeID = $level->level->getBiomeId($x, $z); + switch($biomeID){ + case BIOME_FOREST: + $f = $random->nextFloat(); + if($f > 0.75){ + $meta = SaplingBlock::BIRCH; + }else{ + $meta = SaplingBlock::OAK; + } + break; + case BIOME_JUNGLE: + cosnole("placing jungle wood on $x $z"); + $meta = SaplingBlock::JUNGLE; + break; + case BIOME_TAIGA: + $meta = SaplingBlock::SPRUCE; + break; + case BIOME_PLAINS: + $f = $random->nextFloat(); + if($f > 0.9){ + $meta = SaplingBlock::OAK; + break; + } + return; + default: + return; + } + $y = $this->getHighestWorkableBlock($x, $z); + if($y === -1){ + continue; + } + TreeObject::growTree($this->level, new Vector3($x, $y, $z), $random, $meta); + } + } +} + diff --git a/src/world/generator/populator/FlowerPatchPopulator.php b/src/world/generator/populator/FlowerPatchPopulator.php new file mode 100644 index 00000000..a844d988 --- /dev/null +++ b/src/world/generator/populator/FlowerPatchPopulator.php @@ -0,0 +1,45 @@ +nextRange(0, 15); + $z = $chunkZ*16 + $random->nextRange(0, 15); + + for($i = 0; $i < 4; ++$i){ + $xPos = ($x + $random->nextRange(0, 7)) - $random->nextRange(0, 7); + $zPos = ($z + $random->nextRange(0, 7)) - $random->nextRange(0, 7); + $yPos = $this->getHighestWorkableBlock($level, $xPos, $zPos); + if($level->level->getBlockID($xPos, $yPos, $zPos) == 0){ + $flowerIDMeta = self::$flowerIDS[$random->nextRange(0, count(self::$flowerIDS)-1)]; + $level->level->setBlock($xPos, $yPos, $zPos, $flowerIDMeta[0], $flowerIDMeta[1]); + } + } + } + + private function getHighestWorkableBlock(Level $level, $x, $z){ + for($y = 128; $y > 0; --$y){ + $b = $level->level->getBlockID($x, $y, $z); + if($b == GRASS || $b == DIRT){ + return $y + 1; + } + } + return -1; + } +} + diff --git a/src/world/generator/populator/TreePopulator.php b/src/world/generator/populator/TreePopulator.php index 5ab636bf..50ade1d4 100644 --- a/src/world/generator/populator/TreePopulator.php +++ b/src/world/generator/populator/TreePopulator.php @@ -1,8 +1,8 @@ randomAmount = $amount; @@ -33,7 +33,7 @@ class TreePopulator extends Populator{ } } - private function getHighestWorkableBlock($x, $z){ + public function getHighestWorkableBlock($x, $z){ for($y = 128; $y > 0; --$y){ $b = $this->level->level->getBlockID($x, $y, $z); if($b !== DIRT and $b !== GRASS){