New Chunk objects / structure, Anvil region loader [WiP]

This commit is contained in:
Shoghi Cervantes 2014-04-10 05:05:36 +02:00
parent abccfeac37
commit aa1de79337
19 changed files with 594 additions and 26 deletions

View File

@ -68,7 +68,7 @@ use pocketmine\network\raknet\Packet;
use pocketmine\permission\PermissibleBase;
use pocketmine\permission\PermissionAttachment;
use pocketmine\plugin\Plugin;
use pocketmine\pmf\LevelFormat;
use pocketmine\level\format\pmf\LevelFormat;
use pocketmine\recipes\Crafting;
use pocketmine\scheduler\CallbackTask;
use pocketmine\tile\Chest;
@ -78,7 +78,6 @@ use pocketmine\tile\Spawnable;
use pocketmine\tile\Tile;
use pocketmine\utils\Binary;
use pocketmine\utils\TextFormat;
use pocketmine\utils\Utils;
/**
* Main class that handles networking, recovery, and packet sending to the server part

View File

@ -69,7 +69,7 @@ use pocketmine\permission\DefaultPermissions;
use pocketmine\plugin\Plugin;
use pocketmine\plugin\PluginLoadOrder;
use pocketmine\plugin\PluginManager;
use pocketmine\pmf\LevelFormat;
use pocketmine\level\format\pmf\LevelFormat;
use pocketmine\recipes\Crafting;
use pocketmine\scheduler\CallbackTask;
use pocketmine\scheduler\SendUsageTask;

View File

@ -41,7 +41,7 @@ use pocketmine\network\protocol\SetEntityMotionPacket;
use pocketmine\network\protocol\SetTimePacket;
use pocketmine\Network;
use pocketmine\Player;
use pocketmine\pmf\LevelFormat;
use pocketmine\level\format\pmf\LevelFormat;
use pocketmine\Server;
abstract class Entity extends Position{

View File

@ -42,7 +42,7 @@ use pocketmine\nbt\tag\String;
use pocketmine\network\protocol\SetTimePacket;
use pocketmine\network\protocol\UpdateBlockPacket;
use pocketmine\Player;
use pocketmine\pmf\LevelFormat;
use pocketmine\level\format\pmf\LevelFormat;
use pocketmine\Server;
use pocketmine\tile\Chest;
use pocketmine\tile\Furnace;

View File

@ -23,7 +23,7 @@ namespace pocketmine\level;
use pocketmine\level\format\PocketChunkParser;
use pocketmine\nbt\NBT;
use pocketmine\pmf\LevelFormat;
use pocketmine\level\format\pmf\LevelFormat;
use pocketmine\utils\Config;
class LevelImport{

View File

@ -22,7 +22,7 @@
namespace pocketmine\level;
use pocketmine\level\generator\Generator;
use pocketmine\pmf\LevelFormat;
use pocketmine\level\format\pmf\LevelFormat;
use pocketmine\Server;
use pocketmine\utils\Binary;
use pocketmine\utils\Random;

View File

@ -22,6 +22,7 @@
namespace pocketmine\level\format;
interface Chunk{
const SECTION_COUNT = 8;
/**
* @return int
@ -39,19 +40,72 @@ interface Chunk{
public function getLevel();
/**
* Modifies $blockId and $meta
*
* @param int $x 0-15
* @param int $y 0-127
* @param int $z 0-15
* @param int &$blockId
* @param int &$meta
*/
public function getBlock($x, $y, $z, &$blockId, &$meta = null);
/**
* @param int $x 0-15
* @param int $y 0-127
* @param int $z 0-15
* @param int $blockId, if null, do not change
* @param int $meta 0-15, if null, do not change
*/
public function setBlock($x, $y, $z, $blockId = null, $meta = null);
/**
* @param int $x 0-15
* @param int $y 0-127
* @param int $z 0-15
*
* @return \pocketmine\block\Block
* @return int 0-15
*/
public function getBlock($x, $y, $z);
public function getBlockSkyLight($x, $y, $z);
/**
* @param int $x 0-15
* @param int $y 0-127
* @param int $z 0-15
* @param int $level 0-15
*/
public function setBlockSkyLight($x, $y, $z, $level);
/**
* @param int $x 0-15
* @param int $y 0-127
* @param int $z 0-15
*
* @return int 0-15
*/
public function getBlockLight($x, $y, $z);
/**
* @param int $x 0-15
* @param int $y 0-127
* @param int $z 0-15
* @param int $level 0-15
*/
public function setBlockLight($x, $y, $z, $level);
/**
* @param int $x 0-15
* @param int $z 0-15
*
* @return int 0-127
*/
public function getHighestBlockAt($x, $z);
/**
* Thread-safe read-only chunk
*
* @return ChunkSnapShot
* @return ChunkSnapshot
*/
public function getChunkSnapshot();
@ -87,4 +141,28 @@ interface Chunk{
*/
public function unload($save = true, $safe = true);
/**
* Tests whether a section (mini-chunk) is empty
*
* @param $fY 0-7, (Y / 16)
*
* @return bool
*/
public function isSectionEmpty($fY);
/**
* @param int $fY 0-7
*
* @return ChunkSection
*/
public function getSection($fY);
/**
* @param int $fY 0-7
* @param ChunkSection $section
*
* @return boolean
*/
public function setSection($fY, ChunkSection $section);
}

