Structure.php rewrite, all structures(except smallfarm) are probably broken now

This commit is contained in:
GameHerobrine 2023-03-04 17:32:15 +03:00
parent 64b9e376ca
commit 96a62953fa
9 changed files with 141 additions and 186 deletions

View file

@ -32,6 +32,7 @@ class PocketMinecraftServer{
console("[INFO] Starting Minecraft PE server on " . ($this->serverip === "0.0.0.0" ? "*" : $this->serverip) . ":" . $this->port);
EntityRegistry::registerEntities();
Structures::initialize();
define("BOOTUP_RANDOM", Utils::getRandomBytes(16));
$this->serverID = $this->serverID === false ? Utils::readLong(substr(Utils::getUniqueID(true, $this->serverip . $this->port), 8)) : $this->serverID;
$this->seed = $this->seed === false ? Utils::readInt(Utils::getRandomBytes(4, false)) : $this->seed;

View file

@ -15,6 +15,12 @@ class MyceliumBlock extends SolidBlock{
}
public function onActivate(Item $item, Player $player){ //uwu
/*if(mt_rand(0, 1) === 0){
Structures::$SMALLFARM_VILLAGE->rotate90deg($this->level, $this->x, $this->y, $this->z)->build($this->level, $this->x, $this->y, $this->z);
}else{
Structures::$SMALLFARM_VILLAGE->build($this->level, $this->x, $this->y, $this->z);
}*/
//(new WoodHutStructure())->build($this->level, $this->getX(), $this->getY(), $this->getZ());
}

View file

@ -1,77 +1,74 @@
<?php
/*XYZ in Structure String:
|--→x+
|--->x+
|
↓z+
|z+
*/
abstract class Structure{
const MAP_NO_KEY = -255;
const LEVEL_RSV1 = 1;
private static $structure, $tmpStructure;
private $map;
public $api, $pm, $width, $length, $name;
const MAP_NO_KEY = -127;
protected $structure;
protected $map;
public $width, $length;
public $name;
public function __construct($width = 0, $length = 0, $name = "Unknown", $map = []){
$this->pm = ServerAPI::request();
$this->api = $this->pm->api;
$this->width = $width;
$this->length = $length;
$this->name = $name;
$this->map = $map;
}
public function getName(){
return $this->name;
}
public function setName($name){
$this->name = $name;
}
public function parseMeta($meta){
if(!is_numeric($meta)){
$arrmeta = explode(" ", $meta);
if($arrmeta[0] === "random"){
$meta = mt_rand($arrmeta[1], $arrmeta[2]);
}else{
$meta = 0; //undefined
}
}
return $meta;
}
protected function getMappingFor($char){
if(!isset($this->map[$char])) return Structure::MAP_NO_KEY;
$blockClass = is_array($this->map[$char]) ? $this->map[$char][0] : $this->map[$char];
return new $blockClass(isset($this->map[$char][1]) ? $this->parseMeta($this->map[$char][1]) : 0, isset($this->map[$char][2]) ? $this->map[$char][2] : 0);
}
protected function placeBlock($level, $char, &$vector){
if($char == "") return;
if($level instanceof Level){
$block = $this->getMappingFor($char);
//var_dump($block);
if($block === Structure::MAP_NO_KEY){
//console("Failed to get block!");
return false;
public function __construct(){
foreach($this->structure as &$s){
foreach($s as &$e){
if(strlen($e) < $this->length){
$e .= str_repeat(" ", $this->length - strlen($e));
}
}
$level->setBlockRaw($vector, $block);
}
}
public function build($level, $centerX, $centerY, $centerZ, $structure = 0){
//console("building ".$this->name);
$offsetX = 0;
$offsetZ = 0;
foreach($structure as $offsetY => $blocksXZ){
protected function getMappingFor($char){
return nullsafe($this->map[$char], self::MAP_NO_KEY);
}
protected function placeBlock(Level $level, $char, $vector){
if($char === "" || (($mk = $this->getMappingFor($char)) === self::MAP_NO_KEY)) return;
$id = is_array($mk) ? $mk[0] : $mk;
$meta = is_array($mk) ? $mk[1] : 0;
return $level->setBlock($vector, BlockAPI::get($id, $meta));
}
protected function init(){}
protected function getFinalStructure(Level $level, $x, $y, $z){
return $this->structure;
}
public function rotate90deg(Level $level, $x, $y, $z){
$str = $this->getFinalStructure($level, $x, $y, $z);
foreach($str as &$arr){
foreach($arr as &$e){
$e = str_split($e);
}
$new = [];
for($i = 0; $i < $this->length; ++$i){
$new[] = implode("", array_reverse(array_column($arr, $i)));
}
$arr = $new;
}
return (clone $this)->setStructure($str, $this->length, $this->width);
}
private function setStructure($struct, $width, $length){
$this->structure = $struct;
$this->width = $width;
$this->length = $length;
console("Modifed");
return $this;
}
public function build(Level $level, $centerX, $centerY, $centerZ){
$offsetX = $offsetZ = 0;
$centWidth = floor($this->width / 2);
$centLength = floor($this->length / 2);
foreach($this->getFinalStructure($level, $centerX, $centerY, $centerZ) as $offsetY => $blocksXZ){
foreach($blocksXZ as $blocks){
$blocks = rtrim($blocks);
foreach(str_split($blocks) as $block){
if($centerY + $offsetY == 128) return false;
$vector = new Vector3($centerX - floor($this->width / 2) + $offsetX, $centerY + $offsetY, $centerZ + $offsetZ);
//$tempVector->setXYZ($x, $centerY + $offsetY, $z);
$this->placeBlock($level, $block, $vector);
$this->placeBlock($level, $block, new Vector3($centerX - $centWidth + $offsetX, $centerY + $offsetY, $centerZ - $centLength + $offsetZ));
++$offsetX;
}
++$offsetZ;
@ -79,8 +76,6 @@ abstract class Structure{
}
$offsetZ = 0;
}
//console("builded!");
return true;
}
}

View file

@ -1,10 +1,9 @@
<?php
class StrongholdLibraryStructure{
public static $width = 14;
public static $length = 15;
public static $tmpStructure;
public static $structure = [
public $width = 14;
public $length = 15;
public $structure = [
-1 => [
"SSSSSSSSSSSSSS",
"SSSSSSSSSSSSSS",
@ -194,7 +193,7 @@ class StrongholdLibraryStructure{
]
];
public static function replaceStoneBricks(){
public function replaceStoneBricks(){
foreach(self::$structure as $layerInt => $layer){
foreach($layer as $key => $str){
$line = str_split($str);
@ -214,7 +213,7 @@ class StrongholdLibraryStructure{
}
}
public static function placeCobweb(){
public function placeCobweb(){
foreach(self::$tmpStructure as $layerInt => $layer){
if($layerInt >= 0 and $layerInt <= 3){
foreach($layer as $key => $str){

View file

@ -2,10 +2,9 @@
class StrongholdPortalRoomStructure extends Structure{
public $width = 11;
public $length = 16;
public $name = "Portal Room";
private static $tmpStructure;
private static $structure = [
public $length = 16;
public $name = "Portal Room";
protected $structure = [
-1 => [
"SSSSSSSSSSS",
"SSSSSSSSSSS",
@ -133,7 +132,7 @@ class StrongholdPortalRoomStructure extends Structure{
"SSSSSSSSSSS",
]
];
private $map = [
protected $map = [
"S" => "StoneBricksBlock",
"M" => ["StoneBricksBlock", 1],
"C" => ["StoneBricksBlock", 2],
@ -195,10 +194,10 @@ class StrongholdPortalRoomStructure extends Structure{
parent::__construct($this->width, $this->length, $this->name, $this->map);
}
public function build($level, $x, $y, $z, $structure = 0){
/*public function build($level, $x, $y, $z, $structure = 0){
$this->replaceStoneBricks();
$this->placeEyes();
parent::build($level, $x, $y, $z, self::$tmpStructure);
}
}*/ // TODO
}

View file

@ -1,14 +1,10 @@
<?php
class SmallFarmStructure extends Structure{
public $width = 7;
public $length = 9;
public $width = 7;
public $length = 9;
public $name = "Small Farm";
private static $tmpStructure;
private static $structure = [
-1 => [
//add sand, clay if here isn't air
],
protected $structure = [
0 => [
"WWWWWWW",
"WFFwFFW",
@ -19,87 +15,55 @@ class SmallFarmStructure extends Structure{
"WFFwFFW",
"WFFwFFW",
"WWWWWWW",
]
],
1 => [
"",
" RR RR ",
" RR RR ",
" RR RR ",
" RR RR ",
" RR RR ",
" RR RR ",
" RR RR ",
"",
],
];
private $map = [
"W" => "WoodBlock",
"F" => "FarmlandBlock",
"w" => "WaterBlock",
"H" => ["WheatBlock", 0],
"C" => ["CarrotBlock", 0],
"P" => ["PotatoBlock", 0],
"B" => ["BeetrootBlock", 0],
" " => "AirBlock"
protected $map = [
"W" => WOOD,
"F" => FARMLAND,
"w" => WATER,
" " => 0,
"D" => DIRT
];
private function generateCrops(){
self::$tmpStructure = self::$structure;
$f = Utils::randomFloat();
if($f <= 0.5){
parent::setName("Wheat ".$this->name);
self::$tmpStructure[1] = [
"",
" HH HH ",
" HH HH ",
" HH HH ",
" HH HH ",
" HH HH ",
" HH HH ",
" HH HH ",
"",
];
}
elseif($f <= 0.7){
parent::setName("Carrot ".$this->name);
self::$tmpStructure[1] = [
"",
" CC CC ",
" CC CC ",
" CC CC ",
" CC CC ",
" CC CC ",
" CC CC ",
" CC CC ",
"",
];
}
elseif($f <= 0.9){
parent::setName("Potato ".$this->name);
self::$tmpStructure[1] = [
"",
" PP PP ",
" PP PP ",
" PP PP ",
" PP PP ",
" PP PP ",
" PP PP ",
" PP PP ",
"",
];
}
else{
parent::setName("Beetroot ".$this->name);
self::$tmpStructure[1] = [
"",
" BB BB ",
" BB BB ",
" BB BB ",
" BB BB ",
" BB BB ",
" BB BB ",
" BB BB ",
"",
];
protected function getFinalStructure(Level $level, $x, $y, $z)
{
if(!$level->getBlockWithoutVector($x, $y - 1, $z)->isSolid){
$structCopy = $this->structure;
$structCopy[-1] = array_fill(0, $this->length, str_repeat("D", $this->width)); //TODO sand or clay, check for the whole row of water?
return $structCopy;
}
return parent::getFinalStructure($level, $x, $y, $z);
}
public function __construct(){
parent::__construct($this->width, $this->length, $this->name, $this->map);
}
public function build($level, $x, $y, $z, $structure = 0){
$this->generateCrops();
parent::build($level, $x, $y, $z, self::$tmpStructure);
protected function getMappingFor($char){
if($char === "R"){
$f = Utils::randomFloat();
if($f <= 0.5){
return [WHEAT_BLOCK, 0];
}
if($f <= 0.7){
return [CARROT_BLOCK, 0];
}
if($f <= 0.9){
return [POTATO_BLOCK, 0];
}
return [BEETROOT_BLOCK, 0];
}
return parent::getMappingFor($char);
}
}

View file

@ -1,11 +1,10 @@
<?php
class SmallHouseStructure extends Structure{
public $width = 5;
public $length = 5;
public $width = 5;
public $length = 5;
public $name = "Small House";
private static $tmpStructure;
private static $structure = [
protected $structure = [
0 => [
"CCCCC",
"CCCCC",
@ -42,7 +41,7 @@ class SmallHouseStructure extends Structure{
"WWWWW",
],
];
private $map = [
protected $map = [
"C" => "CobbleStoneBlock",
"P" => "PlanksBlock",
"W" => "WoodBlock",
@ -53,10 +52,6 @@ class SmallHouseStructure extends Structure{
" " => "AirBlock"
];
public function __construct(){
parent::__construct($this->width, $this->length, $this->name, $this->map);
}
private function generateFence(){
self::$tmpStructure = self::$structure;
if(Utils::chance(50)){
@ -75,9 +70,9 @@ class SmallHouseStructure extends Structure{
}
}
public function build($level, $x, $y, $z, $structure = 0){
/*public function build($level, $x, $y, $z, $structure = 0){
$this->generateFence();
parent::build($level, $x, $y, $z, self::$tmpStructure);
}
}*/ //TODO
}

View file

@ -1,10 +1,10 @@
<?php
class VillageLibraryStructure extends Structure{
public $width = 9;
public $length = 8;
public $width = 9;
public $length = 8;
public $name = "Library";
private static $structure = [
protected $structure = [
0 => [
" S ",
"CCCCCCCCC",
@ -96,7 +96,7 @@ class VillageLibraryStructure extends Structure{
"",
]
];
private $map = [
protected $map = [
"S" => ["CobbleStoneStairsBlock", 2],
"C" => "CobbleStoneBlock",
"P" => "PlanksBlock",
@ -115,10 +115,11 @@ class VillageLibraryStructure extends Structure{
];
public function __construct(){
parent::__construct($this->width, $this->length, $this->name, $this->map);
parent::__construct();
}
/*
public function build($level, $x, $y, $z, $structure = 0){
parent::build($level, $x, $y, $z, self::$structure);
}
*/
}

View file

@ -1,11 +1,10 @@
<?php
class WoodHutStructure extends Structure{
public $width = 4;
public $width = 4;
public $length = 6;
public $name = "Wood Hut";
private static $tmpStructure;
private static $structure = [
protected $structure = [
0 => [
" S ",
"CCCC",
@ -39,7 +38,7 @@ class WoodHutStructure extends Structure{
"WPPW",
],
];
private $map = [
protected $map = [
"S" => ["CobbleStoneStairsBlock", 2],
"C" => "CobbleStoneBlock",
"P" => "PlanksBlock",
@ -93,14 +92,10 @@ class WoodHutStructure extends Structure{
}
}
public function __construct(){
parent::__construct($this->width, $this->length, $this->name, $this->map);
}
public function build($level, $x, $y, $z, $structure = 0){
/*public function build($level, $x, $y, $z, $structure = 0){
$this->generateRoof();
$this->generateTable();
parent::build($level, $x, $y, $z, self::$tmpStructure);
}
}*/ //TODO
}