time always updates, test entity optimizations

This commit is contained in:
GameHerobrine 2022-12-20 23:54:09 +03:00
parent 026daf485b
commit fc2fa6d2f8
5 changed files with 47 additions and 42 deletions

View file

@ -549,7 +549,7 @@ class Entity extends Position
if(Utils::in_range($this->speedZ, - 0.01, 0.01)){ if(Utils::in_range($this->speedZ, - 0.01, 0.01)){
$this->speedZ = 0; $this->speedZ = 0;
} }
if(Utils::in_range($this->speedY, - 0.001, 0.001)){ if(Utils::in_range($this->speedY, - 0.0007, 0.007)){
$this->speedY = 0; $this->speedY = 0;
} }
@ -636,7 +636,7 @@ class Entity extends Position
} }
} }
if(!$support){
for($z = $startZ; $z <= $endZ; ++ $z){ for($z = $startZ; $z <= $endZ; ++ $z){
for($x = $startX; $x <= $endX; ++ $x){ for($x = $startX; $x <= $endX; ++ $x){
$v = new Vector3($x, $y, $z); $v = new Vector3($x, $y, $z);
@ -665,6 +665,8 @@ class Entity extends Position
break; break;
} }
} }
}
if($this->speedX != 0){ if($this->speedX != 0){
$this->x += $this->speedX; $this->x += $this->speedX;
@ -711,18 +713,21 @@ class Entity extends Position
if($this->hasGravity && $support === false){ if($this->hasGravity && $support === false){
$this->speedY -= ($this->class === ENTITY_FALLING) ? 0.04 : ($this->class === ENTITY_ITEM ? 0.06 : 0.08); // TODO: replace with $gravity $this->speedY -= ($this->class === ENTITY_FALLING) ? 0.04 : ($this->class === ENTITY_ITEM ? 0.06 : 0.08); // TODO: replace with $gravity
$update = true; $update = true;
} else{ } elseif($this->lastX != $this->x && $this->lastZ != $this->z & $this->lastY != $this->z){
// $this->speedX = 0; // $this->speedX = 0;
// $this->speedY = 0; // $this->speedY = 0;
// $this->speedZ = 0; // $this->speedZ = 0;
$this->server->api->handle("entity.move", $this); $this->server->api->handle("entity.move", $this);
$update = true; $update = true;
} }
if($update === true){ if($update === true){
$hasUpdate = true; $hasUpdate = true;
if(($this->server->ticks % 4 === 0 && $this->class === ENTITY_ITEM) || $this->class != ENTITY_ITEM){ //update item speed every 4 ticks
$this->server->api->handle("entity.motion", $this); $this->server->api->handle("entity.motion", $this);
} }
}
} elseif($this->player instanceof Player){ } elseif($this->player instanceof Player){
if($isFlying === true and ($this->player->gamemode & 0x01) === 0x00){ if($isFlying === true and ($this->player->gamemode & 0x01) === 0x00){
if($this->fallY === false or $this->fallStart === false){ if($this->fallY === false or $this->fallStart === false){
@ -1523,7 +1528,7 @@ class Entity extends Position
* Debug * Debug
*/ */
public function printSpeed(){ public function printSpeed(){
console("{$this->speedX}:{$this->speedY}:{$this->speedZ}"); ConsoleAPI::debug("{$this->speedX}:{$this->speedY}:{$this->speedZ}");
} }
/* /*

View file

@ -101,7 +101,7 @@ class NetherReactorBlock extends SolidBlock{
$shiftZ = sin(floor(Utils::randomFloat()*360)*(pi()/180)); $shiftZ = sin(floor(Utils::randomFloat()*360)*(pi()/180));
if(Utils::chance(5)) $randomID = $this->rarePossibleLoot[array_rand($this->rarePossibleLoot)]; if(Utils::chance(5)) $randomID = $this->rarePossibleLoot[array_rand($this->rarePossibleLoot)];
else $randomID = $this->possibleLoot[array_rand($this->possibleLoot)]; else $randomID = $this->possibleLoot[array_rand($this->possibleLoot)];
$server->api->entity->drop(new Position($x+($shiftX*$randomRange)+0.5, $y-1, $z+($shiftZ*$randomRange)+0.5, $this->level), BlockAPI::getItem($randomID, 0, 1)); $server->api->entity->drop(new Position($x+($shiftX*$randomRange)+0.5, $y, $z+($shiftZ*$randomRange)+0.5, $this->level), BlockAPI::getItem($randomID, 0, 1));
} }
for($i = 0; $i < $pigmen; $i++) { for($i = 0; $i < $pigmen; $i++) {
$randomRange = floor(Utils::randomFloat()*5+3); $randomRange = floor(Utils::randomFloat()*5+3);
@ -109,7 +109,7 @@ class NetherReactorBlock extends SolidBlock{
$shiftZ = sin(floor(Utils::randomFloat()*360)*(pi()/180)); $shiftZ = sin(floor(Utils::randomFloat()*360)*(pi()/180));
$data = array( $data = array(
"x" => $x+($shiftX*$randomRange)+0.5, "x" => $x+($shiftX*$randomRange)+0.5,
"y" => $y-1, "y" => $y,
"z" => $z+($shiftZ*$randomRange)+0.5, "z" => $z+($shiftZ*$randomRange)+0.5,
); );
$e = $server->api->entity->add($this->level, ENTITY_MOB, MOB_PIGMAN, $data); $e = $server->api->entity->add($this->level, ENTITY_MOB, MOB_PIGMAN, $data);

View file

@ -489,9 +489,7 @@ class PMFLevel extends PMF{
$pos = new Position($x, $y, $z, $this->level); $pos = new Position($x, $y, $z, $this->level);
for($side = 0; $side <= 5; ++$side){ for($side = 0; $side <= 5; ++$side){
$b = $pos->getSide($side); $b = $pos->getSide($side);
if($b instanceof LavaBlock) ServerAPI::request()->api->block->scheduleBlockUpdate($b, ($b instanceof LavaBlock ? 40: 10), BLOCK_UPDATE_NORMAL);
ServerAPI::request()->api->block->scheduleBlockUpdate(new Position($b, 0, 0, $this->level), 40, BLOCK_UPDATE_NORMAL);
else ServerAPI::request()->api->block->scheduleBlockUpdate(new Position($b, 0, 0, $this->level), 10, BLOCK_UPDATE_NORMAL);
} }
} }
return true; return true;

View file

@ -28,6 +28,7 @@ class Explosion{
} }
public function explode(){ public function explode(){
console("a");
if(!Explosion::$enableExplosions){ /*Disable Explosions*/ if(!Explosion::$enableExplosions){ /*Disable Explosions*/
return; return;
} }
@ -40,7 +41,7 @@ class Explosion{
]) === false){ ]) === false){
return false; return false;
} }
console("b");
$mRays = $this->rays - 1; $mRays = $this->rays - 1;
for($i = 0; $i < $this->rays; ++$i){ for($i = 0; $i < $this->rays; ++$i){
for($j = 0; $j < $this->rays; ++$j){ for($j = 0; $j < $this->rays; ++$j){
@ -59,6 +60,7 @@ class Explosion{
$block->x = $vBlock->x; $block->x = $vBlock->x;
$block->y = $vBlock->y; $block->y = $vBlock->y;
$block->z = $vBlock->z; $block->z = $vBlock->z;
$block->level = $this->level;
$blastForce -= ($block->getHardness() / 5 + 0.3) * $this->stepLen; $blastForce -= ($block->getHardness() / 5 + 0.3) * $this->stepLen;
if($blastForce > 0){ if($blastForce > 0){
$index = ($block->x << 15) + ($block->z << 7) + $block->y; $index = ($block->x << 15) + ($block->z << 7) + $block->y;
@ -84,7 +86,6 @@ class Explosion{
} }
foreach($this->affectedBlocks as $block){ foreach($this->affectedBlocks as $block){
if($block instanceof TNTBlock){ if($block instanceof TNTBlock){
$data = [ $data = [
"x" => $block->x + 0.5, "x" => $block->x + 0.5,
@ -100,7 +101,7 @@ class Explosion{
$server->api->entity->drop(new Position($block->x + 0.5, $block->y, $block->z + 0.5, $this->level), BlockAPI::getItem($drop[0], $drop[1], $drop[2])); //id, meta, count $server->api->entity->drop(new Position($block->x + 0.5, $block->y, $block->z + 0.5, $this->level), BlockAPI::getItem($drop[0], $drop[1], $drop[2])); //id, meta, count
} }
} }
$this->level->setBlock($block, new AirBlock(), true, false, true); $this->level->setBlock($block, new AirBlock(), true);
$send[] = $block->subtract($source); $send[] = $block->subtract($source);
} }
$pk = new ExplodePacket; $pk = new ExplodePacket;

View file

@ -261,6 +261,8 @@ class Level{
public function onTick(PocketMinecraftServer $server){ public function onTick(PocketMinecraftServer $server){
//$ents = $server->api->entity->getAll($this); //$ents = $server->api->entity->getAll($this);
if(!$this->stopTime) ++$this->time;
ConsoleAPI::debug($this->getTime());
foreach($this->entityList as $k => $e){ foreach($this->entityList as $k => $e){
if(!($e instanceof Entity)){ if(!($e instanceof Entity)){
unset($this->entityList[$k]); unset($this->entityList[$k]);
@ -280,14 +282,12 @@ class Level{
if(!isset($this->level) or (($pos instanceof Position) and $pos->level !== $this) or $pos->x < 0 or $pos->y < 0 or $pos->z < 0){ if(!isset($this->level) or (($pos instanceof Position) and $pos->level !== $this) or $pos->x < 0 or $pos->y < 0 or $pos->z < 0){
return false; return false;
} }
$ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getMetadata()); $ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getMetadata());
if($ret === true){ if($ret === true){
if(!($pos instanceof Position)){ if(!($pos instanceof Position)){
$pos = new Position($pos->x, $pos->y, $pos->z, $this); $pos = new Position($pos->x, $pos->y, $pos->z, $this);
} }
$block->position($pos); $block->position($pos);
if($direct === true){ if($direct === true){
$pk = new UpdateBlockPacket; $pk = new UpdateBlockPacket;
$pk->x = $pos->x; $pk->x = $pos->x;
@ -500,13 +500,14 @@ class Level{
}else{ }else{
$time = $this->startTime + ($now - $this->startCheck) * 20; $time = $this->startTime + ($now - $this->startCheck) * 20;
} }
if($this->server->api->dhandle("time.change", ["level" => $this, "time" => $time]) !== false){ if($this->server->api->dhandle("time.change", ["level" => $this, "time" => $time]) !== false){ //send time to player every 5 ticks
$this->time = $time; $this->time = $time;
$pk = new SetTimePacket; $pk = new SetTimePacket;
$pk->time = (int) $this->time; $pk->time = (int) $this->time;
$pk->started = $this->stopTime == false; $pk->started = $this->stopTime == false;
$this->server->api->player->broadcastPacket($this->players, $pk); $this->server->api->player->broadcastPacket($this->players, $pk);
}else{
$this->time -= 20 * 13;
} }
} }