From aa9080d91f41dfb7bf50cf6bc04079711c1bfc36 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 4 Mar 2023 23:42:32 +0300 Subject: [PATCH] 90, 180, 240 deg rot + structure fixes --- src/material/block/solid/MyceliumBlock.php | 15 +++-- src/structure/Structure.php | 77 +++++++++++++++++----- 2 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/material/block/solid/MyceliumBlock.php b/src/material/block/solid/MyceliumBlock.php index b533d05f..af31e1e6 100644 --- a/src/material/block/solid/MyceliumBlock.php +++ b/src/material/block/solid/MyceliumBlock.php @@ -15,13 +15,14 @@ class MyceliumBlock extends SolidBlock{ } public function onActivate(Item $item, Player $player){ //uwu - /*$oldtime = microtime(1); - if(mt_rand(0, 1) === 0){ - $time = Structures::$SMALLFARM_VILLAGE->rotate90deg($this->level, $this->x, $this->y, $this->z)->build($this->level, $this->x, $this->y, $this->z); - }else{ - $time = Structures::$SMALLFARM_VILLAGE->build($this->level, $this->x, $this->y, $this->z); - } - console("builded in ".($time - $oldtime));*/ + //$oldtime = microtime(true); + //if(mt_rand(0, 1) === 0){ + // Structures::$SMALLFARM_VILLAGE->rotate(Structure::DEG_240, $this->level)->build($this->level, $this->x, $this->y, $this->z); + //}else{ + // Structures::$SMALLFARM_VILLAGE->build($this->level, $this->x, $this->y, $this->z); + //} + //$time = microtime(true); + //console("builded in ".($time - $oldtime)); //(new WoodHutStructure())->build($this->level, $this->getX(), $this->getY(), $this->getZ()); } diff --git a/src/structure/Structure.php b/src/structure/Structure.php index d0c6a09b..d81863ba 100644 --- a/src/structure/Structure.php +++ b/src/structure/Structure.php @@ -7,6 +7,10 @@ */ abstract class Structure{ const MAP_NO_KEY = -127; + const DEG_90 = 0; + const DEG_180 = 1; + const DEG_240 = 2; + protected $structure; protected $map; public $width, $length; @@ -15,8 +19,8 @@ abstract class Structure{ public function __construct(){ foreach($this->structure as &$s){ foreach($s as &$e){ - if(strlen($e) < $this->length){ - $e .= str_repeat(" ", $this->length - strlen($e)); + if(strlen($e) < $this->width){ + $e .= str_repeat(" ", $this->width - strlen($e)); } } } @@ -38,27 +42,67 @@ abstract class Structure{ 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; + + public function rotate($rotationLevel = self::DEG_90, $level = null, $x = 0, $y = 0, $z = 0){ + $str = $level instanceof Level ? $this->getFinalStructure($level, $x, $y, $z) : $this->structure; + var_dump($str); + switch($rotationLevel){ + case self::DEG_90: + $l = $this->width; + $this->width = $this->length; + $this->length = $l; + + 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; + } + break; + case self::DEG_180: + foreach($str as &$lay){ + $keys = array_keys($lay); + $ak = count($keys)-1; + $lay = array_reverse($lay, true); + foreach($lay as $k => $v){ + unset($lay[$k]); + $lay[$keys[$ak--]] = strrev($v); + } + } + break; + case self::DEG_240: + $l = $this->width; + $this->width = $this->length; + $this->length = $l; + foreach($str as &$arr){ + foreach($arr as &$e){ + $e = str_split(strrev($e)); + } + $new = []; + for($i = 0; $i < $this->length; ++$i){ + $new[] = implode("", array_column($arr, $i)); + } + $arr = $new; + } + + break; //TODO fix + default: + ConsoleAPI::warn(self::class." tried to rotate to invalid state($rotationLevel)"); + return $this; } - return (clone $this)->setStructure($str, $this->length, $this->width); + var_dump($str); + return (clone $this)->setStructure($str, $this->width, $this->length); } private function setStructure($struct, $width, $length){ $this->structure = $struct; $this->width = $width; $this->length = $length; - //console("Modifed"); + ConsoleAPI::debug("Modifed"); return $this; } @@ -79,7 +123,6 @@ abstract class Structure{ } $offsetZ = 0; } - return microtime(1); } } \ No newline at end of file