From 02721c09a415355f8661748cb75adbb27773d56c Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sat, 15 Jun 2013 21:24:06 +0200 Subject: [PATCH] Possible fix for #391 --- src/API/BlockAPI.php | 28 +++++++++++++++++++++++++++- src/material/block/solid/Leaves.php | 14 ++++++++++---- src/math/Vector2.php | 20 ++++++++++++++++++++ src/math/Vector3.php | 28 ++++++++++++++++++++++++++++ src/network/CustomPacketHandler.php | 8 ++++---- 5 files changed, 89 insertions(+), 9 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index d959554e5..283d3f338 100644 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -28,6 +28,7 @@ the Free Software Foundation, either version 3 of the License, or class BlockAPI{ private $server; private $scheduledUpdates = array(); + private $randomUpdates = array(); public static $creative = array( array(COBBLESTONE, 0), array(STONE_BRICKS, 0), @@ -753,7 +754,7 @@ class BlockAPI{ $this->blockUpdateAround($block, $level); $this->server->api->entity->updateRadius($pos, 3); }elseif($level === BLOCK_UPDATE_RANDOM){ - $this->scheduleBlockUpdate($pos, Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); + $this->nextRandomUpdate($pos); } return $level; } @@ -774,6 +775,31 @@ 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])){ + $X = (($pos->x >> 4) << 4); + $Y = (($pos->y >> 4) << 4); + $Z = (($pos->z >> 4) << 4); + $time = microtime(true); + $i = 0; + $offset = 0; + while(true){ + $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.5)." AND delay <= ".($time + $t + 1.5).";"); + if($update instanceof SQLite3Result){ + $update = $update->fetchArray(SQLITE3_NUM); + if($update[0] == 0){ + break; + } + }else{ + break; + } + $offset += 10; + } + $this->scheduleBlockUpdate($pos, $t / 0.05, BLOCK_UPDATE_RANDOM); + } + } + public function blockUpdateTick(){ $time = microtime(true); if(count($this->scheduledUpdates) > 0){ diff --git a/src/material/block/solid/Leaves.php b/src/material/block/solid/Leaves.php index 90f41f24b..b8ed5b9d7 100644 --- a/src/material/block/solid/Leaves.php +++ b/src/material/block/solid/Leaves.php @@ -47,9 +47,13 @@ class LeavesBlock extends TransparentBlock{ } if($pos->getID() === WOOD){ return true; - }elseif($pos->getID() === LEAVES and $distance < 4){ + }elseif($pos->getID() === LEAVES and $distance < 5){ $visited[$index] = true; - for($side = 0; $side <= 5; ++$side){ + $down = $pos->getSide(0)->getID(); + if($down === WOOD or $down == LEAVES){ + return true; + } + for($side = 2; $side <= 5; ++$side){ if($this->findLog($pos->getSide($side), $visited, $distance + 1) === true){ return true; } @@ -69,16 +73,18 @@ class LeavesBlock extends TransparentBlock{ }elseif($type === BLOCK_UPDATE_RANDOM){ if(($this->meta & 0b00001100) === 0x08){ $this->meta &= 0x03; - if($this->findLog($this, array(), 0) === true){ + $visited = array(); + if($this->findLog($this, $visited, 0) === true){ $this->level->setBlock($this, $this, false); }else{ - $this->level->setBlock($this, new AirBlock()); + $this->level->setBlock($this, new AirBlock(), false); if(mt_rand(1,20) === 1){ //Saplings ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem(SAPLING, $this->meta & 0x03, 1)); } if(($this->meta & 0x03) === LeavesBlock::OAK and mt_rand(1,200) === 1){ //Apples ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem(APPLE, 0, 1)); } + return BLOCK_UPDATE_NORMAL; } } } diff --git a/src/math/Vector2.php b/src/math/Vector2.php index d575624ff..d06f203ea 100644 --- a/src/math/Vector2.php +++ b/src/math/Vector2.php @@ -110,6 +110,26 @@ class Vector2{ return pow($this->x - $x, 2) + pow($this->y - $y, 2); } } + + public function length(){ + return sqrt($this->lengthSquared()); + } + + public function lengthSquared(){ + return $this->x * $this->x + $this->y * $this->y; + } + + public function normalize(){ + $len = $this->length(); + if($len != 0){ + return $this->divide($len); + } + return new Vector2(0, 0); + } + + public function dot(Vector2 $v){ + return $this->x * $v->x + $this->y * $v->y; + } public function __toString(){ return "Vector2(x=".$this->x.",y=".$this->y.")"; diff --git a/src/math/Vector3.php b/src/math/Vector3.php index 20f7d1d8b..8510573f9 100644 --- a/src/math/Vector3.php +++ b/src/math/Vector3.php @@ -164,6 +164,34 @@ class Vector3{ return max(abs($this->x - $x), abs($this->z - $z)); } } + + public function length(){ + return sqrt($this->lengthSquared()); + } + + public function lengthSquared(){ + return $this->x * $this->x + $this->y * $this->y + $this->z * $this->z; + } + + public function normalize(){ + $len = $this->length(); + if($len != 0){ + return $this->divide($len); + } + return new Vector3(0, 0, 0); + } + + public function dot(Vector3 $v){ + return $this->x * $v->x + $this->y * $v->y + $this->z * $v->z; + } + + public function cross(Vector3 $v){ + return new Vector3( + $this->y * $v->z - $this->z * $v->y, + $this->z * $v->x - $this->x * $v->z, + $this->x * $v->y - $this->y * $v->x + ); + } public function __toString(){ return "Vector3(x=".$this->x.",y=".$this->y.",z=".$this->z.")"; diff --git a/src/network/CustomPacketHandler.php b/src/network/CustomPacketHandler.php index 1d95223b7..2a4ce8881 100644 --- a/src/network/CustomPacketHandler.php +++ b/src/network/CustomPacketHandler.php @@ -159,14 +159,14 @@ class CustomPacketHandler{ $this->data["username"] = $this->get(Utils::readShort($this->get(2), false)); $this->data["protocol1"] = Utils::readInt($this->get(4)); $this->data["protocol2"] = Utils::readInt($this->get(4)); - $this->data["unknown1"] = Utils::readInt($this->get(4)); - $this->data["unknown2"] = $this->get(Utils::readShort($this->get(2), false)); + $this->data["clientId"] = Utils::readInt($this->get(4)); + $this->data["realms_data"] = $this->get(Utils::readShort($this->get(2), false)); }else{ $this->raw .= Utils::writeShort(strlen($this->data["username"])).$this->data["username"]; $this->raw .= Utils::writeInt(CURRENT_PROTOCOL). Utils::writeInt(CURRENT_PROTOCOL). - Utils::writeInt($this->data["unknown1"]); - $this->raw .= Utils::writeShort(strlen($this->data["unknown2"])).$this->data["unknown2"]; + Utils::writeInt($this->data["clientId"]); + $this->raw .= Utils::writeShort(strlen($this->data["realms_data"])).$this->data["realms_data"]; } break; case MC_LOGIN_STATUS: