dungeon generator(disabled)

This commit is contained in:
GameHerobrine 2023-12-02 13:39:58 +03:00
parent 9abc3cd409
commit 60fd866de6
11 changed files with 157 additions and 30 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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);

View file

@ -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;
}

View file

@ -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) .

View file

@ -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));

View file

@ -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);
}
}

View file

@ -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;

View file

@ -0,0 +1,13 @@
<?php
abstract class Feature
{
public static $DUNGEON;
public static function init(){
self::$DUNGEON = new DungeonFeature();
}
public abstract function place(Level $level, MTRandom $rand, $x, $y, $z);
}

View file

@ -0,0 +1,82 @@
<?php
class DungeonFeature extends Feature
{
protected function selectEID(MTRandom $rand){
}
public function place(Level $level, MTRandom $rand, $initialX, $initialY, $initialZ)
{
$baseSize = 3;
$randomSizeX = $rand->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;
}
}