mobs wont spawn if brightness is too high, aditional file with world data for storing additional information

This commit is contained in:
GameHerobrine 2025-06-23 07:02:31 +03:00
parent d1a724c143
commit cd07581d49
4 changed files with 175 additions and 13 deletions

View file

@ -210,7 +210,11 @@ class ConsoleAPI{
case "status":
$info = $this->server->debugInfo();
if(!($issuer instanceof Player) && $issuer === "console"){
return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]}), Entities: {$info["entities"]}, Events: {$info["events"]}, Handlers: {$info["handlers"]}, Actions: {$info["actions"]}, Garbage: {$info["garbage"]}";
$lightUpdates = 0;
foreach($this->server->api->level->levels as $level){
$lightUpdates += count($level->lightUpdates);
}
return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]}), Entities: {$info["entities"]}, Events: {$info["events"]}, Handlers: {$info["handlers"]}, Actions: {$info["actions"]}, Garbage: {$info["garbage"]} Light updates: {$lightUpdates}";
}
return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]})";
case "stop":

View file

@ -3,13 +3,18 @@
define("PMF_CURRENT_LEVEL_VERSION", 0x00);
class PMFLevel extends PMF{
const NC_DATA_VERSION = 1;
public $isLoaded = true;
private $levelData = [];
public $hasLight = "";
public $locationTable = [];
private $log = 4; //must be 4 or else rip world
private $payloadOffset = 0;
public $chunks = [];
public $chunkChange = [];
/**
* @var Level
*/
public $level;
public function __construct($file, $blank = false){
if(is_array($blank)){
@ -81,8 +86,83 @@ class PMFLevel extends PMF{
if($locationTable !== false){
$this->writeLocationTable();
}
$this->writeNCData();
}
public function writeNCData(){
$dir = dirname($this->file);
try{
$file = fopen("$dir/ncleveldata.pmf", "wb");
fwrite($file, chr(self::NC_DATA_VERSION), 1);
fwrite($file, $this->hasLight, 16*16);
}finally{
fclose($file);
}
}
public function readNCData(){
$this->hasLight = str_repeat("\x00", 16*16);
$dir = dirname($this->file);
$path = "$dir/ncleveldata.pmf";
if(!is_file($path)){
ConsoleAPI::notice("No NC Level data found! Old world?");
return;
}
try{
$file = fopen($path, "rb");
$ncdv = ord(fread($file, 1));
$v = self::NC_DATA_VERSION;
if($ncdv !== $v){
ConsoleAPI::notice("NC level data version on server($v) does not match the world($ncdv)! Creating NC level data backup.");
$newpath = "$path.bak.".microtime(true);
$f = copy($path, $newpath);
if($f === false) ConsoleAPI::error("NC level data backup creation failed.");
else ConsoleAPI::notice("NC level data backup created. (path: $newpath)");
}
$this->hasLight = fread($file, 16*16);
}finally{
fclose($file);
}
}
/**
* Checks was skylight generated for chunk
* @param int $X - chunk x
* @param int $Z - chunk z
*/
public function hasSkylight($X, $Z){
if($X < 0 || $X > 15 || $Z < 0 || $Z > 15) return false;
$index = self::getIndex($X, $Z);
return (ord($this->hasLight[$index]) & 2) > 0;
}
/**
* Checks was blocklight generated for chunk
* @param int $X - chunk x
* @param int $Z - chunk z
*/
public function hasBlocklight($X, $Z){
if($X < 0 || $X > 15 || $Z < 0 || $Z > 15) return false;
$index = self::getIndex($X, $Z);
return (ord($this->hasLight[$index]) & 1) > 0;
}
public function markHasBlocklight($X, $Z){
if($X < 0 || $X > 15 || $Z < 0 || $Z > 15) return false;
$index = self::getIndex($X, $Z);
$o = ord($this->hasLight[$index]);
$this->hasLight[$index] = chr($o | 1);
return true;
}
public function markHasSkylight($X, $Z){
if($X < 0 || $X > 15 || $Z < 0 || $Z > 15) return false;
$index = self::getIndex($X, $Z);
$o = ord($this->hasLight[$index]);
$this->hasLight[$index] = chr($o | 2);
return true;
}
private function writeLocationTable(){
$cnt = pow($this->levelData["width"], 2);
@ftruncate($this->fp, $this->payloadOffset);
@ -159,6 +239,7 @@ class PMFLevel extends PMF{
}else{
$this->payloadOffset = ftell($this->fp);
}
$this->readNCData();
return $this->readLocationTable();
}
@ -274,13 +355,12 @@ class PMFLevel extends PMF{
return $this->chunks[$index][$Y];
}
public $checkLight = [];
public function loadChunk($X, $Z){
$index = self::getIndex($X, $Z);
if($this->isChunkLoaded($X, $Z)){
return true;
}elseif(!isset($this->locationTable[$index])){
return false;
}
@ -307,9 +387,27 @@ class PMFLevel extends PMF{
$this->chunks[$index][$Y] = false;
}
}
$this->checkLight[$index] = [$X, $Z];
@gzclose($chunk);
return true;
}
public function forceLightUpdatesIfNeeded(){
foreach($this->checkLight as $index => [$X, $Z]){
$update = false;
if(!$this->hasBlocklight($X, $Z)){
ConsoleAPI::notice("Chunk $X $Z (Level: {$this->level->getName()} has no blocklight! Forcing light update(it might take a while).");
$this->level->updateLight(0, $X*16, 0, $Z*16, $X*16+15, 127, $Z*16+15);
$update = true;
$this->markHasBlocklight($X, $Z);
}
if($update) while($this->level->updateLights());
unset($this->checkLight[$index]);
}
}
protected function fillMiniChunk($X, $Z, $Y){
if($this->isChunkLoaded($X, $Z) === false){

View file

@ -577,18 +577,71 @@ class Level{
}
}
/**
* Gets block brightness based on skylight, blocklight and subtracts skyDarken.
* @param int $x
* @param int $y
* @param int $z
*/
public function getRawBrightness(int $x, int $y, int $z){
//TODO special way to do it for some blocks
$bl = $this->level->getBlockLight($x, $y, $z);
$l = 0; ///*$this->level->getBrightness(LIGHTLAYER_SKY, $x, $y, $z)*/ (15 - $this->skyDarken); no skylight is implemnted
if($bl > $l) $l = $bl;
if($l < 0) $l = 0;
return $l;
}
/**
* Schedules light update. Make sure to check that coordinates are in range.
* @param int $layer - light layer(0 - block, 15 - sky(not implemented as of now)).
* @param int $minX - must be in range (0, 255). Must be less than maxX.
* @param int $minY - must be in range (0, 127). Must be less than maxY.
* @param int $minZ - must be in range (0, 255). Must be less than maxZ.
* @param int $maxX - must be in range (0, 255). Must be more than minX.
* @param int $maxY - must be in range (0, 127). Must be more than minY.
* @param int $maxZ - must be in range (0, 255). Must be more than minZ.
*/
public function updateLight($layer, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){
/*$max = 5; TODO: expand lightupdate
/*$max = 5; no idea is it needed for block light
$sz = $this->lightUpdateIndx;
if($sz < $max) $max = $sz;
for($i = 0; $i < $max; ++$i){
$update = $this->lightUpdates[$i];
if($update->layer == $layer && ($r = $update->expand($minX, $minY, $minZ, $maxX, $maxY, $maxZ))){
--$this->lightUpdatesCount;
$upd = $this->lightUpdates[$i];
$llayer = $upd >> 48;
if($layer == $llayer){
$maZ = $upd & 0xff;
$maY = ($upd >> 8) & 0xff;
$maX = ($upd >> 16) & 0xff;
$miZ = ($upd >> 24) & 0xff;
$miY = ($upd >> 32) & 0xff;
$miX = ($upd >> 40) & 0xff;
if($miX <= $minX && $miY <= $minY && $miZ <= $minZ && $maX >= $maxX && $maY >= $maxY && $maZ >= $maxZ){
console("light happen is scheduled to happen already");
return;
}
if($miX-1 > $minX || $miY-1 > $minY || $miZ-1 > $minZ || $maX+1 < $maxX || $maY+1 < $maxY || $maZ+1 < $maxZ){
continue;
}
if($miX < $minX) $minX = $miX;
if($miY < $minY) $minY = $miY;
if($miZ < $minZ) $minZ = $miZ;
if($maX > $maxX) $maxX = $maX;
if($maY > $maxY) $maxY = $maY;
if($maZ > $maxZ) $maxZ = $maZ;
if((($maxZ-$minZ)*($maX-$minY)*($maxX-$minX) - ($maX-$miX)*($maZ-$miZ)*($maY-$miY)) > 2){
continue;
}
$indx = $i;
goto update_light_in_array;
}
}*/
if($maxX < $minX){
$indx = ++$this->lightUpdateIndx;
/*if($maxX < $minX){
$s = $minX;
$minX = $maxX;
$maxX = $s;
@ -603,6 +656,8 @@ class Level{
$minZ = $maxZ;
$maxZ = $s;
}
update_light_in_array:
if($minX < 0) $minX = 0;
if($minX > 255) $minX = 255;
if($minY < 0) $minY = 0;
@ -615,10 +670,10 @@ class Level{
if($maxY < 0) $minY = 0;
if($maxY > 127) $minY = 127;
if($maxZ < 0) $maxZ = 0;
if($maxZ > 255) $maxZ = 255;
if($maxZ > 255) $maxZ = 255;*/
$update = ($layer << 48) | ($minX << 40) | ($minY << 32) | ($minZ << 24) | ($maxX << 16) | ($maxY << 8) | ($maxZ);
$this->lightUpdates[++$this->lightUpdateIndx] = $update;
$this->lightUpdates[$indx] = $update;
}
public function updateLightIfOtherThan($layer, $x, $y, $z, $level){
@ -655,6 +710,8 @@ class Level{
return false;
}
public function onTick(PocketMinecraftServer $server, $currentTime){
$this->level->forceLightUpdatesIfNeeded();
if(!$this->stopTime) ++$this->time;
for($cX = 0; $cX < 16; ++$cX){
for($cZ = 0; $cZ < 16; ++$cZ){

View file

@ -88,7 +88,7 @@ class MobSpawner{
$zMob = $z + mt_rand(-3, 3);
$y = $this->getSafeY($xMob, $zMob, $grassOnly, $type >= 32 && $type <= 36 && $type != 35);
$y = $this->getSafeY($xMob, $zMob, $grassOnly, $type >= 32 && $type <= 36 && $type != 35, $type >= 32 && $type <= 36);
if(!$y || $y < 0){
continue;
}
@ -125,7 +125,7 @@ class MobSpawner{
];
}
protected function getSafeY($x, $z, $grassOnly = false, $highMob = false){ //first safe block //TODO check boundingbox
protected function getSafeY($x, $z, $grassOnly = false, $highMob = false, $isMonster = false){ //first safe block //TODO check boundingbox
$allowed = [];
for($y = 0; $y < 128; ++$y){
$b = $this->level->level->getBlockID($x, $y, $z);
@ -136,6 +136,9 @@ class MobSpawner{
(StaticBlock::getIsSolid($b1) && (!$grassOnly || $b1 === GRASS) &&
(!$highMob || !StaticBlock::getIsSolid($b2) && !StaticBlock::getIsLiquid($b2)))
){
if($isMonster && ($rb = $this->level->getRawBrightness($x, $y, $z)) > 8){ //dont spawn if too bright
continue;
}
$allowed[] = $y;
}
}