New Random block update tick

This commit is contained in:
Shoghi Cervantes 2014-03-02 17:56:24 +01:00
parent ca308f8159
commit 9cc9de8dae
7 changed files with 57 additions and 45 deletions

View File

@ -363,7 +363,8 @@ class BlockAPI{
if(($player->gamemode & 0x01) === 0x00 and count($drops) > 0){
foreach($drops as $drop){
$this->server->api->entity->drop(new Position($target->x + 0.5, $target->y, $target->z + 0.5, $target->level), BlockAPI::getItem($drop[0] & 0xFFFF, $drop[1] & 0xFFFF, $drop[2]));
echo "I dropped something\n";
//$this->server->api->entity->drop(new Position($target->x + 0.5, $target->y, $target->z + 0.5, $target->level), BlockAPI::getItem($drop[0] & 0xFFFF, $drop[1] & 0xFFFF, $drop[2]));
}
}
return false;
@ -435,9 +436,10 @@ class BlockAPI{
//$face = -1;
}
if($hand->isSolid === true and $player->entity->inBlock($block)){
//Implement using Bounding Boxes
/*if($hand->isSolid === true and $player->inBlock($block)){
return $this->cancelAction($block, $player, false); //Entity in block
}
}*/
if($this->server->api->dhandle("player.block.place", array("player" => $player, "block" => $block, "target" => $target, "item" => $item)) === false){
return $this->cancelAction($block, $player);
@ -500,8 +502,6 @@ class BlockAPI{
$level = $block->onUpdate($type);
if($level === BLOCK_UPDATE_NORMAL){
$this->blockUpdateAround($block, $level);
}elseif($level === BLOCK_UPDATE_RANDOM){
$this->nextRandomUpdate($pos);
}
return $level;
}
@ -522,27 +522,6 @@ class BlockAPI{
return false;
}
public function nextRandomUpdate(Position $pos){
if(!isset($this->scheduledUpdates[$pos->x.".".$pos->y.".".$pos->z.".".$pos->level->getName().".".BLOCK_UPDATE_RANDOM])){
$time = microtime(true);
$offset = 0;
do{
$t = $offset + Utils::getRandomUpdateTicks() * 0.05;
$update = $this->server->query("SELECT COUNT(*) FROM blockUpdates WHERE level = '".$pos->level->getName()."' AND type = ".BLOCK_UPDATE_RANDOM." AND delay >= ".($time + $t - 1)." AND delay <= ".($time + $t + 1).";");
if($update instanceof SQLite3Result){
$update = $update->fetchArray(SQLITE3_NUM);
if($update[0] < 3){
break;
}
}else{
break;
}
$offset += mt_rand(25, 75);
}while(true);
$this->scheduleBlockUpdate($pos, $t / 0.05, BLOCK_UPDATE_RANDOM);
}
}
public function blockUpdateTick(){
$time = microtime(true);
if(count($this->scheduledUpdates) > 0){

View File

@ -117,7 +117,7 @@ class ServerAPI{
"white-list" => false,
"announce-player-achievements" => true,
"spawn-protection" => 16,
"view-distance" => 10,
"view-distance" => 8,
"max-players" => 20,
"allow-flight" => false,
"spawn-animals" => true,
@ -221,6 +221,14 @@ class ServerAPI{
}
public function checkTickUpdates(){
//Update entities that need update
if(count(Entity::$needUpdate) > 0){
foreach(Entity::$needUpdate as $id => $entity){
if($entity->onUpdate() === false){
unset(Entity::$needUpdate[$id]);
}
}
}
//Update tiles that need update
if(count(Tile::$needUpdate) > 0){

View File

@ -61,10 +61,7 @@ class WheatBlock extends FlowableBlock{
if($this->meta < 0x07){
++$this->meta;
$this->level->setBlock($this, $this, true, false, true);
return BLOCK_UPDATE_RANDOM;
}
}else{
return BLOCK_UPDATE_RANDOM;
}
}
return false;

View File

@ -105,7 +105,6 @@ class LeavesBlock extends TransparentBlock{
if(($this->meta & 0b00001100) === 0){
$this->meta |= 0x08;
$this->level->setBlock($this, $this, false, false, true);
return BLOCK_UPDATE_RANDOM;
}
}elseif($type === BLOCK_UPDATE_RANDOM){
if(($this->meta & 0b00001100) === 0x08){

View File

@ -326,14 +326,24 @@ class PMFLevel extends PMF{
return true;
}
public function isMiniChunkEmpty($X, $Z, $Y){
protected function cleanChunk($X, $Z){
$index = self::getIndex($X, $Z);
if(isset($this->chunks[$index]) and $this->chunks[$index][$Y] !== false){
if(substr_count($this->chunks[$index][$Y], "\x00") < 8192){
return false;
if(isset($this->chunks[$index])){
for($Y = 0; $Y < 8; ++$Y){
if($this->chunks[$index][$Y] !== false and substr_count($this->chunks[$index][$Y], "\x00") === 8192){
$this->chunks[$index][$Y] = false;
$this->chunkInfo[$index][0] &= ~(1 << $Y);
}
}
}
return true;
}
public function isMiniChunkEmpty($X, $Z, $Y){
$index = self::getIndex($X, $Z);
if(!isset($this->chunks[$index]) or $this->chunks[$index][$Y] === false){
return true;
}
return false;
}
protected function fillMiniChunk($X, $Z, $Y){
@ -645,6 +655,7 @@ class PMFLevel extends PMF{
@mkdir(dirname($path), 0755);
}
$bitmap = 0;
$this->cleanChunk($X, $Z);
for($Y = 0; $Y < 8; ++$Y){
if($this->chunks[$index][$Y] !== false and ((isset($this->chunkChange[$index][$Y]) and $this->chunkChange[$index][$Y] === 0) or !$this->isMiniChunkEmpty($X, $Z, $Y))){
$bitmap |= 1 << $Y;

View File

@ -39,7 +39,7 @@ class Level{
$this->nextSave = $this->startCheck = microtime(true);
$this->nextSave += 90;
$this->stopTime = false;
$this->server->schedule(2, array($this, "checkThings"), array(), true);
$this->server->schedule(1, array($this, "doTick"), array(), true);
$this->server->schedule(20 * 13, array($this, "checkTime"), array(), true);
$this->name = $name;
$this->usedChunks = array();
@ -107,11 +107,10 @@ class Level{
}
}
public function checkThings(){
public function doTick(){
if(!isset($this->level)){
return false;
}
$now = microtime(true);
if($this->level->isGenerating === 0 and count($this->changedCount) > 0){
foreach($this->changedCount as $index => $mini){
@ -149,15 +148,35 @@ class Level{
}
$this->changedBlocks = array();
}
$X = null;
$Z = null;
//Do chunk updates
foreach($this->usedChunks as $index => $p){
PMFLevel::getXZ($index, $X, $Z);
for($Y = 0; $Y < 8; ++$Y){
if(!$this->level->isMiniChunkEmpty($X, $Z, $Y)){
for($i = 0; $i < 3; ++$i){
$block = $this->getBlockRaw(new Vector3(($X << 4) + mt_rand(0, 15), ($Y << 4) + mt_rand(0, 15), ($Z << 4) + mt_rand(0, 15)));
if($block instanceof Block){
if($block->onUpdate(BLOCK_UPDATE_RANDOM) === BLOCK_UPDATE_NORMAL){
$this->server->api->block->blockUpdateAround($block, $this);
}
}
}
}
}
}
}
if($this->nextSave < $now){
if($this->nextSave < microtime(true)){
$X = null;
$Z = null;
foreach($this->usedChunks as $i => $c){
if(count($c) === 0){
unset($this->usedChunks[$i]);
$X = explode(".", $i);
$Z = (int) array_pop($X);
$X = (int) array_pop($X);
PMFLevel::getXZ($i, $X, $Z);
if(!$this->isSpawnChunk($X, $Z)){
$this->level->unloadChunk($X, $Z, $this->server->saveEnabled);
}

View File

@ -46,8 +46,7 @@ class WorldGenerator{
public function generate(){
$this->generator->init($this->level, $this->random);
//Generate 4 chunks for spawning players
for($Z = 7; $Z <= 8; ++$Z){
for($Z = 7; $Z <= 9; ++$Z){
for($X = 7; $X <= 9; ++$X){
$this->level->level->loadChunk($X, $Z);
}