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

@ -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());
}