@heroiclands/package-build 22.4.3 → 22.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -18
- package/CONTENT.md +77 -1
- package/README.md +9 -0
- package/bin/content-build.mjs +143 -0
- package/bin/package-build.mjs +18 -4
- package/content-config.mjs +7 -0
- package/docs/api.md +84 -0
- package/docs/commands.md +182 -2
- package/docs/configuration.md +19 -1
- package/docs/content-format.md +105 -6
- package/docs/diagnostics.md +1 -0
- package/docs/project-setup.md +7 -6
- package/engine/changelog-group.mjs +27 -6
- package/engine/changelog-lint.mjs +27 -7
- package/engine/frontmatter-lint.mjs +11 -1
- package/engine/homepage.mjs +9 -1
- package/engine/index.mjs +18 -0
- package/engine/map-build.mjs +240 -0
- package/engine/map-dot.mjs +685 -0
- package/engine/map-graphviz.mjs +138 -0
- package/engine/map-layout.mjs +712 -0
- package/engine/map-places.mjs +561 -0
- package/engine/metadata-index.mjs +7 -0
- package/engine/note-vocabulary.mjs +28 -0
- package/engine/place-relations.mjs +637 -0
- package/engine/related-pages.mjs +161 -0
- package/engine/site-build.mjs +107 -9
- package/engine/site-config.mjs +1 -1
- package/engine/site-index.mjs +5 -1
- package/engine/site-root.mjs +147 -6
- package/engine/web-wikilinks.mjs +13 -2
- package/package.json +3 -2
- package/types/engine/changelog-lint.d.mts +9 -7
- package/types/engine/homepage.d.mts +8 -1
- package/types/engine/index.d.mts +6 -0
- package/types/engine/map-build.d.mts +78 -0
- package/types/engine/map-dot.d.mts +130 -0
- package/types/engine/map-graphviz.d.mts +61 -0
- package/types/engine/map-layout.d.mts +359 -0
- package/types/engine/map-places.d.mts +259 -0
- package/types/engine/note-vocabulary.d.mts +12 -0
- package/types/engine/place-relations.d.mts +95 -0
- package/types/engine/related-pages.d.mts +70 -0
- package/types/engine/site-build.d.mts +36 -3
- package/types/engine/site-config.d.mts +1 -1
- package/types/engine/site-index.d.mts +5 -1
- package/types/engine/site-root.d.mts +59 -7
- package/types/engine/web-wikilinks.d.mts +11 -2
|
@@ -0,0 +1,712 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of the Song of Heroic Lands (SoHL) system for Foundry VTT.
|
|
3
|
+
* Copyright (c) 2024-2026 Tom Rodriguez ("Toasty") — <toasty@heroiclands.org>
|
|
4
|
+
*
|
|
5
|
+
* This work is licensed under the GNU General Public License v3.0 (GPLv3).
|
|
6
|
+
* You may copy, modify, and distribute it under the terms of that license.
|
|
7
|
+
*
|
|
8
|
+
* For full terms, see the LICENSE.md file in the project root or visit:
|
|
9
|
+
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
10
|
+
*
|
|
11
|
+
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The geometry of the map from a place.
|
|
16
|
+
*
|
|
17
|
+
* A pre-modern map was an itinerary: centred on its maker, exact about the
|
|
18
|
+
* next stage and vague about the tenth. A place note's `borders` and `routes`
|
|
19
|
+
* are exactly that, so the map from a place puts the place at the centre and
|
|
20
|
+
* every neighbour where its bearing and its days say — **north up, east
|
|
21
|
+
* right**, the angle from the bearing and the radius from the days marker.
|
|
22
|
+
* One ring per marker, spaced logarithmically so a day's ride and a month's
|
|
23
|
+
* voyage both fit on one page; a border with no route on the innermost ring; a
|
|
24
|
+
* place whose days nobody stated on the rim, marked unknown.
|
|
25
|
+
*
|
|
26
|
+
* **Positions are computed here, not solved by a layout engine.** GraphViz
|
|
27
|
+
* receives every node pinned and only draws, so the picture states what the
|
|
28
|
+
* notes state and nothing a solver decided. Everything in this module is
|
|
29
|
+
* arithmetic over the place records — no file, no process, no GraphViz — so
|
|
30
|
+
* it is tested by asserting coordinates.
|
|
31
|
+
*
|
|
32
|
+
* @module
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import { BEARINGS, TRAVEL_DAYS } from "./place-relations.mjs";
|
|
36
|
+
|
|
37
|
+
/* --------------------------------------------------------------------- */
|
|
38
|
+
/* Bearings */
|
|
39
|
+
/* --------------------------------------------------------------------- */
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The angle of a bearing in degrees, counter-clockwise from east, so north
|
|
43
|
+
* is up and east is right on a page whose `y` grows upward — GraphViz's
|
|
44
|
+
* coordinate system for a pinned position.
|
|
45
|
+
*
|
|
46
|
+
* @param {unknown} bearing - A bearing.
|
|
47
|
+
* @returns {number|undefined} `N` is 90, `E` 0, `S` -90, `W` 180; `undefined`
|
|
48
|
+
* for a value that is not a bearing.
|
|
49
|
+
*/
|
|
50
|
+
export function bearingAngle(bearing) {
|
|
51
|
+
const i = BEARINGS.indexOf(/** @type {string} */ (bearing));
|
|
52
|
+
if (i === -1) return undefined;
|
|
53
|
+
const angle = 90 - 45 * i;
|
|
54
|
+
return angle <= -180 ? angle + 360 : angle;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The unit vector of a bearing.
|
|
59
|
+
*
|
|
60
|
+
* @param {unknown} bearing - A bearing.
|
|
61
|
+
* @returns {{x: number, y: number}|undefined} The vector, or `undefined` for a
|
|
62
|
+
* value that is not a bearing.
|
|
63
|
+
*/
|
|
64
|
+
export function bearingVector(bearing) {
|
|
65
|
+
const angle = bearingAngle(bearing);
|
|
66
|
+
if (angle === undefined) return undefined;
|
|
67
|
+
const rad = (angle * Math.PI) / 180;
|
|
68
|
+
return { x: Math.cos(rad), y: Math.sin(rad) };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* How many steps around the compass separate two bearings, the shorter way:
|
|
73
|
+
* 0 for the same bearing, 1 for adjacent ones, 4 for opposites.
|
|
74
|
+
*
|
|
75
|
+
* @param {unknown} a - A bearing.
|
|
76
|
+
* @param {unknown} b - A bearing.
|
|
77
|
+
* @returns {number|undefined} The steps, or `undefined` when either is not a
|
|
78
|
+
* bearing.
|
|
79
|
+
*/
|
|
80
|
+
export function bearingSteps(a, b) {
|
|
81
|
+
const i = BEARINGS.indexOf(/** @type {string} */ (a));
|
|
82
|
+
const j = BEARINGS.indexOf(/** @type {string} */ (b));
|
|
83
|
+
if (i === -1 || j === -1) return undefined;
|
|
84
|
+
const d = Math.abs(i - j) % BEARINGS.length;
|
|
85
|
+
return Math.min(d, BEARINGS.length - d);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/* --------------------------------------------------------------------- */
|
|
89
|
+
/* Rings */
|
|
90
|
+
/* --------------------------------------------------------------------- */
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The horizon of the map from a place, in days. A place farther than this
|
|
94
|
+
* is a name at the rim rather than a node on a ring.
|
|
95
|
+
*
|
|
96
|
+
* @type {number}
|
|
97
|
+
*/
|
|
98
|
+
export const HORIZON_DAYS = 90;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The days markers that are rings — every marker of the scale up to the
|
|
102
|
+
* horizon.
|
|
103
|
+
*
|
|
104
|
+
* @type {readonly number[]}
|
|
105
|
+
*/
|
|
106
|
+
export const RING_DAYS = Object.freeze(TRAVEL_DAYS.filter((d) => d <= HORIZON_DAYS));
|
|
107
|
+
|
|
108
|
+
/** The radius of the one-day ring, in points. */
|
|
109
|
+
const INNER_RADIUS = 160;
|
|
110
|
+
|
|
111
|
+
/** The radius of the horizon ring, in points. */
|
|
112
|
+
const HORIZON_RADIUS = 560;
|
|
113
|
+
|
|
114
|
+
/** Points of radius per unit of `ln(days)`: the log spacing of the rings. */
|
|
115
|
+
const RADIUS_PER_LOG_DAY = (HORIZON_RADIUS - INNER_RADIUS) / Math.log(HORIZON_DAYS);
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The radius of the rim — where a place beyond the horizon, or one whose
|
|
119
|
+
* days nobody stated, is named. One log step beyond the horizon, so the rim
|
|
120
|
+
* reads as the next ring rather than a margin.
|
|
121
|
+
*
|
|
122
|
+
* @returns {number} Points.
|
|
123
|
+
*/
|
|
124
|
+
export function rimRadius() {
|
|
125
|
+
return INNER_RADIUS + RADIUS_PER_LOG_DAY * Math.log(HORIZON_DAYS * 2);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The radius of the ring a days value sits on.
|
|
130
|
+
*
|
|
131
|
+
* Logarithmic in days, so equal ratios of days are equal steps of radius: a
|
|
132
|
+
* day to two days is the same step as five to ten. Any value beyond the
|
|
133
|
+
* horizon, and no value at all, is the rim.
|
|
134
|
+
*
|
|
135
|
+
* @param {number|undefined} days - A days value, on the scale or not.
|
|
136
|
+
* @returns {number} Points.
|
|
137
|
+
*/
|
|
138
|
+
export function ringRadius(days) {
|
|
139
|
+
if (typeof days !== "number" || !Number.isFinite(days) || days <= 0) return rimRadius();
|
|
140
|
+
if (days > HORIZON_DAYS) return rimRadius();
|
|
141
|
+
return INNER_RADIUS + RADIUS_PER_LOG_DAY * Math.log(days);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The rings, inside out — one per marker up to the horizon.
|
|
146
|
+
*
|
|
147
|
+
* @returns {Array<{days: number, radius: number}>} Each marker and its radius.
|
|
148
|
+
*/
|
|
149
|
+
export function rings() {
|
|
150
|
+
return RING_DAYS.map((days) => ({ days, radius: ringRadius(days) }));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The smallest marker of the scale that covers a total of days, or `undefined`
|
|
155
|
+
* beyond the scale's end or for no total at all.
|
|
156
|
+
*
|
|
157
|
+
* A two-hop journey adds two markers, and the sum is rarely a marker itself;
|
|
158
|
+
* the ring it belongs on is the first one no shorter than it — "about this,
|
|
159
|
+
* under normal conditions", rounded the pessimistic way.
|
|
160
|
+
*
|
|
161
|
+
* @param {number|undefined} total - Days, on the scale or not.
|
|
162
|
+
* @returns {number|undefined} The marker.
|
|
163
|
+
*/
|
|
164
|
+
export function daysMarker(total) {
|
|
165
|
+
if (typeof total !== "number" || !Number.isFinite(total)) return undefined;
|
|
166
|
+
return TRAVEL_DAYS.find((d) => d >= total);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The length a route's edge asks of `neato` in the whole route graph, in
|
|
171
|
+
* inches — the same log scale as the rings, so the god's-eye view and the
|
|
172
|
+
* itinerary agree about what a day and a month look like.
|
|
173
|
+
*
|
|
174
|
+
* @param {number|undefined} days - The route's days, or none for a border.
|
|
175
|
+
* @returns {number} Inches.
|
|
176
|
+
*/
|
|
177
|
+
export function edgeLength(days) {
|
|
178
|
+
const d = typeof days === "number" && Number.isFinite(days) && days > 0 ? days : 1;
|
|
179
|
+
return (INNER_RADIUS + RADIUS_PER_LOG_DAY * Math.log(d)) / 72;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/* --------------------------------------------------------------------- */
|
|
183
|
+
/* Composing hops */
|
|
184
|
+
/* --------------------------------------------------------------------- */
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* One hop of a journey: a bearing and, for a route, its days.
|
|
188
|
+
*
|
|
189
|
+
* @typedef {object} Hop
|
|
190
|
+
* @property {string} bearing - Where the far end lies from the near end.
|
|
191
|
+
* @property {number} [days] - The route's marker; absent for a border.
|
|
192
|
+
*/
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Two hops composed into one bearing and one total, or nothing where they
|
|
196
|
+
* disagree.
|
|
197
|
+
*
|
|
198
|
+
* Two hops agree when their bearings are the same or adjacent; then the
|
|
199
|
+
* composed direction is that of the two hops added as vectors weighted by
|
|
200
|
+
* their days, and the total is their sum. A hop with no days — a border —
|
|
201
|
+
* makes the total unknown, because a frontier states no distance, and then
|
|
202
|
+
* the two hops weigh the same, because nothing says which is the longer.
|
|
203
|
+
* Hops two or more steps apart do not compose:
|
|
204
|
+
* a place east and then north of here is not in any one direction, and the
|
|
205
|
+
* map omits it rather than guess.
|
|
206
|
+
*
|
|
207
|
+
* @param {Hop} first - The hop from the centre.
|
|
208
|
+
* @param {Hop} second - The hop onward.
|
|
209
|
+
* @returns {{angle: number, days: number|undefined}|undefined} The composed
|
|
210
|
+
* bearing in degrees and the total days, or `undefined` where the hops
|
|
211
|
+
* disagree or either bearing is not one.
|
|
212
|
+
*/
|
|
213
|
+
export function composeHops(first, second) {
|
|
214
|
+
const steps = bearingSteps(first?.bearing, second?.bearing);
|
|
215
|
+
if (steps === undefined || steps > 1) return undefined;
|
|
216
|
+
const u = /** @type {{x: number, y: number}} */ (bearingVector(first.bearing));
|
|
217
|
+
const v = /** @type {{x: number, y: number}} */ (bearingVector(second.bearing));
|
|
218
|
+
const known = (d) => typeof d === "number" && Number.isFinite(d) && d > 0;
|
|
219
|
+
const both = known(first.days) && known(second.days);
|
|
220
|
+
const w1 = both ? /** @type {number} */ (first.days) : 1;
|
|
221
|
+
const w2 = both ? /** @type {number} */ (second.days) : 1;
|
|
222
|
+
const x = w1 * u.x + w2 * v.x;
|
|
223
|
+
const y = w1 * u.y + w2 * v.y;
|
|
224
|
+
const angle = (Math.atan2(y, x) * 180) / Math.PI;
|
|
225
|
+
const days =
|
|
226
|
+
both ? /** @type {number} */ (first.days) + /** @type {number} */ (second.days) : undefined;
|
|
227
|
+
// -0 from atan2 on an exactly-east result reads as 0.
|
|
228
|
+
return { angle: angle === 0 ? 0 : angle, days };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/* --------------------------------------------------------------------- */
|
|
232
|
+
/* Reading a place's relations from both ends */
|
|
233
|
+
/* --------------------------------------------------------------------- */
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* A place as the map reads it. The shape {@link module:engine/map-places}
|
|
237
|
+
* builds from the content index; only the relation fields matter here.
|
|
238
|
+
*
|
|
239
|
+
* @typedef {object} MapPlace
|
|
240
|
+
* @property {string} shortcode
|
|
241
|
+
* @property {string} name
|
|
242
|
+
* @property {string} [subType]
|
|
243
|
+
* @property {string[]} parents
|
|
244
|
+
* @property {Array<{to: string, bearing: string}>} borders
|
|
245
|
+
* @property {Array<{to: string, bearing: string, mode?: string, days?: number}>} routes
|
|
246
|
+
* @property {string} [url]
|
|
247
|
+
* @property {string} [address]
|
|
248
|
+
*/
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* A relation between two places, read from whichever end states it.
|
|
252
|
+
*
|
|
253
|
+
* @typedef {object} Relation
|
|
254
|
+
* @property {string} from
|
|
255
|
+
* @property {string} to
|
|
256
|
+
* @property {string} bearing - Where `to` lies from `from`.
|
|
257
|
+
* @property {"border"|"route"} kind
|
|
258
|
+
* @property {number} [days]
|
|
259
|
+
* @property {string} [mode]
|
|
260
|
+
* @property {boolean} stated - Whether `from` states it, as opposed to `to`.
|
|
261
|
+
*/
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The lower-case shortcode a relation entry names.
|
|
265
|
+
*
|
|
266
|
+
* @param {unknown} entry - A `borders` or `routes` entry.
|
|
267
|
+
* @returns {string} The shortcode, or `""` for an entry naming none.
|
|
268
|
+
*/
|
|
269
|
+
function targetOf(entry) {
|
|
270
|
+
return String(entry?.to ?? "").toLowerCase();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* A days value as a number on the scale, or `undefined`.
|
|
275
|
+
*
|
|
276
|
+
* @param {unknown} value - The authored value.
|
|
277
|
+
* @returns {number|undefined} The days.
|
|
278
|
+
*/
|
|
279
|
+
function daysOf(value) {
|
|
280
|
+
const n = typeof value === "string" ? Number(value) : value;
|
|
281
|
+
return typeof n === "number" && TRAVEL_DAYS.includes(n) ? n : undefined;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* The bearing the other end of a relation states.
|
|
286
|
+
*
|
|
287
|
+
* @param {string} bearing - A bearing.
|
|
288
|
+
* @returns {string} The opposite one.
|
|
289
|
+
*/
|
|
290
|
+
function opposite(bearing) {
|
|
291
|
+
const i = BEARINGS.indexOf(bearing);
|
|
292
|
+
return BEARINGS[(i + 4) % BEARINGS.length];
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Every relation touching one place, read from both ends.
|
|
297
|
+
*
|
|
298
|
+
* A border or a route is one fact stated from each end, and a note that
|
|
299
|
+
* states one side of it does not make the fact untrue — so the map reads what
|
|
300
|
+
* the place states *and* what every other place states about it, inverting
|
|
301
|
+
* the bearing on the way back. The place's own statement wins where both
|
|
302
|
+
* exist, and a target no place declares is returned separately so the caller
|
|
303
|
+
* can name it. The lint is where a missing reciprocal is reported; here it is
|
|
304
|
+
* simply drawn.
|
|
305
|
+
*
|
|
306
|
+
* @param {Map<string, MapPlace>} places - Every place, by shortcode.
|
|
307
|
+
* @param {string} shortcode - The place asked about.
|
|
308
|
+
* @returns {{relations: Relation[], unresolved: Array<{from: string, to: string}>}}
|
|
309
|
+
* The relations, own statements first, and the targets that resolved to
|
|
310
|
+
* nothing.
|
|
311
|
+
*/
|
|
312
|
+
export function relationsOf(places, shortcode) {
|
|
313
|
+
const self = places.get(shortcode);
|
|
314
|
+
/** @type {Relation[]} */
|
|
315
|
+
const relations = [];
|
|
316
|
+
/** @type {Array<{from: string, to: string}>} */
|
|
317
|
+
const unresolved = [];
|
|
318
|
+
const seen = new Set();
|
|
319
|
+
const key = (r) => `${r.to}|${r.kind}|${r.mode ?? ""}`;
|
|
320
|
+
|
|
321
|
+
const own = (list, kind) => {
|
|
322
|
+
for (const entry of Array.isArray(list) ? list : []) {
|
|
323
|
+
const to = targetOf(entry);
|
|
324
|
+
const bearing = String(entry?.bearing ?? "");
|
|
325
|
+
if (!to || !BEARINGS.includes(bearing)) continue;
|
|
326
|
+
if (!places.has(to)) {
|
|
327
|
+
unresolved.push({ from: shortcode, to });
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
const relation = {
|
|
331
|
+
from: shortcode,
|
|
332
|
+
to,
|
|
333
|
+
bearing,
|
|
334
|
+
kind,
|
|
335
|
+
stated: true,
|
|
336
|
+
...(kind === "route" ?
|
|
337
|
+
{ days: daysOf(entry.days), mode: String(entry.mode ?? "") }
|
|
338
|
+
: {}),
|
|
339
|
+
};
|
|
340
|
+
if (seen.has(key(relation))) continue;
|
|
341
|
+
seen.add(key(relation));
|
|
342
|
+
relations.push(relation);
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
own(self?.borders, "border");
|
|
346
|
+
own(self?.routes, "route");
|
|
347
|
+
|
|
348
|
+
for (const [other, place] of places) {
|
|
349
|
+
if (other === shortcode) continue;
|
|
350
|
+
const inbound = (list, kind) => {
|
|
351
|
+
for (const entry of Array.isArray(list) ? list : []) {
|
|
352
|
+
if (targetOf(entry) !== shortcode) continue;
|
|
353
|
+
const theirs = String(entry?.bearing ?? "");
|
|
354
|
+
if (!BEARINGS.includes(theirs)) continue;
|
|
355
|
+
const relation = {
|
|
356
|
+
from: shortcode,
|
|
357
|
+
to: other,
|
|
358
|
+
bearing: opposite(theirs),
|
|
359
|
+
kind,
|
|
360
|
+
stated: false,
|
|
361
|
+
...(kind === "route" ?
|
|
362
|
+
{ days: daysOf(entry.days), mode: String(entry.mode ?? "") }
|
|
363
|
+
: {}),
|
|
364
|
+
};
|
|
365
|
+
if (seen.has(key(relation))) continue;
|
|
366
|
+
seen.add(key(relation));
|
|
367
|
+
relations.push(relation);
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
inbound(place.borders, "border");
|
|
371
|
+
inbound(place.routes, "route");
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return { relations, unresolved };
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/* --------------------------------------------------------------------- */
|
|
378
|
+
/* The map from a place */
|
|
379
|
+
/* --------------------------------------------------------------------- */
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* One place on the map from a place.
|
|
383
|
+
*
|
|
384
|
+
* @typedef {object} FromNode
|
|
385
|
+
* @property {string} shortcode
|
|
386
|
+
* @property {number} hop - 0 for the centre, 1 for a neighbour, 2 beyond.
|
|
387
|
+
* @property {number} x - Points, east positive.
|
|
388
|
+
* @property {number} y - Points, north positive.
|
|
389
|
+
* @property {number} angle - Degrees, counter-clockwise from east.
|
|
390
|
+
* @property {number} radius - Points from the centre.
|
|
391
|
+
* @property {number} [days] - The days the ring was chosen by, or none.
|
|
392
|
+
* @property {boolean} border - Reached by a border alone.
|
|
393
|
+
* @property {boolean} unknown - On the rim because nobody stated its days.
|
|
394
|
+
* @property {string} [via] - The neighbour a second hop is reached through.
|
|
395
|
+
*/
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* One edge on the map from a place.
|
|
399
|
+
*
|
|
400
|
+
* @typedef {object} FromEdge
|
|
401
|
+
* @property {string} from
|
|
402
|
+
* @property {string} to
|
|
403
|
+
* @property {"border"|"route"} kind - A route where any route reaches `to`.
|
|
404
|
+
* @property {string} bearing
|
|
405
|
+
* @property {number} [days] - The shortest route's.
|
|
406
|
+
* @property {string} [mode] - The shortest route's.
|
|
407
|
+
* @property {Array<{mode: string, days: number|undefined}>} routes - Every
|
|
408
|
+
* route that reaches `to`, for the label.
|
|
409
|
+
* @property {number} hop - 1 from the centre, 2 onward.
|
|
410
|
+
*/
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Half the spread, in degrees, between neighbours that share a ring and a
|
|
414
|
+
* bearing, so they fan out rather than stack.
|
|
415
|
+
*/
|
|
416
|
+
const FAN_DEGREES = 14;
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Fan apart the nodes that would otherwise sit on one point.
|
|
420
|
+
*
|
|
421
|
+
* Nodes on one ring at one angle are spread evenly about that angle, in name
|
|
422
|
+
* order so the picture is stable between runs, and their mean stays where the
|
|
423
|
+
* bearing put it.
|
|
424
|
+
*
|
|
425
|
+
* @param {FromNode[]} nodes - The nodes, angles set.
|
|
426
|
+
* @param {Map<string, MapPlace>} places - For the names.
|
|
427
|
+
* @returns {void}
|
|
428
|
+
*/
|
|
429
|
+
function fanApart(nodes, places) {
|
|
430
|
+
/** @type {Map<string, FromNode[]>} */
|
|
431
|
+
const groups = new Map();
|
|
432
|
+
for (const node of nodes) {
|
|
433
|
+
if (node.hop === 0) continue;
|
|
434
|
+
const key = `${node.radius.toFixed(3)}@${node.angle.toFixed(3)}`;
|
|
435
|
+
const group = groups.get(key) ?? [];
|
|
436
|
+
group.push(node);
|
|
437
|
+
groups.set(key, group);
|
|
438
|
+
}
|
|
439
|
+
for (const group of groups.values()) {
|
|
440
|
+
if (group.length < 2) continue;
|
|
441
|
+
group.sort((a, b) =>
|
|
442
|
+
String(places.get(a.shortcode)?.name ?? a.shortcode).localeCompare(
|
|
443
|
+
String(places.get(b.shortcode)?.name ?? b.shortcode),
|
|
444
|
+
),
|
|
445
|
+
);
|
|
446
|
+
const step = Math.min(FAN_DEGREES, (2 * FAN_DEGREES) / (group.length - 1));
|
|
447
|
+
group.forEach((node, i) => {
|
|
448
|
+
node.angle += (i - (group.length - 1) / 2) * step;
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Lay out the map from a place.
|
|
455
|
+
*
|
|
456
|
+
* The centre at the origin. Every neighbour it states, or that states it, at
|
|
457
|
+
* the angle of its bearing and on the ring of its days — the innermost ring
|
|
458
|
+
* for a border with no route, the shortest route where several modes reach
|
|
459
|
+
* one neighbour. Beyond each neighbour, every place a second hop reaches, at
|
|
460
|
+
* the composed bearing where the two hops agree and on the ring of their
|
|
461
|
+
* total; on the rim, marked unknown, where either hop states no days; omitted
|
|
462
|
+
* where the hops disagree. A place reachable by several second hops takes
|
|
463
|
+
* the one with known days, then the shortest. Nodes sharing a ring and a
|
|
464
|
+
* bearing are fanned apart.
|
|
465
|
+
*
|
|
466
|
+
* @param {Map<string, MapPlace>} places - Every place, by shortcode.
|
|
467
|
+
* @param {string} centre - The place the map is from.
|
|
468
|
+
* @returns {{centre: string, nodes: FromNode[], edges: FromEdge[],
|
|
469
|
+
* rings: Array<{days: number, radius: number}>, rim: number,
|
|
470
|
+
* unresolved: Array<{from: string, to: string}>}} The layout, in points.
|
|
471
|
+
* @throws {Error} When `centre` is not a place.
|
|
472
|
+
*/
|
|
473
|
+
export function layoutFrom(places, centre) {
|
|
474
|
+
if (!places.has(centre)) {
|
|
475
|
+
throw new Error(`"${centre}" is not a place in this package or a fetched index`);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/** @type {FromNode[]} */
|
|
479
|
+
const nodes = [
|
|
480
|
+
{
|
|
481
|
+
shortcode: centre,
|
|
482
|
+
hop: 0,
|
|
483
|
+
x: 0,
|
|
484
|
+
y: 0,
|
|
485
|
+
angle: 0,
|
|
486
|
+
radius: 0,
|
|
487
|
+
border: false,
|
|
488
|
+
unknown: false,
|
|
489
|
+
},
|
|
490
|
+
];
|
|
491
|
+
/** @type {FromEdge[]} */
|
|
492
|
+
const edges = [];
|
|
493
|
+
/** @type {Map<string, FromNode>} */
|
|
494
|
+
const byShortcode = new Map([[centre, nodes[0]]]);
|
|
495
|
+
/** @type {Map<string, {bearing: string, days: number|undefined}>} */
|
|
496
|
+
const firstHops = new Map();
|
|
497
|
+
|
|
498
|
+
const { relations, unresolved } = relationsOf(places, centre);
|
|
499
|
+
|
|
500
|
+
// Hop 1: one node and one edge per neighbour, the ring from its shortest
|
|
501
|
+
// route, the edge carrying every mode that reaches it.
|
|
502
|
+
/** @type {Map<string, FromEdge>} */
|
|
503
|
+
const firstEdges = new Map();
|
|
504
|
+
for (const relation of relations) {
|
|
505
|
+
const days = relation.kind === "route" ? relation.days : undefined;
|
|
506
|
+
let node = byShortcode.get(relation.to);
|
|
507
|
+
if (!node) {
|
|
508
|
+
node = {
|
|
509
|
+
shortcode: relation.to,
|
|
510
|
+
hop: 1,
|
|
511
|
+
x: 0,
|
|
512
|
+
y: 0,
|
|
513
|
+
angle: /** @type {number} */ (bearingAngle(relation.bearing)),
|
|
514
|
+
radius: days === undefined ? ringRadius(1) : ringRadius(days),
|
|
515
|
+
days,
|
|
516
|
+
border: relation.kind === "border",
|
|
517
|
+
// A route whose days are off the scale sits on the rim, and
|
|
518
|
+
// says so; a border sits on the innermost ring by rule.
|
|
519
|
+
unknown: relation.kind === "route" && days === undefined,
|
|
520
|
+
};
|
|
521
|
+
nodes.push(node);
|
|
522
|
+
byShortcode.set(relation.to, node);
|
|
523
|
+
firstHops.set(relation.to, { bearing: relation.bearing, days });
|
|
524
|
+
firstEdges.set(relation.to, {
|
|
525
|
+
from: centre,
|
|
526
|
+
to: relation.to,
|
|
527
|
+
kind: relation.kind,
|
|
528
|
+
bearing: relation.bearing,
|
|
529
|
+
hop: 1,
|
|
530
|
+
routes: [],
|
|
531
|
+
});
|
|
532
|
+
edges.push(/** @type {FromEdge} */ (firstEdges.get(relation.to)));
|
|
533
|
+
}
|
|
534
|
+
const edge = /** @type {FromEdge} */ (firstEdges.get(relation.to));
|
|
535
|
+
if (relation.kind !== "route") continue;
|
|
536
|
+
edge.routes.push({ mode: String(relation.mode ?? ""), days });
|
|
537
|
+
// A route outranks a border for the node's ring and the edge's style,
|
|
538
|
+
// and the shortest route sets both.
|
|
539
|
+
if (
|
|
540
|
+
edge.kind === "border" ||
|
|
541
|
+
(days !== undefined && (edge.days === undefined || days < edge.days))
|
|
542
|
+
) {
|
|
543
|
+
edge.kind = "route";
|
|
544
|
+
edge.days = days;
|
|
545
|
+
edge.mode = String(relation.mode ?? "");
|
|
546
|
+
node.days = days;
|
|
547
|
+
node.border = false;
|
|
548
|
+
node.radius = ringRadius(days);
|
|
549
|
+
node.unknown = days === undefined;
|
|
550
|
+
firstHops.set(relation.to, { bearing: relation.bearing, days });
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// Hop 2: the best composed candidate per place beyond the neighbours.
|
|
555
|
+
/** @type {Map<string, {node: FromNode, edge: FromEdge, marker: number|undefined}>} */
|
|
556
|
+
const candidates = new Map();
|
|
557
|
+
const better = (a, b) => {
|
|
558
|
+
if (!b) return true;
|
|
559
|
+
const aKnown = a.node.days !== undefined;
|
|
560
|
+
const bKnown = b.node.days !== undefined;
|
|
561
|
+
if (aKnown !== bKnown) return aKnown;
|
|
562
|
+
if (aKnown && a.node.days !== b.node.days) return a.node.days < b.node.days;
|
|
563
|
+
return String(a.node.via).localeCompare(String(b.node.via)) < 0;
|
|
564
|
+
};
|
|
565
|
+
for (const [via, first] of firstHops) {
|
|
566
|
+
const onward = relationsOf(places, via);
|
|
567
|
+
for (const relation of onward.relations) {
|
|
568
|
+
if (byShortcode.has(relation.to)) continue;
|
|
569
|
+
const second = { bearing: relation.bearing, days: relation.days };
|
|
570
|
+
const composed = composeHops(first, second);
|
|
571
|
+
if (!composed) continue;
|
|
572
|
+
const marker = daysMarker(composed.days);
|
|
573
|
+
const node = {
|
|
574
|
+
shortcode: relation.to,
|
|
575
|
+
hop: 2,
|
|
576
|
+
x: 0,
|
|
577
|
+
y: 0,
|
|
578
|
+
angle: composed.angle,
|
|
579
|
+
radius: ringRadius(marker),
|
|
580
|
+
days: composed.days,
|
|
581
|
+
border: relation.kind === "border" && first.days === undefined,
|
|
582
|
+
unknown: composed.days === undefined,
|
|
583
|
+
via,
|
|
584
|
+
};
|
|
585
|
+
const edge = {
|
|
586
|
+
from: via,
|
|
587
|
+
to: relation.to,
|
|
588
|
+
kind: relation.kind,
|
|
589
|
+
bearing: relation.bearing,
|
|
590
|
+
hop: 2,
|
|
591
|
+
routes:
|
|
592
|
+
relation.kind === "route" ?
|
|
593
|
+
[{ mode: String(relation.mode ?? ""), days: relation.days }]
|
|
594
|
+
: [],
|
|
595
|
+
...(relation.kind === "route" ? { days: relation.days, mode: relation.mode } : {}),
|
|
596
|
+
};
|
|
597
|
+
const candidate = { node, edge, marker };
|
|
598
|
+
if (better(candidate, candidates.get(relation.to))) {
|
|
599
|
+
candidates.set(relation.to, candidate);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
for (const { node, edge } of [...candidates.values()].sort((a, b) =>
|
|
604
|
+
a.node.shortcode.localeCompare(b.node.shortcode),
|
|
605
|
+
)) {
|
|
606
|
+
nodes.push(node);
|
|
607
|
+
edges.push(edge);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
fanApart(nodes, places);
|
|
611
|
+
for (const node of nodes) {
|
|
612
|
+
if (node.hop === 0) continue;
|
|
613
|
+
const rad = (node.angle * Math.PI) / 180;
|
|
614
|
+
node.x = exact(node.radius * Math.cos(rad));
|
|
615
|
+
node.y = exact(node.radius * Math.sin(rad));
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
return { centre, nodes, edges, rings: rings(), rim: rimRadius(), unresolved };
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* A coordinate with the floating-point dust of a right angle swept off, so a
|
|
623
|
+
* place due north sits at `x = 0` rather than at `1e-14`.
|
|
624
|
+
*
|
|
625
|
+
* @param {number} value - Points.
|
|
626
|
+
* @returns {number} The value, or `0` where it is within a millionth of it.
|
|
627
|
+
*/
|
|
628
|
+
function exact(value) {
|
|
629
|
+
return Math.abs(value) < 1e-6 ? 0 : value;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/* --------------------------------------------------------------------- */
|
|
633
|
+
/* The whole route graph */
|
|
634
|
+
/* --------------------------------------------------------------------- */
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* One edge of the whole route graph.
|
|
638
|
+
*
|
|
639
|
+
* @typedef {object} TravelEdge
|
|
640
|
+
* @property {string} from
|
|
641
|
+
* @property {string} to
|
|
642
|
+
* @property {"border"|"route"} kind
|
|
643
|
+
* @property {string} bearing - Where `to` lies from `from`.
|
|
644
|
+
* @property {number} [days]
|
|
645
|
+
* @property {string} [mode]
|
|
646
|
+
* @property {number} len - The length asked of `neato`, in inches.
|
|
647
|
+
*/
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* The whole route graph: one edge per pair of places, however many ends and
|
|
651
|
+
* modes state it, with a length from its days.
|
|
652
|
+
*
|
|
653
|
+
* A pair reached by several modes takes the shortest; a pair that both
|
|
654
|
+
* borders and travels takes the route, since the days are what the length
|
|
655
|
+
* is from. The end that states the relation is `from`, so a one-sided entry
|
|
656
|
+
* is drawn from the note that carries it.
|
|
657
|
+
*
|
|
658
|
+
* @param {Map<string, MapPlace>} places - Every place, by shortcode.
|
|
659
|
+
* @returns {{edges: TravelEdge[], unresolved: Array<{from: string, to: string}>}}
|
|
660
|
+
* The edges, in a stable order.
|
|
661
|
+
*/
|
|
662
|
+
export function travelGraph(places) {
|
|
663
|
+
/** @type {Map<string, TravelEdge>} */
|
|
664
|
+
const edges = new Map();
|
|
665
|
+
/** @type {Array<{from: string, to: string}>} */
|
|
666
|
+
const unresolved = [];
|
|
667
|
+
const pairKey = (a, b) => [a, b].sort().join("|");
|
|
668
|
+
|
|
669
|
+
for (const shortcode of [...places.keys()].sort()) {
|
|
670
|
+
const place = places.get(shortcode);
|
|
671
|
+
const consider = (list, kind) => {
|
|
672
|
+
for (const entry of Array.isArray(list) ? list : []) {
|
|
673
|
+
const to = targetOf(entry);
|
|
674
|
+
const bearing = String(entry?.bearing ?? "");
|
|
675
|
+
if (!to || !BEARINGS.includes(bearing)) continue;
|
|
676
|
+
if (!places.has(to)) {
|
|
677
|
+
unresolved.push({ from: shortcode, to });
|
|
678
|
+
continue;
|
|
679
|
+
}
|
|
680
|
+
const days = kind === "route" ? daysOf(entry.days) : undefined;
|
|
681
|
+
const edge = {
|
|
682
|
+
from: shortcode,
|
|
683
|
+
to,
|
|
684
|
+
kind,
|
|
685
|
+
bearing,
|
|
686
|
+
len: edgeLength(days),
|
|
687
|
+
...(kind === "route" ? { days, mode: String(entry.mode ?? "") } : {}),
|
|
688
|
+
};
|
|
689
|
+
const key = pairKey(shortcode, to);
|
|
690
|
+
const existing = edges.get(key);
|
|
691
|
+
if (!existing) {
|
|
692
|
+
edges.set(key, edge);
|
|
693
|
+
continue;
|
|
694
|
+
}
|
|
695
|
+
// A route outranks a border, and a shorter route a longer.
|
|
696
|
+
if (existing.kind === "border" && kind === "route") edges.set(key, edge);
|
|
697
|
+
else if (
|
|
698
|
+
existing.kind === "route" &&
|
|
699
|
+
kind === "route" &&
|
|
700
|
+
days !== undefined &&
|
|
701
|
+
(existing.days === undefined || days < existing.days)
|
|
702
|
+
) {
|
|
703
|
+
edges.set(key, edge);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
consider(place?.borders, "border");
|
|
708
|
+
consider(place?.routes, "route");
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
return { edges: [...edges.values()], unresolved };
|
|
712
|
+
}
|