Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/ru/wilyfox/trajectory/sim/ArrowFlight.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,38 @@ static List<Hit> 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) {}
}
19 changes: 8 additions & 11 deletions src/main/java/ru/wilyfox/trajectory/sim/FireworkFlight.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.wilyfox.trajectory.sim;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
Expand All @@ -10,8 +9,8 @@
*
* <ol>
* <li>no acceleration and no gravity: the velocity never changes;
* <li>find the hit along the velocity ({@code ProjectileUtil.getHitResultOnMoveVector}, collider
* shapes, entity margin as for arrows);
* <li>find the hit along the velocity ({@code ProjectileUtil.getHitResultOnMoveVector}: collider
* shapes, then the nearest entity box inflated by {@code computeMargin});
* <li>move; a hit entity or a block ends the flight;
* <li>{@code life} grows by one; the rocket explodes once {@code life > lifetime}.
* </ol>
Expand Down Expand Up @@ -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<ArrowFlight.Hit> 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()));
}
Expand Down
19 changes: 5 additions & 14 deletions src/main/java/ru/wilyfox/trajectory/sim/FishingHookFlight.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.wilyfox.trajectory.sim;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
Expand Down Expand Up @@ -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<ArrowFlight.Hit> 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
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/ru/wilyfox/trajectory/sim/ThrowableFlight.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.wilyfox.trajectory.sim;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
Expand Down Expand Up @@ -62,20 +61,18 @@ public static Trajectory simulate(

Vec3d origin = pos;
Aabb searchArea = box(pos).expandTowards(vel).inflate(1.0);
List<ArrowFlight.Hit> 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) {
Expand Down
22 changes: 7 additions & 15 deletions src/main/java/ru/wilyfox/trajectory/sim/WindChargeFlight.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.wilyfox.trajectory.sim;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
Expand All @@ -11,7 +10,8 @@
* <ol>
* <li>{@code applyInertia}: acceleration power 0 and inertia 1.0, in air and water alike, so the
* velocity never changes; no gravity;
* <li>find the hit along the velocity (collider shapes, entity margin as for other projectiles);
* <li>find the hit along the velocity (collider shapes, then the nearest entity box inflated by
* {@code computeMargin});
* <li>move to the hit or by the velocity;
* <li>{@code Projectile.tick}: check whether the charge has left its owner (from the next tick
* on).
Expand Down Expand Up @@ -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<ArrowFlight.Hit> 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) {
Expand Down
106 changes: 106 additions & 0 deletions src/test/java/ru/wilyfox/trajectory/sim/NearestEntityHitTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading