From 7297f8d2c004157f305da3292bed0cd69bb0879c Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Sat, 18 May 2013 15:22:07 +0200 Subject: [PATCH] Level Generation API updated --- src/world/generator/LevelGenerator.php | 10 +++++---- src/world/generator/SuperflatGenerator.php | 25 +++++++++++++--------- src/world/generator/WorldGenerator.php | 9 ++++---- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/world/generator/LevelGenerator.php b/src/world/generator/LevelGenerator.php index 0d5cc0fcc..0d71ae886 100644 --- a/src/world/generator/LevelGenerator.php +++ b/src/world/generator/LevelGenerator.php @@ -27,12 +27,14 @@ the Free Software Foundation, either version 3 of the License, or interface LevelGenerator{ public function __construct(array $options = array()); + + public function init(Level $level, Random $random); - public function generateChunk(Level $level, $chunkX, $chunkY, $chunkZ, Random $random); + public function generateChunk($chunkX, $chunkY, $chunkZ); - public function populateChunk(Level $level, $chunkX, $chunkY, $chunkZ, Random $random); + public function populateChunk($chunkX, $chunkY, $chunkZ); - public function populateLevel(Level $level, Random $random); + public function populateLevel(); - public function getSpawn(Random $random); + public function getSpawn(); } \ No newline at end of file diff --git a/src/world/generator/SuperflatGenerator.php b/src/world/generator/SuperflatGenerator.php index 38e14bc18..cbb1082d7 100644 --- a/src/world/generator/SuperflatGenerator.php +++ b/src/world/generator/SuperflatGenerator.php @@ -30,7 +30,7 @@ require_once("LevelGenerator.php"); /***REM_END***/ class SuperflatGenerator implements LevelGenerator{ - private $config, $structure, $chunks, $options, $floorLevel; + private $level, $random, $structure, $chunks, $options, $floorLevel; public function __construct(array $options = array()){ $this->preset = "2;7,2x3,2;1;spawn(radius=10 block=24)"; @@ -101,16 +101,21 @@ class SuperflatGenerator implements LevelGenerator{ $this->options[$option] = $params; } } + + public function init(Level $level, Random $random){ + $this->level = $level; + $this->random = $random; + } - public function generateChunk(Level $level, $chunkX, $chunkY, $chunkZ, Random $random){ - $level->setMiniChunk($chunkX, $chunkZ, $chunkY, $this->chunks[$chunkY]); + public function generateChunk($chunkX, $chunkY, $chunkZ){ + $this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $this->chunks[$chunkY]); } - public function populateChunk(Level $level, $chunkX, $chunkY, $chunkZ, Random $random){ + public function populateChunk($chunkX, $chunkY, $chunkZ){ } - public function populateLevel(Level $level, Random $random){ + public function populateLevel(){ if(isset($this->options["spawn"])){ $spawn = array(10, new SandstoneBlock()); if(isset($this->options["spawn"]["radius"])){ @@ -128,7 +133,7 @@ class SuperflatGenerator implements LevelGenerator{ for($x = $start; $x <= $end; ++$x){ for($z = $start; $z <= $end; ++$z){ if(floor(sqrt(pow($x - 128, 2) + pow($z - 128, 2))) <= $spawn[0]){ - $level->setBlockRaw(new Vector3($x, $this->floorLevel - 1, $z), $spawn[1]); + $this->level->setBlockRaw(new Vector3($x, $this->floorLevel - 1, $z), $spawn[1]); } } } @@ -140,14 +145,14 @@ class SuperflatGenerator implements LevelGenerator{ $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)); + $centerX = $this->random->nextRange(0, 256); + $centerZ = $this->random->nextRange(0, 256); + TreeObject::growTree($this->level, new Vector3($centerX, $this->floorLevel, $centerZ), $this->random->nextRange(0,3)); } } } - public function getSpawn(Random $random){ + public function getSpawn(){ return new Vector3(128, $this->floorLevel, 128); } } \ No newline at end of file diff --git a/src/world/generator/WorldGenerator.php b/src/world/generator/WorldGenerator.php index a021e2de2..fb8de5ba8 100644 --- a/src/world/generator/WorldGenerator.php +++ b/src/world/generator/WorldGenerator.php @@ -51,17 +51,18 @@ class WorldGenerator{ } public function generate(){ + $this->generator->init($this->level, $this->random); for($Z = 0; $Z < $this->width; ++$Z){ for($X = 0; $X < $this->width; ++$X){ for($Y = 0; $Y < $this->height; ++$Y){ - $this->generator->generateChunk($this->level, $X, $Y, $Z, $this->random); - $this->generator->populateChunk($this->level, $X, $Y, $Z, $this->random); + $this->generator->generateChunk($X, $Y, $Z); + $this->generator->populateChunk($X, $Y, $Z); } } console("[NOTICE] Generating level ".ceil((($Z + 1)/$this->width) * 100)."%"); } - $this->generator->populateLevel($this->level, $this->random); - $this->level->setSpawn($this->generator->getSpawn($this->random)); + $this->generator->populateLevel(); + $this->level->setSpawn($this->generator->getSpawn()); $this->level->save(true); }