Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
Improved Level->getTile() to a direct lookup instead of linear search
Browse files Browse the repository at this point in the history
  • Loading branch information
shoghicp committed Nov 6, 2014
1 parent 3b9a9bc commit f1519e6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/pocketmine/level/Level.php
Original file line number Diff line number Diff line change
Expand Up @@ -1366,13 +1366,10 @@ public function getTile(Vector3 $pos){
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;
Expand Down
7 changes: 7 additions & 0 deletions src/pocketmine/level/format/FullChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ public function getEntities();
*/
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
*/
Expand Down
10 changes: 10 additions & 0 deletions src/pocketmine/level/format/generic/BaseFullChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ abstract class BaseFullChunk implements FullChunk{
/** @var Tile[] */
protected $tiles = [];

/** @var Tile[] */
protected $tileList = [];

/** @var string */
protected $biomeIds;

Expand Down Expand Up @@ -234,11 +237,13 @@ public function removeEntity(Entity $entity){
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;
}
Expand All @@ -250,6 +255,11 @@ public function getTiles(){
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());
}
Expand Down

1 comment on commit f1519e6

@PEMapModder
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.