Improved Level->getTile() to a direct lookup instead of linear search

This commit is contained in:
Shoghi Cervantes 2014-11-06 12:34:33 +01:00
parent 3b9a9bcd5d
commit f1519e6d13
3 changed files with 21 additions and 7 deletions

View File

@ -1366,13 +1366,10 @@ class Level implements ChunkManager, Metadatable{
if($pos instanceof Position and $pos->getLevel() !== $this){
return null;
}
$tiles = $this->getChunkTiles($pos->x >> 4, $pos->z >> 4);
if(count($tiles) > 0){
foreach($tiles as $tile){
if($tile->x === (int) $pos->x and $tile->y === (int) $pos->y and $tile->z === (int) $pos->z){
return $tile;
}
}
$chunk = $this->getChunk($pos->x >> 4, $pos->z >> 4);
if($chunk instanceof FullChunk){
return $chunk->getTile($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f);
}
return null;

View File

@ -226,6 +226,13 @@ interface FullChunk{
*/
public function getTiles();
/**
* @param int $x 0-15
* @param int $y 0-127
* @param int $z 0-15
*/
public function getTile($x, $y, $z);
/**
* @return bool
*/

View File

@ -37,6 +37,9 @@ abstract class BaseFullChunk implements FullChunk{
/** @var Tile[] */
protected $tiles = [];
/** @var Tile[] */
protected $tileList = [];
/** @var string */
protected $biomeIds;
@ -234,11 +237,13 @@ abstract class BaseFullChunk implements FullChunk{
public function addTile(Tile $tile){
$this->tiles[$tile->getID()] = $tile;
$this->tiles[(($tile->z & 0x0f) << 8) | (($tile->x & 0x0f) << 4) | ($tile->y & 0x7f)] = $tile;
$this->hasChanged = true;
}
public function removeTile(Tile $tile){
unset($this->tiles[$tile->getID()]);
unset($this->tiles[(($tile->z & 0x0f) << 8) | (($tile->x & 0x0f) << 4) | ($tile->y & 0x7f)]);
$this->hasChanged = true;
}
@ -250,6 +255,11 @@ abstract class BaseFullChunk implements FullChunk{
return $this->tiles;
}
public function getTile($x, $y, $z){
$index = ($z << 8) | ($x << 4) | $y;
return isset($this->tileList[$index]) ? $this->tileList[$index] : null;
}
public function isLoaded(){
return $this->getProvider() === null ? false : $this->getProvider()->isChunkLoaded($this->getX(), $this->getZ());
}