BetterGen/src/Ad5001/BetterGen/populator/CavePopulator.php

163 lines
5.6 KiB
PHP
Raw Normal View History

2017-04-23 14:42:51 +00:00
<?php
/**
* ____ __ __ ____
* /\ _`\ /\ \__ /\ \__ /\ _`\
* \ \ \L\ \ __ \ \ ,_\\ \ ,_\ __ _ __ \ \ \L\_\ __ ___
* \ \ _ <' /'__`\\ \ \/ \ \ \/ /'__`\/\`'__\\ \ \L_L /'__`\ /' _ `\
* \ \ \L\ \/\ __/ \ \ \_ \ \ \_ /\ __/\ \ \/ \ \ \/, \/\ __/ /\ \/\ \
* \ \____/\ \____\ \ \__\ \ \__\\ \____\\ \_\ \ \____/\ \____\\ \_\ \_\
* \/___/ \/____/ \/__/ \/__/ \/____/ \/_/ \/___/ \/____/ \/_/\/_/
* Tomorrow's pocketmine generator.
* @author Ad5001
* @link https://github.com/Ad5001/BetterGen
2017-04-23 14:42:51 +00:00
*/
2017-04-23 14:42:51 +00:00
namespace Ad5001\BetterGen\populator;
2017-05-11 12:07:26 +00:00
use Ad5001\BetterGen\utils\BuildingUtils;
2017-04-23 14:42:51 +00:00
use pocketmine\block\Block;
use pocketmine\level\ChunkManager;
2017-05-11 12:07:26 +00:00
use pocketmine\level\Level;
2017-04-23 14:42:51 +00:00
use pocketmine\math\Vector3;
2017-05-11 12:07:26 +00:00
use pocketmine\utils\Random;
2017-04-23 14:42:51 +00:00
class CavePopulator extends AmountPopulator {
/** @var ChunkManager */
2017-04-23 14:42:51 +00:00
protected $level;
const STOP = false;
const CONTINUE = true;
/*
* Populate the chunk
* @param $level pocketmine\level\ChunkManager
* @param $chunkX int
* @param $chunkZ int
* @param $random pocketmine\utils\Random
*/
public function populate(ChunkManager $level, $chunkX, $chunkZ, Random $random) {
$this->level = $level;
2017-04-29 09:59:44 +00:00
$amount = $this->getAmount($random);
for($i = 0; $i < $amount; $i++) {
$x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15);
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15);
$y = $random->nextRange(10, $this->getHighestWorkableBlock($x, $z ));
2017-04-23 14:42:51 +00:00
// echo "Generating cave at $x, $y, $z." . PHP_EOL;
2017-04-29 09:59:44 +00:00
$this->generateCave($x, $y, $z, $random);
2017-04-23 14:42:51 +00:00
}
// echo "Finished Populating chunk $chunkX, $chunkZ !" . PHP_EOL;
// Filling water & lava sources randomly
2017-05-11 05:46:44 +00:00
for($i = 0; $i < $random->nextBoundedInt(5) + 3; $i ++) {
2017-04-29 09:59:44 +00:00
$x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15);
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15);
$y = $random->nextRange(10, $this->getHighestWorkableBlock($x, $z ));
if ($level->getBlockIdAt($x, $y, $z ) == Block::STONE && ($level->getBlockIdAt($x + 1, $y, $z ) == Block::AIR || $level->getBlockIdAt($x - 1, $y, $z ) == Block::AIR || $level->getBlockIdAt($x, $y, $z + 1 ) == Block::AIR || $level->getBlockIdAt($x, $y, $z - 1 ) == Block::AIR) && $level->getBlockIdAt($x, $y - 1, $z ) !== Block::AIR && $level->getBlockIdAt($x, $y + 1, $z ) !== Block::AIR) {
2017-04-23 14:42:51 +00:00
if ($y < 40 && $random->nextBoolean ()) {
2017-04-29 09:59:44 +00:00
$level->setBlockIdAt($x, $y, $z, Block::LAVA);
2017-04-23 14:42:51 +00:00
} else {
2017-04-29 09:59:44 +00:00
$level->setBlockIdAt($x, $y, $z, Block::WATER);
2017-04-23 14:42:51 +00:00
}
}
}
}
/*
* Gets the top block (y) on an x and z axes
* @param $x int
* @param $z int
*/
protected function getHighestWorkableBlock($x, $z) {
2017-05-11 14:41:42 +00:00
for($y = Level::Y_MAX - 1; $y > 0; -- $y) {
2017-04-29 09:59:44 +00:00
$b = $this->level->getBlockIdAt($x, $y, $z);
2017-04-23 14:42:51 +00:00
if ($b === Block::DIRT or $b === Block::GRASS or $b === Block::PODZOL or $b === Block::SAND or $b === Block::SNOW_BLOCK or $b === Block::SANDSTONE) {
break;
} elseif ($b !== 0 and $b !== Block::SNOW_LAYER and $b !== Block::WATER) {
return - 1;
}
}
2017-05-11 13:12:17 +00:00
return ++$y;
2017-04-23 14:42:51 +00:00
}
/*
* Generates a cave
* @param $x int
* @param $y int
* @param $z int
* @param $random pocketmine\utils\Random
* @return void
*/
public function generateCave($x, $y, $z, Random $random) {
2017-04-29 09:59:44 +00:00
$generatedBranches = $random->nextBoundedInt(10 ) + 1;
2017-04-23 14:42:51 +00:00
// echo "Num of branch left => " . $generatedBranches . PHP_EOL;
2017-04-29 09:59:44 +00:00
foreach($gen = $this->generateBranch($x, $y, $z, 5, 3, 5, $random ) as $v3 ) {
2017-04-23 14:42:51 +00:00
$generatedBranches --;
if ($generatedBranches <= 0) {
2017-05-13 15:03:39 +00:00
$gen->send(self::STOP);
2017-04-23 14:42:51 +00:00
} else {
2017-04-29 09:59:44 +00:00
$gen->send(self::CONTINUE);
2017-04-23 14:42:51 +00:00
}
}
}
/*
* Generates a cave branch.
* @param $x int
* @param $y int
* @param $z int
* @param $length int
* @param $height int
* @param $depth int
* @param $random pocketmine\utils\Random
* @yield int
* @return void
*/
public function generateBranch($x, $y, $z, $length, $height, $depth, Random $random) {
2017-04-29 09:59:44 +00:00
if (! (yield new Vector3($x, $y, $z ))) {
2017-04-23 14:42:51 +00:00
for($i = 0; $i <= 4; $i ++) {
2017-04-29 09:59:44 +00:00
BuildingUtils::buildRandom($this->level, new Vector3($x, $y, $z ), new Vector3($length - $i, $height - $i, $depth - $i ), $random, Block::get(Block::AIR ));
$x += round(($random->nextBoundedInt(round(30 * ($length / 10) ) + 1 ) / 10 - 2));
$yP = $random->nextRange(- 14, 14);
2017-04-23 14:42:51 +00:00
if ($yP > 12) {
$y ++;
} elseif ($yP < - 12) {
$y --;
}
2017-04-29 09:59:44 +00:00
$z += round(($random->nextBoundedInt(round(30 * ($depth / 10) ) + 1 ) / 10 - 1));
return;
2017-04-23 14:42:51 +00:00
}
}
2017-04-29 09:59:44 +00:00
$repeat = $random->nextBoundedInt(25 ) + 15;
while($repeat -- > 0 ) {
BuildingUtils::buildRandom($this->level, new Vector3($x, $y, $z ), new Vector3($length, $height, $depth ), $random, Block::get(Block::AIR ));
$x += round(($random->nextBoundedInt(round(30 * ($length / 10) ) + 1 ) / 10 - 2));
$yP = $random->nextRange(- 14, 14);
2017-04-23 14:42:51 +00:00
if ($yP > 12) {
$y ++;
} elseif ($yP < - 12) {
$y --;
}
2017-04-29 09:59:44 +00:00
$z += round(($random->nextBoundedInt(round(30 * ($depth / 10) ) + 1 ) / 10 - 1));
$height += $random->nextBoundedInt(3 ) - 1;
$length += $random->nextBoundedInt(3 ) - 1;
$depth += $random->nextBoundedInt(3 ) - 1;
2017-04-23 14:42:51 +00:00
if ($height < 3)
$height = 3;
if ($length < 3)
$length = 3;
if ($height < 3)
$height = 3;
if ($height < 7)
$height = 7;
if ($length < 7)
$length = 7;
if ($height < 7)
$height = 7;
2017-04-29 09:59:44 +00:00
if ($random->nextBoundedInt(10 ) == 0) {
foreach($generator = $this->generateBranch($x, $y, $z, $length, $height, $depth, $random ) as $gen ) {
2017-04-23 14:42:51 +00:00
if (! (yield $gen))
2017-04-29 09:59:44 +00:00
$generator->send(self::STOP);
2017-04-23 14:42:51 +00:00
}
}
}
return;
}
}