Chunk unloading

This commit is contained in:
Shoghi Cervantes Pueyo
2013-05-15 22:43:12 +02:00
parent 3f532d419c
commit 244fde8143
4 changed files with 51 additions and 8 deletions

View File

@ -39,6 +39,24 @@ class Level{
$this->server->schedule(15, array($this, "checkThings"), array(), true);
$this->server->event("server.close", array($this, "save"));
$this->name = $name;
$this->usedChunks = array();
}
public function useChunk($X, $Z, Player $player){
if(!isset($this->usedChunks[$X.".".$Z])){
$this->usedChunks[$X.".".$Z] = array();
}
$this->usedChunks[$X.".".$Z][$player->CID] = true;
$this->level->loadChunk($X, $Z);
}
public function freeAllChunks(Player $player){
foreach($this->usedChunks as $i => $c){
unset($this->usedChunks[$i][$player->CID]);
}
}
public function freeChunk($X, $Z, Player $player){
unset($this->usedChunks[$X.".".$Z][$player->CID]);
}
public function checkThings(){
@ -47,6 +65,15 @@ class Level{
if($this->server->api->dhandle("time.change", array("level" => $this, "time" => $time)) !== false){
$this->time = $time;
}
foreach($this->usedChunks as $i => $c){
if(count($c) === 0){
unset($this->usedChunks[$i]);
$X = explode(".", $i);
$Z = array_pop($X);
$this->level->unloadChunk((int) $X, (int) $Z);
}
}
}
public function __destruct(){
@ -68,10 +95,14 @@ class Level{
return BlockAPI::get($b[0], $b[1], new Position($pos->x, $pos->y, $pos->z, $this));
}
public function setBlock(Position $pos, Block $block, $update = true, $tiles = false){
public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false){
if((($pos instanceof Position) and $pos->level !== $this) or $pos->x < 0 or $pos->y < 0 or $pos->z < 0){
return false;
}elseif($this->server->api->dhandle("block.change", array(
}elseif(!($pos instanceof Position)){
$pos = new Position($pos->x, $pos->y, $pos->z, $this);
}
if($this->server->api->dhandle("block.change", array(
"position" => $pos,
"block" => $block,
)) !== false){

View File

@ -66,7 +66,7 @@ class SmallTreeObject extends TreeObject{
for($xx = -$xzRadius; $xx < ($xzRadius + 1); ++$xx){
for($zz = -$xzRadius; $zz < ($xzRadius + 1); ++$zz){
if((abs($xx) != $xzRadius or abs($zz) != $xzRadius) and $yRadius != 0){
$level->setBlock(new Vector3($x + $xx, $y + $yy, $z + $zz), new LeavesBlck($this->type));
$level->setBlock(new Vector3($x + $xx, $y + $yy, $z + $zz), new LeavesBlock($this->type));
}
}
}