From 756c597e47837808c735be564704d92c6c143048 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 26 Jun 2025 08:17:14 +0300 Subject: [PATCH] rewritten pumpkin and melon growth, fixed new world generation --- src/entity/Entity.php | 3 +- src/material/block/plant/MelonStemBlock.php | 103 +++++++++++----- src/material/block/plant/PumpkinStemBlock.php | 112 ++++++++++++------ src/pmf/PMFLevel.php | 1 + 4 files changed, 149 insertions(+), 70 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 214b2307..f13e36b5 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -118,7 +118,7 @@ class Entity extends Position public $moveStrafing, $moveForward; - function __construct(Level $level, $eid, $class, $type = 0, $data = []) + public function __construct(Level $level, $eid, $class, $type = 0, $data = []) { $this->random = new XorShift128Random(); $this->last = [&$this->lastX, &$this->lastY, &$this->lastZ, &$this->lastYaw, &$this->lastPitch, &$this->lastTime]; //pointers to variables @@ -202,6 +202,7 @@ class Entity extends Position $this->outOfWorld(); } } + public function handlePrePlayerSearcher(){ } diff --git a/src/material/block/plant/MelonStemBlock.php b/src/material/block/plant/MelonStemBlock.php index c98d8677..8042d889 100644 --- a/src/material/block/plant/MelonStemBlock.php +++ b/src/material/block/plant/MelonStemBlock.php @@ -17,34 +17,79 @@ class MelonStemBlock extends FlowableBlock{ } return false; } + + public static function getGrowthSpeed(Level $level, $x, $y, $z){ + $zneg = $level->level->getBlockID($x, $y, $z-1); + $zpos = $level->level->getBlockID($x, $y, $z+1); + $xneg = $level->level->getBlockID($x-1, $y, $z); + $xpos = $level->level->getBlockID($x+1, $y, $z); + $znxn = $level->level->getBlockID($x-1, $y, $z-1); + $znxp = $level->level->getBlockID($x+1, $y, $z-1); + $zpxp = $level->level->getBlockID($x+1, $y, $z+1); + $zpxn = $level->level->getBlockID($x-1, $y, $z+1); + $xEqual = $xneg == MELON_STEM || $xpos == MELON_STEM; + $zEqual = $zneg == MELON_STEM || $zpos == MELON_STEM; + $xzEqual = $znxn == MELON_STEM || $znxp == MELON_STEM || $zpxn == MELON_STEM || $zpxp == MELON_STEM; + + $speed = 1; + for($xx = $x-1; $xx >= $x+1; ++$xx){ + for($zz = $z-1; $zz >= $z+1; ++$zz){ + [$id, $meta] = $level->level->getBlock($x, $y, $z); + $v18 = 0; + if($id == FARMLAND) $v18 = $meta > 0 ? 3 : 1; + if($xx != $x || $zz != $z) $v18 *= 0.25; + $speed += $v18; + } + } + if($xzEqual || ($xEqual && $zEqual)) return $speed*0.5; + return $speed; + } + public static function onRandomTick(Level $level, $x, $y, $z){ - if(mt_rand(0, 2) == 1){ - $block = $level->level->getBlock($x, $y, $z); - if($block[1] < 0x07){ - //++$this->meta; - //$this->level->setBlock($this, $this, true, false, true); - $level->fastSetBlockUpdate($x, $y, $z, $block[0], $block[1] + 1); - }else{ - - $position = new AirBlock(); //feke block - $position->x = $x; - $position->y = $y; - $position->z = $z; - $position->level = $level; - for($side = 2; $side <= 5; ++$side){ - $b = $position->getSide($side); - if($b->getID() === MELON_BLOCK){ - return; - } - } - $side = $position->getSide(mt_rand(2,5)); - $d = $side->getSide(0); - if($side->getID() === AIR and ($d->getID() === FARMLAND or $d->getID() === GRASS or $d->getID() === DIRT)){ - $level->setBlock($side, new MelonBlock(), true, false, true); - } + //TODO checkAlive + //if ( Level::getRawBrightness(a2, a3, a4 + 1, a5) > 8 ) TODO - skylight + + $growSpeed = static::getGrowthSpeed($level, $x, $y, $z); + $rand = mt_rand(0, (int)(25/$growSpeed)); + if($rand != 0) return; + + $meta = $level->level->getBlockDamage($x, $y, $z); + if($meta <= 6){ + $level->fastSetBlockUpdateMeta($x, $y, $z, $meta+1); + return; + } + + $xn = $level->level->getBlockID($x-1, $y, $z); + $xp = $level->level->getBlockID($x+1, $y, $z); + $zn = $level->level->getBlockID($x, $y, $z-1); + $zp = $level->level->getBlockID($x, $y, $z+1); + if($xn != MELON_BLOCK && $xp != MELON_BLOCK && $zn != MELON_BLOCK && $zp != MELON_BLOCK){ + $below = $level->level->getBlockID($x-1, $y-1, $z); + if($level->level->getBlockID($x-1, $y, $z) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x-1, $y, $z, MELON_BLOCK, 0); + return; + } + + $below = $level->level->getBlockID($x+1, $y-1, $z); + if($level->level->getBlockID($x+1, $y, $z) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x+1, $y, $z, MELON_BLOCK, 0); + return; + } + + $below = $level->level->getBlockID($x, $y-1, $z-1); + if($level->level->getBlockID($x, $y, $z-1) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x, $y, $z-1, MELON_BLOCK, 0); + return; + } + + $below = $level->level->getBlockID($x, $y-1, $z+1); + if($level->level->getBlockID($x, $y, $z+1) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x, $y, $z+1, MELON_BLOCK, 0); + return; } } } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ if($level->level->getBlockID($x, $y - 1, $z) != FARMLAND){ ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(MELON_SEEDS, 0, mt_rand(0, 2))); @@ -67,14 +112,8 @@ class MelonStemBlock extends FlowableBlock{ public function getDrops(Item $item, Player $player){ $drops = []; - if($this->meta >= 0x07){ - $drops[] = [MELON_SEEDS, 0, mt_rand(1, 2)]; - } - elseif($this->meta >= 0x01 and $this->meta <= 0x07){ - $drops[] = [MELON_SEEDS, 0, 1]; - } - else{ - $drops[] = [MELON_SEEDS, 0, 0]; + for($i = 0; $i < 3; ++$i){ + if(mt_rand(0, 15) <= $this->meta) $drops[] = [MELON_SEEDS, 0, 1]; } return $drops; } diff --git a/src/material/block/plant/PumpkinStemBlock.php b/src/material/block/plant/PumpkinStemBlock.php index c662af3a..874e1a8e 100644 --- a/src/material/block/plant/PumpkinStemBlock.php +++ b/src/material/block/plant/PumpkinStemBlock.php @@ -10,38 +10,82 @@ class PumpkinStemBlock extends FlowableBlock{ $this->material = Material::$plant; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $down = $this->getSide(0); - if($down->getID() === FARMLAND){ - $this->level->setBlock($block, $this, true, false, true); - return true; - } + $down = $this->getSide(0); + if($down->getID() === FARMLAND){ + $this->level->setBlock($block, $this, true, false, true); + return true; + } return false; } + + public static function getGrowthSpeed(Level $level, $x, $y, $z){ + $zneg = $level->level->getBlockID($x, $y, $z-1); + $zpos = $level->level->getBlockID($x, $y, $z+1); + $xneg = $level->level->getBlockID($x-1, $y, $z); + $xpos = $level->level->getBlockID($x+1, $y, $z); + $znxn = $level->level->getBlockID($x-1, $y, $z-1); + $znxp = $level->level->getBlockID($x+1, $y, $z-1); + $zpxp = $level->level->getBlockID($x+1, $y, $z+1); + $zpxn = $level->level->getBlockID($x-1, $y, $z+1); + $xEqual = $xneg == PUMPKIN_STEM || $xpos == PUMPKIN_STEM; + $zEqual = $zneg == PUMPKIN_STEM || $zpos == PUMPKIN_STEM; + $xzEqual = $znxn == PUMPKIN_STEM || $znxp == PUMPKIN_STEM || $zpxn == PUMPKIN_STEM || $zpxp == PUMPKIN_STEM; + + $speed = 1; + for($xx = $x-1; $xx >= $x+1; ++$xx){ + for($zz = $z-1; $zz >= $z+1; ++$zz){ + [$id, $meta] = $level->level->getBlock($x, $y, $z); + if($id == FARMLAND) $v18 = $meta > 0 ? 3 : 1; + else $v18 = 0; + if($xx != $x || $zz != $z) $v18 *= 0.25; + $speed += $v18; + } + } + if($xzEqual || ($xEqual && $zEqual)) return $speed*0.5; + return $speed; + } + public static function onRandomTick(Level $level, $x, $y, $z){ - if(mt_rand(0, 2) == 1){ - $block = $level->level->getBlock($x, $y, $z); - if($block[1] < 0x07){ - //++$this->meta; - //$this->level->setBlock($this, $this, true, false, true); - $level->fastSetBlockUpdate($x, $y, $z, $block[0], $block[1] + 1); - }else{ - - $position = new AirBlock(); //feke block - $position->x = $x; - $position->y = $y; - $position->z = $z; - $position->level = $level; - for($side = 2; $side <= 5; ++$side){ - $b = $position->getSide($side); - if($b->getID() === PUMPKIN){ - return; - } - } - $side = $position->getSide(mt_rand(2,5)); - $d = $side->getSide(0); - if($side->getID() === AIR and ($d->getID() === FARMLAND or $d->getID() === GRASS or $d->getID() === DIRT)){ - $level->setBlock($side, new PumpkinBlock(), true, false, true); - } + //TODO checkAlive + //if ( Level::getRawBrightness(a2, a3, a4 + 1, a5) > 8 ) TODO - skylight + + $growSpeed = static::getGrowthSpeed($level, $x, $y, $z); + $rand = mt_rand(0, (int)(25/$growSpeed)); + if($rand != 0) return; + + $meta = $level->level->getBlockDamage($x, $y, $z); + if($meta <= 6){ + $level->fastSetBlockUpdateMeta($x, $y, $z, $meta+1); + return; + } + + $xn = $level->level->getBlockID($x-1, $y, $z); + $xp = $level->level->getBlockID($x+1, $y, $z); + $zn = $level->level->getBlockID($x, $y, $z-1); + $zp = $level->level->getBlockID($x, $y, $z+1); + if($xn != PUMPKIN && $xp != PUMPKIN && $zn != PUMPKIN && $zp != PUMPKIN){ + $below = $level->level->getBlockID($x-1, $y-1, $z); + if($level->level->getBlockID($x-1, $y, $z) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x-1, $y, $z, PUMPKIN, 0); + return; + } + + $below = $level->level->getBlockID($x+1, $y-1, $z); + if($level->level->getBlockID($x+1, $y, $z) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x+1, $y, $z, PUMPKIN, 0); + return; + } + + $below = $level->level->getBlockID($x, $y-1, $z-1); + if($level->level->getBlockID($x, $y, $z-1) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x, $y, $z-1, PUMPKIN, 0); + return; + } + + $below = $level->level->getBlockID($x, $y-1, $z+1); + if($level->level->getBlockID($x, $y, $z+1) == 0 && ($below == FARMLAND || $below == DIRT || $below == GRASS)){ + $level->fastSetBlockUpdate($x, $y, $z+1, PUMPKIN, 0); + return; } } } @@ -67,14 +111,8 @@ class PumpkinStemBlock extends FlowableBlock{ public function getDrops(Item $item, Player $player){ $drops = []; - if($this->meta >= 0x07){ - $drops[] = [PUMPKIN_SEEDS, 0, mt_rand(1, 2)]; - } - elseif($this->meta >= 0x01 and $this->meta <= 0x07){ - $drops[] = [PUMPKIN_SEEDS, 0, 1]; - } - else{ - $drops[] = [PUMPKIN_SEEDS, 0, 0]; + for($i = 0; $i < 3; ++$i){ + if(mt_rand(0, 15) <= $this->meta) $drops[] = [PUMPKIN_SEEDS, 0, 1]; } return $drops; } diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 2e56276c..af575251 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -45,6 +45,7 @@ class PMFLevel extends PMF{ @mkdir($dirname , 0755); } + $this->hasLight = str_repeat("\x00", 16*16); for($index = 0; $index < $cnt; ++$index){ $this->chunks[$index] = false; $this->chunkChange[$index] = false;