Fixing BuildingUtils::fillCallback arguments

This commit is contained in:
Adsooi 2017-05-03 16:37:32 +02:00
parent 4ef639fc80
commit 7b4b4e1d63
4 changed files with 75 additions and 45 deletions

View file

@ -38,7 +38,8 @@ class FallenTreePopulator extends AmountPopulator {
public function populate(ChunkManager $level, $chunkX, $chunkZ, Random $random) { public function populate(ChunkManager $level, $chunkX, $chunkZ, Random $random) {
$this->level = $level; $this->level = $level;
$amount = $this->getAmount($random); $amount = $this->getAmount($random);
$fallenTree = new FallenTree(new TreePopulator::$types[$this->type]()); $tree = TreePopulator::$types[$this->type];
$fallenTree = new FallenTree(new $tree());
for($i = 0; $i < $amount; $i++) { for($i = 0; $i < $amount; $i++) {
$x = $random->nextRange($chunkX * 16, $chunkX * 16 + 15); $x = $random->nextRange($chunkX * 16, $chunkX * 16 + 15);
$z = $random->nextRange($chunkZ * 16, $chunkZ * 16 + 15); $z = $random->nextRange($chunkZ * 16, $chunkZ * 16 + 15);

View file

@ -85,15 +85,12 @@ class MineshaftPopulator extends AmountPopulator {
// First, filling everything blank. // First, filling everything blank.
BuildingUtils::fill($this->level, new Vector3($x, $y, $z - 1 ), new Vector3($x + 4, $y + 2, $z + 1 ), Block::get(Block::AIR )); BuildingUtils::fill($this->level, new Vector3($x, $y, $z - 1 ), new Vector3($x + 4, $y + 2, $z + 1 ), Block::get(Block::AIR ));
// Then, making sure the floor is solid. // Then, making sure the floor is solid.
BuildingUtils::fillCallback(new Vector3($x, $y - 1, $z - 1 ), new Vector3($x + 4, $y - 1, $z + 1 ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x, $y - 1, $z - 1 ), new Vector3($x + 4, $y - 1, $z + 1 ), function ($v3, $level) {
$level = $params [0];
if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR) if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR)
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK);
}, $this->level); }, $this->level);
// Putting rails // Putting rails
BuildingUtils::fillCallback(new Vector3($x, $y, $z ), new Vector3($x + 4, $y, $z ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x, $y, $z ), new Vector3($x + 4, $y, $z ), function ($v3, $level, $random) {
$level = $params [0];
$random = $params [1];
if ($random->nextBoundedInt(3 ) !== 0) { if ($random->nextBoundedInt(3 ) !== 0) {
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::RAIL); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::RAIL);
$level->setBlockDataAt($v3->x, $v3->y, $v3->z, 1); $level->setBlockDataAt($v3->x, $v3->y, $v3->z, 1);
@ -128,15 +125,13 @@ class MineshaftPopulator extends AmountPopulator {
// First, filling everything blank. // First, filling everything blank.
BuildingUtils::fill($this->level, new Vector3($x, $y, $z - 1 ), new Vector3($x - 4, $y + 2, $z + 1 )); BuildingUtils::fill($this->level, new Vector3($x, $y, $z - 1 ), new Vector3($x - 4, $y + 2, $z + 1 ));
// Then, making sure the floor is solid. // Then, making sure the floor is solid.
BuildingUtils::fillCallback(new Vector3($x, $y - 1, $z - 1 ), new Vector3($x - 4, $y - 1, $z + 1 ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x, $y - 1, $z - 1 ), new Vector3($x - 4, $y - 1, $z + 1 ), function ($v3, $level) {
$level = $params [0];
if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR) if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR)
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK);
}, $this->level); }, $this->level);
// Putting rails // Putting rails
BuildingUtils::fillCallback(new Vector3($x, $y, $z ), new Vector3($x - 4, $y, $z ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x, $y, $z ), new Vector3($x - 4, $y, $z ), function ($v3, $level, $level) {
$level = $params [0];
$random = $params [1];
if ($random->nextBoundedInt(3 ) !== 0) { if ($random->nextBoundedInt(3 ) !== 0) {
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::RAIL); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::RAIL);
$level->setBlockDataAt($v3->x, $v3->y, $v3->z, 1); $level->setBlockDataAt($v3->x, $v3->y, $v3->z, 1);
@ -171,15 +166,13 @@ class MineshaftPopulator extends AmountPopulator {
// First, filling everything blank. // First, filling everything blank.
BuildingUtils::fill($this->level, new Vector3($x - 1, $y, $z ), new Vector3($x + 1, $y + 2, $z + 4 )); BuildingUtils::fill($this->level, new Vector3($x - 1, $y, $z ), new Vector3($x + 1, $y + 2, $z + 4 ));
// Then, making sure the floor is solid. // Then, making sure the floor is solid.
BuildingUtils::fillCallback(new Vector3($x - 1, $y - 1, $z ), new Vector3($x + 1, $y - 1, $z + 4 ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x - 1, $y - 1, $z ), new Vector3($x + 1, $y - 1, $z + 4 ), function ($v3, $level) {
$level = $params [0];
if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR) if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR)
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK);
}, $this->level); }, $this->level);
// Putting rails // Putting rails
BuildingUtils::fillCallback(new Vector3($x, $y, $z ), new Vector3($x, $y, $z + 4 ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x, $y, $z ), new Vector3($x, $y, $z + 4 ), function ($v3, $level, $random) {
$level = $params [0];
$random = $params [1];
if ($random->nextBoundedInt(3 ) !== 0) { if ($random->nextBoundedInt(3 ) !== 0) {
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::RAIL); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::RAIL);
$level->setBlockDataAt($v3->x, $v3->y, $v3->z, 0); $level->setBlockDataAt($v3->x, $v3->y, $v3->z, 0);
@ -214,15 +207,13 @@ class MineshaftPopulator extends AmountPopulator {
// First, filling everything blank. // First, filling everything blank.
BuildingUtils::fill($this->level, new Vector3($x - 1, $y, $z ), new Vector3($x + 1, $y + 2, $z - 4 )); BuildingUtils::fill($this->level, new Vector3($x - 1, $y, $z ), new Vector3($x + 1, $y + 2, $z - 4 ));
// Then, making sure the floor is solid. // Then, making sure the floor is solid.
BuildingUtils::fillCallback(new Vector3($x - 1, $y - 1, $z ), new Vector3($x + 1, $y - 1, $z - 4 ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x - 1, $y - 1, $z ), new Vector3($x + 1, $y - 1, $z - 4 ), function ($v3, $level) {
$level = $params [0];
if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR) if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR)
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK);
}, $this->level); }, $this->level);
// Putting rails // Putting rails
BuildingUtils::fillCallback(new Vector3($x, $y, $z ), new Vector3($x, $y, $z - 4 ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x, $y, $z ), new Vector3($x, $y, $z - 4 ), function ($v3, $level, $random) {
$level = $params [0];
$random = $params [1];
if ($random->nextBoundedInt(3 ) !== 0) { if ($random->nextBoundedInt(3 ) !== 0) {
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::RAIL); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::RAIL);
$level->setBlockDataAt($v3->x, $v3->y, $v3->z, 0); $level->setBlockDataAt($v3->x, $v3->y, $v3->z, 0);
@ -291,8 +282,8 @@ class MineshaftPopulator extends AmountPopulator {
} }
// Then, making sure the floor is solid. // Then, making sure the floor is solid.
BuildingUtils::fillCallback(new Vector3($x + 1, $y - 1, $z - 1 ), new Vector3($x - 1, $y - 1, $z + 1 ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x + 1, $y - 1, $z - 1 ), new Vector3($x - 1, $y - 1, $z + 1 ), function ($v3, $level) {
$level = $params [0];
if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR) if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR)
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK);
}, $this->level); }, $this->level);
@ -372,32 +363,31 @@ class MineshaftPopulator extends AmountPopulator {
switch ($path) { switch ($path) {
case self::DIR_XPLUS : case self::DIR_XPLUS :
BuildingUtils::fill($this->level, new Vector3($x + $i, $y - $i - 1, $z - 2 ), new Vector3($x + $i, $y - $i + 3, $z + 2 ), Block::get(Block::AIR )); BuildingUtils::fill($this->level, new Vector3($x + $i, $y - $i - 1, $z - 2 ), new Vector3($x + $i, $y - $i + 3, $z + 2 ), Block::get(Block::AIR ));
BuildingUtils::fillCallback(new Vector3($x + $i, $y - $i - 2, $z - 2 ), new Vector3($x + $i, $y - $i - 2, $z + 2 ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x + $i, $y - $i - 2, $z - 2 ), new Vector3($x + $i, $y - $i - 2, $z + 2 ), function ($v3, $level) {
$level = $params [0];
if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR) if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR)
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK);
}, $this->level); }, $this->level);
break; break;
case self::DIR_XMIN : case self::DIR_XMIN :
BuildingUtils::fill($this->level, new Vector3($x - $i, $y - $i - 1, $z - 2 ), new Vector3($x - $i, $y - $i + 3, $z + 2 ), Block::get(Block::AIR )); BuildingUtils::fill($this->level, new Vector3($x - $i, $y - $i - 1, $z - 2 ), new Vector3($x - $i, $y - $i + 3, $z + 2 ), Block::get(Block::AIR ));
BuildingUtils::fillCallback(new Vector3($x - $i, $y - $i - 2, $z - 2 ), new Vector3($x - $i, $y - $i - 2, $z + 2 ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x - $i, $y - $i - 2, $z - 2 ), new Vector3($x - $i, $y - $i - 2, $z + 2 ), function ($v3, $level) {
$level = $params [0];
if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR) if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR)
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK);
}, $this->level); }, $this->level);
break; break;
case self::DIR_ZPLUS : case self::DIR_ZPLUS :
BuildingUtils::fill($this->level, new Vector3($x - 2, $y - $i - 1, $z + $i ), new Vector3($x + 2, $y - $i + 3, $z + $i ), Block::get(Block::AIR )); BuildingUtils::fill($this->level, new Vector3($x - 2, $y - $i - 1, $z + $i ), new Vector3($x + 2, $y - $i + 3, $z + $i ), Block::get(Block::AIR ));
BuildingUtils::fillCallback(new Vector3($x - 2, $y - $i - 2, $z + $i ), new Vector3($x + 2, $y - $i - 2, $z + $i ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x - 2, $y - $i - 2, $z + $i ), new Vector3($x + 2, $y - $i - 2, $z + $i ), function ($v3, $level) {
$level = $params [0];
if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR) if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR)
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK);
}, $this->level); }, $this->level);
break; break;
case self::DIR_ZMIN : case self::DIR_ZMIN :
BuildingUtils::fill($this->level, new Vector3($x - 2, $y - $i - 1, $z - $i ), new Vector3($x + 2, $y - $i + 3, $z - $i ), Block::get(Block::AIR )); BuildingUtils::fill($this->level, new Vector3($x - 2, $y - $i - 1, $z - $i ), new Vector3($x + 2, $y - $i + 3, $z - $i ), Block::get(Block::AIR ));
BuildingUtils::fillCallback(new Vector3($x - 2, $y - $i - 2, $z - $i ), new Vector3($x + 2, $y - $i - 2, $z - $i ), function ($v3, $params) { BuildingUtils::fillCallback(new Vector3($x - 2, $y - $i - 2, $z - $i ), new Vector3($x + 2, $y - $i - 2, $z - $i ), function ($v3, $level) {
$level = $params [0];
if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR) if ($level->getBlockIdAt($v3->x, $v3->y, $v3->z ) == Block::AIR)
$level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK); $level->setBlockIdAt($v3->x, $v3->y, $v3->z, Block::PLANK);
}, $this->level); }, $this->level);

