biome decorators, jungle biome

This commit is contained in:
GameHerobrine 2023-11-29 03:56:04 +03:00
parent f405ad8b17
commit f274b36985
15 changed files with 147 additions and 80 deletions

View file

@ -134,6 +134,9 @@ require_once(FILE_PATH . "/src/plugin/phar/PharUtils.php");
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/BiomeDecorator.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");

View file

@ -600,7 +600,18 @@ class PMFLevel extends PMF{
$Z = $z >> 4;
$Y = $y >> 4;
$block &= 0xFF;
$index = $this->getIndex($X, $Z);
$index = $this->getIndex($X, $Z);
if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){
if($this->loadChunk($X, $Z, false) === false){
$this->createUnpopulatedChunk($X, $Z);
}
}
if($this->chunks[$index][$Y] === false){
$this->fillMiniChunk($X, $Z, $Y);
}
$aX = $x & 0xf;
$aZ = $z & 0xf;
$aY = $y & 0xf;
@ -726,13 +737,12 @@ class PMFLevel extends PMF{
if($Y >= 128 or $y < 0){
return false;
}
$loaded = false;
$index = $this->getIndex($X, $Z);
if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){
if($this->loadChunk($X, $Z, false) === false){
$this->createUnpopulatedChunk($X, $Z);
}
$loaded = true;
}
if($this->chunks[$index][$Y] === false){
$this->fillMiniChunk($X, $Z, $Y);

View file

@ -50,10 +50,9 @@ class NormalGenerator implements NewLevelGenerator{
new OreType(new StoneBlock(5), 12, 16, 0, 128),
));
$this->populators[] = $ores;
$this->populators[] = new DesertPopulator(); //TODO biome populators
$trees = new BiomeBasedTreePopulator();
$trees->setBaseAmount(8);
$trees->setRandomAmount(2);
$trees->setBaseAmount(8); //TODO biome based amount?
$trees->setRandomAmount(4);
$this->populators[] = $trees;
$this->populators[] = new FlowerPatchPopulator();
@ -157,6 +156,16 @@ class NormalGenerator implements NewLevelGenerator{
public function populateChunk($chunkX, $chunkZ){
$this->level->level->setPopulated($chunkX, $chunkZ, true);
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
$biomeID = $this->level->level->getBiomeId($chunkX*16, $chunkZ*16);
$biome = BiomeSelector::get($biomeID);
if($biome === false){
ConsoleAPI::warn("Failed to get Biome with id {$biomeID} at $chunkX, $chunkZ");
}else{
$biome->decorator->decorate($this->level, $chunkX, $chunkZ, $this->random);
}
foreach($this->populators as $populator){
$populator->populate($this->level, $chunkX, $chunkZ, $this->random);
}

View file

@ -2,15 +2,27 @@
abstract class Biome{
public $id, $topBlocks, $fillerBlock, $name, $minY, $maxY;
public $decorator = false;
public function __construct($id, $name){
$this->name = $name;
$this->id = $id;
$this->decorator = $this->createBiomeDecorator();
}
public function setTopBlocks($id){
$this->topBlocks = $id;
}
public function createBiomeDecorator(){
return new BiomeDecorator();
}
public function getTree(Random $random){
return null;
}
public function getID(){
return $this->id;
}
@ -57,6 +69,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"));
BiomeSelector::registerBiome(new BiomeJungle(BIOME_JUNGLE, "Jungle"));
}

View file

@ -0,0 +1,9 @@
<?php
class BiomeDecorator
{
public function decorate(Level $level, $chunkX, $chunkZ, Random $random){
return new SmallTreeObject(SaplingBlock::JUNGLE);
}
}

View file

@ -6,5 +6,9 @@ class BiomeDesert extends BiomeWithSand
parent::__construct($id, $name);
$this->setMinMax(63, 74);
}
public function createBiomeDecorator(){
return new DesertBiomeDecorator();
}
}

View file

@ -7,5 +7,13 @@ class BiomeForest extends BiomeWithGrass
$this->setMinMax(63, 81);
}
public function getTree(Random $random){
$f = $random->nextFloat();
if($f > 0.75){
return new SmallTreeObject(SaplingBlock::BIRCH);
}
return new SmallTreeObject(SaplingBlock::OAK);
}
}

View file

@ -0,0 +1,14 @@
<?php
class BiomeJungle extends BiomeWithGrass
{
public function __construct($id, $name){
parent::__construct($id, $name);
$this->setMinMax(63, 81);
}
public function getTree(Random $random){
return new SmallTreeObject(SaplingBlock::JUNGLE);
}
}

View file

@ -5,4 +5,8 @@ class BiomePlains extends BiomeWithGrass{
parent::__construct($id, $name);
$this->setMinMax(61, 68);
}
public function getTree(Random $random){
return $random->nextInt(10) == 0 ? new SmallTreeObject(SaplingBlock::OAK) : null;
}
}

View file

@ -13,5 +13,13 @@ class BiomeTaiga extends BiomeWithSnow
[DIRT, 0],
]);
}
public function getTree(Random $random){
$n = $random->nextInt(3);
if($n == 0){
return new PineTreeObject();
}
return new SpruceTreeObject();
}
}

