better generator

This commit is contained in:
GameHerobrine 2023-11-28 06:58:04 +03:00
parent 3a1f5a28b1
commit bb045abb09
8 changed files with 157 additions and 13 deletions

View file

@ -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");

View file

@ -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/");

View file

@ -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);
}
}

View file

@ -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;
}

View file

@ -0,0 +1,24 @@
<?php
class NormalGeneratorBiomeSelector extends BiomeSelector
{
public function getBiomeTR($t, $r){
if($r < 0.60){
if($t < 0.75){
return BIOME_PLAINS;
}else{
return BIOME_DESERT;
}
}else if($r < 0.80){
if($t < 0.25){
return BIOME_TAIGA;
}else if($t < 0.75){
return BIOME_FOREST;
}else{
return BIOME_JUNGLE;
}
}
return BIOME_PLAINS;
}
}

View file

@ -0,0 +1,46 @@
<?php
class BiomeBasedTreePopulator extends \TreePopulator
{
public function populate(Level $level, $chunkX, $chunkZ, Random $random){
$this->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);
}
}
}

View file

@ -0,0 +1,45 @@
<?php
class FlowerPatchPopulator extends Populator
{
public static $flowerIDS = [
[DANDELION, 0],
[MULTIFLOWER, 0],
[MULTIFLOWER, 1],
[MULTIFLOWER, 2],
[MULTIFLOWER, 3],
[MULTIFLOWER, 4],
[MULTIFLOWER, 5],
[MULTIFLOWER, 6],
[MULTIFLOWER, 7],
[MULTIFLOWER, 8],
];
public function populate(Level $level, $chunkX, $chunkZ, Random $random)
{
$x = $chunkX*16 + $random->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;
}
}

View file

@ -1,8 +1,8 @@
<?php
class TreePopulator extends Populator{
private $level;
private $randomAmount;
private $baseAmount;
public $level;
public $randomAmount;
public $baseAmount;
public function setRandomAmount($amount){
$this->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){