World: extract getBlockCollisionBoxes() from getCollisionBoxes()

closes #6104

This function has been a footgun for anyone using it, since it also returns entity AABBs by default.
In all core use cases, this functionality was disabled, and we were paying a needless (admittedly micro) performance penalty for passing the extra useless argument and useless condition check.
This commit is contained in:
Dylan K. Taylor 2023-10-19 12:52:02 +01:00
parent 48dcf0e32c
commit c7a358a56f
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 19 additions and 6 deletions

View File

@ -842,7 +842,7 @@ abstract class Entity{
protected function checkObstruction(float $x, float $y, float $z) : bool{ protected function checkObstruction(float $x, float $y, float $z) : bool{
$world = $this->getWorld(); $world = $this->getWorld();
if(count($world->getCollisionBoxes($this, $this->getBoundingBox(), false)) === 0){ if(count($world->getBlockCollisionBoxes($this->boundingBox)) === 0){
return false; return false;
} }
@ -1144,7 +1144,7 @@ abstract class Entity{
assert(abs($dx) <= 20 && abs($dy) <= 20 && abs($dz) <= 20, "Movement distance is excessive: dx=$dx, dy=$dy, dz=$dz"); assert(abs($dx) <= 20 && abs($dy) <= 20 && abs($dz) <= 20, "Movement distance is excessive: dx=$dx, dy=$dy, dz=$dz");
$list = $this->getWorld()->getCollisionBoxes($this, $moveBB->addCoord($dx, $dy, $dz), false); $list = $this->getWorld()->getBlockCollisionBoxes($moveBB->addCoord($dx, $dy, $dz));
foreach($list as $bb){ foreach($list as $bb){
$dy = $bb->calculateYOffset($moveBB, $dy); $dy = $bb->calculateYOffset($moveBB, $dy);
@ -1176,7 +1176,7 @@ abstract class Entity{
$stepBB = clone $this->boundingBox; $stepBB = clone $this->boundingBox;
$list = $this->getWorld()->getCollisionBoxes($this, $stepBB->addCoord($dx, $dy, $dz), false); $list = $this->getWorld()->getBlockCollisionBoxes($stepBB->addCoord($dx, $dy, $dz));
foreach($list as $bb){ foreach($list as $bb){
$dy = $bb->calculateYOffset($stepBB, $dy); $dy = $bb->calculateYOffset($stepBB, $dy);
} }

View File

@ -1526,7 +1526,7 @@ class World implements ChunkManager{
* *
* @return AxisAlignedBB[] * @return AxisAlignedBB[]
*/ */
private function getCollisionBoxesForCell(int $x, int $y, int $z) : array{ private function getBlockCollisionBoxesForCell(int $x, int $y, int $z) : array{
$block = $this->getBlockAt($x, $y, $z); $block = $this->getBlockAt($x, $y, $z);
$boxes = $block->getCollisionBoxes(); $boxes = $block->getCollisionBoxes();
@ -1547,7 +1547,7 @@ class World implements ChunkManager{
* @return AxisAlignedBB[] * @return AxisAlignedBB[]
* @phpstan-return list<AxisAlignedBB> * @phpstan-return list<AxisAlignedBB>
*/ */
public function getCollisionBoxes(Entity $entity, AxisAlignedBB $bb, bool $entities = true) : array{ public function getBlockCollisionBoxes(AxisAlignedBB $bb) : array{
$minX = (int) floor($bb->minX); $minX = (int) floor($bb->minX);
$minY = (int) floor($bb->minY); $minY = (int) floor($bb->minY);
$minZ = (int) floor($bb->minZ); $minZ = (int) floor($bb->minZ);
@ -1563,7 +1563,7 @@ class World implements ChunkManager{
for($y = $minY; $y <= $maxY; ++$y){ for($y = $minY; $y <= $maxY; ++$y){
$relativeBlockHash = World::chunkBlockHash($x, $y, $z); $relativeBlockHash = World::chunkBlockHash($x, $y, $z);
$boxes = $this->blockCollisionBoxCache[$chunkPosHash][$relativeBlockHash] ??= $this->getCollisionBoxesForCell($x, $y, $z); $boxes = $this->blockCollisionBoxCache[$chunkPosHash][$relativeBlockHash] ??= $this->getBlockCollisionBoxesForCell($x, $y, $z);
foreach($boxes as $blockBB){ foreach($boxes as $blockBB){
if($blockBB->intersectsWith($bb)){ if($blockBB->intersectsWith($bb)){
@ -1574,6 +1574,19 @@ class World implements ChunkManager{
} }
} }
return $collides;
}
/**
* @deprecated Use {@link World::getBlockCollisionBoxes()} instead (alongside {@link World::getCollidingEntities()}
* if entity collision boxes are also required).
*
* @return AxisAlignedBB[]
* @phpstan-return list<AxisAlignedBB>
*/
public function getCollisionBoxes(Entity $entity, AxisAlignedBB $bb, bool $entities = true) : array{
$collides = $this->getBlockCollisionBoxes($bb);
if($entities){ if($entities){
foreach($this->getCollidingEntities($bb->expandedCopy(0.25, 0.25, 0.25), $entity) as $ent){ foreach($this->getCollidingEntities($bb->expandedCopy(0.25, 0.25, 0.25), $entity) as $ent){
$collides[] = clone $ent->boundingBox; $collides[] = clone $ent->boundingBox;