small tweaks

This commit is contained in:
GameHerobrine 2024-01-01 08:38:39 +03:00
parent 68b8232f07
commit 6594e7d0ef
4 changed files with 31 additions and 38 deletions

View file

@ -26,7 +26,7 @@ class Vector2{
}
public function subtract($x = 0, $y = 0){
if(($x instanceof Vector2) === true){
if($x instanceof Vector2){
return $this->add(-$x->x, -$x->y);
}else{
return $this->add(-$x, -$y);
@ -34,7 +34,7 @@ class Vector2{
}
public function add($x = 0, $y = 0){
if(($x instanceof Vector2) === true){
if($x instanceof Vector2){
return $this->add($x->x, $x->y);
}else{
$this->x += $x;

View file

@ -135,7 +135,7 @@ class Vector3{
if(($x instanceof Vector3) === true){
return $this->distanceSquared($x->x, $x->y, $x->z);
}else{
return pow($this->x - $x, 2) + pow($this->y - $y, 2) + pow($this->z - $z, 2);
return ($this->x - $x)*($this->x - $x) + ($this->y - $y)*($this->y - $y) + ($this->z - $z)*($this->z - $z);
}
}

View file

@ -7,7 +7,7 @@ class PMFLevel extends PMF{
public $isLoaded = true;
private $levelData = [];
public $locationTable = [];
private $log = 4;
private $log = 4; //must be 4 or else rip world
private $payloadOffset = 0;
public $chunks = [];
public $chunkChange = [];
@ -18,7 +18,6 @@ class PMFLevel extends PMF{
$this->levelData = $blank;
$this->createBlank();
$this->isLoaded = true;
$this->log = (int) ((string) log($this->levelData["width"], 2));
}else{
if($this->load($file) !== false){
$this->parseInfo();
@ -26,7 +25,6 @@ class PMFLevel extends PMF{
$this->isLoaded = false;
}else{
$this->isLoaded = true;
$this->log = (int) ((string) log($this->levelData["width"], 2));
}
}else{
$this->isLoaded = false;
@ -37,7 +35,7 @@ class PMFLevel extends PMF{
private function createBlank(){
$this->saveData(false);
$this->locationTable = [];
$cnt = pow($this->levelData["width"], 2);
$cnt = $this->levelData["width"] * $this->levelData["width"];
$dirname = dirname($this->file) . "/chunks/";
if(!is_dir($dirname)){
@mkdir($dirname , 0755);
@ -96,8 +94,8 @@ class PMFLevel extends PMF{
}
public function getXZ($index, &$X = null, &$Z = null){
$X = $index >> $this->log;
$Z = $index & (pow($this->log, 2) - 1);
$X = $index >> 4;
$Z = $index & 0xf;
return [$X, $Z];
}
@ -187,7 +185,7 @@ class PMFLevel extends PMF{
}elseif($save !== false){
$this->saveChunk($X, $Z);
}
$index = $this->getIndex($X, $Z);
$index = self::getIndex($X, $Z);
$this->chunks[$index] = null;
$this->chunkChange[$index] = null;
unset($this->chunks[$index], $this->chunkChange[$index]);
@ -195,14 +193,14 @@ class PMFLevel extends PMF{
}
public function isChunkLoaded($X, $Z){
$index = $this->getIndex($X, $Z);
$index = self::getIndex($X, $Z);
if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){
return false;
}
return true;
}
public function getIndex($X, $Z){
public static function getIndex($X, $Z){
return ((int) $Z << 4) + (int) $X; //statically 4, setting it to something else would destroy everything
}
@ -212,7 +210,7 @@ class PMFLevel extends PMF{
if(!$this->isChunkLoaded($X, $Z)){
return false;
}
$index = $this->getIndex($X, $Z);
$index = self::getIndex($X, $Z);
if(!isset($this->chunkChange[$index]) or $this->chunkChange[$index][-1] === false){//No changes in chunk
return true;
}
@ -236,7 +234,7 @@ class PMFLevel extends PMF{
}
protected function isMiniChunkEmpty($X, $Z, $Y){
$index = $this->getIndex($X, $Z);
$index = self::getIndex($X, $Z);
if($this->chunks[$index][$Y] !== false){
if(substr_count($this->chunks[$index][$Y], "\x00") < 8192){
return false;
@ -249,7 +247,7 @@ class PMFLevel extends PMF{
if($this->loadChunk($X, $Z) === false){
return str_repeat("\x00", 8192);
}
$index = $this->getIndex($X, $Z);
$index = self::getIndex($X, $Z);
if(!isset($this->chunks[$index][$Y]) or $this->chunks[$index][$Y] === false){
return str_repeat("\x00", 8192);
}
@ -296,7 +294,7 @@ class PMFLevel extends PMF{
if($this->isChunkLoaded($X, $Z) === false){
return false;
}
$index = $this->getIndex($X, $Z);
$index = self::getIndex($X, $Z);
$this->chunks[$index][$Y] = str_repeat("\x00", 8192);
$this->chunkChange[$index][-1] = true;
$this->chunkChange[$index][$Y] = 8192;
@ -311,7 +309,7 @@ class PMFLevel extends PMF{
if(strlen($data) !== 8192){
return false;
}
$index = $this->getIndex($X, $Z);
$index = self::getIndex($X, $Z);
$this->chunks[$index][$Y] = (string) $data;
$this->chunkChange[$index][-1] = true;
$this->chunkChange[$index][$Y] = 8192;
@ -340,7 +338,7 @@ class PMFLevel extends PMF{
$X = $x >> 4;
$Z = $z >> 4;
$Y = $y >> 4;
$index = $this->getIndex($X, $Z);
$index = self::getIndex($X, $Z);
if(!isset($this->chunks[$index]) || $this->chunks[$index] === false || ($this->chunks[$index][$Y] === false)){
return 0;
}
@ -364,7 +362,7 @@ class PMFLevel extends PMF{
if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"] or $y < 0){
return false;
}
$index = $this->getIndex($X, $Z);
$index = self::getIndex($X, $Z);
$aX = $x & 0xf;
$aZ = $z & 0xf;
$aY = $y & 0xf;
@ -385,7 +383,7 @@ class PMFLevel extends PMF{
$X = $x >> 4;
$Z = $z >> 4;
$Y = $y >> 4;
$index = $this->getIndex($X, $Z);
$index = self::getIndex($X, $Z);
if(!isset($this->chunks[$index]) || $this->chunks[$index] === false || ($this->chunks[$index][$Y] === false)){
return 0;
}
@ -407,7 +405,7 @@ class PMFLevel extends PMF{
if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"] or $y < 0){
return false;
}
$index = $this->getIndex($X, $Z);
$index = self::getIndex($X, $Z);
$aX = $x & 0xf;
$aZ = $z & 0xf;
$aY = $y & 0xf;
@ -440,7 +438,7 @@ class PMFLevel extends PMF{
return [AIR, 0];
}
$index = $this->getIndex($X, $Z);
$index = self::getIndex($X, $Z);
if(!isset($this->chunks[$index]) || $this->chunks[$index] === false){
if($this->loadChunk($X, $Z) === false){
return [AIR, 0];
@ -475,8 +473,8 @@ class PMFLevel extends PMF{
if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){
return false;
}
$index = $this->getIndex($X, $Z);
if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){
$index = self::getIndex($X, $Z);
if(!isset($this->chunks[$index]) || $this->chunks[$index] === false){
if($this->loadChunk($X, $Z) === false){
return false;
}

View file

@ -294,13 +294,7 @@ class Level{
public function setBlockRaw(Vector3 $pos, Block $block, $direct = true, $send = true){
if(($ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getMetadata())) === true and $send !== false){
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->addBlockToSendQueue($pos->x, $pos->y, $pos->z, $block->id, $block->meta);
}elseif($direct === false){
if(!($pos instanceof Position)){
$pos = new Position($pos->x, $pos->y, $pos->z, $this);
@ -372,6 +366,14 @@ class Level{
if(!$e->isPlayer()) $post[] = $k;
}
}
$this->checkSleep();
if($server->ticks % 40 === 0){ //40 ticks delay
$this->mobSpawner->handle();
}
foreach($this->players as $player){
foreach($post as $eid){
$e = $this->entityList[$eid] ?? false;
@ -393,13 +395,6 @@ class Level{
}
$player->sendBlockUpdateQueue();
}
$this->checkSleep();
if($server->ticks % 40 === 0){ //40 ticks delay
$this->mobSpawner->handle();
}
}
public function addBlockToSendQueue($x, $y, $z, $id, $meta){