PocketMine-MP 5.21.2 git-09bf203267aca9b83a09640e17bfac0fc7b58ff8
Loading...
Searching...
No Matches
AcaciaTree.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\world\generator\object;
25
31use function abs;
32use function array_rand;
33
34final class AcaciaTree extends Tree{
35 private const MIN_HEIGHT = 5;
36
37 private ?Vector3 $mainBranchTip = null;
38 private ?Vector3 $secondBranchTip = null;
39
40 public function __construct(){
41 parent::__construct(
42 VanillaBlocks::ACACIA_LOG(),
43 VanillaBlocks::ACACIA_LEAVES(),
44 0 //we don't use this anyway - everything is overridden
45 );
46 }
47
48 protected function generateTrunkHeight(Random $random) : int{
49 //50% chance of 2 extra blocks, 33% chance 1 or 3, 17% chance 0 or 4
50 return self::MIN_HEIGHT + $random->nextRange(0, 2) + $random->nextRange(0, 2);
51 }
52
53 protected function placeTrunk(int $x, int $y, int $z, Random $random, int $trunkHeight, BlockTransaction $transaction) : void{
54 // The base dirt block
55 $transaction->addBlockAt($x, $y - 1, $z, VanillaBlocks::DIRT());
56
57 $firstBranchHeight = $trunkHeight - 1 - $random->nextRange(0, 3);
58
59 for($yy = 0; $yy <= $firstBranchHeight; ++$yy){
60 $transaction->addBlockAt($x, $y + $yy, $z, $this->trunkBlock);
61 }
62
63 $mainBranchFacing = Facing::HORIZONTAL[array_rand(Facing::HORIZONTAL)];
64
65 //this branch may grow a second trunk if the diagonal length is less than the max length
66 $this->mainBranchTip = $this->placeBranch(
67 $transaction,
68 new Vector3($x, $y + $firstBranchHeight, $z),
69 $mainBranchFacing,
70 $random->nextRange(1, 3),
71 $trunkHeight - $firstBranchHeight
72 );
73
74 $secondBranchFacing = Facing::HORIZONTAL[array_rand(Facing::HORIZONTAL)];
75 if($secondBranchFacing !== $mainBranchFacing){
76 $secondBranchLength = $random->nextRange(1, 3);
77 $this->secondBranchTip = $this->placeBranch(
78 $transaction,
79 new Vector3($x, $y + ($firstBranchHeight - $random->nextRange(0, 1)), $z),
80 $secondBranchFacing,
81 $secondBranchLength,
82 $secondBranchLength //the secondary branch may not form a second trunk
83 );
84 }
85 }
86
87 protected function placeBranch(BlockTransaction $transaction, Vector3 $start, int $branchFacing, int $maxDiagonal, int $length) : Vector3{
88 $diagonalPlaced = 0;
89
90 $nextBlockPos = $start;
91 for($yy = 0; $yy < $length; $yy++){
92 $nextBlockPos = $nextBlockPos->up();
93 if($diagonalPlaced < $maxDiagonal){
94 $nextBlockPos = $nextBlockPos->getSide($branchFacing);
95 $diagonalPlaced++;
96 }
97 $transaction->addBlock($nextBlockPos, $this->trunkBlock);
98 }
99
100 return $nextBlockPos;
101 }
102
103 protected function placeCanopyLayer(BlockTransaction $transaction, Vector3 $center, int $radius, int $maxTaxicabDistance) : void{
104 $centerX = $center->getFloorX();
105 $centerY = $center->getFloorY();
106 $centerZ = $center->getFloorZ();
107
108 for($x = $centerX - $radius; $x <= $centerX + $radius; ++$x){
109 for($z = $centerZ - $radius; $z <= $centerZ + $radius; ++$z){
110 if(
111 abs($x - $centerX) + abs($z - $centerZ) <= $maxTaxicabDistance &&
112 $transaction->fetchBlockAt($x, $centerY, $z)->canBeReplaced()
113 ){
114 $transaction->addBlockAt($x, $centerY, $z, $this->leafBlock);
115 }
116 }
117 }
118 }
119
120 protected function placeCanopy(int $x, int $y, int $z, Random $random, BlockTransaction $transaction) : void{
121 $mainBranchTip = $this->mainBranchTip;
122 if($mainBranchTip !== null){
123 $this->placeCanopyLayer($transaction, $mainBranchTip, radius: 3, maxTaxicabDistance: 5);
124 $this->placeCanopyLayer($transaction, $mainBranchTip->up(), radius: 2, maxTaxicabDistance: 2);
125 }
126 $secondBranchTip = $this->secondBranchTip;
127 if($secondBranchTip !== null){
128 $this->placeCanopyLayer($transaction, $secondBranchTip, radius: 2, maxTaxicabDistance: 3);
129 $this->placeCanopyLayer($transaction, $secondBranchTip->up(), radius: 1, maxTaxicabDistance: 2);
130 }
131 }
132}
nextRange(int $start=0, int $end=0x7fffffff)
Definition Random.php:119
addBlock(Vector3 $pos, Block $state)
addBlockAt(int $x, int $y, int $z, Block $state)
fetchBlockAt(int $x, int $y, int $z)