PageRenderTime 78ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/classes/SuperflatGenerator.class.php

https://github.com/domanicYL/PocketMine-MP
PHP | 107 lines | 74 code | 9 blank | 24 comment | 6 complexity | bb09cc5fae739693f9477550df6cacc2 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /*
  3. -
  4. / \
  5. / \
  6. / PocketMine \
  7. / MP \
  8. |\ @shoghicp /|
  9. |. \ / .|
  10. | .. \ / .. |
  11. | .. | .. |
  12. | .. | .. |
  13. \ | /
  14. \ | /
  15. \ | /
  16. \ | /
  17. This program is free software: you can redistribute it and/or modify
  18. it under the terms of the GNU Lesser General Public License as published by
  19. the Free Software Foundation, either version 3 of the License, or
  20. (at your option) any later version.
  21. */
  22. class SuperflatGenerator{
  23. private $config, $spawn, $structure;
  24. public function __construct($seed){
  25. $this->config = array(
  26. "preset" => "7;20x1;3x3;2",
  27. "spawn-surface" => 24,
  28. "spawn-radius" => 10,
  29. "torches" => 0,
  30. "seed" => (int) $seed,
  31. );
  32. $this->parsePreset();
  33. }
  34. public function set($name, $value){
  35. $this->config[$name] = $value;
  36. if($name === "preset"){
  37. $this->parsePreset();
  38. }
  39. }
  40. private function parsePreset(){
  41. $this->structure = array(
  42. 0 => "",
  43. 1 => "",
  44. 2 => str_repeat("\x00", 64),
  45. 3 => str_repeat("\x00", 64),
  46. );
  47. $preset = explode(";", trim($this->config["preset"]));
  48. foreach($preset as $i => $data){
  49. $num = 1;
  50. if(preg_match('#([a-zA-Z\-_]*)\((.*)\)#', $data, $matches) > 0){ //Property
  51. $this->config[$matches[1]] = $matches[2];
  52. continue;
  53. }elseif(preg_match('#([0-9]*)x([0-9:]*)#', $data, $matches) > 0){
  54. $num = (int) $matches[1];
  55. $d = explode(":", $matches[2]);
  56. }else{
  57. $d = explode(":", $data);
  58. }
  59. $block = (int) array_shift($d);
  60. $meta = (int) @array_shift($d);
  61. for($j = 0; $j < $num; ++$j){
  62. $this->structure[0] .= chr($block & 0xFF);
  63. $this->structure[1] .= substr(dechex($meta & 0x0F), -1);
  64. }
  65. }
  66. $this->structure[1] = pack("h*", str_pad($this->structure[1], (strlen($this->structure[1])&0xFE) + 2, "0", STR_PAD_RIGHT)); //invert nibbles
  67. $this->structure[0] = substr($this->structure[0], 0, 128);
  68. $this->structure[1] = substr($this->structure[1], 0, 64);
  69. $this->structure[2] = substr($this->structure[2], 0, 64);
  70. $this->structure[3] = substr($this->structure[3], 0, 64);
  71. }
  72. public function init(){
  73. $this->spawn = array(128, strlen($this->structure[0]), 128);
  74. }
  75. public function getSpawn(){
  76. return $this->spawn;
  77. }
  78. public function getColumn($x, $z){
  79. $x = (int) $x;
  80. $z = (int) $z;
  81. $column = $this->structure;
  82. if(floor(sqrt(pow($x - $this->spawn[0], 2) + pow($z - $this->spawn[2], 2))) <= $this->config["spawn-radius"]){
  83. $column[0]{strlen($column[0])-1} = chr($this->config["spawn-surface"]);
  84. }
  85. if(($x % 8) === 0 and ($z % 8) === 0 and $this->config["torches"] == "1"){
  86. $column[0] .= chr(50);
  87. }
  88. $column[0] .= str_repeat(chr(0), 128 - strlen($column[0]));
  89. $column[1] .= str_repeat(chr(0), 64 - strlen($column[1]));
  90. $column[2] .= str_repeat(chr(0), 64 - strlen($column[2]));
  91. $column[3] .= str_repeat(chr(0), 64 - strlen($column[3]));
  92. return $column;
  93. }
  94. }