View file

@ -0,0 +1,34 @@
<?php
class DesertBiomeDecorator extends BiomeDecorator
{
public function decorate(Level $level, $chunkX, $chunkZ, Random $random){
for($i = 0; $i < 2; ++$i){
$x = $chunkX*16 + $random->nextInt(16);
$z = $chunkZ*16 + $random->nextInt(16);
$y = $this->getHighestWorkableBlock($level, $x, $z);
$level->level->setBlockID($x, $y, $z, DEAD_BUSH);
}
for($i = 0; $i < 2; ++$i){
$x = $chunkX*16 + $random->nextInt(16);
$z = $chunkZ*16 + $random->nextInt(16);
$y = $this->getHighestWorkableBlock($level, $x, $z);
$cactiSize = $random->nextRange(0, 3);
for($yOff = 0; $yOff < $cactiSize; ++$yOff){
$level->level->setBlockID($x, $y + $yOff, $z, CACTUS);
}
}
}
private function getHighestWorkableBlock(Level $level, $x, $z){
for($y = 128; $y > 0; --$y){
$b = $level->level->getBlockID($x, $y, $z);
if($b == SAND && $level->level->getBlockID($x, $y + 1, $z) == 0){
return $y + 1;
}
}
return -1;
}
}

View file

@ -8,13 +8,16 @@ class SmallTreeObject extends TreeObject{
private static $leavesHeight = 4;
private static $leafRadii = [1, 1.41, 2.83, 2.24];
public $type = 0; // All trees appear to be 4 tall
public $type = 0;
public $treeHeight = 7;
private $trunkHeight = 5;
private $addLeavesVines = false;
private $addLogVines = false;
private $addCocoaPlants = false;
public function __construct($treeType = 0){
parent::__construct();
$this->type = $treeType;
}
public function canPlaceObject(Level $level, Vector3 $pos, Random $random){
$radiusToCheck = 0;
for($yy = 0; $yy < $this->trunkHeight + 3; ++$yy){

View file

@ -1,7 +1,7 @@
<?php
class TreeObject{
public function __construct(){}
public $overridable = [
0 => true,
6 => true,
@ -21,15 +21,12 @@ class TreeObject{
}
break;
case SaplingBlock::BIRCH:
$tree = new SmallTreeObject();
$tree->type = SaplingBlock::BIRCH;
$tree = new SmallTreeObject(SaplingBlock::BIRCH);
break;
case SaplingBlock::JUNGLE:
$tree = new SmallTreeObject();
$tree->type = SaplingBlock::JUNGLE;
$tree = new SmallTreeObject(SaplingBlock::JUNGLE);
break;
case SaplingBlock::ACACIA:
console("acacia");
$tree = new AcaciaTreeObject();
break;
case SaplingBlock::DARK_OAK:

View file

@ -9,36 +9,27 @@ class BiomeBasedTreePopulator extends \TreePopulator
$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:
$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;
$biome = BiomeSelector::get($biomeID);
$treeFeature = null;
if($biome instanceof Biome){
if($biome->id === BIOME_JUNGLE){
console("JUNGLE AT $chunkX $chunkZ");
}
$treeFeature = $biome->getTree($random);
}
$y = $this->getHighestWorkableBlock($x, $z);
if($y === -1){
if($treeFeature instanceof TreeObject){
$y = $this->getHighestWorkableBlock($x, $z);
if($y === -1){
continue;
}
$v3 = new Vector3($x, $y, $z); //TODO no v3
if($treeFeature->canPlaceObject($level, $v3, $random)){
$treeFeature->placeObject($level, $v3, $random);
}
}else{
continue;
}
TreeObject::growTree($this->level, new Vector3($x, $y, $z), $random, $meta);
}
}
}

View file

@ -1,40 +0,0 @@
<?php
class DesertPopulator extends Populator //TODO Biome Decorators ?
{
public function populate(Level $level, $chunkX, $chunkZ, Random $random)
{
for($i = 0; $i < 4; ++$i){
$x = $chunkX*16 + $random->nextRange(0, 15);
$z = $chunkZ*16 + $random->nextRange(0, 15);
if($level->level->getBiomeId($x, $z) === BIOME_DESERT){
$y = $this->getHighestWorkableBlock($level, $x, $z);
$level->level->setBlockID($x, $y, $z, DEAD_BUSH);
}
}
for($i = 0; $i < 2; ++$i){
$x = $chunkX*16 + $random->nextRange(0, 15);
$z = $chunkZ*16 + $random->nextRange(0, 15);
if($level->level->getBiomeId($x, $z) === BIOME_DESERT){
$y = $this->getHighestWorkableBlock($level, $x, $z);
$cactiSize = $random->nextRange(0, 3);
for($yOff = 0; $yOff < $cactiSize; ++$yOff){
$level->level->setBlockID($x, $y + $yOff, $z, CACTUS);
}
}
}
}
private function getHighestWorkableBlock(Level $level, $x, $z){
for($y = 128; $y > 0; --$y){
$b = $level->level->getBlockID($x, $y, $z);
if($b == SAND && $level->level->getBlockID($x, $y + 1, $z) == 0){
return $y + 1;
}
}
return -1;
}
}