World generation presets with metadata

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-23 20:02:58 +01:00
parent fbfbd53204
commit fe3c054c83

View File

@ -27,13 +27,14 @@ the Free Software Foundation, either version 3 of the License, or
class DefaultGenerator{ class DefaultGenerator{
private $config, $spawn, $seed, $structure; private $config, $spawn, $structure;
public function __construct($seed){ public function __construct($seed){
$this->seed = (int) $seed;
$this->config = array( $this->config = array(
"preset" => "7;20x1;3x3;2;spawn-surface(24);spawn-radius(10)", "preset" => "7;20x1;3x3;2",
"spawn-surface" => 24, "spawn-surface" => 24,
"spawn-radius" => 10, "spawn-radius" => 10,
"torches" => 0,
"seed" => (int) $seed,
); );
$this->parsePreset(); $this->parsePreset();
} }
@ -47,26 +48,41 @@ class DefaultGenerator{
private function parsePreset(){ private function parsePreset(){
$preset = explode(";", $this->config["preset"]); $preset = explode(";", $this->config["preset"]);
$this->structure = ""; $this->structure = array(
0 => "",
1 => "",
2 => str_repeat("\x00", 64),
3 => str_repeat("\x00", 64),
);
foreach($preset as $i => $data){ foreach($preset as $i => $data){
$num = 1;
if(preg_match('#([a-zA-Z\-_]*)\((.*)\)#', $data, $matches) > 0){ //Property if(preg_match('#([a-zA-Z\-_]*)\((.*)\)#', $data, $matches) > 0){ //Property
$this->config[$matches[1]] = $matches[2]; $this->config[$matches[1]] = $matches[2];
}elseif(preg_match('#([0-9]*)x([0-9]*)#', $data, $matches) > 0){ continue;
}elseif(preg_match('#([0-9]*)x([0-9:]*)#', $data, $matches) > 0){
$num = (int) $matches[1]; $num = (int) $matches[1];
$block = (int) $matches[2]; $d = explode(":", $matches[2]);
for($j = 0; $j < $num; ++$j){
$this->structure .= chr($block%256);
}
}else{ }else{
$block = (int) $data; $d = explode(":", $data);
$this->structure .= chr($block%256); }
$block = (int) array_shift($d);
$meta = (int) @array_shift($d);
for($j = 0; $j < $num; ++$j){
$this->structure[0] .= chr($block & 0xFF);
$this->structure[1] .= substr(dechex($meta & 0x0F),0,-1);
} }
} }
$this->structure = substr($this->structure, 0, 128); $this->structure[1] = Utils::hexToStr(str_pad($this->structure[1], (strlen($this->structure[1])&0xFE) + 2, "0", STR_PAD_RIGHT));
$this->structure[0] = substr($this->structure[0], 0, 128);
$this->structure[1] = substr($this->structure[1], 0, 64);
$this->structure[2] = substr($this->structure[2], 0, 64);
$this->structure[3] = substr($this->structure[3], 0, 64);
} }
public function init(){ public function init(){
$this->spawn = array(128, strlen($this->structure), 128); $this->spawn = array(128, strlen($this->structure[0]), 128);
} }
public function getSpawn(){ public function getSpawn(){
@ -82,14 +98,17 @@ class DefaultGenerator{
2 => "", 2 => "",
3 => "", 3 => "",
); );
$column[0] = $this->structure; $column = $this->structure;
if(floor(sqrt(pow($x - $this->spawn[0], 2) + pow($z - $this->spawn[2], 2))) <= $this->config["spawn-radius"]){ if(floor(sqrt(pow($x - $this->spawn[0], 2) + pow($z - $this->spawn[2], 2))) <= $this->config["spawn-radius"]){
$column[0]{strlen($column[0])-1} = chr($this->config["spawn-surface"]); $column[0]{strlen($column[0])-1} = chr($this->config["spawn-surface"]);
} }
if(($x % 8) === 0 and ($z % 8) === 0){
$column[0] .= chr(50);
}
$column[0] .= str_repeat(chr(0), 128 - strlen($column[0])); $column[0] .= str_repeat(chr(0), 128 - strlen($column[0]));
$column[1] = str_repeat(chr(0), 64); $column[1] .= str_repeat(chr(0), 64 - strlen($column[1]));
$column[2] = str_repeat(chr(0), 64); $column[2] .= str_repeat(chr(0), 64 - strlen($column[2]));
$column[3] = str_repeat(chr(0), 64); $column[3] .= str_repeat(chr(0), 64 - strlen($column[3]));
return $column; return $column;
} }