From c98c7f724ea8d4be2ff6cbfdf6efc81b9e6f75ae Mon Sep 17 00:00:00 2001 From: Axionize <154778082+Axionize@users.noreply.github.com> Date: Fri, 23 Aug 2024 01:37:48 -0400 Subject: [PATCH] Deobfuscate and document purpose of misunderstood code in traverseBlocks() --- .../grimac/utils/nmsutil/BlockRayTrace.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/ac/grim/grimac/utils/nmsutil/BlockRayTrace.java b/src/main/java/ac/grim/grimac/utils/nmsutil/BlockRayTrace.java index b66b1529d4..63c6f13ad9 100644 --- a/src/main/java/ac/grim/grimac/utils/nmsutil/BlockRayTrace.java +++ b/src/main/java/ac/grim/grimac/utils/nmsutil/BlockRayTrace.java @@ -60,26 +60,27 @@ public static HitData traverseBlocks(GrimPlayer player, Vector3d start, Vector3d double posYInverse = ySign == 0 ? Double.MAX_VALUE : ySign / yDiff; double posZInverse = zSign == 0 ? Double.MAX_VALUE : zSign / zDiff; - double d12 = posXInverse * (xSign > 0 ? 1.0D - GrimMath.frac(startX) : GrimMath.frac(startX)); - double d13 = posYInverse * (ySign > 0 ? 1.0D - GrimMath.frac(startY) : GrimMath.frac(startY)); - double d14 = posZInverse * (zSign > 0 ? 1.0D - GrimMath.frac(startZ) : GrimMath.frac(startZ)); - - // Can't figure out what this code does currently - while (d12 <= 1.0D || d13 <= 1.0D || d14 <= 1.0D) { - if (d12 < d13) { - if (d12 < d14) { + double tMaxX = posXInverse * (xSign > 0 ? 1.0D - GrimMath.frac(startX) : GrimMath.frac(startX)); + double tMaxY = posYInverse * (ySign > 0 ? 1.0D - GrimMath.frac(startY) : GrimMath.frac(startY)); + double tMaxZ = posZInverse * (zSign > 0 ? 1.0D - GrimMath.frac(startZ) : GrimMath.frac(startZ)); + + // tMax represents the maximum distance along each axis before crossing a block boundary + // The loop continues until the ray has crossed a block boundary along all axes + while (tMaxX <= 1.0D || tMaxY <= 1.0D || tMaxZ <= 1.0D) { + if (tMaxX < tMaxY) { + if (tMaxX < tMaxZ) { floorStartX += xSign; - d12 += posXInverse; + tMaxX += posXInverse; } else { floorStartZ += zSign; - d14 += posZInverse; + tMaxZ += posZInverse; } - } else if (d13 < d14) { + } else if (tMaxY < tMaxZ) { floorStartY += ySign; - d13 += posYInverse; + tMaxY += posYInverse; } else { floorStartZ += zSign; - d14 += posZInverse; + tMaxZ += posZInverse; } state = player.compensatedWorld.getWrappedBlockStateAt(floorStartX, floorStartY, floorStartZ);