diff --git a/adapters/bukkit/plugin/src/main/java/art/arcane/iris/core/gui/BukkitVisionOverlay.java b/adapters/bukkit/plugin/src/main/java/art/arcane/iris/core/gui/BukkitVisionOverlay.java index 557db228b..6292f9cd8 100644 --- a/adapters/bukkit/plugin/src/main/java/art/arcane/iris/core/gui/BukkitVisionOverlay.java +++ b/adapters/bukkit/plugin/src/main/java/art/arcane/iris/core/gui/BukkitVisionOverlay.java @@ -81,7 +81,7 @@ public final class BukkitVisionOverlay implements GuiOverlay { List markers = new ArrayList<>(); for (Player player : BukkitWorldBinding.players(engine.getWorld())) { Location at = player.getLocation(); - markers.add(GuiMarker.player(player.getName(), at.getX(), at.getZ())); + markers.add(GuiMarker.player(player.getName(), at.getX(), at.getZ(), at.getYaw())); } playerMarkers = List.copyOf(markers); } finally { diff --git a/adapters/modded-common/src/main/java/art/arcane/iris/modded/command/ModdedVisionOverlay.java b/adapters/modded-common/src/main/java/art/arcane/iris/modded/command/ModdedVisionOverlay.java index e4f59f88c..e9f87a036 100644 --- a/adapters/modded-common/src/main/java/art/arcane/iris/modded/command/ModdedVisionOverlay.java +++ b/adapters/modded-common/src/main/java/art/arcane/iris/modded/command/ModdedVisionOverlay.java @@ -58,7 +58,7 @@ public final class ModdedVisionOverlay implements GuiOverlay { List markers = new ArrayList<>(); for (ServerPlayer player : level.players()) { Vec3 position = player.position(); - markers.add(GuiMarker.player(player.getScoreboardName(), position.x(), position.z())); + markers.add(GuiMarker.player(player.getScoreboardName(), position.x(), position.z(), player.getYRot())); } return markers; } diff --git a/core/src/main/java/art/arcane/iris/core/gui/GuiMarker.java b/core/src/main/java/art/arcane/iris/core/gui/GuiMarker.java index 58173f942..fb93a04ce 100644 --- a/core/src/main/java/art/arcane/iris/core/gui/GuiMarker.java +++ b/core/src/main/java/art/arcane/iris/core/gui/GuiMarker.java @@ -18,12 +18,18 @@ package art.arcane.iris.core.gui; -public record GuiMarker(String label, double worldX, double worldY, double worldZ, double health, double maxHealth) { - public static GuiMarker player(String name, double worldX, double worldZ) { - return new GuiMarker(name, worldX, 0, worldZ, 0, 0); +/** + * @param yaw the way it faces, in Minecraft's degrees: 0 is south, 90 is west + */ +public record GuiMarker(String label, double worldX, double worldY, double worldZ, float yaw, double health, double maxHealth) { + static final float YAW_STEP_DEGREES = 5F; + + /** The yaw is rounded to {@link #YAW_STEP_DEGREES}, so a turning head does not resend the player every poll. */ + public static GuiMarker player(String name, double worldX, double worldZ, float yaw) { + return new GuiMarker(name, worldX, 0, worldZ, Math.round(yaw / YAW_STEP_DEGREES) * YAW_STEP_DEGREES, 0, 0); } public static GuiMarker entity(String label, double worldX, double worldY, double worldZ, double health, double maxHealth) { - return new GuiMarker(label, worldX, worldY, worldZ, health, maxHealth); + return new GuiMarker(label, worldX, worldY, worldZ, 0F, health, maxHealth); } } diff --git a/core/src/test/java/art/arcane/iris/core/gui/GuiMarkerTest.java b/core/src/test/java/art/arcane/iris/core/gui/GuiMarkerTest.java new file mode 100644 index 000000000..ea45d36b2 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/gui/GuiMarkerTest.java @@ -0,0 +1,44 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.gui; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class GuiMarkerTest { + private static final float EXACT = 0F; + + @Test + public void playerYawIsRoundedToTheStep() { + assertEquals(90F, GuiMarker.player("a", 0, 0, 91.4F).yaw(), EXACT); + assertEquals(95F, GuiMarker.player("a", 0, 0, 92.6F).yaw(), EXACT); + assertEquals(-45F, GuiMarker.player("a", 0, 0, -44F).yaw(), EXACT); + } + + @Test + public void nearbyYawsCompareEqualSoTurningSlightlyResendsNothing() { + assertEquals(GuiMarker.player("a", 1, 2, 180.2F), GuiMarker.player("a", 1, 2, 181.9F)); + } + + @Test + public void entitiesHaveNoFacing() { + assertEquals(0F, GuiMarker.entity("zombie", 0, 64, 0, 20, 20).yaw(), EXACT); + } +} diff --git a/studio/src/renderer/src/bridge/types.ts b/studio/src/renderer/src/bridge/types.ts index c6ccfbf54..cbc50978b 100644 --- a/studio/src/renderer/src/bridge/types.ts +++ b/studio/src/renderer/src/bridge/types.ts @@ -298,6 +298,8 @@ export interface Marker { worldX: number worldY: number worldZ: number + /** The way it faces, in Minecraft's degrees: 0 is south, 90 is west; 0 for entities. */ + yaw: number health: number maxHealth: number } diff --git a/studio/src/renderer/src/map/MapController.ts b/studio/src/renderer/src/map/MapController.ts index 0cdb5b9c4..5c5179742 100644 --- a/studio/src/renderer/src/map/MapController.ts +++ b/studio/src/renderer/src/map/MapController.ts @@ -4,6 +4,7 @@ import { NO_LABEL, type MapLabel, type MapTile, type Marker, type PickDisplay, t import { countShares, type PlacedTile, type Share } from './census' import { bandIndexOf, HEIGHT_BANDS } from './heightColors' import { mapTileSource, type TileLayer } from './mapTileSource' +import { drawPlayerArrow } from './playerArrow' import { drawVisibleTiles } from './tileDrawing' import type { Highlight, TileStyle } from './tileStyle' import { TileStore } from './TileStore' @@ -58,10 +59,8 @@ const REGION_BLOCKS = 512 const SMALLEST_GRID_GAP_PIXELS = 8 const CHUNK_LINE = 'rgba(255, 255, 255, 0.14)' const REGION_LINE = 'rgba(255, 255, 255, 0.38)' -const PLAYER_FILL = '#3574f0' const ENTITY_FILL = '#f75464' const MARKER_OUTLINE = '#ffffff' -const PLAYER_RADIUS_PIXELS = 6 const ENTITY_RADIUS_PIXELS = 3 /** @@ -303,34 +302,22 @@ export class MapController { private drawMarkers(): void { for (const entity of this.entities) { - this.drawMarker(entity, ENTITY_FILL, ENTITY_RADIUS_PIXELS, false) + this.drawEntity(entity) } for (const player of this.players) { - this.drawMarker(player, PLAYER_FILL, PLAYER_RADIUS_PIXELS, true) + drawPlayerArrow(this.context, worldToScreenX(this.view, player.worldX), worldToScreenZ(this.view, player.worldZ), player.yaw, player.label) } } - private drawMarker(marker: Marker, fill: string, radius: number, named: boolean): void { + private drawEntity(entity: Marker): void { const context = this.context - const x = worldToScreenX(this.view, marker.worldX) - const z = worldToScreenZ(this.view, marker.worldZ) context.beginPath() - context.arc(x, z, radius, 0, Math.PI * 2) - context.fillStyle = fill + context.arc(worldToScreenX(this.view, entity.worldX), worldToScreenZ(this.view, entity.worldZ), ENTITY_RADIUS_PIXELS, 0, Math.PI * 2) + context.fillStyle = ENTITY_FILL context.fill() context.lineWidth = 2 context.strokeStyle = MARKER_OUTLINE context.stroke() - if (!named) { - return - } - context.font = '600 12px Inter, "Segoe UI", system-ui, sans-serif' - context.textAlign = 'center' - context.lineWidth = 3 - context.strokeStyle = 'rgba(0, 0, 0, 0.7)' - context.strokeText(marker.label, x, z - radius - 6) - context.fillStyle = MARKER_OUTLINE - context.fillText(marker.label, x, z - radius - 6) } private lastReady = 0 diff --git a/studio/src/renderer/src/map/map.test.ts b/studio/src/renderer/src/map/map.test.ts index 79f00f37f..1bbcca662 100644 --- a/studio/src/renderer/src/map/map.test.ts +++ b/studio/src/renderer/src/map/map.test.ts @@ -3,6 +3,7 @@ import { decodeTile, gridIndex } from '../bridge/tileDecoding' import type { MapLabel, MapTile } from '../bridge/types' import { countShares } from './census' import { bandIndexOf, HEIGHT_BANDS, heightColor } from './heightColors' +import { arrowCorners } from './playerArrow' import { stylePixels, type TileStyle } from './tileStyle' import { coarserTile, previewsFor, renderBlocksPerPixel, screenToWorldX, visibleTiles, worldToScreenX, type Viewport } from './viewport' @@ -214,3 +215,27 @@ describe('census', () => { expect(shares[0].subject).toMatchObject({ kind: 'HEIGHT', name: '0–8 above sea' }) }) }) + +describe('player arrow', () => { + const tipAt = (yaw: number) => arrowCorners(yaw, 10, 7, 3)[0] + + it('points the tip south, down the map, at yaw 0', () => { + expect(tipAt(0).x).toBeCloseTo(0) + expect(tipAt(0).z).toBeCloseTo(10) + }) + + it('points west at 90, north at 180 and east at 270', () => { + expect(tipAt(90).x).toBeCloseTo(-10) + expect(tipAt(90).z).toBeCloseTo(0) + expect(tipAt(180).z).toBeCloseTo(-10) + expect(tipAt(270).x).toBeCloseTo(10) + }) + + it('keeps its shape while turning', () => { + const [tip, right, notch, left] = arrowCorners(37, 10, 7, 3) + + expect(Math.hypot(tip.x, tip.z)).toBeCloseTo(10) + expect(Math.hypot(right.x - left.x, right.z - left.z)).toBeCloseTo(14) + expect(Math.hypot(notch.x, notch.z)).toBeCloseTo(7) + }) +}) diff --git a/studio/src/renderer/src/map/playerArrow.ts b/studio/src/renderer/src/map/playerArrow.ts new file mode 100644 index 000000000..cb9f4bb99 --- /dev/null +++ b/studio/src/renderer/src/map/playerArrow.ts @@ -0,0 +1,85 @@ +export interface ScreenPoint { + x: number + z: number +} + +const MINECRAFT_YAW_TO_SCREEN_DEGREES = 180 +const ARROW_HALF_LENGTH_PIXELS = 9.5 +const ARROW_HALF_WIDTH_PIXELS = 7.5 +const ARROW_NOTCH_PIXELS = 3.5 +const ARROW_EDGE_WIDTH_PIXELS = 1.75 +const ARROW_OUTLINE_WIDTH_PIXELS = 1 +const ARROW_SHADOW_OFFSET_PIXELS = 1.5 +const ARROW_REACH_PIXELS = ARROW_HALF_LENGTH_PIXELS + ARROW_EDGE_WIDTH_PIXELS + ARROW_OUTLINE_WIDTH_PIXELS +const ARROW_FILL = '#3574f0' +const ARROW_EDGE = '#ffffff' +const ARROW_OUTLINE = 'rgba(0, 0, 0, 0.59)' +const ARROW_SHADOW = 'rgba(0, 0, 0, 0.35)' +const LABEL_GAP_PIXELS = 5 +const LABEL_HEIGHT_PIXELS = 18 +const LABEL_PADDING_X_PIXELS = 7 +const LABEL_FONT = '600 12px Inter, "Segoe UI", system-ui, sans-serif' +const LABEL_BACKGROUND = 'rgba(30, 31, 34, 0.86)' +const LABEL_BORDER = 'rgba(255, 255, 255, 0.22)' +const LABEL_TEXT = '#ffffff' + +/** + * The corners of a player's arrow around the origin: tip, right wing, notch, left wing. Minecraft's yaw 0 faces + * south and 90 west, which on the map is down and left, so the heading is (-sin, cos) in (x, z). + */ +export function arrowCorners(yawDegrees: number, halfLength: number, halfWidth: number, notch: number): ScreenPoint[] { + const turn = ((yawDegrees + MINECRAFT_YAW_TO_SCREEN_DEGREES) * Math.PI) / 180 + const sine = Math.sin(turn) + const cosine = Math.cos(turn) + const upright: ScreenPoint[] = [ + { x: 0, z: -halfLength }, + { x: halfWidth, z: halfLength }, + { x: 0, z: halfLength - notch }, + { x: -halfWidth, z: halfLength } + ] + return upright.map((corner) => ({ x: corner.x * cosine - corner.z * sine, z: corner.x * sine + corner.z * cosine })) +} + +/** A blue arrow with a white edge, dark outline and shadow pointing where the player looks, and their name in a pill above. */ +export function drawPlayerArrow(context: CanvasRenderingContext2D, x: number, z: number, yawDegrees: number, name: string): void { + const corners = arrowCorners(yawDegrees, ARROW_HALF_LENGTH_PIXELS, ARROW_HALF_WIDTH_PIXELS, ARROW_NOTCH_PIXELS) + context.save() + context.lineJoin = 'round' + context.fillStyle = ARROW_SHADOW + context.fill(arrowPath(corners, x, z + ARROW_SHADOW_OFFSET_PIXELS)) + const arrow = arrowPath(corners, x, z) + // Strokes are centred on the outline, so each ring is drawn twice its width and the next one covers its inner half. + context.lineWidth = (ARROW_EDGE_WIDTH_PIXELS + ARROW_OUTLINE_WIDTH_PIXELS) * 2 + context.strokeStyle = ARROW_OUTLINE + context.stroke(arrow) + context.lineWidth = ARROW_EDGE_WIDTH_PIXELS * 2 + context.strokeStyle = ARROW_EDGE + context.stroke(arrow) + context.fillStyle = ARROW_FILL + context.fill(arrow) + drawNamePill(context, name, x, z - ARROW_REACH_PIXELS - LABEL_GAP_PIXELS) + context.restore() +} + +function arrowPath(corners: ScreenPoint[], x: number, z: number): Path2D { + const path = new Path2D() + corners.forEach((corner, index) => (index === 0 ? path.moveTo(x + corner.x, z + corner.z) : path.lineTo(x + corner.x, z + corner.z))) + path.closePath() + return path +} + +function drawNamePill(context: CanvasRenderingContext2D, name: string, centerX: number, bottom: number): void { + context.font = LABEL_FONT + context.textAlign = 'center' + context.textBaseline = 'middle' + const width = context.measureText(name).width + LABEL_PADDING_X_PIXELS * 2 + const pill = new Path2D() + pill.roundRect(centerX - width / 2, bottom - LABEL_HEIGHT_PIXELS, width, LABEL_HEIGHT_PIXELS, LABEL_HEIGHT_PIXELS / 2) + context.fillStyle = LABEL_BACKGROUND + context.fill(pill) + context.lineWidth = 1 + context.strokeStyle = LABEL_BORDER + context.stroke(pill) + context.fillStyle = LABEL_TEXT + context.fillText(name, centerX, bottom - LABEL_HEIGHT_PIXELS / 2) +}