From e81d68c8d0a78d73ad116ee5f8c13ecd0dcb1df4 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Sat, 18 May 2013 11:02:00 +0200 Subject: [PATCH] Added trees to teh SuperflatGenerator --- src/material/block/plant/Sapling.php | 2 +- src/utils/Random.php | 4 ++++ src/world/generator/SuperflatGenerator.class.php | 12 ++++++++++++ src/world/generator/object/tree/BigTreeObject.php | 12 ++++++------ src/world/generator/object/tree/PineTreeObject.php | 12 ++++++------ .../generator/object/tree/SmallTreeObject.php | 12 ++++++------ .../generator/object/tree/SpruceTreeObject.php | 14 +++++++------- src/world/generator/object/tree/TreeObject.php | 8 ++++---- 8 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/material/block/plant/Sapling.php b/src/material/block/plant/Sapling.php index fee9622425..d6a75322d8 100644 --- a/src/material/block/plant/Sapling.php +++ b/src/material/block/plant/Sapling.php @@ -54,7 +54,7 @@ class SaplingBlock extends TransparentBlock{ public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ //Bonemeal - TreeObject::growTree($this->level, $this); + TreeObject::growTree($this->level, $this, $this->meta); return true; } return false; diff --git a/src/utils/Random.php b/src/utils/Random.php index deb83a2047..4ecdb3e67d 100644 --- a/src/utils/Random.php +++ b/src/utils/Random.php @@ -57,5 +57,9 @@ class Random{ public function nextBoolean(){ return $this->random->rangeint(0, 1) === 1; } + + public function nextRange($start = 0, $end = PHP_INT_MAX){ + return $this->random->rangeint($start, $end); + } } \ No newline at end of file diff --git a/src/world/generator/SuperflatGenerator.class.php b/src/world/generator/SuperflatGenerator.class.php index d29d810cbe..c132c79bcf 100644 --- a/src/world/generator/SuperflatGenerator.class.php +++ b/src/world/generator/SuperflatGenerator.class.php @@ -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){ diff --git a/src/world/generator/object/tree/BigTreeObject.php b/src/world/generator/object/tree/BigTreeObject.php index 28a99dab6e..fc478af010 100644 --- a/src/world/generator/object/tree/BigTreeObject.php +++ b/src/world/generator/object/tree/BigTreeObject.php @@ -44,14 +44,14 @@ class BigTreeObject extends TreeObject{ private $addLogVines = false; private $addCocoaPlants = false; - public function canPlaceObject(Level $level, $x, $y, $z){ + public function canPlaceObject(Level $level, Vector3 $pos){ 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); - $leaves = $this->getLeafGroupPoints($level, $x, $y, $z); + $leaves = $this->getLeafGroupPoints($level, $pos); foreach($leaves as $leafGroup){ $groupX = $leafGroup->getBlockX(); $groupY = $leafGrou->getBlockY(); @@ -66,7 +66,7 @@ class BigTreeObject extends TreeObject{ } 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->leavesHeight += mt_rand(0, 1); 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($zz = -$xzRadius; $zz < ($xzRadius + 1); ++$zz){ 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){ - $level->setBlock($x, $y + $yy, $z, 17, $type); + $level->setBlock($x, $pos->y + $yy, $z, 17, $type); } */ } diff --git a/src/world/generator/object/tree/PineTreeObject.php b/src/world/generator/object/tree/PineTreeObject.php index c70f70c768..7fd245ad9c 100644 --- a/src/world/generator/object/tree/PineTreeObject.php +++ b/src/world/generator/object/tree/PineTreeObject.php @@ -35,7 +35,7 @@ class PineTreeObject extends TreeObject{ private $leavesSizeY = -1; private $leavesAbsoluteMaxRadius = -1; - public function canPlaceObject(Level $level, $x, $y, $z){ + public function canPlaceObject(Level $level, Vector3 $pos){ $this->findRandomLeavesSize(); $checkRadius = 0; for($yy = 0; $yy < $this->totalHeight; ++$yy) { @@ -44,7 +44,7 @@ class PineTreeObject extends TreeObject{ } for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){ 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()])){ return false; } @@ -60,11 +60,11 @@ class PineTreeObject extends TreeObject{ $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) { $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); $leavesMaxRadius = 1; $leavesBottomY = $this->totalHeight - $this->leavesSizeY; @@ -74,7 +74,7 @@ class PineTreeObject extends TreeObject{ for ($xx = -$leavesRadius; $xx < ($leavesRadius + 1); ++$xx) { for ($zz = -$leavesRadius; $zz < ($leavesRadius + 1); ++$zz) { 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); 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)); } } diff --git a/src/world/generator/object/tree/SmallTreeObject.php b/src/world/generator/object/tree/SmallTreeObject.php index ed3eb6ab37..e013193d0c 100644 --- a/src/world/generator/object/tree/SmallTreeObject.php +++ b/src/world/generator/object/tree/SmallTreeObject.php @@ -38,7 +38,7 @@ class SmallTreeObject extends TreeObject{ private $addLogVines = false; private $addCocoaPlants = false; - public function canPlaceObject(Level $level, $x, $y, $z){ + public function canPlaceObject(Level $level, Vector3 $pos){ $radiusToCheck = $this->radiusIncrease; for ($yy = 0; $yy < $this->totalHeight + 2; ++$yy) { if ($yy == 1 or $yy === $this->totalHeight - 1) { @@ -46,7 +46,7 @@ class SmallTreeObject extends TreeObject{ } for($xx = -$radiusToCheck; $xx < ($radiusToCheck + 1); ++$xx){ 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()])){ return false; } @@ -56,8 +56,8 @@ class SmallTreeObject extends TreeObject{ return true; } - public function placeObject(Level $level, $x, $y, $z){ - $level->setBlock(new Vector3($x, $y - 1, $z), new DirtBlock()); + public function placeObject(Level $level, Vector3 $pos){ + $level->setBlock(new Vector3($x, $pos->y - 1, $z), new DirtBlock()); $this->totalHeight += mt_rand(-1, 3); $this->leavesHeight += mt_rand(0, 1); 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($zz = -$xzRadius; $zz < ($xzRadius + 1); ++$zz){ 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){ - $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)); } } diff --git a/src/world/generator/object/tree/SpruceTreeObject.php b/src/world/generator/object/tree/SpruceTreeObject.php index ecd81d8f4f..759e39a163 100644 --- a/src/world/generator/object/tree/SpruceTreeObject.php +++ b/src/world/generator/object/tree/SpruceTreeObject.php @@ -35,7 +35,7 @@ class SpruceTreeObject extends TreeObject{ private $leavesBottomY = -1; private $leavesMaxRadius = -1; - public function canPlaceObject(Level $level, $x, $y, $z){ + public function canPlaceObject(Level $level, Vector3 $pos){ $this->findRandomLeavesSize(); $checkRadius = 0; for($yy = 0; $yy < $this->totalHeight + 2; ++$yy) { @@ -44,7 +44,7 @@ class SpruceTreeObject extends TreeObject{ } for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){ 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()])){ return false; } @@ -60,28 +60,28 @@ class SpruceTreeObject extends TreeObject{ $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) { $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; for($yy = $this->totalHeight; $yy >= $this->leavesBottomY; --$yy){ for ($xx = -$leavesRadius; $xx < ($leavesRadius + 1); ++$xx) { for ($zz = -$leavesRadius; $zz < ($leavesRadius + 1); ++$zz) { 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; }elseif($leavesRadius < $this->leavesMaxRadius){ ++$leavesRadius; } } 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)); } } diff --git a/src/world/generator/object/tree/TreeObject.php b/src/world/generator/object/tree/TreeObject.php index 7351c25ad4..241d666685 100644 --- a/src/world/generator/object/tree/TreeObject.php +++ b/src/world/generator/object/tree/TreeObject.php @@ -33,8 +33,8 @@ class TreeObject{ 17 => true, 18 => true, ); - public static function growTree(Level $level, Block $block){ - switch($block->getMetadata() & 0x03){ + public static function growTree(Level $level, Vector3 $pos, $type = 0){ + switch($type & 0x03){ case SaplingBlock::SPRUCE: if(mt_rand(0,1) == 1){ $tree = new SpruceTreeObject(); @@ -55,8 +55,8 @@ class TreeObject{ } break; } - if($tree->canPlaceObject($level, $block->x, $block->y, $block->z)){ - $tree->placeObject($level, $block->x, $block->y, $block->z); + if($tree->canPlaceObject($level, $pos)){ + $tree->placeObject($level, $pos); } } } \ No newline at end of file