Use the published board instead of drawing a worse one

Three attempts at drawing this map came to nothing worth looking at. Dividing
the plane by nearest province gives a tidy board that looks nothing like
Europe. Drawing the adjacency graph gives something unimpeachable about the
rules and not a map at all -- circles joined by lines. Fifty-six polygons
placed by eye gives fifty-six islands.

The standard map published with the diplomacy engine is AGPL-3.0, the same
licence as this project, so it can be carried across whole with attribution
rather than approximated badly. tools/map.py extracts the outlines, the unit
coordinates and the layer transform into board.json and can be re-run against
a newer copy. NOTICE.md says where it came from and what is still ours, which
is every colour and every mark drawn on top.

Drawing Europe accurately is real work, somebody did it properly, and they
published it under a licence that invites exactly this. Three days of my
approximating it would have produced something worse than the thing that
already existed.

Clicking still answers with the rules rather than the picture: a shape cannot
say that a fleet on Spain's north coast may not enter the Gulf of Lyon while
one on the south coast may, because both coasts are the same shape.
This commit is contained in:
2026-09-09 08:10:30 -07:00
parent fd05217f90
commit dbf4b9dea4
8 changed files with 358 additions and 449 deletions
+20 -20
View File
@@ -45,52 +45,52 @@ header p { margin: 2px 0 0; font-size: 0.9rem; }
.board {
width: 100%;
height: 100%;
border: 3px solid #241a10;
border-radius: 16px;
background: #6ea3c9;
border: 2px solid #2b2318;
border-radius: 8px;
background: #d3e6f2;
}
.ocean { fill: #4a7fa8; }
.ocean { fill: #d3e6f2; }
.region {
stroke: #241a10;
stroke-width: 2.5;
stroke: #2b2318;
stroke-width: 2;
stroke-linejoin: round;
cursor: pointer;
}
/* Open water gets a lighter line: a coastline is a border, the middle of the
Atlantic is a convention. */
.region.sea { stroke-opacity: 0.3; stroke-width: 2; }
/* A coastline is a real border; the middle of the Atlantic is a convention. */
.region.sea { stroke-opacity: 0.32; stroke-width: 1.6; }
.region.picked { stroke: #ffd479; stroke-width: 5; }
.region.picked { stroke: #b8341f; stroke-width: 7; }
/* Where the selected unit may actually go, taken from the rules rather than
from which shapes happen to touch. */
.region.open { stroke: #fff6e0; stroke-width: 4; stroke-dasharray: 7 5; }
.region.open { stroke: #b8341f; stroke-width: 5; stroke-dasharray: 12 9; }
.pip {
fill: #fff6e0;
stroke: #241a10;
stroke-width: 1.8;
fill: #fffaf0;
stroke: #2b2318;
stroke-width: 2.4;
pointer-events: none;
}
.label {
font-size: 11.5px;
font-size: 21px;
font-weight: 800;
letter-spacing: 0.04em;
letter-spacing: 0.03em;
text-anchor: middle;
pointer-events: none;
fill: #241a10;
fill: #2b2318;
}
.label.wet { fill: #dceaf5; }
.label.wet { fill: #63808f; font-weight: 700; }
.unit path {
stroke: #241a10;
stroke-width: 2.5;
stroke: #2b2318;
stroke-width: 3;
stroke-linejoin: round;
pointer-events: none;
}
/* --- the side ------------------------------------------------------- */
+53 -65
View File
@@ -1,39 +1,34 @@
import { PROVINCES, POWERS, base, type Power } from '../game/map'
import { CENTRES, bounds, cell, path, radius, reachableFrom } from '../game/layout'
import { CENTRES, SHAPES, SHIFT, VIEW_BOX, reachableFrom } from '../game/layout'
import type { Board as Units } from '../game/orders'
import type { Ownership } from '../game/turn'
/**
* The board.
*
* Territories, drawn from coordinates I placed by hand and divided by the
* halfway line between them. Nothing here is a picture of anybody's map.
* Flat fills, one heavy outline, no gradients: the same cel style the other
* three games use.
* The outlines are the standard map's; everything drawn on them is this
* game's. Flat fills and one dark line, a pale sea, and each power in a
* colour you can tell from the others at a glance while somebody is arguing
* with you about Galicia.
*
* The regions are what the map looks like; they are not what the rules are
* read from. A Voronoi cell cannot reproduce every adjacency in this game --
* provinces interleave, and the Adriatic borders Venice with Trieste sitting
* between their centres -- so clicking a province lights up exactly where its
* unit may legally go, taken from the adjacency graph itself. The picture is
* a picture; the rules answer for themselves when asked.
* Clicking a province lights up exactly where its unit may legally go, taken
* from the rules rather than from which shapes happen to share an edge.
*/
export const COLOURS: Record<Power, string> = {
austria: '#e4572e',
england: '#3b5bdb',
france: '#4dabf7',
germany: '#4b545e',
italy: '#37b24d',
russia: '#9775fa',
turkey: '#f2a03d',
austria: '#d4736c',
england: '#e7a8c8',
france: '#89b4dd',
germany: '#9d9d9d',
italy: '#93cc9e',
russia: '#b08fc4',
turkey: '#e8dc8c',
}
const SEA = '#5b93bd'
const SEA_DEEP = '#4a7fa8'
const LAND = '#ded0ab'
const LAND_SC = '#efe3c2'
const OUTLINE = '#241a10'
const SEA = '#d3e6f2'
const LAND = '#e6d8ba'
const LAND_SC = '#f3ead4'
const INK = '#2b2318'
export function Board({
units,
@@ -46,54 +41,45 @@ export function Board({
selected?: string | null
onPick?: (province: string) => void
}) {
const box = bounds(10)
const ids = Object.keys(PROVINCES)
const standing = selected ? units.get(selected) : undefined
const reachable = new Set(standing ? reachableFrom(standing) : [])
const shade = (id: string) => {
const p = PROVINCES[id]!
if (p.terrain === 'sea') return SEA
const owner = own.get(id)
return owner ? COLOURS[owner] : p.sc ? LAND_SC : LAND
}
return (
<svg
className="board"
viewBox={`${box.x} ${box.y} ${box.w} ${box.h}`}
role="img"
aria-label="The board: seventy-five provinces, coloured by who holds them."
>
<rect className="ocean" x={box.x} y={box.y} width={box.w} height={box.h} />
<svg className="board" viewBox={VIEW_BOX} role="img" aria-label="Europe in 1901.">
<rect className="ocean" x="0" y="0" width="100%" height="100%" />
{/* Territories. Sea first so the coastlines sit on top of the water. */}
{ids
.sort((a, b) => Number(PROVINCES[b]!.terrain === 'sea') - Number(PROVINCES[a]!.terrain === 'sea'))
.map((id) => {
const p = PROVINCES[id]!
const owner = own.get(id)
const fill =
p.terrain === 'sea' ? (p.sc ? SEA : SEA_DEEP) : owner ? COLOURS[owner] : p.sc ? LAND_SC : LAND
<g transform={SHIFT}>
{ids.map((id) => (
<path
key={id}
className={`region ${PROVINCES[id]!.terrain === 'sea' ? 'sea' : 'land'} ${
selected === id ? 'picked' : reachable.has(id) ? 'open' : ''
}`}
d={SHAPES[id] ?? ''}
fill={shade(id)}
onClick={() => onPick?.(id)}
/>
))}
</g>
return (
<path
key={id}
className={`region ${p.terrain} ${p.sc ? 'centre' : ''} ${
selected === id ? 'picked' : reachable.has(id) ? 'open' : ''
}`}
d={path(cell(id))}
fill={fill}
onClick={() => onPick?.(id)}
/>
)
})}
{/* Supply centres get a mark of their own: they are the only thing
anybody is actually counting. */}
{/* A supply centre is the only thing anybody is counting. */}
{ids
.filter((id) => PROVINCES[id]!.sc)
.map((id) => (
<circle
className="pip"
key={`pip-${id}`}
key={`p-${id}`}
cx={CENTRES[id]!.x}
cy={CENTRES[id]!.y - radius(id) * 0.55}
r={4.4}
cy={CENTRES[id]!.y - 26}
r={7}
/>
))}
@@ -102,21 +88,23 @@ export function Board({
className={`label ${PROVINCES[id]!.terrain === 'sea' ? 'wet' : ''}`}
key={`t-${id}`}
x={CENTRES[id]!.x}
y={CENTRES[id]!.y + 4}
y={CENTRES[id]!.y - 38}
>
{id.toUpperCase()}
</text>
))}
{/* Units last: they are what you are actually looking at. */}
{[...units.entries()].map(([at, unit]) => {
const p = CENTRES[base(unit.at)] ?? CENTRES[at]!
const p = CENTRES[unit.at] ?? CENTRES[base(unit.at)] ?? CENTRES[at]!
return (
<g className="unit" key={at} transform={`translate(${p.x} ${p.y + 17})`}>
<g className="unit" key={at} transform={`translate(${p.x} ${p.y})`}>
{unit.type === 'army' ? (
<path d="M-11 6 L-11 -3 L0 -9 L11 -3 L11 6 Z" fill={COLOURS[unit.power]} />
<path d="M-15 9 L-15 -3 L0 -13 L15 -3 L15 9 Z" fill={COLOURS[unit.power]} />
) : (
<path d="M-12 4 L12 4 L7 -2 L2 -2 L2 -9 L-3 -2 L-12 -2 Z" fill={COLOURS[unit.power]} />
<path
d="M-17 7 L17 7 L9 -1 L3 -1 L3 -13 L-4 -1 L-17 -1 Z"
fill={COLOURS[unit.power]}
/>
)}
</g>
)
@@ -135,4 +123,4 @@ export const POWER_NAMES: Record<Power, string> = {
turkey: 'Turkey',
}
export { POWERS, OUTLINE }
export { POWERS, INK }
File diff suppressed because one or more lines are too long
+44 -110
View File
@@ -1,142 +1,68 @@
import { describe, expect, it } from 'vitest'
import { ARMY, FLEET, PROVINCES, base } from './map'
import { CENTRES, HEIGHT, WIDTH, borders, cell, radius, reachableFrom } from './layout'
import { FLEET, PROVINCES, base } from './map'
import { CENTRES, SHAPES, SOURCE, VIEW_BOX, WIDTH, HEIGHT, reachableFrom } from './layout'
/**
* Coordinates typed by hand are wrong somewhere, and a map is the one thing
* where wrong looks like a style choice. These are the checks that turn
* "Bulgaria seems to be in the North Sea" into a failing test.
* The geometry is not ours -- it comes from the standard map published with
* the diplomacy engine, under the same licence as this project. So these do
* not check that Europe is the right shape. They check that what was carried
* across lines up with the rules this game plays by, which is the join where
* a mistake would actually hide.
*/
const ids = Object.keys(PROVINCES)
const dist = (a: string, b: string) =>
Math.hypot(CENTRES[a]!.x - CENTRES[b]!.x, CENTRES[a]!.y - CENTRES[b]!.y)
describe('the placings', () => {
it('has a spot for every province and nothing else', () => {
expect(Object.keys(CENTRES).sort()).toEqual(ids.sort())
describe('the board that was carried across', () => {
it('says where it came from', () => {
expect(SOURCE).toContain('github.com/diplomacy/diplomacy')
expect(SOURCE).toContain('AGPL-3.0')
})
it('keeps them all on the board', () => {
for (const [id, p] of Object.entries(CENTRES)) {
expect(p.x, id).toBeGreaterThan(0)
expect(p.x, id).toBeLessThan(WIDTH)
expect(p.y, id).toBeGreaterThan(0)
expect(p.y, id).toBeLessThan(HEIGHT)
}
it('has an outline for every province this game knows and no others', () => {
expect(Object.keys(SHAPES).sort()).toEqual(ids.sort())
})
it('keeps them far enough apart to be separate places', () => {
const close: string[] = []
for (const a of ids) {
for (const b of ids) {
if (a >= b) continue
if (dist(a, b) < 24) close.push(`${a}/${b} at ${dist(a, b).toFixed(0)}`)
}
}
expect(close).toEqual([])
})
})
describe('the geography agrees with the rules', () => {
const edges = borders()
it('draws a line for every border in the rules, and no others', () => {
const expected = new Set<string>()
it('has somewhere to stand a unit in every province', () => {
for (const id of ids) {
for (const to of ARMY[id] ?? []) if (base(to) !== id) expected.add([id, base(to)].sort().join(':'))
const keys = PROVINCES[id]!.coasts ? PROVINCES[id]!.coasts!.map((c) => `${id}/${c}`) : [id]
for (const key of keys) {
for (const to of FLEET[key] ?? []) if (base(to) !== id) expected.add([id, base(to)].sort().join(':'))
}
expect(CENTRES[id], id).toBeDefined()
expect(CENTRES[id]!.x, id).toBeGreaterThan(0)
expect(CENTRES[id]!.x, id).toBeLessThan(WIDTH)
expect(CENTRES[id]!.y, id).toBeGreaterThan(0)
expect(CENTRES[id]!.y, id).toBeLessThan(HEIGHT)
}
expect(new Set(edges.map((e) => e.join(':')))).toEqual(expected)
})
it('never draws the same border twice', () => {
expect(new Set(edges.map((e) => e.join(':'))).size).toBe(edges.length)
})
/**
* A smoke test for a province placed in the wrong country, and nothing
* more ambitious than that. The threshold is loose because provinces are
* not the same size: Moscow borders five things and is enormous, Norway
* reaches St Petersburg across the top of Finland, and North Africa runs
* the length of the Mediterranean. Tightening this until those failed
* would be measuring Europe rather than checking my typing.
*/
it('does not place a province in the wrong part of the continent', () => {
const far: string[] = []
for (const [a, b] of edges) {
if (PROVINCES[a]!.terrain === 'sea' || PROVINCES[b]!.terrain === 'sea') continue
if (dist(a, b) > 250) far.push(`${a}-${b} at ${dist(a, b).toFixed(0)}`)
it('knows where a fleet on each named coast belongs', () => {
// The three split coasts get their own spot, or a fleet in Spain has
// nowhere to be drawn that says which sea it is sitting in.
for (const id of ['spa/nc', 'spa/sc', 'bul/ec', 'bul/sc', 'stp/nc', 'stp/sc']) {
expect(CENTRES[id], id).toBeDefined()
}
expect(far).toEqual([])
})
it('puts the seas where the water is', () => {
// The Atlantic west of Portugal, the Black Sea east of Bulgaria, and
// Norway north of Spain. Three facts that would survive any redrawing.
expect(CENTRES.mao!.x).toBeLessThan(CENTRES.por!.x)
expect(CENTRES.bla!.x).toBeGreaterThan(CENTRES.bul!.x)
expect(CENTRES.nwy!.y).toBeLessThan(CENTRES.spa!.y)
})
})
describe('the drawing', () => {
it('never lets two provinces overlap on the board', () => {
const overlapping: string[] = []
for (const a of ids) {
for (const b of ids) {
if (a >= b) continue
if (dist(a, b) < radius(a) + radius(b) - 12) overlapping.push(`${a}/${b}`)
}
}
expect(overlapping).toEqual([])
})
})
describe('the regions', () => {
it('gives every province a shape with area in it', () => {
it('gives every outline something to draw', () => {
for (const id of ids) {
const poly = cell(id)
expect(poly.length, id).toBeGreaterThan(2)
// Shoelace: a region nobody can see is a region that is not there.
let area = 0
for (let i = 0; i < poly.length; i++) {
const a = poly[i]!
const b = poly[(i + 1) % poly.length]!
area += a.x * b.y - b.x * a.y
}
expect(Math.abs(area / 2), id).toBeGreaterThan(300)
expect(SHAPES[id]!.length, id).toBeGreaterThan(20)
expect(SHAPES[id]!.startsWith('M'), id).toBe(true)
}
})
it('contains its own centre', () => {
for (const id of ids) {
const poly = cell(id, 0)
const me = CENTRES[id]!
for (let i = 0; i < poly.length; i++) {
const a = poly[i]!
const b = poly[(i + 1) % poly.length]!
const cross = (b.x - a.x) * (me.y - a.y) - (b.y - a.y) * (me.x - a.x)
expect(cross, `${id} edge ${i}`).toBeGreaterThan(-0.01)
}
}
it('is one box the whole board fits in', () => {
expect(VIEW_BOX.split(' ')).toHaveLength(4)
})
})
describe('what a unit may reach', () => {
/**
* The point of this being separate from the drawing. The regions cannot
* express every adjacency in the rules, so the map never claims to: this
* is what lights up when a province is clicked, and it comes from the
* graph rather than from which shapes happen to share an edge.
* Kept apart from the drawing on purpose. Shapes cannot answer every
* question this game asks -- a fleet in Spain can reach the Gulf of Lyon
* from one coast and not the other, and both coasts are the same shape --
* so this comes from the rules and lights up when a province is clicked.
*/
it('is the rules, not the picture', () => {
expect(reachableFrom({ type: 'army', at: 'vie' }).sort()).toEqual(
['boh', 'bud', 'gal', 'tri', 'tyr'],
)
expect(reachableFrom({ type: 'army', at: 'vie' }).sort()).toEqual([
'boh', 'bud', 'gal', 'tri', 'tyr',
])
expect(reachableFrom({ type: 'fleet', at: 'stp/sc' }).sort()).toEqual(['bot', 'fin', 'lvn'])
})
@@ -144,4 +70,12 @@ describe('what a unit may reach', () => {
expect(reachableFrom({ type: 'fleet', at: 'spa/nc' })).not.toContain('lyo')
expect(reachableFrom({ type: 'fleet', at: 'spa/sc' })).toContain('lyo')
})
it('agrees with the fleet graph about every sea', () => {
for (const id of ids.filter((i) => PROVINCES[i]!.terrain === 'sea')) {
expect(reachableFrom({ type: 'fleet', at: id }).sort()).toEqual(
[...new Set((FLEET[id] ?? []).map(base))].sort(),
)
}
})
})
+40 -233
View File
@@ -1,253 +1,60 @@
import { ARMY, FLEET, PROVINCES, base } from './map'
import board from './board.json'
import { ARMY, FLEET, base } from './map'
/**
* Where everything sits, and how the regions are drawn.
* The board's geometry.
*
* The board this game is played on is somebody's artwork and is not here.
* What is here is a set of coordinates I placed by hand -- roughly where each
* province falls in Europe, in a thousand by eight hundred box -- and the map
* is drawn from those and from the adjacency rules. It is a drawing of the
* data rather than a tracing of a board, the same rule the cave game's
* dodecahedron is drawn under.
* Three attempts at drawing this by hand came to nothing worth looking at.
* Voronoi cells divide a plane tidily and look nothing like Europe; a graph
* of circles and lines is unimpeachable about adjacency and is not a map;
* and fifty-six polygons placed by eye were fifty-six separate islands.
*
* **It draws the graph, not regions, and that was not the first idea.**
* The geometry here comes from the standard map published with the diplomacy
* engine at https://github.com/diplomacy/diplomacy, which is AGPL-3.0 -- the
* same licence this project uses -- so it can be carried across whole, with
* attribution. `tools/map.py` pulls it out and NOTICE.md says where it came
* from. What comes across is outlines and coordinates; every colour, every
* mark and every decision about what to draw on top of it is ours.
*
* The first idea was Voronoi cells -- every point on the board belonging to
* the nearest province -- which gives a handsome cut-paper board and is
* wrong. Sixty-five pairs that border each other in the rules had regions
* that did not touch, and no amount of nudging coordinates would fix it,
* because the fault is structural: provinces here interleave. The Adriatic
* borders Venice with Trieste sitting between their centres; the Atlantic
* borders North Africa around the outside of Spain. Convex cells cannot say
* that.
*
* A map that shows two regions meeting when the rules say they do not is
* worse than an ugly map. It is a map that loses you the game. So the
* adjacency is drawn as lines, which can say exactly what the rules say, and
* the province is a shape sitting on top of it. Nobody can misread a line.
* The lesson is worth keeping. Drawing Europe accurately is a real piece of
* work by somebody who did it properly, and three days of my approximating
* it would have produced something worse than the thing that already exists
* under a licence that invites this exactly.
*/
export const WIDTH = 1000
export const HEIGHT = 800
export interface Point {
x: number
y: number
}
/**
* The centres, west to east and north to south. Placed by eye against an
* atlas: near enough that a player recognises Europe, and not a copy of
* anybody's board.
*/
export const CENTRES: Record<string, Point> = {
// --- the ocean and the northern seas ---
nao: { x: 60, y: 120 },
nwg: { x: 250, y: 80 },
bar: { x: 520, y: 40 },
iri: { x: 160, y: 300 },
nth: { x: 315, y: 235 },
ska: { x: 395, y: 195 },
hel: { x: 352, y: 265 },
bal: { x: 470, y: 235 },
bot: { x: 505, y: 155 },
eng: { x: 215, y: 375 },
mao: { x: 75, y: 400 },
// --- the British Isles ---
cly: { x: 215, y: 215 },
edi: { x: 250, y: 240 },
lvp: { x: 210, y: 275 },
yor: { x: 258, y: 288 },
wal: { x: 200, y: 318 },
lon: { x: 258, y: 332 },
// --- Scandinavia and the north ---
nwy: { x: 390, y: 130 },
swe: { x: 445, y: 140 },
fin: { x: 520, y: 100 },
stp: { x: 625, y: 90 },
den: { x: 398, y: 248 },
// --- Russia ---
lvn: { x: 560, y: 190 },
mos: { x: 690, y: 200 },
war: { x: 545, y: 278 },
ukr: { x: 620, y: 320 },
sev: { x: 700, y: 355 },
// --- Germany and the Low Countries ---
pru: { x: 483, y: 268 },
ber: { x: 432, y: 292 },
kie: { x: 390, y: 286 },
ruh: { x: 363, y: 332 },
mun: { x: 392, y: 368 },
sil: { x: 468, y: 322 },
hol: { x: 330, y: 300 },
bel: { x: 300, y: 345 },
// --- France and Iberia ---
pic: { x: 280, y: 376 },
par: { x: 275, y: 418 },
bre: { x: 208, y: 418 },
bur: { x: 328, y: 404 },
gas: { x: 248, y: 462 },
mar: { x: 312, y: 482 },
spa: { x: 182, y: 512 },
por: { x: 115, y: 522 },
// --- Italy ---
pie: { x: 372, y: 456 },
ven: { x: 408, y: 448 },
tus: { x: 396, y: 492 },
rom: { x: 420, y: 524 },
nap: { x: 458, y: 562 },
apu: { x: 468, y: 522 },
// --- Austria and the Balkans ---
tyr: { x: 412, y: 402 },
boh: { x: 442, y: 358 },
vie: { x: 472, y: 392 },
bud: { x: 518, y: 402 },
gal: { x: 542, y: 342 },
tri: { x: 452, y: 442 },
ser: { x: 508, y: 446 },
alb: { x: 500, y: 482 },
gre: { x: 518, y: 532 },
bul: { x: 566, y: 462 },
rum: { x: 598, y: 406 },
// --- Turkey and the Levant ---
con: { x: 628, y: 506 },
ank: { x: 702, y: 500 },
smy: { x: 668, y: 556 },
arm: { x: 765, y: 500 },
syr: { x: 742, y: 572 },
// --- the Mediterranean and Africa ---
adr: { x: 458, y: 482 },
ion: { x: 478, y: 610 },
aeg: { x: 572, y: 548 },
eas: { x: 655, y: 622 },
bla: { x: 665, y: 432 },
tys: { x: 412, y: 568 },
lyo: { x: 330, y: 532 },
wes: { x: 268, y: 572 },
naf: { x: 230, y: 640 },
tun: { x: 398, y: 640 },
const data = board as unknown as {
source: string
shift: number[]
viewBox: number[]
shapes: Record<string, string>
units: Record<string, [number, number]>
labels: Record<string, [number, number]>
}
export const SOURCE = data.source
export const VIEW_BOX = data.viewBox.join(' ')
export const WIDTH = data.viewBox[2]!
export const HEIGHT = data.viewBox[3]!
/** The outline of each province, as an SVG path. */
export const SHAPES: Record<string, string> = data.shapes
/**
* Every pair of provinces that border each other, once each.
*
* The union of both graphs at province level: an army's border and a fleet's
* are different questions, but a line on the map means "these two touch", and
* which unit can use it is what the panel beside the map is for.
* The outlines are drawn in a layer that is shifted; the unit coordinates
* are not. Applying it to the shapes rather than to the paths themselves is
* what keeps the extracted data a faithful copy of what was published.
*/
export function borders(): [string, string][] {
const seen = new Set<string>()
const out: [string, string][] = []
export const SHIFT = `translate(${data.shift[0]} ${data.shift[1]})`
const add = (a: string, b: string) => {
const key = a < b ? `${a}:${b}` : `${b}:${a}`
if (seen.has(key) || a === b) return
seen.add(key)
out.push(a < b ? [a, b] : [b, a])
}
for (const [id, tos] of Object.entries(ARMY)) for (const to of tos) add(id, base(to))
for (const [key, tos] of Object.entries(FLEET)) {
for (const to of tos) add(base(key), base(to))
}
return out
}
/** How big a province is drawn. Seas are wide and vague, land is compact. */
export const radius = (id: string): number =>
PROVINCES[id]!.terrain === 'sea' ? 26 : PROVINCES[id]!.sc ? 21 : 17
/**
* The box the board actually occupies, rather than the box the coordinates
* were typed into. Europe is not rectangular and the placings do not fill a
* thousand by eight hundred; drawing that box leaves a third of the frame as
* empty ocean, which wastes the one thing this game was given more of than
* the others -- room.
*/
export function bounds(pad = 34): { x: number; y: number; w: number; h: number } {
const ids = Object.keys(CENTRES)
const lo = (f: (id: string) => number) => Math.min(...ids.map(f))
const hi = (f: (id: string) => number) => Math.max(...ids.map(f))
const minX = lo((id) => CENTRES[id]!.x - radius(id))
const maxX = hi((id) => CENTRES[id]!.x + radius(id))
const minY = lo((id) => CENTRES[id]!.y - radius(id))
const maxY = hi((id) => CENTRES[id]!.y + radius(id))
return { x: minX - pad, y: minY - pad, w: maxX - minX + pad * 2, h: maxY - minY + pad * 2 }
}
/**
* The region belonging to one province: the whole board, cut back by the
* halfway line between it and every other centre.
*
* This is a Voronoi diagram, and it is worth being straight about what it
* can and cannot do. It divides the plane with no gaps and no overlaps, and
* it looks like a board. It cannot reproduce every adjacency in the rules,
* because provinces here interleave -- the Adriatic borders Venice with
* Trieste between their centres -- and convex cells cannot say that.
*
* So the regions are what the map *looks* like, and they are not what the
* rules are read from. Clicking a province lights up exactly where its unit
* may legally go, taken from the adjacency graph. That is the honest split:
* the picture is a picture, and the rules answer for themselves when asked.
*/
export function cell(id: string, margin = 5): Point[] {
const me = CENTRES[id]
if (!me) return []
let poly: Point[] = [
{ x: -60, y: -60 },
{ x: WIDTH + 60, y: -60 },
{ x: WIDTH + 60, y: HEIGHT + 60 },
{ x: -60, y: HEIGHT + 60 },
]
for (const [other, them] of Object.entries(CENTRES)) {
if (other === id) continue
const nx = them.x - me.x
const ny = them.y - me.y
const len = Math.hypot(nx, ny)
if (len === 0) continue
const on = {
x: (me.x + them.x) / 2 - (nx / len) * (margin / 2),
y: (me.y + them.y) / 2 - (ny / len) * (margin / 2),
}
poly = clip(poly, on, { x: nx / len, y: ny / len })
if (poly.length === 0) break
}
return poly
}
/** Everything on the near side of a line through `on` facing `normal`. */
function clip(poly: Point[], on: Point, normal: Point): Point[] {
const side = (p: Point) => (p.x - on.x) * normal.x + (p.y - on.y) * normal.y
const out: Point[] = []
for (let i = 0; i < poly.length; i++) {
const a = poly[i]!
const b = poly[(i + 1) % poly.length]!
const sa = side(a)
const sb = side(b)
if (sa <= 0) out.push(a)
if ((sa < 0 && sb > 0) || (sa > 0 && sb < 0)) {
const t = sa / (sa - sb)
out.push({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t })
}
}
return out
}
export const path = (poly: readonly Point[]): string =>
poly.length === 0 ? '' : `M${poly.map((p) => `${p.x.toFixed(1)} ${p.y.toFixed(1)}`).join('L')}Z`
/** Where a unit stands, and where the province's name goes above it. */
export const CENTRES: Record<string, Point> = Object.fromEntries(
Object.entries(data.units).map(([id, [x, y]]) => [id, { x, y }]),
)
/** Where a unit standing here may legally go. The rules, not the picture. */
export function reachableFrom(unit: { type: 'army' | 'fleet'; at: string }): string[] {