ported by needs fixing

This commit is contained in:
GameHerobrine 2024-05-23 04:59:48 +03:00
parent fbf4131bbd
commit b94d0f2ba1
9 changed files with 201 additions and 13 deletions

View file

@ -536,6 +536,9 @@ class BlockAPI{
return $level;
}
public function blockUpdateAround(Position $pos, $type = BLOCK_UPDATE_NORMAL, $delay = false){
if($type == BLOCK_UPDATE_NORMAL){
@ -564,6 +567,24 @@ class BlockAPI{
$this->blockUpdate($pos->getSide(5), $type);
}
}
public function scheduleBlockUpdateXYZ(Level $level, $x, $y, $z, $type = BLOCK_UPDATE_SCHEDULED, $delay = false){
$type = (int) $type;
ConsoleAPI::debug($delay);
if($delay < 0){
return false;
}
$index = $x . "." . $y . "." . $z . "." . $level->getName() . "." . $type;
$delay = microtime(true) + $delay * 0.05;
ConsoleAPI::debug($index);
if(!isset($this->scheduledUpdates[$index])){
$this->scheduledUpdates[$index] = new Position($x, $y, $z, $level); //TODO dont create a position
$this->server->query("INSERT INTO blockUpdates (x, y, z, level, type, delay) VALUES ($x, $y, $z, '{$level->getName()}', $type, $delay);");
ConsoleAPI::debug("added block update $x $y $z $type");
return true;
}
return false;
}
public function scheduleBlockUpdate(Position $pos, $delay, $type = BLOCK_UPDATE_SCHEDULED){
$type = (int) $type;

View file

@ -24,7 +24,9 @@ class LiquidBlock extends TransparentBlock{
return -1;
}
public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){
static::updateLiquid($level, $x, $y, $z);
}
public static function getAABB(Level $level, $x, $y, $z){
return null;
}
@ -40,7 +42,7 @@ class LiquidBlock extends TransparentBlock{
public static function updateLiquid(Level $level, $x, $y, $z){
[$id, $meta] = $level->level->getBlock($x, $y, $z);
if($id != LAVA && $id != STILL_LAVA) continue;
if($id != LAVA && $id != STILL_LAVA) return;
$zNeg = $level->level->getBlockID($x, $y, $z - 1);
$zPos = $level->level->getBlockID($x, $y, $z + 1);

View file

@ -1,4 +1,5 @@
<?php
require_once("LiquidBlockDynamic.php"); //TODO class loader?
class LavaBlock extends LiquidBlockDynamic implements LightingBlock{
public function __construct($meta = 0){

View file

@ -7,6 +7,7 @@ class LiquidBlockDynamic extends LiquidBlock{
public static $blockID = 0;
public static $f_d8 = 0; //TODO rename
public static $spread = [0, 0, 0, 0];
public static function getHighest(Level $level, $x, $y, $z, $highest){
$depth = self::getDepth($level, $x, $y, $z);
@ -29,6 +30,133 @@ class LiquidBlockDynamic extends LiquidBlock{
$level->fastSetBlockUpdate($x, $y, $z, self::$blockID + 1, $meta);
}
public static function isWaterBlocking(Level $level, $x, $y, $z){
$id = $level->level->getBlockID($x, $y, $z);
if($id == AIR) return false;
if($id == DOOR_BLOCK || $id == SIGN || $id == LADDER || $id == SUGARCANE_BLOCK) return true;
if($id == CARPET || $id == SNOW_LAYER || $id == RAIL || $id == POWERED_RAIL) return false; //TODO Tile::getThickness() > 0
//TODO materials
if(!StaticBlock::getIsFlowable($id)) return 1;
else return StaticBlock::getIsSolid($id);
}
public static function canSpreadTo(Level $level, $x, $y, $z){
$id = $level->level->getBlockID($x, $y, $z);
if($id == LAVA || $id == STILL_LAVA) return false;
if((static::$blockID == WATER || static::$blockID == STILL_WATER) && ($id == WATER || $id == STILL_WATER)) return false;
return static::isWaterBlocking($level, $x, $y, $z) ^ 1;
}
public static function getSpread(Level $level, $x, $y, $z){
//TODO get rid of gotos
for($i = 0; $i < 4; ++$i){
static::$spread[$i] = 1000;
$xs = $x;
$ys = $y;
$zs = $z;
switch($i){
case 0:
--$xs;
break;
case 1:
++$xs;
break;
case 2:
--$zs;
break;
case 3:
++$zs;
break;
}
if(static::isWaterBlocking($level, $xs, $ys, $zs)) continue;
[$id, $meta] = $level->level->getBlock($xs, $ys, $zs);
if(
(((static::$blockID == WATER || static::$blockID == STILL_WATER) && ($id == WATER || $id == STILL_WATER)) ||
((static::$blockID == LAVA || static::$blockID == STILL_LAVA) && ($id == LAVA || $id == STILL_LAVA))) &&
$meta == 0
){
continue;
}
if(!static::isWaterBlocking($level, $xs, $ys - 1, $zs)) static::$spread[$i] = 0;
else static::$spread[$i] = static::getSlopeDistance($level, $xs, $ys, $zs, 1, $i);
}
$i1 = max(static::$spread);
$ba = [];
for($i = 0; $i < 4; ++$i) $ba[$i] = ($i1 == static::$spread[$i]);
return $ba;
}
public static function getSlopeDistance(Level $level, $x, $y, $z, $l, $i1){
$j1 = 1000;
for($k1 = 0; $k1 < 4; ++$k1){
if($k1 == 0 && $i1 == 1 || $k1 == 1 && $i1 == 0 || $k1 == 2 && $i1 == 3 || $k1 == 3 && $i1 == 2){
continue;
}
$xs = $x;
$ys = $y;
$zs = $z;
switch($k1){
case 0:
--$xs;
break;
case 1:
++$xs;
break;
case 2:
--$zs;
break;
case 3:
++$zs;
break;
}
if(static::isWaterBlocking($level, $xs, $ys, $zs)) continue;
[$id, $meta] = $level->level->getBlock($xs, $ys, $zs);
if(
(((static::$blockID == WATER || static::$blockID == STILL_WATER) && ($id == WATER || $id == STILL_WATER)) ||
((static::$blockID == LAVA || static::$blockID == STILL_LAVA) && ($id == LAVA || $id == STILL_LAVA))) &&
$meta == 0
){
continue;
}
if(!static::isWaterBlocking($level, $xs, $ys - 1, $zs)) return $l;
if($l >= 4) continue;
$k2 = static::getSlopeDistance($level, $xs, $ys, $zs, $l + 1, $k1);
if($k2 < $j1) $j1 = $k2;
}
return $j1;
}
public static function trySpreadTo(Level $level, $x, $y, $z, $meta){
if(static::canSpreadTo($level, $x, $y, $z)){
$id = $level->level->getBlockID($x, $y, $z);
if($id > 0){
if(($id == LAVA || $id == STILL_LAVA) && (self::$id == LAVA || self::$id == STILL_LAVA)); //fizz
else{
//TODO spawnResources
}
}
$level->fastSetBlockUpdate($x, $y, $z, self::$blockID, $meta, true);
}
}
public static function onUpdate(Level $level, $x, $y, $z, $type){
static::$blockID = $id = $level->level->getBlockID($x, $y, $z);
$depth = static::getDepth($level, $x, $y, $z);
@ -71,7 +199,7 @@ class LiquidBlockDynamic extends LiquidBlock{
if($v16 > 7) goto LABEL_32; //TODO remove gotos
if($v16 > $depth){
if(mt_rand(0, 3)) $tickDelay *= 4;
//if(mt_rand(0, 3)) $tickDelay *= 4;
LABEL_30:
if($v16 < 0){
@ -83,17 +211,45 @@ class LiquidBlockDynamic extends LiquidBlock{
LABEL_32:
$level->fastSetBlockUpdateMeta($x, $y, $z, $v16, false);
ServerAPI::request()->api->block->scheduleBlockUpdate(new Position($x, $y, $z, $level), $tickDelay); //TODO get rid of new Position
$level->updateNeighborsAt($x, $y, $z, self::$blockID);
$level->updateNeighborsAt($x, $y, $z, static::$blockID);
goto LABEL_33;
}
}
if($v16 != $depth) goto LABEL_30;
}
self::setStatic($level, $x, $y, $z);
static::setStatic($level, $x, $y, $z);
LABEL_35:
//TODO
if(static::canSpreadTo($level, $x, $y - 1, $z)){
if($id == LAVA || $id == STILL_LAVA){
$idBottom = $level->level->getBlockID($x, $y, $z);
if($idBottom == WATER || $idBottom == STILL_WATER){
$level->fastSetBlockUpdate($x, $y - 1, $z, STONE, 0, true);
//fizz
}
}else{
if($depth > 7){
$meta = $depth;
}else{
$meta = $depth + 8;
}
$level->fastSetBlockUpdate($x, $y - 1, $z, $id, $meta, true);
}
}else if($depth >= 0 && ($depth == 0 || static::isWaterBlocking($level, $x, $y - 1, $z))){
$spread = static::getSpread($level, $x, $y, $z);
if($depth > 7) $v18 = 1;
else{
$v18 = $depth + $spreadAdder;
if($v18 > 7) return;
}
if($spread[0]) static::trySpreadTo($level, $x - 1, $y, $z, $v18);
if($spread[1]) static::trySpreadTo($level, $x + 1, $y, $z, $v18);
if($spread[2]) static::trySpreadTo($level, $x, $y, $z - 1, $v18);
if($spread[3]) static::trySpreadTo($level, $x, $y, $z + 1, $v18);
}
}
}

View file

@ -10,16 +10,18 @@ class LiquidBlockStatic extends LiquidBlock{
public static function setDynamic(Level $level, $x, $y, $z){
//TODO set dynamic
[$id, $meta] = $level->level->getBlock($x, $y, $z);
$dynamicID =$id - 1; //very unsafe
$dynamicID = $id - 1; //very unsafe
$level->fastSetBlockUpdate($x, $y, $z, $dynamicID, $meta);
$delay = static::getTickDelay();
ServerAPI::request()->api->block->blockUpdateAroundXYZ($x, $y, $z, BLOCK_UPDATE_SCHEDULED, $delay);
ServerAPI::request()->api->block->scheduleBlockUpdateXYZ($level, $x, $y, $z, BLOCK_UPDATE_SCHEDULED, $delay);
}
public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){
$oldID = $level->level->getBlockID($x, $y, $z);
static::updateLiquid($level, $x, $y, $z);
$newID = $level->level->getBlockID($x, $y, $z);
ConsoleAPI::debug($oldID.":".$newID);
if($oldID == $newID){
static::setDynamic($level, $x, $y, $z);
}

View file

@ -1,4 +1,5 @@
<?php
require_once("LiquidBlockStatic.php"); //TODO class loader?
class StillLavaBlock extends LiquidBlockStatic{
public function __construct($meta = 0){

View file

@ -3,6 +3,8 @@
/***REM_START***/
require_once("WaterBlock.php"); //TODO class loader?
require_once("LiquidBlockStatic.php"); //TODO class loader?
/***REM_END***/
class StillWaterBlock extends LiquidBlockStatic{

View file

@ -1,4 +1,5 @@
<?php
require_once("LiquidBlockDynamic.php"); //TODO class loader?
class WaterBlock extends LiquidBlockDynamic{
public function __construct($meta = 0){
@ -15,8 +16,4 @@ class WaterBlock extends LiquidBlockDynamic{
public static function getTickDelay(){
return 5;
}
public static function onUpdate(Level $level, $x, $y, $z, $type){
}
}

View file

@ -468,18 +468,24 @@ class Level{
public function updateNeighborsAt($x, $y, $z, $oldID){
$block = $this->level->getBlockID($x - 1, $y, $z);
ConsoleAPI::debug(StaticBlock::getBlock($block));
if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x - 1, $y, $z, $x, $y, $z, $oldID);
$block = $this->level->getBlockID($x + 1, $y, $z);
ConsoleAPI::debug(StaticBlock::getBlock($block));
if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x + 1, $y, $z, $x, $y, $z, $oldID);
$block = $this->level->getBlockID($x, $y - 1, $z);
ConsoleAPI::debug(StaticBlock::getBlock($block));
if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y - 1, $z, $x, $y, $z, $oldID);
$block = $this->level->getBlockID($x, $y + 1, $z);
ConsoleAPI::debug(StaticBlock::getBlock($block));
if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y + 1, $z, $x, $y, $z, $oldID);
$block = $this->level->getBlockID($x, $y, $z - 1);
ConsoleAPI::debug(StaticBlock::getBlock($block));
if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y, $z - 1, $x, $y, $z, $oldID);
$block = $this->level->getBlockID($x, $y, $z + 1);
ConsoleAPI::debug(StaticBlock::getBlock($block));
if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y, $z + 1, $x, $y, $z, $oldID);
}