@johnmorrisdotca/wakusei-planets 0.31.0 → 0.32.0
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 +3 -0
- package/NOTICE.md +5 -0
- package/README.md +57 -3
- package/dist/data-door.d.ts +357 -0
- package/dist/data-door.js +393 -0
- package/dist/door/book.d.ts +133 -0
- package/dist/door/book.js +256 -0
- package/dist/door/host.d.ts +30 -0
- package/dist/door/host.js +44 -0
- package/dist/door/index.d.ts +6 -0
- package/dist/door/index.js +10 -0
- package/dist/door/map.d.ts +152 -0
- package/dist/door/map.js +162 -0
- package/dist/door/own.d.ts +36 -0
- package/dist/door/own.js +86 -0
- package/dist/door/points.d.ts +61 -0
- package/dist/door/points.js +175 -0
- package/dist/door/small-bodies.d.ts +157 -0
- package/dist/door/small-bodies.js +148 -0
- package/dist/element.d.ts +21 -0
- package/dist/element.js +74 -4
- package/dist/scene/body.d.ts +4 -1
- package/dist/scene/body.js +2 -1
- package/dist/scene/detail.d.ts +29 -12
- package/dist/scene/detail.js +217 -135
- package/dist/scene/painter.d.ts +3 -0
- package/dist/scene/painter.js +7 -4
- package/dist/scene/textures.d.ts +9 -2
- package/dist/scene/textures.js +17 -0
- package/dist/space/element.d.ts +24 -0
- package/dist/space/element.js +56 -3
- package/dist/space/scene.d.ts +8 -0
- package/dist/space/scene.js +28 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +6 -1
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// The sources a view holds: which are in it, which are switched off, the one tile store they share (one byte budget for all of them), the records of the record sources once read, and the credits of what was drawn.
|
|
2
|
+
// An element or a painter owns one. Everything here is plain TypeScript with no DOM and no three.js; the elements call it, and a test can too.
|
|
3
|
+
import { createTileStore, validateSource } from "../data-door.js";
|
|
4
|
+
import { BODIES } from "../facts.js";
|
|
5
|
+
import { isOwnSource } from "./own.js";
|
|
6
|
+
import { readSmallBodies } from "./small-bodies.js";
|
|
7
|
+
const owners = new WeakMap();
|
|
8
|
+
let ownerCount = 0;
|
|
9
|
+
/** The name a source's tiles are held under in the store for one body and one drawing of it (`owner`): `<id>/<body>#<n>`, so two drawings of the same body never share, or drop, each other's tiles. */
|
|
10
|
+
export const accountOf = (source, body, owner) => {
|
|
11
|
+
let n = owners.get(owner);
|
|
12
|
+
if (n === undefined)
|
|
13
|
+
owners.set(owner, (n = ownerCount += 1));
|
|
14
|
+
return `${source.info.id}/${body}#${n}`;
|
|
15
|
+
};
|
|
16
|
+
const isTile = (tile) => {
|
|
17
|
+
if (typeof tile !== "object" || tile === null)
|
|
18
|
+
return false;
|
|
19
|
+
const one = tile;
|
|
20
|
+
return typeof one.width === "number" && one.width > 0 && Number.isFinite(one.width) && typeof one.height === "number" && one.height > 0 && Number.isFinite(one.height) && typeof one.image === "object" && one.image !== null;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Makes the sources of one view.
|
|
24
|
+
*
|
|
25
|
+
* @param options - The kinds the view draws, and the budget.
|
|
26
|
+
* @returns The book.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { createSourceBook } from "@johnmorrisdotca/wakusei-planets/door";
|
|
31
|
+
*
|
|
32
|
+
* const book = createSourceBook({ kinds: ["tiles"] });
|
|
33
|
+
* console.log(book.active().length);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function createSourceBook(options = {}) {
|
|
37
|
+
const kinds = new Set(options.kinds ?? ["tiles", "records"]);
|
|
38
|
+
const list = [];
|
|
39
|
+
const off = new Set();
|
|
40
|
+
const controllers = new Map();
|
|
41
|
+
const reads = new Map();
|
|
42
|
+
const accounts = new Map();
|
|
43
|
+
const loaders = new Map();
|
|
44
|
+
const listeners = new Set();
|
|
45
|
+
const reading = new Set();
|
|
46
|
+
let onlyOwn = false;
|
|
47
|
+
let gone = false;
|
|
48
|
+
let version = 0;
|
|
49
|
+
const notify = () => {
|
|
50
|
+
for (const listener of [...listeners])
|
|
51
|
+
listener();
|
|
52
|
+
};
|
|
53
|
+
const store = createTileStore({
|
|
54
|
+
...(options.budgetBytes === undefined ? {} : { budgetBytes: options.budgetBytes }),
|
|
55
|
+
...(options.maxTiles === undefined ? {} : { maxTiles: options.maxTiles }),
|
|
56
|
+
...(options.maxInFlight === undefined ? {} : { maxInFlight: options.maxInFlight }),
|
|
57
|
+
onChange: notify,
|
|
58
|
+
});
|
|
59
|
+
const isOwn = (source) => isOwnSource(source);
|
|
60
|
+
const inUse = (source) => !off.has(source.info.id) && !(onlyOwn && !isOwn(source));
|
|
61
|
+
const active = () => list.filter(inUse);
|
|
62
|
+
/** Aborts a source's loads and frees its tiles and records. */
|
|
63
|
+
const release = (id) => {
|
|
64
|
+
controllers.get(id)?.abort();
|
|
65
|
+
controllers.delete(id);
|
|
66
|
+
if (reads.delete(id))
|
|
67
|
+
version += 1;
|
|
68
|
+
for (const account of accounts.get(id) ?? []) {
|
|
69
|
+
store.drop(account);
|
|
70
|
+
loaders.delete(account);
|
|
71
|
+
}
|
|
72
|
+
accounts.delete(id);
|
|
73
|
+
};
|
|
74
|
+
/** Starts reading a record source: once, when it is added or switched on. */
|
|
75
|
+
const start = (source) => {
|
|
76
|
+
if (source.kind !== "records" || controllers.has(source.info.id))
|
|
77
|
+
return;
|
|
78
|
+
const controller = new AbortController();
|
|
79
|
+
controllers.set(source.info.id, controller);
|
|
80
|
+
const done = readSmallBodies(source, controller.signal).then((read) => {
|
|
81
|
+
reading.delete(done);
|
|
82
|
+
if (controllers.get(source.info.id) !== controller || read.status === "aborted")
|
|
83
|
+
return;
|
|
84
|
+
reads.set(source.info.id, read);
|
|
85
|
+
version += 1;
|
|
86
|
+
notify();
|
|
87
|
+
});
|
|
88
|
+
reading.add(done);
|
|
89
|
+
};
|
|
90
|
+
const check = (source) => {
|
|
91
|
+
validateSource(source, list.map((one) => one.info.id));
|
|
92
|
+
const id = source.info.id;
|
|
93
|
+
if (!kinds.has(source.kind))
|
|
94
|
+
throw new TypeError(`source "${id}": this view takes ${[...kinds].join(" and ")} sources, not ${source.kind} sources`);
|
|
95
|
+
if (source.kind === "tiles") {
|
|
96
|
+
const map = source;
|
|
97
|
+
if (typeof map.body !== "string" || (map.body !== "*" && !(map.body in BODIES)))
|
|
98
|
+
throw new TypeError(`source "${id}": a map source names its body (a body of this package, or "*" for a folder of them), not ${JSON.stringify(map.body)}`);
|
|
99
|
+
if (map.layer !== "colour" && map.layer !== "height")
|
|
100
|
+
throw new TypeError(`source "${id}": a map source's layer is "colour" or "height", not ${JSON.stringify(map.layer)}`);
|
|
101
|
+
}
|
|
102
|
+
return source;
|
|
103
|
+
};
|
|
104
|
+
const remove = (id) => {
|
|
105
|
+
const at = list.findIndex((source) => source.info.id === id);
|
|
106
|
+
if (at < 0)
|
|
107
|
+
return false;
|
|
108
|
+
list.splice(at, 1);
|
|
109
|
+
off.delete(id);
|
|
110
|
+
release(id);
|
|
111
|
+
notify();
|
|
112
|
+
return true;
|
|
113
|
+
};
|
|
114
|
+
return {
|
|
115
|
+
get sources() {
|
|
116
|
+
return [...list];
|
|
117
|
+
},
|
|
118
|
+
addSource(source) {
|
|
119
|
+
const one = check(source);
|
|
120
|
+
list.push(one);
|
|
121
|
+
if (inUse(one))
|
|
122
|
+
start(one);
|
|
123
|
+
notify();
|
|
124
|
+
return () => void remove(one.info.id);
|
|
125
|
+
},
|
|
126
|
+
removeSource: remove,
|
|
127
|
+
enableSource(id, on) {
|
|
128
|
+
const source = list.find((one) => one.info.id === id);
|
|
129
|
+
if (source === undefined || off.has(id) === !on)
|
|
130
|
+
return;
|
|
131
|
+
if (on) {
|
|
132
|
+
off.delete(id);
|
|
133
|
+
if (inUse(source))
|
|
134
|
+
start(source);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
off.add(id);
|
|
138
|
+
release(id);
|
|
139
|
+
}
|
|
140
|
+
notify();
|
|
141
|
+
},
|
|
142
|
+
isEnabled: (id) => list.some((source) => source.info.id === id) && !off.has(id),
|
|
143
|
+
ownOnly(on) {
|
|
144
|
+
if (on === onlyOwn)
|
|
145
|
+
return;
|
|
146
|
+
onlyOwn = on;
|
|
147
|
+
for (const source of list) {
|
|
148
|
+
if (isOwn(source) || off.has(source.info.id))
|
|
149
|
+
continue;
|
|
150
|
+
if (on)
|
|
151
|
+
release(source.info.id);
|
|
152
|
+
else
|
|
153
|
+
start(source);
|
|
154
|
+
}
|
|
155
|
+
notify();
|
|
156
|
+
},
|
|
157
|
+
get onlyOwn() {
|
|
158
|
+
return onlyOwn;
|
|
159
|
+
},
|
|
160
|
+
active,
|
|
161
|
+
mapSources: (body, layer = "colour") => active()
|
|
162
|
+
.filter((source) => source.kind === "tiles" && source.layer === layer && (source.body === body || source.body === "*"))
|
|
163
|
+
.reverse(),
|
|
164
|
+
smallBodies: () => active().flatMap((source) => (reads.get(source.info.id)?.records ?? [])),
|
|
165
|
+
recordReads: () => new Map([...reads].filter(([id]) => list.some((source) => source.info.id === id && inUse(source)))),
|
|
166
|
+
isOwn,
|
|
167
|
+
get: (id) => list.find((source) => source.info.id === id),
|
|
168
|
+
store,
|
|
169
|
+
dropAccount(source, body, owner) {
|
|
170
|
+
const account = accountOf(source, body, owner);
|
|
171
|
+
store.drop(account);
|
|
172
|
+
loaders.delete(account);
|
|
173
|
+
accounts.get(source.info.id)?.delete(account);
|
|
174
|
+
},
|
|
175
|
+
loader(source, body, owner, onRequest) {
|
|
176
|
+
const account = accountOf(source, body, owner);
|
|
177
|
+
const held = loaders.get(account);
|
|
178
|
+
if (held !== undefined && held.source === source)
|
|
179
|
+
return held.loader;
|
|
180
|
+
const info = { ...source.info, id: account };
|
|
181
|
+
const loader = {
|
|
182
|
+
info,
|
|
183
|
+
async load(key, signal) {
|
|
184
|
+
onRequest?.();
|
|
185
|
+
const tile = await source.load(key, signal);
|
|
186
|
+
if (!isTile(tile))
|
|
187
|
+
throw new TypeError(`Source "${source.info.id}" gave a tile "${key}" that is not a picture`);
|
|
188
|
+
return tile;
|
|
189
|
+
},
|
|
190
|
+
bytes: (tile) => source.bytes(tile),
|
|
191
|
+
};
|
|
192
|
+
loaders.set(account, { source, loader });
|
|
193
|
+
const mine = accounts.get(source.info.id) ?? new Set();
|
|
194
|
+
mine.add(account);
|
|
195
|
+
accounts.set(source.info.id, mine);
|
|
196
|
+
return loader;
|
|
197
|
+
},
|
|
198
|
+
onChange(listener) {
|
|
199
|
+
listeners.add(listener);
|
|
200
|
+
return () => void listeners.delete(listener);
|
|
201
|
+
},
|
|
202
|
+
stats() {
|
|
203
|
+
const records = {};
|
|
204
|
+
for (const [id, read] of reads)
|
|
205
|
+
records[id] = { status: read.status, kept: read.records.length, skipped: read.skipped };
|
|
206
|
+
return { ...store.stats(), records };
|
|
207
|
+
},
|
|
208
|
+
get version() {
|
|
209
|
+
return version;
|
|
210
|
+
},
|
|
211
|
+
async ready() {
|
|
212
|
+
await Promise.all([...reading]);
|
|
213
|
+
await store.idle();
|
|
214
|
+
},
|
|
215
|
+
dispose() {
|
|
216
|
+
if (gone)
|
|
217
|
+
return;
|
|
218
|
+
gone = true;
|
|
219
|
+
for (const source of [...list])
|
|
220
|
+
release(source.info.id);
|
|
221
|
+
list.length = 0;
|
|
222
|
+
listeners.clear();
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* The credits of a view for the last drawn frame: the package's own first, then the sources a caller added, each with the marks or tiles it gave; a source that gave none is left out.
|
|
228
|
+
*
|
|
229
|
+
* @param own - The package's own credits (`ownCredit`), with how many marks each gave.
|
|
230
|
+
* @param book - The view's sources.
|
|
231
|
+
* @param used - How many marks or tiles each source of the book gave, by id.
|
|
232
|
+
* @returns The credits, own first, in a stable order.
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* ```ts
|
|
236
|
+
* import { createSourceBook, creditsOf, ownCredit } from "@johnmorrisdotca/wakusei-planets/door";
|
|
237
|
+
*
|
|
238
|
+
* const book = createSourceBook();
|
|
239
|
+
* console.log(creditsOf([ownCredit("wakusei-pictures", 1)], book, new Map()).length);
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
export function creditsOf(own, book, used) {
|
|
243
|
+
const mine = own.filter((credit) => credit.used > 0);
|
|
244
|
+
const theirs = [];
|
|
245
|
+
for (const source of book.active()) {
|
|
246
|
+
const count = used.get(source.info.id) ?? 0;
|
|
247
|
+
if (count <= 0)
|
|
248
|
+
continue;
|
|
249
|
+
const credit = { source: source.info, own: book.isOwn(source), used: count };
|
|
250
|
+
if (credit.own)
|
|
251
|
+
mine.push(credit);
|
|
252
|
+
else
|
|
253
|
+
theirs.push(credit);
|
|
254
|
+
}
|
|
255
|
+
return [...mine, ...theirs];
|
|
256
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Credit, DataSource } from "../data-door.ts";
|
|
2
|
+
import { type SourceBook, type SourceBookOptions } from "./book.ts";
|
|
3
|
+
/** What an element tells its host about the last drawn frame. */
|
|
4
|
+
export interface DoorFrame {
|
|
5
|
+
/** The package's own credits, with how many marks each gave (`ownCredit`). */
|
|
6
|
+
own(): readonly Credit[];
|
|
7
|
+
/** How many marks or tiles each of the book's sources gave, by id. */
|
|
8
|
+
used(): ReadonlyMap<string, number>;
|
|
9
|
+
}
|
|
10
|
+
export interface DoorHost {
|
|
11
|
+
readonly book: SourceBook;
|
|
12
|
+
/** The sources set by a page, in the order added. */
|
|
13
|
+
sources(): readonly DataSource[];
|
|
14
|
+
/** Replaces every source: all are checked first, and if one is refused nothing changes and the `TypeError` is thrown. */
|
|
15
|
+
setSources(list: readonly unknown[]): void;
|
|
16
|
+
/** The credits of the last drawn frame, the package's own first. */
|
|
17
|
+
credits(): Credit[];
|
|
18
|
+
/** Fires `wakusei-creditschange` when the set of credited sources changed since the last call (not at each draw). */
|
|
19
|
+
announce(): void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Makes the door of one element.
|
|
23
|
+
*
|
|
24
|
+
* @param target - The element: it gets `wakusei-creditschange`.
|
|
25
|
+
* @param frame - What the last drawn frame held.
|
|
26
|
+
* @param options - The kinds of source the element draws, and the budget.
|
|
27
|
+
* @param redraw - Asked for when a source was added, removed or switched, or a tile arrived.
|
|
28
|
+
* @returns The host.
|
|
29
|
+
*/
|
|
30
|
+
export declare function createDoorHost(target: EventTarget, frame: DoorFrame, options: SourceBookOptions, redraw: () => void): DoorHost;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createSourceBook, creditsOf } from "./book.js";
|
|
2
|
+
/**
|
|
3
|
+
* Makes the door of one element.
|
|
4
|
+
*
|
|
5
|
+
* @param target - The element: it gets `wakusei-creditschange`.
|
|
6
|
+
* @param frame - What the last drawn frame held.
|
|
7
|
+
* @param options - The kinds of source the element draws, and the budget.
|
|
8
|
+
* @param redraw - Asked for when a source was added, removed or switched, or a tile arrived.
|
|
9
|
+
* @returns The host.
|
|
10
|
+
*/
|
|
11
|
+
export function createDoorHost(target, frame, options, redraw) {
|
|
12
|
+
const book = createSourceBook(options);
|
|
13
|
+
let shown = "";
|
|
14
|
+
const credits = () => creditsOf(frame.own(), book, frame.used());
|
|
15
|
+
const announce = () => {
|
|
16
|
+
const now = credits();
|
|
17
|
+
const signature = now.map((credit) => credit.source.id).join(",");
|
|
18
|
+
if (signature === shown)
|
|
19
|
+
return;
|
|
20
|
+
shown = signature;
|
|
21
|
+
target.dispatchEvent(new CustomEvent("wakusei-creditschange", { detail: { credits: now } }));
|
|
22
|
+
};
|
|
23
|
+
book.onChange(() => {
|
|
24
|
+
redraw();
|
|
25
|
+
announce();
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
book,
|
|
29
|
+
sources: () => book.sources,
|
|
30
|
+
setSources(list) {
|
|
31
|
+
const old = [...book.sources];
|
|
32
|
+
const checker = createSourceBook(options);
|
|
33
|
+
for (const one of list)
|
|
34
|
+
checker.addSource(one);
|
|
35
|
+
checker.dispose();
|
|
36
|
+
for (const one of old)
|
|
37
|
+
book.removeSource(one.info.id);
|
|
38
|
+
for (const one of list)
|
|
39
|
+
book.addSource(one);
|
|
40
|
+
},
|
|
41
|
+
credits,
|
|
42
|
+
announce,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { createTileStore, creditLine, loadRecords, MAX_IN_FLIGHT, MAX_RECORDS, TILE_BUDGET_BYTES, validateSource } from "../data-door.ts";
|
|
2
|
+
export type { BitmapLike, Credit, DataSource, ImageTile, Licence, MapPlace, RecordsRead, RecordSource, SourceInfo, TileLoader, TileSource, TileStats, TileStore, TileStoreOptions } from "../data-door.ts";
|
|
3
|
+
export { DEFAULT_LEVELS, forgetManifests, imageBytes, mapKeys, tileKeyParts, tilesSource, type BodyMapPlace, type BodyMapSource, type MapLayer, type TilesSourceOptions } from "./map.ts";
|
|
4
|
+
export { OWN_SOURCES, ownCredit } from "./own.ts";
|
|
5
|
+
export { createSourceBook, creditsOf, type SourceBook, type SourceBookOptions, type SourceStats } from "./book.ts";
|
|
6
|
+
export { GAUSS_K, isSmallBody, MAX_POINTS, packSmallBodies, readSmallBodies, smallBodyAt, smallBodyNumbers, type SmallBodiesRead, type SmallBody, type SmallBodySource } from "./small-bodies.ts";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// The data door of the planets (`@johnmorrisdotca/wakusei-planets/door`, MODULES-831): one way to hand the package outside data, and to see whose data is on the screen. A source is a small typed object that carries its
|
|
2
|
+
// own licence and credit. A map source (`BodyMapSource`) is the finer map of a body in tiles, and `tilesSource` is the package's own folder of them, which is what the `tiles` attribute means; a record source of
|
|
3
|
+
// small bodies (`SmallBodySource`) is drawn as points in `<wakusei-space>`. `<wakusei-body>` and `<wakusei-space>` take `sources` and `addSource`, and `credits()` says whose data was in the last drawn frame.
|
|
4
|
+
// The shapes and the four small functions are `../data-door.ts`, the same file in every package of the family (kazoku-ui's templates/data-door.ts). Nothing here is imported by the main entry or the elements' own
|
|
5
|
+
// entries beyond what they use: a page that never uses the door pays nothing. With no outside source every view draws what it always drew. Pure: no DOM, no three.js, no clock.
|
|
6
|
+
export { createTileStore, creditLine, loadRecords, MAX_IN_FLIGHT, MAX_RECORDS, TILE_BUDGET_BYTES, validateSource } from "../data-door.js";
|
|
7
|
+
export { DEFAULT_LEVELS, forgetManifests, imageBytes, mapKeys, tileKeyParts, tilesSource } from "./map.js";
|
|
8
|
+
export { OWN_SOURCES, ownCredit } from "./own.js";
|
|
9
|
+
export { createSourceBook, creditsOf } from "./book.js";
|
|
10
|
+
export { GAUSS_K, isSmallBody, MAX_POINTS, packSmallBodies, readSmallBodies, smallBodyAt, smallBodyNumbers } from "./small-bodies.js";
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { BodyId } from "../facts.ts";
|
|
2
|
+
import type { ImageTile, MapPlace, TileSource } from "../data-door.ts";
|
|
3
|
+
import { type TileLevel } from "../scene/tiles.ts";
|
|
4
|
+
/**
|
|
5
|
+
* What a map source draws: `colour` is the picture of the body's surface; `height` is its relief, which the package does not draw yet (the type is the door it will come through).
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import type { MapLayer } from "@johnmorrisdotca/wakusei-planets/door";
|
|
10
|
+
*
|
|
11
|
+
* const layers: MapLayer[] = ["colour", "height"];
|
|
12
|
+
* console.log(layers.join(", "));
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export type MapLayer = "colour" | "height";
|
|
16
|
+
/**
|
|
17
|
+
* The place a map source is asked about: the family's `MapPlace` in degrees (longitude from `lonMin` to `lonMax`, which may pass 180 and is at most a whole turn, latitude from `latMin` to `latMax`), and the body it is for.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import type { BodyMapPlace } from "@johnmorrisdotca/wakusei-planets/door";
|
|
22
|
+
*
|
|
23
|
+
* const place: BodyMapPlace = { lonMin: 170, lonMax: 190, latMin: -10, latMax: 10, body: "mars" };
|
|
24
|
+
* console.log(place.body);
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export type BodyMapPlace = MapPlace & {
|
|
28
|
+
body?: string;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* A source of a body's map in tiles, for the colour of its surface or for its height: a `TileSource` of the family's map shape (the place is a `MapPlace`, the detail is the pixels across the whole circumference, a tile is an
|
|
32
|
+
* `ImageTile`) that says which `body` and `layer` it is for. A `body` of `"*"` is every body of a folder (the place then names the one, in `place.body`, and the keys start with its name). The keys a source returns end in
|
|
33
|
+
* `level/column/row` on the package's pyramid. `levels` says which levels the source has when that has to be asked (a folder's manifest); left out, levels 1 to 3.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import type { BodyMapSource } from "@johnmorrisdotca/wakusei-planets/door";
|
|
38
|
+
*
|
|
39
|
+
* const bodies = (sources: readonly BodyMapSource[]): string[] => sources.map((source) => `${source.body}:${source.layer}`);
|
|
40
|
+
* console.log(bodies([]));
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export type BodyMapSource = TileSource<BodyMapPlace, number, ImageTile> & {
|
|
44
|
+
body: BodyId | "*";
|
|
45
|
+
layer: MapLayer;
|
|
46
|
+
levels?(body: string, signal: AbortSignal): readonly TileLevel[] | undefined | Promise<readonly TileLevel[] | undefined>;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* The levels a source has when it does not say: the three the package's cutter makes at most.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { DEFAULT_LEVELS } from "@johnmorrisdotca/wakusei-planets/door";
|
|
54
|
+
*
|
|
55
|
+
* console.log(DEFAULT_LEVELS.map((level) => level.width));
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare const DEFAULT_LEVELS: readonly TileLevel[];
|
|
59
|
+
/**
|
|
60
|
+
* The level, column and row a tile key ends in, or `undefined` for a key that does not end in three numbers.
|
|
61
|
+
*
|
|
62
|
+
* @param key - The tile's key.
|
|
63
|
+
* @returns Where the tile is in the pyramid, or `undefined` for a key that does not end in three numbers.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { tileKeyParts } from "@johnmorrisdotca/wakusei-planets/door";
|
|
68
|
+
*
|
|
69
|
+
* console.log(tileKeyParts("mars/3/12/5"));
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare function tileKeyParts(key: string): {
|
|
73
|
+
level: number;
|
|
74
|
+
col: number;
|
|
75
|
+
row: number;
|
|
76
|
+
} | undefined;
|
|
77
|
+
/**
|
|
78
|
+
* The keys of the tiles that cover a place at a detail, on the package's pyramid: the level is the lowest whose width is at least `detail` pixels round (or the finest there is), and the tiles are every one the place touches.
|
|
79
|
+
* Pure. A source of your own can build its `need` on it.
|
|
80
|
+
*
|
|
81
|
+
* @param place - The part of the map, in degrees.
|
|
82
|
+
* @param detail - The pixels across the whole circumference wanted.
|
|
83
|
+
* @param levels - The levels the source has, level 1 up; levels 1 to 3 where left out.
|
|
84
|
+
* @param prefix - Put before each key (`"mars/"`), for a folder of several bodies.
|
|
85
|
+
* @returns The keys, `[<prefix>]level/column/row`, row by row; none for a place with no area.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* import { mapKeys } from "@johnmorrisdotca/wakusei-planets/door";
|
|
90
|
+
*
|
|
91
|
+
* console.log(mapKeys({ lonMin: -10, lonMax: 10, latMin: -5, latMax: 5 }, 8192));
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export declare function mapKeys(place: MapPlace, detail: number, levels?: readonly TileLevel[], prefix?: string): string[];
|
|
95
|
+
/**
|
|
96
|
+
* What `tilesSource` takes.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import type { TilesSourceOptions } from "@johnmorrisdotca/wakusei-planets/door";
|
|
101
|
+
*
|
|
102
|
+
* const options: TilesSourceOptions = { fetch: (url, init) => fetch(url, init) };
|
|
103
|
+
* console.log(typeof options.fetch);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export interface TilesSourceOptions {
|
|
107
|
+
/** The `fetch` to use: the global one where left out. It is called as `fetch(address, { mode: "cors", signal })`. */
|
|
108
|
+
fetch?: (url: string, init?: RequestInit) => Promise<Response>;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Forget what was read of the tile folders (for a test that serves different ones).
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts no-run
|
|
115
|
+
* import { forgetManifests } from "@johnmorrisdotca/wakusei-planets/door";
|
|
116
|
+
*
|
|
117
|
+
* forgetManifests();
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export declare function forgetManifests(): void;
|
|
121
|
+
/**
|
|
122
|
+
* The package's own finer map tiles as a source: a folder with a `manifest.json` and a tile for each body, level and place at `<body>/<level>/<column>/<row>.webp` (made by `scripts/make-body-tiles.py`). This is what the
|
|
123
|
+
* `tiles` attribute of an element means, and what a body with no source of yours is drawn from. It is for every body in the folder (`body: "*"`); the manifest says which levels each has, and it is read once, only when a view
|
|
124
|
+
* wants more than the body's own 2048 pixel map. Its `id` is `wakusei-tiles`, and a view counts it as the package's own data.
|
|
125
|
+
*
|
|
126
|
+
* @param base - The folder's address, with or without the closing slash.
|
|
127
|
+
* @param options - The `fetch` to use.
|
|
128
|
+
* @returns The source.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts no-run
|
|
132
|
+
* import { tilesSource } from "@johnmorrisdotca/wakusei-planets/door";
|
|
133
|
+
*
|
|
134
|
+
* const mine = tilesSource("/tiles/");
|
|
135
|
+
* console.log(mine.need({ lonMin: 0, lonMax: 20, latMin: 0, latMax: 10, body: "mars" }, 8192));
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export declare function tilesSource(base: string, options?: TilesSourceOptions): BodyMapSource;
|
|
139
|
+
/**
|
|
140
|
+
* What a decoded picture holds in memory, with its mipmaps (4/3 of its bytes): the `bytes` of a map source.
|
|
141
|
+
*
|
|
142
|
+
* @param tile - The tile.
|
|
143
|
+
* @returns Bytes.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* import { imageBytes } from "@johnmorrisdotca/wakusei-planets/door";
|
|
148
|
+
*
|
|
149
|
+
* console.log(imageBytes({ width: 512, height: 512, image: { width: 512, height: 512 } }));
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export declare function imageBytes(tile: ImageTile): number;
|
package/dist/door/map.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { BASE_WIDTH, parseManifest, TILE_SIZE } from "../scene/tiles.js";
|
|
2
|
+
import { OWN_BY_ID, madeHere } from "./own.js";
|
|
3
|
+
const RAD = Math.PI / 180;
|
|
4
|
+
const TAU = Math.PI * 2;
|
|
5
|
+
/** A tile's own edge is not the next tile: places are measured to a billionth of a tile. */
|
|
6
|
+
const EDGE = 1e-9;
|
|
7
|
+
/**
|
|
8
|
+
* The levels a source has when it does not say: the three the package's cutter makes at most.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { DEFAULT_LEVELS } from "@johnmorrisdotca/wakusei-planets/door";
|
|
13
|
+
*
|
|
14
|
+
* console.log(DEFAULT_LEVELS.map((level) => level.width));
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export const DEFAULT_LEVELS = [1, 2, 3].map((level) => {
|
|
18
|
+
const width = BASE_WIDTH * 2 ** level;
|
|
19
|
+
return { level, width, height: width / 2, columns: width / TILE_SIZE, rows: width / 2 / TILE_SIZE };
|
|
20
|
+
});
|
|
21
|
+
const KEY_END = /(\d+)\/(\d+)\/(\d+)$/;
|
|
22
|
+
/**
|
|
23
|
+
* The level, column and row a tile key ends in, or `undefined` for a key that does not end in three numbers.
|
|
24
|
+
*
|
|
25
|
+
* @param key - The tile's key.
|
|
26
|
+
* @returns Where the tile is in the pyramid, or `undefined` for a key that does not end in three numbers.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { tileKeyParts } from "@johnmorrisdotca/wakusei-planets/door";
|
|
31
|
+
*
|
|
32
|
+
* console.log(tileKeyParts("mars/3/12/5"));
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export function tileKeyParts(key) {
|
|
36
|
+
const found = KEY_END.exec(key);
|
|
37
|
+
return found === null ? undefined : { level: Number(found[1]), col: Number(found[2]), row: Number(found[3]) };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The keys of the tiles that cover a place at a detail, on the package's pyramid: the level is the lowest whose width is at least `detail` pixels round (or the finest there is), and the tiles are every one the place touches.
|
|
41
|
+
* Pure. A source of your own can build its `need` on it.
|
|
42
|
+
*
|
|
43
|
+
* @param place - The part of the map, in degrees.
|
|
44
|
+
* @param detail - The pixels across the whole circumference wanted.
|
|
45
|
+
* @param levels - The levels the source has, level 1 up; levels 1 to 3 where left out.
|
|
46
|
+
* @param prefix - Put before each key (`"mars/"`), for a folder of several bodies.
|
|
47
|
+
* @returns The keys, `[<prefix>]level/column/row`, row by row; none for a place with no area.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { mapKeys } from "@johnmorrisdotca/wakusei-planets/door";
|
|
52
|
+
*
|
|
53
|
+
* console.log(mapKeys({ lonMin: -10, lonMax: 10, latMin: -5, latMax: 5 }, 8192));
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export function mapKeys(place, detail, levels = DEFAULT_LEVELS, prefix = "") {
|
|
57
|
+
if (levels.length === 0 || !(place.lonMax > place.lonMin) || !(place.latMax >= place.latMin))
|
|
58
|
+
return [];
|
|
59
|
+
const level = levels.find((one) => one.width >= detail) ?? levels[levels.length - 1];
|
|
60
|
+
const { columns, rows } = level;
|
|
61
|
+
const span = Math.min(TAU, (place.lonMax - place.lonMin) * RAD);
|
|
62
|
+
const first = Math.floor(((place.lonMin * RAD) / TAU + 0.5) * columns + EDGE);
|
|
63
|
+
const count = span >= TAU - 1e-9 ? columns : Math.min(columns, Math.ceil(((place.lonMin * RAD + span) / TAU + 0.5) * columns - EDGE) - first);
|
|
64
|
+
const row0 = Math.max(0, Math.floor((0.5 - (place.latMax * RAD) / Math.PI) * rows + EDGE));
|
|
65
|
+
const row1 = Math.min(rows, Math.max(row0 + 1, Math.ceil((0.5 - (place.latMin * RAD) / Math.PI) * rows - EDGE)));
|
|
66
|
+
const keys = [];
|
|
67
|
+
for (let row = row0; row < row1; row += 1)
|
|
68
|
+
for (let i = 0; i < count; i += 1)
|
|
69
|
+
keys.push(`${prefix}${level.level}/${(((first + i) % columns) + columns) % columns}/${row}`);
|
|
70
|
+
return keys;
|
|
71
|
+
}
|
|
72
|
+
const slash = (base) => (base.endsWith("/") ? base : `${base}/`);
|
|
73
|
+
const RETRY_MS = 30_000;
|
|
74
|
+
// One manifest read for each folder of the page, shared by every element: a folder that could not be read is asked again only after a while, and only when a view wants more than the 2048 pixel map.
|
|
75
|
+
const manifests = new Map();
|
|
76
|
+
function readManifest(base, get) {
|
|
77
|
+
const now = performance.now();
|
|
78
|
+
const held = manifests.get(base);
|
|
79
|
+
if (held !== undefined && now - held.at < RETRY_MS)
|
|
80
|
+
return held.value;
|
|
81
|
+
const value = get(`${base}manifest.json`, { mode: "cors" }).then(async (response) => (response.ok ? parseManifest(await response.json().catch(() => undefined)) : undefined), () => undefined);
|
|
82
|
+
manifests.set(base, { at: now, value });
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Forget what was read of the tile folders (for a test that serves different ones).
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts no-run
|
|
90
|
+
* import { forgetManifests } from "@johnmorrisdotca/wakusei-planets/door";
|
|
91
|
+
*
|
|
92
|
+
* forgetManifests();
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export function forgetManifests() {
|
|
96
|
+
manifests.clear();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The package's own finer map tiles as a source: a folder with a `manifest.json` and a tile for each body, level and place at `<body>/<level>/<column>/<row>.webp` (made by `scripts/make-body-tiles.py`). This is what the
|
|
100
|
+
* `tiles` attribute of an element means, and what a body with no source of yours is drawn from. It is for every body in the folder (`body: "*"`); the manifest says which levels each has, and it is read once, only when a view
|
|
101
|
+
* wants more than the body's own 2048 pixel map. Its `id` is `wakusei-tiles`, and a view counts it as the package's own data.
|
|
102
|
+
*
|
|
103
|
+
* @param base - The folder's address, with or without the closing slash.
|
|
104
|
+
* @param options - The `fetch` to use.
|
|
105
|
+
* @returns The source.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts no-run
|
|
109
|
+
* import { tilesSource } from "@johnmorrisdotca/wakusei-planets/door";
|
|
110
|
+
*
|
|
111
|
+
* const mine = tilesSource("/tiles/");
|
|
112
|
+
* console.log(mine.need({ lonMin: 0, lonMax: 20, latMin: 0, latMax: 10, body: "mars" }, 8192));
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export function tilesSource(base, options = {}) {
|
|
116
|
+
const folder = slash(base);
|
|
117
|
+
const get = options.fetch ?? ((url, init) => globalThis.fetch(url, init));
|
|
118
|
+
const source = {
|
|
119
|
+
kind: "tiles",
|
|
120
|
+
body: "*",
|
|
121
|
+
layer: "colour",
|
|
122
|
+
info: OWN_BY_ID.get("wakusei-tiles"),
|
|
123
|
+
need(place, detail) {
|
|
124
|
+
const body = place.body;
|
|
125
|
+
if (typeof body !== "string" || body === "")
|
|
126
|
+
throw new RangeError("a folder of tiles is asked for a place with its body");
|
|
127
|
+
return mapKeys(place, detail, DEFAULT_LEVELS, `${body}/`);
|
|
128
|
+
},
|
|
129
|
+
async levels(body) {
|
|
130
|
+
const manifest = await readManifest(folder, get);
|
|
131
|
+
return manifest?.bodies[body];
|
|
132
|
+
},
|
|
133
|
+
async load(key, signal) {
|
|
134
|
+
if (tileKeyParts(key) === undefined)
|
|
135
|
+
throw new RangeError(`"${key}" is not the key of a tile`);
|
|
136
|
+
const response = await get(`${folder}${key}.webp`, { mode: "cors", signal });
|
|
137
|
+
if (!response.ok)
|
|
138
|
+
throw new Error(`The tile ${key} could not be fetched (${response.status})`);
|
|
139
|
+
const image = await createImageBitmap(await response.blob());
|
|
140
|
+
return { width: image.width > 0 ? image.width : TILE_SIZE, height: image.height > 0 ? image.height : TILE_SIZE, image };
|
|
141
|
+
},
|
|
142
|
+
bytes: (tile) => imageBytes(tile),
|
|
143
|
+
};
|
|
144
|
+
madeHere(source);
|
|
145
|
+
return source;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* What a decoded picture holds in memory, with its mipmaps (4/3 of its bytes): the `bytes` of a map source.
|
|
149
|
+
*
|
|
150
|
+
* @param tile - The tile.
|
|
151
|
+
* @returns Bytes.
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```ts
|
|
155
|
+
* import { imageBytes } from "@johnmorrisdotca/wakusei-planets/door";
|
|
156
|
+
*
|
|
157
|
+
* console.log(imageBytes({ width: 512, height: 512, image: { width: 512, height: 512 } }));
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
export function imageBytes(tile) {
|
|
161
|
+
return Math.ceil(tile.width * tile.height * 4 * (4 / 3));
|
|
162
|
+
}
|