Chunk: simplify heightmap calculation

This commit is contained in:
Dylan K. Taylor 2020-09-08 23:03:52 +01:00
parent c7070788f9
commit 03de2bcc67
5 changed files with 16 additions and 15 deletions

View File

@ -260,12 +260,10 @@ class Chunk{
/** /**
* Recalculates the heightmap for the whole chunk. * Recalculates the heightmap for the whole chunk.
* *
* @param \SplFixedArray|int[] $lightFilters * @param \SplFixedArray|bool[] $directSkyLightBlockers
* @param \SplFixedArray|bool[] $lightDiffusers * @phpstan-param \SplFixedArray<bool> $directSkyLightBlockers
* @phpstan-param \SplFixedArray<int> $lightFilters
* @phpstan-param \SplFixedArray<bool> $lightDiffusers
*/ */
public function recalculateHeightMap(\SplFixedArray $lightFilters, \SplFixedArray $lightDiffusers) : void{ public function recalculateHeightMap(\SplFixedArray $directSkyLightBlockers) : void{
$maxSubChunkY = $this->subChunks->count() - 1; $maxSubChunkY = $this->subChunks->count() - 1;
for(; $maxSubChunkY >= 0; $maxSubChunkY--){ for(; $maxSubChunkY >= 0; $maxSubChunkY--){
if(!$this->getSubChunk($maxSubChunkY)->isEmptyFast()){ if(!$this->getSubChunk($maxSubChunkY)->isEmptyFast()){
@ -292,7 +290,7 @@ class Chunk{
$this->setHeightMap($x, $z, 0); $this->setHeightMap($x, $z, 0);
}else{ }else{
for(; $y >= 0; --$y){ for(; $y >= 0; --$y){
if($lightFilters[$state = $this->getFullBlock($x, $y, $z)] > 1 or $lightDiffusers[$state]){ if($directSkyLightBlockers[$this->getFullBlock($x, $y, $z)]){
$this->setHeightMap($x, $z, $y + 1); $this->setHeightMap($x, $z, $y + 1);
break; break;
} }
@ -307,17 +305,15 @@ class Chunk{
* *
* @param int $x 0-15 * @param int $x 0-15
* @param int $z 0-15 * @param int $z 0-15
* @param \SplFixedArray|int[] $lightFilters * @param \SplFixedArray|bool[] $directSkyLightBlockers
* @param \SplFixedArray|bool[] $lightDiffusers * @phpstan-param \SplFixedArray<bool> $directSkyLightBlockers
* @phpstan-param \SplFixedArray<int> $lightFilters
* @phpstan-param \SplFixedArray<bool> $lightDiffusers
* *
* @return int New calculated heightmap value (0-256 inclusive) * @return int New calculated heightmap value (0-256 inclusive)
*/ */
public function recalculateHeightMapColumn(int $x, int $z, \SplFixedArray $lightFilters, \SplFixedArray $lightDiffusers) : int{ public function recalculateHeightMapColumn(int $x, int $z, \SplFixedArray $directSkyLightBlockers) : int{
$y = $this->getHighestBlockAt($x, $z); $y = $this->getHighestBlockAt($x, $z);
for(; $y >= 0; --$y){ for(; $y >= 0; --$y){
if($lightFilters[$state = $this->getFullBlock($x, $y, $z)] > 1 or $lightDiffusers[$state]){ if($directSkyLightBlockers[$this->getFullBlock($x, $y, $z)]){
break; break;
} }
} }

View File

@ -119,7 +119,7 @@ class PopulationTask extends AsyncTask{
$chunk->setPopulated(); $chunk->setPopulated();
$blockFactory = BlockFactory::getInstance(); $blockFactory = BlockFactory::getInstance();
$chunk->recalculateHeightMap($blockFactory->lightFilter, $blockFactory->blocksDirectSkyLight); $chunk->recalculateHeightMap($blockFactory->blocksDirectSkyLight);
$chunk->populateSkyLight($blockFactory->lightFilter); $chunk->populateSkyLight($blockFactory->lightFilter);
$chunk->setLightPopulated(); $chunk->setLightPopulated();

View File

@ -61,7 +61,7 @@ class LightPopulationTask extends AsyncTask{
$chunk = FastChunkSerializer::deserialize($this->chunk); $chunk = FastChunkSerializer::deserialize($this->chunk);
$blockFactory = BlockFactory::getInstance(); $blockFactory = BlockFactory::getInstance();
$chunk->recalculateHeightMap($blockFactory->lightFilter, $blockFactory->blocksDirectSkyLight); $chunk->recalculateHeightMap($blockFactory->blocksDirectSkyLight);
$chunk->populateSkyLight($blockFactory->lightFilter); $chunk->populateSkyLight($blockFactory->lightFilter);
$chunk->setLightPopulated(); $chunk->setLightPopulated();

View File

@ -70,7 +70,7 @@ class SkyLightUpdate extends LightUpdate{
$yPlusOne = $y + 1; $yPlusOne = $y + 1;
if($yPlusOne === $oldHeightMap){ //Block changed directly beneath the heightmap. Check if a block was removed or changed to a different light-filter. if($yPlusOne === $oldHeightMap){ //Block changed directly beneath the heightmap. Check if a block was removed or changed to a different light-filter.
$newHeightMap = $chunk->recalculateHeightMapColumn($x & 0x0f, $z & 0x0f, $this->lightFilters, $this->directSkyLightBlockers); $newHeightMap = $chunk->recalculateHeightMapColumn($x & 0x0f, $z & 0x0f, $this->directSkyLightBlockers);
}elseif($yPlusOne > $oldHeightMap){ //Block changed above the heightmap. }elseif($yPlusOne > $oldHeightMap){ //Block changed above the heightmap.
if($this->directSkyLightBlockers[$source]){ if($this->directSkyLightBlockers[$source]){
$chunk->setHeightMap($x & 0xf, $z & 0xf, $yPlusOne); $chunk->setHeightMap($x & 0xf, $z & 0xf, $yPlusOne);

View File

@ -755,6 +755,11 @@ parameters:
count: 1 count: 1
path: ../../../src/world/biome/BiomeRegistry.php path: ../../../src/world/biome/BiomeRegistry.php
-
message: "#^Only booleans are allowed in an if condition, bool\\|null given\\.$#"
count: 2
path: ../../../src/world/format/Chunk.php
- -
message: "#^Method pocketmine\\\\world\\\\format\\\\Chunk\\:\\:getSubChunk\\(\\) should return pocketmine\\\\world\\\\format\\\\SubChunkInterface but returns pocketmine\\\\world\\\\format\\\\SubChunk\\|null\\.$#" message: "#^Method pocketmine\\\\world\\\\format\\\\Chunk\\:\\:getSubChunk\\(\\) should return pocketmine\\\\world\\\\format\\\\SubChunkInterface but returns pocketmine\\\\world\\\\format\\\\SubChunk\\|null\\.$#"
count: 1 count: 1