View file

@ -17,13 +17,21 @@ use pocketmine\math\Vector3;
use pocketmine\level\generator\object\Tree; use pocketmine\level\generator\object\Tree;
use pocketmine\level\generator\normal\object\Tree as Tree2; use pocketmine\level\generator\normal\object\Tree as Tree2;
use pocketmine\level\generator\object\Object; use pocketmine\level\generator\object\Object;
use Ad5001\BetterGen\utils\BuildingUtils;
class FallenTree extends Object { class FallenTree extends Object {
public $overridable = [ public $overridable = [
Block::AIR => true, Block::AIR => true,
6 => true,
17 => true, 17 => true,
18 => true,
Block::DANDELION => true,
Block::POPPY => true,
Block::SNOW_LAYER => true, Block::SNOW_LAYER => true,
Block::LOG2 => true Block::LOG2 => true,
Block::LEAVES2 => true,
Block::CACTUS => true
]; ];
protected $tree; protected $tree;
protected $direction; protected $direction;
@ -51,6 +59,26 @@ class FallenTree extends Object {
public function canPlaceObject(ChunkManager $level, $x, $y, $z, Random $random) { public function canPlaceObject(ChunkManager $level, $x, $y, $z, Random $random) {
$randomHeight = round($random->nextBoundedInt(6) - 3); $randomHeight = round($random->nextBoundedInt(6) - 3);
$this->length = $this->tree->trunkHeight + $randomHeight; $this->length = $this->tree->trunkHeight + $randomHeight;
$this->direction = $random->nextBoundedInt(4);
switch($this->direction) {
case 0:
case 1:// Z+
if(in_array(false, BuildingUtils::fillCallback(new Vector3($x, $y, $z), new Vector3($x, $y, $z + $this->length), function($v3, $level) {
if(!in_array($level->getBlockIdAt($v3->x, $v3->y, $v3->z), \Ad5001\BetterGen\structure\FallenTree::$overridable)) return false;
}, $level))) {
return false;
}
break;
case 2:
case 3:
if(in_array(false, BuildingUtils::fillCallback(new Vector3($x, $y, $z), new Vector3($x + $this->length, $y, $z), function($v3, $level) {
if(!in_array($level->getBlockIdAt($v3->x, $v3->y, $v3->z), \Ad5001\BetterGen\structure\FallenTree::$overridable)) return false;
}, $level))) {
return false;
}
break;
}
return true;
} }
/* /*
@ -61,16 +89,25 @@ class FallenTree extends Object {
* @param $z int * @param $z int
*/ */
public function placeObject(ChunkManager $level, $x, $y, $z) { public function placeObject(ChunkManager $level, $x, $y, $z) {
$number = $random->nextBoundedInt(6); switch($this->direction) {
$pos = new Vector3($x, $y, $z); case 0:
$this->placeLeaf($pos->x, $pos->y, $pos->z, $level); $level->setBlockIdAt($x, $y, $z, $this->tree->trunkBlock);
for($i = 0; $i < $number; $i ++) { $level->setBlockDataAt($x, $y, $z, $this->tree->type);
$transfer = $random->nextBoolean (); $z += 2;
$direction = $random->nextBoundedInt(4); case 1:// Z+
$newPos = $pos->getSide($direction); BuildingUtils::fill($level, new Vector3($x, $y, $z), new Vector3($x, $y, $z + $this->length), Block::get($this->tree->trunkBlock, $this->tree->type + 4));
if ($transfer) break;
$pos = $newPos; case 2:
$this->placeLeaf($newPos->x, $newPos->y, $newPos->z, $level); $level->setBlockIdAt($x, $y, $z, $this->tree->trunkBlock);
$level->setBlockDataAt($x, $y, $z, $this->tree->type);
$x += 2;
case 3:
BuildingUtils::fill($level, new Vector3($x, $y, $z), new Vector3($x, $y, $z + $this->length), Block::get($this->tree->trunkBlock, $this->tree->type + 4));
break;
}
// Second call to build the last wood block
switch($this->direction) {
} }
} }

View file

@ -51,15 +51,17 @@ class BuildingUtils {
* @param $pos2 pocketmine\math\Vector3 * @param $pos2 pocketmine\math\Vector3
* @param $call callback * @param $call callback
* @param $params array * @param $params array
* @return void * @return array
*/ */
public static function fillCallback(Vector3 $pos1, Vector3 $pos2, callable $call, ...$params) { public static function fillCallback(Vector3 $pos1, Vector3 $pos2, callable $call, ...$params) : array {
list($pos1, $pos2 ) = self::minmax($pos1, $pos2); list($pos1, $pos2 ) = self::minmax($pos1, $pos2);
$return = [];
for($x = $pos1->x; $x >= $pos2->x; $x --) for($x = $pos1->x; $x >= $pos2->x; $x --)
for($y = $pos1->y; $y >= $pos2->y; $y --) for($y = $pos1->y; $y >= $pos2->y; $y --)
for($z = $pos1->z; $z >= $pos2->z; $z --) { for($z = $pos1->z; $z >= $pos2->z; $z --) {
call_user_func($call, new Vector3($x, $y, $z ), $params); $return[] = call_user_func($call, new Vector3($x, $y, $z ), ...$params);
} }
return $return;
} }
/* /*