BetterGen/src/Ad5001/BetterGen/structure/SakuraTree.php

690 lines
10 KiB
PHP
Raw Normal View History

2017-04-23 14:42:51 +00:00
<?php
/**
2017-05-11 12:07:26 +00:00
* ____ __ __ ____
* /\ _`\ /\ \__ /\ \__ /\ _`\
* \ \ \L\ \ __ \ \ ,_\\ \ ,_\ __ _ __ \ \ \L\_\ __ ___
* \ \ _ <' /'__`\\ \ \/ \ \ \/ /'__`\/\`'__\\ \ \L_L /'__`\ /' _ `\
* \ \ \L\ \/\ __/ \ \ \_ \ \ \_ /\ __/\ \ \/ \ \ \/, \/\ __/ /\ \/\ \
* \ \____/\ \____\ \ \__\ \ \__\\ \____\\ \_\ \ \____/\ \____\\ \_\ \_\
* \/___/ \/____/ \/__/ \/__/ \/____/ \/_/ \/___/ \/____/ \/_/\/_/
* Tomorrow's pocketmine generator.
2017-05-14 18:13:15 +00:00
* @author Ad5001 <mail@ad5001.eu>, XenialDan <https://github.com/thebigsmileXD>
* @link https://github.com/Ad5001/BetterGen
2017-05-14 18:13:15 +00:00
* @category World Generator
* @api 3.0.0
* @version 1.1
2017-04-23 14:42:51 +00:00
*/
namespace Ad5001\BetterGen\structure;
2017-05-11 12:07:26 +00:00
use Ad5001\BetterGen\Main;
2017-04-23 14:42:51 +00:00
use pocketmine\block\Block;
use pocketmine\block\Wood;
use pocketmine\level\ChunkManager;
2017-05-11 12:07:26 +00:00
use pocketmine\level\generator\object\Tree;
2017-04-23 14:42:51 +00:00
use pocketmine\utils\Random;
2017-05-11 12:07:26 +00:00
if (Main::isOtherNS()) {
class_alias("pocketmine\\level\\generator\\normal\\object\\Tree", "Ad5001\\BetterGen\\structure\\Tree");
} else {
class_alias("pocketmine\\level\\generator\\object\\Tree", "Ad5001\\BetterGen\\structure\\Tree");
}
2017-04-23 14:42:51 +00:00
class SakuraTree extends Tree {
const TRUNK_POS = [ // Checks for trees trunks. Not automatically generated but there is no point of making more or less
2017-05-11 12:07:26 +00:00
7 => [
0,
1
], // 0 vertical, 1 horizontal and same goes for others...
8 => [
1,
0
],
9 => [
1,
1
],
10 => [
2,
0
],
11 => [
2,
1
],
12 => [
2,
2
],
13 => [
3,
1
]
2017-04-23 14:42:51 +00:00
];
const DIAG_LEAVES = [ // Diag poses of the leaves based on the height of the tree.X relative to $lastX and Z from $lastZ
2017-05-11 12:07:26 +00:00
7 => [
[
4,
4
],
[
-4,
4
],
[
4,
-4
],
[
-4,
-4
]
],
8 => [
[
6,
6
],
[
-6,
6
],
[
6,
-6
],
[
-6,
-6
]
],
9 => [
[
6,
6
],
[
-6,
6
],
[
6,
-6
],
[
-6,
-6
]
],
10 => [
[
6,
6
],
[
-6,
6
],
[
6,
-6
],
[
-6,
-6
]
],
11 => [
[
7,
7
],
[
6,
8
],
[
8,
6
],
[
-7,
7
],
[
-6,
8
],
[
-8,
6
],
[
7,
-7
],
[
6,
-8
],
[
8,
-6
],
[
-7,
-7
],
[
-6,
-8
],
[
-8,
-6
]
],
12 => [
[
7,
7
],
[
6,
8
],
[
8,
6
],
[
-7,
7
],
[
-6,
8
],
[
-8,
6
],
[
7,
-7
],
[
6,
-8
],
[
8,
-6
],
[
-7,
-7
],
[
-6,
-8
],
[
-8,
-6
]
],
13 => [
[
7,
7
],
[
6,
8
],
[
8,
6
],
[
-7,
7
],
[
-6,
8
],
[
-8,
6
],
[
7,
-7
],
[
6,
-8
],
[
8,
-6
],
[
-7,
-7
],
[
-6,
-8
],
[
-8,
-6
]
]
2017-04-23 14:42:51 +00:00
];
const ADDITIONAL_BLOCKS = [ // Blocks who aren't set to fully fill the tree. X relative to $lastX and Z from $lastZ
2017-05-11 12:07:26 +00:00
7 => [],
8 => [],
9 => [],
10 => [],
11 => [
[
6,
6
],
[
7,
6
],
[
6,
7
],
[
-6,
6
],
[
-7,
6
],
[
-6,
7
],
[
6,
-6
],
[
7,
-6
],
[
6,
-7
],
[
-6,
-6
],
[
-7,
-6
],
[
-6,
-7
]
],
12 => [
[
6,
6
],
[
7,
6
],
[
6,
7
],
[
-6,
6
],
[
-7,
6
],
[
-6,
7
],
[
6,
-6
],
[
7,
-6
],
[
6,
-7
],
[
-6,
-6
],
[
-7,
-6
],
[
-6,
-7
]
],
13 => [
[
6,
6
],
[
7,
6
],
[
6,
7
],
[
-6,
6
],
[
-7,
6
],
[
-6,
7
],
[
6,
-6
],
[
7,
-6
],
[
6,
-7
],
[
-6,
-6
],
[
-7,
-6
],
[
-6,
-7
]
]
2017-04-23 14:42:51 +00:00
];
const maxPerChunk = 2;
public $trunkHeight = 11;
public $leafType;
public $leaf2Type;
2017-05-11 12:07:26 +00:00
2017-04-23 14:42:51 +00:00
/*
* Constructs the class
*/
public function __construct() {
$this->trunkBlock = Block::LOG;
2017-05-11 19:24:28 +00:00
$this->leafBlock = Block::AIR; // To remove bushes
$this->realLeafBlock = Block::WOOL;
2017-04-23 14:42:51 +00:00
$this->leafType = 6;
$this->leaf2Type = 0;
$this->type = Wood::OAK;
}
2017-05-11 12:07:26 +00:00
2017-04-23 14:42:51 +00:00
/*
* Builds the tree.
* @param $level \pocketmine\level\ChunkManager
* @param $x int
* @param $y int
* @param $z int
* @param $random $random
*/
public function placeObject(ChunkManager $level, $x, $y, $z, Random $random) {
2017-04-29 09:59:44 +00:00
$percentage = $random->nextBoundedInt(100);
2017-04-23 14:42:51 +00:00
if ($percentage > 10) {
return;
}
2017-04-29 09:59:44 +00:00
$trunkHeight = 7 + $random->nextBoundedInt(7);
2017-05-11 12:07:26 +00:00
$xDiff = $zDiff = 0;
2017-04-29 09:59:44 +00:00
$direction = $random->nextBoundedInt(3); // Choosing building north east west south
2017-04-23 14:42:51 +00:00
switch ($direction) {
case 0 :
$xDiff = 0;
2017-04-29 09:59:44 +00:00
$zDiff = -1;
2017-04-23 14:42:51 +00:00
break;
case 1 :
$xDiff = 0;
$zDiff = 1;
break;
case 2 :
2017-04-29 09:59:44 +00:00
$xDiff = -1;
2017-04-23 14:42:51 +00:00
$zDiff = 0;
break;
case 3 :
$xDiff = 1;
$zDiff = 0;
break;
}
2017-05-13 21:42:44 +00:00
list($vParts, $hParts) = self::TRUNK_POS[$trunkHeight];
2017-05-11 12:07:26 +00:00
2017-04-29 09:59:44 +00:00
$this->setLog($level, $x, $y, $z);
2017-05-11 12:07:26 +00:00
list($lastX, $lastY, $lastZ) = [
$x,
$y,
$z
2017-04-23 14:42:51 +00:00
];
2017-05-11 12:07:26 +00:00
// Filling horizontally
2017-04-23 14:42:51 +00:00
if ($hParts > 0) {
2017-05-11 12:07:26 +00:00
for ($i = 0; $i < $hParts; $i++) {
2017-04-23 14:42:51 +00:00
$lastX += $xDiff * 2;
2017-05-11 12:07:26 +00:00
$lastY++;
2017-04-23 14:42:51 +00:00
$lastZ += $zDiff * 2;
2017-04-29 09:59:44 +00:00
$this->setLog($level, $lastX - $xDiff, $lastY, $lastZ - $zDiff);
$this->setLog($level, $lastX, $lastY, $lastZ);
2017-04-23 14:42:51 +00:00
}
}
2017-05-11 12:07:26 +00:00
2017-04-23 14:42:51 +00:00
// The middle block
$lastX += $xDiff;
2017-05-11 12:07:26 +00:00
$lastY++;
2017-04-23 14:42:51 +00:00
$lastZ += $zDiff;
2017-04-29 09:59:44 +00:00
$this->setLog($level, $lastX, $lastY, $lastZ);
2017-05-11 12:07:26 +00:00
// Filling vertically
2017-04-23 14:42:51 +00:00
if ($vParts > 0) {
2017-05-11 12:07:26 +00:00
for ($i = 0; $i < $vParts; $i++) {
2017-04-23 14:42:51 +00:00
$lastX += $xDiff;
$lastY += 2;
$lastZ += $zDiff;
2017-04-29 09:59:44 +00:00
$this->setLog($level, $lastX, $lastY - 1, $lastZ);
$this->setLog($level, $lastX, $lastY, $lastZ);
2017-04-23 14:42:51 +00:00
}
}
// Filling the top trunk.
$lastX += $xDiff;
2017-05-11 12:07:26 +00:00
$lastY++;
2017-04-23 14:42:51 +00:00
$lastZ += $zDiff;
2017-05-11 12:07:26 +00:00
for ($i = 0; $i < 4; $i++) {
2017-04-29 09:59:44 +00:00
$this->setLog($level, $lastX, $lastY + $i, $lastZ);
2017-04-23 14:42:51 +00:00
}
2017-05-11 12:07:26 +00:00
$lastY++;
// Filling the branches.
2017-04-23 14:42:51 +00:00
$branchLen2 = function ($base) {
2017-04-29 09:59:44 +00:00
return ceil($base / 2);
2017-04-23 14:42:51 +00:00
};
2017-05-11 12:07:26 +00:00
$xd = $zd = 0;
for ($dir = 0; $dir < 4; $dir++) {
2017-04-23 14:42:51 +00:00
switch ($dir) {
case 0 :
$xd = 0;
2017-04-29 09:59:44 +00:00
$zd = -1;
2017-04-23 14:42:51 +00:00
break;
case 1 :
$xd = 0;
$zd = 1;
break;
case 2 :
2017-04-29 09:59:44 +00:00
$xd = -1;
2017-04-23 14:42:51 +00:00
$zd = 0;
break;
case 3 :
$xd = 1;
$zd = 0;
break;
}
2017-05-11 12:07:26 +00:00
2017-04-29 09:59:44 +00:00
$stickLen = round($trunkHeight / 3);
$stickLen2 = call_user_func($branchLen2, $stickLen);
2017-04-23 14:42:51 +00:00
$totalLength = $stickLen + $stickLen2; // Length of the stick
$sideLen = $totalLength ** 2; // Side length
2017-05-11 12:07:26 +00:00
//TODO CHECK WHAT THIS IS SUPPOSED TO BE
2017-05-11 14:41:42 +00:00
$numForward = ($totalLength % 2 == 0) ? $totalLength - 1 : $totalLength;
2017-05-11 12:07:26 +00:00
//TODO END
$lX1 = $lZ1 = $lX = $lZ = 0;
2017-04-23 14:42:51 +00:00
// First branch part + first leave part
2017-05-11 12:07:26 +00:00
for ($i = 1; $i < $stickLen + 1; $i++) {
2017-04-23 14:42:51 +00:00
$lX1 = $lastX + ($xd * $i);
$lZ1 = $lastZ + ($zd * $i);
if ($zd !== 0)
2017-05-11 12:07:26 +00:00
for ($x = $lX1 - $numForward; $x !== $lX1 + $numForward + 1; $x++) {
2017-04-29 09:59:44 +00:00
$this->setLeave($level, $x, $lastY + 3, $lZ1, $random);
2017-04-23 14:42:51 +00:00
}
if ($xd !== 0)
2017-05-11 12:07:26 +00:00
for ($z = $lZ1 - $numForward; $z !== $lZ1 + $numForward + 1; $z++) {
2017-04-29 09:59:44 +00:00
$this->setLeave($level, $lX1, $lastY + 3, $z, $random);
2017-04-23 14:42:51 +00:00
}
2017-04-29 09:59:44 +00:00
$this->setLog($level, $lX1, $lastY, $lZ1);
2017-04-23 14:42:51 +00:00
}
2017-05-11 12:07:26 +00:00
2017-04-23 14:42:51 +00:00
// Second branch part. + second leave part
2017-05-11 12:07:26 +00:00
for ($i = 1; $i < $stickLen + 1; $i++) {
2017-04-23 14:42:51 +00:00
$lX = $lX1 + ($xd * $i);
$lZ = $lZ1 + ($zd * $i);
if ($zd !== 0)
2017-05-11 12:07:26 +00:00
for ($x = $lX - $numForward; $x !== $lX + $numForward + 1; $x++) {
2017-04-29 09:59:44 +00:00
$this->setLeave($level, $x, $lastY + 2, $lZ, $random);
2017-04-23 14:42:51 +00:00
}
if ($xd !== 0)
2017-05-11 12:07:26 +00:00
for ($z = $lZ - $numForward; $z !== $lZ + $numForward + 1; $z++) {
2017-04-29 09:59:44 +00:00
$this->setLeave($level, $lX, $lastY + 2, $z, $random);
2017-04-23 14:42:51 +00:00
}
2017-04-29 09:59:44 +00:00
$this->setLog($level, $lX, $lastY + 1, $lZ);
2017-04-23 14:42:51 +00:00
}
2017-05-11 12:07:26 +00:00
2017-04-23 14:42:51 +00:00
$lX += $xd;
$lZ += $zd;
// Leaves falling from the tree forward
if ($lastZ !== $lZ) { // Z has changed, using X for setting
2017-05-11 12:07:26 +00:00
for ($x = $lX - $numForward; $x <= $lX + $numForward; $x++) {
$numDown = $random->nextBoundedInt(3) + 1;
for ($y = $lastY + 1; $y > $lastY - $numDown; $y--)
2017-04-29 09:59:44 +00:00
$this->setLeave($level, $x, $y, $lZ, $random);
2017-04-23 14:42:51 +00:00
}
} else { // Z have stayed, X has changed
2017-05-11 12:07:26 +00:00
for ($z = $lZ - $numForward; $z <= $lZ + $numForward; $z++) {
$numDown = $random->nextBoundedInt(3) + 1;
for ($y = $lastY + 1; $y > $lastY + 1 - $numDown; $y--)
2017-04-29 09:59:44 +00:00
$this->setLeave($level, $lX, $y, $z, $random);
2017-04-23 14:42:51 +00:00
}
}
2017-05-11 12:07:26 +00:00
2017-04-23 14:42:51 +00:00
// continue;
2017-05-11 12:07:26 +00:00
2017-04-23 14:42:51 +00:00
switch ($dir + 1) {
case 4 :
$xd2 = 0;
2017-04-29 09:59:44 +00:00
$zd2 = -1;
2017-04-23 14:42:51 +00:00
break;
case 1 :
$xd2 = 0;
$zd2 = 1;
break;
case 2 :
2017-04-29 09:59:44 +00:00
$xd2 = -1;
2017-04-23 14:42:51 +00:00
$zd2 = 0;
break;
case 3 :
$xd2 = 1;
$zd2 = 0;
break;
}
2017-05-11 12:07:26 +00:00
// Leaves falling from the tree diagonally
2017-05-13 21:42:44 +00:00
foreach (self::DIAG_LEAVES[$trunkHeight] as $pos) {
2017-05-11 12:07:26 +00:00
$numDown = $random->nextBoundedInt(3) + 1;
for ($y = $lastY + 1; $y > $lastY - $numDown; $y--)
2017-05-13 21:42:44 +00:00
$this->setLeave($level, $lastX + $pos[0], $y, $lastZ + $pos[1], $random);
2017-04-23 14:42:51 +00:00
}
2017-05-11 12:07:26 +00:00
2017-04-23 14:42:51 +00:00
// Additional leaves
2017-05-13 21:42:44 +00:00
foreach (self::ADDITIONAL_BLOCKS[$trunkHeight] as $pos) {
$this->setLeave($level, $lastX + $pos[0], $lastY + 2, $lastZ + $pos[1], $random);
2017-04-23 14:42:51 +00:00
}
}
}
2017-05-11 12:07:26 +00:00
2017-04-23 14:42:51 +00:00
/*
* Fills a log at.
* @param $level pocketmine\level\ChunkManager
* @param $x int
* @param $y int
* @param $z int
*/
public function setLog(ChunkManager $level, $x, $y, $z) {
2017-04-29 09:59:44 +00:00
$level->setBlockIdAt($x, $y, $z, $this->trunkBlock);
$level->setBlockDataAt($x, $y, $z, $this->type);
2017-04-23 14:42:51 +00:00
}
2017-05-11 12:07:26 +00:00
2017-04-23 14:42:51 +00:00
/*
* Fills a leave at.
* @param $level pocketmine\level\ChunkManager
* @param $x int
* @param $y int
* @param $z int
* @param $random pocketmine\utils\Random
*/
public function setLeave(ChunkManager $level, $x, $y, $z, Random $random) {
2017-05-11 12:07:26 +00:00
$data = [
$this->leafType,
$this->leaf2Type
2017-05-13 21:27:10 +00:00
] [( int)$random->nextBoolean()];
2017-05-11 19:24:28 +00:00
$level->setBlockIdAt($x, $y, $z, $this->realLeafBlock);
2017-04-29 09:59:44 +00:00
$level->setBlockDataAt($x, $y, $z, $data);
2017-04-23 14:42:51 +00:00
}
}