Minor performance improvement to explosions

This improves calculation time by 20-25% per explosion on flat terrain.
This commit is contained in:
Dylan K. Taylor 2017-06-30 17:31:41 +01:00
parent eeedcf7332
commit cd02f1e4be
2 changed files with 13 additions and 5 deletions

View File

@ -58,6 +58,8 @@ class Block extends Position implements BlockIds, Metadatable{
public static $transparent = null;
/** @var \SplFixedArray<bool> */
public static $diffusesSkyLight = null;
/** @var \SplFixedArray */
public static $blastResistance = null;
/**
* Initializes the block factory. By default this is called only once on server start, however you may wish to use
@ -75,6 +77,7 @@ class Block extends Position implements BlockIds, Metadatable{
self::$hardness = new \SplFixedArray(256);
self::$transparent = new \SplFixedArray(256);
self::$diffusesSkyLight = new \SplFixedArray(256);
self::$blastResistance = new \SplFixedArray(256);
self::registerBlock(new Air());
self::registerBlock(new Stone());
@ -366,6 +369,7 @@ class Block extends Position implements BlockIds, Metadatable{
self::$light[$id] = $block->getLightLevel();
self::$lightFilter[$id] = $block->getLightFilter() + 1; //opacity plus 1 standard light filter
self::$diffusesSkyLight[$id] = $block->diffusesSkyLight();
self::$blastResistance[$id] = $block->getBlastResistance();
}
/**

View File

@ -89,23 +89,27 @@ class Explosion{
$vBlock->x = $pointerX >= $x ? $x : $x - 1;
$vBlock->y = $pointerY >= $y ? $y : $y - 1;
$vBlock->z = $pointerZ >= $z ? $z : $z - 1;
if(!$this->level->isInWorld($vBlock->x, $vBlock->y, $vBlock->z)){
break;
}
$block = $this->level->getBlock($vBlock);
if($block->getId() !== 0){
$blastForce -= ($block->getBlastResistance() / 5 + 0.3) * $this->stepLen;
$blockId = $this->level->getBlockIdAt($vBlock->x, $vBlock->y, $vBlock->z);
if($blockId !== 0){
$blastForce -= (Block::$blastResistance[$blockId] / 5 + 0.3) * $this->stepLen;
if($blastForce > 0){
if(!isset($this->affectedBlocks[$index = Level::blockHash($block->x, $block->y, $block->z)])){
$this->affectedBlocks[$index] = $block;
if(!isset($this->affectedBlocks[$index = Level::blockHash($vBlock->x, $vBlock->y, $vBlock->z)])){
$this->affectedBlocks[$index] = $this->level->getBlock($vBlock);
}
}
}
$pointerX += $vector->x;
$pointerY += $vector->y;
$pointerZ += $vector->z;
}
}
}
}