Improved nibble array re-ordering in most cases by checking for common values

This commit is contained in:
Dylan K. Taylor 2017-01-17 12:49:11 +00:00
parent 162b993e65
commit 7de7593b89
2 changed files with 21 additions and 12 deletions

View File

@ -35,11 +35,14 @@ class ChunkUtils{
public static final function reorderByteArray(string $array) : string{ public static final function reorderByteArray(string $array) : string{
$result = str_repeat("\x00", 4096); $result = str_repeat("\x00", 4096);
$i = 0; $i = 0;
$zM = 0;
$yM = 0;
for($x = 0; $x < 16; ++$x){ for($x = 0; $x < 16; ++$x){
for($z = 0; $z < 256; $z += 16){ $zM = $x + 256;
$zx = ($z + $x); for($z = $x; $z < $zM; $z += 16){
for($y = 0; $y < 4096; $y += 256){ $yM = $z + 4096;
$result{$i} = $array{$y + $zx}; for($y = $z; $y < $yM; $y += 256){
$result{$i} = $array{$y};
++$i; ++$i;
} }
} }
@ -51,21 +54,27 @@ class ChunkUtils{
* Re-orders a nibble array (YZX -> XZY and vice versa) * Re-orders a nibble array (YZX -> XZY and vice versa)
* *
* @param string $array length 2048 * @param string $array length 2048
* @param string $commonValue length 1 common value to fill the default array with and to expect, may improve sort time
* *
* @return string length 2048 * @return string length 2048
*/ */
public static final function reorderNibbleArray(string $array) : string{ public static final function reorderNibbleArray(string $array, string $commonValue = "\x00") : string{
$result = str_repeat("\x00", 2048); $result = str_repeat($commonValue, 2048);
$i = 0; $i = 0;
for($x = 0; $x < 8; ++$x){ for($x = 0; $x < 8; ++$x){
for($z = 0; $z < 16; ++$z){ for($z = 0; $z < 16; ++$z){
$zx = (($z << 3) | $x); $zx = (($z << 3) | $x);
for($y = 0; $y < 8; ++$y){ for($y = 0; $y < 8; ++$y){
$j = (($y << 8) | $zx); $j = (($y << 8) | $zx);
$i1 = ord($array{$j}); $j80 = ($j | 0x80);
$i2 = ord($array{$j | 0x80}); if($array{$j} === $commonValue and $array{$j80} === $commonValue){
$result{$i} = chr(($i2 << 4) | ($i1 & 0x0f)); //values are already filled
$result{$i | 0x80} = chr(($i1 >> 4) | ($i2 & 0xf0)); }else{
$i1 = ord($array{$j});
$i2 = ord($array{$j80});
$result{$i} = chr(($i2 << 4) | ($i1 & 0x0f));
$result{$i | 0x80} = chr(($i1 >> 4) | ($i2 & 0xf0));
}
$i++; $i++;
} }
} }

View File

@ -60,7 +60,7 @@ class Anvil extends McRegion{
"Y" => new ByteTag("Y", $y), "Y" => new ByteTag("Y", $y),
"Blocks" => new ByteArrayTag("Blocks", ChunkUtils::reorderByteArray($subChunk->getBlockIdArray())), //Generic in-memory chunks are currently always XZY "Blocks" => new ByteArrayTag("Blocks", ChunkUtils::reorderByteArray($subChunk->getBlockIdArray())), //Generic in-memory chunks are currently always XZY
"Data" => new ByteArrayTag("Data", ChunkUtils::reorderNibbleArray($subChunk->getBlockDataArray())), "Data" => new ByteArrayTag("Data", ChunkUtils::reorderNibbleArray($subChunk->getBlockDataArray())),
"SkyLight" => new ByteArrayTag("SkyLight", ChunkUtils::reorderNibbleArray($subChunk->getSkyLightArray())), "SkyLight" => new ByteArrayTag("SkyLight", ChunkUtils::reorderNibbleArray($subChunk->getSkyLightArray(), "\xff")),
"BlockLight" => new ByteArrayTag("BlockLight", ChunkUtils::reorderNibbleArray($subChunk->getBlockLightArray())) "BlockLight" => new ByteArrayTag("BlockLight", ChunkUtils::reorderNibbleArray($subChunk->getBlockLightArray()))
]); ]);
} }
@ -118,7 +118,7 @@ class Anvil extends McRegion{
$subChunks[$subChunk->Y->getValue()] = new SubChunk( $subChunks[$subChunk->Y->getValue()] = new SubChunk(
ChunkUtils::reorderByteArray($subChunk->Blocks->getValue()), ChunkUtils::reorderByteArray($subChunk->Blocks->getValue()),
ChunkUtils::reorderNibbleArray($subChunk->Data->getValue()), ChunkUtils::reorderNibbleArray($subChunk->Data->getValue()),
ChunkUtils::reorderNibbleArray($subChunk->SkyLight->getValue()), ChunkUtils::reorderNibbleArray($subChunk->SkyLight->getValue(), "\xff"),
ChunkUtils::reorderNibbleArray($subChunk->BlockLight->getValue()) ChunkUtils::reorderNibbleArray($subChunk->BlockLight->getValue())
); );
} }