Merge pull request #12 from GameHerobrine/test

Tree Generation Tweaks
This commit is contained in:
ArkQuark 2022-05-15 22:59:55 +03:00 • committed by GitHub
commit 06e9a1d6cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 52 deletions

View file

@ -28,7 +28,7 @@ class SmallTreeObject extends TreeObject{
private $trunkHeight = 5; private $trunkHeight = 5;
private static $leavesHeight = 4; // All trees appear to be 4 tall private static $leavesHeight = 4; // All trees appear to be 4 tall
private static $leafRadii = array( 1, 1.41, 2.83, 2.24 ); private static $leafRadii = array( 1, 1.41, 2.83, 2.24 );
public $treeHeight = 7;
private $addLeavesVines = false; private $addLeavesVines = false;
private $addLogVines = false; private $addLogVines = false;
private $addCocoaPlants = false; private $addCocoaPlants = false;
@ -50,56 +50,45 @@ class SmallTreeObject extends TreeObject{
return true; return true;
} }
public function placeObject(Level $level, Vector3 $pos, Random $random){ protected function placeTrunk(Level $level, $x, $y, $z, Random $random, $trunkHeight){
// The base dirt block // The base dirt block
$dirtpos = new Vector3( $pos->x, $pos->y - 1, $pos->z ); $dirtpos = new Vector3($x, $y - 1, $z);
$level->setBlockRaw($dirtpos, new DirtBlock()); $level->setBlockRaw($dirtpos, new DirtBlock());
// Adjust the tree trunk's height randomly for($yy = 0; $yy < $this->treeHeight; ++$yy){
// plot [-14:11] int( x / 8 ) + 5 $blockId = $level->getBlock(new Vector3($x, $y + $yy, $z))->getID();
// - min=4 (all leaves are 4 tall, some trunk must show) if(isset($this->overridable[$blockId])){
// - max=6 (top leaves are within ground-level whacking range $trunkpos = new Vector3($x, $y + $yy, $z);
// on all small trees) $level->setBlockRaw($trunkpos, new WoodBlock($this->type));
$heightPre = $random->nextRange(-14, 11); }
$this->trunkHeight = intval( $heightPre / 8 ) + 5; }
}
// Adjust the starting leaf density using the trunk height as a public function placeObject(Level $level, Vector3 $pos, Random $random){
// starting position (tall trees with skimpy leaves don't look $this->treeHeight = mt_rand(0, 3) + 4; //randomized tree height
// too good) $x = $pos->getX();
$leafPre = $random->nextRange($this->trunkHeight, 10) / 20; // (TODO: seed may apply) $y = $pos->getY();
$z = $pos->getZ();
// Now build the tree (from the top down) for($yy = $y - 3 + $this->treeHeight; $yy <= $y + $this->treeHeight; ++$yy){
$leaflevel = 0; $yOff = $yy - ($y + $this->treeHeight);
for( $yy = ($this->trunkHeight); $yy >= 0; --$yy ) $mid = (int) (1 - $yOff / 2);
{ for($xx = $x - $mid; $xx <= $x + $mid; ++$xx){
if( $leaflevel < self::$leavesHeight ) $xOff = abs($xx - $x);
{ for($zz = $z - $mid; $zz <= $z + $mid; ++$zz){
// The size is a slight variation on the trunkheight $zOff = abs($zz - $z);
$radius = self::$leafRadii[ $leaflevel ] + $leafPre; if($xOff === $mid and $zOff === $mid and ($yOff === 0 or mt_rand(0, 2) === 0)){
$bRadius = 2; continue;
for( $xx = -$bRadius; $xx <= $bRadius; ++ $xx ) }
{ if(!$level->getBlock(new Vector3($xx, $yy, $zz))->isSolid){
for( $zz = -$bRadius; $zz <= $bRadius; ++ $zz ) $leafpos = new Vector3($xx, $yy, $zz);
{
if( sqrt(($xx * $xx) + ($zz * $zz)) <= $radius )
{
$leafpos = new Vector3( $pos->x + $xx,
$pos->y + $yy,
$pos->z + $zz );
$level->setBlockRaw($leafpos, new LeavesBlock($this->type)); $level->setBlockRaw($leafpos, new LeavesBlock($this->type));
} }
} }
} }
$leaflevel ++;
} }
$this->placeTrunk($level, $x, $y, $z, $random, $this->treeHeight - 1);
// Place the trunk last
if($leaflevel > 1)
{
$trunkpos = new Vector3( $pos->x, $pos->y + $yy, $pos->z );
$level->setBlockRaw($trunkpos, new WoodBlock( $this->type ) );
} }
} }
}
}

View file

@ -24,7 +24,6 @@ class TreeObject{
public $overridable = array( public $overridable = array(
0 => true, 0 => true,
6 => true, 6 => true,
17 => true,
18 => true, 18 => true,
); );