Archive incari-client (client, 26.3, clean)
This commit is contained in:
+339
@@ -0,0 +1,339 @@
|
||||
package incari.features.waypoints;
|
||||
|
||||
import incari.features.waypoints.WaypointsModule.Labels;
|
||||
import incari.framework.event.OverlayListener;
|
||||
import incari.framework.game.PlayerView;
|
||||
import incari.framework.hud.Shape;
|
||||
import incari.framework.hud.Tone;
|
||||
import incari.framework.hud.WorldOverlay;
|
||||
import incari.framework.waypoint.Waypoint;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The waypoints over the world, drawn every frame. On screen: the icon in place, with a glass tag
|
||||
* of name and distance above. Off screen: an arrow at the screen's edge pointing the way to turn,
|
||||
* beside the icon and distance. Farther ones are drawn first, so nearer tags are on top; one the
|
||||
* player stands at isn't drawn.
|
||||
*
|
||||
* <p>A hot path: the arrays grow only when there are more waypoints than before, and a distance's
|
||||
* text is made only when the number it says changes.
|
||||
*/
|
||||
final class WaypointOverlay implements OverlayListener {
|
||||
|
||||
/** An icon's size over the world at Size 1, in GUI units. */
|
||||
static final float ICON = 9;
|
||||
|
||||
/** How far in from the screen's edge the arrows sit. */
|
||||
static final float EDGE_MARGIN = 22;
|
||||
|
||||
/** How far up from the screen's bottom: above the hotbar, hearts and hunger. */
|
||||
static final float BOTTOM_MARGIN = 62;
|
||||
|
||||
/** The least room between two edge arrows on the left or right side, at Size 1. */
|
||||
static final float EDGE_GAP_SIDE = 16;
|
||||
|
||||
/** And along the top or bottom, where each one's distance is written beside the next. */
|
||||
static final float EDGE_GAP_ACROSS = 36;
|
||||
|
||||
/** Within this many blocks across (and a few up or down), the player is at the waypoint. */
|
||||
static final double HERE = 1.5;
|
||||
|
||||
/** With "When looked at", a waypoint this near the crosshair (GUI units, at Size 1) shows its name. */
|
||||
static final float LOOK_RADIUS = 36;
|
||||
|
||||
private final WaypointsModule module;
|
||||
private final float[] point = new float[3];
|
||||
/** By waypoint (each its own object: a waypoint is equal only to itself). */
|
||||
private final Map<Waypoint, DistanceText> texts = new HashMap<>();
|
||||
|
||||
private long textsRevision = -1;
|
||||
|
||||
/** This frame's waypoints to draw, with where each shows and its distance, farthest first. */
|
||||
private Waypoint[] order = new Waypoint[0];
|
||||
|
||||
private float[] screenX = new float[0];
|
||||
private float[] screenY = new float[0];
|
||||
private float[] distances = new float[0];
|
||||
private boolean[] onScreen = new boolean[0];
|
||||
|
||||
/** This frame's waypoints off screen: how far round the edges each sits, the way it lies, and its index above. */
|
||||
private float[] edgeAlong = new float[0];
|
||||
|
||||
private float[] edgeDx = new float[0];
|
||||
private float[] edgeDy = new float[0];
|
||||
private int[] edgeIndex = new int[0];
|
||||
|
||||
/** This frame's rectangle the edge arrows sit on. */
|
||||
private float left;
|
||||
|
||||
private float right;
|
||||
private float top;
|
||||
private float bottom;
|
||||
|
||||
WaypointOverlay(WaypointsModule module) {
|
||||
this.module = module;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOverlay(WorldOverlay overlay) {
|
||||
PlayerView player = module.player();
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
List<Waypoint> all = module.store().all();
|
||||
long revision = module.store().revision();
|
||||
if (revision != textsRevision) {
|
||||
// Forget the texts of waypoints that are gone.
|
||||
textsRevision = revision;
|
||||
texts.keySet().retainAll(all);
|
||||
}
|
||||
if (order.length < all.size()) {
|
||||
int capacity = Math.max(all.size(), order.length * 2);
|
||||
order = new Waypoint[capacity];
|
||||
screenX = new float[capacity];
|
||||
screenY = new float[capacity];
|
||||
distances = new float[capacity];
|
||||
onScreen = new boolean[capacity];
|
||||
}
|
||||
int width = overlay.screenWidth();
|
||||
int height = overlay.screenHeight();
|
||||
int count = 0;
|
||||
for (int i = 0; i < all.size(); i++) {
|
||||
Waypoint waypoint = all.get(i);
|
||||
if (!WaypointsModule.shown(waypoint, player) || isHere(waypoint, player)) {
|
||||
continue;
|
||||
}
|
||||
boolean front = overlay.project(waypoint.x() + 0.5, waypoint.y() + 0.5, waypoint.z() + 0.5, point);
|
||||
float distance = (float) Math.sqrt(waypoint.distanceSquared(player.x(), player.y(), player.z()));
|
||||
// Insertion by distance, farthest first: a handful of waypoints, no sort's allocation.
|
||||
int at = count;
|
||||
while (at > 0 && distances[at - 1] < distance) {
|
||||
order[at] = order[at - 1];
|
||||
screenX[at] = screenX[at - 1];
|
||||
screenY[at] = screenY[at - 1];
|
||||
distances[at] = distances[at - 1];
|
||||
onScreen[at] = onScreen[at - 1];
|
||||
at--;
|
||||
}
|
||||
order[at] = waypoint;
|
||||
screenX[at] = point[0];
|
||||
screenY[at] = point[1];
|
||||
distances[at] = distance;
|
||||
onScreen[at] = front && point[0] >= 0 && point[0] <= width && point[1] >= 0 && point[1] <= height;
|
||||
count++;
|
||||
}
|
||||
float scale = module.size.get();
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (onScreen[i]) {
|
||||
drawOnScreen(overlay, order[i], screenX[i], screenY[i], distances[i], scale);
|
||||
}
|
||||
}
|
||||
if (module.arrows.get()) {
|
||||
drawAtEdges(overlay, count, scale);
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether the player stands at {@code waypoint}: drawn over their view, it would only be in the way. */
|
||||
private static boolean isHere(Waypoint waypoint, PlayerView player) {
|
||||
double dx = waypoint.x() + 0.5 - player.x();
|
||||
double dz = waypoint.z() + 0.5 - player.z();
|
||||
double dy = waypoint.y() - player.y();
|
||||
return dx * dx + dz * dz < HERE * HERE && Math.abs(dy) < 3;
|
||||
}
|
||||
|
||||
private void drawOnScreen(WorldOverlay overlay, Waypoint waypoint, float x, float y, float distance, float scale) {
|
||||
overlay.push(x, y, scale);
|
||||
try {
|
||||
overlay.shape(waypoint.icon(), 0, 0, ICON, 0, waypoint.argb());
|
||||
if (!named(overlay, x, y, scale)) {
|
||||
return;
|
||||
}
|
||||
String name = waypoint.name();
|
||||
String far = module.distance.get() ? text(waypoint).of(distance) : "";
|
||||
int nameWidth = overlay.textWidth(name);
|
||||
int farWidth = far.isEmpty() ? 0 : overlay.textWidth(far);
|
||||
int width = 8 + nameWidth + (farWidth == 0 ? 0 : 5 + farWidth);
|
||||
int height = overlay.lineHeight() + 4;
|
||||
int left = -width / 2;
|
||||
int top = -Math.round(ICON / 2) - 3 - height;
|
||||
overlay.fill(left, top, width, height, Tone.BACKGROUND);
|
||||
overlay.text(name, left + 4, top + 3, Tone.TEXT);
|
||||
if (farWidth > 0) {
|
||||
overlay.text(far, left + 4 + nameWidth + 5, top + 3, Tone.MUTED);
|
||||
}
|
||||
} finally {
|
||||
overlay.pop();
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether a waypoint shown at {@code x, y} has its name written, by the Names setting. */
|
||||
private boolean named(WorldOverlay overlay, float x, float y, float scale) {
|
||||
Labels labels = module.labels.get();
|
||||
if (labels == Labels.LOOKED_AT) {
|
||||
float dx = x - overlay.screenWidth() / 2.0F;
|
||||
float dy = y - overlay.screenHeight() / 2.0F;
|
||||
float radius = LOOK_RADIUS * scale;
|
||||
return dx * dx + dy * dy < radius * radius;
|
||||
}
|
||||
return labels == Labels.ALWAYS;
|
||||
}
|
||||
|
||||
/**
|
||||
* For each waypoint off screen, an arrow at the screen's edge pointing the way to it, with its
|
||||
* icon and distance just inside. Arrows that would overlap are spread along the edge, in order.
|
||||
*/
|
||||
private void drawAtEdges(WorldOverlay overlay, int count, float scale) {
|
||||
if (edgeAlong.length < count) {
|
||||
edgeAlong = new float[count];
|
||||
edgeDx = new float[count];
|
||||
edgeDy = new float[count];
|
||||
edgeIndex = new int[count];
|
||||
}
|
||||
float centerX = overlay.screenWidth() / 2.0F;
|
||||
float centerY = overlay.screenHeight() / 2.0F;
|
||||
// The edges' inset rectangle, kept above the hotbar and hearts at the bottom.
|
||||
float margin = EDGE_MARGIN * scale;
|
||||
left = margin;
|
||||
right = Math.max(left + 1, overlay.screenWidth() - margin);
|
||||
top = margin;
|
||||
bottom = Math.max(top + 1, overlay.screenHeight() - Math.max(margin, BOTTOM_MARGIN));
|
||||
int edges = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (onScreen[i]) {
|
||||
continue;
|
||||
}
|
||||
float dx = screenX[i] - centerX;
|
||||
float dy = screenY[i] - centerY;
|
||||
float length = (float) Math.sqrt(dx * dx + dy * dy);
|
||||
if (length < 1.0E-3F) {
|
||||
// Straight behind: point down, the way to turn round.
|
||||
dx = 0;
|
||||
dy = 1;
|
||||
length = 1;
|
||||
}
|
||||
dx /= length;
|
||||
dy /= length;
|
||||
float reach = Math.max(
|
||||
0,
|
||||
Math.min(
|
||||
Math.abs(dx) < 1.0E-4F ? Float.MAX_VALUE : ((dx > 0 ? right : left) - centerX) / dx,
|
||||
Math.abs(dy) < 1.0E-4F ? Float.MAX_VALUE : ((dy > 0 ? bottom : top) - centerY) / dy));
|
||||
float along = along(centerX + dx * reach, centerY + dy * reach);
|
||||
// Insertion by the place along the edge.
|
||||
int at = edges;
|
||||
while (at > 0 && edgeAlong[at - 1] > along) {
|
||||
edgeAlong[at] = edgeAlong[at - 1];
|
||||
edgeDx[at] = edgeDx[at - 1];
|
||||
edgeDy[at] = edgeDy[at - 1];
|
||||
edgeIndex[at] = edgeIndex[at - 1];
|
||||
at--;
|
||||
}
|
||||
edgeAlong[at] = along;
|
||||
edgeDx[at] = dx;
|
||||
edgeDy[at] = dy;
|
||||
edgeIndex[at] = i;
|
||||
edges++;
|
||||
}
|
||||
for (int k = 1; k < edges; k++) {
|
||||
// Room for the icon beside the last; along the top and bottom, for its distance too.
|
||||
float gap = (onSide(edgeAlong[k - 1]) ? EDGE_GAP_SIDE : EDGE_GAP_ACROSS) * scale;
|
||||
edgeAlong[k] = Math.max(edgeAlong[k], edgeAlong[k - 1] + gap);
|
||||
}
|
||||
for (int k = 0; k < edges; k++) {
|
||||
int i = edgeIndex[k];
|
||||
drawAtEdge(overlay, order[i], edgeAlong[k], edgeDx[k], edgeDy[k], distances[i], scale);
|
||||
}
|
||||
}
|
||||
|
||||
/** How far round the edges' rectangle {@code x, y} on it is, clockwise from its top left. */
|
||||
private float along(float x, float y) {
|
||||
float width = right - left;
|
||||
float height = bottom - top;
|
||||
if (y <= top + 0.5F) {
|
||||
return x - left;
|
||||
}
|
||||
if (x >= right - 0.5F) {
|
||||
return width + (y - top);
|
||||
}
|
||||
if (y >= bottom - 0.5F) {
|
||||
return width + height + (right - x);
|
||||
}
|
||||
return 2 * width + height + (bottom - y);
|
||||
}
|
||||
|
||||
/** Whether the place {@code along} the edges is on the left or right side. */
|
||||
private boolean onSide(float along) {
|
||||
float width = right - left;
|
||||
float height = bottom - top;
|
||||
float at = along % (2 * (width + height));
|
||||
return (at > width && at < width + height) || at > 2 * width + height;
|
||||
}
|
||||
|
||||
private void drawAtEdge(
|
||||
WorldOverlay overlay, Waypoint waypoint, float along, float dx, float dy, float distance, float scale) {
|
||||
float width = right - left;
|
||||
float height = bottom - top;
|
||||
float at = along % (2 * (width + height));
|
||||
float x;
|
||||
float y;
|
||||
if (at < width) {
|
||||
x = left + at;
|
||||
y = top;
|
||||
} else if (at < width + height) {
|
||||
x = right;
|
||||
y = top + (at - width);
|
||||
} else if (at < 2 * width + height) {
|
||||
x = right - (at - width - height);
|
||||
y = bottom;
|
||||
} else {
|
||||
x = left;
|
||||
y = bottom - (at - 2 * width - height);
|
||||
}
|
||||
float turn = (float) Math.toDegrees(Math.atan2(dx, -dy));
|
||||
overlay.push(x, y, scale);
|
||||
try {
|
||||
overlay.shape(Shape.ARROW, dx * 7, dy * 7, 8, turn, waypoint.argb());
|
||||
overlay.shape(waypoint.icon(), -dx * 4, -dy * 4, ICON - 2, 0, waypoint.argb());
|
||||
if (module.distance.get() && module.labels.get() != Labels.NEVER) {
|
||||
String far = text(waypoint).of(distance);
|
||||
int farWidth = overlay.textWidth(far);
|
||||
// Inside the icon, on the side toward the middle, centred on the line to it.
|
||||
int textX = Math.round(-dx * 14 - farWidth / 2.0F - dx * farWidth / 2.0F);
|
||||
int textY = Math.round(-dy * 14 - overlay.lineHeight() / 2.0F - dy * overlay.lineHeight() / 2.0F);
|
||||
overlay.text(far, textX, textY, Tone.TEXT);
|
||||
}
|
||||
} finally {
|
||||
overlay.pop();
|
||||
}
|
||||
}
|
||||
|
||||
private DistanceText text(Waypoint waypoint) {
|
||||
DistanceText text = texts.get(waypoint);
|
||||
if (text == null) {
|
||||
text = new DistanceText();
|
||||
texts.put(waypoint, text);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/** A waypoint's distance as text, made again only when what it says changes. */
|
||||
static final class DistanceText {
|
||||
|
||||
/** Whole meters below a kilometer; tenths of a kilometer from there, as negative numbers. */
|
||||
private int said = Integer.MIN_VALUE;
|
||||
|
||||
private String text = "";
|
||||
|
||||
/** "12 m", "999 m", "1.2 km", "34.5 km". */
|
||||
String of(float blocks) {
|
||||
int key = blocks < 1000 ? (int) blocks : -(int) (blocks / 100);
|
||||
if (key != said) {
|
||||
said = key;
|
||||
text = key >= 0 ? key + " m" : (-key / 10) + "." + (-key % 10) + " km";
|
||||
}
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
+268
@@ -0,0 +1,268 @@
|
||||
package incari.features.waypoints;
|
||||
|
||||
import incari.framework.Category;
|
||||
import incari.framework.Module;
|
||||
import incari.framework.event.Events;
|
||||
import incari.framework.game.PlayerView;
|
||||
import incari.framework.hud.Shape;
|
||||
import incari.framework.hud.WorldCanvas;
|
||||
import incari.framework.setting.ActionSetting;
|
||||
import incari.framework.setting.BooleanSetting;
|
||||
import incari.framework.setting.ChoiceSetting;
|
||||
import incari.framework.setting.FloatSetting;
|
||||
import incari.framework.setting.Key;
|
||||
import incari.framework.setting.KeySetting;
|
||||
import incari.framework.waypoint.Waypoint;
|
||||
import incari.framework.waypoint.Waypoints;
|
||||
import java.util.List;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Waypoints: places the player marks in a world, each with a name, a color and an icon. Over the
|
||||
* world each shows its icon, its name and how far it is, and a beacon's beam rises from it; one off
|
||||
* screen is pointed to from the screen's edge. A key marks where the player stands, another lists
|
||||
* the world's waypoints, and where the player dies is marked for them.
|
||||
*
|
||||
* <p>The waypoints themselves are the framework's ({@code ModuleSystem.waypoints()}), kept for each
|
||||
* world; the minimap shows them too. This module draws them over the world and makes new ones.
|
||||
*/
|
||||
public final class WaypointsModule extends Module {
|
||||
|
||||
/** When a waypoint's name and distance are written beside its icon. */
|
||||
public enum Labels {
|
||||
/** Every waypoint's. */
|
||||
ALWAYS("Always"),
|
||||
/** Only the one near the crosshair: the others show their icons. */
|
||||
LOOKED_AT("When looked at"),
|
||||
/** Never: icons only. */
|
||||
NEVER("Never");
|
||||
|
||||
private final String label;
|
||||
|
||||
Labels(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
/** What the settings screen calls it. */
|
||||
public String label() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
|
||||
/** How {@code Game.world()} names a server's world. */
|
||||
static final String SERVER_PREFIX = "server/";
|
||||
|
||||
/** The name a death's waypoint gets. */
|
||||
static final String DEATH_NAME = "Death";
|
||||
|
||||
/** A death's waypoint's color: the palette's red. */
|
||||
static final int DEATH_COLOR = Waypoint.COLORS.get(0);
|
||||
|
||||
/** N: a New waypoint (B is voice chat's push to talk). */
|
||||
private static final int KEY_N = 17;
|
||||
|
||||
/** U, as other clients' waypoint lists. */
|
||||
private static final int KEY_U = 24;
|
||||
|
||||
final ActionSetting manage = settings()
|
||||
.action("manage", "This world's waypoints", "Manage...", this::openList)
|
||||
.describedAs("Lists this world's waypoints to add, change, hide or remove them.");
|
||||
final KeySetting newKey = settings()
|
||||
.key("new", "New waypoint key", Key.keyboard(KEY_N))
|
||||
.describedAs("Marks where you stand: opens a new waypoint to name.")
|
||||
.onPress(this::newWaypoint);
|
||||
final KeySetting listKey = settings()
|
||||
.key("list", "Waypoint list key", Key.keyboard(KEY_U))
|
||||
.describedAs("Opens this world's waypoints.")
|
||||
.onPress(this::openList);
|
||||
final ChoiceSetting<Labels> labels = settings()
|
||||
.choice("labels", "Names", Labels.values(), Labels.ALWAYS, Labels::label)
|
||||
.describedAs("When a waypoint's name shows above its icon.");
|
||||
final BooleanSetting distance =
|
||||
settings().bool("distance", "Show distance", true).describedAs("Shows the distance beside each name.");
|
||||
final BooleanSetting beams = settings()
|
||||
.bool("beams", "Beams", true)
|
||||
.describedAs("Shows a beacon beam on waypoints that have one turned on.");
|
||||
final BooleanSetting arrows = settings()
|
||||
.bool("arrows", "Arrows at the edge", true)
|
||||
.describedAs("Points to waypoints off the screen from its edge.");
|
||||
final FloatSetting size = settings()
|
||||
.decimal("size", "Size", 0.5F, 2.0F, 0.25F, 1.0F)
|
||||
.describedAs("How big icons and names are drawn over the world.");
|
||||
final BooleanSetting deaths = settings()
|
||||
.bool("deaths", "Mark deaths", true)
|
||||
.describedAs("Adds a waypoint where you die, replacing the last one; on a server, until you leave.");
|
||||
final BooleanSetting clearDeaths = settings()
|
||||
.bool("clear-deaths", "Clear deaths on arrival", true)
|
||||
.describedAs("Removes a death's waypoint once you get back to it.")
|
||||
.visibleWhen(deaths::get);
|
||||
|
||||
/** Within this many blocks of a death's waypoint, the player has got back to it. */
|
||||
static final double ARRIVED = 4;
|
||||
|
||||
/**
|
||||
* The player must first get this far away, so a respawn beside the place of death isn't an
|
||||
* arrival, but walking away and back is.
|
||||
*/
|
||||
static final double AWAY = 8;
|
||||
|
||||
private final WaypointOverlay overlay = new WaypointOverlay(this);
|
||||
private boolean wasDead;
|
||||
/** Whether the player has been {@link #AWAY} from the death's waypoint since it was made. */
|
||||
private boolean leftDeath;
|
||||
|
||||
/** Waypoints, off until the player turns it on. */
|
||||
public WaypointsModule() {
|
||||
super(
|
||||
"waypoints",
|
||||
"Waypoints",
|
||||
"Marks places in the world, shown with their distance and a beacon beam.",
|
||||
Category.UTILITY);
|
||||
on(Events.OVERLAY, overlay);
|
||||
on(Events.WORLD, this::drawBeams);
|
||||
on(Events.TICK, this::watchForDeath);
|
||||
}
|
||||
|
||||
/** The world's waypoints. */
|
||||
Waypoints store() {
|
||||
return system().waypoints();
|
||||
}
|
||||
|
||||
/** The player, or null outside a world. */
|
||||
@Nullable
|
||||
PlayerView player() {
|
||||
return system().game().player();
|
||||
}
|
||||
|
||||
/** Opens the waypoint list. */
|
||||
void openList() {
|
||||
if (store().world() != null) {
|
||||
system().game().openWaypoints();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the editor on a new waypoint where the player stands: the next free "Waypoint N", the
|
||||
* palette's next color. It's added only if the player saves it.
|
||||
*/
|
||||
void newWaypoint() {
|
||||
PlayerView player = player();
|
||||
Waypoints store = store();
|
||||
if (player == null || store.world() == null) {
|
||||
return;
|
||||
}
|
||||
Waypoint draft = new Waypoint(
|
||||
nextName(store.all()),
|
||||
floor(player.x()),
|
||||
floor(player.y()),
|
||||
floor(player.z()),
|
||||
player.dimension(),
|
||||
Waypoint.COLORS.get(store.all().size() % Waypoint.COLORS.size()),
|
||||
Shape.CIRCLE);
|
||||
system().game().editWaypoint(draft, true);
|
||||
}
|
||||
|
||||
/** "Waypoint 1", or the first number after it no waypoint is called. */
|
||||
static String nextName(List<Waypoint> waypoints) {
|
||||
for (int n = waypoints.size() + 1; ; n++) {
|
||||
String name = "Waypoint " + n;
|
||||
boolean taken = false;
|
||||
for (Waypoint waypoint : waypoints) {
|
||||
if (waypoint.name().equals(name)) {
|
||||
taken = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!taken) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether {@code waypoint} is shown to the player now: visible and in their dimension. */
|
||||
static boolean shown(Waypoint waypoint, PlayerView player) {
|
||||
return waypoint.isVisible() && waypoint.dimension().equals(player.dimension());
|
||||
}
|
||||
|
||||
private void drawBeams(WorldCanvas canvas) {
|
||||
PlayerView player = player();
|
||||
if (!beams.get() || player == null) {
|
||||
return;
|
||||
}
|
||||
List<Waypoint> all = store().all();
|
||||
for (int i = 0; i < all.size(); i++) {
|
||||
Waypoint waypoint = all.get(i);
|
||||
if (waypoint.hasBeam() && shown(waypoint, player)) {
|
||||
canvas.beam(waypoint.x(), waypoint.y(), waypoint.z(), waypoint.rgb());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Once a tick: a death just now leaves a waypoint where it happened. */
|
||||
private void watchForDeath() {
|
||||
PlayerView player = player();
|
||||
boolean dead = player != null && player.isDead();
|
||||
if (player != null && dead && !wasDead && deaths.get()) {
|
||||
markDeath(player);
|
||||
leftDeath = false;
|
||||
}
|
||||
wasDead = dead;
|
||||
if (player != null && !dead && clearDeaths.get()) {
|
||||
clearOnArrival(player);
|
||||
}
|
||||
}
|
||||
|
||||
/** Removes the death's waypoint once the player, having been away from it, gets back to it. */
|
||||
private void clearOnArrival(PlayerView player) {
|
||||
Waypoints store = store();
|
||||
Waypoint death = store.world() == null ? null : store.death();
|
||||
if (death == null || !death.dimension().equals(player.dimension())) {
|
||||
return;
|
||||
}
|
||||
double dx = player.x() - (death.x() + 0.5);
|
||||
double dy = player.y() - death.y();
|
||||
double dz = player.z() - (death.z() + 0.5);
|
||||
double distanceSquared = dx * dx + dy * dy + dz * dz;
|
||||
if (distanceSquared > AWAY * AWAY) {
|
||||
leftDeath = true;
|
||||
} else if (leftDeath && distanceSquared <= ARRIVED * ARRIVED) {
|
||||
store.remove(death);
|
||||
leftDeath = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void markDeath(PlayerView player) {
|
||||
Waypoints store = store();
|
||||
String world = store.world();
|
||||
if (world == null) {
|
||||
return;
|
||||
}
|
||||
Waypoint last = store.death();
|
||||
if (last != null) {
|
||||
store.remove(last);
|
||||
}
|
||||
Waypoint death = new Waypoint(
|
||||
DEATH_NAME,
|
||||
floor(player.x()),
|
||||
floor(player.y()),
|
||||
floor(player.z()),
|
||||
player.dimension(),
|
||||
DEATH_COLOR,
|
||||
Shape.CROSS);
|
||||
death.setDeath(true);
|
||||
// A server's address can be many maps (a network's lobby and its games, each with its own
|
||||
// places at the same coordinates): there a death is marked for this visit only, never kept
|
||||
// in the server's file to show up on the next map or the next day.
|
||||
death.setTemporary(onServer(world));
|
||||
store.add(death);
|
||||
}
|
||||
|
||||
/** Whether {@code world} ({@code Game.world()}) is a server's, rather than singleplayer's. */
|
||||
static boolean onServer(String world) {
|
||||
return world.startsWith(SERVER_PREFIX);
|
||||
}
|
||||
|
||||
private static int floor(double value) {
|
||||
return (int) Math.floor(value);
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
/** Waypoints (ADR 0051): named marks in the world, shown over it with their distance and beams. */
|
||||
@NullMarked
|
||||
package incari.features.waypoints;
|
||||
|
||||
import org.jspecify.annotations.NullMarked;
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package incari.features.waypoints;
|
||||
|
||||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
|
||||
|
||||
import com.tngtech.archunit.core.importer.ImportOption;
|
||||
import com.tngtech.archunit.junit.AnalyzeClasses;
|
||||
import com.tngtech.archunit.junit.ArchTest;
|
||||
import com.tngtech.archunit.lang.ArchRule;
|
||||
|
||||
/** A feature sees the module framework and the JDK's basics: no game, platform or other feature. */
|
||||
@AnalyzeClasses(packages = "incari.features.waypoints", importOptions = ImportOption.DoNotIncludeTests.class)
|
||||
class BoundaryTest {
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule onlyTheFramework = classes()
|
||||
.should()
|
||||
.onlyDependOnClassesThat()
|
||||
.resideInAnyPackage(
|
||||
"java.lang..",
|
||||
"java.util..",
|
||||
"org.jspecify..",
|
||||
"incari.framework..",
|
||||
"incari.features.waypoints..");
|
||||
}
|
||||
+254
@@ -0,0 +1,254 @@
|
||||
package incari.features.waypoints;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import incari.features.waypoints.WaypointsModule.Labels;
|
||||
import incari.framework.game.PlayerView;
|
||||
import incari.framework.hud.Shape;
|
||||
import incari.framework.testing.FakePlayer;
|
||||
import incari.framework.testing.TestGame;
|
||||
import incari.framework.waypoint.Waypoint;
|
||||
import incari.framework.waypoint.Waypoints;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class WaypointsModuleTest {
|
||||
|
||||
private final WaypointsModule module = new WaypointsModule();
|
||||
private final TestGame game = new TestGame(module);
|
||||
private final Waypoints store = game.system().waypoints();
|
||||
private FakePlayer player = new FakePlayer();
|
||||
|
||||
@BeforeEach
|
||||
void inAWorld() {
|
||||
module.setEnabled(true);
|
||||
player = game.join().at(10.5, 64, 20.5);
|
||||
}
|
||||
|
||||
private Waypoint add(String name, int x, int y, int z) {
|
||||
Waypoint waypoint = new Waypoint(name, x, y, z, PlayerView.OVERWORLD, 0x5B8CFF, Shape.DIAMOND);
|
||||
store.add(waypoint);
|
||||
return waypoint;
|
||||
}
|
||||
|
||||
@Test
|
||||
void theNewKeyOpensANewWaypointWhereThePlayerStands() {
|
||||
game.press(module.newKey.get());
|
||||
game.tick();
|
||||
assertThat(game.screens).containsExactly("new Waypoint 1");
|
||||
// Not added until the player saves it.
|
||||
assertThat(store.all()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void newNamesSkipThoseTaken() {
|
||||
add("Waypoint 2", 0, 64, 0);
|
||||
assertThat(WaypointsModule.nextName(store.all())).isEqualTo("Waypoint 3");
|
||||
add("Waypoint 3", 0, 64, 0);
|
||||
assertThat(WaypointsModule.nextName(store.all())).isEqualTo("Waypoint 4");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theListKeyAndTheSettingsButtonOpenTheList() {
|
||||
game.press(module.listKey.get());
|
||||
game.tick();
|
||||
module.manage.run();
|
||||
assertThat(game.screens).containsExactly("waypoints", "waypoints");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeathIsMarkedAndTheNextReplacesIt() {
|
||||
player.at(-3.2, 70, 8.9).dead(true);
|
||||
game.tick();
|
||||
game.tick();
|
||||
player.dead(false);
|
||||
game.tick();
|
||||
assertThat(store.all()).singleElement().satisfies(death -> {
|
||||
assertThat(death.isDeath()).isTrue();
|
||||
assertThat(death.name()).isEqualTo("Death");
|
||||
assertThat(death.icon()).isEqualTo(Shape.CROSS);
|
||||
assertThat(death.x()).isEqualTo(-4);
|
||||
assertThat(death.y()).isEqualTo(70);
|
||||
assertThat(death.z()).isEqualTo(8);
|
||||
});
|
||||
player.at(100, 12, 100).dead(true);
|
||||
game.tick();
|
||||
assertThat(store.all())
|
||||
.singleElement()
|
||||
.satisfies(death -> assertThat(death.x()).isEqualTo(100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void onAServerADeathIsMarkedForTheVisitOnly() {
|
||||
// One address, many maps: the death mustn't be kept to show on the next one.
|
||||
store.close();
|
||||
store.open("server/play.example.net");
|
||||
add("Base", 0, 64, 0);
|
||||
player.at(-3.2, 70, 8.9).dead(true);
|
||||
game.tick();
|
||||
assertThat(store.death())
|
||||
.isNotNull()
|
||||
.satisfies(death -> assertThat(death.isTemporary()).isTrue());
|
||||
store.flush();
|
||||
String saved = String.join("\n", game.files.values());
|
||||
assertThat(saved).contains("Base").doesNotContain("Death");
|
||||
|
||||
store.close();
|
||||
store.open("server/play.example.net");
|
||||
assertThat(store.all())
|
||||
.singleElement()
|
||||
.satisfies(kept -> assertThat(kept.name()).isEqualTo("Base"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void inSingleplayerADeathIsKept() {
|
||||
player.at(-3.2, 70, 8.9).dead(true);
|
||||
game.tick();
|
||||
assertThat(store.death())
|
||||
.isNotNull()
|
||||
.satisfies(death -> assertThat(death.isTemporary()).isFalse());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeathIsClearedOnceThePlayerGetsBackToIt() {
|
||||
player.at(0, 70, 0).dead(true);
|
||||
game.tick();
|
||||
// Respawned beside it: not an arrival.
|
||||
player.dead(false);
|
||||
game.tick();
|
||||
assertThat(store.all()).hasSize(1);
|
||||
player.at(50, 70, 50);
|
||||
game.tick();
|
||||
assertThat(store.all()).hasSize(1);
|
||||
player.at(2, 71, -1);
|
||||
game.tick();
|
||||
assertThat(store.all()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeathStaysWithClearingOff() {
|
||||
module.clearDeaths.set(false);
|
||||
player.at(0, 70, 0).dead(true);
|
||||
game.tick();
|
||||
player.dead(false);
|
||||
player.at(50, 70, 50);
|
||||
game.tick();
|
||||
player.at(0, 70, 0);
|
||||
game.tick();
|
||||
assertThat(store.all()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void noDeathMarkedWithMarkDeathsOff() {
|
||||
module.deaths.set(false);
|
||||
player.dead(true);
|
||||
game.tick();
|
||||
assertThat(store.all()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWaypointAheadShowsItsIconNameAndDistance() {
|
||||
// 30 blocks north, at eye height: straight ahead.
|
||||
add("Base", 10, 65, -10);
|
||||
List<String> drawn = game.drawLive().drawn;
|
||||
assertThat(drawn).hasSize(4);
|
||||
assertThat(drawn.get(0)).startsWith("shape DIAMOND 9 at 160,").endsWith("#ff5b8cff");
|
||||
assertThat(drawn.get(1)).startsWith("fill").endsWith("BACKGROUND");
|
||||
assertThat(drawn.get(2)).startsWith("text \"Base\"").endsWith("TEXT");
|
||||
assertThat(drawn.get(3)).startsWith("text \"30 m\"").endsWith("MUTED");
|
||||
}
|
||||
|
||||
@Test
|
||||
void namesOnlyNearTheCrosshairWhenSoSet() {
|
||||
module.labels.set(Labels.LOOKED_AT);
|
||||
add("Ahead", 10, 65, -10);
|
||||
// 4.5 blocks to the side: on screen, 45 units from the crosshair.
|
||||
add("Aside", 15, 65, -10);
|
||||
List<String> drawn = game.drawLive().drawn;
|
||||
assertThat(drawn)
|
||||
.filteredOn(line -> line.startsWith("text"))
|
||||
.containsExactly(
|
||||
drawn.stream()
|
||||
.filter(line -> line.startsWith("text \"Ahead\""))
|
||||
.findFirst()
|
||||
.orElseThrow(),
|
||||
drawn.stream()
|
||||
.filter(line -> line.startsWith("text \"30 m\""))
|
||||
.findFirst()
|
||||
.orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fartherWaypointsAreDrawnFirst() {
|
||||
module.labels.set(Labels.NEVER);
|
||||
add("Near", 12, 65, 10);
|
||||
add("Far", 8, 65, -50);
|
||||
// The far one, 70 blocks off and left of the middle, before the near one, 10 off and right.
|
||||
assertThat(game.drawLive().drawn)
|
||||
.extracting(line -> line.substring(0, line.indexOf(",")))
|
||||
.containsExactly("shape DIAMOND 9 at 140", "shape DIAMOND 9 at 180");
|
||||
}
|
||||
|
||||
@Test
|
||||
void oneBehindIsPointedToFromTheEdge() {
|
||||
add("Behind", 10, 65, 60);
|
||||
List<String> drawn = game.drawLive().drawn;
|
||||
assertThat(drawn.get(0)).startsWith("shape ARROW");
|
||||
assertThat(drawn.get(1)).startsWith("shape DIAMOND");
|
||||
assertThat(drawn.get(2)).startsWith("text \"40 m\"");
|
||||
module.arrows.set(false);
|
||||
assertThat(game.drawLive().drawn).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void arrowsThatWouldSitTogetherAreSpreadAlongTheEdge() {
|
||||
// Three behind, nearly in a line: their arrows would all sit at the bottom's middle.
|
||||
add("One", 10, 65, 60);
|
||||
add("Two", 11, 65, 80);
|
||||
add("Three", 10, 65, 100);
|
||||
assertThat(game.drawLive().drawn)
|
||||
.filteredOn(line -> line.startsWith("shape ARROW"))
|
||||
.extracting(line -> line.substring(line.indexOf(" at ") + 4, line.indexOf(" turned")))
|
||||
.hasSize(3)
|
||||
.doesNotHaveDuplicates();
|
||||
}
|
||||
|
||||
@Test
|
||||
void hiddenOthersDimensionsAndTheOneUnderfootArentDrawn() {
|
||||
add("Hidden", 10, 65, -10).setVisible(false);
|
||||
store.add(new Waypoint("Nether", 10, 65, -10, PlayerView.NETHER, 0, Shape.CIRCLE));
|
||||
add("Here", 10, 64, 20);
|
||||
assertThat(game.drawLive().drawn).isEmpty();
|
||||
// The beam of the one underfoot still rises round the player.
|
||||
assertThat(game.drawWorld()).containsExactly("beam at 10,64,20 #5b8cff");
|
||||
}
|
||||
|
||||
@Test
|
||||
void beamsRiseFromWaypointsWithTheirBeamOn() {
|
||||
add("Beam", 10, 65, -10);
|
||||
add("No beam", 40, 70, -10).setBeam(false);
|
||||
assertThat(game.drawWorld()).containsExactly("beam at 10,65,-10 #5b8cff");
|
||||
module.beams.set(false);
|
||||
assertThat(game.drawWorld()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void distancesReadInMetersThenKilometers() {
|
||||
WaypointOverlay.DistanceText text = new WaypointOverlay.DistanceText();
|
||||
assertThat(text.of(12.7F)).isEqualTo("12 m");
|
||||
assertThat(text.of(999.9F)).isEqualTo("999 m");
|
||||
assertThat(text.of(1234)).isEqualTo("1.2 km");
|
||||
String same = text.of(1250);
|
||||
assertThat(text.of(1299)).isSameAs(same);
|
||||
assertThat(text.of(34_560)).isEqualTo("34.5 km");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nothingDrawnWhileOff() {
|
||||
add("Base", 10, 65, -10);
|
||||
module.setEnabled(false);
|
||||
assertThat(game.drawLive().drawn).isEmpty();
|
||||
assertThat(game.drawWorld()).isEmpty();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user