PocketMine-MP 5.39.3 git-9a46a8bd745880ddf8eebaf28cda326bb97d2efa
Loading...
Searching...
No Matches
AzaleaTree.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
28use pocketmine\block\utils\DirtType;
36use function count;
37use function max;
38use function min;
39
40final class AzaleaTree extends Tree{
42 private array $foliageAttachments = [];
43
44 private const TREE_HEIGHT_BASE = 4;
45 private const TREE_HEIGHT_RANDOM = 3;
46 private const MIN_HEIGHT_FOR_LEAVES = 3;
47
48 private const LEADUP_SMALL = 2;
49 private const LEADUP_LARGE = 3;
50 private const SIDE_UP_STEPS = 2;
51
52 public function __construct(){
53 parent::__construct(VanillaBlocks::OAK_LOG(), VanillaBlocks::AZALEA_LEAVES(), 0);
54 }
55
56 public function getBlockTransaction(ChunkManager $world, int $x, int $y, int $z, Random $random) : ?BlockTransaction{
57 $this->treeHeight = $random->nextBoundedInt(self::TREE_HEIGHT_RANDOM) + self::TREE_HEIGHT_BASE;
58 $this->foliageAttachments = [];
59 return parent::getBlockTransaction($world, $x, $y, $z, $random);
60 }
61
62 protected function generateTrunkHeight(Random $random) : int{
63 return min(self::TREE_HEIGHT_BASE + $random->nextBoundedInt(2), 5);
64 }
65
66 protected function placeTrunk(int $x, int $y, int $z, Random $random, int $trunkHeight, BlockTransaction $transaction) : void{
67 $transaction->addBlockAt($x, $y - 1, $z, VanillaBlocks::DIRT()->setDirtType(DirtType::ROOTED));
68
69 $direction = Facing::HORIZONTAL[$random->nextRange(0, count(Facing::HORIZONTAL) - 1)];
70 $cx = $x;
71 $cy = $y;
72 $cz = $z;
73
74 $sideUpCount = min(self::SIDE_UP_STEPS, max(0, $trunkHeight - self::LEADUP_SMALL));
75 $leadUpCount = min($trunkHeight - $sideUpCount, self::LEADUP_LARGE);
76 $total = $leadUpCount + $sideUpCount + 1;
77 if($total < $trunkHeight){
78 $leadUpCount += ($trunkHeight - $total);
79 }
80
81 for($i = 0; $i < $total; ++$i){
82 $isLeadUp = $i < $leadUpCount;
83 $isSideUp = $i < $leadUpCount + $sideUpCount - 1;
84
85 if(!$isLeadUp){
86 $cx += Facing::OFFSET[$direction->value][0];
87 $cz += Facing::OFFSET[$direction->value][2];
88 }
89
90 if($this->canOverride($transaction->fetchBlockAt($cx, $cy, $cz))){
91 $transaction->addBlockAt($cx, $cy, $cz, $this->trunkBlock);
92 }
93
94 if($i >= self::MIN_HEIGHT_FOR_LEAVES){
95 $this->foliageAttachments[] = new Vector3($cx, $cy, $cz);
96 }
97
98 if($isLeadUp || $isSideUp){
99 $cy++;
100 }
101 }
102 }
103
104 protected function placeCanopy(int $x, int $y, int $z, Random $random, BlockTransaction $transaction) : void{
105 $radius = 3;
106 $foliageHeight = 2;
107 $attempts = 50;
108
109 $visited = [];
110 foreach($this->foliageAttachments as $attachment){
111 $centerX = $attachment->getFloorX();
112 $centerY = $attachment->getFloorY();
113 $centerZ = $attachment->getFloorZ();
114
115 for($a = 0; $a < $attempts; ++$a){
116 $dx = $random->nextBoundedInt($radius) - $random->nextBoundedInt($radius);
117 $dy = $random->nextBoundedInt($foliageHeight) - $random->nextBoundedInt($foliageHeight);
118 $dz = $random->nextBoundedInt($radius) - $random->nextBoundedInt($radius);
119
120 $xx = $centerX + $dx;
121 $yy = $centerY + $dy;
122 $zz = $centerZ + $dz;
123
124 $hash = World::blockHash($xx, $yy, $zz);
125 if(isset($visited[$hash])){
126 continue;
127 }
128 $visited[$hash] = true;
129
130 $existing = $transaction->fetchBlockAt($xx, $yy, $zz);
131 if($existing->isTransparent()){
132 $leafBlock = ($random->nextBoundedInt(4) === 0) ? VanillaBlocks::FLOWERING_AZALEA_LEAVES() : VanillaBlocks::AZALEA_LEAVES();
133 $transaction->addBlockAt($xx, $yy, $zz, $leafBlock);
134 }
135 }
136 }
137 }
138
139 protected function canOverride(Block $block) : bool{
140 return parent::canOverride($block)
141 || $block->getTypeId() === BlockTypeIds::AZALEA
142 || $block->getTypeId() === BlockTypeIds::FLOWERING_AZALEA;
143 }
144}
nextRange(int $start=0, int $end=0x7fffffff)
Definition Random.php:119
addBlockAt(int $x, int $y, int $z, Block $state)
fetchBlockAt(int $x, int $y, int $z)
getBlockTransaction(ChunkManager $world, int $x, int $y, int $z, Random $random)