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

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