Skip to content

Commit

Permalink
Revert collision box checks optimizations (pmmp#6606: pmmp#6606)
Browse files Browse the repository at this point in the history
  • Loading branch information
krivey committed Feb 18, 2025
1 parent 919f627 commit 01445d0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 43 deletions.
2 changes: 0 additions & 2 deletions src/block/RuntimeBlockStateRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ private function fillStaticArrays(int $index, Block $block) : void{
if($block->blocksDirectSkyLight()){
$this->blocksDirectSkyLight[$index] = true;
}

$this->collisionInfo[$index] = self::calculateCollisionInfo($block);
}
}

Expand Down
57 changes: 16 additions & 41 deletions src/world/World.php
Original file line number Diff line number Diff line change
Expand Up @@ -1627,18 +1627,13 @@ public function getCollisionBlocks(AxisAlignedBB $bb, bool $targetFirst = false)

$collides = [];

$collisionInfo = $this->blockStateRegistry->collisionInfo;
if($targetFirst){
for($z = $minZ; $z <= $maxZ; ++$z){
for($x = $minX; $x <= $maxX; ++$x){
for($y = $minY; $y <= $maxY; ++$y){
$stateCollisionInfo = $this->getBlockCollisionInfo($x, $y, $z, $collisionInfo);
if(match($stateCollisionInfo){
RuntimeBlockStateRegistry::COLLISION_CUBE => true,
RuntimeBlockStateRegistry::COLLISION_NONE => false,
default => $this->getBlockAt($x, $y, $z)->collidesWithBB($bb)
}){
return [$this->getBlockAt($x, $y, $z)];
$block = $this->getBlockAt($x, $y, $z);
if($block->collidesWithBB($bb)){
return [$block];
}
}
}
Expand All @@ -1647,13 +1642,9 @@ public function getCollisionBlocks(AxisAlignedBB $bb, bool $targetFirst = false)
for($z = $minZ; $z <= $maxZ; ++$z){
for($x = $minX; $x <= $maxX; ++$x){
for($y = $minY; $y <= $maxY; ++$y){
$stateCollisionInfo = $this->getBlockCollisionInfo($x, $y, $z, $collisionInfo);
if(match($stateCollisionInfo){
RuntimeBlockStateRegistry::COLLISION_CUBE => true,
RuntimeBlockStateRegistry::COLLISION_NONE => false,
default => $this->getBlockAt($x, $y, $z)->collidesWithBB($bb)
}){
$collides[] = $this->getBlockAt($x, $y, $z);
$block = $this->getBlockAt($x, $y, $z);
if($block->collidesWithBB($bb)){
$collides[] = $block;
}
}
}
Expand Down Expand Up @@ -1696,31 +1687,15 @@ private function getBlockCollisionInfo(int $x, int $y, int $z, array $collisionI
* @return AxisAlignedBB[]
* @phpstan-return list<AxisAlignedBB>
*/
private function getBlockCollisionBoxesForCell(int $x, int $y, int $z, array $collisionInfo) : array{
$stateCollisionInfo = $this->getBlockCollisionInfo($x, $y, $z, $collisionInfo);
$boxes = match($stateCollisionInfo){
RuntimeBlockStateRegistry::COLLISION_NONE => [],
RuntimeBlockStateRegistry::COLLISION_CUBE => [AxisAlignedBB::one()->offset($x, $y, $z)],
default => $this->getBlockAt($x, $y, $z)->getCollisionBoxes()
};

//overlapping AABBs can't make any difference if this is a cube, so we can save some CPU cycles in this common case
if($stateCollisionInfo !== RuntimeBlockStateRegistry::COLLISION_CUBE){
$cellBB = null;
foreach(Facing::OFFSET as [$dx, $dy, $dz]){
$offsetX = $x + $dx;
$offsetY = $y + $dy;
$offsetZ = $z + $dz;
$stateCollisionInfo = $this->getBlockCollisionInfo($offsetX, $offsetY, $offsetZ, $collisionInfo);
if($stateCollisionInfo === RuntimeBlockStateRegistry::COLLISION_MAY_OVERFLOW){
//avoid allocating this unless it's needed
$cellBB ??= AxisAlignedBB::one()->offset($x, $y, $z);
$extraBoxes = $this->getBlockAt($offsetX, $offsetY, $offsetZ)->getCollisionBoxes();
foreach($extraBoxes as $extraBox){
if($extraBox->intersectsWith($cellBB)){
$boxes[] = $extraBox;
}
}
private function getBlockCollisionBoxesForCell(int $x, int $y, int $z) : array{

Check failure on line 1690 in src/world/World.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

PHPDoc tag @param references unknown parameter: $collisionInfo

Check failure on line 1690 in src/world/World.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

PHPDoc tag @param references unknown parameter: $collisionInfo

Check failure on line 1690 in src/world/World.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

PHPDoc tag @param references unknown parameter: $collisionInfo
$block = $this->getBlockAt($x, $y, $z);
$boxes = $block->getCollisionBoxes();
$cellBB = AxisAlignedBB::one()->offset($x, $y, $z);
foreach(Facing::OFFSET as [$dx, $dy, $dz]){
$extraBoxes = $this->getBlockAt($x + $dx, $y + $dy, $z + $dz)->getCollisionBoxes();
foreach($extraBoxes as $extraBox){
if($extraBox->intersectsWith($cellBB)){
$boxes[] = $extraBox;
}
}
}
Expand Down Expand Up @@ -1750,7 +1725,7 @@ public function getBlockCollisionBoxes(AxisAlignedBB $bb) : array{
for($y = $minY; $y <= $maxY; ++$y){
$relativeBlockHash = World::chunkBlockHash($x, $y, $z);

$boxes = $this->blockCollisionBoxCache[$chunkPosHash][$relativeBlockHash] ??= $this->getBlockCollisionBoxesForCell($x, $y, $z, $collisionInfo);
$boxes = $this->blockCollisionBoxCache[$chunkPosHash][$relativeBlockHash] ??= $this->getBlockCollisionBoxesForCell($x, $y, $z);

foreach($boxes as $blockBB){
if($blockBB->intersectsWith($bb)){
Expand Down

0 comments on commit 01445d0

Please sign in to comment.