From d8b2752226985bfbe8a0c07c93e3880989658641 Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Wed, 29 Jan 2025 19:40:06 -0800 Subject: [PATCH] raycast from simulated player position --- .../zenith/command/impl/RaycastCommand.java | 4 ++ .../feature/world/raycast/RaycastHelper.java | 43 ++++++++++++++++--- .../zenith/module/impl/PlayerSimulation.java | 2 +- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/zenith/command/impl/RaycastCommand.java b/src/main/java/com/zenith/command/impl/RaycastCommand.java index 53dcb65f..0131a374 100644 --- a/src/main/java/com/zenith/command/impl/RaycastCommand.java +++ b/src/main/java/com/zenith/command/impl/RaycastCommand.java @@ -1,6 +1,7 @@ package com.zenith.command.impl; import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.zenith.Proxy; import com.zenith.command.Command; import com.zenith.command.CommandUsage; import com.zenith.command.brigadier.CommandCategory; @@ -25,6 +26,7 @@ public CommandUsage commandUsage() { public LiteralArgumentBuilder register() { return command("raycast").executes(c -> { var sim = MODULE.get(PlayerSimulation.class); + if (Proxy.getInstance().hasActivePlayer()) sim.syncFromCache(true); var result = RaycastHelper.playerBlockOrEntityRaycast(sim.getBlockReachDistance(), sim.getEntityInteractDistance()); var embed = c.getSource().getEmbed(); embed.title("Raycast Result") @@ -43,6 +45,7 @@ public LiteralArgumentBuilder register() { }}) .then(literal("e").executes(c -> { var sim = MODULE.get(PlayerSimulation.class); + if (Proxy.getInstance().hasActivePlayer()) sim.syncFromCache(true); var result = RaycastHelper.playerEntityRaycast(sim.getEntityInteractDistance()); c.getSource().getEmbed() .title("Raycast Result") @@ -53,6 +56,7 @@ public LiteralArgumentBuilder register() { })) .then(literal("b").executes(c -> { var sim = MODULE.get(PlayerSimulation.class); + if (Proxy.getInstance().hasActivePlayer()) sim.syncFromCache(true); var result = RaycastHelper.playerBlockRaycast(sim.getBlockReachDistance(), false); c.getSource().getEmbed() .title("Raycast Result") diff --git a/src/main/java/com/zenith/feature/world/raycast/RaycastHelper.java b/src/main/java/com/zenith/feature/world/raycast/RaycastHelper.java index e4bcc9f4..49be9b17 100644 --- a/src/main/java/com/zenith/feature/world/raycast/RaycastHelper.java +++ b/src/main/java/com/zenith/feature/world/raycast/RaycastHelper.java @@ -5,6 +5,7 @@ import com.zenith.feature.world.World; import com.zenith.mc.block.*; import com.zenith.mc.entity.EntityData; +import com.zenith.module.impl.PlayerSimulation; import com.zenith.util.math.MathHelper; import org.cloudburstmc.math.vector.Vector3d; @@ -15,7 +16,8 @@ public class RaycastHelper { public static BlockRaycastResult playerBlockRaycast(double maxDistance, boolean includeFluids) { - return blockRaycastFromPos(CACHE.getPlayerCache().getX(), CACHE.getPlayerCache().getEyeY(), CACHE.getPlayerCache().getZ(), CACHE.getPlayerCache().getYaw(), CACHE.getPlayerCache().getPitch(), maxDistance, includeFluids); + var sim = MODULE.get(PlayerSimulation.class); + return blockRaycastFromPos(sim.getX(), sim.getEyeY(), sim.getZ(), sim.getYaw(), sim.getPitch(), maxDistance, includeFluids); } public static BlockRaycastResult blockRaycastFromPos(double x, double y, double z, double yaw, double pitch, double maxDistance, boolean includeFluids) { @@ -83,7 +85,8 @@ public static BlockRaycastResult blockRaycast(double x1, double y1, double z1, / } public static EntityRaycastResult playerEntityRaycast(double maxDistance) { - return entityRaycastFromPos(CACHE.getPlayerCache().getX(), CACHE.getPlayerCache().getEyeY(), CACHE.getPlayerCache().getZ(), CACHE.getPlayerCache().getYaw(), CACHE.getPlayerCache().getPitch(), maxDistance); + var sim = MODULE.get(PlayerSimulation.class); + return entityRaycastFromPos(sim.getX(), sim.getEyeY(), sim.getZ(), sim.getYaw(), sim.getPitch(), maxDistance); } public static EntityRaycastResult entityRaycastFromPos(final double x, final double y, final double z, final float yaw, final float pitch, final double maxDistance) { @@ -122,6 +125,35 @@ private static EntityRaycastResult entityRaycast(final double x1, final double y return resultRaycast; } + // ignoring all intersections with other blocks and entities + public static EntityRaycastResult playerEyeRaycastThroughToTarget(Entity target, double entityReachDistance) { + var sim = MODULE.get(PlayerSimulation.class); + return playerEyeRaycastThroughToTarget(target, sim.getYaw(), sim.getPitch(), entityReachDistance); + } + + public static EntityRaycastResult playerEyeRaycastThroughToTarget(Entity target, float yaw, float pitch, double entityReachDistance) { + var sim = MODULE.get(PlayerSimulation.class); + final double x1 = sim.getX(); + final double y1 = sim.getEyeY(); + final double z1 = sim.getZ(); + var rayEndPos = MathHelper.calculateRayEndPos(x1, y1, z1, yaw, pitch, entityReachDistance); + final double startX = MathHelper.lerp(-1.0E-7, x1, rayEndPos.getX()); + final double startY = MathHelper.lerp(-1.0E-7, y1, rayEndPos.getY()); + final double startZ = MathHelper.lerp(-1.0E-7, z1, rayEndPos.getZ()); + final double endX = MathHelper.lerp(-1.0E-7, rayEndPos.getX(), x1); + final double endY = MathHelper.lerp(-1.0E-7, rayEndPos.getY(), y1); + final double endZ = MathHelper.lerp(-1.0E-7, rayEndPos.getZ(), z1); + EntityRaycastResult resultRaycast = EntityRaycastResult.miss(); + EntityData data = ENTITY_DATA.getEntityData(target.getEntityType()); + if (data == null) return resultRaycast; + LocalizedCollisionBox cb = entityCollisionBox(target, data); + RayIntersection intersection = cb.rayIntersection(startX, startY, startZ, endX, endY, endZ); + if (intersection != null) { + resultRaycast = new EntityRaycastResult(true, intersection, target); + } + return resultRaycast; + } + private static LocalizedCollisionBox entityCollisionBox(final Entity entity, final EntityData data) { double width = data.width(); double height = data.height(); @@ -181,11 +213,8 @@ private static BlockRaycastResult checkBlockRaycast( } public static BlockOrEntityRaycastResult playerBlockOrEntityRaycast(double blockReachDistance, double entityReachDistance) { - return blockOrEntityRaycastFromPos( - CACHE.getPlayerCache().getX(), CACHE.getPlayerCache().getEyeY(), CACHE.getPlayerCache().getZ(), - CACHE.getPlayerCache().getYaw(), CACHE.getPlayerCache().getPitch(), - blockReachDistance, entityReachDistance - ); + var sim = MODULE.get(PlayerSimulation.class); + return blockOrEntityRaycastFromPos(sim.getX(), sim.getEyeY(), sim.getZ(), sim.getYaw(), sim.getPitch(), blockReachDistance, entityReachDistance); } public static BlockOrEntityRaycastResult blockOrEntityRaycastFromPos(final double x, final double y, final double z, final float yaw, final float pitch, final double blockReachDistance, final double entityReachDistance) { diff --git a/src/main/java/com/zenith/module/impl/PlayerSimulation.java b/src/main/java/com/zenith/module/impl/PlayerSimulation.java index 2ad1abcd..7767c519 100644 --- a/src/main/java/com/zenith/module/impl/PlayerSimulation.java +++ b/src/main/java/com/zenith/module/impl/PlayerSimulation.java @@ -941,7 +941,7 @@ public void handleExplosion(final ClientboundExplodePacket packet) { this.velocity.add(packet.getPushX(), packet.getPushY(), packet.getPushZ()); } - private void syncFromCache(boolean full) { + public void syncFromCache(boolean full) { this.x = this.lastX = CACHE.getPlayerCache().getX(); this.y = this.lastY = CACHE.getPlayerCache().getY(); this.z = this.lastZ = CACHE.getPlayerCache().getZ();