new Structure.php

This commit is contained in:
ArkQuark 2023-03-03 13:27:24 +03:00
parent 8d67ebd2d4
commit 1c606f3e5e
4 changed files with 80 additions and 152 deletions

View file

@ -15,10 +15,7 @@ class MyceliumBlock extends SolidBlock{
} }
public function onActivate(Item $item, Player $player){ //uwu public function onActivate(Item $item, Player $player){ //uwu
/*$f = Utils::randomFloat(); //(new VillageLibraryStructure())::build($this->level, $this->getX(), $this->getY(), $this->getZ());
if($f <= 0.33) VillageLibraryStructure::buildStructure($this->level, $this->getX(), $this->getY(), $this->getZ());
elseif($f <= 0.67) SmallHouseStructure::buildStructure($this->level, $this->getX(), $this->getY(), $this->getZ());
else WoodHutStructure::buildStructure($this->level, $this->getX(), $this->getY(), $this->getZ());*/
} }
public function getDrops(Item $item, Player $player){ public function getDrops(Item $item, Player $player){

View file

@ -8,61 +8,60 @@
abstract class Structure{ abstract class Structure{
const MAP_NO_KEY = -255; const MAP_NO_KEY = -255;
const LEVEL_RSV1 = 1; const LEVEL_RSV1 = 1;
private $structure; private static $structure, $tmpStructure;
public $api, $pm; public $api, $pm;
public $mapSymToID, $width, $radius; public static $map, $width, $lenght;
public function __construct($struct, $width, $symToID = []){ public function __construct($width, $lenght, $charToBlock = []){
$this->structure = $struct;
$this->mapSymToID = $symToID;
$this->pm = ServerAPI::request(); $this->pm = ServerAPI::request();
$this->api = $this->pm->api; $this->api = $this->pm->api;
$this->width = $width; self::$width = $width;
$this->radius = $width / 2; self::$lenght = $lenght;
self::$map = $charToBlock;
/*foreach($charToBlock as $char => $array){
$this->addMapping($char, , isset($array) ? $array[1] : 0);
}*/
} }
public function addMapping($sym, $id){ public function addMapping($char, $blockClass, $meta){
$this->mapSymToID[$sym] = $id; self::$map[$char] = new $blockClass($meta);
} }
public function getMappingFor($sym){
return isset($this->mapSymToID[$sym]) ? $this->mapSymToID[$sym] : Structure::MAP_NO_KEY; public static function getMappingFor($char){
return isset(self::$map[$char]) ? self::$map[$char] : Structure::MAP_NO_KEY;
} }
protected function placeBlock($level, $sym, &$tv){ protected static function placeBlock($level, $char, &$vector){
//console("@@@"); if($char == "") return;
if($level instanceof Level){ if($level instanceof Level){
$idm = $this->getMappingFor($sym); $blockClass = is_array(self::$map[$char]) ? self::$map[$char][0] : self::$map[$char];
if(!isset($idm[1])) $idm[1] = 0; $block = new $blockClass(isset(self::$map[$char]) ? self::$map[$char][1] : 0, isset(self::$map[$char][2]) ? self::$map[$char][2] : 0);
//var_dump($idm); //var_dump($block);
if($idm === Structure::MAP_NO_KEY){ /*if($block === Structure::MAP_NO_KEY){
//console("Failed to receive id"); //console("Failed to receive id");
return false; return false;
} }*/
$block = $this->api->block->get($idm[0], $idm[1], $tv); //TODO: meta $level->setBlock($vector, $block, true, false, true);
//console($block);
$level->setBlock($tv, $block, true, false, true);
} }
} }
public function build($level, $centerX, $centerY, $centerZ){ public static function build($level, $centerX, $centerY, $centerZ, $structure){
//console("b"); //console("b");
$tempVector = new Vector3(0,0,0); $offsetX = 0;
$x = $centerX - $this->radius; $offsetZ = 0;
$z = $centerZ - $this->radius; foreach($structure as $offsetY => $blocksXZ){
foreach($this->structure as $offsetY => $blocksXZ){
foreach($blocksXZ as $blocks){ foreach($blocksXZ as $blocks){
$blocks = rtrim($blocks);
foreach(str_split($blocks) as $block){ foreach(str_split($blocks) as $block){
$tempVector->setXYZ($x, $centerY + $offsetY, $z); $vector = new Vector3($centerX - floor(self::$width / 2) + $offsetX, $centerY + $offsetY, $centerZ + $offsetZ);
//console("6#&_+7$:373-"); //$tempVector->setXYZ($x, $centerY + $offsetY, $z);
$this->placeBlock($level, $block, $tempVector); self::placeBlock($level, $block, $vector);
++$x; ++$offsetX;
} }
++$z; ++$offsetZ;
$x = $centerX - $this->radius; $offsetX = 0;
} }
$z = $centerZ - $this->radius; $offsetZ = 0;
} }
} }

View file

@ -1,6 +1,6 @@
<?php <?php
class SmallHouseStructure{ class SmallHouseStructure extends Structure{
public static $width = 5; public static $width = 5;
public static $lenght = 5; public static $lenght = 5;
public static $tmpStructure; public static $tmpStructure;
@ -39,9 +39,24 @@ class SmallHouseStructure{
"WPPPW", "WPPPW",
"WPPPW", "WPPPW",
"WWWWW", "WWWWW",
] ],
]; ];
public static $map = [
"C" => "CobbleStoneBlock",
"P" => "PlanksBlock",
"W" => "WoodBlock",
"D" => "DirtBlock",
"G" => "GlassPaneBlock",
"L" => ["LadderBlock", 2],
"F" => "FenceBlock",
" " => "AirBlock"
];
public function __construct($width = 0, $lenght = 0, $charToBlock = []){
parent::__construct(self::$width, self::$lenght, self::$map);
}
public static function generateFence(){ public static function generateFence(){
self::$tmpStructure = self::$structure; self::$tmpStructure = self::$structure;
if(Utils::chance(50)){ if(Utils::chance(50)){
@ -59,52 +74,9 @@ class SmallHouseStructure{
} }
} }
public static function buildStructure($level, $x, $y, $z){ /*use CENTER positions*/ public static function build($level, $x, $y, $z, $structure = []){
self::generateFence(); self::generateFence();
$offsetX = 0; parent::build($level, $x, $y, $z, self::$tmpStructure);
$offsetZ = 0;
foreach(self::$tmpStructure as $layerCount => $layer){
foreach($layer as $line){
$line = rtrim($line); //remove useless spaces(only from right)
foreach(str_split($line) as $char){
$vector = new Vector3($x - floor(self::$width / 2) + $offsetX, $y + $layerCount, $z + $offsetZ);
switch($char){
case "C":
$level->setBlockRaw($vector, new CobbleStoneBlock());
break;
case "P":
$level->setBlockRaw($vector, new PlanksBlock());
break;
case "W":
$level->setBlockRaw($vector, new WoodBlock());
break;
case "D":
$level->setBlockRaw($vector, new DirtBlock());
break;
case "G":
$level->setBlockRaw($vector, new GlassPaneBlock());
break;
case "L":
$level->setBlockRaw($vector, new LadderBlock(2));
break;
case "F":
$level->setBlockRaw($vector, new FenceBlock());
break;
case " ":
$block = $level->getBlock($vector)->getID();
if($block === AIR){
break;
}
$level->setBlockRaw($vector, new AirBlock());
break;
}
++$offsetX;
}
++$offsetZ;
$offsetX = 0;
}
$offsetZ = 0;
}
} }
} }

View file

@ -1,6 +1,6 @@
<?php <?php
class VillageLibraryStructure{ class VillageLibraryStructure extends Structure{
public static $width = 9; public static $width = 9;
public static $lenght = 8; public static $lenght = 8;
public static $structure = [ public static $structure = [
@ -26,7 +26,7 @@ class VillageLibraryStructure{
], ],
2 => [ 2 => [
"", "",
"CdPPGGGPC", "C8PPGGGPC",
"P P", "P P",
"G G", "G G",
"G c c G", "G c c G",
@ -96,69 +96,29 @@ class VillageLibraryStructure{
] ]
]; ];
public static function buildStructure($level, $x, $y, $z){ /*use CENTER positions*/ public static $map = [
$offsetX = 0; "S" => ["CobbleStoneStairsBlock", 2],
$offsetZ = 0; "C" => "CobbleStoneBlock",
foreach(self::$structure as $layerCount => $layer){ "P" => "PlanksBlock",
foreach($layer as $line){ "W" => "WoodBlock",
$line = rtrim($line); //remove useless spaces(only from right) "d" => ["DoorBlock", 64],
foreach(str_split($line) as $char){ "8" => ["DoorBlock", 64, 0x08],
$vector = new Vector3($x - floor(self::$width / 2) + $offsetX, $y + $layerCount, $z + $offsetZ); "G" => "GlassPaneBlock",
switch($char){ "B" => "BookshelfBlock",
case "S": "F" => "FenceBlock",
$level->setBlockRaw($vector, new CobbleStoneStairsBlock(2)); "c" => ["CarpetBlock", 12],
break; "1" => ["WoodStairsBlock", 1],
case "C": "2" => ["WoodStairsBlock", 2],
$level->setBlockRaw($vector, new CobbleStoneBlock()); "3" => ["WoodStairsBlock", 3],
break; "T" => "WorkbenchBlock",
case "P": " " => "AirBlock"
$level->setBlockRaw($vector, new PlanksBlock()); ];
break;
case "W": public function __construct($width = 0, $lenght = 0, $charToBlock = []){
$level->setBlockRaw($vector, new WoodBlock()); parent::__construct(self::$width, self::$lenght, self::$map);
break; }
case "d":
if($layerCount == 1) $level->setBlockRaw($vector, new DoorBlock(64)); public static function build($level, $x, $y, $z, $structure = 0){
else $level->setBlockRaw($vector, new DoorBlock(64, 0x08)); parent::build($level, $x, $y, $z, self::$structure);
break;
case "G":
$level->setBlockRaw($vector, new GlassPaneBlock());
break;
case "B":
$level->setBlockRaw($vector, new BookshelfBlock());
break;
case "F":
$level->setBlockRaw($vector, new FenceBlock());
break;
case "c":
$level->setBlockRaw($vector, new CarpetBlock(12));
break;
case "1":
$level->setBlockRaw($vector, new WoodStairsBlock(1));
break;
case "2":
$level->setBlockRaw($vector, new WoodStairsBlock(2));
break;
case "3":
$level->setBlockRaw($vector, new WoodStairsBlock(3));
break;
case "T":
$level->setBlockRaw($vector, new WorkbenchBlock());
break;
case " ":
$block = $level->getBlock($vector)->getID();
if($block === AIR){
break;
}
$level->setBlockRaw($vector, new AirBlock());
break;
}
++$offsetX;
}
++$offsetZ;
$offsetX = 0;
}
$offsetZ = 0;
}
} }
} }