View File

@ -0,0 +1,134 @@
<?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/
*
*
*/
namespace pocketmine\level\format;
interface ChunkSection{
/**
* @param int $x 0-15
* @param int $y 0-15
* @param int $z 0-15
*
* @return int 0-255
*/
public function getBlockId($x, $y, $z);
/**
* @param int $x 0-15
* @param int $y 0-15
* @param int $z 0-15
* @param int $id 0-255
*/
public function setBlockId($x, $y, $z, $id);
/**
* @param int $x 0-15
* @param int $y 0-15
* @param int $z 0-15
*
* @return int 0-15
*/
public function getBlockData($x, $y, $z);
/**
* @param int $x 0-15
* @param int $y 0-15
* @param int $z 0-15
* @param int $data 0-15
*/
public function setBlockData($x, $y, $z, $data);
/**
* Modifies $blockId and $meta
*
* @param int $x 0-15
* @param int $y 0-15
* @param int $z 0-15
* @param int &$blockId
* @param int &$meta
*/
public function getBlock($x, $y, $z, &$blockId, &$meta = null);
/**
* @param int $x 0-15
* @param int $y 0-15
* @param int $z 0-15
* @param int $blockId, if null, do not change
* @param int $meta 0-15, if null, do not change
*/
public function setBlock($x, $y, $z, $blockId = null, $meta = null);
/**
* @param int $x 0-15
* @param int $y 0-15
* @param int $z 0-15
*
* @return int 0-15
*/
public function getBlockSkyLight($x, $y, $z);
/**
* @param int $x 0-15
* @param int $y 0-15
* @param int $z 0-15
* @param int $level 0-15
*/
public function setBlockSkyLight($x, $y, $z, $level);
/**
* @param int $x 0-15
* @param int $y 0-15
* @param int $z 0-15
*
* @return int 0-15
*/
public function getBlockLight($x, $y, $z);
/**
* @param int $x 0-15
* @param int $y 0-15
* @param int $z 0-15
* @param int $level 0-15
*/
public function setBlockLight($x, $y, $z, $level);
/**
* Returns a id column from high y to low y
*
* @param int $x 0-15
* @param int $z 0-15
*
* @return string[16]
*/
public function getBlockIdColumn($x, $z);
/**
* Returns a data column from high y to low y
*
* @param int $x 0-15
* @param int $z 0-15
*
* @return string[8]
*/
public function getBlockDataColumn($x, $z);
}

View File

