fix(viewer): draw crisp, aligned federation paths

This commit is contained in:
coyotte508 2026-10-02 10:34:51 +02:00
commit adb2413cd8
6 changed files with 108 additions and 126 deletions

View file

@ -1,6 +1,6 @@
{
"name": "@gaia-project/viewer",
"version": "5.20.44",
"version": "5.20.45",
"description": "Functions to visualize the map and boards for gaia project inside html canvas",
"contributors": [
"coyotte508",

View file

@ -30,11 +30,19 @@
:data-recent-opponent-move="recentOpponentMove.command"
pointer-events="none"
/>
<use
<path
v-for="(l, i) in federationLines"
:key="`fl-outline-${i}`"
:d="l.path"
class="federation-path federation-path--outline"
pointer-events="none"
/>
<path
v-for="(l, i) in federationLines"
:key="`fl-${i}`"
:xlink:href="l.id"
:transform="`rotate(${l.rotate})`"
:d="l.path"
:stroke="l.color"
class="federation-path federation-path--color"
pointer-events="none"
/>
<use v-if="powerHighlightClass" xlink:href="#space-hex" :class="['space-hex-federation', powerHighlightClass]" />
@ -683,6 +691,24 @@ svg {
pointer-events: none;
}
.federation-path {
fill: none;
stroke-linecap: round;
stroke-linejoin: round;
&--outline {
stroke: #080e20;
stroke-width: 0.18;
// The coloured caps overlap at shared midpoints; the outline must not cut into that join.
stroke-linecap: butt;
opacity: 0.8;
}
&--color {
stroke-width: 0.09;
}
}
.leech {
&.empty {
fill: white;

View file

@ -8,7 +8,7 @@ import Engine, {
PlayerEnum,
Spaceship,
} from "@gaia-project/engine";
import { render } from "@testing-library/vue";
import { cleanup, render } from "@testing-library/vue";
import { expect } from "chai";
import fs from "fs";
import { hexCenter } from "../graphics/hex";
@ -72,12 +72,58 @@ describe("SpaceMap", () => {
const hexes = container.querySelectorAll("g.sector > g");
expect(hexes.length).to.equal(engine.map.grid.size);
// Definitions.vue/Filters.vue/Buildings.vue each declare one static <defs> block,
// rendered once globally regardless of hex count. This guards against a regression of the
// pre-fix bug where federation gradients were duplicated into every SpaceHex instance
// (~4,500 nodes for ~90 hexes) instead of being hoisted into FederationGradients.vue.
// Definitions remain shared instead of being repeated for every hex.
expect(container.querySelectorAll("defs").length).to.equal(3);
expect(container.querySelector("#federation-gradient-line-r")).to.not.be.null;
expect(container.querySelector(".federation-path--color")).to.not.be.null;
});
it("joins federation paths at the same rendered midpoint across sectors and Lost Fleet tiles", () => {
for (const lostFleet of [false, true]) {
const engine = new Engine(["init 2 federation-seams", "p1 faction terrans", "p2 faction nevlas"], {
lostFleet,
});
for (const hex of engine.map.grid.values()) hex.data.federations = [PlayerEnum.Player1];
const store = makeStore();
store.commit("receiveData", engine);
const { container } = render(SpaceMap, { store });
const cells = new Map([...container.querySelectorAll(".space-hex-cell")].map((cell) => [cell.id, cell]));
const position = (cell: Element) => {
const sectorTransform = cell.parentElement.classList.contains("sector")
? matrixOf(cell.parentElement.getAttribute("style") ?? "")
: [1, 0, 0, 1, 0, 0];
const transform = multiply(sectorTransform, matrixOf(cell.getAttribute("transform") ?? ""));
return { x: transform[4], y: transform[5] };
};
let boundaryConnections = 0;
let curves = 0;
for (const hex of engine.map.grid.values()) {
const cell = cells.get(hex.toString());
const center = position(cell);
const midpoints = engine.map.grid.neighbours(hex).map((neighbour) => {
const other = position(cells.get(neighbour.toString()));
if (hex.data.sector !== neighbour.data.sector) boundaryConnections++;
return { x: (center.x + other.x) / 2, y: (center.y + other.y) / 2 };
});
for (const path of [...cell.querySelectorAll(".federation-path--color")]) {
const d = path.getAttribute("d");
const coordinates = d.match(/-?\d+(?:\.\d+)?(?:e[+-]?\d+)?/gi).map(Number);
const endpoints = [coordinates.slice(-2)];
if (d.includes("Q")) {
curves++;
endpoints.push(coordinates.slice(0, 2));
}
for (const [x, y] of endpoints) {
expect(
midpoints.some((point) => Math.hypot(point.x - center.x - x, point.y - center.y - y) < 1e-9),
`${hex}: ${d} must meet its neighbour exactly`
).to.equal(true);
}
}
}
expect(boundaryConnections).to.be.greaterThan(0);
expect(curves).to.be.greaterThan(0);
cleanup();
}
});
it("marks every hex an opponent's moves touched since the viewer's previous turn", () => {

View file

@ -3,7 +3,6 @@
<filters />
<space-hex />
<buildings />
<FederationGradients />
<path
id="info"
d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zm.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"
@ -19,7 +18,6 @@
</template>
<script lang="ts">
import Buildings from "./Buildings.vue";
import FederationGradients from "./FederationGradients.vue";
import Filters from "./Filters.vue";
import SpaceHex from "./SpaceHex.vue";
@ -28,7 +26,6 @@ export default {
Buildings,
Filters,
SpaceHex,
FederationGradients,
},
};
</script>

View file

@ -1,99 +0,0 @@
<template>
<g>
<linearGradient
v-for="p in planetColors"
:key="`lg-${p.planet}`"
:id="`federation-gradient-line-${p.planet}`"
gradientUnits="userSpaceOnUse"
x1="-1"
y1="-1"
x2="1"
y2="1"
gradientTransform="rotate(135)"
>
<stop offset="40%" stop-opacity="0" />
<stop offset="50%" stop-opacity="1" :stop-color="p.color" />
<stop offset="60%" stop-opacity="0" />
</linearGradient>
<line
v-for="p in planetColors"
:key="`l-${p.planet}`"
:id="`federation-line-${p.planet}`"
x1="0"
y1="0"
x2="0"
y2="0.9"
:stroke="`url(#federation-gradient-line-${p.planet})`"
stroke-width="1"
opacity=".5"
/>
<linearGradient
v-for="p in planetColors"
:key="`blg-${p.planet}`"
:id="`federation-gradient-big-line-${p.planet}`"
gradientUnits="userSpaceOnUse"
x1="-1"
y1="-1"
x2="1"
y2="1"
gradientTransform="rotate(135)"
>
<stop offset="10%" stop-opacity="0" />
<stop offset="50%" stop-opacity="1" :stop-color="p.color" />
<stop offset="90%" stop-opacity="0" />
</linearGradient>
<line
v-for="p in planetColors"
:key="`bl-${p.planet}`"
:id="`federation-big-line-${p.planet}`"
x1="0"
y1="0"
x2="0"
y2="0.9"
:stroke="`url(#federation-gradient-big-line-${p.planet})`"
stroke-width="1"
/>
<radialGradient
v-for="p in planetColors"
:key="`rg-${p.planet}`"
:id="`federation-gradient-arc-${p.planet}`"
gradientUnits="userSpaceOnUse"
cx="0"
cy="0"
r="3"
>
<stop offset="41%" stop-opacity="0" />
<stop offset="50%" stop-opacity=".5" :stop-color="p.color" />
<stop offset="59%" stop-opacity="0" />
</radialGradient>
<path
v-for="p in planetColors"
:key="`arc-${p.planet}`"
:id="`federation-arc-${p.planet}`"
d="M 1.5 0 A 1.5 1.5 0 0 0 0.7500000000000002 -1.299038105676658"
fill="none"
:stroke="`url(#federation-gradient-arc-${p.planet})`"
stroke-width=".7"
transform="translate(-1.52,.86)"
/>
</g>
</template>
<script lang="ts">
import Vue from "vue";
import { Component } from "vue-property-decorator";
import planets from "../../data/planets";
@Component
export default class FederationGradients extends Vue {
// Planet colours are static, so these federation gradient/line/arc defs are
// identical for every hex. They are defined once here (globally) instead of
// being re-rendered inside all ~90 SpaceHex components on every map re-render.
planetColors = Object.entries(planets).map((e) => ({ planet: e[0], color: e[1].color }));
}
</script>

View file

@ -1,9 +1,9 @@
import { GaiaHex, Player } from "@gaia-project/engine";
import { classifySectorId, GaiaHex, LostFleetSectorType, Player } from "@gaia-project/engine";
import { Direction, Grid } from "hexagrid";
import { CubeCoordinatesPartial } from "hexagrid/src/cubecoordinates";
import { factionPiecePlanet } from "./utils";
import { factionColor, lightenDarkenColor } from "./utils";
export type FederationLine = { rotate: number; id: string };
export type FederationLine = { path: string; color: string };
const vSpacing = Math.sqrt(3) / 2;
@ -21,6 +21,17 @@ export function hexCenter(hex: CubeCoordinatesPartial, radius = 1) {
};
}
/** Sectors are spread apart as whole tiles; their internal hex spacing stays unchanged. */
export function displayedHexCenter(hex: GaiaHex) {
const point = hexCenter(hex);
if (classifySectorId(hex.data.sector) !== LostFleetSectorType.Space) {
return { x: point.x * HEX_SPREAD, y: point.y * HEX_SPREAD };
}
const relative = hex.relativeCoordinates;
const center = hexCenter({ q: hex.q - relative.q, r: hex.r - relative.r });
return { x: point.x + center.x * (HEX_SPREAD - 1), y: point.y + center.y * (HEX_SPREAD - 1) };
}
export function corners(radius = 1) {
return [
{ x: -radius, y: 0 },
@ -39,12 +50,14 @@ function rotateRight(d: Direction, times: number): Direction {
return rotateRight(d == Direction.NorthWest ? Direction.North : 2 * d, times - 1);
}
function rotate(direction: Direction): number {
return Math.log2(direction) * 60 + 180;
}
export function playerFederationLines(grid: Grid<GaiaHex>, hex: GaiaHex, player: Player): FederationLine[] {
const directions = Direction.list().filter((d) => grid.neighbour(hex, d)?.federations?.includes(player.player));
const center = displayedHexCenter(hex);
const color = lightenDarkenColor(factionColor(player.faction), 30);
const midpoint = (direction: Direction) => {
const neighbour = displayedHexCenter(grid.neighbour(hex, direction));
return `${(neighbour.x - center.x) / 2} ${(neighbour.y - center.y) / 2}`;
};
const arcs: FederationLine[] = [];
const skipped: Direction[] = [];
@ -59,8 +72,8 @@ export function playerFederationLines(grid: Grid<GaiaHex>, hex: GaiaHex, player:
skipped.push(r);
}
arcs.push({
id: `#federation-arc-${factionPiecePlanet(player.faction)}`,
rotate: rotate(direction),
path: `M ${midpoint(direction)} Q 0 0 ${midpoint(r)}`,
color,
});
}
}
@ -70,15 +83,14 @@ export function playerFederationLines(grid: Grid<GaiaHex>, hex: GaiaHex, player:
return directions
.flatMap((direction) => {
const gaiaHex = grid.neighbour(hex, direction);
const big = gaiaHex.colonizedBy(player.player) && building;
if (!big && skipped.includes(direction)) {
const connectsBuildings = gaiaHex.colonizedBy(player.player) && building;
if (!connectsBuildings && skipped.includes(direction)) {
return [];
}
const line = big ? "big-line" : "line";
return [
{
rotate: rotate(direction),
id: `#federation-${line}-${factionPiecePlanet(player.faction)}`,
path: `M 0 0 L ${midpoint(direction)}`,
color,
},
];
})