mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-12 00:39:45 +00:00
extracted a HeightArray type from Chunk
This commit is contained in:
parent
a31240f60b
commit
88715c7055
@ -81,10 +81,7 @@ class Chunk{
|
|||||||
/** @var Entity[] */
|
/** @var Entity[] */
|
||||||
protected $entities = [];
|
protected $entities = [];
|
||||||
|
|
||||||
/**
|
/** @var HeightArray */
|
||||||
* @var \SplFixedArray|int[]
|
|
||||||
* @phpstan-var \SplFixedArray<int>
|
|
||||||
*/
|
|
||||||
protected $heightMap;
|
protected $heightMap;
|
||||||
|
|
||||||
/** @var BiomeArray */
|
/** @var BiomeArray */
|
||||||
@ -100,9 +97,8 @@ class Chunk{
|
|||||||
* @param SubChunk[] $subChunks
|
* @param SubChunk[] $subChunks
|
||||||
* @param CompoundTag[] $entities
|
* @param CompoundTag[] $entities
|
||||||
* @param CompoundTag[] $tiles
|
* @param CompoundTag[] $tiles
|
||||||
* @param int[] $heightMap
|
|
||||||
*/
|
*/
|
||||||
public function __construct(int $chunkX, int $chunkZ, array $subChunks = [], ?array $entities = null, ?array $tiles = null, ?BiomeArray $biomeIds = null, array $heightMap = []){
|
public function __construct(int $chunkX, int $chunkZ, array $subChunks = [], ?array $entities = null, ?array $tiles = null, ?BiomeArray $biomeIds = null, ?HeightArray $heightMap = null){
|
||||||
$this->x = $chunkX;
|
$this->x = $chunkX;
|
||||||
$this->z = $chunkZ;
|
$this->z = $chunkZ;
|
||||||
|
|
||||||
@ -112,14 +108,8 @@ class Chunk{
|
|||||||
$this->subChunks[$y] = $subChunks[$y] ?? new SubChunk(BlockLegacyIds::AIR << 4, []);
|
$this->subChunks[$y] = $subChunks[$y] ?? new SubChunk(BlockLegacyIds::AIR << 4, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(count($heightMap) === 256){
|
$val = ($this->subChunks->getSize() * 16);
|
||||||
$this->heightMap = \SplFixedArray::fromArray($heightMap);
|
$this->heightMap = $heightMap ?? new HeightArray(array_fill(0, 256, $val));
|
||||||
}else{
|
|
||||||
assert(count($heightMap) === 0, "Wrong HeightMap value count, expected 256, got " . count($heightMap));
|
|
||||||
$val = ($this->subChunks->getSize() * 16);
|
|
||||||
$this->heightMap = \SplFixedArray::fromArray(array_fill(0, 256, $val));
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->biomeIds = $biomeIds ?? new BiomeArray(str_repeat("\x00", 256));
|
$this->biomeIds = $biomeIds ?? new BiomeArray(str_repeat("\x00", 256));
|
||||||
|
|
||||||
$this->NBTtiles = $tiles;
|
$this->NBTtiles = $tiles;
|
||||||
@ -258,7 +248,7 @@ class Chunk{
|
|||||||
* @param int $z 0-15
|
* @param int $z 0-15
|
||||||
*/
|
*/
|
||||||
public function getHeightMap(int $x, int $z) : int{
|
public function getHeightMap(int $x, int $z) : int{
|
||||||
return $this->heightMap[($z << 4) | $x];
|
return $this->heightMap->get($x, $z);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -268,7 +258,7 @@ class Chunk{
|
|||||||
* @param int $z 0-15
|
* @param int $z 0-15
|
||||||
*/
|
*/
|
||||||
public function setHeightMap(int $x, int $z, int $value) : void{
|
public function setHeightMap(int $x, int $z, int $value) : void{
|
||||||
$this->heightMap[($z << 4) | $x] = $value;
|
$this->heightMap->set($x, $z, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -552,18 +542,14 @@ class Chunk{
|
|||||||
* @return int[]
|
* @return int[]
|
||||||
*/
|
*/
|
||||||
public function getHeightMapArray() : array{
|
public function getHeightMapArray() : array{
|
||||||
return $this->heightMap->toArray();
|
return $this->heightMap->getValues();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int[] $values
|
* @param int[] $values
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*/
|
*/
|
||||||
public function setHeightMapArray(array $values) : void{
|
public function setHeightMapArray(array $values) : void{
|
||||||
if(count($values) !== 256){
|
$this->heightMap = new HeightArray($values);
|
||||||
throw new \InvalidArgumentException("Expected exactly 256 values");
|
|
||||||
}
|
|
||||||
$this->heightMap = \SplFixedArray::fromArray($values);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isDirty() : bool{
|
public function isDirty() : bool{
|
||||||
|
73
src/world/format/HeightArray.php
Normal file
73
src/world/format/HeightArray.php
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\world\format;
|
||||||
|
|
||||||
|
use function count;
|
||||||
|
|
||||||
|
final class HeightArray{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \SplFixedArray|int[]
|
||||||
|
* @phpstan-var \SplFixedArray<int>
|
||||||
|
*/
|
||||||
|
private $array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int[] $values ZZZZXXXX key bit order
|
||||||
|
* @phpstan-param list<int> $values
|
||||||
|
*/
|
||||||
|
public function __construct(array $values){
|
||||||
|
if(count($values) !== 256){
|
||||||
|
throw new \InvalidArgumentException("Expected exactly 256 values");
|
||||||
|
}
|
||||||
|
$this->array = \SplFixedArray::fromArray($values);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function idx(int $x, int $z) : int{
|
||||||
|
if($x < 0 or $x >= 16 or $z < 0 or $z >= 16){
|
||||||
|
throw new \InvalidArgumentException("x and z must be in the range 0-15");
|
||||||
|
}
|
||||||
|
return ($z << 4) | $x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(int $x, int $z) : int{
|
||||||
|
return $this->array[self::idx($x, $z)];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set(int $x, int $z, int $height) : void{
|
||||||
|
$this->array[self::idx($x, $z)] = $height;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int[] ZZZZXXXX key bit order
|
||||||
|
* @phpstan-return list<int>
|
||||||
|
*/
|
||||||
|
public function getValues() : array{
|
||||||
|
return $this->array->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __clone(){
|
||||||
|
$this->array = clone $this->array;
|
||||||
|
}
|
||||||
|
}
|
@ -27,6 +27,7 @@ use pocketmine\block\BlockLegacyIds;
|
|||||||
use pocketmine\utils\BinaryStream;
|
use pocketmine\utils\BinaryStream;
|
||||||
use pocketmine\world\format\BiomeArray;
|
use pocketmine\world\format\BiomeArray;
|
||||||
use pocketmine\world\format\Chunk;
|
use pocketmine\world\format\Chunk;
|
||||||
|
use pocketmine\world\format\HeightArray;
|
||||||
use pocketmine\world\format\LightArray;
|
use pocketmine\world\format\LightArray;
|
||||||
use pocketmine\world\format\PalettedBlockArray;
|
use pocketmine\world\format\PalettedBlockArray;
|
||||||
use pocketmine\world\format\SubChunk;
|
use pocketmine\world\format\SubChunk;
|
||||||
@ -120,7 +121,7 @@ final class FastChunkSerializer{
|
|||||||
|
|
||||||
$subChunks = [];
|
$subChunks = [];
|
||||||
$biomeIds = null;
|
$biomeIds = null;
|
||||||
$heightMap = [];
|
$heightMap = null;
|
||||||
if($terrainGenerated){
|
if($terrainGenerated){
|
||||||
$count = $stream->getByte();
|
$count = $stream->getByte();
|
||||||
for($subCount = 0; $subCount < $count; ++$subCount){
|
for($subCount = 0; $subCount < $count; ++$subCount){
|
||||||
@ -142,7 +143,7 @@ final class FastChunkSerializer{
|
|||||||
|
|
||||||
$biomeIds = new BiomeArray($stream->get(256));
|
$biomeIds = new BiomeArray($stream->get(256));
|
||||||
if($lightPopulated){
|
if($lightPopulated){
|
||||||
$heightMap = array_values(unpack("S*", $stream->get(512)));
|
$heightMap = new HeightArray(array_values(unpack("S*", $stream->get(512))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user