@ -65,15 +65,6 @@ interface ChunkSnapshot{
*/
public function getBlockSkyLight($x, $y, $z);
/**
* @param int $x 0-15
* @param int $y 0-127
* @param int $z 0-15
*
* @return int 0-15
*/
public function getBlockEmittedLight($x, $y, $z);
/**
* @param int $x 0-15
* @param int $y 0-127
@ -108,4 +99,19 @@ interface ChunkSnapshot{
*/
public function isSectionEmpty($fY);
/**
* @param int $Y 0-7
*
* @return ChunkSection
*/
public function getSection($Y);
/**
* @param int $Y 0-7
* @param ChunkSection $section
*
* @return boolean
*/
public function setSection($Y, ChunkSection $section);
}

View File

@ -57,6 +57,10 @@ interface LevelFormat{
public function unloadChunks();
public function loadChunk($X, $Z);
public function unloadChunk($X, $Z);
public function getName();
/**

View File

@ -33,4 +33,9 @@ class Anvil implements LevelFormat{
public static function isValid($path){
return file_exists(realpath($path) . "region/");
}
public static function getRegionIndex($chunkX, $chunkZ, &$x, &$z){
$x = $chunkX >> 5;
$z = $chunkZ >> 5;
}
}

View File

@ -0,0 +1,87 @@
<?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/
*
*
*/
namespace pocketmine\level\format\anvil;
use pocketmine\utils\Binary;
class RegionLoader{
const COMPRESSION_GZIP = 1;
const COMPRESSION_ZLIB = 2;
protected $x;
protected $z;
protected $filePath;
protected $filePointer;
protected $locationTable = array();
public function __construct($path, $regionX, $regionZ){
$this->filePath = $path . "r.$regionX.$regionZ.mca";
touch($this->filePath);
$this->filePointer = fopen($this->filePath, "r+b");
flock($this->filePointer, LOCK_EX);
stream_set_read_buffer($this->filePointer, 4096);
stream_set_write_buffer($this->filePointer, 1024 * 16); //16KB
if(!file_exists($this->filePath)){
$this->createBlank();
}else{
$this->loadLocationTable();
}
}
public function __destruct(){
if(is_resource($this->filePointer)){
flock($this->filePointer, LOCK_UN);
fclose($this->filePointer);
}
}
private function loadLocationTable(){
fseek($this->filePointer, 0);
for($i = 0; $i < 1024; ++$i){
$index = Binary::readInt(fread($this->filePointer, 4));
$this->locationTable[$i] = array(($index & ~0xff) >> 8, $index & 0xff);
}
}
private function createBlank(){
fseek($this->filePointer, 0);
ftruncate($this->filePointer, 0);
for($i = 0; $i < 1024; ++$i){
$this->locationTable[$i] = array($i, 1);
fwrite($this->filePointer, Binary::writeInt(($i << 8) | 1)); //Default: 1 sector per chunk
}
$data = str_pad(Binary::writeInt(-1) . chr(self::COMPRESSION_ZLIB), 4096, "\x00", STR_PAD_RIGHT);
for($i = 0; $i < 1024; ++$i){
fwrite($this->filePointer, $data);
}
}
public function getX(){
return $this->x;
}
public function getZ(){
return $this->z;
}
}

View File

@ -0,0 +1,125 @@
<?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/
*
*
*/
namespace pocketmine\level\format\generic;
use pocketmine\level\format\Chunk;
use pocketmine\level\format\ChunkSection;
use pocketmine\level\Level;
abstract class BaseChunk implements Chunk{
/** @var ChunkSection[] */
protected $sections = array();
protected $level;
protected $x;
protected $z;
/**
* @param Level $level
* @param int $x
* @param int $z
* @param ChunkSection[] $sections
*/
public function __construct(Level $level, $x, $z, array $sections){
$this->level = $level;
$this->x = (int) $x;
$this->z = (int) $z;
$Y = 0;
foreach($sections as $section){
if($section instanceof ChunkSection){
$this->sections[$Y] = $section;
}else{
trigger_error("Received invalid ChunkSection instance", E_USER_ERROR);
return;
}
++$Y;
if($section >= self::SECTION_COUNT){
trigger_error("Invalid amount of chunks", E_USER_WARNING);
return;
}
}
}
public function getX(){
return $this->x;
}
public function getZ(){
return $this->z;
}
public function getLevel(){
return $this->level;
}
public function getBlock($x, $y, $z, &$blockId, &$meta = null){
$this->sections[$y >> 4]->getBlock($x, $y - ($y >> 4), $z, $blockId, $meta);
}
public function setBlock($x, $y, $z, $blockId = null, $meta = null){
$this->sections[$y >> 4]->setBlock($x, $y - ($y >> 4), $z, $blockId, $meta);
}
public function getBlockSkyLight($x, $y, $z){
return $this->sections[$y >> 4]->getBlockSkyLight($x, $y - ($y >> 4), $z);
}
public function setBlockSkyLight($x, $y, $z, $data){
$this->sections[$y >> 4]->getBlockSkyLight($x, $y - ($y >> 4), $z, $data);
}
public function getBlockLight($x, $y, $z){
return $this->sections[$y >> 4]->getBlockSkyLight($x, $y - ($y >> 4), $z);
}
public function setBlockLight($x, $y, $z, $data){
$this->sections[$y >> 4]->getBlockSkyLight($x, $y - ($y >> 4), $z, $data);
}
public function getHighestBlockAt($x, $z){
for($Y = self::SECTION_COUNT; $Y >= 0; --$Y){
if(!$this->isSectionEmpty($Y)){
$column = $this->sections[$Y]->getBlockIdColumn($x, $z);
for($y = 15; $y >= 0; --$y){
if($column{$y} !== "\x00"){
return $y + $Y << 4;
}
}
}
}
return 0;
}
public function isSectionEmpty($fY){
return $this->sections[(int) $fY] instanceof EmptyChunkSection;
}
public function getSection($fY){
return $this->sections[(int) $fY];
}
public function setSection($fY, ChunkSection $section){
$this->sections[(int) $fY] = $section;
}
}

View File

@ -19,7 +19,8 @@
*
*/
namespace pocketmine\level\format;
namespace pocketmine\level\format\generic;
use pocketmine\level\format\ChunkSection;
/**
* Stub used to detect empty chunks

View File

@ -19,4 +19,132 @@
*
*/
namespace
namespace pocketmine\level\format\pmf;
class ChunkSection implements \pocketmine\level\format\ChunkSection{
const METADATA_OFFSET = 16;
const LIGHT_OFFSET = 24;
const SKYLIGHT_OFFSET = 32;
private $section = "";
public function __construct($section = null){
if($section !== null){
if(strlen($section) !== 8192){
trigger_error("Invalid ChunkSection generated", E_USER_WARNING);
return;
}
$this->section = $section;
}else{
$this->section = str_repeat("\x00\x00\x00\x00\x00\x00\x00\x00", 1024);
}
}
public function getBlockId($x, $y, $z){
return $b = ord($this->section{$y + ($x << 5) + ($z << 9)});
}
public function getBlock($x, $y, $z, &$blockId, &$meta = null){
$i = ($x << 5) + ($z << 9);
$blockId = ord($this->section{$i + $y});
$meta = ord($this->section{($y >> 1) + self::METADATA_OFFSET});
}
public function setBlock($x, $y, $z, $blockId = null, $meta = null){
$i = ($x << 5) + ($z << 9);
if($blockId !== null){
$this->section{$i + $y} = chr($blockId);
}
if($meta !== null){
$i += ($y >> 1) + self::METADATA_OFFSET;
$m = ord($this->section{$i});
if(($y & 1) === 0){
$this->section{$i} = chr(($m & 0xf0) | ($meta & 0x0f));
}else{
$this->section{$i} = chr((($meta & 0x0f) << 4) | ($m & 0x0f));
}
}
}
public function setBlockId($x, $y, $z, $id){
$this->section{$y + ($x << 5) + ($z << 9)} = chr($id & 0xff);
}
public function getBlockData($x, $y, $z){
$m = ord($this->section{($y >> 1) + self::METADATA_OFFSET + ($x << 5) + ($z << 9)});
if(($y & 1) === 0){
return $m & 0x0F;
}else{
return $m >> 4;
}
}
public function setBlockData($x, $y, $z, $data){
$i = ($y >> 1) + self::METADATA_OFFSET + ($x << 5) + ($z << 9);
$m = ord($this->section{$i});
if(($y & 1) === 0){
$this->section{$i} = chr(($m & 0xf0) | ($data & 0x0f));
}else{
$this->section{$i} = chr((($data & 0x0f) << 4) | ($m & 0x0f));
}
}
public function getBlockIdColumn($x, $z){
$column = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
$i = ($x << 5) + ($z << 9);
for($y = 15; $y >= 0; --$y){
$column{15 - $y} = $this->section{$i + $y};
}
return $column;
}
public function getBlockDataColumn($x, $z){
$column = "\x00\x00\x00\x00\x00\x00\x00\x00";
$i = ($x << 5) + ($z << 9) + self::METADATA_OFFSET;
for($y = 7; $y >= 0; --$y){
$column{7 - $y} = $this->section{$i + $y};
}
return $column;
}
public function getBlockLight($x, $y, $z){
$l = ord($this->section{($y >> 1) + self::LIGHT_OFFSET + ($x << 5) + ($z << 9)});
if(($y & 1) === 0){
return $l & 0x0F;
}else{
return $l >> 4;
}
}
public function setBlockLight($x, $y, $z, $level){
$i = ($y >> 1) + self::LIGHT_OFFSET + ($x << 5) + ($z << 9);
$l = ord($this->section{$i});
if(($y & 1) === 0){
$this->section{$i} = chr(($l & 0xf0) | ($level & 0x0f));
}else{
$this->section{$i} = chr((($level & 0x0f) << 4) | ($l & 0x0f));
}
}
public function getBlockSkyLight($x, $y, $z){
$sl = ord($this->section{($y >> 1) + self::SKYLIGHT_OFFSET + ($x << 5) + ($z << 9)});
if(($y & 1) === 0){
return $sl & 0x0F;
}else{
return $sl >> 4;
}
}
public function setBlockSkyLight($x, $y, $z, $level){
$i = ($y >> 1) + self::SKYLIGHT_OFFSET + ($x << 5) + ($z << 9);
$sl = ord($this->section{$i});
if(($y & 1) === 0){
$this->section{$i} = chr(($sl & 0xf0) | ($level & 0x0f));
}else{
$this->section{$i} = chr((($level & 0x0f) << 4) | ($sl & 0x0f));
}
}
}

View File

@ -19,7 +19,7 @@
*
*/
namespace pocketmine\pmf;
namespace pocketmine\level\format\pmf;
use pocketmine\level\Level;
use pocketmine\nbt\NBT;

View File

@ -21,9 +21,8 @@
/**
* PMF (PocketMine Format) handling
* TODO: Remove in favor of NBT
*/
namespace pocketmine\pmf;
namespace pocketmine\level\format\pmf;
class PMF{

View File

@ -73,7 +73,9 @@ class NBT{
}elseif($len === true){
return substr($this->buffer, $this->offset);
}
if($len > 1024){
return substr($this->buffer, ($this->offset += $len) - $len, $len);
}
$buffer = "";
for(; $len > 0; --$len, ++$this->offset){
$buffer .= @$this->buffer{$this->offset};

View File

@ -28,7 +28,7 @@ namespace pocketmine\tile;
use pocketmine\level\Level;
use pocketmine\level\Position;
use pocketmine\nbt\tag\Compound;
use pocketmine\pmf\LevelFormat;
use pocketmine\level\format\pmf\LevelFormat;
use pocketmine\Server;
abstract class Tile extends Position{