added StaticBlock

This commit is contained in:
GameHerobrine 2023-08-16 19:22:05 +03:00
parent 0b28ed8042
commit c6c088a7d5
7 changed files with 211 additions and 23 deletions

View file

@ -0,0 +1,13 @@
<?php
//TileEntities
define("TILE_SIGN", "Sign");
define("TILE_CHEST", "Chest");
define("CHEST_SLOTS", 27);
define("TILE_FURNACE", "Furnace");
define("FURNACE_SLOTS", 3);
define("TILE_MOB_SPAWNER", "MobSpawner");

View file

@ -0,0 +1,96 @@
<?php
/**
* Small class to hopefully optimize entities a bit
*/
class StaticBlock
{
const DEFAULT_SLIPPERINESS = 0.6;
const DEFAULT_HARDNESS = 0;
private static $NULL_BOUNDS;
public static $isSolid = [];
public static $isTransparent = [];
public static $isFlowable = [];
public static $isReplaceable = [];
public static $isPlaceable = [];
public static $hasPhysics = [];
public static $isLiquid = [];
public static $isFullBlock = [];
public static $hardness = [];
public static $slipperiness = [];
public static $boundingBoxes = [];
public static function init(){
self::$NULL_BOUNDS = new AxisAlignedBB(0, 0, 0, 0, 0, 0);
foreach(Block::$class as $nonstaticname){
/**@var Block $b*/
$b = new $nonstaticname();
self::$isSolid[$b->getID()] = $b->isSolid;
self::$isTransparent[$b->getID()] = $b->isTransparent;
self::$isFlowable[$b->getID()] = $b->isFlowable;
self::$isReplaceable[$b->getID()] = $b->isReplaceable;
self::$isPlaceable[$b->getID()] = $b->isPlaceable;
self::$hasPhysics[$b->getID()] = $b->hasPhysics;
self::$isLiquid[$b->getID()] = $b->isLiquid;
self::$isFullBlock[$b->getID()] = $b->isFullBlock;
self::$slipperiness[$b->getID()] = $b->slipperiness;
self::$boundingBoxes[$b->getID()] = $b->boundingBox;
self::$hardness[$b->getID()] = $b->getHardness();
}
}
public static function getBlock($id){
return Block::$class[$id] ?? Block::$class[0];
}
public static function getHardness($id){
return self::$hardness[$id] ?? StaticBlock::DEFAULT_HARDNESS;
}
public static function getBoundingBoxForBlockCoords($id, $x, $y, $z){
/**@var AxisAlignedBB $bb*/
$bb = self::$boundingBoxes[$id] ?? false;
if($bb === false){
return clone self::$NULL_BOUNDS;
}
$bb = clone $bb;
return $bb->setBounds($x + $bb->minX, $y + $bb->minY, $z + $bb->minZ, $x + $bb->maxX, $y + $bb->maxY, $z + $bb->maxZ);
}
public static function getSlipperiness($id){
return self::$slipperiness[$id] ?? StaticBlock::DEFAULT_SLIPPERINESS;
}
public static function getIsSolid($id){
return self::$isSolid[$id] ?? false;
}
public static function getIsTransparent($id){
return self::$isTransparent[$id] ?? false;
}
public static function getIsFlowable($id){
return self::$isFlowable[$id] ?? false;
}
public static function getIsReplaceable($id){
return self::$isReplaceable[$id] ?? false;
}
public static function getIsPlaceable($id){
return self::$isPlaceable[$id] ?? false;
}
public static function getHasPhysics($id){
return self::$hasPhysics[$id] ?? false;
}
public static function getIsLiquid($id){
return self::$isLiquid[$id] ?? false;
}
public static function getIsFullBlock($id){
return self::$isFullBlock[$id] ?? false;
}
}

View file

@ -0,0 +1,30 @@
<?php
class MobSpawnerBlock extends SolidBlock
{
public function __construct($meta = 0){
parent::__construct(MONSTER_SPAWNER, $meta, "Mob Spawner");
$this->isActivable = true;
}
public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
$api = ServerAPI::request();
$tile = $api->api->tile->add($this->level, TILE_MOB_SPAWNER, $this->x, $this->y, $this->z, [
"id" => TILE_MOB_SPAWNER,
"x" => $this->x,
"y" => $this->y,
"z" => $this->z,
"EntityId" => MOB_SPIDER, //TODO vanilla values
"Delay" => 5,
"MinSpawnDelay" => 20,
"MaxSpawnDelay" => 80,
"SpawnCount" => 4,
"MaxNearbyEntities" => 0,
"RequiredPlayerRange" => 0,
"SpawnRange" => 4
]);
parent::place($item, $player, $block, $target, $face, $fx, $fy, $fz);
}
}

View file

