diff --git a/src/main/java/ru/wilyfox/trajectory/sim/ArrowFlight.java b/src/main/java/ru/wilyfox/trajectory/sim/ArrowFlight.java index 9bc0b1e..cf83bed 100644 --- a/src/main/java/ru/wilyfox/trajectory/sim/ArrowFlight.java +++ b/src/main/java/ru/wilyfox/trajectory/sim/ArrowFlight.java @@ -203,5 +203,38 @@ static List entityHits( return hits; } + /** + * {@code ProjectileUtil.getEntityHitResult(level, source, from, to, area, matching, margin)}, + * reached through {@code getHitResultOnMoveVector} by throwables, fireworks, the fishing hook and + * the wind charge: each target box inflated by the margin is clipped by the segment, and the hit + * nearest to {@code from} wins, the earlier target on a tie. The location is where the segment + * enters the inflated box; unlike arrows there is no ray towards the center and no projection + * onto the real box. + * + * @return the nearest hit, or {@code null} if the segment touches no target + */ + static Hit nearestEntityHit( + WorldView world, Vec3d from, Vec3d to, Aabb area, float margin, boolean leftOwner) { + double nearest = Double.MAX_VALUE; + Hit hit = null; + for (WorldView.EntityTarget target : world.entities(area)) { + if (target.shooterGroup() && !leftOwner) { + continue; + } + + Vec3d location = target.box().inflate(margin).clip(from, to); + if (location == null) { + continue; + } + + double distance = from.distanceToSqr(location); + if (distance < nearest) { + nearest = distance; + hit = new Hit(target, location); + } + } + return hit; + } + record Hit(WorldView.EntityTarget target, Vec3d location) {} } diff --git a/src/main/java/ru/wilyfox/trajectory/sim/FireworkFlight.java b/src/main/java/ru/wilyfox/trajectory/sim/FireworkFlight.java index 8a9c4af..b0a4da3 100644 --- a/src/main/java/ru/wilyfox/trajectory/sim/FireworkFlight.java +++ b/src/main/java/ru/wilyfox/trajectory/sim/FireworkFlight.java @@ -1,7 +1,6 @@ package ru.wilyfox.trajectory.sim; import java.util.ArrayList; -import java.util.Comparator; import java.util.List; /** @@ -10,8 +9,8 @@ * *
    *
  1. no acceleration and no gravity: the velocity never changes; - *
  2. find the hit along the velocity ({@code ProjectileUtil.getHitResultOnMoveVector}, collider - * shapes, entity margin as for arrows); + *
  3. find the hit along the velocity ({@code ProjectileUtil.getHitResultOnMoveVector}: collider + * shapes, then the nearest entity box inflated by {@code computeMargin}); *
  4. move; a hit entity or a block ends the flight; *
  5. {@code life} grows by one; the rocket explodes once {@code life > lifetime}. *
@@ -69,25 +68,23 @@ public static Trajectory simulate(Vec3d spawn, Vec3d velocity, WorldView world, Aabb searchArea = box(pos).expandTowards(velocity).inflate(1.0); Vec3d origin = pos; - List hits = - ArrowFlight.entityHits( + ArrowFlight.Hit hit = + ArrowFlight.nearestEntityHit( world, origin, blocks.location(), searchArea, ArrowFlight.margin(tickCount), leftOwner); - if (!hits.isEmpty()) { - hits.sort(Comparator.comparingDouble(h -> origin.distanceToSqr(h.location()))); - ArrowFlight.Hit first = hits.getFirst(); - points.add(first.location()); + if (hit != null) { + points.add(hit.location()); return new Trajectory( points, Trajectory.End.ENTITY, tickCount, - first.target(), + hit.target(), null, - first.location(), + hit.location(), velocity, Math.min(certain, points.size())); } diff --git a/src/main/java/ru/wilyfox/trajectory/sim/FishingHookFlight.java b/src/main/java/ru/wilyfox/trajectory/sim/FishingHookFlight.java index 4cd8840..b8dfd81 100644 --- a/src/main/java/ru/wilyfox/trajectory/sim/FishingHookFlight.java +++ b/src/main/java/ru/wilyfox/trajectory/sim/FishingHookFlight.java @@ -1,7 +1,6 @@ package ru.wilyfox.trajectory.sim; import java.util.ArrayList; -import java.util.Comparator; import java.util.List; /** @@ -97,21 +96,13 @@ public static Trajectory simulate(Vec3d spawn, Vec3d velocity, WorldView world, } Vec3d origin = pos; Aabb area = box(pos).expandTowards(vel).inflate(1.0); - List hits = - ArrowFlight.entityHits( + ArrowFlight.Hit hit = + ArrowFlight.nearestEntityHit( world, origin, blocks.location(), area, ArrowFlight.margin(tickCount), leftOwner); - if (!hits.isEmpty()) { - hits.sort(Comparator.comparingDouble(h -> origin.distanceToSqr(h.target().position()))); - ArrowFlight.Hit first = hits.getFirst(); - points.add(first.location()); + if (hit != null) { + points.add(hit.location()); return new Trajectory( - points, - Trajectory.End.ENTITY, - tickCount, - first.target(), - null, - first.location(), - vel); + points, Trajectory.End.ENTITY, tickCount, hit.target(), null, hit.location(), vel); } if (blocks.type() == WorldView.BlockClip.Type.BLOCK) { // HitResult.distanceTo returns the squared distance: close to a block the hook slows down diff --git a/src/main/java/ru/wilyfox/trajectory/sim/ThrowableFlight.java b/src/main/java/ru/wilyfox/trajectory/sim/ThrowableFlight.java index ed0f74d..5f9baea 100644 --- a/src/main/java/ru/wilyfox/trajectory/sim/ThrowableFlight.java +++ b/src/main/java/ru/wilyfox/trajectory/sim/ThrowableFlight.java @@ -1,7 +1,6 @@ package ru.wilyfox.trajectory.sim; import java.util.ArrayList; -import java.util.Comparator; import java.util.List; /** @@ -62,20 +61,18 @@ public static Trajectory simulate( Vec3d origin = pos; Aabb searchArea = box(pos).expandTowards(vel).inflate(1.0); - List hits = - ArrowFlight.entityHits( + ArrowFlight.Hit hit = + ArrowFlight.nearestEntityHit( world, origin, blocks.location(), searchArea, ArrowFlight.margin(tickCount), leftOwner); - if (!hits.isEmpty()) { - hits.sort(Comparator.comparingDouble(h -> origin.distanceToSqr(h.location()))); - ArrowFlight.Hit first = hits.getFirst(); - points.add(first.location()); + if (hit != null) { + points.add(hit.location()); return new Trajectory( - points, Trajectory.End.ENTITY, tickCount, first.target(), null, first.location(), vel); + points, Trajectory.End.ENTITY, tickCount, hit.target(), null, hit.location(), vel); } if (blocks.type() == WorldView.BlockClip.Type.BLOCK) { diff --git a/src/main/java/ru/wilyfox/trajectory/sim/WindChargeFlight.java b/src/main/java/ru/wilyfox/trajectory/sim/WindChargeFlight.java index 6816685..9ebe024 100644 --- a/src/main/java/ru/wilyfox/trajectory/sim/WindChargeFlight.java +++ b/src/main/java/ru/wilyfox/trajectory/sim/WindChargeFlight.java @@ -1,7 +1,6 @@ package ru.wilyfox.trajectory.sim; import java.util.ArrayList; -import java.util.Comparator; import java.util.List; /** @@ -11,7 +10,8 @@ *
    *
  1. {@code applyInertia}: acceleration power 0 and inertia 1.0, in air and water alike, so the * velocity never changes; no gravity; - *
  2. find the hit along the velocity (collider shapes, entity margin as for other projectiles); + *
  3. find the hit along the velocity (collider shapes, then the nearest entity box inflated by + * {@code computeMargin}); *
  4. move to the hit or by the velocity; *
  5. {@code Projectile.tick}: check whether the charge has left its owner (from the next tick * on). @@ -55,26 +55,18 @@ public static Trajectory simulate(Vec3d spawn, Vec3d velocity, WorldView world, Vec3d origin = pos; Aabb searchArea = box(pos).expandTowards(velocity).inflate(1.0); - List hits = - ArrowFlight.entityHits( + ArrowFlight.Hit hit = + ArrowFlight.nearestEntityHit( world, origin, blocks.location(), searchArea, ArrowFlight.margin(tickCount), leftOwner); - if (!hits.isEmpty()) { - hits.sort(Comparator.comparingDouble(h -> origin.distanceToSqr(h.location()))); - ArrowFlight.Hit first = hits.getFirst(); - points.add(first.location()); + if (hit != null) { + points.add(hit.location()); return new Trajectory( - points, - Trajectory.End.ENTITY, - tickCount, - first.target(), - null, - first.location(), - velocity); + points, Trajectory.End.ENTITY, tickCount, hit.target(), null, hit.location(), velocity); } if (blocks.type() == WorldView.BlockClip.Type.BLOCK) { diff --git a/src/test/java/ru/wilyfox/trajectory/sim/NearestEntityHitTest.java b/src/test/java/ru/wilyfox/trajectory/sim/NearestEntityHitTest.java new file mode 100644 index 0000000..4fdd93c --- /dev/null +++ b/src/test/java/ru/wilyfox/trajectory/sim/NearestEntityHitTest.java @@ -0,0 +1,106 @@ +package ru.wilyfox.trajectory.sim; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +/** + * {@code ProjectileUtil.getEntityHitResult} with a margin, as throwables, fireworks, the fishing + * hook and the wind charge use it. + */ +class NearestEntityHitTest { + // Widths and the margin are floats, widened to double as in vanilla. + private static final double EPS = 1.0E-6; + private static final float MARGIN = 0.3F; + private static final Aabb AREA = new Aabb(-10, -10, -10, 10, 10, 10); + + @Test + void locationIsWhereTheSegmentEntersTheInflatedBox() { + TestWorld world = TestWorld.empty(); + world.entities.add(TestWorld.player(1, new Vec3d(0, 0, 0), false)); + Vec3d from = new Vec3d(0, 1, -5); + Vec3d to = new Vec3d(0, 1, 5); + + ArrowFlight.Hit hit = ArrowFlight.nearestEntityHit(world, from, to, AREA, MARGIN, false); + + assertNotNull(hit); + assertEquals(1, hit.target().id()); + assertEquals(-0.6, hit.location().z(), EPS); + assertEquals(0.0, hit.location().x(), EPS); + assertEquals(1.0, hit.location().y(), EPS); + // Arrows report the entry into the real box instead. + ArrowFlight.Hit arrow = ArrowFlight.entityHits(world, from, to, AREA, MARGIN, false).getFirst(); + assertEquals(-0.3, arrow.location().z(), EPS); + } + + @Test + void marginHitCountsWithoutTheRayToTheCenter() { + TestWorld world = TestWorld.empty(); + world.entities.add(TestWorld.player(1, new Vec3d(0, 0, 0), false)); + // A wall between the path and the box: the ray from the margin hit to the center meets it. + world.solids.add(new Aabb(0.35, 0, -1, 0.45, 2, 1)); + Vec3d from = new Vec3d(0.5, 1, -5); + Vec3d to = new Vec3d(0.5, 1, 5); + + ArrowFlight.Hit hit = ArrowFlight.nearestEntityHit(world, from, to, AREA, MARGIN, false); + + assertNotNull(hit); + assertEquals(0.5, hit.location().x(), EPS); + assertEquals(-0.6, hit.location().z(), EPS); + assertTrue(ArrowFlight.entityHits(world, from, to, AREA, MARGIN, false).isEmpty()); + } + + @Test + void noMarginMeansTheRealBox() { + TestWorld world = TestWorld.empty(); + world.entities.add(TestWorld.player(1, new Vec3d(0, 0, 0), false)); + + assertNull( + ArrowFlight.nearestEntityHit( + world, new Vec3d(0.5, 1, -5), new Vec3d(0.5, 1, 5), AREA, 0.0F, false)); + } + + @Test + void nearestOfTwoOverlappingTargetsWins() { + TestWorld world = TestWorld.empty(); + // Listed first but entered later: its inflated box starts at z = -0.2. + world.entities.add(TestWorld.player(1, new Vec3d(0, 0, 0.4), false)); + world.entities.add(TestWorld.player(2, new Vec3d(0, 0, 0), false)); + + ArrowFlight.Hit hit = + ArrowFlight.nearestEntityHit( + world, new Vec3d(0, 1, -5), new Vec3d(0, 1, 5), AREA, MARGIN, false); + + assertNotNull(hit); + assertEquals(2, hit.target().id()); + assertEquals(-0.6, hit.location().z(), EPS); + } + + @Test + void onATieTheEarlierTargetWins() { + TestWorld world = TestWorld.empty(); + world.entities.add(TestWorld.player(1, new Vec3d(-0.35, 0, 0), false)); + world.entities.add(TestWorld.player(2, new Vec3d(0.35, 0, 0), false)); + + ArrowFlight.Hit hit = + ArrowFlight.nearestEntityHit( + world, new Vec3d(0, 1, -5), new Vec3d(0, 1, 5), AREA, MARGIN, false); + + assertNotNull(hit); + assertEquals(1, hit.target().id()); + } + + @Test + void shooterGroupIsSkippedUntilTheProjectileLeavesIt() { + TestWorld world = TestWorld.empty(); + world.entities.add(TestWorld.player(1, new Vec3d(0, 0, 0), true)); + Vec3d from = new Vec3d(0, 1, -5); + Vec3d to = new Vec3d(0, 1, 5); + + assertNull(ArrowFlight.nearestEntityHit(world, from, to, AREA, MARGIN, false)); + assertNotNull(ArrowFlight.nearestEntityHit(world, from, to, AREA, MARGIN, true)); + } +}