From c646b786e557c2c712df487a0446873a3a906416 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Mon, 24 Dec 2012 19:22:01 +0100 Subject: [PATCH] Kind of Water spreading --- classes/API/BlockAPI.php | 77 +++++++++++++++++++++++-- classes/API/LevelAPI.php | 40 +++---------- classes/Data.class.php | 19 ++++++ classes/PocketMinecraftServer.class.php | 21 +++---- 4 files changed, 111 insertions(+), 46 deletions(-) diff --git a/classes/API/BlockAPI.php b/classes/API/BlockAPI.php index 870eb0d4..b82ebf83 100644 --- a/classes/API/BlockAPI.php +++ b/classes/API/BlockAPI.php @@ -39,7 +39,7 @@ class BlockAPI{ } public function init(){ - $this->server->addHandler("world.block.update", array($this, "handle")); + $this->server->addHandler("world.block.update", array($this, "updateBlockRemote")); $this->server->addHandler("player.block.break", array($this, "blockBreak")); $this->server->addHandler("player.block.action", array($this, "blockAction")); } @@ -175,8 +175,7 @@ class BlockAPI{ default: $cancelPlace = true; break; - } - + } } if($cancelPlace === true or $data["face"] < 0 or $data["face"] > 5){ @@ -347,6 +346,7 @@ class BlockAPI{ break; } $this->server->trigger("player.block.place", $data); + $this->updateBlock($data["x"], $data["y"], $data["z"], BLOCK_UPDATE_NORMAL); $this->updateBlocksAround($data["x"], $data["y"], $data["z"], BLOCK_UPDATE_NORMAL); } @@ -354,15 +354,82 @@ class BlockAPI{ $this->updateBlock($data["x"], $data["y"], $data["z"], BLOCK_UPDATE_SCHEDULED); } - public function updateBlock($x, $y, $z, $type){ + public function updateBlockRemote($data, $event){ + if($event !== "world.block.update"){ + return; + } + $this->updateBlock($data["x"], $data["y"], $data["z"], isset($data["type"]) ? $data["type"]:BLOCK_UPDATE_RANDOM); + } + + public function updateBlock($x, $y, $z, $type = BLOCK_UPDATE_NORMAL){ $block = $this->server->api->level->getBlock($x, $y, $z); $changed = false; switch($block[0]){ + case 8: + case 9: + $blockDown = $this->server->api->level->getBlock($x, $y - 1, $z); + $down = false; + if(isset(Material::$flowable[$blockDown[0]])){ + $this->server->schedule(5, array($this, "blockScheduler"), array( + "x" => $x, + "y" => $y - 1, + "z" => $z, + "type" => BLOCK_UPDATE_NORMAL, + )); + $this->server->api->level->setBlock($x, $y - 1, $z, 8, 8); + if($down[0] !== 9){ + $down = true; + } + } + if(($block[1] & 0x07) < 7 and ($down === false or $block[0] === 9)){ + $b0 = $this->server->api->level->getBlock($x + 1, $y, $z); + if(isset(Material::$flowable[$b0[0]]) and $b0[0] !== 9){ + $this->server->schedule(10, array($this, "blockScheduler"), array( + "x" => $x + 1, + "y" => $y, + "z" => $z, + "type" => BLOCK_UPDATE_NORMAL, + )); + $this->server->api->level->setBlock($x + 1, $y, $z, 8, ($block[1] + 1) & 0x07); + } + $b1 = $this->server->api->level->getBlock($x - 1, $y, $z); + if(isset(Material::$flowable[$b1[0]]) and $b1[0] !== 9){ + $this->server->schedule(10, array($this, "blockScheduler"), array( + "x" => $x - 1, + "y" => $y, + "z" => $z, + "type" => BLOCK_UPDATE_NORMAL, + )); + $this->server->api->level->setBlock($x - 1, $y, $z, 8, ($block[1] + 1) & 0x07); + } + $b2 = $this->server->api->level->getBlock($x, $y, $z + 1); + if(isset(Material::$flowable[$b2[0]]) and $b2[0] !== 9){ + $this->server->schedule(10, array($this, "blockScheduler"), array( + "x" => $x, + "y" => $y, + "z" => $z + 1, + "type" => BLOCK_UPDATE_NORMAL, + )); + $this->server->api->level->setBlock($x, $y, $z + 1, 8, ($block[1] + 1) & 0x07); + } + $b3 = $this->server->api->level->getBlock($x, $y, $z - 1); + if(isset(Material::$flowable[$b3[0]]) and $b3[0] !== 9){ + $this->server->schedule(10, array($this, "blockScheduler"), array( + "x" => $x, + "y" => $y, + "z" => $z - 1, + "type" => BLOCK_UPDATE_NORMAL, + )); + $this->server->api->level->setBlock($x, $y, $z - 1, 8, ($block[1] + 1) & 0x07); + } + } + break; case 74: - if($type === BLOCK_UPDATE_SCHEDULED){ + if($type === BLOCK_UPDATE_SCHEDULED or $type === BLOCK_UPDATE_RANDOM){ $changed = true; $this->server->api->level->setBlock($x, $y, $z, 73, $block[1]); + $type = BLOCK_UPDATE_WEAK; } break; case 73: diff --git a/classes/API/LevelAPI.php b/classes/API/LevelAPI.php index b0479253..39400edf 100644 --- a/classes/API/LevelAPI.php +++ b/classes/API/LevelAPI.php @@ -26,13 +26,11 @@ the Free Software Foundation, either version 3 of the License, or */ class LevelAPI{ - private $server, $map, $active = false; + private $server, $map; function __construct($server){ $this->server = $server; $this->map = $this->server->map; - if($this->map !== false){ - $this->active = true; - } + $this->heightMap = array_fill(0, 256, array()); } public function init(){ @@ -57,42 +55,25 @@ class LevelAPI{ break; } } - - private function check(){ - if($this->active === true){ - return true; - }elseif($this->active === false and $this->server->map === false){ - return false; - } - $this->active = true; - return true; - } public function getChunk($X, $Z){ - if($this->check() and isset($this->map->map[$X][$Z])){ - return $this->map->map[$X][$Z]; - } - return false; + return $this->map->map[$X][$Z]; } public function getBlock($x, $y, $z){ - if($this->check()){ - return $this->map->getBlock($x, $y, $z); - } - return array(0,0); + return $this->map->getBlock($x, $y, $z); } public function getFloor($x, $z){ - if($this->check()){ - return $this->map->getFloor($x, $z); + if(!isset($this->heightMap[$z][$x])){ + $this->heightMap[$z][$x] = $this->map->getFloor($x, $z); } - return 0; + return $this->heightMap[$z][$x]; } public function setBlock($x, $y, $z, $block, $meta = 0){ - if($this->check()){ - $this->map->setBlock($x, $y, $z, $block, $meta); - } + $this->map->setBlock($x, $y, $z, $block, $meta); + $this->heightMap[$z][$x] = $this->map->getFloor($x, $z); $this->server->trigger("world.block.change", array( "x" => $x, "y" => $y, @@ -105,9 +86,6 @@ class LevelAPI{ public function getOrderedChunk($X, $Z, $columnsPerPacket = 2){ $columnsPerPacket = max(1, (int) $columnsPerPacket); $c = $this->getChunk($X, $Z); - if($c === false){ - return array(str_repeat("\x00", 256)); - } $ordered = array(); $i = 0; $cnt = 0; diff --git a/classes/Data.class.php b/classes/Data.class.php index 458b6547..fad9c727 100644 --- a/classes/Data.class.php +++ b/classes/Data.class.php @@ -26,6 +26,25 @@ the Free Software Foundation, either version 3 of the License, or */ class Material{ + static $flowable = array( + 0 => true, + 6 => true, + //8 => true, + //9 => true, + 30 => true, + 31 => true, + 32 => true, + 37 => true, + 38 => true, + 39 => true, + 40 => true, + 50 => true, + 51 => true, + 55 => true, + 59 => true, + 78 => true, + 105 => true, + ); static $unbreakable = array( 0 => true, 7 => true, diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index 929c4feb..452887ae 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -102,16 +102,16 @@ class PocketMinecraftServer extends stdClass{ public function startDatabase(){ $this->preparedSQL = new stdClass(); $this->database = new SQLite3(":memory:"); - $this->query("PRAGMA journal_mode = OFF;"); - $this->query("PRAGMA encoding = \"UTF-8\";"); - $this->query("PRAGMA secure_delete = OFF;"); + //$this->query("PRAGMA journal_mode = OFF;"); + //$this->query("PRAGMA encoding = \"UTF-8\";"); + //$this->query("PRAGMA secure_delete = OFF;"); $this->query("CREATE TABLE players (clientID INTEGER PRIMARY KEY, EID NUMERIC, ip TEXT, port NUMERIC, name TEXT UNIQUE);"); $this->query("CREATE TABLE entities (EID INTEGER PRIMARY KEY, type NUMERIC, class NUMERIC, name TEXT, x NUMERIC, y NUMERIC, z NUMERIC, yaw NUMERIC, pitch NUMERIC, health NUMERIC);"); $this->query("CREATE TABLE metadata (EID INTEGER PRIMARY KEY, name TEXT, value TEXT);"); $this->query("CREATE TABLE actions (ID INTEGER PRIMARY KEY, interval NUMERIC, last NUMERIC, code TEXT, repeat NUMERIC);"); $this->query("CREATE TABLE events (ID INTEGER PRIMARY KEY, name TEXT);"); $this->query("CREATE TABLE handlers (ID INTEGER PRIMARY KEY, name TEXT, priority NUMERIC);"); - $this->query("PRAGMA synchronous = OFF;"); + //$this->query("PRAGMA synchronous = OFF;"); $this->preparedSQL->selectHandlers = $this->database->prepare("SELECT ID FROM handlers WHERE name = :name ORDER BY priority DESC;"); $this->preparedSQL->selectEvents = $this->database->prepare("SELECT ID FROM events WHERE name = :name;"); $this->preparedSQL->selectActions = $this->database->prepare("SELECT ID,code,repeat FROM actions WHERE last <= (:time - interval);"); @@ -429,14 +429,11 @@ class PocketMinecraftServer extends stdClass{ } public function process(){ - $cnt = 0; while($this->stop === false){ $packet = @$this->interface->readPacket(); - if($packet !== false and $cnt < 20){ + if($packet !== false){ $this->packetHandler($packet); - ++$cnt; }else{ - $cnt = 0; usleep(1); } } @@ -467,12 +464,16 @@ class PocketMinecraftServer extends stdClass{ return false; } - public function schedule($ticks, $callback, $data = array(), $eventName = "server.schedule"){ + public function schedule($ticks, $callback, $data = array(), $repeat = false, $eventName = "server.schedule"){ if(!is_callable($callback)){ return false; } + $add = ""; + if($repeat === false){ + $add = ' unset($this->schedule['.$this->scheduleCnt.']);'; + } $this->schedule[$this->scheduleCnt] = array($callback, $data, $eventName); - $this->action(50000 * $ticks, '$schedule = $this->schedule['.$this->scheduleCnt.']; unset($this->schedule['.$this->scheduleCnt.']); call_user_func($schedule[0], $schedule[1], $schedule[2]);', false); + $this->action(50000 * $ticks, '$schedule = $this->schedule['.$this->scheduleCnt.'];'.$add.' call_user_func($schedule[0], $schedule[1], $schedule[2]);', (bool) $repeat); return $this->scheduleCnt++; }