From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Martijn Muijsers Date: Wed, 23 Nov 2022 16:15:43 +0100 Subject: [PATCH] Strip raytracing for EntityLiving#hasLineOfSight License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) Gale - https://galemc.org This patch is based on the following patch: "Strip raytracing for EntityLiving#hasLineOfSight" By: Paul Sauve As part of: Airplane (https://github.com/TECHNOVE/Airplane) Licensed under: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) * Airplane description * The IBlockAccess#rayTrace method is very wasteful in both allocations, and in logic. While EntityLiving#hasLineOfSight provides static parameters for collisions with blocks and fluids, the method still does a lot of dynamic checks for both of these, which result in extra work. As well, since the fluid collision option is set to NONE, the entire fluid collision system is completely unneeded, yet used anyways. * Airplane copyright * Airplane Copyright (C) 2020 Technove LLC This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java index 3a116b37a89306ea8f2b7eec867d0e3c3523f74c..6d7f51274537f5041c9f98923bb72b9fc9ddd295 100644 --- a/src/main/java/net/minecraft/world/entity/LivingEntity.java +++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java @@ -3629,7 +3629,7 @@ public abstract class LivingEntity extends Entity { Vec3 vec3d1 = new Vec3(entity.getX(), entity.getEyeY(), entity.getZ()); // Paper - diff on change - used in CraftLivingEntity#hasLineOfSight(Location) and CraftWorld#lineOfSightExists - return vec3d1.distanceToSqr(vec3d) > 128D * 128D ? false : this.level.clip(new ClipContext(vec3d, vec3d1, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, this)).getType() == HitResult.Type.MISS; // Paper - use distanceToSqr + return vec3d1.distanceToSqr(vec3d) > 128D * 128D ? false : this.level.rayTraceDirect(vec3d, vec3d1, net.minecraft.world.phys.shapes.CollisionContext.of(this)) == net.minecraft.world.phys.BlockHitResult.Type.MISS; // Paper - use distanceToSqr // Gale - Airplane - strip raytracing for EntityLiving#hasLineOfSight } } diff --git a/src/main/java/net/minecraft/world/level/BlockGetter.java b/src/main/java/net/minecraft/world/level/BlockGetter.java index d1eefa6ef3e9abfe7af4d8310aa64465fa2d5463..c970be55461f3063ec22ec0490781b974db134f6 100644 --- a/src/main/java/net/minecraft/world/level/BlockGetter.java +++ b/src/main/java/net/minecraft/world/level/BlockGetter.java @@ -73,6 +73,16 @@ public interface BlockGetter extends LevelHeightAccessor { }); } + // Gale start - Airplane - strip raytracing for EntityLiving#hasLineOfSight - broken down variant of below rayTraceBlock, used by World#rayTraceDirect + default net.minecraft.world.phys.BlockHitResult.Type rayTraceBlockDirect(Vec3 vec3d, Vec3 vec3d1, BlockPos blockposition, BlockState iblockdata, net.minecraft.world.phys.shapes.CollisionContext voxelshapecoll) { + if (iblockdata.isAir()) return null; // Tuinity - optimise air cases + VoxelShape voxelshape = ClipContext.Block.COLLIDER.get(iblockdata, this, blockposition, voxelshapecoll); + net.minecraft.world.phys.BlockHitResult movingobjectpositionblock = this.clipWithInteractionOverride(vec3d, vec3d1, blockposition, voxelshape, iblockdata); + + return movingobjectpositionblock == null ? null : movingobjectpositionblock.getType(); + } + // Gale end - Airplane - strip raytracing for EntityLiving#hasLineOfSight - broken down variant of below rayTraceBlock, used by World#rayTraceDirect + // CraftBukkit start - moved block handling into separate method for use by Block#rayTrace default BlockHitResult clip(ClipContext raytrace1, BlockPos blockposition) { // Paper start - Prevent raytrace from loading chunks diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java index d92a2ac99b22ae8617c295b44e6b8cab2a95ae48..924e507a67e87c56346e7ae0ba6ef3fd8f72f853 100644 --- a/src/main/java/net/minecraft/world/level/Level.java +++ b/src/main/java/net/minecraft/world/level/Level.java @@ -403,6 +403,91 @@ public abstract class Level implements LevelAccessor, AutoCloseable { return null; } + // Gale start - Airplane - strip raytracing for EntityLiving#hasLineOfSight - broken down method of raytracing for EntityLiving#hasLineOfSight, replaces IBlockAccess#rayTrace(RayTrace) + public net.minecraft.world.phys.BlockHitResult.Type rayTraceDirect(net.minecraft.world.phys.Vec3 vec3d, net.minecraft.world.phys.Vec3 vec3d1, net.minecraft.world.phys.shapes.CollisionContext voxelshapecoll) { + // most of this code comes from IBlockAccess#a(RayTrace, BiFunction, Function), but removes the needless functions + if (vec3d.equals(vec3d1)) { + return net.minecraft.world.phys.BlockHitResult.Type.MISS; + } + + double endX = Mth.lerp(-1.0E-7D, vec3d1.x, vec3d.x); + double endY = Mth.lerp(-1.0E-7D, vec3d1.y, vec3d.y); + double endZ = Mth.lerp(-1.0E-7D, vec3d1.z, vec3d.z); + + double startX = Mth.lerp(-1.0E-7D, vec3d.x, vec3d1.x); + double startY = Mth.lerp(-1.0E-7D, vec3d.y, vec3d1.y); + double startZ = Mth.lerp(-1.0E-7D, vec3d.z, vec3d1.z); + + int currentX = Mth.floor(startX); + int currentY = Mth.floor(startY); + int currentZ = Mth.floor(startZ); + + BlockPos.MutableBlockPos currentBlock = new BlockPos.MutableBlockPos(currentX, currentY, currentZ); + + LevelChunk chunk = this.getChunkIfLoaded(currentBlock); + if (chunk == null) { + return net.minecraft.world.phys.BlockHitResult.Type.MISS; + } + + net.minecraft.world.phys.BlockHitResult.Type initialCheck = this.rayTraceBlockDirect(vec3d, vec3d1, currentBlock, chunk.getBlockState(currentBlock), voxelshapecoll); + + if (initialCheck != null) { + return initialCheck; + } + + double diffX = endX - startX; + double diffY = endY - startY; + double diffZ = endZ - startZ; + + int xDirection = Mth.sign(diffX); + int yDirection = Mth.sign(diffY); + int zDirection = Mth.sign(diffZ); + + double normalizedX = xDirection == 0 ? Double.MAX_VALUE : (double) xDirection / diffX; + double normalizedY = yDirection == 0 ? Double.MAX_VALUE : (double) yDirection / diffY; + double normalizedZ = zDirection == 0 ? Double.MAX_VALUE : (double) zDirection / diffZ; + + double normalizedXDirection = normalizedX * (xDirection > 0 ? 1.0D - Mth.frac(startX) : Mth.frac(startX)); + double normalizedYDirection = normalizedY * (yDirection > 0 ? 1.0D - Mth.frac(startY) : Mth.frac(startY)); + double normalizedZDirection = normalizedZ * (zDirection > 0 ? 1.0D - Mth.frac(startZ) : Mth.frac(startZ)); + + net.minecraft.world.phys.BlockHitResult.Type result; + + do { + if (normalizedXDirection > 1.0D && normalizedYDirection > 1.0D && normalizedZDirection > 1.0D) { + return net.minecraft.world.phys.BlockHitResult.Type.MISS; + } + + if (normalizedXDirection < normalizedYDirection) { + if (normalizedXDirection < normalizedZDirection) { + currentX += xDirection; + normalizedXDirection += normalizedX; + } else { + currentZ += zDirection; + normalizedZDirection += normalizedZ; + } + } else if (normalizedYDirection < normalizedZDirection) { + currentY += yDirection; + normalizedYDirection += normalizedY; + } else { + currentZ += zDirection; + normalizedZDirection += normalizedZ; + } + + currentBlock.set(currentX, currentY, currentZ); + if (chunk.getPos().x != currentBlock.getX() >> 4 || chunk.getPos().z != currentBlock.getZ() >> 4) { + chunk = this.getChunkIfLoaded(currentBlock); + if (chunk == null) { + return net.minecraft.world.phys.BlockHitResult.Type.MISS; + } + } + result = this.rayTraceBlockDirect(vec3d, vec3d1, currentBlock, chunk.getBlockState(currentBlock), voxelshapecoll); + } while (result == null); + + return result; + } + // Gale end - Airplane - strip raytracing for EntityLiving#hasLineOfSight - broken down method of raytracing for EntityLiving#hasLineOfSight, replaces IBlockAccess#rayTrace(RayTrace) + public boolean isInWorldBounds(BlockPos pos) { return pos.isInsideBuildHeightAndWorldBoundsHorizontal(this); // Paper - use better/optimized check }