From eaa53908795f4a79daa8d3e253e564eca01cdefd Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 7 Mar 2023 14:35:31 +0300 Subject: [PATCH] biomez --- src/API/ServerAPI.php | 2 +- src/constants/BiomeIDs.php | 86 +++++++++---------- src/world/Level.php | 1 - src/world/generator/ExperimentalGenerator.php | 44 +++++++--- src/world/generator/biome/Biome.php | 45 +++++++--- src/world/generator/biome/BiomeSelector.php | 39 ++++++--- 6 files changed, 134 insertions(+), 83 deletions(-) diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index e71ed9cb..91180f91 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -134,7 +134,7 @@ class ServerAPI{ "rcon.password" => substr(base64_encode(Utils::getRandomBytes(20, false)), 3, 10), "auto-save" => true, ]); - + Biome::init(); $this->parseProperties(); MobSpawner::$MOB_LIMIT = $this->getProperty("mobs-amount", 50); //Load advanced properties diff --git a/src/constants/BiomeIDs.php b/src/constants/BiomeIDs.php index bf8213f3..22a8f2b0 100644 --- a/src/constants/BiomeIDs.php +++ b/src/constants/BiomeIDs.php @@ -1,50 +1,50 @@ 0.0, 0.5 -define("MUSHROOM_ISLAND", 14); -define("MUSHROOM_ISLAND_SHORE", 15); -define("BEACH", 16); -define("DESERT_HILLS", 17); -define("FOREST_HILLS", 18); -define("TAIGA_HILLS", 19); -define("EXTREME_HILLS_EDGE", 20); -define("JUNGLE", 21); -define("JUNGLE_HILLS", 22); -define("JUNGLE_EDGE", 23); -define("DEEP_OCEAN", 24); -define("STONE_BEACH", 25); -define("COLD_BEACH", 26); -define("BIRCH_FOREST", 27); -define("BIRCH_FOREST_HILLS", 28); -define("ROOFED_FOREST", 29); -define("COLD_TAIGA", 30); -define("COLD_TAIGA_HILLS", 31); -define("MEGA_TAIGA", 32); -define("MEGA_TAIGA_HILLS", 33); -define("EXTREME_HILLS_PLUS", 34); -define("SAVANNA", 35); -define("SAVANNA_PLATEAU", 36); -define("MESA", 37); -define("MESA_PLATEAU_F", 38); -define("MESA_PLATEAU", 39); -//define("SUNFLOWER_PLAINS", 129); metioning not found +define("BIOME_FROZEN_OCEAN", 10); +define("BIOME_FROZEN_RIVER", 11); +define("BIOME_ICE_PLAINS", 12); +define("BIOME_ICE_MOUNTAINS", 13); //snow covered, temp&fall->0.0, 0.5 +define("BIOME_MUSHROOM_ISLAND", 14); +define("BIOME_MUSHROOM_ISLAND_SHORE", 15); +define("BIOME_BEACH", 16); +define("BIOME_DESERT_HILLS", 17); +define("BIOME_FOREST_HILLS", 18); +define("BIOME_TAIGA_HILLS", 19); +define("BIOME_EXTREME_HILLS_EDGE", 20); +define("BIOME_JUNGLE", 21); +define("BIOME_JUNGLE_HILLS", 22); +define("BIOME_JUNGLE_EDGE", 23); +define("BIOME_DEEP_OCEAN", 24); +define("BIOME_STONE_BEACH", 25); +define("BIOME_COLD_BEACH", 26); +define("BIOME_BIRCH_FOREST", 27); +define("BIOME_BIRCH_FOREST_HILLS", 28); +define("BIOME_ROOFED_FOREST", 29); +define("BIOME_COLD_TAIGA", 30); +define("BIOME_COLD_TAIGA_HILLS", 31); +define("BIOME_MEGA_TAIGA", 32); +define("BIOME_MEGA_TAIGA_HILLS", 33); +define("BIOME_EXTREME_HILLS_PLUS", 34); +define("BIOME_SAVANNA", 35); +define("BIOME_SAVANNA_PLATEAU", 36); +define("BIOME_MESA", 37); +define("BIOME_MESA_PLATEAU_F", 38); +define("BIOME_MESA_PLATEAU", 39); +//define("BIOME_SUNFLOWER_PLAINS", 129); metioning not found -//define("ICE_PLAINS_SPIKES", 140); metioning not found +//define("BIOME_ICE_PLAINS_SPIKES", 140); metioning not found -define("REDWOOD_TAIGA_MUTATED", 160); //mentioned, but cannot check id +define("BIOME_REDWOOD_TAIGA_MUTATED", 160); //mentioned, but cannot check id -define("MAX_BIOMES", 256); \ No newline at end of file +define("BIOME_MAX_BIOMES", 256); diff --git a/src/world/Level.php b/src/world/Level.php index ee80d043..959812e5 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -138,7 +138,6 @@ class Level{ } } $chunkTiles = []; - console(strlen($orderedLight)); $tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '".$this->getName()."' AND x >= ".($X * 16 - 1)." AND x < ".($X * 16 + 17)." AND z >= ".($Z * 16 - 1)." AND z < ".($Z * 16 + 17).";"); if($tiles !== false and $tiles !== true){ while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ diff --git a/src/world/generator/ExperimentalGenerator.php b/src/world/generator/ExperimentalGenerator.php index 2e389752..2b055bec 100644 --- a/src/world/generator/ExperimentalGenerator.php +++ b/src/world/generator/ExperimentalGenerator.php @@ -55,7 +55,7 @@ class ExperimentalGenerator implements NewLevelGenerator{ $this->noisePatches = new NoiseGeneratorSimplex($this->random, 2); $this->noisePatchesSmall = new NoiseGeneratorSimplex($this->random, 2); $this->noiseBase = new NoiseGeneratorPerlin($this->random, 4); - + $this->selector = new BiomeSelector($this->random, BiomeSelector::$biomes[BIOME_PLAINS]); $ores = new OrePopulator(); $ores->setOreTypes(array( new OreType(new CoalOreBlock(), 20, 16, 0, 128), @@ -85,10 +85,25 @@ class ExperimentalGenerator implements NewLevelGenerator{ //$this->populators[] = $tallGrass; } + public function pickBiome($x, $z){ + $hash = $x * 2345803 ^ $z * 9236449 ^ $this->level->getSeed(); + $hash *= $hash + 223; + $xNoise = $hash >> 20 & 3; + $zNoise = $hash >> 22 & 3; + if($xNoise == 3){ + $xNoise = 1; + } + if($zNoise == 3){ + $zNoise = 1; + } + return $this->selector->pickBiome($x + $xNoise - 1, $z + $zNoise - 1); + } + public function generateChunk($chunkX, $chunkZ){ $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); $noiseArray = ExperimentalGenerator::getFastNoise3D($this->noiseBase, 16, 128, 16, 4, 8, 4, $chunkX * 16, 0, $chunkZ * 16); + $biomeCache = []; for($chunkY = 0; $chunkY < 8; ++$chunkY){ $chunk = ""; @@ -99,23 +114,26 @@ class ExperimentalGenerator implements NewLevelGenerator{ $minSum = 0; $maxSum = 0; $weightSum = 0; + + $biome = $this->pickBiome($chunkX * 16 + $x, $chunkZ * 16 + $z); + 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]; - /*if($sx === 0 and $sz === 0){ - $adjacent = $biome; - }else{ - $index = Level::chunkHash($chunkX * 16 + $x + $sx, $chunkZ * 16 + $z + $sz); - if(isset($biomeCache[$index])){ - $adjacent = $biomeCache[$index]; - }else{ - $biomeCache[$index] = $adjacent = $this->pickBiome($chunkX * 16 + $x + $sx, $chunkZ * 16 + $z + $sz); - } - }*/ + if($sx === 0 and $sz === 0){ + $adjacent = $biome; + }else{ + $index = ($chunkX * 16 + $x + $sx).":".($chunkZ * 16 + $z + $sz); + if(isset($biomeCache[$index])){ + $adjacent = $biomeCache[$index]; + }else{ + $biomeCache[$index] = $adjacent = $this->pickBiome($chunkX * 16 + $x + $sx, $chunkZ * 16 + $z + $sz); + } + } - $minSum += (63 - 1) * $weight; //TODO biomes - $maxSum += 127 * $weight; + $minSum += ($adjacent->getMin() - 1) * $weight; //TODO biomes + $maxSum += $adjacent->getMax() * $weight; $weightSum += $weight; } diff --git a/src/world/generator/biome/Biome.php b/src/world/generator/biome/Biome.php index e2dbda31..601d79f7 100644 --- a/src/world/generator/biome/Biome.php +++ b/src/world/generator/biome/Biome.php @@ -1,32 +1,51 @@ name = $name; - self::$biomes[$id] = $this; + $this->id = $id; } public function setTopBlock($id){ $this->topBlock = $id; - return $this; + } + + public function getID(){ + return $this->id; + } + + public function setMinMax($min, $max){ + $this->minY = $min; + $this->maxY = $max; + } + + public function getMin(){ + return $this->minY; + } + + public function getMax(){ + return $this->maxY; } public function setFillerBlock($id){ $this->fillerBlock = $id; - return $this; } public function getBiomeAt($x, $z){ self::$biomeLookup[$x + $z * 64]; } - - public static function init(){ - //register biomes - } - - public static $temperature, $rainfall; - private static $biomeLookup = []; - public static $biomes = []; -} \ No newline at end of file + public function __toString(){ + return $this->name; + } + + public static function init(){ + BiomeSelector::registerBiome(new BiomeExtremeHills(BIOME_EXTREME_HILLS, "Extreme Hills")); + BiomeSelector::registerBiome(new BiomePlains(BIOME_PLAINS, "Plains")); + } + + private static $initialized = false; + public static $temperature, $rainfall; + public static $biomes = []; +} diff --git a/src/world/generator/biome/BiomeSelector.php b/src/world/generator/biome/BiomeSelector.php index b324366b..630cdd2e 100644 --- a/src/world/generator/biome/BiomeSelector.php +++ b/src/world/generator/biome/BiomeSelector.php @@ -11,41 +11,56 @@ class BiomeSelector private $rainfall; /** @var Biome[] */ - private $biomes = []; + public static $biomes = []; - private $map; + private $map = []; private $lookup; - public function __construct(Random $random, callable $lookup, Biome $fallback){ + public function __construct(Random $random, Biome $fallback){ $this->fallback = $fallback; - $this->lookup = $lookup; $this->temperature = new NoiseGeneratorSimplex($random, 2); $this->rainfall = new NoiseGeneratorSimplex($random, 2); + $this->generateBiomeLookup(); + } + + public static function registerBiome(Biome $b){ + ConsoleAPI::debug("Registered $b"); + self::$biomes[$b->getID()] = $b; } public function generateBiomeLookup(){ - $this->map = new SplFixedArray(64 * 64); for($t = 0; $t < 64; ++$t){ for($r = 0; $r < 64; ++$r){ - $this->map[$t + $r << 6] = call_user_func($this->lookup, $t / 63, $r / 63); + $this->map[$t + ($r << 6)] = $this->getBiomeTR($t / 63, $r / 63); } } } + + public function getBiomeTR($t, $r){ + if($r < 0.60){ + return BIOME_PLAINS; + }else{ + if($t < 0.70){ + return BIOME_EXTREME_HILLS; + } + } + } + public function getTemperature($x, $z){ //TODO cache - return ($this->temperature->noise2D($x, $z, 1 / 16, 1 / 512, true) + 1) / 2; + return ($this->temperature->noise2D($x * (1/512), $z * (1/512), 2, 1 / 16, true) + 1) / 2; } public function getRainfall($x, $z){ - return ($this->rainfall->noise2D($x, $z, 1 / 16, 1 / 512, true) + 1) / 2; + return ($this->rainfall->noise2D($x * (1/512), $z * (1/512), 2, 1 / 16, true) + 1) / 2; } - public function getBiome($x, $z){ + 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)]; - return isset($this->biomes[$biomeId]) ? $this->biomes[$biomeId] : $this->fallback; + + $biomeId = nullsafe($this->map[$temperature + ($rainfall << 6)], -1); + return nullsafe(self::$biomes[$biomeId], $this->fallback); } }