BetterGen/src/Ad5001/BetterGen/populator/FallenTreePopulator.php

81 lines
2.4 KiB
PHP
Raw Normal View History

2017-04-29 09:59:44 +00:00
<?php
/**
2017-05-11 13:12:17 +00:00
* ____ __ __ ____
* /\ _`\ /\ \__ /\ \__ /\ _`\
* \ \ \L\ \ __ \ \ ,_\\ \ ,_\ __ _ __ \ \ \L\_\ __ ___
* \ \ _ <' /'__`\\ \ \/ \ \ \/ /'__`\/\`'__\\ \ \L_L /'__`\ /' _ `\
* \ \ \L\ \/\ __/ \ \ \_ \ \ \_ /\ __/\ \ \/ \ \ \/, \/\ __/ /\ \/\ \
* \ \____/\ \____\ \ \__\ \ \__\\ \____\\ \_\ \ \____/\ \____\\ \_\ \_\
* \/___/ \/____/ \/__/ \/__/ \/____/ \/_/ \/___/ \/____/ \/_/\/_/
* Tomorrow's pocketmine generator.
* @author Ad5001
* @link https://github.com/Ad5001/BetterGen
2017-04-29 09:59:44 +00:00
*/
namespace Ad5001\BetterGen\populator;
2017-05-11 12:07:26 +00:00
use Ad5001\BetterGen\structure\FallenTree;
2017-04-29 09:59:44 +00:00
use pocketmine\block\Block;
use pocketmine\level\ChunkManager;
use pocketmine\level\Level;
2017-04-29 09:59:44 +00:00
use pocketmine\utils\Random;
2017-05-11 05:46:44 +00:00
2017-04-29 09:59:44 +00:00
class FallenTreePopulator extends AmountPopulator {
/** @var ChunkManager */
2017-04-29 09:59:44 +00:00
protected $level;
protected $type;
2017-04-29 09:59:44 +00:00
/*
* Constructs the class
* @param $type int
*/
public function __construct(int $type = 0) {
2017-05-11 05:46:44 +00:00
$this->type = $type;
2017-04-29 09:59:44 +00:00
$this->setBaseAmount(1);
$this->setRandomAmount(2);
}
2017-05-11 13:12:17 +00:00
2017-04-29 09:59:44 +00:00
/*
* 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;
$amount = $this->getAmount($random);
$tree = TreePopulator::$types[$this->type];
2017-05-11 13:12:17 +00:00
$fallenTree = new \Ad5001\BetterGen\structure\FallenTree(
new $tree()
);
2017-04-29 09:59:44 +00:00
for($i = 0; $i < $amount; $i++) {
$x = $random->nextRange($chunkX * 16, $chunkX * 16 + 15);
$z = $random->nextRange($chunkZ * 16, $chunkZ * 16 + 15);
$y = $this->getHighestWorkableBlock($x, $z);
if(isset(FallenTree::$overridable[$level->getBlockIdAt($x, $y, $z)])) $y--; // Changing $y if 1 block to high.
2017-05-13 21:27:10 +00:00
if ($y !== -1 and $fallenTree->canPlaceObject($level, $x, $y + 1, $z, $random)) {
$fallenTree->placeObject($level, $x, $y + 1, $z);
2017-04-29 09:59:44 +00:00
}
}
}
2017-05-11 13:12:17 +00:00
/**
2017-04-29 09:59:44 +00:00
* Gets the top block (y) on an x and z axes
2017-05-11 13:12:17 +00:00
* @param $x
* @param $z
* @return int
2017-04-29 09:59:44 +00:00
*/
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-05-11 14:41:42 +00:00
if($b === Block::DIRT or $b === Block::GRASS){
2017-04-29 09:59:44 +00:00
break;
2017-05-11 14:41:42 +00:00
}elseif($b !== Block::AIR and $b !== Block::SNOW_LAYER){
return -1;
2017-04-29 09:59:44 +00:00
}
}
2017-05-11 14:41:42 +00:00
return ++$y;
2017-04-29 09:59:44 +00:00
}
}