From 9cc9de8daefd9628ad67aee432b3620d50743f55 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sun, 2 Mar 2014 17:56:24 +0100 Subject: [PATCH] New Random block update tick --- src/API/BlockAPI.php | 31 ++++-------------------- src/API/ServerAPI.php | 10 +++++++- src/material/block/plant/Wheat.php | 3 --- src/material/block/solid/Leaves.php | 1 - src/pmf/PMFLevel.php | 21 ++++++++++++---- src/world/Level.php | 33 ++++++++++++++++++++------ src/world/generator/WorldGenerator.php | 3 +-- 7 files changed, 57 insertions(+), 45 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index e66f55c29..918fd9ce3 100644 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -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){ diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 9c5637986..41dce1f28 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -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){ diff --git a/src/material/block/plant/Wheat.php b/src/material/block/plant/Wheat.php index b049c608f..0f1a1ae9c 100644 --- a/src/material/block/plant/Wheat.php +++ b/src/material/block/plant/Wheat.php @@ -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; diff --git a/src/material/block/solid/Leaves.php b/src/material/block/solid/Leaves.php index cef6e32aa..a3b821512 100644 --- a/src/material/block/solid/Leaves.php +++ b/src/material/block/solid/Leaves.php @@ -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){ diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 7646ccb81..e8c21d3fe 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -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; diff --git a/src/world/Level.php b/src/world/Level.php index 639e0dc25..3e4cc8eba 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -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); } diff --git a/src/world/generator/WorldGenerator.php b/src/world/generator/WorldGenerator.php index eef1f0c52..f3e1bf141 100644 --- a/src/world/generator/WorldGenerator.php +++ b/src/world/generator/WorldGenerator.php @@ -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); }