Worked on chunk loading

This commit is contained in:
Shoghi Cervantes 2014-06-07 22:29:45 +02:00
parent d8f9f9231f
commit 2566f2c4cb
4 changed files with 87 additions and 74 deletions

View File

@ -612,20 +612,25 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$index = key($this->chunksOrder); $index = key($this->chunksOrder);
LevelFormat::getXZ($index, $X, $Z); LevelFormat::getXZ($index, $X, $Z);
$this->getLevel()->loadChunk($X, $Z); $radius = 1;
if(!$this->getLevel()->isChunkPopulated($X, $Z)){ for($z = $Z - $radius; $z <= ($Z + $radius); ++$z){
$this->getLevel()->loadChunk($X - 1, $Z); for($x = $X - $radius; $x <= ($X + $radius); ++$x){
$this->getLevel()->loadChunk($X + 1, $Z); $this->getLevel()->loadChunk($x, $z);
$this->getLevel()->loadChunk($X, $Z - 1); if(!$this->getLevel()->isChunkPopulated($x, $z)){
$this->getLevel()->loadChunk($X, $Z + 1); $this->getLevel()->loadChunk($x - 1, $z);
$this->getLevel()->loadChunk($X + 1, $Z + 1); $this->getLevel()->loadChunk($x + 1, $z);
$this->getLevel()->loadChunk($X + 1, $Z - 1); $this->getLevel()->loadChunk($x, $z - 1);
$this->getLevel()->loadChunk($X - 1, $Z - 1); $this->getLevel()->loadChunk($x, $z + 1);
$this->getLevel()->loadChunk($X - 1, $Z + 1); $this->getLevel()->loadChunk($x + 1, $z + 1);
$this->getLevel()->loadChunk($x + 1, $z - 1);
$this->getLevel()->loadChunk($x - 1, $z - 1);
$this->getLevel()->loadChunk($x - 1, $z + 1);
}
}
} }
foreach($lastChunk as $index => $Yndex){ foreach($lastChunk as $index => $Yndex){
if($Yndex !== 0xff){ if($Yndex === 0){
$X = null; $X = null;
$Z = null; $Z = null;
LevelFormat::getXZ($index, $X, $Z); LevelFormat::getXZ($index, $X, $Z);
@ -1153,6 +1158,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->server->getPluginManager()->callEvent(new PlayerJoinEvent($this, $this->username . " joined the game")); $this->server->getPluginManager()->callEvent(new PlayerJoinEvent($this, $this->username . " joined the game"));
$this->orderChunks(); //Get first chunk ready
break; break;
case ProtocolInfo::READY_PACKET: case ProtocolInfo::READY_PACKET:
if($this->loggedIn === false){ if($this->loggedIn === false){

View File

@ -95,6 +95,7 @@ class Level{
/** @var Server */ /** @var Server */
private $server; private $server;
private $name; private $name;
/** @var Player[][] */
private $usedChunks; private $usedChunks;
private $changedBlocks; private $changedBlocks;
private $changedCount; private $changedCount;
@ -302,6 +303,7 @@ class Level{
if(count($this->changedBlocks) > 0){ if(count($this->changedBlocks) > 0){
foreach($this->changedBlocks as $index => $mini){ foreach($this->changedBlocks as $index => $mini){
if(isset($this->usedChunks[$index]) and count($this->usedChunks[$index]) > 0){
foreach($mini as $blocks){ foreach($mini as $blocks){
/** @var Block $b */ /** @var Block $b */
foreach($blocks as $b){ foreach($blocks as $b){
@ -311,7 +313,8 @@ class Level{
$pk->z = $b->z; $pk->z = $b->z;
$pk->block = $b->getID(); $pk->block = $b->getID();
$pk->meta = $b->getDamage(); $pk->meta = $b->getDamage();
$this->server->broadcastPacket($this->players, $pk); $this->server->broadcastPacket($this->usedChunks[$index], $pk);
}
} }
} }
} }
@ -383,7 +386,6 @@ class Level{
public function populateChunk($X, $Z){ public function populateChunk($X, $Z){
$this->level->setPopulated($X, $Z); $this->level->setPopulated($X, $Z);
$this->generator->populateChunk($X, $Z); $this->generator->populateChunk($X, $Z);
return true; return true;
} }
@ -471,7 +473,7 @@ class Level{
* @param int $type * @param int $type
*/ */
public function updateAround(Vector3 $pos, $type = self::BLOCK_UPDATE_NORMAL){ public function updateAround(Vector3 $pos, $type = self::BLOCK_UPDATE_NORMAL){
$block = $this->getBlockRaw($pos); $block = $this->getBlock($pos);
$block->getSide(0)->onUpdate($type); $block->getSide(0)->onUpdate($type);
$block->getSide(1)->onUpdate($type); $block->getSide(1)->onUpdate($type);
$block->getSide(2)->onUpdate($type); $block->getSide(2)->onUpdate($type);
@ -583,6 +585,8 @@ class Level{
*/ */
public function setBlockRaw(Vector3 $pos, Block $block, $direct = true, $send = true){ public function setBlockRaw(Vector3 $pos, Block $block, $direct = true, $send = true){
if(($ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getDamage())) === true and $send !== false){ if(($ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getDamage())) === true and $send !== false){
$index = LevelFormat::getIndex($pos->x >> 4, $pos->z >> 4);
if(isset($this->usedChunks[$index]) and count($this->usedChunks[$index]) > 0){
if($direct === true){ if($direct === true){
$pk = new UpdateBlockPacket; $pk = new UpdateBlockPacket;
$pk->x = $pos->x; $pk->x = $pos->x;
@ -590,13 +594,12 @@ class Level{
$pk->z = $pos->z; $pk->z = $pos->z;
$pk->block = $block->getID(); $pk->block = $block->getID();
$pk->meta = $block->getDamage(); $pk->meta = $block->getDamage();
$this->server->broadcastPacket($this->players, $pk); $this->server->broadcastPacket($this->usedChunks[$index], $pk);
}elseif($direct === false){ }elseif($direct === false){
if(!($pos instanceof Position)){ if(!($pos instanceof Position)){
$pos = new Position($pos->x, $pos->y, $pos->z, $this); $pos = new Position($pos->x, $pos->y, $pos->z, $this);
} }
$block->position($pos); $block->position($pos);
$index = LevelFormat::getIndex($pos->x >> 4, $pos->z >> 4);
if(ADVANCED_CACHE == true){ if(ADVANCED_CACHE == true){
Cache::remove("world:{$this->name}:{$index}"); Cache::remove("world:{$this->name}:{$index}");
} }
@ -612,6 +615,7 @@ class Level{
$this->changedBlocks[$index][$Y][] = clone $block; $this->changedBlocks[$index][$Y][] = clone $block;
} }
} }
}
return $ret; return $ret;
} }
@ -637,6 +641,8 @@ class Level{
} }
$block->position($pos); $block->position($pos);
$index = LevelFormat::getIndex($pos->x >> 4, $pos->z >> 4);
if(isset($this->usedChunks[$index]) and count($this->usedChunks[$index]) > 0){
if($direct === true){ if($direct === true){
$pk = new UpdateBlockPacket; $pk = new UpdateBlockPacket;
$pk->x = $pos->x; $pk->x = $pos->x;
@ -644,9 +650,9 @@ class Level{
$pk->z = $pos->z; $pk->z = $pos->z;
$pk->block = $block->getID(); $pk->block = $block->getID();
$pk->meta = $block->getDamage(); $pk->meta = $block->getDamage();
$this->server->broadcastPacket($this->players, $pk); $this->server->broadcastPacket($this->usedChunks[$index], $pk);
}else{ }else{
$index = LevelFormat::getIndex($pos->x >> 4, $pos->z >> 4);
if(ADVANCED_CACHE == true){ if(ADVANCED_CACHE == true){
Cache::remove("world:{$this->name}:{$index}"); Cache::remove("world:{$this->name}:{$index}");
} }
@ -661,6 +667,7 @@ class Level{
} }
$this->changedBlocks[$index][$Y][] = clone $block; $this->changedBlocks[$index][$Y][] = clone $block;
} }
}
if($update === true){ if($update === true){
$this->updateAround($pos, self::BLOCK_UPDATE_NORMAL); $this->updateAround($pos, self::BLOCK_UPDATE_NORMAL);

View File

@ -61,8 +61,8 @@ class WorldGenerator{
public function generate(){ public function generate(){
$this->generator->init($this->level, $this->random); $this->generator->init($this->level, $this->random);
for($Z = 7; $Z <= 9; ++$Z){ for($Z = 6; $Z <= 10; ++$Z){
for($X = 7; $X <= 9; ++$X){ for($X = 6; $X <= 10; ++$X){
$this->level->level->loadChunk($X, $Z); $this->level->level->loadChunk($X, $Z);
} }
} }

View File

@ -40,7 +40,7 @@ class Ore{
} }
public function canPlaceObject(Level $level, $x, $y, $z){ public function canPlaceObject(Level $level, $x, $y, $z){
return ($level->level->getBlockID($x, $y, $z) !== 0); return ($level->level->getBlockID($x, $y, $z) === 1);
} }
public function placeObject(Level $level, Vector3 $pos){ public function placeObject(Level $level, Vector3 $pos){