mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-13 21:35:21 +00:00
Chunk: Use an SplFixedArray for subchunks
This commit is contained in:
parent
8bc733514b
commit
49301b0d74
@ -53,7 +53,7 @@ class Chunk{
|
|||||||
|
|
||||||
protected $height = Chunk::MAX_SUBCHUNKS;
|
protected $height = Chunk::MAX_SUBCHUNKS;
|
||||||
|
|
||||||
/** @var SubChunkInterface[] */
|
/** @var \SplFixedArray|SubChunkInterface[] */
|
||||||
protected $subChunks = [];
|
protected $subChunks = [];
|
||||||
|
|
||||||
/** @var EmptySubChunk */
|
/** @var EmptySubChunk */
|
||||||
@ -97,23 +97,11 @@ class Chunk{
|
|||||||
|
|
||||||
$this->height = Chunk::MAX_SUBCHUNKS; //TODO: add a way of changing this
|
$this->height = Chunk::MAX_SUBCHUNKS; //TODO: add a way of changing this
|
||||||
|
|
||||||
|
$this->subChunks = new \SplFixedArray($this->height);
|
||||||
$this->emptySubChunk = new EmptySubChunk();
|
$this->emptySubChunk = new EmptySubChunk();
|
||||||
|
|
||||||
foreach($subChunks as $y => $subChunk){
|
foreach($this->subChunks as $y => $null){
|
||||||
if($y < 0 or $y >= $this->height){
|
$this->subChunks[$y] = $subChunks[$y] ?? $this->emptySubChunk;
|
||||||
throw new ChunkException("Invalid subchunk index $y!");
|
|
||||||
}
|
|
||||||
if($subChunk->isEmpty()){
|
|
||||||
$this->subChunks[$y] = $this->emptySubChunk;
|
|
||||||
}else{
|
|
||||||
$this->subChunks[$y] = $subChunk;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for($i = 0; $i < $this->height; ++$i){
|
|
||||||
if(!isset($this->subChunks[$i])){
|
|
||||||
$this->subChunks[$i] = $this->emptySubChunk;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(count($heightMap) === 256){
|
if(count($heightMap) === 256){
|
||||||
@ -843,7 +831,7 @@ class Chunk{
|
|||||||
}elseif($generateNew and $this->subChunks[$y] instanceof EmptySubChunk){
|
}elseif($generateNew and $this->subChunks[$y] instanceof EmptySubChunk){
|
||||||
$this->subChunks[$y] = new SubChunk();
|
$this->subChunks[$y] = new SubChunk();
|
||||||
}
|
}
|
||||||
assert($this->subChunks[$y] !== null, "Somehow something broke, no such subchunk at index $y");
|
|
||||||
return $this->subChunks[$y];
|
return $this->subChunks[$y];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -870,9 +858,9 @@ class Chunk{
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return SubChunk[]
|
* @return \SplFixedArray|SubChunkInterface[]
|
||||||
*/
|
*/
|
||||||
public function getSubChunks() : array{
|
public function getSubChunks() : \SplFixedArray{
|
||||||
return $this->subChunks;
|
return $this->subChunks;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -882,8 +870,8 @@ class Chunk{
|
|||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getHighestSubChunkIndex() : int{
|
public function getHighestSubChunkIndex() : int{
|
||||||
for($y = count($this->subChunks) - 1; $y >= 0; --$y){
|
for($y = $this->subChunks->count() - 1; $y >= 0; --$y){
|
||||||
if($this->subChunks[$y] === null or $this->subChunks[$y] instanceof EmptySubChunk){
|
if($this->subChunks[$y] instanceof EmptySubChunk){
|
||||||
//No need to thoroughly prune empties at runtime, this will just reduce performance.
|
//No need to thoroughly prune empties at runtime, this will just reduce performance.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -907,10 +895,7 @@ class Chunk{
|
|||||||
*/
|
*/
|
||||||
public function pruneEmptySubChunks(){
|
public function pruneEmptySubChunks(){
|
||||||
foreach($this->subChunks as $y => $subChunk){
|
foreach($this->subChunks as $y => $subChunk){
|
||||||
if($y < 0 or $y >= $this->height){
|
if($subChunk instanceof EmptySubChunk){
|
||||||
assert(false, "Invalid subchunk index");
|
|
||||||
unset($this->subChunks[$y]);
|
|
||||||
}elseif($subChunk instanceof EmptySubChunk){
|
|
||||||
continue;
|
continue;
|
||||||
}elseif($subChunk->isEmpty()){ //normal subchunk full of air, remove it and replace it with an empty stub
|
}elseif($subChunk->isEmpty()){ //normal subchunk full of air, remove it and replace it with an empty stub
|
||||||
$this->subChunks[$y] = $this->emptySubChunk;
|
$this->subChunks[$y] = $this->emptySubChunk;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user