31 public const COORD_BIT_SIZE = 4;
32 public const COORD_MASK = ~(~0 << self::COORD_BIT_SIZE);
33 public const EDGE_LENGTH = 1 << self::COORD_BIT_SIZE;
41 private int $emptyBlockId,
42 private array $blockLayers,
43 private PalettedBlockArray $biomes,
44 private ?LightArray $skyLight =
null,
45 private ?LightArray $blockLight =
null
54 $this->collectGarbage();
63 return count($this->blockLayers) === 0;
72 public function getBlockStateId(
int $x,
int $y,
int $z) : int{
73 if(count($this->blockLayers) === 0){
74 return $this->emptyBlockId;
76 return $this->blockLayers[0]->get($x, $y, $z);
79 public function setBlockStateId(
int $x,
int $y,
int $z,
int $block) : void{
80 if(count($this->blockLayers) === 0){
81 $this->blockLayers[] =
new PalettedBlockArray($this->emptyBlockId);
83 $this->blockLayers[0]->set($x, $y, $z, $block);
90 return $this->blockLayers;
93 public function getHighestBlockAt(
int $x,
int $z) : ?int{
94 if(count($this->blockLayers) === 0){
97 for($y = self::EDGE_LENGTH - 1; $y >= 0; --$y){
98 if($this->blockLayers[0]->
get($x, $y, $z) !== $this->emptyBlockId){
106 public function getBiomeArray() : PalettedBlockArray{ return $this->biomes; }
108 public function getBlockSkyLightArray() : LightArray{
109 return $this->skyLight ??= LightArray::fill(0);
112 public function setBlockSkyLightArray(LightArray $data) : void{
113 $this->skyLight = $data;
116 public function getBlockLightArray() : LightArray{
117 return $this->blockLight ??= LightArray::fill(0);
120 public function setBlockLightArray(LightArray $data) : void{
121 $this->blockLight = $data;
131 public function collectGarbage() : void{
132 foreach($this->blockLayers as $k => $layer){
133 $layer->collectGarbage();
135 foreach($layer->getPalette() as $p){
136 if($p !== $this->emptyBlockId){
140 unset($this->blockLayers[$k]);
142 $this->blockLayers = array_values($this->blockLayers);
143 $this->biomes->collectGarbage();
145 if($this->skyLight !==
null && $this->skyLight->isUniform(0)){
146 $this->skyLight =
null;
148 if($this->blockLight !==
null && $this->blockLight->isUniform(0)){
149 $this->blockLight =
null;
153 public function __clone(){
154 $this->blockLayers = array_map(
function(PalettedBlockArray $array) : PalettedBlockArray{
156 }, $this->blockLayers);
157 $this->biomes = clone $this->biomes;
159 if($this->skyLight !==
null){
160 $this->skyLight = clone $this->skyLight;
162 if($this->blockLight !==
null){
163 $this->blockLight = clone $this->blockLight;