From e70c501df800def90260e527708bc350f3adfa49 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 8 Jul 2023 20:54:03 +0300 Subject: [PATCH] more stuff uses StaticBlock class now --- src/astarnavigator/TileNavigator.php | 3 +- .../algorithms/Pythagoras3D.php | 2 +- .../providers/MCBlockedProvider.php | 6 +-- .../providers/MCDiagonalProvider.php | 7 ++-- src/entity/Entity.php | 42 +------------------ src/entity/object/Arrow.php | 12 +++--- .../block/misc/NetherReactorBlock.php | 11 +++-- src/material/block/nonfull/FarmlandBlock.php | 6 +-- src/material/block/solid/GrassBlock.php | 6 +-- src/material/block/solid/LeavesBlock.php | 4 +- src/utils/RailLogic.php | 7 ++-- src/world/Level.php | 29 ++++++++----- 12 files changed, 49 insertions(+), 86 deletions(-) diff --git a/src/astarnavigator/TileNavigator.php b/src/astarnavigator/TileNavigator.php index 632e275a..c8b70c97 100644 --- a/src/astarnavigator/TileNavigator.php +++ b/src/astarnavigator/TileNavigator.php @@ -42,12 +42,11 @@ class TileNavigator implements ITileNavigator $current = $open->top(); $open->next(); if ($current == $to){ - return $this->reconstructPath($path, $current); } foreach($this->neighborProvider->getNeighbors($current) as $neighbor) { - if(!Utils::in_range(Utils::distance_noroot($neighbor->asArray(), $from->asArray()), -$maxDist, $maxDist)){ + if(!Utils::in_range(($from->x - $neighbor->x)*($from->x - $neighbor->x) + ($from->y - $neighbor->y)*($from->y - $neighbor->y) + ($from->z - $neighbor->z)*($from->z - $neighbor->z), -$maxDist, $maxDist)){ //Utils::distance_noroot($neighbor->asArray(), $from->asArray()) continue; } if(isset($visited[(string)$neighbor])){ diff --git a/src/astarnavigator/algorithms/Pythagoras3D.php b/src/astarnavigator/algorithms/Pythagoras3D.php index 7651a3ac..a350bb65 100644 --- a/src/astarnavigator/algorithms/Pythagoras3D.php +++ b/src/astarnavigator/algorithms/Pythagoras3D.php @@ -7,7 +7,7 @@ class Pythagoras3D implements IDistanceAlgorithm public function calculate(PathTile $from, PathTile $to) { if($from instanceof PathTileXYZ && $to instanceof PathTileXYZ){ - return Utils::distance_noroot($from->asArray(), $to->asArray()); + return sqrt(($to->x - $from->x) * ($to->x - $from->x) + ($to->y - $from->y) * ($to->y - $from->y) + ($to->z - $from->z) * ($to->z - $from->z)); } return INF; } diff --git a/src/astarnavigator/providers/MCBlockedProvider.php b/src/astarnavigator/providers/MCBlockedProvider.php index da3bfac1..9fdd40c4 100644 --- a/src/astarnavigator/providers/MCBlockedProvider.php +++ b/src/astarnavigator/providers/MCBlockedProvider.php @@ -5,11 +5,7 @@ class MCBlockedProvider implements IBlockedProvider { public function isBlocked(PathTile $tile) { - if($tile instanceof PathTileXYZ){ - $b = $tile->level->getBlockWithoutVector($tile->x, $tile->y, $tile->z); - return $b instanceof Block && $b->isSolid; - } - return false; + return $tile instanceof PathTileXYZ && StaticBlock::getIsSolid($tile->level->level->getBlockID($tile->x, $tile->y, $tile->z)); } } diff --git a/src/astarnavigator/providers/MCDiagonalProvider.php b/src/astarnavigator/providers/MCDiagonalProvider.php index 811c6838..d9f69c44 100644 --- a/src/astarnavigator/providers/MCDiagonalProvider.php +++ b/src/astarnavigator/providers/MCDiagonalProvider.php @@ -7,18 +7,17 @@ class MCDiagonalProvider implements INeighborProvider private static $moveDownOffset = [0, -1, 0]; public function getNeighbors(PathTile $tile) { - if($tile instanceof PathTileXYZ){ $pnts = []; foreach(MCDiagonalProvider::$neighbors as $offset){ $pnt = $tile->addOffset($offset); - if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y, $pnt->z, false)->isSolid){ - if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y - 1, $pnt->z, false)->isSolid){ + if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y, $pnt->z))){ + if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y - 1, $pnt->z, false))){ --$pnt->y; } $pnts[] = $pnt; }else{ - if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y + 1, $pnt->z, false)->isSolid){ + if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y + 1, $pnt->z, false))){ ++$pnt->y; $pnts[] = $pnt; } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 602c9551..c02d40c2 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -522,46 +522,6 @@ class Entity extends Position } ++$this->counter; if($this->isStatic === false){ - /*$startX = floor($this->x - 0.5 - $this->width - 1); - // prefix for flying when player on fence - $y = floor($this->y - 1); - $yC = ceil($this->y - 1); - $startZ = floor($this->z - 0.5 - $this->width - 1); - $endX = ceil($this->x - 0.5 + $this->width + 1); - $endZ = ceil($this->z - 0.5 + $this->width + 1); - $support = false; - $isFlying = true; - - if($this->isPlayer()){ - for($z = $startZ; $z <= $endZ; ++ $z){ - for($x = $startX; $x <= $endX; ++ $x){ - $v = new Vector3($x, $y, $z); - $v1 = new Vector3($x, $yC, $z); - if($this->isSupport($v, $this->width)){ - $b = $this->level->level->getBlockID($x, $y, $z); - if(StaticBlock::getIsSolid($b) === true){ - $support = true; - $isFlying = false; - break; - } elseif(StaticBlock::getIsLiquid($b) or $b === COBWEB or $b === LADDER or $b === FENCE or $b=== STONE_WALL or $b === IRON_BARS){ - $isFlying = false; - } - } elseif($this->isSupport($v1, $this->width)){ - $b = $this->level->getBlock($v1); - if($b->isSolid === true){ - $support = true; - $isFlying = false; - break; - } elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){ - $isFlying = false; - } - } - } - if($support === true){ - break; - } - } - }*/ //this one is not neccessary anymore? if(!$this->isPlayer()){ $update = false; if(Utils::in_range($this->speedX, -0.01, 0.01)){ @@ -645,7 +605,7 @@ class Entity extends Position $x = (int) ($this->x - 0.5); $z = (int) ($this->z - 0.5); $lim = (int) floor($ny); - for($y = (int) ceil($this->y) - 1; $y >= $lim; -- $y){ + for($y = floor($this->y); $y >= $lim; --$y){ if(StaticBlock::getIsSolid($this->level->level->getBlockID($x, $y, $z))){ //$support = true; $this->y = $ny; diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index f158ed54..5572f38b 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -89,12 +89,12 @@ class Arrow extends Projectile{ for($x = floor($rt->minX); $x < ceil($rt->maxX); ++$x){ for($z = floor($rt->minZ); $z < ceil($rt->maxZ); ++$z){ for($y = ceil($rt->minY); $y < ceil($rt->maxY); ++$y){ - $pos = new Vector3($x, $y, $z); - $b = $this->level->getBlock($pos); - if($b != false && $b->isSolid){ - $this->speedY = $b->boundingBox->calculateYOffset($this->boundingBox, $this->speedY); - $this->speedX = $b->boundingBox->calculateXOffset($this->boundingBox, $this->speedX); - $this->speedZ = $b->boundingBox->calculateZOffset($this->boundingBox, $this->speedZ); + $b = $this->level->level->getBlockID($x, $y, $z); + if(StaticBlock::getIsSolid($b)){ + $bb = StaticBlock::getBoundingBoxForBlockCoords($b, $x, $y, $z); + $this->speedY = $bb->calculateYOffset($this->boundingBox, $this->speedY); + $this->speedX = $bb->calculateXOffset($this->boundingBox, $this->speedX); + $this->speedZ = $bb->calculateZOffset($this->boundingBox, $this->speedZ); $this->inWall = true; break; } diff --git a/src/material/block/misc/NetherReactorBlock.php b/src/material/block/misc/NetherReactorBlock.php index cfba8be1..72972c97 100644 --- a/src/material/block/misc/NetherReactorBlock.php +++ b/src/material/block/misc/NetherReactorBlock.php @@ -61,8 +61,9 @@ class NetherReactorBlock extends SolidBlock{ for($a = $aOne; $a < $aTwo; $a += $aThree) { //wth those cycles are? TODO simplify if possible(makes server lag) for($b = $bOne; $b < $bTwo; $b += $bThree) { for($c = $cOne; $c < $cTwo; $c += $cThree) { - if ($this->level->getBlock(new Vector3($x+$a, $y+$b, $z+$c))->getID() === 87 && Utils::randomFloat() > 0.75){ - $this->level->setBlock(new Vector3($x+$a, $y+$b, $z+$c), new AirBlock()); + if ($this->level->level->getBlockID($x+$a, $y+$b, $z+$c) === NETHERRACK && Utils::randomFloat() > 0.75){ + //$this->level->setBlock(new Vector3($x+$a, $y+$b, $z+$c), new AirBlock()); + $this->level->fastSetBlockUpdate($x+$a, $y+$b, $z+$c, 0, 0); //TODO might be better to set not directly? } } } @@ -188,7 +189,7 @@ class NetherReactorBlock extends SolidBlock{ foreach($this->core as $yOffset => $layer){ foreach($layer as $line){ foreach(str_split($line) as $char){ - $b = $this->level->getBlock(new Vector3($x + $offsetX, $y + $yOffset, $z + $offsetZ))->getID(); + $b = $this->level->level->getBlockID($x + $offsetX, $y + $yOffset, $z + $offsetZ); switch($char){ case "G": if($b === GOLD_BLOCK){ //TODO make it use structure class @@ -201,7 +202,7 @@ class NetherReactorBlock extends SolidBlock{ } return false; case "R": - if($b === NETHER_REACTOR and $this->level->getBlock(new Vector3($x + $offsetX, $y + $yOffset, $z + $offsetZ))->getMetadata() === 0){ + if($b === NETHER_REACTOR and $this->level->level->getBlockDamage($x + $offsetX, $y + $yOffset, $z + $offsetZ) === 0){ break; } return false; @@ -210,8 +211,6 @@ class NetherReactorBlock extends SolidBlock{ break; } return false; - default: - break; } ++$offsetX; } diff --git a/src/material/block/nonfull/FarmlandBlock.php b/src/material/block/nonfull/FarmlandBlock.php index 9ea8ceff..f3e21432 100644 --- a/src/material/block/nonfull/FarmlandBlock.php +++ b/src/material/block/nonfull/FarmlandBlock.php @@ -18,17 +18,17 @@ class FarmlandBlock extends TransparentBlock{ public static function onRandomTick(Level $level, $x, $y, $z){ $meta = $level->level->getBlockDamage($x, $y, $z); - $b = $level->getBlockWithoutVector($x, $y + 1, $z, $level, false); + $b = $level->level->getBlockID($x, $y + 1, $z); if($meta === 0 && mt_rand(0, 5) === 0){ $water = self::checkWaterStatic($level, $x, $y, $z); if($water){ $level->setBlock(new Position($x, $y, $z, $level), BlockAPI::get(FARMLAND, 1), true, false, true); - }elseif(!(($b instanceof Block) && $b->isTransparent && $b->id != 0)){ + }elseif($b != 0 && StaticBlock::getIsTransparent($b)){ $level->setBlock(new Position($x, $y, $z, $level), BlockAPI::get(DIRT, 0), true, false, true); } } - if($b instanceof Block && !$b->isFlowable){ + if(!StaticBlock::getIsFlowable($b)){ $level->setBlock(new Position($x, $y, $z, $level), BlockAPI::get(DIRT, 0), true, false, true); } } diff --git a/src/material/block/solid/GrassBlock.php b/src/material/block/solid/GrassBlock.php index dc7cb0e2..99eec531 100644 --- a/src/material/block/solid/GrassBlock.php +++ b/src/material/block/solid/GrassBlock.php @@ -47,7 +47,7 @@ class GrassBlock extends SolidBlock{ return; } public static function onRandomTick(Level $level, $x, $y, $z){ - if(!$level->getBlockWithoutVector($x, $y + 1, $z, false)->isTransparent && mt_rand(0, 2) == 1){ + if(!StaticBlock::getIsTransparent($level->level->getBlockID($x, $y + 1, $z)) && mt_rand(0, 2) == 1){ $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0); }else{ for($cnt = 0; $cnt < 4; ++$cnt){ @@ -55,8 +55,8 @@ class GrassBlock extends SolidBlock{ $y = $y + mt_rand(0, 4) - 3; $z = $z + mt_rand(0, 2) - 1; - $blockUp = $level->getBlockWithoutVector($x, $y + 1, $z, false); - if($blockUp->isTransparent && !$blockUp->isLiquid && !($blockUp->getID() === 60) && $level->level->getBlockID($x, $y, $z) === DIRT){ + $blockUp = $level->level->getBlockID($x, $y + 1, $z); + if(StaticBlock::getIsTransparent($blockUp) && !StaticBlock::getIsLiquid($blockUp) && !($blockUp === 60) && $level->level->getBlockID($x, $y, $z) === DIRT){ $level->fastSetBlockUpdate($x, $y, $z, GRASS, 0); } diff --git a/src/material/block/solid/LeavesBlock.php b/src/material/block/solid/LeavesBlock.php index b0655a6d..4eaa38f8 100644 --- a/src/material/block/solid/LeavesBlock.php +++ b/src/material/block/solid/LeavesBlock.php @@ -25,8 +25,8 @@ class LeavesBlock extends TransparentBlock{ } $visited[$index] = true; - $block = $level->getBlockWithoutVector($x, $y, $z, false); - if($block instanceof WoodBlock){ //type doesn't matter + $block = $level->level->getBlockID($x, $y, $z); + if($block === WOOD){ //type doesn't matter return true; } diff --git a/src/utils/RailLogic.php b/src/utils/RailLogic.php index d64cfdf4..09caef61 100644 --- a/src/utils/RailLogic.php +++ b/src/utils/RailLogic.php @@ -184,9 +184,10 @@ class RailLogic $meta = $this->level->level->getBlockDamage($this->x, $this->x, $this->z) & 8 | $state; } - $b = $this->level->getBlockWithoutVector($this->x, $this->y, $this->z); - $b->setMetadata($meta); - $this->level->setBlock(new Vector3($this->x, $this->y, $this->z), $b, true, false, true); + //$b = $this->level->getBlockWithoutVector($this->x, $this->y, $this->z); + //$b->setMetadata($meta); + //$this->level->setBlock(new Vector3($this->x, $this->y, $this->z), $b, true, false, true); + $this->level->fastSetBlockUpdateMeta($this->x, $this->y, $this->z, $meta, true); } public function updateConnections($meta){ diff --git a/src/world/Level.php b/src/world/Level.php index 960eaa00..b4cb5b36 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -287,8 +287,21 @@ class Level{ } return $ret; } + public function fastSetBlockUpdateMeta($x, $y, $z, $meta, $updateBlock = false){ + $this->level->setBlockDamage($x, $y, $z, $meta); + $pk = new UpdateBlockPacket; + $pk->x = $x; + $pk->y = $y; + $pk->z = $z; + $pk->block = $this->level->getBlockID($x, $y, $z); + $pk->meta = $meta; + $this->server->api->player->broadcastPacket($this->players, $pk); + if($updateBlock){ + $this->server->api->block->blockUpdateAround(new Position($x, $y, $z, $this), BLOCK_UPDATE_NORMAL, 1); + } + } - public function fastSetBlockUpdate($x, $y, $z, $id, $meta){ + public function fastSetBlockUpdate($x, $y, $z, $id, $meta, $updateBlock = false){ $this->level->setBlock($x, $y, $z, $id, $meta); $pk = new UpdateBlockPacket; $pk->x = $x; @@ -297,6 +310,9 @@ class Level{ $pk->block = $id; $pk->meta = $meta; $this->server->api->player->broadcastPacket($this->players, $pk); + if($updateBlock){ + $this->server->api->block->blockUpdateAround(new Vector3($x, $y, $z), BLOCK_UPDATE_NORMAL, 1); + } } public function onTick(PocketMinecraftServer $server){ @@ -305,22 +321,14 @@ class Level{ for($cX = 0; $cX < 16; ++$cX){ for($cZ = 0; $cZ < 16; ++$cZ){ for($c = 0; $c <= 20; ++$c){ - //$this->randInt1 = ($this->randInt1 * 3 + $this->randInt2) & 0xffffffff; - $xyz = mt_rand(0, 0xffffffff) >> 2;//$this->randInt1 >> 2; + $xyz = mt_rand(0, 0xffffffff) >> 2;//TODO fix php5 $x = $xyz & 0xf; $z = ($xyz >> 8) & 0xf; //TODO might be possible to make some micro optmizations $y = ($xyz >> 16) & 0x7f; $id = $this->level->getBlockID(($cX << 4) + $x, $y, $z + ($cZ << 4)); if(isset(self::$randomUpdateBlocks[$id])){ $cl = Block::$class[$id]; - //if($cl !== false){ $cl::onRandomTick($this, ($cX << 4) + $x, $y, $z + ($cZ << 4)); - //} - //$p->setXYZ(($cX << 4) + $x, $y, $z + ($cZ << 4)); - //$b = BlockAPI::get($idmeta[0], $idmeta[1], $p); //$this->getBlockWithoutVector(); - //if($b instanceof Block){ - // $b->onUpdate(BLOCK_UPDATE_RANDOM); - //} } } } @@ -329,6 +337,7 @@ class Level{ foreach($this->entityList as $k => $e){ if(!($e instanceof Entity)){ unset($this->entityList[$k]); + unset($this->server->entities[$k]); continue; }