Skip to content

Commit

Permalink
Deobfuscate and document purpose of misunderstood code in traverseBlo…
Browse files Browse the repository at this point in the history
…cks()
  • Loading branch information
Axionize committed Sep 17, 2024
1 parent e2f3722 commit 1355454
Showing 1 changed file with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,29 @@ public static HitData traverseBlocks(GrimPlayer player, double[] start, double[]
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 as long as the ray hasn't reached its end point along at least one axis.
// In each iteration, it moves to the next block boundary along the axis with the smallest tMax value,
// updates the corresponding coordinate, and checks for a hit in the new block, Google "3D DDA" for more info
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);
Expand Down Expand Up @@ -851,8 +854,6 @@ public static HitData getNearestReachHitResult(GrimPlayer player, double[] eyePo
eyePos[2] + lookVec[2] * maxDistance
};

StateType heldItem = null;

return traverseBlocks(player, eyePos, endPos, (block, vector3i) -> {
ClientVersion clientVersion = player.getClientVersion();
CollisionBox data = HitboxData.getBlockHitbox(player, null, clientVersion, block, vector3i.getX(), vector3i.getY(), vector3i.getZ());
Expand Down

0 comments on commit 1355454

Please sign in to comment.