Support 256-block build height and fixed world saving

This commit is contained in:
Dylan K. Taylor
2016-12-01 23:44:25 +00:00
parent ce289cbe25
commit 577dbbce1f
5 changed files with 71 additions and 53 deletions

View File

@@ -116,6 +116,8 @@ class Level implements ChunkManager, Metadatable{
private static $chunkLoaderCounter = 1;
public static $COMPRESSION_LEVEL = 8;
const Y_MASK = 0xFF;
const Y_MAX = 0x100; //256
const BLOCK_UPDATE_NORMAL = 1;
const BLOCK_UPDATE_RANDOM = 2;
@@ -265,17 +267,17 @@ class Level implements ChunkManager, Metadatable{
}
public static function blockHash(int $x, int $y, int $z){
return PHP_INT_SIZE === 8 ? (($x & 0xFFFFFFF) << 35) | (($y & 0x7f) << 28) | ($z & 0xFFFFFFF) : $x . ":" . $y . ":" . $z;
return PHP_INT_SIZE === 8 ? (($x & 0xFFFFFFF) << 36) | (($y & Level::Y_MASK) << 28) | ($z & 0xFFFFFFF) : $x . ":" . $y . ":" . $z;
}
public static function chunkBlockHash(int $x, int $y, int $z) : int{
return ($x << 11) | ($z << 7) | $y;
return ($x << 12) | ($z << 8) | $y;
}
public static function getBlockXYZ($hash, &$x, &$y, &$z){
if(PHP_INT_SIZE === 8){
$x = ($hash >> 35) << 36 >> 36;
$y = (($hash >> 28) & 0x7f);// << 57 >> 57; //it's always positive
$x = $hash >> 36;
$y = $hash >> 28; //it's always positive
$z = ($hash & 0xFFFFFFF) << 36 >> 36;
}else{
$hash = explode(":", $hash);
@@ -287,7 +289,7 @@ class Level implements ChunkManager, Metadatable{
public static function getXZ($hash, &$x, &$z){
if(PHP_INT_SIZE === 8){
$x = ($hash >> 32) << 32 >> 32;
$x = $hash >> 32;
$z = ($hash & 0xFFFFFFFF) << 32 >> 32;
}else{
$hash = explode(":", $hash);
@@ -1204,10 +1206,10 @@ class Level implements ChunkManager, Metadatable{
$chunk = $this->getChunk($pos->x >> 4, $pos->z >> 4, false);
$level = 0;
if($chunk !== null){
$level = $chunk->getBlockSkyLight($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f);
$level = $chunk->getBlockSkyLight($pos->x & 0x0f, $pos->y & Level::Y_MASK, $pos->z & 0x0f);
//TODO: decrease light level by time of day
if($level < 15){
$level = max($chunk->getBlockLight($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f));
$level = max($chunk->getBlockLight($pos->x & 0x0f, $pos->y & Level::Y_MASK, $pos->z & 0x0f));
}
}
@@ -1222,7 +1224,7 @@ class Level implements ChunkManager, Metadatable{
* @return int bitmap, (id << 4) | data
*/
public function getFullBlock(int $x, int $y, int $z) : int{
return $this->getChunk($x >> 4, $z >> 4, false)->getFullBlock($x & 0x0f, $y & 0x7f, $z & 0x0f);
return $this->getChunk($x >> 4, $z >> 4, false)->getFullBlock($x & 0x0f, $y & Level::Y_MASK, $z & 0x0f);
}
/**
@@ -1238,8 +1240,8 @@ class Level implements ChunkManager, Metadatable{
$index = Level::blockHash($pos->x, $pos->y, $pos->z);
if($cached and isset($this->blockCache[$index])){
return $this->blockCache[$index];
}elseif($pos->y >= 0 and $pos->y < 128 and isset($this->chunks[$chunkIndex = Level::chunkHash($pos->x >> 4, $pos->z >> 4)])){
$fullState = $this->chunks[$chunkIndex]->getFullBlock($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f);
}elseif($pos->y >= 0 and $pos->y < $this->provider->getWorldHeight() and isset($this->chunks[$chunkIndex = Level::chunkHash($pos->x >> 4, $pos->z >> 4)])){
$fullState = $this->chunks[$chunkIndex]->getFullBlock($pos->x & 0x0f, $pos->y & Level::Y_MASK, $pos->z & 0x0f);
}else{
$fullState = 0;
}
@@ -1370,11 +1372,11 @@ class Level implements ChunkManager, Metadatable{
*/
public function setBlock(Vector3 $pos, Block $block, bool $direct = false, bool $update = true) : bool{
$pos = $pos->floor();
if($pos->y < 0 or $pos->y >= 128){
if($pos->y < 0 or $pos->y >= $this->provider->getWorldHeight()){
return false;
}
if($this->getChunk($pos->x >> 4, $pos->z >> 4, true)->setBlock($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f, $block->getId(), $block->getDamage())){
if($this->getChunk($pos->x >> 4, $pos->z >> 4, true)->setBlock($pos->x & 0x0f, $pos->y & Level::Y_MASK, $pos->z & 0x0f, $block->getId(), $block->getDamage())){
if(!($pos instanceof Position)){
$pos = $this->temporalPosition->setComponents($pos->x, $pos->y, $pos->z);
}
@@ -1611,7 +1613,8 @@ class Level implements ChunkManager, Metadatable{
$target = $this->getBlock($vector);
$block = $target->getSide($face);
if($block->y > 127 or $block->y < 0){
if($block->y >= $this->provider->getWorldHeight() or $block->y < 0){
//TODO: build height limit messages for custom world heights and mcregion cap
return false;
}
@@ -1859,7 +1862,7 @@ class Level implements ChunkManager, Metadatable{
$chunk = $this->getChunk($pos->x >> 4, $pos->z >> 4, false);
if($chunk !== null){
return $chunk->getTile($pos->x & 0x0f, $pos->y & 0xff, $pos->z & 0x0f);
return $chunk->getTile($pos->x & 0x0f, $pos->y & Level::Y_MASK, $pos->z & 0x0f);
}
return null;
@@ -1899,7 +1902,7 @@ class Level implements ChunkManager, Metadatable{
* @return int 0-255
*/
public function getBlockIdAt(int $x, int $y, int $z) : int{
return $this->getChunk($x >> 4, $z >> 4, true)->getBlockId($x & 0x0f, $y & 0x7f, $z & 0x0f);
return $this->getChunk($x >> 4, $z >> 4, true)->getBlockId($x & 0x0f, $y & Level::Y_MASK, $z & 0x0f);
}
/**
@@ -1912,7 +1915,7 @@ class Level implements ChunkManager, Metadatable{
*/
public function setBlockIdAt(int $x, int $y, int $z, int $id){
unset($this->blockCache[Level::blockHash($x, $y, $z)]);
$this->getChunk($x >> 4, $z >> 4, true)->setBlockId($x & 0x0f, $y & 0x7f, $z & 0x0f, $id & 0xff);
$this->getChunk($x >> 4, $z >> 4, true)->setBlockId($x & 0x0f, $y & Level::Y_MASK, $z & 0x0f, $id & 0xff);
if(!isset($this->changedBlocks[$index = Level::chunkHash($x >> 4, $z >> 4)])){
$this->changedBlocks[$index] = [];
@@ -1933,7 +1936,7 @@ class Level implements ChunkManager, Metadatable{
* @return int 16-bit
*/
public function getBlockExtraDataAt(int $x, int $y, int $z) : int{
return $this->getChunk($x >> 4, $z >> 4, true)->getBlockExtraData($x & 0x0f, $y & 0x7f, $z & 0x0f);
return $this->getChunk($x >> 4, $z >> 4, true)->getBlockExtraData($x & 0x0f, $y & Level::Y_MASK, $z & 0x0f);
}
/**
@@ -1946,7 +1949,7 @@ class Level implements ChunkManager, Metadatable{
* @param int $data
*/
public function setBlockExtraDataAt(int $x, int $y, int $z, int $id, int $data){
$this->getChunk($x >> 4, $z >> 4, true)->setBlockExtraData($x & 0x0f, $y & 0x7f, $z & 0x0f, ($data << 8) | $id);
$this->getChunk($x >> 4, $z >> 4, true)->setBlockExtraData($x & 0x0f, $y & Level::Y_MASK, $z & 0x0f, ($data << 8) | $id);
$this->sendBlockExtraData($x, $y, $z, $id, $data);
}
@@ -1961,7 +1964,7 @@ class Level implements ChunkManager, Metadatable{
* @return int 0-15
*/
public function getBlockDataAt(int $x, int $y, int $z) : int{
return $this->getChunk($x >> 4, $z >> 4, true)->getBlockData($x & 0x0f, $y & 0x7f, $z & 0x0f);
return $this->getChunk($x >> 4, $z >> 4, true)->getBlockData($x & 0x0f, $y & Level::Y_MASK, $z & 0x0f);
}
/**
@@ -1974,7 +1977,7 @@ class Level implements ChunkManager, Metadatable{
*/
public function setBlockDataAt(int $x, int $y, int $z, int $data){
unset($this->blockCache[Level::blockHash($x, $y, $z)]);
$this->getChunk($x >> 4, $z >> 4, true)->setBlockData($x & 0x0f, $y & 0x7f, $z & 0x0f, $data & 0x0f);
$this->getChunk($x >> 4, $z >> 4, true)->setBlockData($x & 0x0f, $y & Level::Y_MASK, $z & 0x0f, $data & 0x0f);
if(!isset($this->changedBlocks[$index = Level::chunkHash($x >> 4, $z >> 4)])){
$this->changedBlocks[$index] = [];
@@ -1995,7 +1998,7 @@ class Level implements ChunkManager, Metadatable{
* @return int 0-15
*/
public function getBlockSkyLightAt(int $x, int $y, int $z) : int{
return $this->getChunk($x >> 4, $z >> 4, true)->getBlockSkyLight($x & 0x0f, $y & 0x7f, $z & 0x0f);
return $this->getChunk($x >> 4, $z >> 4, true)->getBlockSkyLight($x & 0x0f, $y & Level::Y_MASK, $z & 0x0f);
}
/**
@@ -2007,7 +2010,7 @@ class Level implements ChunkManager, Metadatable{
* @param int $level 0-15
*/
public function setBlockSkyLightAt(int $x, int $y, int $z, int $level){
$this->getChunk($x >> 4, $z >> 4, true)->setBlockSkyLight($x & 0x0f, $y & 0x7f, $z & 0x0f, $level & 0x0f);
$this->getChunk($x >> 4, $z >> 4, true)->setBlockSkyLight($x & 0x0f, $y & Level::Y_MASK, $z & 0x0f, $level & 0x0f);
}
/**
@@ -2020,7 +2023,7 @@ class Level implements ChunkManager, Metadatable{
* @return int 0-15
*/
public function getBlockLightAt(int $x, int $y, int $z) : int{
return $this->getChunk($x >> 4, $z >> 4, true)->getBlockLight($x & 0x0f, $y & 0x7f, $z & 0x0f);
return $this->getChunk($x >> 4, $z >> 4, true)->getBlockLight($x & 0x0f, $y & Level::Y_MASK, $z & 0x0f);
}
/**
@@ -2032,7 +2035,7 @@ class Level implements ChunkManager, Metadatable{
* @param int $level 0-15
*/
public function setBlockLightAt(int $x, int $y, int $z, int $level){
$this->getChunk($x >> 4, $z >> 4, true)->setBlockLight($x & 0x0f, $y & 0x7f, $z & 0x0f, $level & 0x0f);
$this->getChunk($x >> 4, $z >> 4, true)->setBlockLight($x & 0x0f, $y & Level::Y_MASK, $z & 0x0f, $level & 0x0f);
}
/**
@@ -2205,7 +2208,7 @@ class Level implements ChunkManager, Metadatable{
* @param int $x
* @param int $z
*
* @return int 0-127
* @return int 0-255
*/
public function getHighestBlockAt(int $x, int $z) : int{
return $this->getChunk($x >> 4, $z >> 4, true)->getHighestBlockAt($x & 0x0f, $z & 0x0f);
@@ -2572,12 +2575,13 @@ class Level implements ChunkManager, Metadatable{
$spawn = $this->getSpawnLocation();
}
if($spawn instanceof Vector3){
$max = $this->provider->getWorldHeight();
$v = $spawn->floor();
$chunk = $this->getChunk($v->x >> 4, $v->z >> 4, false);
$x = $v->x & 0x0f;
$z = $v->z & 0x0f;
if($chunk !== null){
$y = (int) min(126, $v->y);
$y = (int) min($max - 2, $v->y);
$wasAir = ($chunk->getBlockId($x, $y - 1, $z) === 0);
for(; $y > 0; --$y){
$b = $chunk->getFullBlock($x, $y, $z);
@@ -2592,7 +2596,7 @@ class Level implements ChunkManager, Metadatable{
}
}
for(; $y >= 0 and $y < 128; ++$y){
for(; $y >= 0 and $y < $max; ++$y){
$b = $chunk->getFullBlock($x, $y + 1, $z);
$block = Block::get($b >> 4, $b & 0x0f);
if(!$this->isFullBlock($block)){