From 5799426f1b0b2c38baeba7015f2be4dbdda2e59a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 30 Aug 2023 08:20:43 +0300 Subject: [PATCH 01/43] b --- src/API/PluginAPI.php | 3 ++- src/world/Level.php | 2 +- src/world/generator/ExperimentalGenerator.php | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/API/PluginAPI.php b/src/API/PluginAPI.php index f8b87aa8..808a3a26 100644 --- a/src/API/PluginAPI.php +++ b/src/API/PluginAPI.php @@ -148,7 +148,8 @@ class PluginAPI extends stdClass{ $aver = CURRENT_API_VERSION; if(!in_array((string) CURRENT_API_VERSION, $pluginInfo["api"])){ - console("[WARNING] API is not the same as Core, might cause bugs({$pluginInfo["api"]} != {$aver})"); + $s = implode(", ", $pluginInfo["api"]); + console("[WARNING] API is not the same as Core, might cause bugs({$s} != {$aver})"); } $phr = "phar://$filePath/"; diff --git a/src/world/Level.php b/src/world/Level.php index 93f25172..add23eed 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -477,7 +477,7 @@ class Level{ return false; } $this->changedCount[$X . ":" . $Y . ":" . $Z] = 4096; - if(ADVANCED_CACHE == true){ + if(ADVANCED_CACHE){ Cache::remove("world:{$this->name}:$X:$Z"); } return $this->level->setMiniChunk($X, $Z, $Y, $data); diff --git a/src/world/generator/ExperimentalGenerator.php b/src/world/generator/ExperimentalGenerator.php index b8f5bf09..a324e093 100644 --- a/src/world/generator/ExperimentalGenerator.php +++ b/src/world/generator/ExperimentalGenerator.php @@ -103,11 +103,9 @@ class ExperimentalGenerator implements NewLevelGenerator{ public function generateChunk($chunkX, $chunkZ){ $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->level->getSeed()); - $noiseArray = ExperimentalGenerator::getFastNoise3D($this->noiseBase, 16, 128, 16, 4, 8, 4, $chunkX * 16, 0, $chunkZ * 16); - $biomeCache = []; - + $t = microtime(1); for($chunkY = 0; $chunkY < 8; ++$chunkY){ $chunk = ""; $startY = $chunkY << 4; @@ -164,6 +162,8 @@ class ExperimentalGenerator implements NewLevelGenerator{ } $this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk); } + $t = microtime(1) - $t; + console("gen:".$t); foreach($this->genPopulators as $pop){ From b227d937870050815ce0be6281fa08eafc5ebc33 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 30 Aug 2023 08:24:23 +0300 Subject: [PATCH 02/43] e --- src/API/PluginAPI.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/API/PluginAPI.php b/src/API/PluginAPI.php index 808a3a26..04d65d53 100644 --- a/src/API/PluginAPI.php +++ b/src/API/PluginAPI.php @@ -148,7 +148,8 @@ class PluginAPI extends stdClass{ $aver = CURRENT_API_VERSION; if(!in_array((string) CURRENT_API_VERSION, $pluginInfo["api"])){ - $s = implode(", ", $pluginInfo["api"]); + if(is_array($pluginInfo)) $s = implode(",",$pluginInfo["api"]); + else $s = $pluginInfo["api"]; console("[WARNING] API is not the same as Core, might cause bugs({$s} != {$aver})"); } From 04bd307bb09221a1e771c39c60c6d4cc5b18a867 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 30 Aug 2023 11:57:09 +0300 Subject: [PATCH 03/43] i commited another crime --- src/entity/Entity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 89c101b0..24bf7ecb 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -736,7 +736,7 @@ class Entity extends Position if($this->lastHeadYaw != $this->headYaw){ $this->sendHeadYaw(); } - if($this->class !== ENTITY_PLAYER && $update){ + if($this->class === ENTITY_PLAYER || $update){ $this->updateMovement(); } From 81464e3e26883eaa35d07df991ae91c144d44a98 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 30 Aug 2023 12:50:32 +0300 Subject: [PATCH 04/43] fixed signs --- src/entity/Entity.php | 1 + src/recipes/CraftingRecipes.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 24bf7ecb..c8e0b93b 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -766,6 +766,7 @@ class Entity extends Position $this->updateLast(); $players = $this->server->api->player->getAll($this->level); if($this->player instanceof Player){ + console("gujnbguib"); unset($players[$this->player->CID]); $pk = new MovePlayerPacket(); $pk->eid = $this->eid; diff --git a/src/recipes/CraftingRecipes.php b/src/recipes/CraftingRecipes.php index 02c6ed81..666719f9 100644 --- a/src/recipes/CraftingRecipes.php +++ b/src/recipes/CraftingRecipes.php @@ -182,7 +182,7 @@ class CraftingRecipes{ "DYE:4x9=>LAPIS_BLOCK:0x1", "WOOL:?x1,STICK:?x8=>PAINTING:0x1", "SUGARCANE:?x3=>PAPER:0x3", - "WOODEN_PLANKS:?x6,STICK:?x1=>SIGN:0x1", + "WOODEN_PLANKS:?x6,STICK:?x1=>SIGN:0x3", "IRON_INGOT:?x6=>IRON_BARS:0x16", "COAL:0x9=>COAL_BLOCK:0x1", "COAL_BLOCK:?x1=>COAL:0x9", From 083ace7eb1ee86fe55f1d489c770b85281938d43 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 30 Aug 2023 12:54:37 +0300 Subject: [PATCH 05/43] stone with different meta will drop itself --- src/material/block/solid/StoneBlock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/material/block/solid/StoneBlock.php b/src/material/block/solid/StoneBlock.php index 71c9d46d..cf0f6e4c 100644 --- a/src/material/block/solid/StoneBlock.php +++ b/src/material/block/solid/StoneBlock.php @@ -40,7 +40,7 @@ class StoneBlock extends SolidBlock{ public function getDrops(Item $item, Player $player){ if($item->getPickaxeLevel() >= 1){ return array( - array(COBBLESTONE, 0, 1), + array($this->meta == 0 ? COBBLESTONE : $this->id, $this->meta, 1), ); }else{ return array(); From 385c8a1bc073fa4c2a4152b23641932378d69642 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 31 Aug 2023 04:09:08 +0300 Subject: [PATCH 06/43] fixes from npe 0.9 test --- src/Player.php | 13 ++++++++++--- src/entity/Entity.php | 4 ++-- src/network/protocol/packet/MovePlayerPacket.php | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Player.php b/src/Player.php index d9c14a5d..bbb39358 100755 --- a/src/Player.php +++ b/src/Player.php @@ -470,7 +470,7 @@ class Player{ } } } - $this->stopUsingChunk($X, $Z); //just in case + //$this->stopUsingChunk($X, $Z); //just in case $pk = new FullChunkDataPacket; $pk->chunkX = $X; $pk->chunkZ = $Z; @@ -480,13 +480,16 @@ class Player{ return false; } } - + public $chunkTicker = 0; public function entityTick(){ //ConsoleAPI::debug("{$this->username}, cl: ".count($this->chunksLoaded).", oc: ".count($this->chunksOrder)); if(count($this->chunksOrder) <= 0 && $this->level->generatorType != 0){ $this->orderChunks(); } - $this->getNextChunk($this->level); + if($this->chunkTicker++ > 5){ + $this->getNextChunk($this->level); + $this->chunkTicker = 0; + } } public function getNextChunk($world){ @@ -1533,6 +1536,7 @@ class Player{ $this->spawned = true; $this->server->handle("player.spawn", $this); $this->server->api->chat->broadcast($this->username." joined the game"); + //console("Current position: {$this->entity}"); $this->server->api->player->spawnAllPlayers($this); $this->server->api->player->spawnToAllPlayers($this); } @@ -1543,15 +1547,18 @@ class Player{ if($this->forceMovement->distance($newPos) <= 0.7){ $this->forceMovement = false; }else{ + //console("force"); $this->teleport($this->forceMovement, $this->entity->yaw, $this->entity->pitch, false); } } $speed = $this->entity->getSpeedMeasure(); if($this->blocked === true or ($this->server->api->getProperty("allow-flight") !== true and (($speed > 9 and ($this->gamemode & 0x01) === 0x00) or $speed > 20 or $this->entity->distance($newPos) > 7)) or $this->server->api->handle("player.move", $this->entity) === false){ if($this->lastCorrect instanceof Vector3){ + //console("last"); $this->teleport($this->lastCorrect, $this->entity->yaw, $this->entity->pitch, false); } }else{ + //console("New Pos: $newPos"); $this->entity->setPosition($newPos, $packet->yaw, $packet->pitch); } $this->entity->updateAABB(); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index c8e0b93b..6d930601 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -766,12 +766,12 @@ class Entity extends Position $this->updateLast(); $players = $this->server->api->player->getAll($this->level); if($this->player instanceof Player){ - console("gujnbguib"); + //console("gujnbguib"); unset($players[$this->player->CID]); $pk = new MovePlayerPacket(); $pk->eid = $this->eid; $pk->x = $this->x; - $pk->y = $this->y + $this->getEyeHeight(); + $pk->y = $this->y; $pk->z = $this->z; $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; diff --git a/src/network/protocol/packet/MovePlayerPacket.php b/src/network/protocol/packet/MovePlayerPacket.php index 6b3d361d..45c8229c 100644 --- a/src/network/protocol/packet/MovePlayerPacket.php +++ b/src/network/protocol/packet/MovePlayerPacket.php @@ -19,8 +19,8 @@ class MovePlayerPacket extends RakNetDataPacket{ $this->y = $this->getFloat(); $this->z = $this->getFloat(); $this->yaw = $this->getFloat(); - $this->bodyYaw = $this->getFloat(); $this->pitch = $this->getFloat(); + $this->bodyYaw = $this->getFloat(); $this->teleport = ($this->getByte() & 0x80) > 0; } @@ -36,4 +36,4 @@ class MovePlayerPacket extends RakNetDataPacket{ $this->putByte($this->teleport ? 0x80 : 0x00); } -} \ No newline at end of file +} From d70d2073179641161b1671ec0ce377dc4ff24d89 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 21 Nov 2023 12:58:27 +0300 Subject: [PATCH 07/43] painting fix, sleeping fix --- src/Player.php | 31 +++++++++++-------------------- src/entity/Entity.php | 27 ++++++++++++++------------- src/world/Level.php | 18 ++++++++++++++++-- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/Player.php b/src/Player.php index bbb39358..80a07411 100755 --- a/src/Player.php +++ b/src/Player.php @@ -49,11 +49,11 @@ class Player{ private $ip; private $port; private $counter = [0, 0, 0, 0]; - private $username; - private $iusername; + public $username; + public $iusername; private $eid = false; private $startAction = false; - private $isSleeping = false; + public $isSleeping = false; private $chunksOrder = []; private $lastMeasure = 0; private $bandwidthRaw = 0; @@ -68,6 +68,9 @@ class Player{ private $chunkCount = []; private $received = []; public $cratingItems; + + public $sleepingTime = 0; + /** * @param integer $clientID * @param string $ip @@ -130,7 +133,7 @@ class Player{ $this->entity->updateMetadata(); } $this->setSpawn($pos); - $this->server->schedule(60, [$this, "checkSleep"]); + return true; } @@ -486,6 +489,8 @@ class Player{ if(count($this->chunksOrder) <= 0 && $this->level->generatorType != 0){ $this->orderChunks(); } + + if($this->isSleeping) ++$this->sleepingTime; if($this->chunkTicker++ > 5){ $this->getNextChunk($this->level); $this->chunkTicker = 0; @@ -607,22 +612,6 @@ class Player{ $this->dataPacket($pk); } - public function checkSleep(){ - if($this->isSleeping !== false){ - if($this->server->api->time->getPhase($this->level) === "night"){ - foreach($this->server->api->player->getAll($this->level) as $p){ - if($p->isSleeping === false){ - return false; - } - } - $this->server->api->time->set("day", $this->level); - foreach($this->server->api->player->getAll($this->level) as $p){ - $p->stopSleep(); - } - } - } - } - /** * @param $type * @param $damage @@ -1540,6 +1529,7 @@ class Player{ $this->server->api->player->spawnAllPlayers($this); $this->server->api->player->spawnToAllPlayers($this); } + if($this->isSleeping) break; if(($this->entity instanceof Entity) and $packet->messageIndex > $this->lastMovement){ $this->lastMovement = $packet->messageIndex; $newPos = new Vector3($packet->x, $packet->y, $packet->z); @@ -2224,6 +2214,7 @@ class Player{ public function stopSleep(){ $this->isSleeping = false; + $this->sleepingTime = 0; if($this->entity instanceof Entity){ $this->entity->updateMetadata(); } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 6d930601..24a61f1e 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -418,20 +418,21 @@ class Entity extends Position $endX = ceil($this->boundingBox->maxX); $endY = ceil($this->boundingBox->maxY); $endZ = ceil($this->boundingBox->maxZ); - - for($i = 0; $i < 8; ++$i){ - $x = ((($i >> 0) % 2) - 0.5) * $this->width * 0.8; - $y= ((($i >> 1) % 2) - 0.5) * 0.1; - $z = ((($i >> 2) % 2) - 0.5) * $this->width * 0.8; + if(!($this instanceof Painting) && !($this->isPlayer() && $this->player->isSleeping !== false)){ + for($i = 0; $i < 8; ++$i){ + $x = ((($i >> 0) % 2) - 0.5) * $this->width * 0.8; + $y= ((($i >> 1) % 2) - 0.5) * 0.1; + $z = ((($i >> 2) % 2) - 0.5) * $this->width * 0.8; + + $blockX = floor($this->x + $x); + $blockY = floor($this->y + $this->getEyeHeight() + $y); + $blockZ = floor($this->z + $z); - $blockX = floor($this->x + $x); - $blockY = floor($this->y + $this->getEyeHeight() + $y); - $blockZ = floor($this->z + $z); - - if($this->level->getBlockWithoutVector($blockX, $blockY, $blockZ)->isSolid){ - $this->harm(1, "suffocation"); // Suffocation - $hasUpdate = true; - break; + if($this->level->getBlockWithoutVector($blockX, $blockY, $blockZ)->isSolid){ + $this->harm(1, "suffocation"); // Suffocation + $hasUpdate = true; + break; + } } } diff --git a/src/world/Level.php b/src/world/Level.php index add23eed..28ae0033 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -107,7 +107,21 @@ class Level{ unset($this->mobSpawner->level); } - + public function checkSleep(){ //TODO events? + if(count($this->players) == 0) return false; + if($this->server->api->time->getPhase($this->level) === "night"){ //TODO vanilla + foreach($this->players as $p){ + if($p->isSleeping == false || $p->sleepingTime < 100){ + return false; + } + } + $this->server->api->time->set("day", $this->level); + } + foreach($this->players as $p){ + $p->stopSleep(); + } + } + public function getOrderedFullChunk($X, $Z){ $X = (int)$X; $Z = (int)$Z; @@ -293,7 +307,7 @@ class Level{ } $now = microtime(true); $this->players = $this->server->api->player->getAll($this); - + $this->checkSleep(); if(count($this->changedCount) > 0){ arsort($this->changedCount); $resendChunks = []; From e1368ce4a1ad1ba7b30511af3bbb3ab12162aed5 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 21 Nov 2023 17:31:45 +0300 Subject: [PATCH 08/43] fixed trees on chunk borders, bumped pmf ver to 2 --- src/pmf/PMFLevel.php | 47 ++++++++++++++----- src/world/Level.php | 11 ++++- src/world/generator/ExperimentalGenerator.php | 1 + src/world/generator/HellGenerator.php | 3 +- src/world/generator/NormalGenerator.php | 1 + 5 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 7d95c1eb..2c9939e8 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -1,10 +1,13 @@ create($file, 0); @@ -36,7 +39,11 @@ class PMFLevel extends PMF{ } } } - + + public function setPopulated($X, $Z, $bool = true){ + $this->populated[self::getIndex($X, $Z)] = $bool; + } + private function createBlank(){ $this->saveData(false); $this->locationTable = []; @@ -119,7 +126,9 @@ class PMFLevel extends PMF{ } $this->seek(5); $this->levelData["version"] = ord($this->read(1)); - if($this->levelData["version"] > PMF_CURRENT_LEVEL_VERSION){ //TODO old worlds support + if($this->levelData["version"] != PMF_CURRENT_LEVEL_VERSION){ //TODO old worlds support + $cv = PMF_CURRENT_LEVEL_VERSION; + ConsoleAPI::warn("The level version does not match current. ({$this->levelData["version"]} != {$cv})"); return false; } $this->levelData["name"] = $this->read(Utils::readShort($this->read(2), false)); @@ -275,7 +284,8 @@ class PMFLevel extends PMF{ for($Y = 0; $Y < 8; ++$Y){ $bitmap |= ($this->chunks[$index][$Y] !== false and ((isset($this->chunkChange[$index][$Y]) and $this->chunkChange[$index][$Y] === 0) or !$this->isMiniChunkEmpty($X, $Z, $Y))) << $Y; } - gzwrite($chunk, Utils::writeShort($bitmap), 2); //2 bytes locmap + gzwrite($chunk, Utils::writeShort($bitmap), 2); //2 bytes locmap(actually it should be only 1) + gzwrite($chunk, chr($this->populated[$index]), 1); //isPopulated $biomedata = $this->chunkInfo[$index][0]; if(strlen($biomedata) < 256){ $biomedata = str_repeat("\x01", 256); @@ -335,7 +345,7 @@ class PMFLevel extends PMF{ } } - public function loadChunk($X, $Z){ + public function loadChunk($X, $Z, $populate = false){ $index = $this->getIndex($X, $Z); @@ -359,6 +369,8 @@ class PMFLevel extends PMF{ if(strlen($chunk) === 0) return false; $info = [0 => Utils::readShort(substr($chunk, $offset, 2))]; $offset+=2; + $populated = ord($chunk[$offset]) > 0; + ++$offset; $this->chunks[$index] = []; $this->chunkChange[$index] = [-1 => false]; $this->chunkInfo[$index][0] = substr($chunk, $offset, 256); //Biome data @@ -376,6 +388,10 @@ class PMFLevel extends PMF{ $this->chunks[$index][$Y] = false; } } + $this->setPopulated($X, $Z, $populated); + if($populate && !$populated){ + $this->level->generator->populateChunk($X, $Z); + } return true; } @@ -420,6 +436,7 @@ class PMFLevel extends PMF{ 0 => str_repeat("\x00", 256) ); $this->locationTable[$index] = array(0); + $this->setPopulated($X, $Z, false); } } public function setMiniChunk($X, $Z, $Y, $data){ @@ -588,7 +605,13 @@ class PMFLevel extends PMF{ } return [$b, $m]; } - + + public function createUnpopulatedChunk($X, $Z){ + $this->initCleanChunk($X, $Z); + $this->level->generator->generateChunk($X, $Z); + $this->fakeLoaded[self::getIndex($X, $Z)] = "$X.$Z"; //TODO do not use string + } + public function setBlock($x, $y, $z, $block, $meta = 0){ $X = $x >> 4; $Z = $z >> 4; @@ -598,12 +621,14 @@ class PMFLevel extends PMF{ if($Y >= 128 or $y < 0){ return false; } + $loaded = false; $index = $this->getIndex($X, $Z); if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){ - if($this->loadChunk($X, $Z) === false){ - return false; + if($this->loadChunk($X, $Z, false) === false){ + $this->createUnpopulatedChunk($X, $Z); } - }else + $loaded = true; + } if($this->chunks[$index][$Y] === false){ $this->fillMiniChunk($X, $Z, $Y); } diff --git a/src/world/Level.php b/src/world/Level.php index 28ae0033..97b9fce0 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -21,7 +21,7 @@ class Level{ public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner; public $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime; - private $generator; + public $generator; public function __construct(PMFLevel $level, Config $entities, Config $tiles, Config $blockUpdates, $name){ $this->server = ServerAPI::request(); $this->level = $level; @@ -140,7 +140,7 @@ class Level{ $tileEntities = ""; if($gen) $this->level->generateChunk($X, $Z, $this->generator); if(!$this->level->isChunkLoaded($X, $Z)){ - $this->level->loadChunk($X, $Z); + $this->level->loadChunk($X, $Z, true); } $miniChunks = []; @@ -433,6 +433,13 @@ class Level{ } } + foreach($this->level->fakeLoaded as $ind => $val){ + $xz = explode(".", $val); + $this->level->unloadChunk($xz[0], $xz[1]); + ConsoleAPI::info("unloading feke chunk: {$xz[0]} {$xz[1]}"); + unset($this->level->fakeLoaded[$ind]); + } + } public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){ diff --git a/src/world/generator/ExperimentalGenerator.php b/src/world/generator/ExperimentalGenerator.php index a324e093..b3951799 100644 --- a/src/world/generator/ExperimentalGenerator.php +++ b/src/world/generator/ExperimentalGenerator.php @@ -172,6 +172,7 @@ class ExperimentalGenerator implements NewLevelGenerator{ } public function populateChunk($chunkX, $chunkZ){ + $this->level->level->setPopulated($chunkX, $chunkZ, true); $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->level->getSeed()); foreach($this->populators as $populator){ $populator->populate($this->level, $chunkX, $chunkZ, $this->random); diff --git a/src/world/generator/HellGenerator.php b/src/world/generator/HellGenerator.php index b149f7b9..3d24ce6d 100644 --- a/src/world/generator/HellGenerator.php +++ b/src/world/generator/HellGenerator.php @@ -1,6 +1,6 @@ level->level->setPopulated($chunkX, $chunkZ, true); $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); foreach($this->populators as $populator){ $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index 47db5f54..0186b809 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -138,6 +138,7 @@ class NormalGenerator implements NewLevelGenerator{ } public function populateChunk($chunkX, $chunkZ){ + $this->level->level->setPopulated($chunkX, $chunkZ, true); $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); foreach($this->populators as $populator){ $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); From ac1a3b67fbc87fdf0fb851f9815dfb87a352227d Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 22 Nov 2023 18:05:17 +0300 Subject: [PATCH 09/43] entity.death event from 0.8 --- src/entity/Entity.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 24a61f1e..4dd838a2 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1387,6 +1387,7 @@ class Entity extends Position } public function makeDead($cause){ + if($this->server->api->dhandle("entity.death", ["entity" => $this, "cause" => $cause]) === false) return false; $this->spawnDrops(); $this->air = 200; $this->fire = 0; From d1cfe7b5f582cab4696f053a6ccfa4c5c828808a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 22 Nov 2023 18:53:05 +0300 Subject: [PATCH 10/43] pmf1->pmf2 converter --- src/API/ConsoleAPI.php | 3 ++ src/functions.php | 10 +++- src/pmf/PMFLevel.php | 111 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 120 insertions(+), 4 deletions(-) diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index e05876c2..ec2cbf36 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -275,6 +275,9 @@ class ConsoleAPI{ public static function debug($msg){ console("[DEBUG] ".$msg, true, true, 2); } + public static function notice($msg){ + console("[NOTICE] ".$msg); + } public static function info($msg){ console("[INFO] ".$msg); } diff --git a/src/functions.php b/src/functions.php index e6ac065e..c9ffb592 100644 --- a/src/functions.php +++ b/src/functions.php @@ -10,7 +10,15 @@ if(!function_exists("cli_set_process_title")){ } } } - +function copydir($src, $dest){ + if(!is_dir($dest)) mkdir($dest); + foreach(scandir($src) as $f){ + if($f != "." && $f != ".."){ + if(is_dir("$src/$f")) copydir("$src/$f", "$dest/$f"); + else copy("$src/$f", "$dest/$f"); + } + } +} function dummy(){ } diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 2c9939e8..e8b5a686 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -28,7 +28,7 @@ class PMFLevel extends PMF{ }else{ if($this->load($file) !== false){ $this->parseInfo(); - if($this->parseLevel() === false){ + if($this->parseLevel($file) === false){ $this->isLoaded = false; }else{ $this->isLoaded = true; @@ -119,8 +119,55 @@ class PMFLevel extends PMF{ private function getChunkPath($X, $Z){ return dirname($this->file) . "/chunks/" . $Z . "." . $X . ".pmc"; } - - protected function parseLevel(){ + + public function loadNCPMF1Chunk($X, $Z){ + $index = $this->getIndex($X, $Z); + + if($this->isChunkLoaded($X, $Z)){ + return true; + + }//elseif(!isset($this->locationTable[$index])){ + // return false; + //} + + //$info = $this->locationTable[$index]; + //$this->seek($info[0]); + $cp = $this->getChunkPath($X, $Z); + if(!is_file($cp)) return false; + $chunk = file_get_contents($cp); + if($chunk === false){ + return false; + } + $chunk = zlib_decode($chunk); + $offset = 0; + if(strlen($chunk) === 0) return false; + $info = [0 => Utils::readShort(substr($chunk, $offset, 2))]; + $offset+=2; + $this->chunks[$index] = []; + $this->chunkChange[$index] = [-1 => false]; + $this->chunkInfo[$index][0] = substr($chunk, $offset, 256); //Biome data + $offset += 256; + for($Y = 0; $Y < $this->levelData["height"]; ++$Y){ + $t = 1 << $Y; + if(($info[0] & $t) === $t){ + // 4096 + 4096 + 4096 + 4096, Id, Meta, BlockLight, Skylight + if(strlen($this->chunks[$index][$Y] = substr($chunk, $offset, 16384)) < 16384){ + console("[NOTICE] Empty corrupt chunk detected [$X,$Z,:$Y], recovering contents", true, true, 2); + $this->fillMiniChunk($X, $Z, $Y); + } + $offset += 16384; + }else{ + $this->chunks[$index][$Y] = false; + } + } + + $this->setPopulated($X, $Z, true); + + $this->chunkChange[$index][-1] = true; //force save + return true; + } + + protected function parseLevel($worldFile){ if($this->getType() !== 0x00){ return false; } @@ -129,8 +176,66 @@ class PMFLevel extends PMF{ if($this->levelData["version"] != PMF_CURRENT_LEVEL_VERSION){ //TODO old worlds support $cv = PMF_CURRENT_LEVEL_VERSION; ConsoleAPI::warn("The level version does not match current. ({$this->levelData["version"]} != {$cv})"); + + switch($this->levelData["version"]){ + case 1: + ConsoleAPI::notice("Converting the world from NCPMF-{$this->levelData["version"]} to NCPMF-$cv..."); + $worldDir = substr($worldFile, 0, -strlen("/level.pmf")); + $backupDir = "auto-world-backup-".microtime(true); + ConsoleAPI::info("Creating backup in $backupDir..."); + copydir($worldDir, $backupDir); + ConsoleAPI::info("Started converting..."); + $this->levelData["name"] = $this->read(Utils::readShort($this->read(2), false)); + $this->levelData["seed"] = Utils::readInt($this->read(4)); + $this->levelData["time"] = Utils::readInt($this->read(4)); + $this->levelData["spawnX"] = Utils::readFloat($this->read(4)); + $this->levelData["spawnY"] = Utils::readFloat($this->read(4)); + $this->levelData["spawnZ"] = Utils::readFloat($this->read(4)); + $this->levelData["width"] = ord($this->read(1)); + $this->levelData["height"] = ord($this->read(1)); + $this->levelData["generator"] = $this->read(Utils::readShort($this->read(2), false)); + $lastseek = ftell($this->fp); + if(($len = $this->read(2)) === false or ($this->levelData["extra"] = @gzinflate($this->read(Utils::readShort($len, false)))) === false){ //Corruption protection + console("[NOTICE] Empty/corrupt location table detected, forcing recovery"); + fseek($this->fp, $lastseek); + $c = gzdeflate(""); + $this->write(Utils::writeShort(strlen($c)) . $c); + $this->payloadOffset = ftell($this->fp); + $this->levelData["extra"] = ""; + //$cnt = pow($this->levelData["width"], 2); + for($X = 0; $X < 16; ++$X){ + for($Z = 0; $Z < 16; ++$Z){ + $this->write("\x00\xFF"); //Force index recreation + } + } + fseek($this->fp, $this->payloadOffset); + }else{ + $this->payloadOffset = ftell($this->fp); + } + + + foreach(scandir("$worldDir/chunks/") as $f){ + if($f != "." && $f != ".."){ + $xz = explode(".", $f); + $X = (int) $xz[0]; + $Z = (int) $xz[1]; + ConsoleAPI::info("Converting $X-$Z..."); + $this->loadNCPMF1Chunk($X, $Z); + $this->unloadChunk($X, $Z); + } + } + + ConsoleAPI::notice("Modifying level.pmf..."); + $this->saveData(false); + ConsoleAPI::notice("World converted. Reloading..."); + break; + } + return false; } + + + $this->levelData["name"] = $this->read(Utils::readShort($this->read(2), false)); $this->levelData["seed"] = Utils::readInt($this->read(4)); $this->levelData["time"] = Utils::readInt($this->read(4)); From 1bba9b83d10c599b8b536ba51f4b80a54b007826 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 22 Nov 2023 18:55:59 +0300 Subject: [PATCH 11/43] removed 1 debug message --- src/world/Level.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/world/Level.php b/src/world/Level.php index 97b9fce0..62324804 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -436,7 +436,6 @@ class Level{ foreach($this->level->fakeLoaded as $ind => $val){ $xz = explode(".", $val); $this->level->unloadChunk($xz[0], $xz[1]); - ConsoleAPI::info("unloading feke chunk: {$xz[0]} {$xz[1]}"); unset($this->level->fakeLoaded[$ind]); } From 3ee0bd14c1ec998c841e03ef70d9a761c32cd9f7 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 28 Nov 2023 02:58:32 +0300 Subject: [PATCH 12/43] emerald gen changes + nc 0.8 fixes --- src/API/EntityAPI.php | 4 +- src/entity/Animal.php | 4 +- src/entity/Entity.php | 4 +- src/entity/ai/tasks/TaskLookAround.php | 2 +- src/entity/ai/tasks/TaskLookAtPlayer.php | 2 +- src/entity/ai/tasks/TaskSwimming.php | 2 +- .../block/misc/NetherReactorBlock.php | 16 +-- src/structure/village/SmallFarmStructure.php | 2 +- src/utils/Utils.php | 106 ++---------------- src/world/generator/ExperimentalGenerator.php | 2 +- src/world/generator/NormalGenerator.php | 4 +- 11 files changed, 29 insertions(+), 119 deletions(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index efc7965c..4c9e5dcd 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -262,9 +262,9 @@ class EntityAPI{ "y" => $pos->y + 0.19, "z" => $pos->z + mt_rand(-10, 10) / 50, "level" => $pos->level, - "speedX" => Utils::randomFloat() * 0.2 - 0.1, + "speedX" => lcg_value() * 0.2 - 0.1, "speedY" => 0.2, - "speedZ" => Utils::randomFloat() * 0.2 - 0.1, + "speedZ" => lcg_value() * 0.2 - 0.1, "item" => $item, ]; if($this->server->api->handle("item.drop", $data) !== false){ diff --git a/src/entity/Animal.php b/src/entity/Animal.php index c1d6c4a0..3e461de2 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -60,9 +60,9 @@ abstract class Animal extends Creature implements Ageable, Breedable{ public function spawnChild() { return $this->server->api->entity->add($this->level, $this->class, $this->type, [ - "x" => $this->x + Utils::randomFloat() * mt_rand(-1, 1), + "x" => $this->x + lcg_value() * mt_rand(-1, 1), "y" => $this->y, - "z" => $this->z + Utils::randomFloat() * mt_rand(-1, 1), + "z" => $this->z + lcg_value() * mt_rand(-1, 1), "IsBaby" => true, "Age" => -24000, ]); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 4dd838a2..d3ed05fe 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1239,8 +1239,8 @@ class Entity extends Position if ($ret != false && $this->hasKnockback && is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false) { $d = $entity->x - $this->x; - for ($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (Utils::randomFloat() - Utils::randomFloat()) * 0.01) { - $d = (Utils::randomFloat() - Utils::randomFloat()) * 0.01; + for ($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (lcg_value() - lcg_value()) * 0.01) { + $d = (lcg_value() - lcg_value()) * 0.01; } // attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) > $this->knockBack($d, $d1); diff --git a/src/entity/ai/tasks/TaskLookAround.php b/src/entity/ai/tasks/TaskLookAround.php index 2a010ddc..af13cff6 100644 --- a/src/entity/ai/tasks/TaskLookAround.php +++ b/src/entity/ai/tasks/TaskLookAround.php @@ -16,7 +16,7 @@ class TaskLookAround extends TaskBase public function canBeExecuted(EntityAI $ai) { - return !$ai->entity->inPanic && !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/ + return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/ } public function onUpdate(EntityAI $ai) diff --git a/src/entity/ai/tasks/TaskLookAtPlayer.php b/src/entity/ai/tasks/TaskLookAtPlayer.php index 17fb49b0..cf9c624c 100644 --- a/src/entity/ai/tasks/TaskLookAtPlayer.php +++ b/src/entity/ai/tasks/TaskLookAtPlayer.php @@ -4,7 +4,7 @@ class TaskLookAtPlayer extends TaskBase{ public $target = false; private $state, $yaw, $pitch; public function canBeExecuted(EntityAI $ai){ - return !$ai->entity->inPanic && Utils::randomFloat() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); + return !$ai->entity->inPanic && lcg_value() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); } protected function findTarget($e, $r){ diff --git a/src/entity/ai/tasks/TaskSwimming.php b/src/entity/ai/tasks/TaskSwimming.php index 2bf1b910..5cc72be9 100644 --- a/src/entity/ai/tasks/TaskSwimming.php +++ b/src/entity/ai/tasks/TaskSwimming.php @@ -18,7 +18,7 @@ class TaskSwimming extends TaskBase $this->reset(); } - if(Utils::randomFloat() < 0.8){ #1.5.2 method + if(lcg_value() < 0.8){ #1.5.2 method $ai->entity->speedY = 0.42; } diff --git a/src/material/block/misc/NetherReactorBlock.php b/src/material/block/misc/NetherReactorBlock.php index 587b3c93..7c20e94c 100644 --- a/src/material/block/misc/NetherReactorBlock.php +++ b/src/material/block/misc/NetherReactorBlock.php @@ -61,7 +61,7 @@ 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){ + if ($this->level->getBlock(new Vector3($x+$a, $y+$b, $z+$c))->getID() === 87 && lcg_value() > 0.75){ $this->level->setBlock(new Vector3($x+$a, $y+$b, $z+$c), new AirBlock()); } } @@ -91,23 +91,23 @@ class NetherReactorBlock extends SolidBlock{ $forceAmount = $data[3]; $server = ServerAPI::request(); if(!$forceAmount){ - $spawnNumber = $minAmount + floor(Utils::randomFloat()*($maxAmount-$minAmount+1)); + $spawnNumber = $minAmount + floor(lcg_value()*($maxAmount-$minAmount+1)); } else{ $spawnNumber = $maxAmount; } for($i = 0; $i < $spawnNumber; $i++) { - $randomRange = floor(Utils::randomFloat()*5+3); - $shiftX = cos(floor(Utils::randomFloat()*360)*(pi()/180)); - $shiftZ = sin(floor(Utils::randomFloat()*360)*(pi()/180)); + $randomRange = floor(lcg_value()*5+3); + $shiftX = cos(floor(lcg_value()*360)*(pi()/180)); + $shiftZ = sin(floor(lcg_value()*360)*(pi()/180)); if(Utils::chance(5)) $randomID = $this->rarePossibleLoot[array_rand($this->rarePossibleLoot)]; else $randomID = $this->possibleLoot[array_rand($this->possibleLoot)]; $server->api->entity->drop(new Position($x+($shiftX*$randomRange)+0.5, $y, $z+($shiftZ*$randomRange)+0.5, $this->level), BlockAPI::getItem($randomID, 0, 1)); } for($i = 0; $i < $pigmen; $i++) { - $randomRange = floor(Utils::randomFloat()*5+3); - $shiftX = cos(floor(Utils::randomFloat()*360)*(pi()/180)); - $shiftZ = sin(floor(Utils::randomFloat()*360)*(pi()/180)); + $randomRange = floor(lcg_value()*5+3); + $shiftX = cos(floor(lcg_value()*360)*(pi()/180)); + $shiftZ = sin(floor(lcg_value()*360)*(pi()/180)); $data = array( "x" => $x+($shiftX*$randomRange)+0.5, "y" => $y, diff --git a/src/structure/village/SmallFarmStructure.php b/src/structure/village/SmallFarmStructure.php index b706127c..d63adf72 100644 --- a/src/structure/village/SmallFarmStructure.php +++ b/src/structure/village/SmallFarmStructure.php @@ -48,7 +48,7 @@ class SmallFarmStructure extends Structure{ protected function getMappingFor($char){ if($char === "R"){ - $f = Utils::randomFloat(); + $f = lcg_value(); if($f <= 0.5){ return [WHEAT_BLOCK, 0]; diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 5465d862..910a4e0e 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -456,107 +456,17 @@ class Utils{ } public static function getRandomBytes($length = 16, $secure = true, $raw = true, $startEntropy = "", &$rounds = 0, &$drop = 0){ - static $lastRandom = ""; - $output = b""; - $length = abs((int) $length); - $secureValue = ""; - $rounds = 0; - $drop = 0; - while(!isset($output[$length - 1])){ - //some entropy, but works ^^ - $weakEntropy = [ - is_array($startEntropy) ? implode($startEntropy) : $startEntropy, - serialize(@stat(__FILE__)), - __DIR__, - PHP_OS, - microtime(), - (string) lcg_value(), - (string) PHP_MAXPATHLEN, - PHP_SAPI, - (string) PHP_INT_MAX . "." . PHP_INT_SIZE, - serialize($_SERVER), - serialize(get_defined_constants()), - get_current_user(), - serialize(ini_get_all()), - (string) memory_get_usage() . "." . memory_get_peak_usage(), - php_uname(), - phpversion(), - //extension_loaded("gmp") ? gmp_strval(gmp_random(4)):microtime(), - zend_version(), - (string) getmypid(), - (string) getmyuid(), - (string) mt_rand(), - (string) getmyinode(), - (string) getmygid(), - (string) rand(), - function_exists("zend_thread_id") ? ((string) zend_thread_id()) : microtime(), - function_exists("getrusage") ? @implode(getrusage()) : microtime(), - function_exists("sys_getloadavg") ? @implode(sys_getloadavg()) : microtime(), - serialize(get_loaded_extensions()), - sys_get_temp_dir(), - (string) disk_free_space("."), - (string) disk_total_space("."), - uniqid(microtime(), true), - file_exists("/proc/cpuinfo") ? file_get_contents("/proc/cpuinfo") : microtime(), - ]; - - shuffle($weakEntropy); - $value = hash("sha512", implode($weakEntropy), true); - $lastRandom .= $value; - foreach($weakEntropy as $k => $c){ //mixing entropy values with XOR and hash randomness extractor - $value ^= hash("sha256", $c . microtime() . $k, true) . hash("sha256", mt_rand() . microtime() . $k . $c, true); - $value ^= hash("sha512", ((string) lcg_value()) . $c . microtime() . $k, true); - } - unset($weakEntropy); - - if($secure === true){ - $strongEntropyValues = [ - is_array($startEntropy) ? hash("sha512", $startEntropy[($rounds + $drop) % count($startEntropy)], true) : hash("sha512", $startEntropy, true), //Get a random index of the startEntropy, or just read it - file_exists("/dev/urandom") ? fread(fopen("/dev/urandom", "rb"), 64) : str_repeat("\x00", 64), - (function_exists("openssl_random_pseudo_bytes") and version_compare(PHP_VERSION, "5.3.4", ">=")) ? openssl_random_pseudo_bytes(64) : str_repeat("\x00", 64), - function_exists("mcrypt_create_iv") ? mcrypt_create_iv(64, MCRYPT_DEV_URANDOM) : str_repeat("\x00", 64), - $value, - ]; - $strongEntropy = array_pop($strongEntropyValues); - foreach($strongEntropyValues as $value){ - $strongEntropy = $strongEntropy ^ $value; - } - $value = ""; - //Von Neumann randomness extractor, increases entropy - $bitcnt = 0; - for($j = 0; $j < 64; ++$j){ - $a = ord($strongEntropy[$j]); - for($i = 0; $i < 8; $i += 2){ - $b = ($a & (1 << $i)) > 0 ? 1 : 0; - if($b != (($a & (1 << ($i + 1))) > 0 ? 1 : 0)){ - $secureValue = $b << $bitcnt; - if($bitcnt == 7){ - $value .= chr($secureValue); - $secureValue = 0; - $bitcnt = 0; - }else{ - ++$bitcnt; - } - ++$drop; - }else{ - $drop += 2; - } - } - } - } - $output .= substr($value, 0, min($length - strlen($output), $length)); - unset($value); - ++$rounds; - } - $lastRandom = hash("sha512", $lastRandom, true); - return $raw === false ? bin2hex($output) : $output; + return $raw ? random_bytes($length) : bin2hex(random_bytes($length)); //nobody would ever notice other parameters } - public static function chance($i){//GameHerobrine's code - return self::randomFloat() <= $i / 100; + public static function chance($i){ + return lcg_value() <= $i / 100; } - - public static function randomFloat(){//GameHerobrine's code + /** + * @deprecated use lcg_value() instead. + * @return number + */ + public static function randomFloat(){ return rand() / getrandmax(); } diff --git a/src/world/generator/ExperimentalGenerator.php b/src/world/generator/ExperimentalGenerator.php index b3951799..dc419eb4 100644 --- a/src/world/generator/ExperimentalGenerator.php +++ b/src/world/generator/ExperimentalGenerator.php @@ -66,7 +66,7 @@ class ExperimentalGenerator implements NewLevelGenerator{ new OreType(new LapisOreBlock(), 1, 6, 0, 32), new OreType(new GoldOreBlock(), 2, 8, 0, 32), new OreType(new DiamondOreBlock(), 1, 7, 0, 16), - new OreType(new EmeraldOreBlock(), 1, 7, 0, 16), //TODO vanilla + new OreType(new EmeraldOreBlock(), 1, 2, 0, 16), //TODO vanilla new OreType(new DirtBlock(), 20, 32, 0, 128), new OreType(new GravelBlock(), 10, 16, 0, 128), diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index 0186b809..510a6963 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -40,7 +40,7 @@ class NormalGenerator implements NewLevelGenerator{ new OreType(new LapisOreBlock(), 1, 6, 0, 32), new OreType(new GoldOreBlock(), 2, 8, 0, 32), new OreType(new DiamondOreBlock(), 1, 7, 0, 16), - new OreType(new EmeraldOreBlock(), 1, 7, 0, 16), //TODO vanilla + new OreType(new EmeraldOreBlock(), 1, 2, 0, 16), //TODO vanilla new OreType(new DirtBlock(), 20, 32, 0, 128), new OreType(new GravelBlock(), 10, 16, 0, 128), @@ -70,7 +70,7 @@ class NormalGenerator implements NewLevelGenerator{ $i = ($z << 4) + $x; $hills[$i] = $this->noiseHills->noise3D($x + ($chunkX << 4), 0, $z + ($chunkZ << 4), 0.11, 12, true); $patches[$i] = $this->noisePatches->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.03, 16, true); - $patchesSmall[$i] = $this->noisePatchesSmall->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.5, 4, true); + //$patchesSmall[$i] = $this->noisePatchesSmall->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.5, 4, true); $base[$i] = $this->noiseBase->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.7, 16, true); if($base[$i] < 0){ $base[$i] *= 0.5; From c150a463835e5e4e535df6b65e46c77940a196ad Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 28 Nov 2023 03:24:14 +0300 Subject: [PATCH 13/43] solid block collision ported from latest --- src/entity/Entity.php | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index d3ed05fe..973e913b 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -580,6 +580,25 @@ class Entity extends Position if(Utils::in_range($this->speedY, -0.007, 0.007)){ $this->speedY = 0; } + + $horizontalMultiplyFactor = 0.91; + if($this->onGround){ + $horizontalMultiplyFactor = 0.54; + $b = $this->level->level->getBlockID(floor($this->x), floor($this->boundingBox->minX) - 1, floor($this->z)); + if($b > 0){ + $horizontalMultiplyFactor = StaticBlock::getSlipperiness($b) * 0.91; + } + } + if($this->inWater){ + $this->speedX *= 0.8; + $this->speedY *= 0.8; + $this->speedZ *= 0.8; + }else{ + $this->speedX *= $horizontalMultiplyFactor; + $this->speedY *= 0.98; + $this->speedZ *= $horizontalMultiplyFactor; + } + $savedSpeedY = $this->speedY; if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM || ($this->class === ENTITY_OBJECT && $this->type === OBJECT_PRIMEDTNT)){ $water = false; @@ -614,24 +633,7 @@ class Entity extends Position } $support = $savedSpeedY != $this->speedY && $savedSpeedY < 0; - $horizontalMultiplyFactor = 0.91; - if($support){ - $horizontalMultiplyFactor = 0.54; - $b = $this->level->getBlockWithoutVector(floor($this->x), floor($this->boundingBox->minX) - 1, floor($this->z)); - if($b instanceof Block){ - $horizontalMultiplyFactor = $b->slipperiness * 0.91; - } - } - if($this->inWater){ - $this->speedX *= 0.8; - $this->speedY *= 0.8; - $this->speedZ *= 0.8; - }else{ - $this->speedX *= $horizontalMultiplyFactor; - $this->speedY *= 0.98; - $this->speedZ *= $horizontalMultiplyFactor; - } if($this->speedX != 0){ $this->x += $this->speedX; $update = true; From 07632377aca802ab3f28415102741165a086d1b9 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 28 Nov 2023 03:32:34 +0300 Subject: [PATCH 14/43] added chunk send delay into server.properties(min=0, default=5) --- src/API/ServerAPI.php | 2 ++ src/Player.php | 2 +- src/PocketMinecraftServer.php | 2 +- src/entity/Living.php | 9 ++++++--- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 805eddaf..739cdfc3 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -138,6 +138,7 @@ class ServerAPI{ "enable-rcon" => false, "rcon.password" => substr(base64_encode(Utils::getRandomBytes(20, false)), 3, 10), "auto-save" => true, + "chunk-send-delay-ticks" => 5, ]); Biome::init(); $this->parseProperties(); @@ -151,6 +152,7 @@ class ServerAPI{ } MobSpawner::$spawnAnimals = $this->getProperty("spawn-animals"); MobSpawner::$spawnMobs = $this->getProperty("spawn-mobs"); + PocketMinecraftServer::$chukSendDelay = $this->getProperty("chunk-send-delay-ticks"); if($this->getProperty("upnp-forwarding") == true){ console("[INFO] [UPnP] Trying to port forward..."); UPnP_PortForward($this->getProperty("server-port")); diff --git a/src/Player.php b/src/Player.php index 80a07411..8234ac6f 100755 --- a/src/Player.php +++ b/src/Player.php @@ -491,7 +491,7 @@ class Player{ } if($this->isSleeping) ++$this->sleepingTime; - if($this->chunkTicker++ > 5){ + if($this->chunkTicker++ > PocketMinecraftServer::$chukSendDelay){ $this->getNextChunk($this->level); $this->chunkTicker = 0; } diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 03c36eb9..1ce1fed6 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -1,7 +1,7 @@ path != null; } - public function __destruct() - { - parent::__destruct(); + public function close(){ + parent::close(); unset($this->pathFollower->entity); unset($this->ai->entity); + unset($this->ai->mobController->entity); + unset($this->ai); + unset($this->parent); } public function update(){ + if($this->closed) return; if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) { $this->ai->updateTasks(); } From 39b35aa30507a6e8b147774d3bd0a3e34681a453 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 28 Nov 2023 03:38:40 +0300 Subject: [PATCH 15/43] emojis from nc discord --- src/PocketMinecraftServer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 1ce1fed6..d89ce005 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -225,7 +225,7 @@ class PocketMinecraftServer{ "url" => $url, "data" => [ "username" => $name, - "content" => $this->extraprops->get("discord-ru-smiles") ? str_replace("@", " ", str_replace("Ы", "<:ru_cool:960113011383738369>", str_replace("Ь", "<:ru_cry:960112920346390548>", str_replace("Ъ", "<:ru_happy:960112868601237504>", $msg)))) : str_replace("@", "", $msg) + "content" => $this->extraprops->get("discord-ru-smiles") ? str_replace("@", " ", str_replace("Û", "<:imp_cool:1151085500396998719>", str_replace("Ü", "<:imp_badphp5:1151085478410457120>", str_replace("Ú", "<:imp_gudjava:1151085431962742784>", str_replace("¨", "<:imp_wut:1151085524241621012>", $msg))))) : str_replace("@", "", $msg) ], ], null); } From 3a1f5a28b16d70f8ae9a4f4c4339f8028b0aa950 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 28 Nov 2023 03:55:08 +0300 Subject: [PATCH 16/43] packets will send every tick, min_possible_speed is 1/8000 --- src/Player.php | 7 ++----- src/entity/Entity.php | 16 ++++++++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Player.php b/src/Player.php index 8234ac6f..6d32549e 100755 --- a/src/Player.php +++ b/src/Player.php @@ -97,7 +97,7 @@ class Player{ $this->packetStats = [0, 0]; $this->buffer = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->buffer->data = []; - $this->server->schedule(2, [$this, "handlePacketQueues"], [], true); + $this->server->schedule(1, [$this, "handlePacketQueues"], [], true); $this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); $this->evid[] = $this->server->event("server.close", [$this, "close"]); console("[DEBUG] New Session started with " . $ip . ":" . $port . ". MTU " . $this->MTU . ", Client ID " . $this->clientID, true, true, 2); @@ -334,7 +334,7 @@ class Player{ $this->bufferLen = 0; $this->buffer = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->buffer->data = []; - $this->nextBuffer = microtime(true) + 0.1; + $this->nextBuffer = microtime(true) + 0.05; } /** @@ -781,9 +781,6 @@ class Player{ $pk = new SetEntityMotionPacket; //TODO one packet for all entities if possible $pk->entities = [[$data->eid, $data->speedX, $data->speedY, $data->speedZ]]; $this->dataPacket($pk); - $data->lastSpeedZ = $data->speedZ; - $data->lastSpeedY = $data->speedY; - $data->lastSpeedX = $data->speedX; break; case "entity.animate": diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 973e913b..1eff1113 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -5,6 +5,7 @@ class Entity extends Position const TYPE = - 1; const CLASS_TYPE = - 1; + const MIN_POSSIBLE_SPEED = 1/8000; //anything below will send 0 to player public $counter = 0; public $invincible, $status, $health, $fire, $crouched, $position, $meta, $stack; @@ -207,11 +208,11 @@ class Entity extends Position } public function isMovingHorizontally() { - return ($this->speedX > 0.01 || $this->speedX < - 0.01) || ($this->speedZ > 0.01 || $this->speedZ < - 0.01); + return ($this->speedX >= self::MIN_POSSIBLE_SPEED || $this->speedX <= -self::MIN_POSSIBLE_SPEED) || ($this->speedZ >= self::MIN_POSSIBLE_SPEED || $this->speedZ <= -self::MIN_POSSIBLE_SPEED); } public function isMoving() { - return $this->isMovingHorizontally() || ($this->speedY > 0.007 || $this->speedY < - 0.007); + return $this->isMovingHorizontally() || ($this->speedY > self::MIN_POSSIBLE_SPEED || $this->speedY < -self::MIN_POSSIBLE_SPEED); } public function setVelocity($vX, $vY = 0, $vZ = 0) @@ -571,13 +572,13 @@ class Entity extends Position } if(!$this->isPlayer()){ $update = false; - if(Utils::in_range($this->speedX, -0.01, 0.01)){ + if($this->speedX > -self::MIN_POSSIBLE_SPEED && $this->speedX < self::MIN_POSSIBLE_SPEED){ $this->speedX = 0; } - if(Utils::in_range($this->speedZ, -0.01, 0.01)){ + if($this->speedZ > -self::MIN_POSSIBLE_SPEED && $this->speedZ < self::MIN_POSSIBLE_SPEED){ $this->speedZ = 0; } - if(Utils::in_range($this->speedY, -0.007, 0.007)){ + if($this->speedY > -self::MIN_POSSIBLE_SPEED && $this->speedY < self::MIN_POSSIBLE_SPEED){ $this->speedY = 0; } @@ -672,7 +673,7 @@ class Entity extends Position $this->onGround = $support; if($this->hasGravity){ - $this->speedY -= $this->inWater ? 0.02 : $this->gravity; // TODO: replace with $gravity + $this->speedY -= $this->inWater ? 0.02 : $this->gravity; // TODO: fix packet spam $update = true; } elseif($this->lastX != $this->x || $this->lastZ != $this->z || $this->lastY != $this->z){ // $this->speedX = 0; @@ -688,6 +689,9 @@ class Entity extends Position $hasUpdate = true; if(($this->server->ticks % 4 === 0 && $this->class === ENTITY_ITEM) || $this->class != ENTITY_ITEM){ //update item speed every 4 ticks $this->server->api->handle("entity.motion", $this); + $this->lastSpeedZ = $this->speedZ; + $this->lastSpeedY = $this->speedY; + $this->lastSpeedX = $this->speedX; } } } elseif($this->player instanceof Player){ From bb045abb09a4df61cf7118ac19c63e45382b7b5a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 28 Nov 2023 06:58:04 +0300 Subject: [PATCH 17/43] better generator --- src/constants/BlockIDs.php | 1 + src/dependencies.php | 9 ++++ src/world/generator/NormalGenerator.php | 36 +++++++++++---- src/world/generator/biome/BiomeSelector.php | 1 - .../biome/NormalGeneratorBiomeSelector.php | 24 ++++++++++ .../populator/BiomeBasedTreePopulator.php | 46 +++++++++++++++++++ .../populator/FlowerPatchPopulator.php | 45 ++++++++++++++++++ .../generator/populator/TreePopulator.php | 8 ++-- 8 files changed, 157 insertions(+), 13 deletions(-) create mode 100644 src/world/generator/biome/NormalGeneratorBiomeSelector.php create mode 100644 src/world/generator/populator/BiomeBasedTreePopulator.php create mode 100644 src/world/generator/populator/FlowerPatchPopulator.php diff --git a/src/constants/BlockIDs.php b/src/constants/BlockIDs.php index 8b8c4797..79e694cd 100755 --- a/src/constants/BlockIDs.php +++ b/src/constants/BlockIDs.php @@ -86,6 +86,7 @@ define("BLACK_WOOL", "35:15"); define("DANDELION", 37); define("YELLOW_FLOWER", 37); define("ROSE", 38); +define("MULTIFLOWER", 38); define("CYAN_FLOWER", 38); //NO define("RED_FLOWER", 38); define("BLUE_ORCHID", "38:1"); diff --git a/src/dependencies.php b/src/dependencies.php index 5258e999..f881fdf5 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -123,6 +123,8 @@ require_once(FILE_PATH . "/src/material/item/base/ItemSword.php"); require_once(FILE_PATH . "/src/material/item/armor/ArmorItem.php"); require_once(FILE_PATH . "/src/structure/Structure.php"); +require_once(FILE_PATH . "/src/world/generator/Populator.php"); +require_once(FILE_PATH . "/src/world/generator/LevelGenerator.php"); require_once(FILE_PATH . "/src/plugin/Plugin.php"); require_once(FILE_PATH . "/src/plugin/OtherPluginRequirement.php"); @@ -134,11 +136,18 @@ require_once(FILE_PATH . "/src/world/generator/LevelGenerator.php"); require_once(FILE_PATH . "/src/world/generator/NewLevelGenerator.php"); require_once(FILE_PATH . "/src/world/generator/biome/Biome.php"); require_once(FILE_PATH . "/src/world/generator/biome/BiomeSelector.php"); +require_once(FILE_PATH . "/src/world/generator/biome/NormalGeneratorBiomeSelector.php"); require_once(FILE_PATH . "/src/world/generator/biome/biomes/BiomeWithGrass.php"); require_once(FILE_PATH . "/src/world/generator/biome/biomes/BiomeWithSand.php"); require_once(FILE_PATH . "/src/world/generator/biome/biomes/BiomeWithSnow.php"); require_once(FILE_PATH . "/src/world/generator/biome/biomes/BiomeExtremeHills.php"); + +require_once(FILE_PATH . "/src/world/generator/populator/TreePopulator.php"); + + + + require_all(FILE_PATH . "src/"); diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index 510a6963..76bb1d9f 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -14,7 +14,7 @@ class NormalGenerator implements NewLevelGenerator{ private $noisePatches; private $noisePatchesSmall; private $noiseBase; - + private $biomeSelector; public function __construct(array $options = array()){ } @@ -31,6 +31,7 @@ class NormalGenerator implements NewLevelGenerator{ $this->noisePatches = new NoiseGeneratorPerlin($this->random, 2); $this->noisePatchesSmall = new NoiseGeneratorPerlin($this->random, 2); $this->noiseBase = new NoiseGeneratorPerlin($this->random, 16); + $this->biomeSelector = new NormalGeneratorBiomeSelector($this->random, BiomeSelector::$biomes[BIOME_PLAINS]); $ores = new OrePopulator(); $ores->setOreTypes(array( @@ -50,23 +51,42 @@ class NormalGenerator implements NewLevelGenerator{ )); $this->populators[] = $ores; - $trees = new TreePopulator(); - $trees->setBaseAmount(3); - $trees->setRandomAmount(0); + $trees = new BiomeBasedTreePopulator(); + $trees->setBaseAmount(8); + $trees->setRandomAmount(2); $this->populators[] = $trees; + $this->populators[] = new FlowerPatchPopulator(); + $tallGrass = new TallGrassPopulator(); $tallGrass->setBaseAmount(5); $tallGrass->setRandomAmount(0); $this->populators[] = $tallGrass; + + } + + public function pickBiome(int $x, int $z){ + $hash = $x * 2345803 ^ $z * 9236449 ^ $this->level->level->getSeed(); + $hash *= $hash + 223; + $xNoise = ((int)$hash) >> 20 & 3; //why dont u have types for local variables?? + $zNoise = ((int)$hash) >> 22 & 3; + if($xNoise == 3){ + $xNoise = 1; + } + if($zNoise == 3){ + $zNoise = 1; + } + return $this->biomeSelector->pickBiome($x + $xNoise - 1, $z + $zNoise - 1); } public function generateChunk($chunkX, $chunkZ){ $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); $hills = array(); $base = array(); + $biomes = str_repeat(chr(BIOME_PLAINS), 256); for($z = 0; $z < 16; ++$z){ for($x = 0; $x < 16; ++$x){ + $biomes[($z << 4) + $x] = chr($this->pickBiome($chunkX * 16 + $x, $chunkZ * 16 + $z)->id); $i = ($z << 4) + $x; $hills[$i] = $this->noiseHills->noise3D($x + ($chunkX << 4), 0, $z + ($chunkZ << 4), 0.11, 12, true); $patches[$i] = $this->noisePatches->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.03, 16, true); @@ -86,7 +106,7 @@ class NormalGenerator implements NewLevelGenerator{ $i = ($z << 4) + $x; $height = $this->worldHeight + $hills[$i] * 14 + $base[$i] * 7; $height = (int) $height; - + $biomeID = ord($biomes[$i]); for($y = $startY; $y < $endY; ++$y){ $diff = $height - $y; if($y <= 4 and ($y === 0 or $this->random->nextFloat() < 0.33)){ @@ -99,7 +119,7 @@ class NormalGenerator implements NewLevelGenerator{ }elseif($patches[$i] < -0.8){ $chunk .= "\x0d"; //gravel }else{ - $chunk .= "\x03"; //dirt + $chunk .= $biomeID === BIOME_DESERT ? chr(SANDSTONE) : chr(DIRT); } }elseif($y <= $this->waterHeight){ if(($this->waterHeight - $y) <= 1 and $diff === 0){ @@ -121,7 +141,7 @@ class NormalGenerator implements NewLevelGenerator{ }elseif($patches[$i] < -0.8){ $chunk .= "\x0d"; //gravel }else{ - $chunk .= "\x02"; //grass + $chunk .= $biomeID === BIOME_DESERT ? chr(SAND) : chr(GRASS); //grass } }else{ $chunk .= "\x00"; @@ -132,7 +152,7 @@ class NormalGenerator implements NewLevelGenerator{ $chunk .= "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; //skylight/light } } - $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, str_repeat(chr(BIOME_PLAINS), 256)); + $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, $biomes); $this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk); } } diff --git a/src/world/generator/biome/BiomeSelector.php b/src/world/generator/biome/BiomeSelector.php index 0de1f1a2..072e52b1 100644 --- a/src/world/generator/biome/BiomeSelector.php +++ b/src/world/generator/biome/BiomeSelector.php @@ -91,7 +91,6 @@ class BiomeSelector public function pickBiome($x, $z){ $temperature = (int) ($this->getTemperature($x, $z) * 63); $rainfall = (int) ($this->getRainfall($x, $z) * 63); - $biomeId = $this->map[$temperature + ($rainfall << 6)] ?? -1; return self::$biomes[$biomeId] ?? $this->fallback; } diff --git a/src/world/generator/biome/NormalGeneratorBiomeSelector.php b/src/world/generator/biome/NormalGeneratorBiomeSelector.php new file mode 100644 index 00000000..9a6ed368 --- /dev/null +++ b/src/world/generator/biome/NormalGeneratorBiomeSelector.php @@ -0,0 +1,24 @@ +level = $level; + $amount = $random->nextRange(0, $this->randomAmount + 1) + $this->baseAmount; + for($i = 0; $i < $amount; ++$i){ + $x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15); + $z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15); + $biomeID = $level->level->getBiomeId($x, $z); + switch($biomeID){ + case BIOME_FOREST: + $f = $random->nextFloat(); + if($f > 0.75){ + $meta = SaplingBlock::BIRCH; + }else{ + $meta = SaplingBlock::OAK; + } + break; + case BIOME_JUNGLE: + cosnole("placing jungle wood on $x $z"); + $meta = SaplingBlock::JUNGLE; + break; + case BIOME_TAIGA: + $meta = SaplingBlock::SPRUCE; + break; + case BIOME_PLAINS: + $f = $random->nextFloat(); + if($f > 0.9){ + $meta = SaplingBlock::OAK; + break; + } + return; + default: + return; + } + $y = $this->getHighestWorkableBlock($x, $z); + if($y === -1){ + continue; + } + TreeObject::growTree($this->level, new Vector3($x, $y, $z), $random, $meta); + } + } +} + diff --git a/src/world/generator/populator/FlowerPatchPopulator.php b/src/world/generator/populator/FlowerPatchPopulator.php new file mode 100644 index 00000000..a844d988 --- /dev/null +++ b/src/world/generator/populator/FlowerPatchPopulator.php @@ -0,0 +1,45 @@ +nextRange(0, 15); + $z = $chunkZ*16 + $random->nextRange(0, 15); + + for($i = 0; $i < 4; ++$i){ + $xPos = ($x + $random->nextRange(0, 7)) - $random->nextRange(0, 7); + $zPos = ($z + $random->nextRange(0, 7)) - $random->nextRange(0, 7); + $yPos = $this->getHighestWorkableBlock($level, $xPos, $zPos); + if($level->level->getBlockID($xPos, $yPos, $zPos) == 0){ + $flowerIDMeta = self::$flowerIDS[$random->nextRange(0, count(self::$flowerIDS)-1)]; + $level->level->setBlock($xPos, $yPos, $zPos, $flowerIDMeta[0], $flowerIDMeta[1]); + } + } + } + + private function getHighestWorkableBlock(Level $level, $x, $z){ + for($y = 128; $y > 0; --$y){ + $b = $level->level->getBlockID($x, $y, $z); + if($b == GRASS || $b == DIRT){ + return $y + 1; + } + } + return -1; + } +} + diff --git a/src/world/generator/populator/TreePopulator.php b/src/world/generator/populator/TreePopulator.php index 5ab636bf..50ade1d4 100644 --- a/src/world/generator/populator/TreePopulator.php +++ b/src/world/generator/populator/TreePopulator.php @@ -1,8 +1,8 @@ randomAmount = $amount; @@ -33,7 +33,7 @@ class TreePopulator extends Populator{ } } - private function getHighestWorkableBlock($x, $z){ + public function getHighestWorkableBlock($x, $z){ for($y = 128; $y > 0; --$y){ $b = $this->level->level->getBlockID($x, $y, $z); if($b !== DIRT and $b !== GRASS){ From 92b89194f12bea5c67bca3125f5da720cf0f9874 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 28 Nov 2023 07:23:45 +0300 Subject: [PATCH 18/43] cacti and ded buses in the desert --- src/world/generator/NormalGenerator.php | 17 ++++---- .../generator/populator/DesertPopulator.php | 40 +++++++++++++++++++ 2 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 src/world/generator/populator/DesertPopulator.php diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index 76bb1d9f..e1439f64 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -50,7 +50,7 @@ class NormalGenerator implements NewLevelGenerator{ new OreType(new StoneBlock(5), 12, 16, 0, 128), )); $this->populators[] = $ores; - + $this->populators[] = new DesertPopulator(); //TODO biome populators $trees = new BiomeBasedTreePopulator(); $trees->setBaseAmount(8); $trees->setRandomAmount(2); @@ -123,17 +123,15 @@ class NormalGenerator implements NewLevelGenerator{ } }elseif($y <= $this->waterHeight){ if(($this->waterHeight - $y) <= 1 and $diff === 0){ - $chunk .= "\x0c"; //sand + $chunk .= ($biomeID === BIOME_TAIGA) ? chr(GRASS) : chr(SAND); }elseif($diff === 0){ - /*if($patchesSmall[$i] > 0.3){ - $chunk .= "\x0d"; //gravel - }elseif($patchesSmall[$i] < -0.45){ - $chunk .= "\x0c"; //sand - }else{*/ - $chunk .= "\x03"; //dirt - //} + $chunk .= "\x03"; //dirt }else{ + //if($y === $this->waterHeight && $biomeID == BIOME_TAIGA){ + // $chunk .= chr(ICE); + ///}else{ $chunk .= "\x09"; //still_water + //} } }elseif($diff === 0){ if($patches[$i] > 0.7){ @@ -161,7 +159,6 @@ class NormalGenerator implements NewLevelGenerator{ $this->level->level->setPopulated($chunkX, $chunkZ, true); $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); foreach($this->populators as $populator){ - $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); $populator->populate($this->level, $chunkX, $chunkZ, $this->random); } } diff --git a/src/world/generator/populator/DesertPopulator.php b/src/world/generator/populator/DesertPopulator.php new file mode 100644 index 00000000..0a4aeed3 --- /dev/null +++ b/src/world/generator/populator/DesertPopulator.php @@ -0,0 +1,40 @@ +nextRange(0, 15); + $z = $chunkZ*16 + $random->nextRange(0, 15); + if($level->level->getBiomeId($x, $z) === BIOME_DESERT){ + $y = $this->getHighestWorkableBlock($level, $x, $z); + $level->level->setBlockID($x, $y, $z, DEAD_BUSH); + } + } + + for($i = 0; $i < 2; ++$i){ + $x = $chunkX*16 + $random->nextRange(0, 15); + $z = $chunkZ*16 + $random->nextRange(0, 15); + if($level->level->getBiomeId($x, $z) === BIOME_DESERT){ + $y = $this->getHighestWorkableBlock($level, $x, $z); + $cactiSize = $random->nextRange(0, 3); + for($yOff = 0; $yOff < $cactiSize; ++$yOff){ + $level->level->setBlockID($x, $y + $yOff, $z, CACTUS); + } + } + } + + } + + private function getHighestWorkableBlock(Level $level, $x, $z){ + for($y = 128; $y > 0; --$y){ + $b = $level->level->getBlockID($x, $y, $z); + if($b == SAND && $level->level->getBlockID($x, $y + 1, $z) == 0){ + return $y + 1; + } + } + return -1; + } +} + From f405ad8b175cdc31d5199dee8366d66877563604 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 28 Nov 2023 10:23:45 +0300 Subject: [PATCH 19/43] using 2d noise + removed console --- src/world/generator/NormalGenerator.php | 5 ++--- src/world/generator/populator/BiomeBasedTreePopulator.php | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index e1439f64..4505497e 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -88,9 +88,8 @@ class NormalGenerator implements NewLevelGenerator{ for($x = 0; $x < 16; ++$x){ $biomes[($z << 4) + $x] = chr($this->pickBiome($chunkX * 16 + $x, $chunkZ * 16 + $z)->id); $i = ($z << 4) + $x; - $hills[$i] = $this->noiseHills->noise3D($x + ($chunkX << 4), 0, $z + ($chunkZ << 4), 0.11, 12, true); + $hills[$i] = $this->noiseHills->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.11, 12, true); $patches[$i] = $this->noisePatches->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.03, 16, true); - //$patchesSmall[$i] = $this->noisePatchesSmall->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.5, 4, true); $base[$i] = $this->noiseBase->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.7, 16, true); if($base[$i] < 0){ $base[$i] *= 0.5; @@ -139,7 +138,7 @@ class NormalGenerator implements NewLevelGenerator{ }elseif($patches[$i] < -0.8){ $chunk .= "\x0d"; //gravel }else{ - $chunk .= $biomeID === BIOME_DESERT ? chr(SAND) : chr(GRASS); //grass + $chunk .= $biomeID === BIOME_DESERT ? chr(SAND) : chr(GRASS); } }else{ $chunk .= "\x00"; diff --git a/src/world/generator/populator/BiomeBasedTreePopulator.php b/src/world/generator/populator/BiomeBasedTreePopulator.php index 08e25fe6..8b63aa50 100644 --- a/src/world/generator/populator/BiomeBasedTreePopulator.php +++ b/src/world/generator/populator/BiomeBasedTreePopulator.php @@ -19,7 +19,6 @@ class BiomeBasedTreePopulator extends \TreePopulator } break; case BIOME_JUNGLE: - cosnole("placing jungle wood on $x $z"); $meta = SaplingBlock::JUNGLE; break; case BIOME_TAIGA: From f274b3698550a7405e4568a6e455a408f1d7e44f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 29 Nov 2023 03:56:04 +0300 Subject: [PATCH 20/43] biome decorators, jungle biome --- src/dependencies.php | 3 ++ src/pmf/PMFLevel.php | 14 +++++- src/world/generator/NormalGenerator.php | 15 +++++-- src/world/generator/biome/Biome.php | 13 ++++++ src/world/generator/biome/BiomeDecorator.php | 9 ++++ .../generator/biome/biomes/BiomeDesert.php | 4 ++ .../generator/biome/biomes/BiomeForest.php | 8 ++++ .../generator/biome/biomes/BiomeJungle.php | 14 ++++++ .../generator/biome/biomes/BiomePlains.php | 4 ++ .../generator/biome/biomes/BiomeTaiga.php | 8 ++++ .../biome/decorator/DesertBiomeDecorator.php | 34 ++++++++++++++ .../generator/object/tree/SmallTreeObject.php | 7 ++- .../generator/object/tree/TreeObject.php | 9 ++-- .../populator/BiomeBasedTreePopulator.php | 45 ++++++++----------- .../generator/populator/DesertPopulator.php | 40 ----------------- 15 files changed, 147 insertions(+), 80 deletions(-) create mode 100644 src/world/generator/biome/BiomeDecorator.php create mode 100644 src/world/generator/biome/biomes/BiomeJungle.php create mode 100644 src/world/generator/biome/decorator/DesertBiomeDecorator.php delete mode 100644 src/world/generator/populator/DesertPopulator.php diff --git a/src/dependencies.php b/src/dependencies.php index f881fdf5..94734f6d 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -134,6 +134,9 @@ require_once(FILE_PATH . "/src/plugin/phar/PharUtils.php"); require_once(FILE_PATH . "/src/world/generator/LevelGenerator.php"); require_once(FILE_PATH . "/src/world/generator/NewLevelGenerator.php"); + +require_once(FILE_PATH . "/src/world/generator/biome/BiomeDecorator.php"); + require_once(FILE_PATH . "/src/world/generator/biome/Biome.php"); require_once(FILE_PATH . "/src/world/generator/biome/BiomeSelector.php"); require_once(FILE_PATH . "/src/world/generator/biome/NormalGeneratorBiomeSelector.php"); diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index e8b5a686..8324e1d5 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -600,7 +600,18 @@ class PMFLevel extends PMF{ $Z = $z >> 4; $Y = $y >> 4; $block &= 0xFF; + $index = $this->getIndex($X, $Z); + $index = $this->getIndex($X, $Z); + if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){ + if($this->loadChunk($X, $Z, false) === false){ + $this->createUnpopulatedChunk($X, $Z); + } + } + if($this->chunks[$index][$Y] === false){ + $this->fillMiniChunk($X, $Z, $Y); + } + $aX = $x & 0xf; $aZ = $z & 0xf; $aY = $y & 0xf; @@ -726,13 +737,12 @@ class PMFLevel extends PMF{ if($Y >= 128 or $y < 0){ return false; } - $loaded = false; + $index = $this->getIndex($X, $Z); if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){ if($this->loadChunk($X, $Z, false) === false){ $this->createUnpopulatedChunk($X, $Z); } - $loaded = true; } if($this->chunks[$index][$Y] === false){ $this->fillMiniChunk($X, $Z, $Y); diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index 4505497e..e095ab19 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -50,10 +50,9 @@ class NormalGenerator implements NewLevelGenerator{ new OreType(new StoneBlock(5), 12, 16, 0, 128), )); $this->populators[] = $ores; - $this->populators[] = new DesertPopulator(); //TODO biome populators $trees = new BiomeBasedTreePopulator(); - $trees->setBaseAmount(8); - $trees->setRandomAmount(2); + $trees->setBaseAmount(8); //TODO biome based amount? + $trees->setRandomAmount(4); $this->populators[] = $trees; $this->populators[] = new FlowerPatchPopulator(); @@ -157,6 +156,16 @@ class NormalGenerator implements NewLevelGenerator{ public function populateChunk($chunkX, $chunkZ){ $this->level->level->setPopulated($chunkX, $chunkZ, true); $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); + + $biomeID = $this->level->level->getBiomeId($chunkX*16, $chunkZ*16); + $biome = BiomeSelector::get($biomeID); + if($biome === false){ + ConsoleAPI::warn("Failed to get Biome with id {$biomeID} at $chunkX, $chunkZ"); + }else{ + $biome->decorator->decorate($this->level, $chunkX, $chunkZ, $this->random); + } + + foreach($this->populators as $populator){ $populator->populate($this->level, $chunkX, $chunkZ, $this->random); } diff --git a/src/world/generator/biome/Biome.php b/src/world/generator/biome/Biome.php index 3858ba34..8822ff69 100644 --- a/src/world/generator/biome/Biome.php +++ b/src/world/generator/biome/Biome.php @@ -2,15 +2,27 @@ abstract class Biome{ public $id, $topBlocks, $fillerBlock, $name, $minY, $maxY; + + public $decorator = false; + public function __construct($id, $name){ $this->name = $name; $this->id = $id; + $this->decorator = $this->createBiomeDecorator(); } public function setTopBlocks($id){ $this->topBlocks = $id; } + public function createBiomeDecorator(){ + return new BiomeDecorator(); + } + + public function getTree(Random $random){ + return null; + } + public function getID(){ return $this->id; } @@ -57,6 +69,7 @@ abstract class Biome{ BiomeSelector::registerBiome(new BiomeTaiga(BIOME_TAIGA, "Taiga")); BiomeSelector::registerBiome(new BiomeForest(BIOME_FOREST, "Forest")); BiomeSelector::registerBiome(new BiomeForest(BIOME_BIRCH_FOREST, "Birch Forest")); + BiomeSelector::registerBiome(new BiomeJungle(BIOME_JUNGLE, "Jungle")); } diff --git a/src/world/generator/biome/BiomeDecorator.php b/src/world/generator/biome/BiomeDecorator.php new file mode 100644 index 00000000..435b2b71 --- /dev/null +++ b/src/world/generator/biome/BiomeDecorator.php @@ -0,0 +1,9 @@ +setMinMax(63, 74); } + + public function createBiomeDecorator(){ + return new DesertBiomeDecorator(); + } } diff --git a/src/world/generator/biome/biomes/BiomeForest.php b/src/world/generator/biome/biomes/BiomeForest.php index 5742c09b..709f889f 100644 --- a/src/world/generator/biome/biomes/BiomeForest.php +++ b/src/world/generator/biome/biomes/BiomeForest.php @@ -7,5 +7,13 @@ class BiomeForest extends BiomeWithGrass $this->setMinMax(63, 81); } + + public function getTree(Random $random){ + $f = $random->nextFloat(); + if($f > 0.75){ + return new SmallTreeObject(SaplingBlock::BIRCH); + } + return new SmallTreeObject(SaplingBlock::OAK); + } } diff --git a/src/world/generator/biome/biomes/BiomeJungle.php b/src/world/generator/biome/biomes/BiomeJungle.php new file mode 100644 index 00000000..87e5a097 --- /dev/null +++ b/src/world/generator/biome/biomes/BiomeJungle.php @@ -0,0 +1,14 @@ +setMinMax(63, 81); + } + + public function getTree(Random $random){ + return new SmallTreeObject(SaplingBlock::JUNGLE); + } +} + diff --git a/src/world/generator/biome/biomes/BiomePlains.php b/src/world/generator/biome/biomes/BiomePlains.php index 86e0a85b..88506ab2 100644 --- a/src/world/generator/biome/biomes/BiomePlains.php +++ b/src/world/generator/biome/biomes/BiomePlains.php @@ -5,4 +5,8 @@ class BiomePlains extends BiomeWithGrass{ parent::__construct($id, $name); $this->setMinMax(61, 68); } + + public function getTree(Random $random){ + return $random->nextInt(10) == 0 ? new SmallTreeObject(SaplingBlock::OAK) : null; + } } diff --git a/src/world/generator/biome/biomes/BiomeTaiga.php b/src/world/generator/biome/biomes/BiomeTaiga.php index 7cd39672..ddf9f17e 100644 --- a/src/world/generator/biome/biomes/BiomeTaiga.php +++ b/src/world/generator/biome/biomes/BiomeTaiga.php @@ -13,5 +13,13 @@ class BiomeTaiga extends BiomeWithSnow [DIRT, 0], ]); } + + public function getTree(Random $random){ + $n = $random->nextInt(3); + if($n == 0){ + return new PineTreeObject(); + } + return new SpruceTreeObject(); + } } diff --git a/src/world/generator/biome/decorator/DesertBiomeDecorator.php b/src/world/generator/biome/decorator/DesertBiomeDecorator.php new file mode 100644 index 00000000..4f5054c3 --- /dev/null +++ b/src/world/generator/biome/decorator/DesertBiomeDecorator.php @@ -0,0 +1,34 @@ +nextInt(16); + $z = $chunkZ*16 + $random->nextInt(16); + $y = $this->getHighestWorkableBlock($level, $x, $z); + $level->level->setBlockID($x, $y, $z, DEAD_BUSH); + } + + for($i = 0; $i < 2; ++$i){ + $x = $chunkX*16 + $random->nextInt(16); + $z = $chunkZ*16 + $random->nextInt(16); + $y = $this->getHighestWorkableBlock($level, $x, $z); + $cactiSize = $random->nextRange(0, 3); + for($yOff = 0; $yOff < $cactiSize; ++$yOff){ + $level->level->setBlockID($x, $y + $yOff, $z, CACTUS); + } + } + } + + private function getHighestWorkableBlock(Level $level, $x, $z){ + for($y = 128; $y > 0; --$y){ + $b = $level->level->getBlockID($x, $y, $z); + if($b == SAND && $level->level->getBlockID($x, $y + 1, $z) == 0){ + return $y + 1; + } + } + return -1; + } +} + diff --git a/src/world/generator/object/tree/SmallTreeObject.php b/src/world/generator/object/tree/SmallTreeObject.php index 03b47bda..3cf49e0f 100644 --- a/src/world/generator/object/tree/SmallTreeObject.php +++ b/src/world/generator/object/tree/SmallTreeObject.php @@ -8,13 +8,16 @@ class SmallTreeObject extends TreeObject{ private static $leavesHeight = 4; private static $leafRadii = [1, 1.41, 2.83, 2.24]; - public $type = 0; // All trees appear to be 4 tall + public $type = 0; public $treeHeight = 7; private $trunkHeight = 5; private $addLeavesVines = false; private $addLogVines = false; private $addCocoaPlants = false; - + public function __construct($treeType = 0){ + parent::__construct(); + $this->type = $treeType; + } public function canPlaceObject(Level $level, Vector3 $pos, Random $random){ $radiusToCheck = 0; for($yy = 0; $yy < $this->trunkHeight + 3; ++$yy){ diff --git a/src/world/generator/object/tree/TreeObject.php b/src/world/generator/object/tree/TreeObject.php index 3973ebb3..c8210c76 100644 --- a/src/world/generator/object/tree/TreeObject.php +++ b/src/world/generator/object/tree/TreeObject.php @@ -1,7 +1,7 @@ true, 6 => true, @@ -21,15 +21,12 @@ class TreeObject{ } break; case SaplingBlock::BIRCH: - $tree = new SmallTreeObject(); - $tree->type = SaplingBlock::BIRCH; + $tree = new SmallTreeObject(SaplingBlock::BIRCH); break; case SaplingBlock::JUNGLE: - $tree = new SmallTreeObject(); - $tree->type = SaplingBlock::JUNGLE; + $tree = new SmallTreeObject(SaplingBlock::JUNGLE); break; case SaplingBlock::ACACIA: - console("acacia"); $tree = new AcaciaTreeObject(); break; case SaplingBlock::DARK_OAK: diff --git a/src/world/generator/populator/BiomeBasedTreePopulator.php b/src/world/generator/populator/BiomeBasedTreePopulator.php index 8b63aa50..512f8fc6 100644 --- a/src/world/generator/populator/BiomeBasedTreePopulator.php +++ b/src/world/generator/populator/BiomeBasedTreePopulator.php @@ -9,36 +9,27 @@ class BiomeBasedTreePopulator extends \TreePopulator $x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15); $z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15); $biomeID = $level->level->getBiomeId($x, $z); - switch($biomeID){ - case BIOME_FOREST: - $f = $random->nextFloat(); - if($f > 0.75){ - $meta = SaplingBlock::BIRCH; - }else{ - $meta = SaplingBlock::OAK; - } - break; - case BIOME_JUNGLE: - $meta = SaplingBlock::JUNGLE; - break; - case BIOME_TAIGA: - $meta = SaplingBlock::SPRUCE; - break; - case BIOME_PLAINS: - $f = $random->nextFloat(); - if($f > 0.9){ - $meta = SaplingBlock::OAK; - break; - } - return; - default: - return; + $biome = BiomeSelector::get($biomeID); + $treeFeature = null; + if($biome instanceof Biome){ + if($biome->id === BIOME_JUNGLE){ + console("JUNGLE AT $chunkX $chunkZ"); + } + $treeFeature = $biome->getTree($random); } - $y = $this->getHighestWorkableBlock($x, $z); - if($y === -1){ + + if($treeFeature instanceof TreeObject){ + $y = $this->getHighestWorkableBlock($x, $z); + if($y === -1){ + continue; + } + $v3 = new Vector3($x, $y, $z); //TODO no v3 + if($treeFeature->canPlaceObject($level, $v3, $random)){ + $treeFeature->placeObject($level, $v3, $random); + } + }else{ continue; } - TreeObject::growTree($this->level, new Vector3($x, $y, $z), $random, $meta); } } } diff --git a/src/world/generator/populator/DesertPopulator.php b/src/world/generator/populator/DesertPopulator.php deleted file mode 100644 index 0a4aeed3..00000000 --- a/src/world/generator/populator/DesertPopulator.php +++ /dev/null @@ -1,40 +0,0 @@ -nextRange(0, 15); - $z = $chunkZ*16 + $random->nextRange(0, 15); - if($level->level->getBiomeId($x, $z) === BIOME_DESERT){ - $y = $this->getHighestWorkableBlock($level, $x, $z); - $level->level->setBlockID($x, $y, $z, DEAD_BUSH); - } - } - - for($i = 0; $i < 2; ++$i){ - $x = $chunkX*16 + $random->nextRange(0, 15); - $z = $chunkZ*16 + $random->nextRange(0, 15); - if($level->level->getBiomeId($x, $z) === BIOME_DESERT){ - $y = $this->getHighestWorkableBlock($level, $x, $z); - $cactiSize = $random->nextRange(0, 3); - for($yOff = 0; $yOff < $cactiSize; ++$yOff){ - $level->level->setBlockID($x, $y + $yOff, $z, CACTUS); - } - } - } - - } - - private function getHighestWorkableBlock(Level $level, $x, $z){ - for($y = 128; $y > 0; --$y){ - $b = $level->level->getBlockID($x, $y, $z); - if($b == SAND && $level->level->getBlockID($x, $y + 1, $z) == 0){ - return $y + 1; - } - } - return -1; - } -} - From 894530e4fbadba5d7411796ab4a9c8d7187c7d62 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 29 Nov 2023 07:24:30 +0300 Subject: [PATCH 21/43] removed jungle spam, unfinished cavegenerator --- src/pmf/PMFLevel.php | 6 +- src/world/generator/NormalGenerator.php | 6 +- src/world/generator/biome/BiomeSelector.php | 10 +- .../biome/NormalGeneratorBiomeSelector.php | 8 + src/world/generator/cave/CaveGenerator.php | 177 ++++++++++++++++++ .../populator/BiomeBasedTreePopulator.php | 3 - 6 files changed, 198 insertions(+), 12 deletions(-) create mode 100644 src/world/generator/cave/CaveGenerator.php diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 8324e1d5..285a7d97 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -608,9 +608,9 @@ class PMFLevel extends PMF{ $this->createUnpopulatedChunk($X, $Z); } } - if($this->chunks[$index][$Y] === false){ - $this->fillMiniChunk($X, $Z, $Y); - } + //if($this->chunks[$index][$Y] === false){ + // $this->fillMiniChunk($X, $Z, $Y); + //} $aX = $x & 0xf; $aZ = $z & 0xf; diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index e095ab19..3c231f0e 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -15,6 +15,7 @@ class NormalGenerator implements NewLevelGenerator{ private $noisePatchesSmall; private $noiseBase; private $biomeSelector; + private $caveGenerator; public function __construct(array $options = array()){ } @@ -61,7 +62,7 @@ class NormalGenerator implements NewLevelGenerator{ $tallGrass->setBaseAmount(5); $tallGrass->setRandomAmount(0); $this->populators[] = $tallGrass; - + $this->caveGenerator = new CaveGenerator($this->level->getSeed()); } public function pickBiome(int $x, int $z){ @@ -151,6 +152,9 @@ class NormalGenerator implements NewLevelGenerator{ $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, $biomes); $this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk); } + //$this->caveGenerator->curChunkX = $chunkX; + //$this->caveGenerator->curChunkZ = $chunkZ; + //$this->caveGenerator->generate($this->level, $chunkX, $chunkZ); } public function populateChunk($chunkX, $chunkZ){ diff --git a/src/world/generator/biome/BiomeSelector.php b/src/world/generator/biome/BiomeSelector.php index 072e52b1..b9a7f2a6 100644 --- a/src/world/generator/biome/BiomeSelector.php +++ b/src/world/generator/biome/BiomeSelector.php @@ -3,19 +3,19 @@ class BiomeSelector { /** @var Biome */ - private $fallback; + public $fallback; /** @var NoiseGeneratorSimplex */ - private $temperature; + public $temperature; /** @var NoiseGeneratorSimplex */ - private $rainfall; + public $rainfall; /** @var Biome[] */ public static $biomes = []; - private $map = []; + public $map = []; - private $lookup; + public $lookup; public function __construct(Random $random, Biome $fallback){ $this->fallback = $fallback; diff --git a/src/world/generator/biome/NormalGeneratorBiomeSelector.php b/src/world/generator/biome/NormalGeneratorBiomeSelector.php index 9a6ed368..bd483645 100644 --- a/src/world/generator/biome/NormalGeneratorBiomeSelector.php +++ b/src/world/generator/biome/NormalGeneratorBiomeSelector.php @@ -2,6 +2,14 @@ class NormalGeneratorBiomeSelector extends BiomeSelector { + public function getTemperature($x, $z){ //TODO cache + return ($this->temperature->noise2D($x * 0.001953125, $z * 0.001953125, 4, 0.0625, true) + 1) / 2; + } + + public function getRainfall($x, $z){ + return ($this->rainfall->noise2D($x * 0.001953125, $z * 0.001953125, 4, 0.0625, true) + 1) / 2; + } + public function getBiomeTR($t, $r){ if($r < 0.60){ if($t < 0.75){ diff --git a/src/world/generator/cave/CaveGenerator.php b/src/world/generator/cave/CaveGenerator.php new file mode 100644 index 00000000..cbf62f8d --- /dev/null +++ b/src/world/generator/cave/CaveGenerator.php @@ -0,0 +1,177 @@ +rand = new Random($seed); + } + + public function generate(Level $level, $xCenter, $zCenter){ + $i = $this->range; + $this->rand->setSeed($level->getSeed()); + $seedInt1 = ((int)($this->rand->nextInt() / 2) * 2) + 1; + $seedInt2 = ((int)($this->rand->nextInt() / 2) * 2) + 1; + + for($x = $xCenter - $i; $x <= $xCenter + $i; ++$x){ + for($z = $zCenter - $i; $z <= $zCenter + $i; ++$z){ + $this->rand->setSeed((($x * $seedInt1) + ($z * $seedInt2)) ^ $level->getSeed()); + $this->generate2($level, $x, $z, $xCenter, $zCenter); + } + } + } + + public function generate2(Level $level, $x, $z, $xCenter, $zCenter){ + $nextInt = $this->rand->nextInt($this->rand->nextInt($this->rand->nextInt(40) + 1) + 1); + if($this->rand->nextInt(15) != 0){ + $nextInt = 0; + } + + for($i = 0; $i < $nextInt; ++$i){ + $randX = ($x * 16) + $this->rand->nextInt(16); + $randY = $this->rand->nextInt($this->rand->nextInt(120) + 8); + $randZ = ($z * 16) + $this->rand->nextInt(16); + + $i2 = 1; + if($this->rand->nextInt(4) == 0){ + $this->generate3($level, $xCenter, $zCenter, $randX, $randY, $randZ); + $i2 = 1 + $this->rand->nextInt(4); + } + + for($i3 = 0; $i3 < $i2; ++$i3){ + //generate(xCenter, zCenter, tileIdArray, randX, randY, randZ, (this.rand.nextFloat() * 2.0f) + this.rand.nextFloat(), this.rand.nextFloat() * 3.1415927f * 2.0f, ((this.rand.nextFloat() - 0.5f) * 2.0f) / 8.0f, 0, 0, 1.0d); + $this->generate4($level, $xCenter, $zCenter, $randX, $randY, $randZ, ($this->rand->nextFloat() * 2) + $this->rand->nextFloat(), $this->rand->nextFloat() * M_PI * 2, (($this->rand->nextFloat() - 0.5) * 2) / 8, 0, 0, 1); + } + } + } + + public function generate3(Level $level, $xCenter, $zCenter, $x, $y, $z){ + //generate(xCenter, zCenter, tileIdArray, x, y, z, 1.0f + (this.rand.nextFloat() * 6.0f), 0.0f, 0.0f, -1, -1, 0.5d); gen4 + $this->generate4($level, $xCenter, $zCenter, $x, $y, $z, 1 + ($this->rand->nextFloat() * 6), 0, 0, -1, -1, 0.5); + } + + public function generate4(Level $level, $xCenter, $zCenter, $x, $y, $z, $randFloat, $f1, $f2, $k, $i1, $d3){ + $d = $xCenter*16 + 8; + $d2 = $zCenter*16 + 8; + $f = $f3 = 0; + $random = new Random($this->rand->nextInt()); + + if($i1 <= 0){ + $i = ($this->range * 16) - 16; + $i1 = $i - $random->nextInt((int)($i / 4)); + } + $z2 = false; + if($k == -1){ + $k = $i1 / 2; + $z2 = true; + } + + $nextInt = $random->nextInt((int)($i1 / 2)); + $z3 = $random->nextInt(6) == 0; + while($k < $i1){ + $sin = 1.5 + (sin(($k * M_PI) / $i1) * $randFloat * 1); + $d4 = $sin * $d3; + $cos = cos($f2); + $x += cos($f1) * $cos; + $y += sin($f2); + $z += sin($f1) * $cos; + + $f22 = $z3 ? ($f2 * 0.92) : ($f2 * 0.7); + $f2 = $f22 + ($f3 * 0.1); + $f1 += $f * 0.1; + $f3 = ($f3 * 0.9) + (($random->nextFloat() - $random->nextFloat()) * $random->nextFloat() * 2); + $f = ($f * 0.75) + (($random->nextFloat() - $random->nextFloat()) * $random->nextFloat() * 4); + if(!$z2 && $k == $nextInt && $randFloat > 1){ + $this->generate4($level, $xCenter, $zCenter, $x, $y, $z, ($random->nextFloat() * 0.5) + 0.5, $f1 - M_PI_2, $f2 / 3, $k, $i1, 1); + $this->generate4($level, $xCenter, $zCenter, $x, $y, $z, ($random->nextFloat() * 0.5) + 0.5, $f1 + M_PI_2, $f2 / 3, $k, $i1, 1); + return; + } + if($z2 || $random->nextInt(4) != 0){ + $d5 = $x - $d; + $d6 = $z - $d2; + $d7 = $i1 - $k; + $d8 = $randFloat + 2 + 16; + if((($d5 * $d5) + ($d6*$d6) - ($d7*$d7)) > $d8*$d8){ + return; + } + + if($x >= (($d - 16) - ($sin * 2)) && $z >= (($d2 - 16) - ($sin * 2)) && $x <= ($d + 16 + ($sin * 2)) && $z <= ($d2 + 16 + ($sin * 2))){ + $floor = (floor($x - $sin) - ($xCenter * 16)) - 1; + $floor2 = (floor($x + $sin) - ($xCenter * 16)) + 1; + $floor3 = floor($y - $d4) - 1; + $floor4 = floor($y + $d4) + 1; + $floor5 = (floor($z - $sin) - ($zCenter * 16)) - 1; + $floor6 = (floor($z + $sin) - ($zCenter * 16)) + 1; + + if($floor < 0) $floor = 0; + if($floor2 > 16) $floor2 = 16; + if($floor3 < 1) $floor3 = 1; + if($floor4 > 120) $floor4 = 120; + if($floor5 < 0) $floor5 = 0; + if($floor6 > 16) $floor6 = 16; + + $z4 = false; + for($i2 = $floor; !$z4 && $i2 < $floor2; ++$i2){ //x + for($i3 = $floor5; !$z4 && $i3 < $floor6; ++$i3){ //z + $i4 = $floor4 + 1; //y + while(!$z4 && $i4 >= ($floor3 - 1)){ + if($i4 >= 0 && $i4 < 128){ + $id = $level->level->getBlockID($this->curChunkX*16 + $x, $y, $this->curChunkZ*16 + $z); + $z4 = $id == WATER || $id == STILL_WATER; + if($i4 != $floor3 - 1 && $i2 != $floor && $i2 != $floor2 - 1 && $i3 != $floor5 && $i3 != $floor6 - 1){ + $i4 = $floor3; + } + } + --$i4; + } + } + } + if($z4){ + continue; + }else{ + for($i6 = $floor; $i6 < $floor2; ++$i6){ //x + $d9 = ((($i6 + ($xCenter * 16)) + 0.5) - $x) / $sin; + for($i7 = $floor5; $i7 < $floor6; ++$i7){ //z + $d10 = ((($i7 + ($zCenter * 16)) + 0.5) - $z) / $sin; + $i8 = ((($i6 * $i6) + $i7) * 128) + $floor4; + $z5 = false; + if(($d9 * $d9) + ($d10 * $d10) < 1){ + for($i9 = $floor4 - 1; $i9 >= $floor3; --$i9){ + $d11 = (($i9 + 0.5) - $y) / $d4; + if($d11 > -0.7 && ($d9*$d9)+($d11*$d11)+($d10*$d10) < 1){ + $b = $level->level->getBlockID($this->curChunkX*16 + $i6, $floor4, $this->curChunkZ*16 + $i7); + if($b == GRASS) $z5 = true; + if($b == STONE || $b == DIRT || $b == GRASS){ + if($i9 < 10){ + $level->level->setBlockID($this->curChunkX*16 + $i6, $floor4, $this->curChunkZ*16 + $i7, LAVA); + }else{ + //ConsoleAPI::info("place air at ".($this->curChunkX*16 + $i6).":$floor4:".($this->curChunkZ*16 + $i7)); + $level->level->setBlockID($this->curChunkX*16 + $i6, $floor4, $this->curChunkZ*16 + $i7, 0, true); + + if($z5){ + $belowID = $level->level->getBlockID($this->curChunkX*16 + $i6, $floor4 - 1, $this->curChunkZ*16 + $i7); + if($belowID == DIRT){ + ConsoleAPI::info("place grass at ".($this->curChunkX*16 + $i6).":$floor4:".($this->curChunkZ*16 + $i7)); + $level->level->setBlockID($this->curChunkX*16 + $i6, $floor4 - 1, $this->curChunkZ*16 + $i7, GRASS); + } + } + } + } + } + --$i8; + } + + } + } + } + if($z2) return; + } + } + } + } + + } +} + diff --git a/src/world/generator/populator/BiomeBasedTreePopulator.php b/src/world/generator/populator/BiomeBasedTreePopulator.php index 512f8fc6..5dc5dd31 100644 --- a/src/world/generator/populator/BiomeBasedTreePopulator.php +++ b/src/world/generator/populator/BiomeBasedTreePopulator.php @@ -12,9 +12,6 @@ class BiomeBasedTreePopulator extends \TreePopulator $biome = BiomeSelector::get($biomeID); $treeFeature = null; if($biome instanceof Biome){ - if($biome->id === BIOME_JUNGLE){ - console("JUNGLE AT $chunkX $chunkZ"); - } $treeFeature = $biome->getTree($random); } From 807662dcb003966e4c0d86582c79194f410cbf3b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 30 Nov 2023 07:08:42 +0300 Subject: [PATCH 22/43] fixed CaveGenerator, currently disabled in NormalGenerator --- src/pmf/PMFLevel.php | 8 +- src/utils/MTRandom.php | 56 +++++ src/utils/MersenneTwister.php | 17 +- src/utils/Random.php | 9 +- src/world/generator/ExperimentalGenerator.php | 6 +- src/world/generator/NormalGenerator.php | 4 +- src/world/generator/cave/CaveGenerator.php | 215 +++++++++--------- 7 files changed, 188 insertions(+), 127 deletions(-) create mode 100644 src/utils/MTRandom.php diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 285a7d97..2bb035d0 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -600,17 +600,15 @@ class PMFLevel extends PMF{ $Z = $z >> 4; $Y = $y >> 4; $block &= 0xFF; - - $index = $this->getIndex($X, $Z); $index = $this->getIndex($X, $Z); if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){ if($this->loadChunk($X, $Z, false) === false){ $this->createUnpopulatedChunk($X, $Z); } } - //if($this->chunks[$index][$Y] === false){ - // $this->fillMiniChunk($X, $Z, $Y); - //} + if($this->chunks[$index][$Y] === false){ + $this->fillMiniChunk($X, $Z, $Y); + } $aX = $x & 0xf; $aZ = $z & 0xf; diff --git a/src/utils/MTRandom.php b/src/utils/MTRandom.php new file mode 100644 index 00000000..5cc5d5c3 --- /dev/null +++ b/src/utils/MTRandom.php @@ -0,0 +1,56 @@ +setSeed($seed == null ? (int)microtime(1) : $seed); + } + + public function setSeed($seed){ + $this->mt[0] = $seed & 0xffffffff; + for($this->index = 1; $this->index < 624; ++$this->index){ + $this->mt[$this->index] = (0x6c078965 * ($this->mt[$this->index-1] >> 30 ^ $this->mt[$this->index - 1]) + $this->index) & 0xffffffff; + } + } + + public function genRandInt(){ + if($this->index >= 624 || $this->index < 0){ + if($this->index >= 625 || $this->index < 0) $this->setSeed(4357); + + for($kk = 0; $kk < 227; ++$kk){ + $y = ($this->mt[$kk] & 0x80000000) | ($this->mt[$kk + 1] & 0x7fffffff); + $this->mt[$kk] = $this->mt[$kk+397] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; + } + + for(;$kk < 623; ++$kk){ + $y = ($this->mt[$kk] & 0x80000000) | ($this->mt[$kk + 1] & 0x7fffffff); + $this->mt[$kk] = $this->mt[$kk-227] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; + } + + $y = ($this->mt[623] & 0x80000000) | ($this->mt[0] & 0x7fffffff); + $this->mt[623] = $this->mt[396] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; + $this->index = 0; + } + + $y = $this->mt[$this->index++]; + $y ^= ($y >> 11); + $y ^= ($y << 7) & 0x9d2c5680; + $y ^= ($y << 15) & 0xefc60000; + $y ^= ($y >> 18); + return $y; + } + + public function nextInt($bound = null){ + return $bound == null ? $this->genRandInt() >> 1 : $this->genRandInt() % $bound; + } + + public function nextFloat(){ + return $this->genRandInt() / 0xffffffff; + } +} + diff --git a/src/utils/MersenneTwister.php b/src/utils/MersenneTwister.php index 5bb7098c..35742daa 100644 --- a/src/utils/MersenneTwister.php +++ b/src/utils/MersenneTwister.php @@ -44,29 +44,22 @@ define("MASK32", $val); class MersenneTwister{ - - + public $mt; + public $mti = 0; static $MAG_01 = [0, MATRIX_A]; - const N = N; //the class constant is not used anywhere in this namespace, //but it makes the API cleaner. //^^ u probably wanted to say makes code look weirder - function __construct(){ - $this->bits32 = PHP_INT_MAX == 2147483647; - - if(func_num_args() == 1){ - $this->init_with_integer(func_get_arg(0)); - } + public function __construct($seed = null){ + $this->init_with_integer($seed == null ? ((int)microtime(1) & 0xffffffff) : $seed); } function init_with_integer($integer_seed){ - $integer_seed = force_32_bit_int($integer_seed); - $this->mt = array_fill(0, N, 0); $this->mt[0] = $integer_seed; - for($this->mti = 1; $this->mti < N; ++$this->mti){ + for($this->mti = 1; $this->mti < 624; ++$this->mti){ $this->mt[$this->mti] = add_2(mul(1812433253, ($this->mt[$this->mti - 1] ^ (($this->mt[$this->mti - 1] >> 30) & 3))), $this->mti); } } diff --git a/src/utils/Random.php b/src/utils/Random.php index 510b1bbd..595b4eaf 100644 --- a/src/utils/Random.php +++ b/src/utils/Random.php @@ -49,7 +49,14 @@ class Random{ public function nextFloat(){ return $this->nextInt() / 0x7FFFFFFF; } - + + public function nextLong($n = -1){ + if($n > 0){ + return Utils::readLong($this->nextBytes(8)) & 0x7FFFFFFF % $n; + } + return Utils::readLong($this->nextBytes(8)) & 0x7FFFFFFF; + } + public function nextInt($n = -1){ if($n > 0){ return Utils::readInt($this->nextBytes(4)) & 0x7FFFFFFF % $n; diff --git a/src/world/generator/ExperimentalGenerator.php b/src/world/generator/ExperimentalGenerator.php index dc419eb4..903e0a7d 100644 --- a/src/world/generator/ExperimentalGenerator.php +++ b/src/world/generator/ExperimentalGenerator.php @@ -105,7 +105,6 @@ class ExperimentalGenerator implements NewLevelGenerator{ $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->level->getSeed()); $noiseArray = ExperimentalGenerator::getFastNoise3D($this->noiseBase, 16, 128, 16, 4, 8, 4, $chunkX * 16, 0, $chunkZ * 16); $biomeCache = []; - $t = microtime(1); for($chunkY = 0; $chunkY < 8; ++$chunkY){ $chunk = ""; $startY = $chunkY << 4; @@ -162,13 +161,12 @@ class ExperimentalGenerator implements NewLevelGenerator{ } $this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk); } - $t = microtime(1) - $t; - console("gen:".$t); - foreach($this->genPopulators as $pop){ $pop->populate($this->level, $chunkX, $chunkZ, $this->random); } + + $this->caveGenerator->generate($this->level, $chunkX, $chunkZ); } public function populateChunk($chunkX, $chunkZ){ diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index 3c231f0e..cdb72170 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -152,9 +152,7 @@ class NormalGenerator implements NewLevelGenerator{ $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, $biomes); $this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk); } - //$this->caveGenerator->curChunkX = $chunkX; - //$this->caveGenerator->curChunkZ = $chunkZ; - //$this->caveGenerator->generate($this->level, $chunkX, $chunkZ); + //$this->caveGenerator->generate($this->level, $chunkX, $chunkZ); TODO speedup } public function populateChunk($chunkX, $chunkZ){ diff --git a/src/world/generator/cave/CaveGenerator.php b/src/world/generator/cave/CaveGenerator.php index cbf62f8d..a4c66564 100644 --- a/src/world/generator/cave/CaveGenerator.php +++ b/src/world/generator/cave/CaveGenerator.php @@ -4,174 +4,185 @@ class CaveGenerator { public $range = 8; public $rand; - public $curChunkX, $curChunkZ; + public function __construct($seed){ - $this->rand = new Random($seed); + $this->rand = new MTRandom($seed); } - public function generate(Level $level, $xCenter, $zCenter){ - $i = $this->range; + public function generate(Level $level, $chunkX, $chunkZ){ + $range = $this->range; $this->rand->setSeed($level->getSeed()); - $seedInt1 = ((int)($this->rand->nextInt() / 2) * 2) + 1; - $seedInt2 = ((int)($this->rand->nextInt() / 2) * 2) + 1; + $seedInt1 = $this->rand->nextInt(); + $seedInt2 = $this->rand->nextInt(); - for($x = $xCenter - $i; $x <= $xCenter + $i; ++$x){ - for($z = $zCenter - $i; $z <= $zCenter + $i; ++$z){ + for($x = $chunkX - $range; $x <= $chunkX + $range; ++$x){ + for($z = $chunkZ - $range; $z <= $chunkZ + $range; ++$z){ $this->rand->setSeed((($x * $seedInt1) + ($z * $seedInt2)) ^ $level->getSeed()); - $this->generate2($level, $x, $z, $xCenter, $zCenter); + $this->recursiveGenerate($level, $x, $z, $chunkX, $chunkZ); } } } - public function generate2(Level $level, $x, $z, $xCenter, $zCenter){ + public function recursiveGenerate(Level $level, $chunkXoffsetted, $chunkZoffsetted, $chunkX, $chunkZ){ $nextInt = $this->rand->nextInt($this->rand->nextInt($this->rand->nextInt(40) + 1) + 1); if($this->rand->nextInt(15) != 0){ $nextInt = 0; } for($i = 0; $i < $nextInt; ++$i){ - $randX = ($x * 16) + $this->rand->nextInt(16); + $randX = ($chunkXoffsetted * 16) + $this->rand->nextInt(16); $randY = $this->rand->nextInt($this->rand->nextInt(120) + 8); - $randZ = ($z * 16) + $this->rand->nextInt(16); + $randZ = ($chunkZoffsetted * 16) + $this->rand->nextInt(16); $i2 = 1; if($this->rand->nextInt(4) == 0){ - $this->generate3($level, $xCenter, $zCenter, $randX, $randY, $randZ); + $this->generateLargeCaveNode($level, $chunkX, $chunkZ, $randX, $randY, $randZ); $i2 = 1 + $this->rand->nextInt(4); } for($i3 = 0; $i3 < $i2; ++$i3){ - //generate(xCenter, zCenter, tileIdArray, randX, randY, randZ, (this.rand.nextFloat() * 2.0f) + this.rand.nextFloat(), this.rand.nextFloat() * 3.1415927f * 2.0f, ((this.rand.nextFloat() - 0.5f) * 2.0f) / 8.0f, 0, 0, 1.0d); - $this->generate4($level, $xCenter, $zCenter, $randX, $randY, $randZ, ($this->rand->nextFloat() * 2) + $this->rand->nextFloat(), $this->rand->nextFloat() * M_PI * 2, (($this->rand->nextFloat() - 0.5) * 2) / 8, 0, 0, 1); + + $v17 = $this->rand->nextFloat() * M_PI * 2; + $v18 = ($this->rand->nextFloat() - 0.5) * 2 / 8; + $v19 = $this->rand->nextFloat() * 2 + $this->rand->nextFloat(); + if($this->rand->nextInt(10) == 0) { + $v19 *= $this->rand->nextFloat() * $this->rand->nextFloat() * 3 + 1; + } + + + $this->generateCaveNode($level, $chunkX, $chunkZ, $randX, $randY, $randZ, $v19, $v17, $v18, 0, 0, 1); } } } - public function generate3(Level $level, $xCenter, $zCenter, $x, $y, $z){ + public function generateLargeCaveNode(Level $level, $xCenter, $zCenter, $x, $y, $z){ //generate(xCenter, zCenter, tileIdArray, x, y, z, 1.0f + (this.rand.nextFloat() * 6.0f), 0.0f, 0.0f, -1, -1, 0.5d); gen4 - $this->generate4($level, $xCenter, $zCenter, $x, $y, $z, 1 + ($this->rand->nextFloat() * 6), 0, 0, -1, -1, 0.5); + $this->generateCaveNode($level, $xCenter, $zCenter, $x, $y, $z, 1 + ($this->rand->nextFloat() * 6), 0, 0, -1, -1, 0.5); } - public function generate4(Level $level, $xCenter, $zCenter, $x, $y, $z, $randFloat, $f1, $f2, $k, $i1, $d3){ - $d = $xCenter*16 + 8; - $d2 = $zCenter*16 + 8; + public function generateCaveNode(Level $level, $chunkX, $chunkZ, $x, $y, $z, $randFloat, $f1, $f2, $unk_1, $unk_2, $d3){ + $chunkCenterX = $chunkX*16 + 8; + $chunkCenterZ = $chunkZ*16 + 8; $f = $f3 = 0; - $random = new Random($this->rand->nextInt()); + $random = new MTRandom($this->rand->nextInt()); - if($i1 <= 0){ + if($unk_2 <= 0){ $i = ($this->range * 16) - 16; - $i1 = $i - $random->nextInt((int)($i / 4)); + $unk_2 = $i - $random->nextInt((int)($i / 4)); } $z2 = false; - if($k == -1){ - $k = $i1 / 2; + if($unk_1 == -1){ + $unk_1 = (int)($unk_2 / 2); $z2 = true; } - $nextInt = $random->nextInt((int)($i1 / 2)); - $z3 = $random->nextInt(6) == 0; - while($k < $i1){ - $sin = 1.5 + (sin(($k * M_PI) / $i1) * $randFloat * 1); - $d4 = $sin * $d3; - $cos = cos($f2); - $x += cos($f1) * $cos; - $y += sin($f2); - $z += sin($f1) * $cos; + $var27 = $random->nextInt((int)($unk_2 / 2)) + ($unk_2 / 4); + for($var28 = ($random->nextInt(6) == 0); $unk_1 < $unk_2; ++$unk_1){ + $var29 = 1.5 + sin($unk_1 * M_PI / $unk_2) * $randFloat /* * 1 */; + $var31 = $var29 * $d3; + $var33 = cos($f2); + $var34 = sin($f2); + $x += cos($f1) * $var33; + $y += $var34; + $z += sin($f1) * $var33; - $f22 = $z3 ? ($f2 * 0.92) : ($f2 * 0.7); - $f2 = $f22 + ($f3 * 0.1); + $f2 = $var28 ? ($f2 * 0.92) : ($f2 * 0.7); + + $f2 += $f3 * 0.1; $f1 += $f * 0.1; - $f3 = ($f3 * 0.9) + (($random->nextFloat() - $random->nextFloat()) * $random->nextFloat() * 2); - $f = ($f * 0.75) + (($random->nextFloat() - $random->nextFloat()) * $random->nextFloat() * 4); - if(!$z2 && $k == $nextInt && $randFloat > 1){ - $this->generate4($level, $xCenter, $zCenter, $x, $y, $z, ($random->nextFloat() * 0.5) + 0.5, $f1 - M_PI_2, $f2 / 3, $k, $i1, 1); - $this->generate4($level, $xCenter, $zCenter, $x, $y, $z, ($random->nextFloat() * 0.5) + 0.5, $f1 + M_PI_2, $f2 / 3, $k, $i1, 1); + + $f3 *= 0.9; + $f *= 0.75; + $f3 += ($random->nextFloat() - $random->nextFloat()) * $random->nextFloat() * 2; + $f += ($random->nextFloat() - $random->nextFloat()) * $random->nextFloat() * 2; + + if(!$z2 && $unk_1 == $var27 && $randFloat > 1 && $unk_2 > 0){ + //this.generateCaveNode(random.nextLong(), chunkX, chunkZ, par5ArrayOfByte, x, y, z, random.nextFloat() * 0.5F + 0.5F, f1 - ((float)Math.PI / 2F), f2 / 3.0F, unk_1, unk_2, 1.0D); + //this.generateCaveNode(random.nextLong(), chunkX, chunkZ, par5ArrayOfByte, x, y, z, random.nextFloat() * 0.5F + 0.5F, f1 + ((float)Math.PI / 2F), f2 / 3.0F, unk_1, unk_2, 1.0D); + $this->generateCaveNode($level, $chunkX, $chunkZ, $x, $y, $z, $random->nextFloat() * 0.5 + 0.5, $f1 - (M_PI / 2), $f2 / 3, $unk_1, $unk_2, 1); + $this->generateCaveNode($level, $chunkX, $chunkZ, $x, $y, $z, $random->nextFloat() * 0.5 + 0.5, $f1 + (M_PI / 2), $f2 / 3, $unk_1, $unk_2, 1); return; } + if($z2 || $random->nextInt(4) != 0){ - $d5 = $x - $d; - $d6 = $z - $d2; - $d7 = $i1 - $k; - $d8 = $randFloat + 2 + 16; - if((($d5 * $d5) + ($d6*$d6) - ($d7*$d7)) > $d8*$d8){ + $distFromCenterX = $x - $chunkCenterX; + $distFromCenterZ = $z - $chunkCenterZ; + + $var39 = ($unk_2 - $unk_1); + $var41 = $randFloat + /*2 + 16*/ 18; + + if($distFromCenterX*$distFromCenterX + $distFromCenterZ*$distFromCenterZ - $var39*$var39 > $var41*$var41){ return; } - - if($x >= (($d - 16) - ($sin * 2)) && $z >= (($d2 - 16) - ($sin * 2)) && $x <= ($d + 16 + ($sin * 2)) && $z <= ($d2 + 16 + ($sin * 2))){ - $floor = (floor($x - $sin) - ($xCenter * 16)) - 1; - $floor2 = (floor($x + $sin) - ($xCenter * 16)) + 1; - $floor3 = floor($y - $d4) - 1; - $floor4 = floor($y + $d4) + 1; - $floor5 = (floor($z - $sin) - ($zCenter * 16)) - 1; - $floor6 = (floor($z + $sin) - ($zCenter * 16)) + 1; + //if (x >= chunkCenterX - 16.0D - var29 * 2.0D && z >= chunkCenterZ - 16.0D - var29 * 2.0D && x <= chunkCenterX + 16.0D + var29 * 2.0D && z <= chunkCenterZ + 16.0D + var29 * 2.0D) + if($x >= $chunkCenterX - 16 - $var29*2 && $z >= $chunkCenterZ - 16 - $var29*2 && $x <= $chunkCenterX + 16 + $var29*2 && $z <= $chunkCenterZ + 16 + $var29*2){ + $minX = floor($x - $var29) - $chunkX*16 - 1; + $maxX = floor($x + $var29) - $chunkX*16 + 1; + $minY = floor($y - $var31) - 1; + $maxY = floor($y + $var31) + 1; + $minZ = floor($z - $var29) - $chunkZ*16 - 1; + $maxZ = floor($z + $var29) - $chunkZ*16 + 1; - if($floor < 0) $floor = 0; - if($floor2 > 16) $floor2 = 16; - if($floor3 < 1) $floor3 = 1; - if($floor4 > 120) $floor4 = 120; - if($floor5 < 0) $floor5 = 0; - if($floor6 > 16) $floor6 = 16; + if($minX < 0) $minX = 0; + if($maxX > 16) $maxX = 16; - $z4 = false; - for($i2 = $floor; !$z4 && $i2 < $floor2; ++$i2){ //x - for($i3 = $floor5; !$z4 && $i3 < $floor6; ++$i3){ //z - $i4 = $floor4 + 1; //y - while(!$z4 && $i4 >= ($floor3 - 1)){ - if($i4 >= 0 && $i4 < 128){ - $id = $level->level->getBlockID($this->curChunkX*16 + $x, $y, $this->curChunkZ*16 + $z); - $z4 = $id == WATER || $id == STILL_WATER; - if($i4 != $floor3 - 1 && $i2 != $floor && $i2 != $floor2 - 1 && $i3 != $floor5 && $i3 != $floor6 - 1){ - $i4 = $floor3; + if($minY < 1) $minY = 1; + if($maxY > 120) $maxY = 120; + + if($minZ < 0) $minZ = 0; + if($maxZ > 16) $maxZ = 16; + + $hasWater = false; + for($blockX = $minX; !$hasWater && $blockX < $maxX; ++$blockX){ + for($blockZ = $minZ; !$hasWater && $blockZ < $maxZ; ++$blockZ){ + for($blockY = $maxY + 1; !$hasWater && $blockY >= $minY - 1; --$blockY){ + if($blockY >= 0 && $blockY < 128){ + $id = $level->level->getBlockID($blockX + $chunkX*16, $blockY, $blockZ + $chunkZ * 16); + $hasWater = $id == WATER || $id == STILL_WATER; + //if (blockY != minY - 1 && blockX != minX && blockX != maxX - 1 && blockZ != minZ && blockZ != maxZ - 1) + if($blockY != $minY - 1 && $blockX != $minX && $blockX != $maxX - 1 && $blockZ != $minZ && $blockZ != $maxZ - 1){ + $blockY = $minY; } } - --$i4; } } } - if($z4){ - continue; - }else{ - for($i6 = $floor; $i6 < $floor2; ++$i6){ //x - $d9 = ((($i6 + ($xCenter * 16)) + 0.5) - $x) / $sin; - for($i7 = $floor5; $i7 < $floor6; ++$i7){ //z - $d10 = ((($i7 + ($zCenter * 16)) + 0.5) - $z) / $sin; - $i8 = ((($i6 * $i6) + $i7) * 128) + $floor4; - $z5 = false; - if(($d9 * $d9) + ($d10 * $d10) < 1){ - for($i9 = $floor4 - 1; $i9 >= $floor3; --$i9){ - $d11 = (($i9 + 0.5) - $y) / $d4; - if($d11 > -0.7 && ($d9*$d9)+($d11*$d11)+($d10*$d10) < 1){ - $b = $level->level->getBlockID($this->curChunkX*16 + $i6, $floor4, $this->curChunkZ*16 + $i7); - if($b == GRASS) $z5 = true; - if($b == STONE || $b == DIRT || $b == GRASS){ - if($i9 < 10){ - $level->level->setBlockID($this->curChunkX*16 + $i6, $floor4, $this->curChunkZ*16 + $i7, LAVA); + + if(!$hasWater){ + for($blockX = $minX; $blockX < $maxX; ++$blockX){ + $var59 = (($blockX + $chunkX*16) + 0.5 - $x) / $var29; + for($blockZ = $minZ; $blockZ < $maxZ; ++$blockZ){ + $var46 = (($blockZ + $chunkZ*16) + 0.5 - $z) / $var29; + $yPosition = $maxY; + $hasGrass = false; + + if($var59*$var59 + $var46*$var46 < 1){ + for($blockY = $maxY - 1; $blockY >= $minY; --$blockY){ + $var51 = ($blockY + 0.5 - $y) / $var31; + if($var51 > -0.7 && $var59*$var59 + $var51*$var51 + $var46*$var46 < 1){ + $blockID = $level->level->getBlockID($blockX + $chunkX*16, $yPosition, $blockZ + $chunkZ * 16); + $hasGrass = $blockID == GRASS; + if($blockID == STONE || $blockID == DIRT || $hasGrass){ + if($blockY < 10){ + $level->level->setBlockID($blockX + $chunkX*16, $yPosition, $blockZ + $chunkZ * 16, LAVA); }else{ - //ConsoleAPI::info("place air at ".($this->curChunkX*16 + $i6).":$floor4:".($this->curChunkZ*16 + $i7)); - $level->level->setBlockID($this->curChunkX*16 + $i6, $floor4, $this->curChunkZ*16 + $i7, 0, true); - - if($z5){ - $belowID = $level->level->getBlockID($this->curChunkX*16 + $i6, $floor4 - 1, $this->curChunkZ*16 + $i7); - if($belowID == DIRT){ - ConsoleAPI::info("place grass at ".($this->curChunkX*16 + $i6).":$floor4:".($this->curChunkZ*16 + $i7)); - $level->level->setBlockID($this->curChunkX*16 + $i6, $floor4 - 1, $this->curChunkZ*16 + $i7, GRASS); - } + $level->level->setBlockID($blockX + $chunkX*16, $yPosition, $blockZ + $chunkZ * 16, 0); + if($hasGrass && $level->level->getBlockID($blockX + $chunkX*16, $yPosition - 1, $blockZ + $chunkZ * 16) == DIRT){ + //ConsoleAPI::debug("place grass at ".($blockX + $chunkX*16).":".($yPosition - 1).":".($blockZ + $chunkZ * 16)); + $level->level->setBlockID($blockX + $chunkX*16, $yPosition - 1, $blockZ + $chunkZ * 16, GRASS); } } } } - --$i8; + --$yPosition; } - } } } - if($z2) return; } } } } - } } From e9a3ba165d5729cf63992598c88987f0c80faaf1 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 30 Nov 2023 07:11:47 +0300 Subject: [PATCH 23/43] experimental generator should work --- src/world/generator/ExperimentalGenerator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/world/generator/ExperimentalGenerator.php b/src/world/generator/ExperimentalGenerator.php index 903e0a7d..35c1f5b6 100644 --- a/src/world/generator/ExperimentalGenerator.php +++ b/src/world/generator/ExperimentalGenerator.php @@ -85,6 +85,7 @@ class ExperimentalGenerator implements NewLevelGenerator{ $tallGrass->setBaseAmount(5); $tallGrass->setRandomAmount(0); $this->populators[] = $tallGrass; + $this->caveGenerator = new CaveGenerator($this->level->getSeed()); } public function pickBiome(int $x, int $z){ From 648784b4ff66b9289f0a09ad562de90ceefabfb0 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 30 Nov 2023 07:38:03 +0300 Subject: [PATCH 24/43] ported rail logic from 0.8 --- src/constants/GeneralConstants.php | 10 +- .../block/nonfull/PoweredRailBlock.php | 28 +- src/material/block/nonfull/RailBaseBlock.php | 56 ++++ src/material/block/nonfull/RailBlock.php | 174 +----------- src/material/item/generic/MinecartItem.php | 8 +- src/utils/RailLogic.php | 248 ++++++++++++++++++ src/world/Level.php | 14 + 7 files changed, 346 insertions(+), 192 deletions(-) create mode 100644 src/material/block/nonfull/RailBaseBlock.php create mode 100644 src/utils/RailLogic.php diff --git a/src/constants/GeneralConstants.php b/src/constants/GeneralConstants.php index 677d2e6e..2adefe81 100644 --- a/src/constants/GeneralConstants.php +++ b/src/constants/GeneralConstants.php @@ -22,4 +22,12 @@ define("BLOCK_UPDATE_NORMAL", 1); define("BLOCK_UPDATE_RANDOM", 2); define("BLOCK_UPDATE_SCHEDULED", 3); define("BLOCK_UPDATE_WEAK", 4); -define("BLOCK_UPDATE_TOUCH", 5); \ No newline at end of file +define("BLOCK_UPDATE_TOUCH", 5); + +//Block Sides +define("SIDE_DOWN", 0); +define("SIDE_UP", 1); +define("SIDE_ZNEG", 2); +define("SIDE_ZPOS", 3); +define("SIDE_XNEG", 4); +define("SIDE_XPOS", 5); \ No newline at end of file diff --git a/src/material/block/nonfull/PoweredRailBlock.php b/src/material/block/nonfull/PoweredRailBlock.php index 68879a65..c9e7a74b 100644 --- a/src/material/block/nonfull/PoweredRailBlock.php +++ b/src/material/block/nonfull/PoweredRailBlock.php @@ -1,31 +1,11 @@ hardness = 0.7; - $this->isFullBlock = false; - $this->isSolid = true; - } - - public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $down = $this->getSide(0); - if($down->getID() !== AIR and $down instanceof SolidBlock){ - $this->level->setBlock($block, $this, true, false, true); - return true; - } - return false; - } - - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() === AIR){ //Replace with common break method - ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, $this->meta, 1)); - $this->level->setBlock($this, new AirBlock(), true, false, true); - return BLOCK_UPDATE_NORMAL; - } - } - return false; + $this->isFullBlock = false; + $this->isSolid = false; } } \ No newline at end of file diff --git a/src/material/block/nonfull/RailBaseBlock.php b/src/material/block/nonfull/RailBaseBlock.php new file mode 100644 index 00000000..beb2c9c0 --- /dev/null +++ b/src/material/block/nonfull/RailBaseBlock.php @@ -0,0 +1,56 @@ +level->getBlockID($x, $y, $z); + return $id === POWERED_RAIL || $id === RAIL; + } + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ + $down = $this->getSide(0); + if($down->getID() !== AIR and $down instanceof SolidBlock){ + + $this->level->setBlock($block, $this, true, false, true); + $logic = new RailLogic($this); + $logic->place(false, true); + return true; + } + return false; + } + + public function updateState(){} + + public function onUpdate($type){ + if($type === BLOCK_UPDATE_NORMAL){ + + $meta = $this->getMetadata(); + if($this->id === POWERED_RAIL){ + $meta &= 7; + } + $x = $this->x; + $y = $this->y; + $z = $this->z; + if( + ($this->level->level->getBlockID($x, $y - 1, $z) == 0) || + (($meta == 2) && $this->level->level->getBlockID($x + 1, $y, $z) == 0) || + (($meta == 3) && $this->level->level->getBlockID($x - 1, $y, $z) == 0) || + (($meta == 4) && $this->level->level->getBlockID($x, $y, $z - 1) == 0) || + (($meta == 5) && $this->level->level->getBlockID($x, $y, $z + 1) == 0) + ){ + $this->level->setBlock($this, new AirBlock(), true, false, true); + ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, $this->meta, 1)); + }else{ + $this->updateState(); + } + + + //if($this->getSide(0)->getID() === AIR){//Replace with common break method + // ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, $this->meta, 1)); + // $this->level->setBlock($this, new AirBlock(), true, false, true); + //} + + } + + } +} diff --git a/src/material/block/nonfull/RailBlock.php b/src/material/block/nonfull/RailBlock.php index 5afb2c71..67bdc276 100644 --- a/src/material/block/nonfull/RailBlock.php +++ b/src/material/block/nonfull/RailBlock.php @@ -1,173 +1,21 @@ hardness = 0.7; - $this->isFullBlock = false; + $this->isFullBlock = false; $this->isSolid = false; } - public static $shouldconnectrails = false; - - private function isRailBlock($id){ - return $id === RAIL || $id === POWERED_RAIL; - } - - private function isValidRailMeta($meta){ - return !($meta < 0 || $meta > 10); - } - - private function canConnectRail($block) { - if(!($block instanceof RailBlock)) return null; - if($this->distanceSquared($block) > 2) return null; - $result = $this->checkRail($this); - if(count($result) === 2) return null; - return $result; - } - - private function checkRail($rail){//fixed - if(!($rail instanceof RailBlock)) return null; - $damage = $rail->meta; - if ($damage < 0 || $damage > 10) return null; - $delta = array( //TODO make static - array(array(0, 1), array(0, -1)), - array(array(1, 0), array(-1, 0)), - array(array(1, 0), array(-1, 0)), - array(array(1, 0), array(-1, 0)), - - array(array(0, 1), array(0, -1)), - array(array(0, 1), array(0, -1)), - array(array(1, 0), array(0, 1)), - array(array(0, 1), array(-1, 0)), - - array(array(-1, 0), array(0, -1)), - array(array(0, -1), array(1, 0)) - ); - $deltaY = array(0, 1, -1); - $blocks = $delta[$damage]; - $connected = array(); - - foreach($deltaY as $y){ - $v3 = new Vector3( - $rail->getFloorX() + $blocks[0][0], - $rail->getFloorY() + $y, - $rail->getFloorZ() + $blocks[0][1] - ); - $railBlock = $rail->level->getBlock($v3); - $idToConnect = $railBlock->getID(); - $metaToConnect = $railBlock->getMetadata(); - if (!$this->isRailBlock($idToConnect) || !$this->isValidRailMeta($metaToConnect)) continue; - $xDiff = $this->getFloorX() - $v3->getFloorX(); - $zDiff = $this->getFloorZ() - $v3->getFloorZ(); - foreach($blocks as $xz) { - if($xz[0] !== $xDiff || $xz[1] !== $zDiff) continue; - $connected[] = $v3; - } - } - foreach($deltaY as $y){ - $v3 = new Vector3( - $rail->getFloorX() + $blocks[1][0], - $rail->getFloorY() + $y, - $rail->getFloorZ() + $blocks[1][1] - ); - $railBlock = $rail->level->getBlock($v3); - $idToConnect = $railBlock->getID(); - $metaToConnect = $railBlock->getMetadata(); - if(!$this->isRailBlock($idToConnect) || !$this->isValidRailMeta($metaToConnect)) continue; - $xDiff = $this->getFloorX() - $v3->getFloorX(); - $zDiff = $this->getFloorZ() - $v3->getFloorZ(); - foreach($blocks as $xz){ - if ($xz[0] !== $xDiff || $xz[1] !== $zDiff) continue; - $connected[] = $v3; - } - } - return $connected; - } - - private function connectRail($rail){ - $connected = $this->canConnectRail($rail); - if ($connected === null || count($connected) === 0) return; - if(count($connected) === 1){ - $v3 = $connected[0]->subtract($this); - $this->meta = ($v3->y !== 1) ? ($v3->x === 0 ? 0 : 1) : (int) ($v3->z === 0 ? ($v3->x / -2) + 2.5 : ($v3->z / 2) + 4.5); - }elseif(count(connected) === 2){ - $subtract = array(new Vector3(0, 0, 0),new Vector3(0, 0, 0)); - for ($i = 0; $i < count($connected); $i++) { - $subtract[$i] = $connected[$i]->subtract($this); - } - if (abs($subtract[0]->x) === abs($subtract[1]->z) && abs($subtract[1]->x) === abs($subtract[0]->z)){ - $v3 = $connected[0]->subtract($this)->add($connected[1]->subtract($this)); - $this->meta = $v3->x === 1 ? ($v3->z === 1 ? 6 : 9) : ($v3->z === 1 ? 7 : 8); - }elseif ($subtract[0]->y === 1 || $subtract[1]->y === 1){ - $v3 = $subtract[0]->y === 1 ? $subtract[0] : $subtract[1]; - $this->meta = $v3->x === 0 ? ($v3->z === -1 ? 4 : 5) : ($v3->x === 1 ? 2 : 3); - }else{ - $this->meta = $subtract[0]->x === 0 ? 0 : 1; - } - } - $this->level->setBlock($rail, new RailBlock($this->meta), true, true); - } - - public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $down = $this->getSide(0); - if($down->getID() !== AIR and $down instanceof SolidBlock){ - //Rail connection(ported from nukkit) - if(RailBlock::$shouldconnectrails){ - $arrayXZ = array(array(1, 0),array(0, 1),array(-1, 0),array(0, -1)); - $arrayY = array(0, 1, -1); - $connected = array(); - foreach($arrayXZ as $xz) { - $x = $xz[0]; - $z = $xz[1]; - foreach($arrayY as $y){ - $v3c = new Vector3($x, $y, $z); - $v3 = $v3c->add($this); - $v3block = $this->level->getBlock($v3); - if ($v3block === null) continue; - if (!$this->isRailBlock($v3block->id) || !$this->isValidRailMeta($v3block->meta)) continue; - if (!($v3block instanceof RailBlock)) continue; - $this->connectRail($v3block); - array_push($connected, $v3block); - } - if(count($connected) >= 2) break; - } - - if(count($connected) === 1) { - $v3 = $connected[0]->subtract($this); - $this->meta = ($v3->y !== 1) ? ($v3->x === 0 ? 0 : 1) : (int) ($v3->z === 0 ? ($v3->x / -2) + 2.5 : ($v3->z / 2) + 4.5); - }elseif(count($connected) === 2){ - $subtract = array(new Vector3(0, 0, 0),new Vector3(0, 0, 0)); - for($i = 0; $i < count($connected); $i++){ - $subtract[$i] = $connected[$i]->subtract($this); - } - if(abs($subtract[0]->x) === abs($subtract[1]->z) && abs($subtract[1]->x) === abs($subtract[0]->z)) { - $v3 = $connected[0]->subtract($this)->add($connected[1]->subtract($this)); - $this->meta = $v3->x === 1 ? ($v3->z === 1 ? 6 : 9) : ($v3->z === 1 ? 7 : 8); - }elseif($subtract[0]->y === 1 || $subtract[1]->y === 1) { - $v3 = $subtract[0]->y == 1 ? $subtract[0] : $subtract[1]; - $this->meta = $v3->x === 0 ? ($v3->z === -1 ? 4 : 5) : ($v3->x === 1 ? 2 : 3); - }else{ - $this->meta = $subtract[0]->x === 0 ? 0 : 1; - } - } - } - //End of Logic - $this->level->setBlock($block, $this, true, false, true); - return true; - } - return false; - } - - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() === AIR){//Replace with common break method - ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, $this->meta, 1)); - $this->level->setBlock($this, new AirBlock(), true, false, true); - return BLOCK_UPDATE_NORMAL; - } - } - return false; + public function updateState(){ + //$logic = (new RailLogic($this)); + //if($logic->countPotentialConnections() == 3){ + // $logic->place(false, false); + //} } } \ No newline at end of file diff --git a/src/material/item/generic/MinecartItem.php b/src/material/item/generic/MinecartItem.php index a82cee48..6d1f2cc0 100755 --- a/src/material/item/generic/MinecartItem.php +++ b/src/material/item/generic/MinecartItem.php @@ -12,10 +12,10 @@ class MinecartItem extends Item{ } $server = ServerAPI::request(); $data = [ - "x" => $target->getX(), - "y" => $target->getY() + 0.8, - "z" => $target->getZ(), - ]; + "x" => $target->getX() + 0.5, + "y" => $target->getY() + 0.8, + "z" => $target->getZ() + 0.5, + ]; $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_MINECART, $data); $server->api->entity->spawnToAll($e); if(($player->gamemode & 0x01) === 0x00){ diff --git a/src/utils/RailLogic.php b/src/utils/RailLogic.php new file mode 100644 index 00000000..63c191f3 --- /dev/null +++ b/src/utils/RailLogic.php @@ -0,0 +1,248 @@ +getID(); + $meta = $rail->getMetadata(); + $this->level = $rail->level; + $this->x = $rail->x; + $this->y = $rail->y; + $this->z = $rail->z; + $this->isStraight = $id === POWERED_RAIL; + + $this->updateConnections($meta); + } + public function getRail($v){ + if(($b = $this->level->getBlock($v)) instanceof RailBaseBlock){ + return new RailLogic($b); + }elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y + 1, $v->z)) instanceof RailBaseBlock){ + return new RailLogic($b); + }elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y - 1, $v->z)) instanceof RailBaseBlock){ + return new RailLogic($b); + } + + return null; + } + + public function hasRail($x, $y, $z){ + return RailBaseBlock::isRailBlock($this->level, $x, $y, $z) || RailBaseBlock::isRailBlock($this->level, $x, $y + 1, $z) || RailBaseBlock::isRailBlock($this->level, $x, $y - 1, $z); + } + + public function countPotentialConnections(){ + return $this->hasRail($this->x, $this->y, $this->z - 1) + $this->hasRail($this->x, $this->y, $this->z + 1) + $this->hasRail($this->x - 1, $this->y, $this->z) + $this->hasRail($this->x + 1, $this->y, $this->z); + } + + public function removeSoftConnections(){ + for($ind = 0; $ind < count($this->railPositions); ++$ind){ + $logic = $this->getRail($this->railPositions[$ind]); + if($logic != null && $this->connectsTo($logic)){ + $this->railPositions[$ind] = new Vector3($logic->x, $logic->y, $logic->z); + }else{ + unset($this->railPositions[$ind--]); //TODO check? + $this->railPositions = array_values($this->railPositions); //reindexing + } + } + } + + public function connectsTo(RailLogic $logic){ + foreach($this->railPositions as $rpos){ + if(($rpos->x === $logic->x) && ($rpos->z === $logic->z)){ + return true; + } + } + return false; + } + + public function canConnectTo(RailLogic $rail){ + //return this.isRailChunkPositionCorrect(par1BlockBaseRailLogic) ? true : (this.railChunkPosition.size() == 2 ? false : (this.railChunkPosition.isEmpty() ? true : true)); + return $this->connectsTo($rail) ? true : (count($this->railPositions) == 2 ? false : (empty($this->railPositions) ? true : true)); + } + + public function hasNeighborRail($x, $y, $z){ + $logic = $this->getRail(new Vector3($x, $y, $z)); + if($logic === null){ + return false; + }else{ + $logic->removeSoftConnections(); //i place $this here and was searching why isnt it working correctly for 2 hours + return $logic->canConnectTo($this); + } + + } + + public function place($b, $b1){ + $hasZneg = $this->hasNeighborRail($this->x, $this->y, $this->z - 1); + $hasZpos = $this->hasNeighborRail($this->x, $this->y, $this->z + 1); + $hasXneg = $this->hasNeighborRail($this->x - 1, $this->y, $this->z); + $hasXpos = $this->hasNeighborRail($this->x + 1, $this->y, $this->z); + $state = -1; + if(($hasZneg || $hasZpos) && !($hasXneg && $hasXpos)){ + $state = 0; + } + if(($hasXneg || $hasXpos) && !($hasZneg && $hasZpos)){ + $state = 1; + } + + if(!$this->isStraight){ + if($hasZpos && $hasXpos && !($hasZneg && $hasXneg)) $state = 6; + if($hasZpos && $hasXneg && !($hasZneg && $hasXpos)) $state = 7; + if($hasZneg && $hasXneg && !($hasZpos && $hasXpos)) $state = 8; + if($hasZneg && $hasXpos && !($hasZpos && $hasXneg)) $state = 9; + } + + if($state === -1){ + if($hasZneg || $hasZpos) $state = 0; + if($hasXneg || $hasXpos) $state = 1; + + if(!$this->isStraight){ + if($b){ + if($hasZpos && $hasXpos) $state = 6; + if($hasXneg && $hasZpos) $state = 7; + if($hasXpos && $hasZneg) $state = 9; + if($hasZneg && $hasXneg) $state = 8; + }else{ + if($hasZneg && $hasXneg) $state = 8; + if($hasXpos && $hasZneg) $state = 9; + if($hasXneg && $hasZpos) $state = 7; + if($hasZpos && $hasXpos) $state = 6; + } + } + } + + + if($state === 0){ + if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z - 1)) $state = 4; + if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z + 1)) $state = 5; + }elseif($state === 1){ + if(RailBaseBlock::isRailBlock($this->level, $this->x + 1, $this->y + 1, $this->z)) $state = 2; + if(RailBaseBlock::isRailBlock($this->level, $this->x - 1, $this->y + 1, $this->z)) $state = 3; + }elseif($state < 0) $state = 0; + + $this->updateConnections($state); + $meta = $state; + if($this->isStraight){ + $meta = $this->level->level->getBlockDamage($this->x, $this->x, $this->z) & 8 | $state; + } + + if($b1 || $this->level->level->getBlockDamage($this->x, $this->y, $this->z)){ + //this.logicWorld.setBlockMetadataWithNotify(this.railX, this.railY, this.railZ, var8, 3); + $bl = $this->level->getBlockWithoutVector($this->x, $this->y, $this->z); + $bl->setMetadata($meta); + $this->level->setBlock(new Vector3($this->x, $this->y, $this->z), $bl, true, false, true); + foreach($this->railPositions as $rpos){ + $logic = $this->getRail($rpos); + if($logic !== null){ + $logic->removeSoftConnections(); + if($logic->canConnectTo($this)){ + $logic->connectTo($this); + } + + } + } + + } + + } + + public function hasConnection($x, $y, $z){ + foreach($this->railPositions as $rpos){ + if($rpos->x === $x && $rpos->z === $z) return true; + } + return false; + } + + public function connectTo(RailLogic $logic){ + $this->railPositions[] = new Vector3($logic->x, $logic->y, $logic->z); + + $hasZneg = $this->hasConnection($this->x, $this->y, $this->z - 1); + $hasZpos = $this->hasConnection($this->x, $this->y, $this->z + 1); + $hasXneg = $this->hasConnection($this->x - 1, $this->y, $this->z); + $hasXpos = $this->hasConnection($this->x + 1, $this->y, $this->z); + $state = -1; + + if($hasZneg || $hasZpos) $state = 0; + if($hasXneg || $hasXpos) $state = 1; + + if(!$this->isStraight){ + if($hasZpos && $hasXpos && !($hasZneg && $hasXneg)) $state = 6; + if($hasZpos && $hasXneg && !($hasZneg && $hasXpos)) $state = 7; + if($hasZneg && $hasXneg && !($hasZpos && $hasXpos)) $state = 8; + if($hasZneg && $hasXpos && !($hasZpos && $hasXneg)) $state = 9; + } + + if($state == 0){ + if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z - 1)) $state = 4; + if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z + 1)) $state = 5; + }elseif($state == 1){ + if(RailBaseBlock::isRailBlock($this->level, $this->x + 1, $this->y + 1, $this->z)) $state = 2; + if(RailBaseBlock::isRailBlock($this->level, $this->x - 1, $this->y + 1, $this->z)) $state = 3; + }elseif($state < 0) $state = 0; + + $meta = $state; + + if($this->isStraight){ + $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); + $this->level->fastSetBlockUpdateMeta($this->x, $this->y, $this->z, $meta, true); + } + + public function updateConnections($meta){ + + $this->railPositions = []; + + switch($meta){ + case 0: + $this->railPositions[] = new Vector3($this->x, $this->y, $this->z - 1); + $this->railPositions[] = new Vector3($this->x, $this->y, $this->z + 1); + break; + case 1: + $this->railPositions[] = new Vector3($this->x - 1, $this->y, $this->z); + $this->railPositions[] = new Vector3($this->x + 1, $this->y, $this->z); + break; + case 2: + $this->railPositions[] = new Vector3($this->x - 1, $this->y, $this->z); + $this->railPositions[] = new Vector3($this->x + 1, $this->y + 1, $this->z); + break; + case 3: + $this->railPositions[] = new Vector3($this->x - 1, $this->y + 1, $this->z); + $this->railPositions[] = new Vector3($this->x + 1, $this->y, $this->z); + break; + case 4: + $this->railPositions[] = new Vector3($this->x, $this->y + 1, $this->z - 1); + $this->railPositions[] = new Vector3($this->x, $this->y, $this->z + 1); + break; + case 5: + $this->railPositions[] = new Vector3($this->x, $this->y, $this->z - 1); + $this->railPositions[] = new Vector3($this->x, $this->y + 1, $this->z + 1); + break; + case 6: + $this->railPositions[] = new Vector3($this->x + 1, $this->y, $this->z); + $this->railPositions[] = new Vector3($this->x, $this->y, $this->z + 1); + break; + case 7: + $this->railPositions[] = new Vector3($this->x - 1, $this->y, $this->z); + $this->railPositions[] = new Vector3($this->x, $this->y, $this->z + 1); + break; + case 8: + $this->railPositions[] = new Vector3($this->x - 1, $this->y, $this->z); + $this->railPositions[] = new Vector3($this->x, $this->y, $this->z - 1); + break; + case 9: + $this->railPositions[] = new Vector3($this->x + 1, $this->y, $this->z); + $this->railPositions[] = new Vector3($this->x, $this->y, $this->z - 1); + break; + } + } +} diff --git a/src/world/Level.php b/src/world/Level.php index 62324804..48b08d6c 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -122,6 +122,20 @@ class Level{ } } + 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 getOrderedFullChunk($X, $Z){ $X = (int)$X; $Z = (int)$Z; From 70e4355d610c3419939d5340709edbe2167934f8 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 1 Dec 2023 06:24:53 +0300 Subject: [PATCH 25/43] using native MTRandom if php8.2+ is detected --- src/config.php | 2 +- src/dependencies.php | 2 + src/entity/object/Minecart.php | 7 +- src/utils/MTRandom.php | 123 +++++++++++++++--------- src/world/generator/NormalGenerator.php | 2 +- 5 files changed, 81 insertions(+), 55 deletions(-) diff --git a/src/config.php b/src/config.php index a53cf9d0..cc63e55a 100644 --- a/src/config.php +++ b/src/config.php @@ -52,7 +52,7 @@ define("MAJOR_VERSION", "2.0.0dev"); define("CODENAME", "Breadfence"); //i'm not very creative - kotyaralih define("CURRENT_MINECRAFT_VERSION", "v0.9.5 alpha"); define("CURRENT_API_VERSION", '13.1'); -define("CURRENT_PHP_VERSION", "5.5"); +define("CURRENT_PHP_VERSION", "8.0"); $gitsha1 = false; if(file_exists(FILE_PATH . ".git/refs/heads/master")){ //Found Git information! define("GIT_COMMIT", strtolower(trim(file_get_contents(FILE_PATH . ".git/refs/heads/master")))); diff --git a/src/dependencies.php b/src/dependencies.php index 94734f6d..aff3def1 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -22,6 +22,8 @@ if(version_compare("8.0.0", PHP_VERSION) > 0){ ++$errors; } +define("USE_NATIVE_RANDOM", version_compare("8.2.0", PHP_VERSION) <= 0); + if(php_sapi_name() !== "cli"){ console("[ERROR] You must run NostalgiaCore using the CLI.", true, true, 0); ++$errors; diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index 9cf5fe16..3bf1519f 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -15,11 +15,8 @@ class Minecart extends Vehicle{ $this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"]:$this->z; $this->setHealth(3, "generic"); //$this->setName((isset($objects[$this->type]) ? $objects[$this->type]:$this->type)); - $this->width = 1; - /*$this->moveVector[Entity::NORTH] = new Vector3(-1, 0, 0); - $this->moveVector[Entity::SOUTH] = new Vector3(1, 0, 0); - $this->moveVector[Entity::EAST] = new Vector3(0, 0, -1); - $this->moveVector[Entity::WEST] = new Vector3(0, 0, 1);*/ + $this->width = 0.98; + $this->height = 0.7; $this->update(); } diff --git a/src/utils/MTRandom.php b/src/utils/MTRandom.php index 5cc5d5c3..a3ceae43 100644 --- a/src/utils/MTRandom.php +++ b/src/utils/MTRandom.php @@ -1,56 +1,83 @@ setSeed($seed == null ? (int)microtime(1) : $seed); - } - - public function setSeed($seed){ - $this->mt[0] = $seed & 0xffffffff; - for($this->index = 1; $this->index < 624; ++$this->index){ - $this->mt[$this->index] = (0x6c078965 * ($this->mt[$this->index-1] >> 30 ^ $this->mt[$this->index - 1]) + $this->index) & 0xffffffff; +if(USE_NATIVE_RANDOM){ +eval(' + class MTRandom + { + public $native; + public function __construct($seed = null){ + $this->native = new \Random\Randomizer(new \Random\Engine\Mt19937($seed)); } - } - - public function genRandInt(){ - if($this->index >= 624 || $this->index < 0){ - if($this->index >= 625 || $this->index < 0) $this->setSeed(4357); - - for($kk = 0; $kk < 227; ++$kk){ - $y = ($this->mt[$kk] & 0x80000000) | ($this->mt[$kk + 1] & 0x7fffffff); - $this->mt[$kk] = $this->mt[$kk+397] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; - } - - for(;$kk < 623; ++$kk){ - $y = ($this->mt[$kk] & 0x80000000) | ($this->mt[$kk + 1] & 0x7fffffff); - $this->mt[$kk] = $this->mt[$kk-227] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; - } - - $y = ($this->mt[623] & 0x80000000) | ($this->mt[0] & 0x7fffffff); - $this->mt[623] = $this->mt[396] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; - $this->index = 0; + public function setSeed($seed){ + $this->native = new \Random\Randomizer(new \Random\Engine\Mt19937($seed)); } - $y = $this->mt[$this->index++]; - $y ^= ($y >> 11); - $y ^= ($y << 7) & 0x9d2c5680; - $y ^= ($y << 15) & 0xefc60000; - $y ^= ($y >> 18); - return $y; + public function genRandInt(){ + return $this->native->getInt(0, 0x7fffffff); + } + + public function nextInt($bound = null){ + return $bound == null ? $this->native->nextInt() : $this->native->getInt(0, $bound - 1); + } + + public function nextFloat(){ + return $this->native->getInt(0, 0x7fffffff) / 0x7fffffff; + } } - - public function nextInt($bound = null){ - return $bound == null ? $this->genRandInt() >> 1 : $this->genRandInt() % $bound; - } - - public function nextFloat(){ - return $this->genRandInt() / 0xffffffff; +'); +}else{ +eval(' + class MTRandom + { + const MAG = [0, 0x9908b0df]; + public $mt; + public $index = 0; + public function __construct($seed = null){ + $this->setSeed($seed == null ? (int)microtime(1) : $seed); + } + + public function setSeed($seed){ + $this->mt[0] = $seed & 0xffffffff; + for($this->index = 1; $this->index < 624; ++$this->index){ + $this->mt[$this->index] = (0x6c078965 * ($this->mt[$this->index-1] >> 30 ^ $this->mt[$this->index - 1]) + $this->index) & 0xffffffff; + } + } + + public function genRandInt(){ + if($this->index >= 624 || $this->index < 0){ + if($this->index >= 625 || $this->index < 0) $this->setSeed(4357); + + for($kk = 0; $kk < 227; ++$kk){ + $y = ($this->mt[$kk] & 0x80000000) | ($this->mt[$kk + 1] & 0x7fffffff); + $this->mt[$kk] = $this->mt[$kk+397] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; + } + + for(;$kk < 623; ++$kk){ + $y = ($this->mt[$kk] & 0x80000000) | ($this->mt[$kk + 1] & 0x7fffffff); + $this->mt[$kk] = $this->mt[$kk-227] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; + } + + $y = ($this->mt[623] & 0x80000000) | ($this->mt[0] & 0x7fffffff); + $this->mt[623] = $this->mt[396] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1]; + $this->index = 0; + } + + $y = $this->mt[$this->index++]; + $y ^= ($y >> 11); + $y ^= ($y << 7) & 0x9d2c5680; + $y ^= ($y << 15) & 0xefc60000; + $y ^= ($y >> 18); + return $y; + } + + public function nextInt($bound = null){ + return $bound == null ? $this->genRandInt() >> 1 : $this->genRandInt() % $bound; + } + + public function nextFloat(){ + return $this->genRandInt() / 0xffffffff; + } } +'); } + diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index cdb72170..15c71b4c 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -152,7 +152,7 @@ class NormalGenerator implements NewLevelGenerator{ $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, $biomes); $this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk); } - //$this->caveGenerator->generate($this->level, $chunkX, $chunkZ); TODO speedup + //$this->caveGenerator->generate($this->level, $chunkX, $chunkZ); //TODO speedup } public function populateChunk($chunkX, $chunkZ){ From 728b8f6d2ea0e17478531a3e8b8a66f4fb9a4d6b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 1 Dec 2023 08:50:46 +0300 Subject: [PATCH 26/43] client-server sync when using items --- src/Player.php | 39 ++++++++++++++++--- .../protocol/packet/UpdateBlockPacket.php | 5 +++ src/world/Level.php | 31 ++++++++++++++- 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/Player.php b/src/Player.php index 6d32549e..76188788 100755 --- a/src/Player.php +++ b/src/Player.php @@ -68,9 +68,10 @@ class Player{ private $chunkCount = []; private $received = []; public $cratingItems; - public $sleepingTime = 0; + public $blockSendQueue; + public $blockSendQueueLength = 0; /** * @param integer $clientID * @param string $ip @@ -97,6 +98,8 @@ class Player{ $this->packetStats = [0, 0]; $this->buffer = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->buffer->data = []; + $this->blockSendQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->blockSendQueue->data = []; $this->server->schedule(1, [$this, "handlePacketQueues"], [], true); $this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); $this->evid[] = $this->server->event("server.close", [$this, "close"]); @@ -248,7 +251,23 @@ class Player{ $pk->teleport = true; $this->dataPacket($pk); } - + public function addToBlockSendQueue(RakNetDataPacket $packet){ + if($this->connected === false) return false; + + $packet->encode(); + $len = strlen($packet->buffer) + 1; + $MTU = $this->MTU - 24; + + + if(($this->blockSendQueueLength + $len) >= $MTU){ + $this->sendBlockUpdateQueue(); + } + + $packet->messageIndex = $this->counter[3]++; + $packet->reliability = 2; + @$this->blockSendQueue->data[] = $packet; + $this->blockSendQueueLength += 6 + $len; + } /** * @param integer $id * @param array $data @@ -325,7 +344,17 @@ class Player{ $this->bandwidthRaw += $this->server->send($packet); } } - + + + public function sendBlockUpdateQueue(){ + if($this->blockSendQueueLength > 0 and $this->blockSendQueue instanceof RakNetPacket){ + $this->blockSendQueue->seqNumber = $this->counter[0]++; + $this->send($this->blockSendQueue); + } + $this->blockSendQueueLength = 0; + $this->blockSendQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); + $this->blockSendQueue->data = []; + } public function sendBuffer(){ if($this->bufferLen > 0 and $this->buffer instanceof RakNetPacket){ $this->buffer->seqNumber = $this->counter[0]++; @@ -1622,10 +1651,10 @@ class Player{ //$this->lastChunk = [$packet->chunkX, $packet->chunkZ]; break; case ProtocolInfo::UPDATE_BLOCK_PACKET: - if($this->gamemode & 0x01 === 0){ + if($this->gamemode & 0x01 === 1){ $this->level->setBlock(new Vector3($packet->x, $packet->y, $packet->z), BlockAPI::get($packet->block, $packet->meta)); } - + $this->level->resendBlocksToPlayers[$this->CID]["{$packet->x}.{$packet->y}.{$packet->z}"] = true; //bad client i hate u break; case ProtocolInfo::USE_ITEM_PACKET: if(!($this->entity instanceof Entity)){ diff --git a/src/network/protocol/packet/UpdateBlockPacket.php b/src/network/protocol/packet/UpdateBlockPacket.php index 230e969b..141f61b1 100644 --- a/src/network/protocol/packet/UpdateBlockPacket.php +++ b/src/network/protocol/packet/UpdateBlockPacket.php @@ -12,6 +12,11 @@ class UpdateBlockPacket extends RakNetDataPacket{ } public function decode(){ + $this->x = $this->getInt(); + $this->z = $this->getInt(); + $this->y = $this->getByte(); + $this->block = $this->getByte(); + $this->meta = $this->getByte(); } public function encode(){ diff --git a/src/world/Level.php b/src/world/Level.php index 48b08d6c..1ff5fa0c 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -20,7 +20,7 @@ class Level{ public $entityList; public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner; public $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime; - + public $resendBlocksToPlayers = []; public $generator; public function __construct(PMFLevel $level, Config $entities, Config $tiles, Config $blockUpdates, $name){ $this->server = ServerAPI::request(); @@ -452,7 +452,34 @@ class Level{ $this->level->unloadChunk($xz[0], $xz[1]); unset($this->level->fakeLoaded[$ind]); } - + foreach($this->resendBlocksToPlayers as $playerCID => $blockz){ + /** + * @var Player $player + */ + $player = $this->server->clients[$playerCID] ?? false; + if($player instanceof Player){ + foreach($blockz as $xyz => $_){ + $xyzz = explode(".", $xyz); + if(count($xyzz) == 3){ + $x = (int) $xyzz[0]; + $y = (int) $xyzz[1]; + $z = (int) $xyzz[2]; + $idm = $this->level->getBlock($x, $y, $z); + //$player->addToBlockSendQueue(new UpdateBlockPacket($x, $y, $z, $idm[0], $idm[1])); + $pk = new UpdateBlockPacket; + $pk->x = $x; + $pk->y = $y; + $pk->z = $z; + $pk->block = $idm[0] & 0xff; + $pk->meta = $idm[1] & 0xff; + $player->addToBlockSendQueue($pk); + } + unset($blockz[$xyz]); + } + $player->sendBlockUpdateQueue(); + } + unset($this->resendBlocksToPlayers[$playerCID]); + } } public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){ From d334b85c636d577e84444f2cf32e7778bdd9ce53 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 1 Dec 2023 09:15:56 +0300 Subject: [PATCH 27/43] added double tall grass --- src/API/BlockAPI.php | 6 ++- src/material/Block.php | 1 + src/material/block/plant/DoubleTallGrass.php | 41 ++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/material/block/plant/DoubleTallGrass.php diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index d15fda93..e010e6cb 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -258,7 +258,11 @@ class BlockAPI{ [DYE, 11], [DYE, 10], [DYE, 9], - [DYE, 8] + [DYE, 8], + [DOUBLE_PLANT, 0], + [DOUBLE_PLANT, 1], + [DOUBLE_PLANT, 2], + [DOUBLE_PLANT, 3], ]; private $server; diff --git a/src/material/Block.php b/src/material/Block.php index eb93ac8c..80c83ea9 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -151,6 +151,7 @@ abstract class Block extends Position{ PODZOL => "PodzolBlock", STAINED_CLAY => "StainedClayBlock", LILY_PAD => "LilypadBlock", + DOUBLE_PLANT => "DoubleTallGrass", ); protected $id; protected $meta; diff --git a/src/material/block/plant/DoubleTallGrass.php b/src/material/block/plant/DoubleTallGrass.php new file mode 100644 index 00000000..2b301177 --- /dev/null +++ b/src/material/block/plant/DoubleTallGrass.php @@ -0,0 +1,41 @@ + "Sunflower", + 1 => "Lilac", + 2 => "Tall Grass", + 3 => "Tall Fern", + ]; + public function __construct($meta = 1){ + parent::__construct(DOUBLE_PLANT, $meta, "Tall Grass"); + $this->name = self::$NAMES[$this->meta]; + $this->hardness = 0; + } + + public function onUpdate($type){ + if($type === BLOCK_UPDATE_NORMAL){ //TODO destroy if 2nd block occupied + if($this->getSide(0)->isTransparent === true){//Replace with common break method + $this->level->setBlock($this, new AirBlock(), false, false, true); + return BLOCK_UPDATE_NORMAL; + } + } + return false; + } + + public function getDrops(Item $item, Player $player){ //TODO vanilla? + return []; + } + + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ + $down = $this->getSide(0); + if($down->getID() == 2 or $down->getID() == 3){ //TODO destroy the block above(tested only in creative) + $this->level->setBlock($block, $this, true, false, true); + return true; + } + return false; + } + +} + From 1e9b5b571b5ab1285d9c2f3afeb55faf4b166668 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 1 Dec 2023 10:35:59 +0300 Subject: [PATCH 28/43] =?UTF-8?q?=CF=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++----- start.sh | 4 ++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d230f8df..07d2bf03 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,10 @@ You should not use it now.

Download Stable NostalgiaCore

https://github.com/kotyaralih/NostalgiaCore/releases/ -

PHP binaries download

-* https://sourceforge.net/projects/pocketmine/files/builds/ - PHP for old PocketMine -* https://owouwu.ru/NCArm_bin.tar.gz - PHP binaries for ARM -* https://jenkins.pmmp.io/job/PHP-8.0-Aggregate/ PHP8(core should have php8 support as of now) - +* https://github.com/pmmp/PHP-Binaries/releases/tag/php-8.0-latest PHP8 (Use PM4 version) +* https://github.com/DaisukeDaisuke/AndroidPHP/releases/tag/8.0.28 PHP8 for ARMv8

How to use auto-restart?

Simply start your server using ./start.sh -l diff --git a/start.sh b/start.sh index 01e91a4d..e06f3f06 100644 --- a/start.sh +++ b/start.sh @@ -20,9 +20,9 @@ while getopts "p:f:l" OPTION 2> /dev/null; do done if [ "$PHP_BINARY" == "" ]; then - if [ -f ./bin/php5/bin/php ]; then + if [ -f ./bin/php7/bin/php ]; then export PHPRC="" - PHP_BINARY="./bin/php5/bin/php" + PHP_BINARY="./bin/php7/bin/php" elif [[ ! -z $(type php) ]]; then PHP_BINARY=$(type -p php) else From 03d5a05149ed02dcbb03b092fb64e41f2947d998 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 1 Dec 2023 11:07:49 +0300 Subject: [PATCH 29/43] small tweaks to tall plants --- src/API/BlockAPI.php | 3 ++- src/material/block/plant/DoubleTallGrass.php | 13 ++++++++----- src/material/block/plant/TallGrassBlock.php | 6 +++--- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index e010e6cb..2f3a04aa 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -263,7 +263,8 @@ class BlockAPI{ [DOUBLE_PLANT, 1], [DOUBLE_PLANT, 2], [DOUBLE_PLANT, 3], - + [DOUBLE_PLANT, 4], + [DOUBLE_PLANT, 5], ]; private $server; private $scheduledUpdates = []; diff --git a/src/material/block/plant/DoubleTallGrass.php b/src/material/block/plant/DoubleTallGrass.php index 2b301177..ea9f1b8a 100644 --- a/src/material/block/plant/DoubleTallGrass.php +++ b/src/material/block/plant/DoubleTallGrass.php @@ -7,19 +7,21 @@ class DoubleTallGrass extends FlowableBlock 1 => "Lilac", 2 => "Tall Grass", 3 => "Tall Fern", + 4 => "Rose Bush", + 5 => "Peony", ]; public function __construct($meta = 1){ parent::__construct(DOUBLE_PLANT, $meta, "Tall Grass"); - $this->name = self::$NAMES[$this->meta]; + $this->name = self::$NAMES[$this->meta] ?? "Tall Grass"; $this->hardness = 0; } public function onUpdate($type){ if($type === BLOCK_UPDATE_NORMAL){ //TODO destroy if 2nd block occupied - if($this->getSide(0)->isTransparent === true){//Replace with common break method - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } + //if($this->meta & 0x8 == 0 && $this->getSide(0)->isTransparent === true){ //Replace with common break method + ///$this->level->setBlock($this, new AirBlock(), false, false, true); + //return BLOCK_UPDATE_NORMAL; + //} //TODO fix } return false; } @@ -32,6 +34,7 @@ class DoubleTallGrass extends FlowableBlock $down = $this->getSide(0); if($down->getID() == 2 or $down->getID() == 3){ //TODO destroy the block above(tested only in creative) $this->level->setBlock($block, $this, true, false, true); + $this->level->setBlock($block->add(0, 1, 0), new DoubleTallGrass($this->meta ^ 0x8), true, false, true); return true; } return false; diff --git a/src/material/block/plant/TallGrassBlock.php b/src/material/block/plant/TallGrassBlock.php index 082a79b3..45a2f5e1 100644 --- a/src/material/block/plant/TallGrassBlock.php +++ b/src/material/block/plant/TallGrassBlock.php @@ -2,11 +2,11 @@ class TallGrassBlock extends FlowableBlock{ public function __construct($meta = 1){ - parent::__construct(TALL_GRASS, $meta, "Tall Grass"); + parent::__construct(TALL_GRASS, $meta, "Grass"); $this->isReplaceable = true; $names = array( 0 => "Dead Shrub", - 1 => "Tall Grass", + 1 => "Grass", 2 => "Fern", ); $this->name = $names[$this->meta & 0x02]; @@ -15,7 +15,7 @@ class TallGrassBlock extends FlowableBlock{ public function onUpdate($type){ if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){//Replace with common break method + if($this->getSide(0)->isTransparent === true){ //Replace with common break method $this->level->setBlock($this, new AirBlock(), false, false, true); if(Utils::chance(15)) ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem(WHEAT_SEEDS)); return BLOCK_UPDATE_NORMAL; From 0e796c8dd324e0f03280d6bb766ec3968383754a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 1 Dec 2023 11:15:51 +0300 Subject: [PATCH 30/43] sending tile entities only once --- src/Player.php | 4 ++-- src/world/Level.php | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/Player.php b/src/Player.php index 76188788..39c69901 100755 --- a/src/Player.php +++ b/src/Player.php @@ -492,7 +492,7 @@ class Player{ } } - $tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '" . $this->level->getName() . "' AND x >= " . (($X << 4) - 1) . " AND x < " . (($X << 4) + 17) . " AND z >= " . (($Z << 4) - 1) . " AND z < " . (($Z << 4) + 17) . ";"); + /*$tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '" . $this->level->getName() . "' AND x >= " . (($X << 4) - 1) . " AND x < " . (($X << 4) + 17) . " AND z >= " . (($Z << 4) - 1) . " AND z < " . (($Z << 4) + 17) . ";"); $this->lastChunk = false; if($tiles !== false and $tiles !== true){ while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ @@ -501,7 +501,7 @@ class Player{ $tile->spawn($this); } } - } + }*/ //$this->stopUsingChunk($X, $Z); //just in case $pk = new FullChunkDataPacket; $pk->chunkX = $X; diff --git a/src/world/Level.php b/src/world/Level.php index 1ff5fa0c..83965d5e 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -186,33 +186,44 @@ class Level{ } $nbt = new NBT_new(NBT_new::LITTLE_ENDIAN); - foreach($chunkTiles as $tile){ //TODO non-hardcoded nbt data + foreach($chunkTiles as $tile){ //TODO rewrite TileEntity system switch($tile->class){ case "Sign": $text = $tile->getText(); - $nbt->setData(new Compound("", array( - new StringTag("Text1", $text[0]), - new StringTag("Text2", $text[1]), - new StringTag("Text3", $text[2]), - new StringTag("Text4", $text[3]), - new StringTag("id", "Sign"), - new IntTag("x", (int) $tile->x), - new IntTag("y", (int) $tile->y), - new IntTag("z", (int) $tile->z) - ))); - $tileEntities .= $nbt->write(); + $nbt->setData(new Compound("", array( + new StringTag("Text1", $text[0]), + new StringTag("Text2", $text[1]), + new StringTag("Text3", $text[2]), + new StringTag("Text4", $text[3]), + new StringTag("id", "Sign"), + new IntTag("x", (int) $tile->x), + new IntTag("y", (int) $tile->y), + new IntTag("z", (int) $tile->z) + ))); + $tileEntities .= $nbt->write(); break; case "Furnace": - //nutting, not spawnable :D + //nutting, not spawnable :D break; case "Chest": + if($tile->isPaired()){ $nbt->setData(new Compound("", array( new StringTag("id", "Chest"), new IntTag("x", (int) $tile->x), new IntTag("y", (int) $tile->y), - new IntTag("z", (int) $tile->z) + new IntTag("z", (int) $tile->z), + new IntTag("pairx", (int) $tile->x), + new IntTag("pairz", (int) $tile->z) ))); - $tileEntities .= $nbt->write(); + }else{ + $nbt->setData(new Compound("", array( + new StringTag("id", "Chest"), + new IntTag("x", (int) $tile->x), + new IntTag("y", (int) $tile->y), + new IntTag("z", (int) $tile->z), + ))); + } + $tileEntities .= $nbt->write(); break; } } From 9abc3cd409343faae5440cf1edb7e06175ae9fdf Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 2 Dec 2023 09:07:29 +0300 Subject: [PATCH 31/43] acacia somewhat fixed --- src/Player.php | 5 +---- src/world/generator/object/tree/AcaciaTreeObject.php | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Player.php b/src/Player.php index 39c69901..3196f7f3 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1651,10 +1651,7 @@ class Player{ //$this->lastChunk = [$packet->chunkX, $packet->chunkZ]; break; case ProtocolInfo::UPDATE_BLOCK_PACKET: - if($this->gamemode & 0x01 === 1){ - $this->level->setBlock(new Vector3($packet->x, $packet->y, $packet->z), BlockAPI::get($packet->block, $packet->meta)); - } - $this->level->resendBlocksToPlayers[$this->CID]["{$packet->x}.{$packet->y}.{$packet->z}"] = true; //bad client i hate u + $this->level->resendBlocksToPlayers[$this->CID]["{$packet->x}.{$packet->y}.{$packet->z}"] = true; break; case ProtocolInfo::USE_ITEM_PACKET: if(!($this->entity instanceof Entity)){ diff --git a/src/world/generator/object/tree/AcaciaTreeObject.php b/src/world/generator/object/tree/AcaciaTreeObject.php index 728a7442..e77b0973 100644 --- a/src/world/generator/object/tree/AcaciaTreeObject.php +++ b/src/world/generator/object/tree/AcaciaTreeObject.php @@ -43,7 +43,7 @@ class AcaciaTreeObject extends TreeObject{ $var17 = $level->level->getBlockID($var23, $var16, $var13); - if ($var17 == AIR || $var17 == LEAVES || $var17 == LEAVES2) + if ($var17 == AIR || $var17 == LEAVES || $var17 == LEAVES2 || $var17 == SAPLING) { $level->fastSetBlockUpdate($var23, $var16, $var13, TRUNK2, 0); $var14 = $var16; @@ -95,7 +95,7 @@ class AcaciaTreeObject extends TreeObject{ $var13 += Direction::offsetZ[$var15]; $var20 = $level->level->getBlockID($var23, $var19, $var13); - if ($var20 == AIR || $var20 == LEAVES || $var20 == LEAVES2) + if ($var20 == AIR || $var20 == LEAVES || $var20 == LEAVES2 || $var20 == SAPLING) { $level->fastSetBlockUpdate($var23, $var19, $var13, TRUNK2, 0); $var14 = $var19; From 60fd866de61fe08451bfbcf3b137db52e3752bf9 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 2 Dec 2023 13:39:58 +0300 Subject: [PATCH 32/43] dungeon generator(disabled) --- src/Player.php | 4 +- src/PocketMinecraftServer.php | 2 + src/constants/BlockIDs.php | 1 + src/entity/Entity.php | 2 +- src/world/Level.php | 27 ++++-- src/world/Tile.php | 6 +- src/world/generator/NormalGenerator.php | 25 +++++- src/world/generator/cave/CaveGenerator.php | 25 +++--- src/world/generator/object/Feature.php | 13 +++ .../object/feature/DungeonFeature.php | 82 +++++++++++++++++++ .../generator/populator/DungeonPopulator.php | 0 11 files changed, 157 insertions(+), 30 deletions(-) create mode 100644 src/world/generator/object/Feature.php create mode 100644 src/world/generator/object/feature/DungeonFeature.php delete mode 100644 src/world/generator/populator/DungeonPopulator.php diff --git a/src/Player.php b/src/Player.php index 3196f7f3..47a30c5e 100755 --- a/src/Player.php +++ b/src/Player.php @@ -540,7 +540,7 @@ class Player{ } } - if(is_array($this->lastChunk)){ + /*if(is_array($this->lastChunk)){ $tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '" . $this->level->getName() . "' AND x >= " . ($this->lastChunk[0] - 1) . " AND x < " . ($this->lastChunk[0] + 17) . " AND z >= " . ($this->lastChunk[1] - 1) . " AND z < " . ($this->lastChunk[1] + 17) . ";"); $this->lastChunk = false; if($tiles !== false and $tiles !== true){ @@ -551,7 +551,7 @@ class Player{ } } } - } + }*/ $c = key($this->chunksOrder); $d = $c != null ? $this->chunksOrder[$c] : null; diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index d89ce005..507b0540 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -33,6 +33,8 @@ class PocketMinecraftServer{ console("[INFO] Starting Minecraft PE server on " . ($this->serverip === "0.0.0.0" ? "*" : $this->serverip) . ":" . $this->port); EntityRegistry::registerEntities(); Structures::initialize(); + Feature::init(); + StaticBlock::init(); 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; diff --git a/src/constants/BlockIDs.php b/src/constants/BlockIDs.php index 79e694cd..2651f043 100755 --- a/src/constants/BlockIDs.php +++ b/src/constants/BlockIDs.php @@ -112,6 +112,7 @@ define("BRICKS_BLOCK", 45); define("TNT", 46); define("BOOKSHELF", 47); define("MOSS_STONE", 48); +define("MOSSY_COBBLESTONE", 48); define("MOSSY_STONE", 48); define("OBSIDIAN", 49); define("TORCH", 50); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 1eff1113..4d26764c 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -840,7 +840,7 @@ class Entity extends Position public function getDirection() { - $rotation = ($this->yaw - 90) % 360; + $rotation = ((int)($this->yaw - 90)) % 360; if($rotation < 0){ $rotation += 360.0; } diff --git a/src/world/Level.php b/src/world/Level.php index 83965d5e..253a6a4a 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -92,7 +92,7 @@ class Level{ $pk->z = $z; $pk->block = $id; $pk->meta = $meta; - $this->server->api->player->broadcastPacket($this->players, $pk); + $this->server->api->player->broadcastPacket($this->players, $pk); //TODO broadcast in another queue? if($updateBlocksAround){ $this->server->api->block->blockUpdateAround(new Position($x, $y, $z, $this), BLOCK_UPDATE_NORMAL, 1); } @@ -188,36 +188,36 @@ class Level{ $nbt = new NBT_new(NBT_new::LITTLE_ENDIAN); foreach($chunkTiles as $tile){ //TODO rewrite TileEntity system switch($tile->class){ - case "Sign": + case TILE_SIGN: $text = $tile->getText(); $nbt->setData(new Compound("", array( new StringTag("Text1", $text[0]), new StringTag("Text2", $text[1]), new StringTag("Text3", $text[2]), new StringTag("Text4", $text[3]), - new StringTag("id", "Sign"), + new StringTag("id", TILE_SIGN), new IntTag("x", (int) $tile->x), new IntTag("y", (int) $tile->y), new IntTag("z", (int) $tile->z) ))); $tileEntities .= $nbt->write(); break; - case "Furnace": + case TILE_FURNACE: //nutting, not spawnable :D break; - case "Chest": + case TILE_CHEST: if($tile->isPaired()){ $nbt->setData(new Compound("", array( new StringTag("id", "Chest"), new IntTag("x", (int) $tile->x), new IntTag("y", (int) $tile->y), new IntTag("z", (int) $tile->z), - new IntTag("pairx", (int) $tile->x), - new IntTag("pairz", (int) $tile->z) + new IntTag("pairx", (int) $tile->getPair()->x), + new IntTag("pairz", (int) $tile->getPair()->z) ))); }else{ $nbt->setData(new Compound("", array( - new StringTag("id", "Chest"), + new StringTag("id", TILE_CHEST), new IntTag("x", (int) $tile->x), new IntTag("y", (int) $tile->y), new IntTag("z", (int) $tile->z), @@ -225,6 +225,17 @@ class Level{ } $tileEntities .= $nbt->write(); break; + case TILE_MOB_SPAWNER: + console("mobspawner spawn {$tile->data["EntityId"]}"); + $nbt->setData(new Compound("", array( + new StringTag("id", TILE_MOB_SPAWNER), + new IntTag("x", (int) $tile->x), + new IntTag("y", (int) $tile->y), + new IntTag("z", (int) $tile->z), + new IntTag("EntityId", (int) $tile->data["EntityId"]), + ))); + $tileEntities .= $nbt->write(); + break; } } $orderedUncompressed = Utils::writeLInt($X) . Utils::writeLInt($Z) . diff --git a/src/world/Tile.php b/src/world/Tile.php index a1ced35b..296d0062 100644 --- a/src/world/Tile.php +++ b/src/world/Tile.php @@ -13,6 +13,8 @@ class Tile extends Position{ public $attach; public $metadata; public $closed; + public $scheduledUpdate; + public $lastUpdate; private $server; function __construct(Level $level, $id, $class, $x, $y, $z, $data = []){ @@ -361,7 +363,7 @@ class Tile extends Position{ $nbt->writeTAG_String("EntityId"); $nbt->writeTAG_Int((int) $this->data["EntityId"]); - $nbt->write(chr(NBT::TAG_SHORT)); + /*$nbt->write(chr(NBT::TAG_SHORT)); $nbt->writeTAG_String("Delay"); $nbt->writeTAG_SHORT((int) $this->data["Delay"]); @@ -387,7 +389,7 @@ class Tile extends Position{ $nbt->write(chr(NBT::TAG_SHORT)); $nbt->writeTAG_String("SpawnRange"); - $nbt->writeTAG_SHORT((int) $this->data["SpawnRange"]); + $nbt->writeTAG_SHORT((int) $this->data["SpawnRange"]);*/ //console(bin2hex($nbt->binary)); diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index 15c71b4c..4ef253ba 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -4,10 +4,11 @@ require_once("NewLevelGenerator.php"); /***REM_END***/ class NormalGenerator implements NewLevelGenerator{ - + const HIDDEN_FEATURES = false; private $populators = array(); private $level; private $random; + public $mtrandom; private $worldHeight = 65; private $waterHeight = 63; private $noiseHills; @@ -28,6 +29,7 @@ class NormalGenerator implements NewLevelGenerator{ $this->level = $level; $this->random = $random; $this->random->setSeed($this->level->getSeed()); + $this->mtrandom = new MTRandom($this->level->getSeed()); $this->noiseHills = new NoiseGeneratorPerlin($this->random, 3); $this->noisePatches = new NoiseGeneratorPerlin($this->random, 2); $this->noisePatchesSmall = new NoiseGeneratorPerlin($this->random, 2); @@ -152,12 +154,17 @@ class NormalGenerator implements NewLevelGenerator{ $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, $biomes); $this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk); } - //$this->caveGenerator->generate($this->level, $chunkX, $chunkZ); //TODO speedup + if(self::HIDDEN_FEATURES) $this->caveGenerator->generate($this->level, $chunkX, $chunkZ); //TODO speedup } public function populateChunk($chunkX, $chunkZ){ + $blockX = $chunkX * 16; + $blockZ = $chunkZ * 16; + $this->level->level->setPopulated($chunkX, $chunkZ, true); + $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); + $this->mtrandom->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); $biomeID = $this->level->level->getBiomeId($chunkX*16, $chunkZ*16); $biome = BiomeSelector::get($biomeID); @@ -166,9 +173,19 @@ class NormalGenerator implements NewLevelGenerator{ }else{ $biome->decorator->decorate($this->level, $chunkX, $chunkZ, $this->random); } - - + + if(self::HIDDEN_FEATURES) { + for ($i = 0; $i < 8; ++$i){ + $x = $blockX + $this->mtrandom->nextInt(16) + 8; + $y = $this->mtrandom->nextInt(128); + $z = $blockZ + $this->mtrandom->nextInt(16) + 8; + if(Feature::$DUNGEON->place($this->level, $this->mtrandom, $x, $y, $z)){ + ConsoleAPI::debug("Placed Dungeon at $x $y $z"); + } + } + } foreach($this->populators as $populator){ + $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); //ty shoghicp for 250k bytes of randomness $populator->populate($this->level, $chunkX, $chunkZ, $this->random); } } diff --git a/src/world/generator/cave/CaveGenerator.php b/src/world/generator/cave/CaveGenerator.php index a4c66564..2f1219e4 100644 --- a/src/world/generator/cave/CaveGenerator.php +++ b/src/world/generator/cave/CaveGenerator.php @@ -56,7 +56,6 @@ class CaveGenerator } public function generateLargeCaveNode(Level $level, $xCenter, $zCenter, $x, $y, $z){ - //generate(xCenter, zCenter, tileIdArray, x, y, z, 1.0f + (this.rand.nextFloat() * 6.0f), 0.0f, 0.0f, -1, -1, 0.5d); gen4 $this->generateCaveNode($level, $xCenter, $zCenter, $x, $y, $z, 1 + ($this->rand->nextFloat() * 6), 0, 0, -1, -1, 0.5); } @@ -78,8 +77,8 @@ class CaveGenerator $var27 = $random->nextInt((int)($unk_2 / 2)) + ($unk_2 / 4); for($var28 = ($random->nextInt(6) == 0); $unk_1 < $unk_2; ++$unk_1){ - $var29 = 1.5 + sin($unk_1 * M_PI / $unk_2) * $randFloat /* * 1 */; - $var31 = $var29 * $d3; + $horizontalDiff = 1.5 + sin($unk_1 * M_PI / $unk_2) * $randFloat /* * 1 */; + $verticalDiff = $horizontalDiff * $d3; $var33 = cos($f2); $var34 = sin($f2); $x += cos($f1) * $var33; @@ -115,13 +114,13 @@ class CaveGenerator return; } //if (x >= chunkCenterX - 16.0D - var29 * 2.0D && z >= chunkCenterZ - 16.0D - var29 * 2.0D && x <= chunkCenterX + 16.0D + var29 * 2.0D && z <= chunkCenterZ + 16.0D + var29 * 2.0D) - if($x >= $chunkCenterX - 16 - $var29*2 && $z >= $chunkCenterZ - 16 - $var29*2 && $x <= $chunkCenterX + 16 + $var29*2 && $z <= $chunkCenterZ + 16 + $var29*2){ - $minX = floor($x - $var29) - $chunkX*16 - 1; - $maxX = floor($x + $var29) - $chunkX*16 + 1; - $minY = floor($y - $var31) - 1; - $maxY = floor($y + $var31) + 1; - $minZ = floor($z - $var29) - $chunkZ*16 - 1; - $maxZ = floor($z + $var29) - $chunkZ*16 + 1; + if($x >= $chunkCenterX - 16 - $horizontalDiff*2 && $z >= $chunkCenterZ - 16 - $horizontalDiff*2 && $x <= $chunkCenterX + 16 + $horizontalDiff*2 && $z <= $chunkCenterZ + 16 + $horizontalDiff*2){ + $minX = floor($x - $horizontalDiff) - $chunkX*16 - 1; + $maxX = floor($x + $horizontalDiff) - $chunkX*16 + 1; + $minY = floor($y - $verticalDiff) - 1; + $maxY = floor($y + $verticalDiff) + 1; + $minZ = floor($z - $horizontalDiff) - $chunkZ*16 - 1; + $maxZ = floor($z + $horizontalDiff) - $chunkZ*16 + 1; if($minX < 0) $minX = 0; if($maxX > 16) $maxX = 16; @@ -150,15 +149,15 @@ class CaveGenerator if(!$hasWater){ for($blockX = $minX; $blockX < $maxX; ++$blockX){ - $var59 = (($blockX + $chunkX*16) + 0.5 - $x) / $var29; + $var59 = (($blockX + $chunkX*16) + 0.5 - $x) / $horizontalDiff; for($blockZ = $minZ; $blockZ < $maxZ; ++$blockZ){ - $var46 = (($blockZ + $chunkZ*16) + 0.5 - $z) / $var29; + $var46 = (($blockZ + $chunkZ*16) + 0.5 - $z) / $horizontalDiff; $yPosition = $maxY; $hasGrass = false; if($var59*$var59 + $var46*$var46 < 1){ for($blockY = $maxY - 1; $blockY >= $minY; --$blockY){ - $var51 = ($blockY + 0.5 - $y) / $var31; + $var51 = ($blockY + 0.5 - $y) / $verticalDiff; if($var51 > -0.7 && $var59*$var59 + $var51*$var51 + $var46*$var46 < 1){ $blockID = $level->level->getBlockID($blockX + $chunkX*16, $yPosition, $blockZ + $chunkZ * 16); $hasGrass = $blockID == GRASS; diff --git a/src/world/generator/object/Feature.php b/src/world/generator/object/Feature.php new file mode 100644 index 00000000..cb2682a3 --- /dev/null +++ b/src/world/generator/object/Feature.php @@ -0,0 +1,13 @@ +nextInt(2) + 2; + $randomSizeZ = $rand->nextInt(2) + 2; + $var9 = 0; + + $minX = $initialX - $randomSizeX - 1; + $maxX = $initialX + $randomSizeX + 1; + $minY = $initialY - 1; + $maxY = $initialY + $baseSize + 1; + $minZ = $initialZ - $randomSizeZ - 1; + $maxZ = $initialZ + $randomSizeZ + 1; + + for($x = $minX; $x <= $maxX; ++$x){ + for($y = $minY; $y <= $maxY; ++$y){ + for($z = $minZ; $z <= $maxZ; ++$z){ + $id = $level->level->getBlockID($x, $y, $z); + + if($y == $minY && !StaticBlock::getIsSolid($id)){ + return false; + } + + if($y == $maxY && !StaticBlock::getIsSolid($id)){ + return false; + } + + if(($x == $minX || $x == $maxX || $z == $minZ || $z == $maxZ) && $y == $initialY && $id == 0 && $level->level->getBlockID($x, $y + 1, $z) == 0){ + ++$var9; + } + } + } + } + + if($var9 < 1 || $var9 > 5) return false; + + for($x = $minX; $x <= $maxX; ++$x){ + for($y = $maxY; $y >= $minY; --$y){ + for($z = $minZ; $z <= $maxZ; ++$z){ + $id = $level->level->getBlockID($x, $y, $z); + if($x != $minX && $y != $minY && $z != $minZ && $x != $maxX && $y != $maxY && $z != $maxZ){ + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); + }else if($y >= 0 && !StaticBlock::getIsSolid($id)){ + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); + }else if(StaticBlock::getIsSolid($id)){ + if($y == $minY && $rand->nextInt(4) != 0){ + $level->fastSetBlockUpdate($x, $y, $z, MOSSY_COBBLESTONE, 0); + }else{ + $level->fastSetBlockUpdate($x, $y, $z, COBBLESTONE, 0); + } + } + } + } + } + + //TODO chest gen + + $level->fastSetBlockUpdate($initialX, $initialY, $initialZ, MONSTER_SPAWNER, 0); //TODO bteter block placement system + ServerAPI::request()->api->tile->add($level, TILE_MOB_SPAWNER, $initialX, $initialY, $initialZ, [ + "EntityId" => match($rand->nextInt(4)){ + 0 => MOB_SKELETON, + 1, 2 => MOB_ZOMBIE, + 3 => MOB_SPIDER + }, + "id" => TILE_MOB_SPAWNER, + "x" => $initialX, + "y" => $initialY, + "z" => $initialZ, + ]); + return true; + } +} + diff --git a/src/world/generator/populator/DungeonPopulator.php b/src/world/generator/populator/DungeonPopulator.php deleted file mode 100644 index e69de29b..00000000 From ea3babff0221f321584b93512502e7383c65fed3 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 2 Dec 2023 14:05:37 +0300 Subject: [PATCH 33/43] clicking on spawner with egg will change a mob there --- src/material/block/solid/MonsterSpawnerBlock.php | 11 ++++++++++- src/material/item/generic/SpawnEggItem.php | 12 ++++++++++++ src/world/Level.php | 1 - src/world/generator/NormalGenerator.php | 2 +- .../generator/populator/DesertWellPopulator.php | 0 .../generator/populator/ForestRockPopulator.php | 0 src/world/generator/populator/VillagePopulator.php | 0 7 files changed, 23 insertions(+), 3 deletions(-) delete mode 100644 src/world/generator/populator/DesertWellPopulator.php delete mode 100644 src/world/generator/populator/ForestRockPopulator.php delete mode 100644 src/world/generator/populator/VillagePopulator.php diff --git a/src/material/block/solid/MonsterSpawnerBlock.php b/src/material/block/solid/MonsterSpawnerBlock.php index 11ac5c3e..ce097b01 100644 --- a/src/material/block/solid/MonsterSpawnerBlock.php +++ b/src/material/block/solid/MonsterSpawnerBlock.php @@ -5,7 +5,16 @@ class MonsterSpawnerBlock extends SolidBlock{ parent::__construct(MONSTER_SPAWNER, $meta, "Monster Spawner"); $this->hardness = 5; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ + parent::place($item, $player, $block, $target, $face, $fx, $fy, $fz); + ServerAPI::request()->api->tile->add($this->level, TILE_MOB_SPAWNER, $this->x, $this->y, $this->z, [ + "EntityId" => 0, + "id" => TILE_MOB_SPAWNER, + "x" => $this->x, + "y" => $this->y, + "z" => $this->z, + ]); + } public function getDrops(Item $item, Player $player){ return []; } diff --git a/src/material/item/generic/SpawnEggItem.php b/src/material/item/generic/SpawnEggItem.php index 3aac8d9c..9deb6c76 100644 --- a/src/material/item/generic/SpawnEggItem.php +++ b/src/material/item/generic/SpawnEggItem.php @@ -8,6 +8,18 @@ class SpawnEggItem extends Item{ } public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ + + if($target instanceof MonsterSpawnerBlock){ + $tile = ServerAPI::request()->api->tile->get($target); + if($tile instanceof Tile){ + $tile->data["EntityId"] = $this->meta; + //TODO update tile somehow + }else{ + ConsoleAPI::warn("No tile was found at $target!"); + } + return false; + } + $ageable = $this->meta === MOB_CHICKEN || $this->meta === MOB_COW || $this->meta === MOB_SHEEP || $this->meta === MOB_PIG; $data = array( "x" => $block->x + 0.5, diff --git a/src/world/Level.php b/src/world/Level.php index 253a6a4a..a9dff4b0 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -226,7 +226,6 @@ class Level{ $tileEntities .= $nbt->write(); break; case TILE_MOB_SPAWNER: - console("mobspawner spawn {$tile->data["EntityId"]}"); $nbt->setData(new Compound("", array( new StringTag("id", TILE_MOB_SPAWNER), new IntTag("x", (int) $tile->x), diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index 4ef253ba..c800abe1 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -185,7 +185,7 @@ class NormalGenerator implements NewLevelGenerator{ } } foreach($this->populators as $populator){ - $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); //ty shoghicp for 250k bytes of randomness + $this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed()); //ty shoghicp for 250k bytes of randomness (where ~65536 are usable) $populator->populate($this->level, $chunkX, $chunkZ, $this->random); } } diff --git a/src/world/generator/populator/DesertWellPopulator.php b/src/world/generator/populator/DesertWellPopulator.php deleted file mode 100644 index e69de29b..00000000 diff --git a/src/world/generator/populator/ForestRockPopulator.php b/src/world/generator/populator/ForestRockPopulator.php deleted file mode 100644 index e69de29b..00000000 diff --git a/src/world/generator/populator/VillagePopulator.php b/src/world/generator/populator/VillagePopulator.php deleted file mode 100644 index e69de29b..00000000 From 284910734fc4326a170cce54efa2289bd56b063d Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 2 Dec 2023 14:16:27 +0300 Subject: [PATCH 34/43] fixed superflat --- src/world/generator/NormalGenerator.php | 2 +- src/world/generator/SuperflatGenerator.php | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index c800abe1..836b0dbc 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -151,9 +151,9 @@ class NormalGenerator implements NewLevelGenerator{ $chunk .= "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; //skylight/light } } - $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, $biomes); $this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk); } + $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, $biomes); if(self::HIDDEN_FEATURES) $this->caveGenerator->generate($this->level, $chunkX, $chunkZ); //TODO speedup } diff --git a/src/world/generator/SuperflatGenerator.php b/src/world/generator/SuperflatGenerator.php index 59590690..87691e1a 100644 --- a/src/world/generator/SuperflatGenerator.php +++ b/src/world/generator/SuperflatGenerator.php @@ -7,7 +7,7 @@ require_once("LevelGenerator.php"); class SuperflatGenerator implements LevelGenerator{ private $level, $random, $structure, $chunks, $options, $floorLevel, $populators = []; - + public $preset; public function __construct(array $options = []){ $this->preset = "2;7,59x1,3x3,2;1;spawn(radius=10 block=89),decoration(treecount=80 grasscount=45)"; $this->options = $options; @@ -78,9 +78,9 @@ class SuperflatGenerator implements LevelGenerator{ $metas = ""; for($y = $startY; $y < $endY; ++$y){ $blocks .= chr($this->structure[$y]->getID()); - $metas .= substr(dechex($this->structure[$y]->getMetadata()), -1); + $metas .= chr($this->structure[$y]->getMetadata()); } - $this->chunks[$Y] .= $blocks . hex2bin($metas) . "\x00\x00\x00\x00\x00\x00\x00\x00"; + $this->chunks[$Y] .= $blocks . $metas . "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" . "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; } } } @@ -111,6 +111,7 @@ class SuperflatGenerator implements LevelGenerator{ for($Y = 0; $Y < 8; ++$Y){ $this->level->setMiniChunk($chunkX, $chunkZ, $Y, $this->chunks[$Y]); } + $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, str_repeat(chr(BIOME_PLAINS), 256)); } public function populateChunk($chunkX, $chunkZ){ From 4715d938e15b679b0cf21b0fae81db428a28ed44 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 2 Dec 2023 17:20:03 +0300 Subject: [PATCH 35/43] final blocks --- src/API/BlockAPI.php | 4 +++- src/material/Block.php | 4 ++-- .../block/solid/BrownMushroomBlock.php | 19 ------------------- .../block/solid/BrownMushroomSolidBlock.php | 14 ++++++++++++++ src/material/block/solid/RedMushroomBlock.php | 19 ------------------- .../block/solid/RedMushroomSolidBlock.php | 14 ++++++++++++++ 6 files changed, 33 insertions(+), 41 deletions(-) delete mode 100644 src/material/block/solid/BrownMushroomBlock.php create mode 100644 src/material/block/solid/BrownMushroomSolidBlock.php delete mode 100644 src/material/block/solid/RedMushroomBlock.php create mode 100644 src/material/block/solid/RedMushroomSolidBlock.php diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 2f3a04aa..f71346c1 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -81,7 +81,9 @@ class BlockAPI{ [ICE, 0], [SNOW_BLOCK, 0], [BROWN_MUSHROOM_BLOCK, 0], - [RED_MUSHROOM_BLOCK, 0], + [BROWN_MUSHROOM_BLOCK, 14], + [BROWN_MUSHROOM_BLOCK, 15], + [RED_MUSHROOM_BLOCK, 14], [MYCELIUM, 0], [PODZOL, 0], [STAINED_CLAY, 0], diff --git a/src/material/Block.php b/src/material/Block.php index 80c83ea9..4ea821be 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -143,8 +143,8 @@ abstract class Block extends Position{ LEAVES2 => "Leaves2Block", END_PORTAL_FRAME => "EndPortalFrameBlock", COCOA => "CocoaBlock", - RED_MUSHROOM_BLOCK => "RedMushroomBlock", - BROWN_MUSHROOM_BLOCK => "BrownMushroomBlock", + RED_MUSHROOM_BLOCK => "RedMushroomSolidBlock", + BROWN_MUSHROOM_BLOCK => "BrownMushroomSolidBlock", MONSTER_SPAWNER => "MonsterSpawnerBlock", VINES => "VinesBlock", MYCELIUM => "MyceliumBlock", diff --git a/src/material/block/solid/BrownMushroomBlock.php b/src/material/block/solid/BrownMushroomBlock.php deleted file mode 100644 index dbbd1750..00000000 --- a/src/material/block/solid/BrownMushroomBlock.php +++ /dev/null @@ -1,19 +0,0 @@ -hardness = 0.2; - } - - public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - //todo - $this->level->setBlock($this, $this, true, false, true); - } - - public function getDrops(Item $item, Player $player){ - return [ - [BROWN_MUSHROOM, 0, mt_rand(0, 2)] - ]; - } -} \ No newline at end of file diff --git a/src/material/block/solid/BrownMushroomSolidBlock.php b/src/material/block/solid/BrownMushroomSolidBlock.php new file mode 100644 index 00000000..f8a5f737 --- /dev/null +++ b/src/material/block/solid/BrownMushroomSolidBlock.php @@ -0,0 +1,14 @@ +hardness = 0.2; - } - - public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - //todo - $this->level->setBlock($this, $this, true, false, true); - } - - public function getDrops(Item $item, Player $player){ - return [ - [RED_MUSHROOM, 0, mt_rand(0, 2)] - ]; - } -} \ No newline at end of file diff --git a/src/material/block/solid/RedMushroomSolidBlock.php b/src/material/block/solid/RedMushroomSolidBlock.php new file mode 100644 index 00000000..075c9a6d --- /dev/null +++ b/src/material/block/solid/RedMushroomSolidBlock.php @@ -0,0 +1,14 @@ + Date: Sun, 3 Dec 2023 15:45:34 +0300 Subject: [PATCH 36/43] tried porting structure api from 1.5.2 --- src/dependencies.php | 2 + src/math/AxisAlignedBB.php | 27 +- src/math/StructureAABB.php | 18 ++ src/pmf/PMFLevel.php | 6 +- src/utils/Utils.php | 4 + src/world/generator/NormalGenerator.php | 9 +- src/world/generator/cave/CaveGenerator.php | 22 +- .../generator/cave/MineshaftGenerator.php | 19 ++ .../generator/structure/StructureBase.php | 28 ++ .../structure/StructureComponent.php | 301 ++++++++++++++++++ .../structure/StructureGenerator.php | 39 +++ .../generator/structure/StructureStart.php | 54 ++++ .../mineshaft/StructureMineshaftPieces.php | 43 +++ .../mineshaft/StructureMineshaftStart.php | 13 + .../component/ComponentMineshaftRoom.php | 91 ++++++ 15 files changed, 649 insertions(+), 27 deletions(-) create mode 100644 src/math/StructureAABB.php create mode 100644 src/world/generator/cave/MineshaftGenerator.php create mode 100644 src/world/generator/structure/StructureBase.php create mode 100644 src/world/generator/structure/StructureComponent.php create mode 100644 src/world/generator/structure/StructureGenerator.php create mode 100644 src/world/generator/structure/StructureStart.php create mode 100644 src/world/generator/structure/mineshaft/StructureMineshaftPieces.php create mode 100644 src/world/generator/structure/mineshaft/StructureMineshaftStart.php create mode 100644 src/world/generator/structure/mineshaft/component/ComponentMineshaftRoom.php diff --git a/src/dependencies.php b/src/dependencies.php index aff3def1..85b6a25a 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -128,6 +128,8 @@ require_once(FILE_PATH . "/src/structure/Structure.php"); require_once(FILE_PATH . "/src/world/generator/Populator.php"); require_once(FILE_PATH . "/src/world/generator/LevelGenerator.php"); +require_once(FILE_PATH . "/src/world/generator/structure/StructureBase.php"); +require_once(FILE_PATH . "/src/world/generator/structure/StructureGenerator.php"); require_once(FILE_PATH . "/src/plugin/Plugin.php"); require_once(FILE_PATH . "/src/plugin/OtherPluginRequirement.php"); require_once(FILE_PATH . "/src/plugin/DummyPlugin.php"); diff --git a/src/math/AxisAlignedBB.php b/src/math/AxisAlignedBB.php index f32b77ea..862651f1 100644 --- a/src/math/AxisAlignedBB.php +++ b/src/math/AxisAlignedBB.php @@ -60,7 +60,7 @@ class AxisAlignedBB{ public function grow($x, $y, $z){ return new AxisAlignedBB($this->minX - $x, $this->minY - $y, $this->minZ - $z, $this->maxX + $x, $this->maxY + $y, $this->maxZ + $z); } - + public function expand($x, $y, $z){ $this->minX -= $x; $this->minY -= $y; @@ -71,7 +71,8 @@ class AxisAlignedBB{ return $this; } - + + public function offset($x, $y, $z){ $this->minX += $x; $this->minY += $y; @@ -305,6 +306,28 @@ class AxisAlignedBB{ return MovingObjectPosition::fromBlock(0, 0, 0, $f, $vector); } + public function expandTo(AxisAlignedBB $bb){ + $this->minX = min($this->minX, $bb->minX); + $this->minY = min($this->minY, $bb->minY); + $this->minZ = min($this->minZ, $bb->minZ); + $this->maxX = max($this->maxX, $bb->maxX); + $this->maxY = max($this->maxY, $bb->maxY); + $this->maxZ = max($this->maxZ, $bb->maxZ); + } + + public function getXSize(){ + return $this->maxX - $this->minX + 1; + } + + public function getYSize(){ + return $this->maxY - $this->minY + 1; + } + + public function getZSize(){ + return $this->maxZ - $this->minZ + 1; + } + + public function __toString(){ return "min: {$this->minX}, {$this->minY}, {$this->minZ}, max: {$this->maxX}, {$this->maxY}, {$this->maxZ}"; } diff --git a/src/math/StructureAABB.php b/src/math/StructureAABB.php new file mode 100644 index 00000000..f85f8709 --- /dev/null +++ b/src/math/StructureAABB.php @@ -0,0 +1,18 @@ +intersectsWith($bb->minX, $bb->minZ, $bb->maxX, $bb->maxZ); + } + return $this->maxX >= $bb && $this->minX <= $maxX && $this->maxZ >= $minZ && $this->minZ <= $maxZ; + } + +} + diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 2bb035d0..7ced9e97 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -742,7 +742,7 @@ class PMFLevel extends PMF{ $this->createUnpopulatedChunk($X, $Z); } } - if($this->chunks[$index][$Y] === false){ + if(!isset($this->chunks[$index][$Y]) || $this->chunks[$index][$Y] === false){ $this->fillMiniChunk($X, $Z, $Y); } $aX = $x - ($X << 4); @@ -750,8 +750,8 @@ class PMFLevel extends PMF{ $aY = $y - ($Y << 4); $bindex = (int) ($aY + ($aX << 6) + ($aZ << 10)); $mindex = (int) (($aY >> 1) + 16 + ($aX << 6) + ($aZ << 10)); - $old_b = ord($this->chunks[$index][$Y][$bindex]); - $old_m = ord($this->chunks[$index][$Y][$mindex]); + $old_b = ord($this->chunks[$index][$Y][$bindex] ?? '\x00'); + $old_m = ord($this->chunks[$index][$Y][$mindex] ?? '\x00'); if(($y & 1) === 0){ $m = ($old_m & 0xF0) | $meta; }else{ diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 910a4e0e..2bb6afce 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -45,6 +45,10 @@ class Utils{ return $angle < 0 ? $angle + 360 : $angle; } + public static function chunkPos2Int($x, $z){ + return ($x & 0xFFFFFFFF) | (($z & 0xFFFFFFFF) << 32); + } + public static function wrapAngleTo180($angle) { $angle = fmod($angle+180, 360); diff --git a/src/world/generator/NormalGenerator.php b/src/world/generator/NormalGenerator.php index 836b0dbc..f259aa93 100644 --- a/src/world/generator/NormalGenerator.php +++ b/src/world/generator/NormalGenerator.php @@ -17,6 +17,7 @@ class NormalGenerator implements NewLevelGenerator{ private $noiseBase; private $biomeSelector; private $caveGenerator; + private $mineshaftGenerator; public function __construct(array $options = array()){ } @@ -65,6 +66,7 @@ class NormalGenerator implements NewLevelGenerator{ $tallGrass->setRandomAmount(0); $this->populators[] = $tallGrass; $this->caveGenerator = new CaveGenerator($this->level->getSeed()); + $this->mineshaftGenerator = new MineshaftGenerator($this->level->getSeed()); } public function pickBiome(int $x, int $z){ @@ -154,7 +156,10 @@ class NormalGenerator implements NewLevelGenerator{ $this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk); } $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, $biomes); - if(self::HIDDEN_FEATURES) $this->caveGenerator->generate($this->level, $chunkX, $chunkZ); //TODO speedup + if(self::HIDDEN_FEATURES) { + $this->caveGenerator->generate($this->level, $chunkX, $chunkZ); //TODO speedup + $this->mineshaftGenerator->generate($this->level, $chunkX, $chunkZ); + } } public function populateChunk($chunkX, $chunkZ){ @@ -175,6 +180,8 @@ class NormalGenerator implements NewLevelGenerator{ } if(self::HIDDEN_FEATURES) { + //this.mineshaftGenerator.generateStructuresInChunk(this.worldObj, this.rand, par2, par3); + $this->mineshaftGenerator->generateStructuresInChunk($this->level, $this->mtrandom, $chunkX, $chunkZ); for ($i = 0; $i < 8; ++$i){ $x = $blockX + $this->mtrandom->nextInt(16) + 8; $y = $this->mtrandom->nextInt(128); diff --git a/src/world/generator/cave/CaveGenerator.php b/src/world/generator/cave/CaveGenerator.php index 2f1219e4..38acbd7a 100644 --- a/src/world/generator/cave/CaveGenerator.php +++ b/src/world/generator/cave/CaveGenerator.php @@ -1,27 +1,7 @@ rand = new MTRandom($seed); - } - - public function generate(Level $level, $chunkX, $chunkZ){ - $range = $this->range; - $this->rand->setSeed($level->getSeed()); - $seedInt1 = $this->rand->nextInt(); - $seedInt2 = $this->rand->nextInt(); - - for($x = $chunkX - $range; $x <= $chunkX + $range; ++$x){ - for($z = $chunkZ - $range; $z <= $chunkZ + $range; ++$z){ - $this->rand->setSeed((($x * $seedInt1) + ($z * $seedInt2)) ^ $level->getSeed()); - $this->recursiveGenerate($level, $x, $z, $chunkX, $chunkZ); - } - } - } public function recursiveGenerate(Level $level, $chunkXoffsetted, $chunkZoffsetted, $chunkX, $chunkZ){ $nextInt = $this->rand->nextInt($this->rand->nextInt($this->rand->nextInt(40) + 1) + 1); diff --git a/src/world/generator/cave/MineshaftGenerator.php b/src/world/generator/cave/MineshaftGenerator.php new file mode 100644 index 00000000..1e198290 --- /dev/null +++ b/src/world/generator/cave/MineshaftGenerator.php @@ -0,0 +1,19 @@ +rand->nextFloat() < $this->spawnChance && $this->rand->nextInt(80) < max(abs($chunkX), abs($chunkZ)); + if($v) ConsoleAPI::debug("u can spawn mineshaft at $chunkX $chunkZ"); + return $v; + } + + public function getStructureStart(Level $level, $chunkX, $chunkZ) + { + return new StructureMineshaftStart($level, $this->rand, $chunkX, $chunkZ); + } +} + diff --git a/src/world/generator/structure/StructureBase.php b/src/world/generator/structure/StructureBase.php new file mode 100644 index 00000000..7e8222c5 --- /dev/null +++ b/src/world/generator/structure/StructureBase.php @@ -0,0 +1,28 @@ +rand = new MTRandom(); + } + + public function generate(Level $level, $chunkX, $chunkZ){ + $range = $this->range; + $this->rand->setSeed($level->getSeed()); + $seedInt1 = $this->rand->nextInt(); + $seedInt2 = $this->rand->nextInt(); + + for($x = $chunkX - $range; $x <= $chunkX + $range; ++$x){ + for($z = $chunkZ - $range; $z <= $chunkZ + $range; ++$z){ + $this->rand->setSeed((($x * $seedInt1) + ($z * $seedInt2)) ^ $level->getSeed()); + $this->recursiveGenerate($level, $x, $z, $chunkX, $chunkZ); + } + } + } + + public function recursiveGenerate(Level $level, $chunkXoffsetted, $chunkZoffsetted, $chunkX, $chunkZ){} +} + diff --git a/src/world/generator/structure/StructureComponent.php b/src/world/generator/structure/StructureComponent.php new file mode 100644 index 00000000..72806380 --- /dev/null +++ b/src/world/generator/structure/StructureComponent.php @@ -0,0 +1,301 @@ +componentType = $type; + } + + public function buildComponent(StructureComponent $component, $aList, MTRandom $random){} + public abstract function addComponentParts(Level $level, MTRandom $random, AxisAlignedBB $boundingBox): bool; + + /** + * @param StructureComponent[] $aList + * @param AxisAlignedBB $boundingBox + */ + public static function findIntersecting($aList, AxisAlignedBB $boundingBox){ + foreach($aList as $component){ + if($component->boundingBox != null && $component->boundingBox->intersectsWith($boundingBox)){ + return $component; + } + } + return null; + } + + public function getCenter(){ + return new Vector3( + (int)($this->boundingBox->minX + (($this->boundingBox->maxX - $this->boundingBox->minX + 1) / 2)), + (int)($this->boundingBox->minY + (($this->boundingBox->maxY - $this->boundingBox->minY + 1) / 2)), + (int)($this->boundingBox->minZ + (($this->boundingBox->maxZ - $this->boundingBox->minZ + 1) / 2)), + ); + } + public function isLiquidInStructureBoundingBox(Level $level, AxisAlignedBB $boundingBox){ + $minX = max($boundingBox->minX - 1, $boundingBox->minX); //TODO optimize? + $minY = max($boundingBox->minY - 1, $boundingBox->minY); + $minZ = max($boundingBox->minZ - 1, $boundingBox->minZ); + $maxX = min($boundingBox->maxX + 1, $boundingBox->maxX); + $maxY = min($boundingBox->maxY + 1, $boundingBox->maxY); + $maxZ = min($boundingBox->maxZ + 1, $boundingBox->maxZ); + + for($x = $minX; $x <= $maxX; ++$x){ + for($z = $minZ; $z <= $maxZ; ++$z){ + $blockID = $level->level->getBlockID($x, $minY, $z); + if($blockID > 0 && StaticBlock::getIsLiquid($blockID)) return true; + $blockID = $level->level->getBlockID($x, $maxY, $z); + if($blockID > 0 && StaticBlock::getIsLiquid($blockID)) return true; + } + } + + for($x = $minX; $x <= $maxX; ++$x){ + for($y = $minY; $y <= $maxY; ++$y){ + $blockID = $level->level->getBlockID($x, $y, $minZ); + if($blockID > 0 && StaticBlock::getIsLiquid($blockID)) return true; + $blockID = $level->level->getBlockID($x, $y, $maxZ); + if($blockID > 0 && StaticBlock::getIsLiquid($blockID)) return true; + } + } + + + for($z = $minZ; $z <= $maxZ; ++$z){ + for($y = $minY; $y <= $maxY; ++$y){ + $blockID = $level->level->getBlockID($minX, $y, $z); + if($blockID > 0 && StaticBlock::getIsLiquid($blockID)) return true; + $blockID = $level->level->getBlockID($maxX, $y, $z); + if($blockID > 0 && StaticBlock::getIsLiquid($blockID)) return true; + } + } + return false; + } + + public function getXWithOffset($par1, $par2){ + return match($this->coordBaseMode){ + 0, 2 => $this->boundingBox->minX + $par1, + 1 => $this->boundingBox->maxX - $par2, + 3 => $this->boundingBox->minX + $par2, + default => $par1 + }; + } + public function getYWithOffset($par1){ + return $this->coordBaseMode == -1 ? $par1 : $par1 + $this->boundingBox->minY; + } + public function getZWithOffset($par1, $par2){ + return match($this->coordBaseMode){ + 0 => $this->boundingBox->minZ + $par2, + 1, 3 => $this->boundingBox->minZ + $par1, + 2 => $this->boundingBox->maxZ - $par2, + default => $par2 + }; + } + public function getMetadataWithOffset($blockID, $blockMeta){ + switch($blockID){ + case WOODEN_DOOR_BLOCK: + case IRON_DOOR_BLOCK: + switch($this->coordBaseMode){ + case 0: + return match($blockMeta){ + 0 => 2, + 2 => 0, + default => $blockMeta + }; + case 1: + return $blockMeta + 1 & 3; + case 3: + return $blockMeta + 3 & 3; + } + + break; + case COBBLESTONE_STAIRS: + case WOODEN_STAIRS: + case NETHER_BRICKS_STAIRS: + case STONE_BRICK_STAIRS: + case SANDSTONE_STAIRS: + switch($this->coordBaseMode){ + case 0: + return match($blockMeta){ + 2 => 3, + 3 => 2, + default => $blockMeta + }; + case 1: + return match($blockMeta){ + 0 => 2, + 1 => 3, + 2 => 0, + 3 => 1, + default => $blockMeta + }; + case 3: + return match($blockMeta){ + 0 => 2, + 1 => 3, + 2 => 1, + 3 => 0, + default => $blockMeta + }; + } + break; + case RAIL: + if($this->coordBaseMode == 1 || $this->coordBaseMode == 3){ + return $blockMeta == 1 ? 0 : 1; + } + break; + case LADDER: + switch($this->coordBaseMode){ + case 0: + return match($blockMeta){ + 2 => 3, + 3 => 2, + default => $blockMeta + }; + case 1: + return match($blockMeta){ + 2 => 4, + 3 => 5, + 4 => 2, + 5 => 3, + default => $blockMeta + }; + case 3: + return match($blockMeta){ + 2 => 5, + 3 => 4, + 4 => 2, + 5 => 3, + default => $blockMeta + }; + } + //case stone_button, not needed + //case pistonbase, pistonstickybase, level, dispenser, not needed + + } + /* TODO directional blocks, tripwire source(not needed prob) + * if (this.coordBaseMode == 0) + { + if (blockMeta == 0 || blockMeta == 2) return Direction.rotateOpposite[blockMeta]; + } + else if (this.coordBaseMode == 1) + { + if (blockMeta == 2) return 1; + + if (blockMeta == 0) return 3; + + if (blockMeta == 1) return 2; + + if (blockMeta == 3) return 0; + } + else if (this.coordBaseMode == 3) + { + if (blockMeta == 2) return 3; + if (blockMeta == 0) return 1; + if (blockMeta == 1) return 2; + if (blockMeta == 3) return 0; + } + */ + return $blockMeta; + } + + public function placeBlockAtCurrentPosition(Level $level, $blockID, $blockMeta, $par4, $par5, $par6, AxisAlignedBB $boundingBox){ + $x = $this->getXWithOffset($par4, $par6); + $y = $this->getYWithOffset($par5); + $z = $this->getZWithOffset($par4, $par6); + + if($boundingBox->isXYZInside($x, $y, $z)){ + $level->fastSetBlockUpdate($x, $y, $z, $blockID, $blockMeta); + } + } + + public function getBlockIdAtCurrentPosition(Level $level, $par2, $par3, $par4, AxisAlignedBB $boundingBox){ + $x = $this->getXWithOffset($par2, $par4); + $y = $this->getYWithOffset($par3); + $z = $this->getZWithOffset($par2, $par4); + return !$boundingBox->isXYZInside($x, $y, $z) ? 0 : $level->level->getBlockID($x, $y, $z); + } + + public function fillWithAir(Level $level, AxisAlignedBB $boundingBox, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + for($y = $minY; $y <= $maxY; ++$y){ + for($x = $minX; $x <= $maxX; ++$x){ + for($z = $minZ; $z < $maxZ; ++$z){ + $this->placeBlockAtCurrentPosition($level, AIR, 0, $x, $y, $z, $boundingBox); + } + } + } + } + + public function fillWithBlocks(Level $level, AxisAlignedBB $boundingBox, $minX, $minY, $minZ, $maxX, $maxY, $maxZ, $blockID, $replaceID, $alwaysReplace){ + for($y = $minY; $y <= $maxY; ++$y){ + for($x = $minX; $x <= $maxX; ++$x){ + for($z = $minZ; $z < $maxZ; ++$z){ + if(!$alwaysReplace || $this->getBlockIdAtCurrentPosition($level, $x, $y, $z, $boundingBox) != 0){ + if($y != $minY && $y != $maxY && $x != $minX && $x != $maxX && $z != $minZ && $z != $maxZ){ + $this->placeBlockAtCurrentPosition($level, $replaceID, 0, $x, $y, $z, $boundingBox); + }else{ + $this->placeBlockAtCurrentPosition($level, $blockID, 0, $x, $y, $z, $boundingBox); + } + } + } + } + } + } + + public function fillWithMetadataBlocks(){throw new Exception("Not Implemented");} //TODO it is too long + not used for mineshafts i want now + public function fillWithRandomizedBlocks(){throw new Exception("Not Implemented");} //TODO it is too long + not used for mineshafts i want now + + public function randomlyFillWithBlocks(Level $level, AxisAlignedBB $boundingBox, MTRandom $random, $randomChance, $minX, $minY, $minZ, $maxX, $maxY, $maxZ, $blockID, $replaceID, $alwaysReplace){ + for($y = $minY; $y <= $maxY; ++$y){ + for($x = $minX; $x <= $maxX; ++$x){ + for($z = $minZ; $z < $maxZ; ++$z){ + if($random->nextFloat() <= $randomChance && (!$alwaysReplace || $this->getBlockIdAtCurrentPosition($level, $x, $y, $z, $boundingBox) != 0)){ + if($y != $minY && $y != $maxY && $x != $minX && $x != $maxX && $z != $minZ && $z != $maxZ){ + $this->placeBlockAtCurrentPosition($level, $replaceID, 0, $x, $y, $z, $boundingBox); + }else{ + $this->placeBlockAtCurrentPosition($level, $blockID, 0, $x, $y, $z, $boundingBox); + } + } + } + } + } + } + + public function randomlyPlaceBlock(Level $level, AxisAlignedBB $boundingBox, MTRandom $random, $randomChance, $x, $y, $z, $blockID, $blockMeta){ + if($random->nextFloat() < $randomChance) $this->placeBlockAtCurrentPosition($level, $blockID, $blockMeta, $x, $y, $z, $boundingBox); + } + + public function randomlyRareFillWithBlocks(Level $level, AxisAlignedBB $boundingBox, $minX, $minY, $minZ, $maxX, $maxY, $maxZ, $blockID, $alwaysreplace){ + $v11 = ($maxX - $minX + 1); + $v12 = ($maxY - $minY + 1); + $v13 = ($maxZ - $minZ + 1); + $v14 = $minX + $v11 / 2; + $v15 = $minZ + $v13 / 2; + + for($y = $minY; $y <= $maxY; ++$y){ + $v17 = ($y - $minY) / $v12; + for($x = $minX; $x <= $maxX; ++$x){ + $v19 = ($x - $v14) / ($v11 * 0.5); + for($z = $minZ; $z <= $maxZ; ++$z){ + $v21 = ($z - $v15) / ($v13 * 0.5); + + if(!$alwaysreplace || $this->getBlockIdAtCurrentPosition($level, $x, $y, $z, $boundingBox) != 0){ + $v22 = $v19*$v19 + $v17*$v17 + $v21*$v21; + + if($v22 <= 1.05){ + $this->placeBlockAtCurrentPosition($level, $blockID, 0, $x, $y, $z, $boundingBox); + } + } + + } + } + } + } + + public function clearCurrentPositionBlocksUpwards(){throw new Exception("Not Implemented");} //TODO implement, used for villages + public function fillCurrentPositionBlocksDownwards(){throw new Exception("Not Implemented");} //TODO implement, used for villages + public function generateStructureChestContents(){throw new Exception("Not Implemented");} //TODO implement + public function placeDoorAtCurrentPosition(){throw new Exception("Not Implemented");} //TODO implement + +} + diff --git a/src/world/generator/structure/StructureGenerator.php b/src/world/generator/structure/StructureGenerator.php new file mode 100644 index 00000000..71a7ca9e --- /dev/null +++ b/src/world/generator/structure/StructureGenerator.php @@ -0,0 +1,39 @@ +structureMap[$chunkXZ])){ + $this->rand->nextInt(); //shouldnt be neccessary, but vanilla has it. + + if($this->canSpawnStructureAt($chunkXoffsetted, $chunkZoffsetted)){ + $structureStrat = $this->getStructureStart($level, $chunkXoffsetted, $chunkZoffsetted); + $this->structureMap[$chunkXZ] = $structureStrat; + } + + } + } + + public function generateStructuresInChunk(Level $level, MTRandom $random, $chunkX, $chunkZ){ + $chunkXCenter = ($chunkX << 4) + 8; + $chunkZCenter = ($chunkZ << 4) + 8; + $var7 = false; + foreach($this->structureMap as $start){ + if($start->isSizeableStructure() && $start->boundingBox->intersectsWith($chunkXCenter, $chunkZCenter, $chunkXCenter + 15, $chunkZCenter + 15)){ + $start->generateStructure($level, $random, new StructureAABB($chunkXCenter, 1, $chunkZCenter, $chunkXCenter + 15, 512, $chunkZCenter + 15)); + $var7 = true; + } + } + return $var7; + } + + public abstract function canSpawnStructureAt($chunkX, $chunkZ); + public abstract function getStructureStart(Level $level, $chunkX, $chunkZ); +} + diff --git a/src/world/generator/structure/StructureStart.php b/src/world/generator/structure/StructureStart.php new file mode 100644 index 00000000..36d3eb17 --- /dev/null +++ b/src/world/generator/structure/StructureStart.php @@ -0,0 +1,54 @@ +components as $k => $component){ + if($component->boundingBox->intersectsWith($boundingBox) && !$component->addComponentParts($level, $random, $boundingBox)){ + unset($this->components[$k]); + } + } + } + + public function updateBoundingBox(){ + $this->boundingBox = new StructureAABB(0, 0, 0, 0, 0, 0); + foreach($this->components as $k => $component){ + $this->boundingBox->expandTo($component->boundingBox); + } + } + + public function markAvailableHeight(Level $level, MTRandom $random, $height){ + $var4 = 63 - $height; + $var5 = $this->boundingBox->getYSize() + 1; + + if($var5 < $var4) $var5 += $random->nextInt($var4 - $var5); + + $var6 = (int)($var5 - $this->boundingBox->maxY); + $this->boundingBox->offset(0, $var6, 0); + + foreach($this->components as $component){ + $component->boundingBox->offset(0, $var6, 0); + } + } + + public function setRandomHeight(Level $level, MTRandom $random, $par3, $par4){ + $var5 = (int)($par4 - $par3 + 1 - $this->boundingBox->getYSize()); + $var10 = $var5 > 1 ? $par3 + $random->nextInt($var5) : $par3; + $var6 = (int)($var10 - $this->boundingBox->minY); + $this->boundingBox->offset(0, $var6, 0); + foreach($this->components as $component){ + $component->boundingBox->offset(0, $var6, 0); + } + } + + public function isSizeableStructure(){ + return true; + } +} + diff --git a/src/world/generator/structure/mineshaft/StructureMineshaftPieces.php b/src/world/generator/structure/mineshaft/StructureMineshaftPieces.php new file mode 100644 index 00000000..ed004585 --- /dev/null +++ b/src/world/generator/structure/mineshaft/StructureMineshaftPieces.php @@ -0,0 +1,43 @@ +nextInt(100); + + /*if($var7 >= 80){ + $var8 = ComponentMineshaftCross //TODO + if($var8 != null) return new ComponentMineshaftCross($par6, $random, $var8, $par5); + }else if($var7 >= 70){ + $var8 = ComponentMineshaftStairs //TODO + if($var8 != null) return new ComponentMineshaftStairs($par6, $random, $var8, $par5); + }else{ + $var8 = ComponentMineshaftCorridor //TODO + if($var8 != null) return new ComponentMineshaftCorridor($par6, $random, $var8, $par5); + }*/ + + return null; + + } + + public static function getNextMineShaftComponent(StructureComponent $component, $aList, MTRandom $random, $par3, $par4, $par5, $par6, $par7){ + if($par7 > 8) return null; + + if(abs($par3 - $component->boundingBox->minX) <= 80 && abs($par5 - $component->boundingBox->minZ) <= 80){ + $var8 = self::getRandomComponent($aList, $random, $par3, $par4, $par5, $par6, $par7 + 1); + if($var8 != null){ + $aList[] = $var8; + $var8->buildComponent($component, $aList, $random); + } + return $var8; + } + + return null; + } +} + diff --git a/src/world/generator/structure/mineshaft/StructureMineshaftStart.php b/src/world/generator/structure/mineshaft/StructureMineshaftStart.php new file mode 100644 index 00000000..f31a715b --- /dev/null +++ b/src/world/generator/structure/mineshaft/StructureMineshaftStart.php @@ -0,0 +1,13 @@ +components[] = $var5; + $var5->buildComponent($var5, $this->components, $random); + $this->updateBoundingBox(); + $this->markAvailableHeight($level, $random, 10); + } +} + diff --git a/src/world/generator/structure/mineshaft/component/ComponentMineshaftRoom.php b/src/world/generator/structure/mineshaft/component/ComponentMineshaftRoom.php new file mode 100644 index 00000000..4d67d413 --- /dev/null +++ b/src/world/generator/structure/mineshaft/component/ComponentMineshaftRoom.php @@ -0,0 +1,91 @@ +boundingBox = new AxisAlignedBB($par3, 50, $par4, $par3 + 7 + $random->nextInt(6), $par4 + 7, $par4 + 7 + $random->nextInt(6)); + } + + public function buildComponent(StructureComponent $component, $aList, MTRandom $random){ + $var4 = $this->componentType; + $var6 = $this->boundingBox->getYSize() - 3 - 1; + if($var6 <= 0) $var6 = 1; + + for($var5 = 0; $var5 < $this->boundingBox->getXSize(); $var5 += 4){ + $var5 += $random->nextInt($this->boundingBox->getXSize()); + if($var5 + 3 < $this->boundingBox->getXSize()) break; + //var7 = StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX + var5, this.boundingBox.minY + par3Random.nextInt(var6) + 1, this.boundingBox.minZ - 1, 2, var4); + $var7 = StructureMineshaftPieces::getNextMineShaftComponent($component, $aList, $random, $this->boundingBox->minX + $var5, $this->boundingBox->minY + $random->nextInt($var6) + 1, $this->boundingBox->minZ - 1, 2, $var4); + + if($var7 != null){ + $var8 = $var7->boundingBox; + //this.roomsLinkedToTheRoom.add(new StructureBoundingBox(var8.minX, var8.minY, this.boundingBox.minZ, var8.maxX, var8.maxY, this.boundingBox.minZ + 1)); + $this->roomsLinkedToTheRoom[] = new StructureAABB($var8->minX, $var8->minY, $this->boundingBox->minZ, $var8->maxX, $var8->maxY, $this->boundingBox->minZ + 1); + } + } + + for($var5 = 0; $var5 < $this->boundingBox->getXSize(); $var5 += 4){ + $var5 += $random->nextInt($this->boundingBox->getXSize()); + if($var5 + 3 < $this->boundingBox->getXSize()) break; + //var7 = StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX + var5, this.boundingBox.minY + par3Random.nextInt(var6) + 1, this.boundingBox.maxZ + 1, 0, var4); + $var7 = StructureMineshaftPieces::getNextMineShaftComponent($component, $aList, $random, $this->boundingBox->minX + $var5, $this->boundingBox->minY + $random->nextInt($var6) + 1, $this->boundingBox->maxZ + 1, 0, $var4); + + if($var7 != null){ + $var8 = $var7->boundingBox; + //this.roomsLinkedToTheRoom.add(new StructureBoundingBox(var8.minX, var8.minY, this.boundingBox.maxZ - 1, var8.maxX, var8.maxY, this.boundingBox.maxZ)); + $this->roomsLinkedToTheRoom[] = new StructureAABB($var8->minX, $var8->minY, $this->boundingBox->maxZ - 1, $var8->maxX, $var8->maxY, $this->boundingBox->maxZ); + } + } + + for($var5 = 0; $var5 < $this->boundingBox->getZSize(); $var5 += 4){ + $var5 += $random->nextInt($this->boundingBox->getZSize()); + if($var5 + 3 < $this->boundingBox->getZSize()) break; + //var7 = StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX - 1, this.boundingBox.minY + par3Random.nextInt(var6) + 1, this.boundingBox.minZ + var5, 1, var4); + $var7 = StructureMineshaftPieces::getNextMineShaftComponent($component, $aList, $random, $this->boundingBox->minX - 1, $this->boundingBox->minY + $random->nextInt($var6) + 1, $this->boundingBox->minZ + $var5, 1, $var4); + + if($var7 != null){ + $var8 = $var7->boundingBox; + //this.roomsLinkedToTheRoom.add(new StructureBoundingBox(this.boundingBox.minX, var8.minY, var8.minZ, this.boundingBox.minX + 1, var8.maxY, var8.maxZ)); + $this->roomsLinkedToTheRoom[] = new StructureAABB($this->boundingBox->minX, $var8->minY, $var8->minZ, $this->boundingBox->minX + 1, $var8->maxY, $var8->maxZ); + } + } + + for($var5 = 0; $var5 < $this->boundingBox->getZSize(); $var5 += 4){ + $var5 += $random->nextInt($this->boundingBox->getZSize()); + if($var5 + 3 < $this->boundingBox->getZSize()) break; + //var7 = StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.maxX + 1, this.boundingBox.minY + par3Random.nextInt(var6) + 1, this.boundingBox.minZ + var5, 3, var4); + $var7 = StructureMineshaftPieces::getNextMineShaftComponent($component, $aList, $random, $this->boundingBox->maxX + 1, $this->boundingBox->minY + $random->nextInt($var6) + 1, $this->boundingBox->minZ + $var5, 3, $var4); + + if($var7 != null){ + $var8 = $var7->boundingBox; + // this.roomsLinkedToTheRoom.add(new StructureBoundingBox(this.boundingBox.maxX - 1, var8.minY, var8.minZ, this.boundingBox.maxX, var8.maxY, var8.maxZ)); + $this->roomsLinkedToTheRoom[] = new StructureAABB($this->boundingBox->maxX - 1, $var8->minY, $var8->minZ, $this->boundingBox->maxX, $var8->maxY, $var8->maxZ); + } + } + } + + public function addComponentParts(Level $level, MTRandom $random, AxisAlignedBB $boundingBox): bool + { + if($this->isLiquidInStructureBoundingBox($level, $boundingBox)){ + return false; + } + console("Adding Mineshaft start components! $boundingBox"); + $this->fillWithBlocks($level, $boundingBox, $this->boundingBox->minX, $this->boundingBox->minY, $this->boundingBox->minZ, $this->boundingBox->maxX, $this->boundingBox->maxY, $this->boundingBox->maxZ, DIRT, 0, true); + $this->fillWithBlocks($level, $boundingBox, $this->boundingBox->minX, $this->boundingBox->minY + 1, $this->boundingBox->minZ, $this->boundingBox->maxX, min($this->boundingBox->minY + 3, $this->boundingBox->maxY), $this->boundingBox->maxZ, 0, 0, false); + + foreach($this->roomsLinkedToTheRoom as $bb){ + //this.fillWithBlocks(par1World, par3StructureBoundingBox, var5.minX, var5.maxY - 2, var5.minZ, var5.maxX, var5.maxY, var5.maxZ, 0, 0, false); + $this->fillWithBlocks($level, $boundingBox, $bb->minX, $bb->maxY - 2, $bb->minZ, $bb->maxX, $bb->maxY, $bb->maxZ, 0, 0, false); + } + + $this->randomlyRareFillWithBlocks($level, $boundingBox, $this->boundingBox->minX, $this->boundingBox->minY + 4, $this->boundingBox->minZ, $this->boundingBox->maxX, $this->boundingBox->maxY, $this->boundingBox->maxZ, 0, false); + return true; + } +} + From 6f68baf7e2a1bad0d7e65fd306804cdd4592d6e6 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 9 Dec 2023 19:43:29 +0300 Subject: [PATCH 37/43] converter between PMF0 and PMF2 worlds --- src/API/LevelAPI.php | 35 ++++---- src/API/ServerAPI.php | 1 + src/PocketMinecraftServer.php | 2 +- src/pmf/PMFLevel.php | 145 +++++++++++++++++++++++++++++++--- 4 files changed, 150 insertions(+), 33 deletions(-) diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index 45b2e8e7..8ef92340 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -6,7 +6,9 @@ class LevelAPI{ */ public $levels; private $server, $default; - + + public static $defaultLevelType = "DEFAULT"; + public function __construct(){ $this->server = ServerAPI::request(); $this->levels = []; @@ -118,7 +120,17 @@ class LevelAPI{ } return true; } - + + public static function createGenerator($type, $options = []){ + return match($type){ + "FLAT" => new SuperflatGenerator(), + "EXPERIMENTAL" => new ExperimentalGenerator($options), + "HELL", "NETHER" => new HellGenerator($options), + "END" => new EndGenerator($options), + default => new NormalGenerator($options) + }; + } + public function generateLevel($name, $seed = false, $generator = false){ if($this->levelExists($name)){ return false; @@ -132,24 +144,7 @@ class LevelAPI{ $generator = new $generator($options); }else{ $type = strtoupper($this->server->api->getProperty("level-type")); - switch($type){ - case "FLAT": - $generator = new SuperflatGenerator($options); - break; - case "EXPERIMENTAL": - $generator = new ExperimentalGenerator($options); - break; - case "HELL": - case "NETHER": - $generator = new HellGenerator($options); - break; - case "END": - $generator = new EndGenerator($options); - break; - default: - $generator = new NormalGenerator($options); - break; - } + $generator = $this->createGenerator($type, $options); } $gen = new WorldGenerator($generator, $name, $seed === false ? Utils::readInt(Utils::getRandomBytes(4, false)) : (int) $seed); $gen->generate(); diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 739cdfc3..95712d5d 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -143,6 +143,7 @@ class ServerAPI{ Biome::init(); $this->parseProperties(); MobSpawner::$MOB_LIMIT = $this->getProperty("mobs-amount", 50); + LevelAPI::$defaultLevelType = $this->getProperty("level-type"); //Load advanced properties define("DEBUG", $this->getProperty("debug", 1)); define("ADVANCED_CACHE", $this->getProperty("enable-advanced-cache", false)); diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 507b0540..34fa1b4c 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -88,8 +88,8 @@ class PocketMinecraftServer{ "discord-bot-name" => "NostalgiaCore Logger", "despawn-mobs" => true, "mob-despawn-ticks" => 18000, - ]); + Living::$despawnMobs = $this->extraprops->get("despawn-mobs"); Living::$despawnTimer = $this->extraprops->get("mob-despawn-ticks"); Entity::$allowedAI = $this->extraprops->get("enable-mob-ai"); diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 7ced9e97..529f9139 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -120,18 +120,73 @@ class PMFLevel extends PMF{ return dirname($this->file) . "/chunks/" . $Z . "." . $X . ".pmc"; } - public function loadNCPMF1Chunk($X, $Z){ + public function loadNCPMF0Chunk($X, $Z){ $index = $this->getIndex($X, $Z); if($this->isChunkLoaded($X, $Z)){ return true; - }//elseif(!isset($this->locationTable[$index])){ - // return false; - //} + }elseif(!isset($this->locationTable[$index])){ + return false; + } - //$info = $this->locationTable[$index]; + $info = $this->locationTable[$index]; //$this->seek($info[0]); + + $chunk = @gzopen($this->getChunkPath($X, $Z), "rb"); + if($chunk === false){ + return false; + } + $this->chunks[$index] = []; + $this->chunkChange[$index] = [-1 => false]; + $this->chunkInfo[$index][0] = str_repeat(ord(BIOME_PLAINS), 256); + $this->setPopulated($X, $Z); + + for($Y = 0; $Y < $this->levelData["height"]; ++$Y){ + $t = 1 << $Y; + if(($info[0] & $t) === $t){ + // 4096 + 2048 + 2048, Block Data, Meta, Light + if(strlen($chunkDataHere = gzread($chunk, 8192)) < 8192){ + console("[NOTICE] Empty corrupt chunk detected [$X,$Z,:$Y], recovering contents", true, true, 2); + $this->fillMiniChunk($X, $Z, $Y); + }else{ + + $this->chunks[$index][$Y] = str_repeat("\x00", 16384); + $this->chunkChange[$index][-1] = true; + $this->chunkChange[$index][$Y] = 16384; + $this->chunkInfo[$index][0] = str_repeat("\x00", 256); + + //Convert id-meta to id-meta-light-light + for($x = 0; $x < 16; ++$x){ + for($z = 0; $z < 16; ++$z){ + for($y = 0; $y < 16; ++$y){ + $id = $chunkDataHere[($y + ($x << 5) + ($z << 9))] ?? "\x00"; + $meta = $chunkDataHere[(($y >> 1) + 16 + ($x << 5) + ($z << 9))] ?? "\x00"; + + $bindex = (int) ($y + ($x << 6) + ($z << 10)); + $mindex = (int) (($y >> 1) + 16 + ($x << 6) + ($z << 10)); + + $this->chunks[$index][$Y][$bindex] = $id; + $this->chunks[$index][$Y][$mindex] = $meta; + } + } + } + } + }else{ + $this->chunks[$index][$Y] = false; + } + } + @gzclose($chunk); + return true; + } + + public function loadNCPMF1Chunk($X, $Z){ + $index = $this->getIndex($X, $Z); + + if($this->isChunkLoaded($X, $Z)){ + return true; + } + $cp = $this->getChunkPath($X, $Z); if(!is_file($cp)) return false; $chunk = file_get_contents($cp); @@ -173,18 +228,85 @@ class PMFLevel extends PMF{ } $this->seek(5); $this->levelData["version"] = ord($this->read(1)); - if($this->levelData["version"] != PMF_CURRENT_LEVEL_VERSION){ //TODO old worlds support + if($this->levelData["version"] != PMF_CURRENT_LEVEL_VERSION){ $cv = PMF_CURRENT_LEVEL_VERSION; ConsoleAPI::warn("The level version does not match current. ({$this->levelData["version"]} != {$cv})"); switch($this->levelData["version"]){ + case 0: + ConsoleAPI::notice("Converting the world from NCPMF-{$this->levelData["version"]} to NCPMF-$cv..."); + $worldDir = substr($worldFile, 0, -strlen("/level.pmf")); + $backupDir = "auto-world-backup-".microtime(true); + ConsoleAPI::info("Creating backup in $backupDir..."); + copydir($worldDir, $backupDir); + + ConsoleAPI::info("Starting converting..."); + $this->levelData["name"] = $this->read(Utils::readShort($this->read(2), false)); + $this->levelData["seed"] = Utils::readInt($this->read(4)); + $this->levelData["time"] = Utils::readInt($this->read(4)); + $this->levelData["spawnX"] = Utils::readFloat($this->read(4)); + $this->levelData["spawnY"] = Utils::readFloat($this->read(4)); + $this->levelData["spawnZ"] = Utils::readFloat($this->read(4)); + $this->levelData["width"] = ord($this->read(1)); + $this->levelData["height"] = ord($this->read(1)); + + ConsoleAPI::notice("Choosing ".LevelAPI::$defaultLevelType." generator."); + $generator = LevelAPI::createGenerator(LevelAPI::$defaultLevelType); + $this->levelData["generator"] = get_class($generator); + + $lastseek = ftell($this->fp); + if(($len = $this->read(2)) === false or ($this->levelData["extra"] = @gzinflate($this->read(Utils::readShort($len, false)))) === false){ //Corruption protection + console("[NOTICE] Empty/corrupt location table detected, forcing recovery"); + fseek($this->fp, $lastseek); + $c = gzdeflate(""); + $this->write(Utils::writeShort(strlen($c)) . $c); + $this->payloadOffset = ftell($this->fp); + $this->levelData["extra"] = ""; + //$cnt = pow($this->levelData["width"], 2); + for($X = 0; $X < 16; ++$X){ + for($Z = 0; $Z < 16; ++$Z){ + $this->write("\x00\xFF"); //Force index recreation + } + } + fseek($this->fp, $this->payloadOffset); + }else{ + $this->payloadOffset = ftell($this->fp); + } + $this->locationTable = []; + $this->seek($this->payloadOffset); + for($Z = 0; $Z < 16; ++$Z){ + for($X = 0; $X < 16; ++$X){ + $index = $this->getIndex($X, $Z); + $this->chunks[$index] = false; + $this->chunkChange[$index] = false; + $this->locationTable[$index] = [ + 0 => Utils::readShort($this->read(2)), //16 bit flags + ]; + } + } + + foreach(scandir("$worldDir/chunks/") as $f){ + if($f != "." && $f != ".."){ + $xz = explode(".", $f); + $X = (int) $xz[0]; + $Z = (int) $xz[1]; + ConsoleAPI::info("Converting $X-$Z..."); + $this->loadNCPMF0Chunk($X, $Z); + $this->unloadChunk($X, $Z); + } + } + + ConsoleAPI::notice("Modifying level.pmf..."); + $this->saveData(false); + ConsoleAPI::notice("World converted. Reloading..."); + break; case 1: ConsoleAPI::notice("Converting the world from NCPMF-{$this->levelData["version"]} to NCPMF-$cv..."); $worldDir = substr($worldFile, 0, -strlen("/level.pmf")); $backupDir = "auto-world-backup-".microtime(true); ConsoleAPI::info("Creating backup in $backupDir..."); copydir($worldDir, $backupDir); - ConsoleAPI::info("Started converting..."); + ConsoleAPI::info("Starting converting..."); $this->levelData["name"] = $this->read(Utils::readShort($this->read(2), false)); $this->levelData["seed"] = Utils::readInt($this->read(4)); $this->levelData["time"] = Utils::readInt($this->read(4)); @@ -202,9 +324,8 @@ class PMFLevel extends PMF{ $this->write(Utils::writeShort(strlen($c)) . $c); $this->payloadOffset = ftell($this->fp); $this->levelData["extra"] = ""; - //$cnt = pow($this->levelData["width"], 2); - for($X = 0; $X < 16; ++$X){ - for($Z = 0; $Z < 16; ++$Z){ + for($Z = 0; $X < 16; ++$Z){ + for($X = 0; $X < 16; ++$X){ $this->write("\x00\xFF"); //Force index recreation } } @@ -273,8 +394,8 @@ class PMFLevel extends PMF{ //$this->locationTable = []; //$cnt = pow($this->levelData["width"], 2); //$this->seek($this->payloadOffset); - for($X = 0; $X < 16; ++$X){ - for($Z = 0; $Z < 16; ++$Z){ + for($Z = 0; $Z < 16; ++$Z){ + for($X = 0; $X < 16; ++$X){ $index = $this->getIndex($X, $Z); $this->chunks[$index] = false; $this->chunkChange[$index] = false; From ad8cbb81ff352e0015b406d7919b25a393db8f03 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 9 Dec 2023 20:17:26 +0300 Subject: [PATCH 38/43] fixed oldmcpe->pmf2 --- src/world/LevelImport.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/world/LevelImport.php b/src/world/LevelImport.php index 49f21773..a0677b92 100644 --- a/src/world/LevelImport.php +++ b/src/world/LevelImport.php @@ -49,7 +49,8 @@ class LevelImport{ "spawnZ" => $level["SpawnZ"], "extra" => "", "width" => 16, - "height" => 8 + "height" => 8, + "generator" => get_class(LevelAPI::createGenerator(LevelAPI::$defaultLevelType)) ]); $chunks = new PocketChunkParser(); $chunks->loadFile($this->path . "chunks.dat"); @@ -72,8 +73,12 @@ class LevelImport{ $meta = $chunks->getChunkColumn($X, $Z, $x, $z, 1); for($Y = 0; $Y < 8; ++$Y){ $chunk[$Y] .= substr($block, $Y << 4, 16); - $chunk[$Y] .= substr($meta, $Y << 3, 8); - $chunk[$Y] .= "\x00\x00\x00\x00\x00\x00\x00\x00"; + $metaf = str_split(substr($meta, $Y << 3, 8)); + foreach($metaf as $m2){ + $chunk[$Y] .= chr(ord($m2) << 4) . chr(ord($m2) & 0xf); + } + $chunk[$Y] .= "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; + $chunk[$Y] .= "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; } } } From 92e56c60491fdf4427761c7199dcd999f28ea832 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 9 Dec 2023 20:19:53 +0300 Subject: [PATCH 39/43] small php8 tweaks --- src/entity/animal/Sheep.php | 2 +- src/world/Level.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index c6bbbf50..5159c3b7 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -37,7 +37,7 @@ class Sheep extends Animal{ $pk->event = EntityEventPacket::ENTITY_ANIM_10; $this->server->api->player->broadcastPacket($this->level->players, $pk); $this->server->schedule(38, [$this, "setSheared"], false); - $this->level->setblock(new Position($this->x, $this->y-1, $this->z, $this->level), new DirtBlock()); + $this->level->setBlock(new Position($this->x, $this->y-1, $this->z, $this->level), new DirtBlock()); } public function setSheared($v = null){ diff --git a/src/world/Level.php b/src/world/Level.php index a9dff4b0..c63f18c8 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -504,10 +504,13 @@ class Level{ } public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){ + $pos->x = (int)$pos->x; + $pos->y = (int)$pos->y; + $pos->z = (int)$pos->z; if(!isset($this->level) or (($pos instanceof Position) and $pos->level !== $this) or $pos->y < 0){ return false; } - $ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getMetadata()); + $ret = $this->level->setBlock((int)$pos->x, (int)$pos->y, (int)$pos->z, $block->getID(), $block->getMetadata()); if($ret === true){ if(!($pos instanceof Position)){ $pos = new Position($pos->x, $pos->y, $pos->z, $this); From cef28d2c3807a64fb2cdf9119008687d5b7ef070 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 9 Dec 2023 20:20:11 +0300 Subject: [PATCH 40/43] e --- src/world/Level.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/world/Level.php b/src/world/Level.php index c63f18c8..27b2aa27 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -510,7 +510,7 @@ class Level{ if(!isset($this->level) or (($pos instanceof Position) and $pos->level !== $this) or $pos->y < 0){ return false; } - $ret = $this->level->setBlock((int)$pos->x, (int)$pos->y, (int)$pos->z, $block->getID(), $block->getMetadata()); + $ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getMetadata()); if($ret === true){ if(!($pos instanceof Position)){ $pos = new Position($pos->x, $pos->y, $pos->z, $this); From c4b03ce13c2e1820b41e05a38a80b812495cf702 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 9 Dec 2023 20:53:07 +0300 Subject: [PATCH 41/43] fixed freeAllChunks --- src/Player.php | 11 +---------- src/entity/Entity.php | 4 ++-- src/world/Level.php | 32 ++++++++++++++++---------------- 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/Player.php b/src/Player.php index 47a30c5e..97d45111 100755 --- a/src/Player.php +++ b/src/Player.php @@ -569,6 +569,7 @@ class Player{ $z = $Z << 4; $this->level->useChunk($X, $Z, $this); $this->chunksLoaded["$X:$Z"] = true; + $pk = new FullChunkDataPacket; $pk->chunkX = $X; $pk->chunkZ = $Z; @@ -1132,16 +1133,6 @@ class Player{ if(isset($this->received[$p->messageIndex])){ continue; } - switch($p->pid()){ - case 0x01: - case ProtocolInfo::PING_PACKET: - case ProtocolInfo::PONG_PACKET: - case ProtocolInfo::MOVE_PLAYER_PACKET: - case ProtocolInfo::REQUEST_CHUNK_PACKET: - case ProtocolInfo::ANIMATE_PACKET: - case ProtocolInfo::SET_HEALTH_PACKET: - break; - } } $this->received[$p->messageIndex] = true; } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 4d26764c..9a2eb1e5 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -408,8 +408,8 @@ class Entity extends Position } else{ $hasUpdate = true; } - if(($this->player instanceof Player) and ($this->player->gamemode & 0x01) === CREATIVE){ // Remove fire effects in next tick - $this->fire = 1; + if(($this->player instanceof Player) and ($this->player->gamemode & 0x01) === CREATIVE){ + $this->fire = 0; } } diff --git a/src/world/Level.php b/src/world/Level.php index 27b2aa27..dc26073d 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -85,19 +85,22 @@ class Level{ } public function fastSetBlockUpdate($x, $y, $z, $id, $meta, $updateBlocksAround = false){ + $x = (int)$x; + $y = (int)$y; + $z = (int)$z; $this->level->setBlock($x, $y, $z, $id, $meta); - $pk = new UpdateBlockPacket; - $pk->x = $x; - $pk->y = $y; - $pk->z = $z; - $pk->block = $id; - $pk->meta = $meta; - $this->server->api->player->broadcastPacket($this->players, $pk); //TODO broadcast in another queue? + $this->sendBlockToAll($x, $y, $z); if($updateBlocksAround){ $this->server->api->block->blockUpdateAround(new Position($x, $y, $z, $this), BLOCK_UPDATE_NORMAL, 1); } } + public function sendBlockToAll($x, $y, $z){ + foreach($this->players as $p){ + $this->resendBlocksToPlayers[$p->CID]["$x.$y.$z"] = true; + } + } + public function __destruct(){ if(isset($this->level)){ $this->save(false, false); @@ -312,8 +315,11 @@ class Level{ } public function freeAllChunks(Player $player){ - foreach($this->usedChunks as $i => $c){ - unset($this->usedChunks[$i][$player->CID]); + foreach($player->chunksLoaded as $chunk => $c){ + $xz = explode(":", $chunk); + $x = $xz[0]; + $z = $xz[1]; + $player->stopUsingChunk($x, $z); } } @@ -517,13 +523,7 @@ class Level{ } $block->position($pos); if($direct === true){ - $pk = new UpdateBlockPacket; - $pk->x = $pos->x; - $pk->y = $pos->y; - $pk->z = $pos->z; - $pk->block = $block->getID(); - $pk->meta = $block->getMetadata(); - $this->server->api->player->broadcastPacket($this->players, $pk); + $this->sendBlockToAll($pos->x, $pos->y, $pos->z); }else{ $i = ($pos->x >> 4) . ":" . ($pos->y >> 4) . ":" . ($pos->z >> 4); if(!isset($this->changedBlocks[$i])){ From bfe23f5547ebeb7bddf13a7f3ce6d032d52b32fb Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 10 Dec 2023 00:13:10 +0300 Subject: [PATCH 42/43] removed Java --- src/entity/Entity.php | 2 +- src/utils/Java.php | 48 ------------------------------------------- 2 files changed, 1 insertion(+), 49 deletions(-) delete mode 100644 src/utils/Java.php diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 9a2eb1e5..e30e6cf5 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -429,7 +429,7 @@ class Entity extends Position $blockY = floor($this->y + $this->getEyeHeight() + $y); $blockZ = floor($this->z + $z); - if($this->level->getBlockWithoutVector($blockX, $blockY, $blockZ)->isSolid){ + if(StaticBlock::getIsSolid($this->level->level->getBlockID($blockX, $blockY, $blockZ))){ $this->harm(1, "suffocation"); // Suffocation $hasUpdate = true; break; diff --git a/src/utils/Java.php b/src/utils/Java.php deleted file mode 100644 index 267cb030..00000000 --- a/src/utils/Java.php +++ /dev/null @@ -1,48 +0,0 @@ -value = (string) $string; - $this->count = strlen($this->value); - } - } - - public function __toString(){ - return $this->value; - } - - public function lenght(){ - return $this->count; - } - - public function isEmpty(){ - return $this->count === 0; - } - - public function hashCode(){ - $h = $this->hash; - if($h === 0 and $this->count > 0){ - for($i = 0; $i < $this->count; ++$i){ - $h = (($h << 5) - $h) + ord($this->charAt($i)); - $h = $h & 0xFFFFFFFF; - $this->hash = $h; - } - $this->hash = $h; - } - return $h; - } - - public function charAt($index){ - $index = (int) $index; - if($index < 0 or $index >= $this->count){ - trigger_error("Undefined offset $index", E_USER_WARNING); - return false; - } - return $this->value[$index]; - } -} \ No newline at end of file From e163200131141a7b4983acee9a83e04da279b1ca Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 10 Dec 2023 00:27:27 +0300 Subject: [PATCH 43/43] small fix of freehijs commit --- src/material/block/nonfull/FenceBlock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/material/block/nonfull/FenceBlock.php b/src/material/block/nonfull/FenceBlock.php index f38f7bf1..7cd43644 100644 --- a/src/material/block/nonfull/FenceBlock.php +++ b/src/material/block/nonfull/FenceBlock.php @@ -1,7 +1,7 @@ "Oak Fence",