PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
SubChunk.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\format;
25
27 public const COORD_BIT_SIZE = 4;
28 public const COORD_MASK = ~(~0 << self::COORD_BIT_SIZE);
29 public const EDGE_LENGTH = 1 << self::COORD_BIT_SIZE;
30
34 public function __construct(
35 private int $emptyBlockId,
36 private ?PalettedBlockArray $blockLayer0,
37 private ?PalettedBlockArray $blockLayer1,
38 private PalettedBlockArray $biomes,
39 private ?LightArray $skyLight = null,
40 private ?LightArray $blockLight = null
41 ){}
42
48 public function isEmptyAuthoritative() : bool{
49 $this->collectGarbage();
50 return $this->isEmptyFast();
51 }
52
57 public function isEmptyFast() : bool{
58 return $this->blockLayer0 === null && $this->blockLayer1 === null;
59 }
60
65 public function getEmptyBlockId() : int{ return $this->emptyBlockId; }
66
67 public function getBlockStateId(int $x, int $y, int $z) : int{
68 return $this->blockLayer0?->get($x, $y, $z) ?? $this->emptyBlockId;
69 }
70
71 public function setBlockStateId(int $x, int $y, int $z, int $block) : void{
72 if($this->blockLayer0 === null){
73 $this->blockLayer0 = new PalettedBlockArray($this->emptyBlockId);
74 }
75 $this->blockLayer0->set($x, $y, $z, $block);
76 }
77
78 public function getBlockLayer0() : ?PalettedBlockArray{
79 return $this->blockLayer0;
80 }
81
82 public function getBlockLayer1() : ?PalettedBlockArray{
83 return $this->blockLayer1;
84 }
85
90 public function getBlockLayers() : array{
91 $layers = [];
92 if($this->blockLayer0 !== null){
93 $layers[] = $this->blockLayer0;
94 }
95 if($this->blockLayer1 !== null){
96 $layers[] = $this->blockLayer1;
97 }
98 return $layers;
99 }
100
101 public function getHighestBlockAt(int $x, int $z) : ?int{
102 if($this->blockLayer0 === null){
103 return null;
104 }
105 for($y = self::EDGE_LENGTH - 1; $y >= 0; --$y){
106 if($this->blockLayer0->get($x, $y, $z) !== $this->emptyBlockId){
107 return $y;
108 }
109 }
110
111 return null; //highest block not in this subchunk
112 }
113
114 public function getBiomeArray() : PalettedBlockArray{ return $this->biomes; }
115
116 public function getBlockSkyLightArray() : LightArray{
117 return $this->skyLight ??= LightArray::fill(0);
118 }
119
120 public function setBlockSkyLightArray(LightArray $data) : void{
121 $this->skyLight = $data;
122 }
123
124 public function getBlockLightArray() : LightArray{
125 return $this->blockLight ??= LightArray::fill(0);
126 }
127
128 public function setBlockLightArray(LightArray $data) : void{
129 $this->blockLight = $data;
130 }
131
135 public function __debugInfo() : array{
136 return [];
137 }
138
139 private static function gcBlockPalette(?PalettedBlockArray $layer, int $emptyBlockId) : ?PalettedBlockArray{
140 if($layer === null){
141 return null;
142 }
143 $layer->collectGarbage();
144 return $layer->getBitsPerBlock() === 0 && $layer->get(0, 0, 0) === $emptyBlockId ? null : $layer;
145 }
146
147 public function collectGarbage() : void{
148 $this->blockLayer0 = self::gcBlockPalette($this->blockLayer0, $this->emptyBlockId);
149 $this->blockLayer1 = self::gcBlockPalette($this->blockLayer1, $this->emptyBlockId);
150 if($this->blockLayer0 === null && $this->blockLayer1 !== null){
151 $this->blockLayer0 = $this->blockLayer1;
152 $this->blockLayer1 = null;
153 }
154 $this->biomes->collectGarbage();
155
156 if($this->skyLight !== null && $this->skyLight->isUniform(0)){
157 $this->skyLight = null;
158 }
159 if($this->blockLight !== null && $this->blockLight->isUniform(0)){
160 $this->blockLight = null;
161 }
162 }
163
164 public function __clone(){
165 $this->blockLayer0 = $this->blockLayer0 !== null ? clone $this->blockLayer0 : null;
166 $this->blockLayer1 = $this->blockLayer1 !== null ? clone $this->blockLayer1 : null;
167 $this->biomes = clone $this->biomes;
168
169 if($this->skyLight !== null){
170 $this->skyLight = clone $this->skyLight;
171 }
172 if($this->blockLight !== null){
173 $this->blockLight = clone $this->blockLight;
174 }
175 }
176}
__construct(private int $emptyBlockId, private ?PalettedBlockArray $blockLayer0, private ?PalettedBlockArray $blockLayer1, private PalettedBlockArray $biomes, private ?LightArray $skyLight=null, private ?LightArray $blockLight=null)
Definition SubChunk.php:34