From fbfbd5320446e20a70e8c1da02fae96c71659951 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Sun, 23 Dec 2012 18:53:37 +0100 Subject: [PATCH] Default Generator Presets --- classes/API/ServerAPI.php | 9 ++++-- classes/DefaultGenerator.class.php | 41 +++++++++++++++++-------- classes/PocketMinecraftServer.class.php | 7 +++-- classes/Session.class.php | 15 +++++++-- 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/classes/API/ServerAPI.php b/classes/API/ServerAPI.php index ae3eb4db..7931fc76 100644 --- a/classes/API/ServerAPI.php +++ b/classes/API/ServerAPI.php @@ -121,14 +121,17 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run $this->server->mapDir = FILE_PATH."data/maps/".$this->server->mapName."/"; if($this->server->mapName === false or trim($this->server->mapName) === "" or !file_exists($this->server->mapDir."chunks.dat")){ $this->server->mapName = "world"; - $this->setProperty("seed", $this->server->seed); - $this->setProperty("level-name", $this->server->mapName); - $this->setProperty("gamemode", 1); $this->server->mapDir = FILE_PATH."data/maps/".$this->server->mapName."/"; $this->gen = new Generator("DefaultGenerator", $this->server->seed); + if($this->getProperty("generator-settings") !== false){ + $this->gen->set("preset", $this->getProperty("generator-settings")); + } $this->gen->init(); $this->gen->generate(); $this->gen->save($this->server->mapDir."chunks.dat"); + $this->setProperty("seed", $this->server->seed); + $this->setProperty("level-name", $this->server->mapName); + $this->setProperty("gamemode", 1); $s = $this->gen->getSpawn(); $this->setProperty("spawn", array("x" => $s[0], "y" => $s[1], "z" => $s[2])); $array = array(); diff --git a/classes/DefaultGenerator.class.php b/classes/DefaultGenerator.class.php index adcb7fe0..91dea6e8 100644 --- a/classes/DefaultGenerator.class.php +++ b/classes/DefaultGenerator.class.php @@ -27,27 +27,46 @@ the Free Software Foundation, either version 3 of the License, or class DefaultGenerator{ - private $config, $spawn, $seed; + private $config, $spawn, $seed, $structure; public function __construct($seed){ $this->seed = (int) $seed; $this->config = array( - "base-height" => 30, - "surface" => 2, + "preset" => "7;20x1;3x3;2;spawn-surface(24);spawn-radius(10)", "spawn-surface" => 24, "spawn-radius" => 10, - "fill" => 3, - "floor" => 7, ); - $this->spawn = array(128, $this->config["base-height"] + 1, 128); + $this->parsePreset(); } public function set($name, $value){ $this->config[$name] = $value; + if($name === "preset"){ + $this->parsePreset(); + } + } + + private function parsePreset(){ + $preset = explode(";", $this->config["preset"]); + $this->structure = ""; + foreach($preset as $i => $data){ + if(preg_match('#([a-zA-Z\-_]*)\((.*)\)#', $data, $matches) > 0){ //Property + $this->config[$matches[1]] = $matches[2]; + }elseif(preg_match('#([0-9]*)x([0-9]*)#', $data, $matches) > 0){ + $num = (int) $matches[1]; + $block = (int) $matches[2]; + for($j = 0; $j < $num; ++$j){ + $this->structure .= chr($block%256); + } + }else{ + $block = (int) $data; + $this->structure .= chr($block%256); + } + } + $this->structure = substr($this->structure, 0, 128); } public function init(){ - $this->config["base-height"] = max(2, $this->config["base-height"]); - $this->spawn = array(128, $this->config["base-height"] + 1, 128); + $this->spawn = array(128, strlen($this->structure), 128); } public function getSpawn(){ @@ -63,11 +82,9 @@ class DefaultGenerator{ 2 => "", 3 => "", ); - $column[0] = chr($this->config["floor"]) . str_repeat(chr($this->config["fill"]), $this->config["base-height"] - 2); + $column[0] = $this->structure; if(floor(sqrt(pow($x - $this->spawn[0], 2) + pow($z - $this->spawn[2], 2))) <= $this->config["spawn-radius"]){ - $column[0] .= chr($this->config["spawn-surface"]); - }else{ - $column[0] .= chr($this->config["surface"]); + $column[0]{strlen($column[0])-1} = chr($this->config["spawn-surface"]); } $column[0] .= str_repeat(chr(0), 128 - strlen($column[0])); $column[1] = str_repeat(chr(0), 64); diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index e94ec9e3..062b7a9a 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -306,8 +306,8 @@ class PocketMinecraftServer extends stdClass{ public function tick(){ $time = microtime(true); if($this->lastTick <= ($time - 0.05)){ - $this->tickMeasure[] = $this->lastTick = $time; array_shift($this->tickMeasure); + $this->tickMeasure[] = $this->lastTick = $time; $this->tickerFunction($time); $this->trigger("onTick", $time); } @@ -427,11 +427,14 @@ class PocketMinecraftServer extends stdClass{ } public function process(){ + $cnt = 0; while($this->stop === false){ $packet = @$this->interface->readPacket(); - if($packet !== false){ + if($packet !== false and $cnt < 20){ $this->packetHandler($packet); + ++$cnt; }else{ + $cnt = 0; usleep(1); } } diff --git a/classes/Session.class.php b/classes/Session.class.php index 74fdae3a..665a72d4 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -49,7 +49,7 @@ class Session{ console("[DEBUG] New Session started with ".$ip.":".$port.". MTU ".$this->MTU.", Client ID ".$this->clientID, true, true, 2); $this->connected = true; $this->auth = false; - $this->counter = array(0, 0); + $this->counter = array(0, 0, 0); } public function onTick($time, $event){ @@ -167,7 +167,7 @@ class Session{ case "onChat": $this->dataPacket(MC_CHAT, array( "message" => str_replace("@username", $this->username, $data), - )); + )); break; } } @@ -187,8 +187,19 @@ class Session{ } break; case 0xc0: //ACK + $diff = $data[2] - $this->counter[2]; + if($diff > 8){ //Packet recovery + array_unshift($this->queue, array(0, $this->buffer[$data[2]][0], $this->buffer[$data[2]][1], $data[2])); + } + $this->counter[2] = $data[2]; unset($this->buffer[$data[2]]); + if(isset($data[3])){ + $diff = $data[3] - $this->counter[2]; + if($diff > 8){ //Packet recovery + array_unshift($this->queue, array(0, $this->buffer[$data[3]][0], $this->buffer[$data[3]][1], $data[3])); + } + $this->counter[2] = $data[3]; unset($this->buffer[$data[3]]); } break;