@ -0,0 +1,62 @@
<?php
class PodzolBlock extends SolidBlock{
public function __construct($meta = 0){
parent::__construct(PODZOL, $meta, "Podzol");
$this->hardness = 2.5;
}
public function onUpdate($type){
if($type === BLOCK_UPDATE_RANDOM){
if(mt_rand(0, 3) == 0){
$up = $this->getSide(1);
if(($up->isTransparent === false) or ($up->isLiquid) or ($up->getID() == 60)) return false;
if($this->getGrassInRadius()){
$this->level->setBlock($this, BlockAPI::get(GRASS, 0), true, false, true);
}
}
}
return BLOCK_UPDATE_RANDOM;
}
public function getBlockID($x, $y, $z){
return $this->level->getBlock(new Vector3($x, $y, $z))->getID();
}
public function getGrassInRadius(){
$x = $this->x;
$y = $this->y;
$z = $this->z;
if($this->getBlockID($x+1, $y, $z+1) == 2) return true;
if($this->getBlockID($x+1, $y, $z) == 2) return true;
if($this->getBlockID($x+1, $y, $z-1) == 2) return true;
if($this->getBlockID($x, $y, $z+1) == 2) return true;
if($this->getBlockID($x, $y, $z-1) == 2) return true;
if($this->getBlockID($x-1, $y, $z+1) == 2) return true;
if($this->getBlockID($x-1, $y, $z) == 2) return true;
if($this->getBlockID($x-1, $y, $z-1) == 2) return true;
if($this->getBlockID($x+1, $y-1, $z+1) == 2) return true;
if($this->getBlockID($x+1, $y-1, $z) == 2) return true;
if($this->getBlockID($x+1, $y-1, $z-1) == 2) return true;
if($this->getBlockID($x, $y-1, $z+1) == 2) return true;
if($this->getBlockID($x, $y-1, $z) == 2) return true;
if($this->getBlockID($x, $y-1, $z-1) == 2) return true;
if($this->getBlockID($x-1, $y-1, $z+1) == 2) return true;
if($this->getBlockID($x-1, $y-1, $z) == 2) return true;
if($this->getBlockID($x-1, $y-1, $z-1) == 2) return true;
if($this->getBlockID($x+1, $y+1, $z+1) == 2) return true;
if($this->getBlockID($x+1, $y+1, $z) == 2) return true;
if($this->getBlockID($x+1, $y+1, $z-1) == 2) return true;
if($this->getBlockID($x, $y+1, $z+1) == 2) return true;
if($this->getBlockID($x, $y+1, $z-1) == 2) return true;
if($this->getBlockID($x-1, $y+1, $z+1) == 2) return true;
if($this->getBlockID($x-1, $y+1, $z) == 2) return true;
if($this->getBlockID($x-1, $y+1, $z-1) == 2) return true;
return false;
}
}

View file

@ -101,12 +101,7 @@ class Level{
if(!isset($this->level)){
return false;
}
/*if(ADVANCED_CACHE == true){
$identifier = "world:{$this->name}:$X:$Z";
if(($cache = Cache::get($identifier)) !== false){
return $cache;
}
}*/
$gen = $this->generatorType === 1 || !($X > 15 || $X < 0 || $Z > 15 || $Z < 0);
$orderedIds = "";
$orderedData = "";
@ -149,7 +144,7 @@ class Level{
}
$nbt = new NBT_new(NBT_new::LITTLE_ENDIAN);
foreach($chunkTiles as $tile){
foreach($chunkTiles as $tile){ //TODO non-hardcoded nbt data
switch($tile->class){
case "Sign":
$text = $tile->getText();
@ -183,9 +178,6 @@ class Level{
$orderedIds . $orderedData . $orderedSkyLight . $orderedLight .
$orderedBiomeIds . $orderedBiomeColors . $tileEntities;
$ordered = zlib_encode($orderedUncompressed, ZLIB_ENCODING_DEFLATE, 6);
/*if(ADVANCED_CACHE == true){
Cache::add($identifier, $ordered, 60);
}*/
return $ordered;
}

View file

@ -14,7 +14,7 @@ class GroundCover extends Populator
if(count($cover) > 0){
$diffY = 0;
if(!BlockAPI::get($cover[0][0], $cover[0][1])->isSolid){
if(!StaticBlock::getIsSolid($cover[0][0])){
$diffY = 1;
}
@ -22,7 +22,7 @@ class GroundCover extends Populator
for($y = 127; $y > 0; --$y){
$chunkY = $y >> 4;
$b = $column[$chunkY][(($y & 0xf) + ($x << 6) + ($z << 10))];
if($b !== "\x00" and !BlockAPI::get(ord($b))->isTransparent){
if($b !== "\x00" and !StaticBlock::getIsTransparent(ord($b))){
break;
}
}
@ -31,20 +31,17 @@ class GroundCover extends Populator
for($y = $startY; $y > $endY and $y >= 0; --$y){
$chunkY = $y >> 4;
$pair = $cover[$startY - $y];
$b = BlockAPI::get($pair[0], $pair[1]);
if($column[$chunkY][(($y & 0xf) + ($x << 6) + ($z << 10))] === "\x00" and $b->isSolid){
//$b = BlockAPI::get($pair[0], $pair[1]);
$bid = $pair[0];
$bmeta = $pair[1];
if($column[$chunkY][(($y & 0xf) + ($x << 6) + ($z << 10))] === "\x00" and StaticBlock::getIsSolid($bid)){
break;
}
if($y <= $waterHeight and $b->getID() == GRASS and $level->level->getBlockID($pcx, $y + 1, $pcz) == STILL_WATER){
//$b = BlockAPI::get(DIRT);
if($y <= $waterHeight and $bid == GRASS and $level->level->getBlockID($pcx, $y + 1, $pcz) == STILL_WATER){
$level->level->setBlock($pcx, $y, $pcz, DIRT, 0);
continue;
}
//if($b->getMetadata() === 0){
// $level->level->setBlockId($pcx, $y, $pcz, $b->getID());
//}else{
$level->level->setBlock($pcx, $y, $pcz, $b->getID(), $b->getMetadata());
//}
$level->level->setBlock($pcx, $y, $pcz, $bid, $bmeta);
}
}
}

View file

@ -48,7 +48,6 @@ class TallGrassPopulator extends Populator{
}else{
$this->level->level->setBlock($xx, $yy, $zz, TALL_GRASS, 1);
}
}
}
}
@ -59,7 +58,6 @@ class TallGrassPopulator extends Populator{
}
private function getHighestWorkableBlock($x, $z){
for($y = 128; $y > 0; --$y){
$b = $this->level->level->getBlockID($x, $y, $z);
if($b == GRASS){