2017-04-23 14:42:51 +00:00
|
|
|
<?php
|
2017-05-10 15:03:04 +00:00
|
|
|
/**
|
|
|
|
* ____ __ __ ____
|
|
|
|
* /\ _`\ /\ \__ /\ \__ /\ _`\
|
|
|
|
* \ \ \L\ \ __ \ \ ,_\\ \ ,_\ __ _ __ \ \ \L\_\ __ ___
|
|
|
|
* \ \ _ <' /'__`\\ \ \/ \ \ \/ /'__`\/\`'__\\ \ \L_L /'__`\ /' _ `\
|
|
|
|
* \ \ \L\ \/\ __/ \ \ \_ \ \ \_ /\ __/\ \ \/ \ \ \/, \/\ __/ /\ \/\ \
|
|
|
|
* \ \____/\ \____\ \ \__\ \ \__\\ \____\\ \_\ \ \____/\ \____\\ \_\ \_\
|
|
|
|
* \/___/ \/____/ \/__/ \/__/ \/____/ \/_/ \/___/ \/____/ \/_/\/_/
|
2017-05-11 11:29:22 +00:00
|
|
|
* Tomorrow's pocketmine generator.
|
2017-05-10 15:03:04 +00:00
|
|
|
* @author Ad5001
|
|
|
|
* @link https://github.com/Ad5001/BetterGen
|
2017-04-23 14:42:51 +00:00
|
|
|
*/
|
2017-05-10 15:03:04 +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;
|
|
|
|
use pocketmine\utils\Random;
|
2017-04-23 14:42:51 +00:00
|
|
|
|
|
|
|
class RavinePopulator extends AmountPopulator {
|
2017-05-11 09:49:57 +00:00
|
|
|
/** @var ChunkManager */
|
2017-04-23 14:42:51 +00:00
|
|
|
protected $level;
|
|
|
|
const NOISE = 250;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2017-04-23 14:42:51 +00:00
|
|
|
if ($amount > 50) { // Only build one per chunk
|
2017-04-29 09:59:44 +00:00
|
|
|
$depth = $random->nextBoundedInt(60 ) + 30; // 2Much4U?
|
|
|
|
$x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15);
|
|
|
|
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15);
|
|
|
|
$y = $random->nextRange(5, $this->getHighestWorkableBlock($x, $z ));
|
2017-04-23 14:42:51 +00:00
|
|
|
$deffX = $x;
|
|
|
|
$deffZ = $z;
|
2017-04-29 09:59:44 +00:00
|
|
|
$height = $random->nextRange(15, 30);
|
|
|
|
$length = $random->nextRange(5, 12);
|
2017-04-23 14:42:51 +00:00
|
|
|
for($i = 0; $i < $depth; $i ++) {
|
2017-04-29 09:59:44 +00:00
|
|
|
$this->buildRavinePart($x, $y, $z, $height, $length, $random);
|
2017-04-23 14:42:51 +00:00
|
|
|
$diffX = $x - $deffX;
|
|
|
|
$diffZ = $z - $deffZ;
|
|
|
|
if ($diffX > $length / 2)
|
|
|
|
$diffX = $length / 2;
|
|
|
|
if ($diffX < - $length / 2)
|
|
|
|
$diffX = - $length / 2;
|
|
|
|
if ($diffZ > $length / 2)
|
|
|
|
$diffZ = $length / 2;
|
|
|
|
if ($diffZ < - $length / 2)
|
|
|
|
$diffZ = - $length / 2;
|
|
|
|
if ($length > 10)
|
|
|
|
$length = 10;
|
|
|
|
if ($length < 5)
|
|
|
|
$length = 5;
|
2017-04-29 09:59:44 +00:00
|
|
|
$x += $random->nextRange(0 + $diffX, 2 + $diffX ) - 1;
|
|
|
|
$y += $random->nextRange(0, 2 ) - 1;
|
|
|
|
$z += $random->nextRange(0 + $diffZ, 2 + $diffZ ) - 1;
|
2017-05-11 06:35:10 +00:00
|
|
|
$height += $random->nextRange(0, 2 ) - 1;
|
2017-04-29 09:59:44 +00:00
|
|
|
$length += $random->nextRange(0, 2 ) - 1;
|
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 09:49:57 +00:00
|
|
|
for($y = Level::Y_MAX; $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-04-29 09:59:44 +00:00
|
|
|
return $y++;
|
2017-04-23 14:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Builds a ravine part
|
|
|
|
* @param $x int
|
|
|
|
* @param $y int
|
|
|
|
* @param $z int
|
|
|
|
* @param $height int
|
|
|
|
* @param $length int
|
|
|
|
* @param $random pocketmine\utils\Random
|
|
|
|
*/
|
|
|
|
protected function buildRavinePart($x, $y, $z, $height, $length, Random $random) {
|
2017-05-11 06:35:10 +00:00
|
|
|
$xBounded = 0;
|
|
|
|
$zBounded = 0;
|
2017-04-23 14:42:51 +00:00
|
|
|
for($xx = $x - $length; $xx <= $x + $length; $xx ++) {
|
|
|
|
for($yy = $y; $yy <= $y + $height; $yy ++) {
|
|
|
|
for($zz = $z - $length; $zz <= $z + $length; $zz ++) {
|
|
|
|
$oldXB = $xBounded;
|
2017-04-29 09:59:44 +00:00
|
|
|
$xBounded = $random->nextBoundedInt(self::NOISE * 2 ) - self::NOISE;
|
2017-04-23 14:42:51 +00:00
|
|
|
$oldZB = $zBounded;
|
2017-04-29 09:59:44 +00:00
|
|
|
$zBounded = $random->nextBoundedInt(self::NOISE * 2 ) - self::NOISE;
|
2017-04-23 14:42:51 +00:00
|
|
|
if ($xBounded > self::NOISE - 2) {
|
|
|
|
$xBounded = 1;
|
|
|
|
} elseif ($xBounded < - self::NOISE + 2) {
|
2017-04-29 09:59:44 +00:00
|
|
|
$xBounded = -1;
|
2017-04-23 14:42:51 +00:00
|
|
|
} else {
|
|
|
|
$xBounded = $oldXB;
|
|
|
|
}
|
|
|
|
if ($zBounded > self::NOISE - 2) {
|
|
|
|
$zBounded = 1;
|
|
|
|
} elseif ($zBounded < - self::NOISE + 2) {
|
2017-04-29 09:59:44 +00:00
|
|
|
$zBounded = -1;
|
2017-04-23 14:42:51 +00:00
|
|
|
} else {
|
|
|
|
$zBounded = $oldZB;
|
|
|
|
}
|
2017-04-29 09:59:44 +00:00
|
|
|
if (abs((abs($xx ) - abs($x )) ** 2 + (abs($zz ) - abs($z )) ** 2 ) < ((($length / 2 - $xBounded) + ($length / 2 - $zBounded)) / 2) ** 2 && $y > 0 && ! in_array($this->level->getBlockIdAt(( int ) round($xx ),(int ) round($yy ),(int ) round($zz ) ), BuildingUtils::TO_NOT_OVERWRITE ) && ! in_array($this->level->getBlockIdAt(( int ) round($xx ),(int ) round($yy + 1 ),(int ) round($zz ) ), BuildingUtils::TO_NOT_OVERWRITE )) {
|
|
|
|
$this->level->setBlockIdAt(( int ) round($xx ),(int ) round($yy ),(int ) round($zz ), Block::AIR);
|
2017-04-23 14:42:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|