Added trees to teh SuperflatGenerator

This commit is contained in:
Shoghi Cervantes Pueyo 2013-05-18 11:02:00 +02:00
parent 3c4d4f5cff
commit e81d68c8d0
8 changed files with 46 additions and 30 deletions

View File

@ -54,7 +54,7 @@ class SaplingBlock extends TransparentBlock{
public function onActivate(Item $item, Player $player){ public function onActivate(Item $item, Player $player){
if($item->getID() === DYE and $item->getMetadata() === 0x0F){ //Bonemeal if($item->getID() === DYE and $item->getMetadata() === 0x0F){ //Bonemeal
TreeObject::growTree($this->level, $this); TreeObject::growTree($this->level, $this, $this->meta);
return true; return true;
} }
return false; return false;

View File

@ -57,5 +57,9 @@ class Random{
public function nextBoolean(){ public function nextBoolean(){
return $this->random->rangeint(0, 1) === 1; return $this->random->rangeint(0, 1) === 1;
} }
public function nextRange($start = 0, $end = PHP_INT_MAX){
return $this->random->rangeint($start, $end);
}
} }

View File

@ -130,6 +130,18 @@ class SuperflatGenerator implements LevelGenerator{
} }
} }
} }
if(isset($this->options["decoration"])){
$treecount = 150;
if(isset($this->options["spawn"]["treecount"])){
$treecount = intval($this->options["spawn"]["treecount"]);
}
for($t = 0; $t < $treecount; ++$t){
$centerX = $random->nextRange(0, 256);
$centerZ = $random->nextRange(0, 256);
TreeObject::growTree($level, new Vector3($centerX, $this->floorLevel, $centerZ), $random->nextRange(0,3));
}
}
} }
public function getSpawn(Random $random){ public function getSpawn(Random $random){

View File

@ -44,14 +44,14 @@ class BigTreeObject extends TreeObject{
private $addLogVines = false; private $addLogVines = false;
private $addCocoaPlants = false; private $addCocoaPlants = false;
public function canPlaceObject(Level $level, $x, $y, $z){ public function canPlaceObject(Level $level, Vector3 $pos){
return false; return false;
} }
public function placeObject(Level $level, $x, $y, $z, $type){ public function placeObject(Level $level, Vector3 $pos, $type){
$this->trunkHeight = (int) ($this->totalHeight * $this->trunkHeightMultiplier); $this->trunkHeight = (int) ($this->totalHeight * $this->trunkHeightMultiplier);
$leaves = $this->getLeafGroupPoints($level, $x, $y, $z); $leaves = $this->getLeafGroupPoints($level, $pos);
foreach($leaves as $leafGroup){ foreach($leaves as $leafGroup){
$groupX = $leafGroup->getBlockX(); $groupX = $leafGroup->getBlockX();
$groupY = $leafGrou->getBlockY(); $groupY = $leafGrou->getBlockY();
@ -66,7 +66,7 @@ class BigTreeObject extends TreeObject{
} }
generateBranches(w, x, y, z, leaves); generateBranches(w, x, y, z, leaves);
$level->setBlock($x, $y - 1, $z, 3, 0); $level->setBlock($x, $pos->y - 1, $z, 3, 0);
$this->totalHeight += mt_rand(-1, 3); $this->totalHeight += mt_rand(-1, 3);
$this->leavesHeight += mt_rand(0, 1); $this->leavesHeight += mt_rand(0, 1);
for($yy = ($this->totalHeight - $this->leavesHeight); $yy < ($this->totalHeight + 1); ++$yy){ for($yy = ($this->totalHeight - $this->leavesHeight); $yy < ($this->totalHeight + 1); ++$yy){
@ -75,13 +75,13 @@ class BigTreeObject extends TreeObject{
for($xx = -$xzRadius; $xx < ($xzRadius + 1); ++$xx){ for($xx = -$xzRadius; $xx < ($xzRadius + 1); ++$xx){
for($zz = -$xzRadius; $zz < ($xzRadius + 1); ++$zz){ for($zz = -$xzRadius; $zz < ($xzRadius + 1); ++$zz){
if((abs($xx) != $xzRadius or abs($zz) != $xzRadius) and $yRadius != 0){ if((abs($xx) != $xzRadius or abs($zz) != $xzRadius) and $yRadius != 0){
$level->setBlock($x + $xx, $y + $yy, $z + $zz, 18, $type); $level->setBlock($pos->x + $xx, $pos->y + $yy, $pos->z + $zz, 18, $type);
} }
} }
} }
} }
for($yy = 0; $yy < ($this->totalHeight - 1); ++$yy){ for($yy = 0; $yy < ($this->totalHeight - 1); ++$yy){
$level->setBlock($x, $y + $yy, $z, 17, $type); $level->setBlock($x, $pos->y + $yy, $z, 17, $type);
} }
*/ */
} }

View File

@ -35,7 +35,7 @@ class PineTreeObject extends TreeObject{
private $leavesSizeY = -1; private $leavesSizeY = -1;
private $leavesAbsoluteMaxRadius = -1; private $leavesAbsoluteMaxRadius = -1;
public function canPlaceObject(Level $level, $x, $y, $z){ public function canPlaceObject(Level $level, Vector3 $pos){
$this->findRandomLeavesSize(); $this->findRandomLeavesSize();
$checkRadius = 0; $checkRadius = 0;
for($yy = 0; $yy < $this->totalHeight; ++$yy) { for($yy = 0; $yy < $this->totalHeight; ++$yy) {
@ -44,7 +44,7 @@ class PineTreeObject extends TreeObject{
} }
for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){ for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){
for($zz = -$checkRadius; $zz < ($checkRadius + 1); ++$zz){ for($zz = -$checkRadius; $zz < ($checkRadius + 1); ++$zz){
$block = $level->getBlock(new Vector3($x + $xx, $y + $yy, $z + $zz)); $block = $level->getBlock(new Vector3($pos->x + $xx, $pos->y + $yy, $pos->z + $zz));
if(!isset($this->overridable[$block->getID()])){ if(!isset($this->overridable[$block->getID()])){
return false; return false;
} }
@ -60,11 +60,11 @@ class PineTreeObject extends TreeObject{
$this->leavesAbsoluteMaxRadius = 2 + mt_rand(0, 2); $this->leavesAbsoluteMaxRadius = 2 + mt_rand(0, 2);
} }
public function placeObject(Level $level, $x, $y, $z){ public function placeObject(Level $level, Vector3 $pos){
if($this->leavesSizeY === -1 or $this->leavesAbsoluteMaxRadius === -1) { if($this->leavesSizeY === -1 or $this->leavesAbsoluteMaxRadius === -1) {
$this->findRandomLeavesSize(); $this->findRandomLeavesSize();
} }
$level->setBlock(new Vector3($x, $y - 1, $z), new DirtBlock()); $level->setBlock(new Vector3($x, $pos->y - 1, $z), new DirtBlock());
$leavesRadius = mt_rand(0,2); $leavesRadius = mt_rand(0,2);
$leavesMaxRadius = 1; $leavesMaxRadius = 1;
$leavesBottomY = $this->totalHeight - $this->leavesSizeY; $leavesBottomY = $this->totalHeight - $this->leavesSizeY;
@ -74,7 +74,7 @@ class PineTreeObject extends TreeObject{
for ($xx = -$leavesRadius; $xx < ($leavesRadius + 1); ++$xx) { for ($xx = -$leavesRadius; $xx < ($leavesRadius + 1); ++$xx) {
for ($zz = -$leavesRadius; $zz < ($leavesRadius + 1); ++$zz) { for ($zz = -$leavesRadius; $zz < ($leavesRadius + 1); ++$zz) {
if (abs($xx) != $leavesRadius or abs($zz) != $leavesRadius or $leavesRadius <= 0) { if (abs($xx) != $leavesRadius or abs($zz) != $leavesRadius or $leavesRadius <= 0) {
$level->setBlock(new Vector3($x + $xx, $y + $yy, $z + $zz), new LeavesBlock($this->type)); $level->setBlock(new Vector3($pos->x + $xx, $pos->y + $yy, $pos->z + $zz), new LeavesBlock($this->type));
} }
} }
} }
@ -90,7 +90,7 @@ class PineTreeObject extends TreeObject{
} }
$trunkHeightReducer = mt_rand(0,3); $trunkHeightReducer = mt_rand(0,3);
for($yy = 0; $yy < ($this->totalHeight - $trunkHeightReducer); ++$yy){ for($yy = 0; $yy < ($this->totalHeight - $trunkHeightReducer); ++$yy){
$level->setBlock(new Vector3($x, $y + $yy, $z), new WoodBlock($this->type)); $level->setBlock(new Vector3($pos->x, $pos->y + $yy, $pos->z), new WoodBlock($this->type));
} }
} }

View File

@ -38,7 +38,7 @@ class SmallTreeObject extends TreeObject{
private $addLogVines = false; private $addLogVines = false;
private $addCocoaPlants = false; private $addCocoaPlants = false;
public function canPlaceObject(Level $level, $x, $y, $z){ public function canPlaceObject(Level $level, Vector3 $pos){
$radiusToCheck = $this->radiusIncrease; $radiusToCheck = $this->radiusIncrease;
for ($yy = 0; $yy < $this->totalHeight + 2; ++$yy) { for ($yy = 0; $yy < $this->totalHeight + 2; ++$yy) {
if ($yy == 1 or $yy === $this->totalHeight - 1) { if ($yy == 1 or $yy === $this->totalHeight - 1) {
@ -46,7 +46,7 @@ class SmallTreeObject extends TreeObject{
} }
for($xx = -$radiusToCheck; $xx < ($radiusToCheck + 1); ++$xx){ for($xx = -$radiusToCheck; $xx < ($radiusToCheck + 1); ++$xx){
for($zz = -$radiusToCheck; $zz < ($radiusToCheck + 1); ++$zz){ for($zz = -$radiusToCheck; $zz < ($radiusToCheck + 1); ++$zz){
$block = $level->getBlock(new Vector3($x + $xx, $y + $yy, $z + $zz)); $block = $level->getBlock(new Vector3($pos->x + $xx, $pos->y + $yy, $pos->z + $zz));
if(!isset($this->overridable[$block->getID()])){ if(!isset($this->overridable[$block->getID()])){
return false; return false;
} }
@ -56,8 +56,8 @@ class SmallTreeObject extends TreeObject{
return true; return true;
} }
public function placeObject(Level $level, $x, $y, $z){ public function placeObject(Level $level, Vector3 $pos){
$level->setBlock(new Vector3($x, $y - 1, $z), new DirtBlock()); $level->setBlock(new Vector3($x, $pos->y - 1, $z), new DirtBlock());
$this->totalHeight += mt_rand(-1, 3); $this->totalHeight += mt_rand(-1, 3);
$this->leavesHeight += mt_rand(0, 1); $this->leavesHeight += mt_rand(0, 1);
for($yy = ($this->totalHeight - $this->leavesHeight); $yy < ($this->totalHeight + 1); ++$yy){ for($yy = ($this->totalHeight - $this->leavesHeight); $yy < ($this->totalHeight + 1); ++$yy){
@ -66,13 +66,13 @@ class SmallTreeObject extends TreeObject{
for($xx = -$xzRadius; $xx < ($xzRadius + 1); ++$xx){ for($xx = -$xzRadius; $xx < ($xzRadius + 1); ++$xx){
for($zz = -$xzRadius; $zz < ($xzRadius + 1); ++$zz){ for($zz = -$xzRadius; $zz < ($xzRadius + 1); ++$zz){
if((abs($xx) != $xzRadius or abs($zz) != $xzRadius) and $yRadius != 0){ if((abs($xx) != $xzRadius or abs($zz) != $xzRadius) and $yRadius != 0){
$level->setBlock(new Vector3($x + $xx, $y + $yy, $z + $zz), new LeavesBlock($this->type)); $level->setBlock(new Vector3($pos->x + $xx, $pos->y + $yy, $pos->z + $zz), new LeavesBlock($this->type));
} }
} }
} }
} }
for($yy = 0; $yy < ($this->totalHeight - 1); ++$yy){ for($yy = 0; $yy < ($this->totalHeight - 1); ++$yy){
$level->setBlock(new Vector3($x, $y + $yy, $z), new WoodBlock($this->type)); $level->setBlock(new Vector3($pos->x, $pos->y + $yy, $pos->z), new WoodBlock($this->type));
} }
} }

View File

@ -35,7 +35,7 @@ class SpruceTreeObject extends TreeObject{
private $leavesBottomY = -1; private $leavesBottomY = -1;
private $leavesMaxRadius = -1; private $leavesMaxRadius = -1;
public function canPlaceObject(Level $level, $x, $y, $z){ public function canPlaceObject(Level $level, Vector3 $pos){
$this->findRandomLeavesSize(); $this->findRandomLeavesSize();
$checkRadius = 0; $checkRadius = 0;
for($yy = 0; $yy < $this->totalHeight + 2; ++$yy) { for($yy = 0; $yy < $this->totalHeight + 2; ++$yy) {
@ -44,7 +44,7 @@ class SpruceTreeObject extends TreeObject{
} }
for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){ for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){
for($zz = -$checkRadius; $zz < ($checkRadius + 1); ++$zz){ for($zz = -$checkRadius; $zz < ($checkRadius + 1); ++$zz){
$block = $level->getBlock(new Vector3($x + $xx, $y + $yy, $z + $zz)); $block = $level->getBlock(new Vector3($pos->x + $xx, $pos->y + $yy, $pos->z + $zz));
if(!isset($this->overridable[$block->getID()])){ if(!isset($this->overridable[$block->getID()])){
return false; return false;
} }
@ -60,28 +60,28 @@ class SpruceTreeObject extends TreeObject{
$this->leavesMaxRadius = 1 + mt_rand(0, 1); $this->leavesMaxRadius = 1 + mt_rand(0, 1);
} }
public function placeObject(Level $level, $x, $y, $z){ public function placeObject(Level $level, Vector3 $pos){
if($this->leavesBottomY === -1 or $this->leavesMaxRadius === -1) { if($this->leavesBottomY === -1 or $this->leavesMaxRadius === -1) {
$this->findRandomLeavesSize(); $this->findRandomLeavesSize();
} }
$level->setBlock(new Vector3($x, $y - 1, $z), new DirtBlock()); $level->setBlock(new Vector3($pos->x, $pos->y - 1, $pos->z), new DirtBlock());
$leavesRadius = 0; $leavesRadius = 0;
for($yy = $this->totalHeight; $yy >= $this->leavesBottomY; --$yy){ for($yy = $this->totalHeight; $yy >= $this->leavesBottomY; --$yy){
for ($xx = -$leavesRadius; $xx < ($leavesRadius + 1); ++$xx) { for ($xx = -$leavesRadius; $xx < ($leavesRadius + 1); ++$xx) {
for ($zz = -$leavesRadius; $zz < ($leavesRadius + 1); ++$zz) { for ($zz = -$leavesRadius; $zz < ($leavesRadius + 1); ++$zz) {
if (abs($xx) != $leavesRadius or abs($zz) != $leavesRadius or $leavesRadius <= 0) { if (abs($xx) != $leavesRadius or abs($zz) != $leavesRadius or $leavesRadius <= 0) {
$level->setBlock(new Vector3($x + $xx, $y + $yy, $z + $zz), new LeavesBLock($this->type)); $level->setBlock(new Vector3($pos->x + $xx, $pos->y + $yy, $pos->z + $zz), new LeavesBLock($this->type));
} }
} }
} }
if ($leavesRadius > 0 and $yy === ($y + $this->leavesBottomY + 1)) { if ($leavesRadius > 0 and $yy === ($pos->y + $this->leavesBottomY + 1)) {
--$leavesRadius; --$leavesRadius;
}elseif($leavesRadius < $this->leavesMaxRadius){ }elseif($leavesRadius < $this->leavesMaxRadius){
++$leavesRadius; ++$leavesRadius;
} }
} }
for($yy = 0; $yy < ($this->totalHeight - 1); ++$yy){ for($yy = 0; $yy < ($this->totalHeight - 1); ++$yy){
$level->setBlock(new Vector3($x, $y + $yy, $z), new WoodBlock($this->type)); $level->setBlock(new Vector3($pos->x, $pos->y + $yy, $pos->z), new WoodBlock($this->type));
} }
} }

View File

@ -33,8 +33,8 @@ class TreeObject{
17 => true, 17 => true,
18 => true, 18 => true,
); );
public static function growTree(Level $level, Block $block){ public static function growTree(Level $level, Vector3 $pos, $type = 0){
switch($block->getMetadata() & 0x03){ switch($type & 0x03){
case SaplingBlock::SPRUCE: case SaplingBlock::SPRUCE:
if(mt_rand(0,1) == 1){ if(mt_rand(0,1) == 1){
$tree = new SpruceTreeObject(); $tree = new SpruceTreeObject();
@ -55,8 +55,8 @@ class TreeObject{
} }
break; break;
} }
if($tree->canPlaceObject($level, $block->x, $block->y, $block->z)){ if($tree->canPlaceObject($level, $pos)){
$tree->placeObject($level, $block->x, $block->y, $block->z); $tree->placeObject($level, $pos);
} }
} }
} }