Default Generator Presets

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-23 18:53:37 +01:00
parent e0c245d86e
commit fbfbd53204
4 changed files with 53 additions and 19 deletions

View File

@ -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();

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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;