@spellkave/world-map 0.0.0-stage → 0.1.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/LICENSE +89 -0
- package/README.md +66 -2
- package/bin/world-map.js +16 -0
- package/bin/world-osm.js +15 -0
- package/package.json +69 -4
- package/src/cli.ts +204 -0
- package/src/coords.ts +479 -0
- package/src/export.ts +861 -0
- package/src/ids.ts +81 -0
- package/src/index.ts +105 -0
- package/src/load-bundle.ts +155 -0
- package/src/model.ts +106 -0
- package/src/node/backdrop-resolve.ts +172 -0
- package/src/node/index.ts +39 -0
- package/src/node/jpeg-write.ts +266 -0
- package/src/node/rasterize.ts +27 -0
- package/src/node/terminal-capabilities.ts +89 -0
- package/src/node/terminal.ts +12 -0
- package/src/node/timg-backend.ts +136 -0
- package/src/node/tool-result-image.ts +53 -0
- package/src/poi.ts +61 -0
- package/src/rows.ts +480 -0
- package/src/svg/compose.ts +185 -0
- package/src/svg/h3-layout.ts +268 -0
- package/src/svg/index.ts +327 -0
- package/src/svg/inventory.ts +168 -0
- package/src/svg/layout.ts +431 -0
- package/src/svg/model.ts +459 -0
- package/src/svg/render-ascii.ts +486 -0
- package/src/svg/render-braille.ts +130 -0
- package/src/svg/render-svg.ts +578 -0
- package/src/svg/scale.ts +99 -0
- package/src/svg/types.ts +244 -0
- package/src/tags.ts +68 -0
- package/src/tiles/encode.ts +35 -0
- package/src/tiles/index.ts +25 -0
- package/src/tiles/layers.ts +338 -0
- package/src/tiles/style.ts +518 -0
- package/src/writers/geojson.ts +104 -0
- package/src/writers/json.ts +126 -0
- package/src/writers/xml.ts +87 -0
package/src/rows.ts
ADDED
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plain-row contract for @spellkave/world-map.
|
|
3
|
+
*
|
|
4
|
+
* Accepts the snake_case shapes of:
|
|
5
|
+
* - `@spellkave/sdk` `readPlaceTopology()` / `readMapLayers()`
|
|
6
|
+
* - manifest TOML rows (`place_graphs`, `place_nodes`, `place_edges`, `map_layers`,
|
|
7
|
+
* `substrate_definitions`)
|
|
8
|
+
*
|
|
9
|
+
* The package never knows whether rows came from a live DB view or a file.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { resolveEffectiveSpatialEnvironmentStates } from "@spellkave/sdk/environment-state";
|
|
13
|
+
|
|
14
|
+
/** Adapt the file/SDK plain-row contract at this boundary, then share precedence. */
|
|
15
|
+
function effectiveStateRows(input: readonly Record<string, unknown>[]): Record<string, unknown>[] {
|
|
16
|
+
return resolveEffectiveSpatialEnvironmentStates(
|
|
17
|
+
input.map((raw) => ({
|
|
18
|
+
raw,
|
|
19
|
+
bundleId: str(raw, "bundle_id"),
|
|
20
|
+
stateId: str(raw, "state_id"),
|
|
21
|
+
stateRowId: BigInt(String(raw.state_row_id ?? raw.stateRowId ?? 0)),
|
|
22
|
+
updatedTick: BigInt(String(raw.updated_tick ?? raw.updatedTick ?? 0)),
|
|
23
|
+
targetKind: str(raw, "target_kind"),
|
|
24
|
+
targetRef: raw.targetRef ?? raw.target_ref,
|
|
25
|
+
placeNodeId: str(raw, "place_node_id", str(raw, "node_id")),
|
|
26
|
+
placeEdgeId: String(raw.place_edge_id ?? raw.edge_id ?? raw.placeEdgeId ?? raw.edge_key ?? 0),
|
|
27
|
+
locationDefId: str(raw, "location_def_id"),
|
|
28
|
+
cellRes10: BigInt(String(raw.cell_res10 ?? raw.cellRes10 ?? 0)),
|
|
29
|
+
cellRes7: BigInt(String(raw.cell_res7 ?? raw.cellRes7 ?? 0)),
|
|
30
|
+
cellRes4: BigInt(String(raw.cell_res4 ?? raw.cellRes4 ?? 0)),
|
|
31
|
+
substrateId: str(raw, "substrate_id"),
|
|
32
|
+
})),
|
|
33
|
+
).map((row) => row.raw);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface GraphRow {
|
|
37
|
+
place_graph_id: string;
|
|
38
|
+
parent_graph_id: string;
|
|
39
|
+
anchor_node_id: string;
|
|
40
|
+
substrate_id: string;
|
|
41
|
+
bearing: number;
|
|
42
|
+
metres_per_unit: number;
|
|
43
|
+
origin_cell_res10: unknown;
|
|
44
|
+
origin_cell_res7: unknown;
|
|
45
|
+
origin_cell_res4: unknown;
|
|
46
|
+
display_name: string;
|
|
47
|
+
description: string;
|
|
48
|
+
bundle_id: string;
|
|
49
|
+
blueprint_id: string;
|
|
50
|
+
is_ephemeral_instance: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface NodeRow {
|
|
54
|
+
node_id: string;
|
|
55
|
+
place_graph_id: string;
|
|
56
|
+
bundle_id: string;
|
|
57
|
+
local_id: string;
|
|
58
|
+
substrate_id: string;
|
|
59
|
+
display_name: string;
|
|
60
|
+
description: string;
|
|
61
|
+
cell_res10: unknown;
|
|
62
|
+
cell_res7: unknown;
|
|
63
|
+
cell_res4: unknown;
|
|
64
|
+
pos_present: boolean;
|
|
65
|
+
pos_x: number;
|
|
66
|
+
pos_y: number;
|
|
67
|
+
pose_elevation_present: boolean;
|
|
68
|
+
pose_elevation: number;
|
|
69
|
+
extent_shape_id: string;
|
|
70
|
+
extent_params_json: string;
|
|
71
|
+
is_ephemeral: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface EdgeRow {
|
|
75
|
+
edge_id?: string;
|
|
76
|
+
edge_key: string;
|
|
77
|
+
bundle_id: string;
|
|
78
|
+
place_graph_id: string;
|
|
79
|
+
from_node_id: string;
|
|
80
|
+
to_node_id: string;
|
|
81
|
+
pose_dt_elevation_present: boolean;
|
|
82
|
+
pose_dt_elevation: number;
|
|
83
|
+
substrate_crossing: boolean;
|
|
84
|
+
cell_anchor_present: boolean;
|
|
85
|
+
cell_anchor: unknown;
|
|
86
|
+
cell_anchor_u: number;
|
|
87
|
+
cell_anchor_v: number;
|
|
88
|
+
from_anchor_side: string;
|
|
89
|
+
from_anchor_offset: number;
|
|
90
|
+
to_anchor_side: string;
|
|
91
|
+
to_anchor_offset: number;
|
|
92
|
+
description: string;
|
|
93
|
+
inherit_movement_default: boolean;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface MapLayerRow {
|
|
97
|
+
map_layer_id: string;
|
|
98
|
+
place_graph_id: string;
|
|
99
|
+
display_name: string;
|
|
100
|
+
asset_module: string;
|
|
101
|
+
asset_path: string;
|
|
102
|
+
source_kind: string;
|
|
103
|
+
posture: string;
|
|
104
|
+
state: string;
|
|
105
|
+
image_width_px: number;
|
|
106
|
+
image_height_px: number;
|
|
107
|
+
grid_size_px: number;
|
|
108
|
+
grid_distance_m: number;
|
|
109
|
+
local_width_cells: number;
|
|
110
|
+
local_height_cells: number;
|
|
111
|
+
image_origin_x: number;
|
|
112
|
+
image_origin_y: number;
|
|
113
|
+
rotation_deg: number;
|
|
114
|
+
min_zoom: number;
|
|
115
|
+
max_zoom: number;
|
|
116
|
+
bundle_id: string;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface SubstrateDef {
|
|
120
|
+
substrate_id: string;
|
|
121
|
+
coordinate_kind_id: string;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface MetadataAttachmentRow {
|
|
125
|
+
target_kind: string;
|
|
126
|
+
target_ref: string;
|
|
127
|
+
namespace: string;
|
|
128
|
+
payload_json: string;
|
|
129
|
+
audience?: string;
|
|
130
|
+
bundle_id?: string;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Normalized typed bag consumed by projectWorld / exportWorld. */
|
|
134
|
+
export interface EdgeAffordanceRow {
|
|
135
|
+
edge_key: string;
|
|
136
|
+
affordance_template_id: string;
|
|
137
|
+
mode_id: string;
|
|
138
|
+
bindings_json?: string;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface EdgeStateRow {
|
|
142
|
+
edge_id?: string;
|
|
143
|
+
edge_key?: string;
|
|
144
|
+
state_id: string;
|
|
145
|
+
value: boolean | string | number | Record<string, unknown>;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface SpatialNicheAttachmentRow {
|
|
149
|
+
place_node_id: string;
|
|
150
|
+
niche_id: string;
|
|
151
|
+
condition_bool?: boolean;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface NicheDefinitionRow {
|
|
155
|
+
niche_id: string;
|
|
156
|
+
display_name: string;
|
|
157
|
+
description?: string;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export interface NodeStateRow {
|
|
161
|
+
node_id: string;
|
|
162
|
+
state_id: string;
|
|
163
|
+
value: boolean | string | number | Record<string, unknown>;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface NormalizedRows {
|
|
167
|
+
place_graphs: GraphRow[];
|
|
168
|
+
place_nodes: NodeRow[];
|
|
169
|
+
place_edges: EdgeRow[];
|
|
170
|
+
map_layers: MapLayerRow[];
|
|
171
|
+
substrate_definitions: SubstrateDef[];
|
|
172
|
+
metadata_attachments: MetadataAttachmentRow[];
|
|
173
|
+
place_edge_affordances: EdgeAffordanceRow[];
|
|
174
|
+
edge_states: EdgeStateRow[];
|
|
175
|
+
spatial_niche_attachments: SpatialNicheAttachmentRow[];
|
|
176
|
+
niche_definitions: NicheDefinitionRow[];
|
|
177
|
+
node_states: NodeStateRow[];
|
|
178
|
+
default_extent_shape_id: string;
|
|
179
|
+
default_extent_params_json: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Loose input accepted at the package boundary.
|
|
184
|
+
* Keys match view/TOML column names (snake_case).
|
|
185
|
+
*/
|
|
186
|
+
export interface WorldOsmInput {
|
|
187
|
+
place_graphs?: readonly Record<string, unknown>[];
|
|
188
|
+
place_nodes?: readonly Record<string, unknown>[];
|
|
189
|
+
place_edges?: readonly Record<string, unknown>[];
|
|
190
|
+
map_layers?: readonly Record<string, unknown>[];
|
|
191
|
+
substrate_definitions?: readonly Record<string, unknown>[];
|
|
192
|
+
metadata_attachments?: readonly Record<string, unknown>[];
|
|
193
|
+
place_edge_affordances?: readonly Record<string, unknown>[];
|
|
194
|
+
edge_states?: readonly Record<string, unknown>[];
|
|
195
|
+
spatial_niche_attachments?: readonly Record<string, unknown>[];
|
|
196
|
+
niche_definitions?: readonly Record<string, unknown>[];
|
|
197
|
+
spatial_environment_states?: readonly Record<string, unknown>[];
|
|
198
|
+
node_states?: readonly Record<string, unknown>[];
|
|
199
|
+
/** Bundle-level default extent (optional). */
|
|
200
|
+
default_extent_shape_id?: string;
|
|
201
|
+
default_extent_params_json?: string;
|
|
202
|
+
/** Alias used by some loaders / bundle header. */
|
|
203
|
+
bundles?: readonly Record<string, unknown>[];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function isRecord(v: unknown): v is Record<string, unknown> {
|
|
207
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function str(row: Record<string, unknown>, key: string, fallback = ""): string {
|
|
211
|
+
const v = row[key];
|
|
212
|
+
return typeof v === "string" ? v : fallback;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function num(row: Record<string, unknown>, key: string, fallback = 0): number {
|
|
216
|
+
const v = row[key];
|
|
217
|
+
return typeof v === "number" && Number.isFinite(v) ? v : fallback;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function bool(row: Record<string, unknown>, key: string, fallback = false): boolean {
|
|
221
|
+
const v = row[key];
|
|
222
|
+
return typeof v === "boolean" ? v : fallback;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function toGraph(row: Record<string, unknown>): GraphRow {
|
|
226
|
+
return {
|
|
227
|
+
place_graph_id: str(row, "place_graph_id"),
|
|
228
|
+
parent_graph_id: str(row, "parent_graph_id"),
|
|
229
|
+
anchor_node_id: str(row, "anchor_node_id"),
|
|
230
|
+
substrate_id: str(row, "substrate_id"),
|
|
231
|
+
bearing: num(row, "bearing"),
|
|
232
|
+
metres_per_unit: num(row, "metres_per_unit", 1),
|
|
233
|
+
origin_cell_res10: row.origin_cell_res10 ?? 0,
|
|
234
|
+
origin_cell_res7: row.origin_cell_res7 ?? 0,
|
|
235
|
+
origin_cell_res4: row.origin_cell_res4 ?? 0,
|
|
236
|
+
display_name: str(row, "display_name"),
|
|
237
|
+
description: str(row, "description"),
|
|
238
|
+
bundle_id: str(row, "bundle_id"),
|
|
239
|
+
blueprint_id: str(row, "blueprint_id"),
|
|
240
|
+
is_ephemeral_instance: bool(row, "is_ephemeral_instance"),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function toNode(row: Record<string, unknown>): NodeRow {
|
|
245
|
+
return {
|
|
246
|
+
node_id: str(row, "node_id"),
|
|
247
|
+
place_graph_id: str(row, "place_graph_id"),
|
|
248
|
+
bundle_id: str(row, "bundle_id"),
|
|
249
|
+
local_id: str(row, "local_id"),
|
|
250
|
+
substrate_id: str(row, "substrate_id"),
|
|
251
|
+
display_name: str(row, "display_name"),
|
|
252
|
+
description: str(row, "description"),
|
|
253
|
+
cell_res10: row.cell_res10 ?? 0,
|
|
254
|
+
cell_res7: row.cell_res7 ?? 0,
|
|
255
|
+
cell_res4: row.cell_res4 ?? 0,
|
|
256
|
+
pos_present: bool(row, "pos_present"),
|
|
257
|
+
pos_x: num(row, "pos_x"),
|
|
258
|
+
pos_y: num(row, "pos_y"),
|
|
259
|
+
pose_elevation_present: bool(row, "pose_elevation_present"),
|
|
260
|
+
pose_elevation: num(row, "pose_elevation"),
|
|
261
|
+
extent_shape_id: str(row, "extent_shape_id"),
|
|
262
|
+
extent_params_json: str(row, "extent_params_json"),
|
|
263
|
+
is_ephemeral: bool(row, "is_ephemeral"),
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function toEdge(row: Record<string, unknown>, index: number): EdgeRow {
|
|
268
|
+
const edgeKey = str(row, "edge_key");
|
|
269
|
+
return {
|
|
270
|
+
edge_id: str(row, "edge_id") || undefined,
|
|
271
|
+
edge_key: edgeKey || `edge:anon:${index}`,
|
|
272
|
+
bundle_id: str(row, "bundle_id"),
|
|
273
|
+
place_graph_id: str(row, "place_graph_id"),
|
|
274
|
+
from_node_id: str(row, "from_node_id"),
|
|
275
|
+
to_node_id: str(row, "to_node_id"),
|
|
276
|
+
pose_dt_elevation_present: bool(row, "pose_dt_elevation_present"),
|
|
277
|
+
pose_dt_elevation: num(row, "pose_dt_elevation"),
|
|
278
|
+
substrate_crossing: bool(row, "substrate_crossing"),
|
|
279
|
+
cell_anchor_present: bool(row, "cell_anchor_present"),
|
|
280
|
+
cell_anchor: row.cell_anchor ?? 0,
|
|
281
|
+
cell_anchor_u: num(row, "cell_anchor_u"),
|
|
282
|
+
cell_anchor_v: num(row, "cell_anchor_v"),
|
|
283
|
+
from_anchor_side: str(row, "from_anchor_side"),
|
|
284
|
+
from_anchor_offset: num(row, "from_anchor_offset"),
|
|
285
|
+
to_anchor_side: str(row, "to_anchor_side"),
|
|
286
|
+
to_anchor_offset: num(row, "to_anchor_offset"),
|
|
287
|
+
description: str(row, "description"),
|
|
288
|
+
inherit_movement_default: bool(row, "inherit_movement_default", true),
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function toMapLayer(row: Record<string, unknown>): MapLayerRow {
|
|
293
|
+
return {
|
|
294
|
+
map_layer_id: str(row, "map_layer_id"),
|
|
295
|
+
place_graph_id: str(row, "place_graph_id"),
|
|
296
|
+
display_name: str(row, "display_name"),
|
|
297
|
+
asset_module: str(row, "asset_module"),
|
|
298
|
+
asset_path: str(row, "asset_path"),
|
|
299
|
+
source_kind: str(row, "source_kind"),
|
|
300
|
+
posture: str(row, "posture"),
|
|
301
|
+
state: str(row, "state"),
|
|
302
|
+
image_width_px: num(row, "image_width_px"),
|
|
303
|
+
image_height_px: num(row, "image_height_px"),
|
|
304
|
+
grid_size_px: num(row, "grid_size_px", 1),
|
|
305
|
+
grid_distance_m: num(row, "grid_distance_m", 1),
|
|
306
|
+
local_width_cells: num(row, "local_width_cells"),
|
|
307
|
+
local_height_cells: num(row, "local_height_cells"),
|
|
308
|
+
image_origin_x: num(row, "image_origin_x"),
|
|
309
|
+
image_origin_y: num(row, "image_origin_y"),
|
|
310
|
+
rotation_deg: num(row, "rotation_deg"),
|
|
311
|
+
min_zoom: num(row, "min_zoom"),
|
|
312
|
+
max_zoom: num(row, "max_zoom"),
|
|
313
|
+
bundle_id: str(row, "bundle_id"),
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function toSubstrate(row: Record<string, unknown>): SubstrateDef {
|
|
318
|
+
return {
|
|
319
|
+
substrate_id: str(row, "substrate_id"),
|
|
320
|
+
coordinate_kind_id: str(row, "coordinate_kind_id"),
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function toMetadataAttachment(row: Record<string, unknown>): MetadataAttachmentRow {
|
|
325
|
+
return {
|
|
326
|
+
target_kind: str(row, "target_kind"),
|
|
327
|
+
target_ref: str(row, "target_ref"),
|
|
328
|
+
namespace: str(row, "namespace"),
|
|
329
|
+
payload_json: str(row, "payload_json"),
|
|
330
|
+
audience: str(row, "audience") || undefined,
|
|
331
|
+
bundle_id: str(row, "bundle_id") || undefined,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function toEdgeAffordance(row: Record<string, unknown>): EdgeAffordanceRow {
|
|
336
|
+
return {
|
|
337
|
+
edge_key: str(row, "edge_key") || str(row, "edgeKey"),
|
|
338
|
+
affordance_template_id: str(row, "affordance_template_id") || str(row, "affordanceTemplateId"),
|
|
339
|
+
mode_id: str(row, "mode_id") || str(row, "modeId"),
|
|
340
|
+
bindings_json: str(row, "bindings_json") || str(row, "bindingsJson") || undefined,
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function toEdgeState(row: Record<string, unknown>): EdgeStateRow {
|
|
345
|
+
const rawVal = row.value;
|
|
346
|
+
let val: boolean | string | number | Record<string, unknown> = false;
|
|
347
|
+
if (typeof rawVal === "boolean" || typeof rawVal === "string" || typeof rawVal === "number") {
|
|
348
|
+
val = rawVal;
|
|
349
|
+
} else if (isRecord(rawVal)) {
|
|
350
|
+
if ("bool" in rawVal) val = Boolean(rawVal.bool);
|
|
351
|
+
else if ("text" in rawVal) val = String(rawVal.text);
|
|
352
|
+
else if ("int" in rawVal) val = Number(rawVal.int);
|
|
353
|
+
else if ("tag" in rawVal && "value" in rawVal) {
|
|
354
|
+
if (rawVal.tag === "bool") val = Boolean(rawVal.value);
|
|
355
|
+
else if (rawVal.tag === "text") val = String(rawVal.value);
|
|
356
|
+
else if (rawVal.tag === "int") val = Number(rawVal.value);
|
|
357
|
+
else val = rawVal;
|
|
358
|
+
} else {
|
|
359
|
+
val = rawVal;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
const edgeId =
|
|
363
|
+
row.edge_id !== undefined
|
|
364
|
+
? String(row.edge_id)
|
|
365
|
+
: row.place_edge_id !== undefined
|
|
366
|
+
? String(row.place_edge_id)
|
|
367
|
+
: row.placeEdgeId !== undefined
|
|
368
|
+
? String(row.placeEdgeId)
|
|
369
|
+
: undefined;
|
|
370
|
+
return {
|
|
371
|
+
edge_id: edgeId,
|
|
372
|
+
edge_key: str(row, "edge_key") || str(row, "edgeKey") || undefined,
|
|
373
|
+
state_id: str(row, "state_id") || str(row, "stateId"),
|
|
374
|
+
value: val,
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function toSpatialNiche(row: Record<string, unknown>): SpatialNicheAttachmentRow {
|
|
379
|
+
return {
|
|
380
|
+
place_node_id: str(row, "place_node_id") || str(row, "placeNodeId") || str(row, "node_id"),
|
|
381
|
+
niche_id: str(row, "niche_id") || str(row, "nicheId"),
|
|
382
|
+
condition_bool: row.condition_bool !== undefined ? Boolean(row.condition_bool) : undefined,
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function toNicheDef(row: Record<string, unknown>): NicheDefinitionRow {
|
|
387
|
+
return {
|
|
388
|
+
niche_id: str(row, "niche_id") || str(row, "nicheId"),
|
|
389
|
+
display_name: str(row, "display_name") || str(row, "displayName"),
|
|
390
|
+
description: str(row, "description") || undefined,
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function toNodeState(row: Record<string, unknown>): NodeStateRow | null {
|
|
395
|
+
const rawVal = row.value;
|
|
396
|
+
let val: boolean | string | number | Record<string, unknown> = false;
|
|
397
|
+
if (typeof rawVal === "boolean" || typeof rawVal === "string" || typeof rawVal === "number") {
|
|
398
|
+
val = rawVal;
|
|
399
|
+
} else if (isRecord(rawVal)) {
|
|
400
|
+
if ("bool" in rawVal) val = Boolean(rawVal.bool);
|
|
401
|
+
else if ("text" in rawVal) val = String(rawVal.text);
|
|
402
|
+
else if ("int" in rawVal) val = Number(rawVal.int);
|
|
403
|
+
else if ("tag" in rawVal && "value" in rawVal) {
|
|
404
|
+
if (rawVal.tag === "bool") val = Boolean(rawVal.value);
|
|
405
|
+
else if (rawVal.tag === "text") val = String(rawVal.value);
|
|
406
|
+
else if (rawVal.tag === "int") val = Number(rawVal.value);
|
|
407
|
+
else val = rawVal;
|
|
408
|
+
} else {
|
|
409
|
+
val = rawVal;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
const nodeId =
|
|
413
|
+
str(row, "place_node_id") ||
|
|
414
|
+
str(row, "placeNodeId") ||
|
|
415
|
+
str(row, "node_id") ||
|
|
416
|
+
str(row, "nodeId") ||
|
|
417
|
+
(row.target_kind === "place_node" || row.targetKind === "place_node"
|
|
418
|
+
? str(row, "target_ref") || str(row, "targetRef")
|
|
419
|
+
: "");
|
|
420
|
+
if (!nodeId) return null;
|
|
421
|
+
return {
|
|
422
|
+
node_id: nodeId,
|
|
423
|
+
state_id: str(row, "state_id") || str(row, "stateId"),
|
|
424
|
+
value: val,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** Coerce a loose row bag into typed rows. */
|
|
429
|
+
export function normalizeWorldOsmInput(input: WorldOsmInput): NormalizedRows {
|
|
430
|
+
const graphs = (input.place_graphs ?? []).filter(isRecord).map(toGraph);
|
|
431
|
+
const nodes = (input.place_nodes ?? []).filter(isRecord).map(toNode);
|
|
432
|
+
const edges = (input.place_edges ?? []).filter(isRecord).map(toEdge);
|
|
433
|
+
const mapLayers = (input.map_layers ?? []).filter(isRecord).map(toMapLayer);
|
|
434
|
+
const substrates = (input.substrate_definitions ?? []).filter(isRecord).map(toSubstrate);
|
|
435
|
+
const metadataAttachments = (input.metadata_attachments ?? [])
|
|
436
|
+
.filter(isRecord)
|
|
437
|
+
.map(toMetadataAttachment);
|
|
438
|
+
const edgeAffordances = (input.place_edge_affordances ?? [])
|
|
439
|
+
.filter(isRecord)
|
|
440
|
+
.map(toEdgeAffordance);
|
|
441
|
+
const edgeStates = effectiveStateRows((input.edge_states ?? []).filter(isRecord)).map(
|
|
442
|
+
toEdgeState,
|
|
443
|
+
);
|
|
444
|
+
const niches = (input.spatial_niche_attachments ?? []).filter(isRecord).map(toSpatialNiche);
|
|
445
|
+
const nicheDefs = (input.niche_definitions ?? []).filter(isRecord).map(toNicheDef);
|
|
446
|
+
|
|
447
|
+
const rawNodeStates = effectiveStateRows(
|
|
448
|
+
[...(input.node_states ?? []), ...(input.spatial_environment_states ?? [])].filter(isRecord),
|
|
449
|
+
);
|
|
450
|
+
const nodeStates = rawNodeStates
|
|
451
|
+
.filter(isRecord)
|
|
452
|
+
.map(toNodeState)
|
|
453
|
+
.filter((s): s is NodeStateRow => s !== null);
|
|
454
|
+
|
|
455
|
+
let defaultShape = input.default_extent_shape_id ?? "";
|
|
456
|
+
let defaultParams = input.default_extent_params_json ?? "";
|
|
457
|
+
if ((!defaultShape || !defaultParams) && input.bundles?.length) {
|
|
458
|
+
const bundleRow = input.bundles.find(isRecord);
|
|
459
|
+
if (bundleRow) {
|
|
460
|
+
if (!defaultShape) defaultShape = str(bundleRow, "default_extent_shape_id");
|
|
461
|
+
if (!defaultParams) defaultParams = str(bundleRow, "default_extent_params_json");
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
return {
|
|
466
|
+
place_graphs: graphs,
|
|
467
|
+
place_nodes: nodes,
|
|
468
|
+
place_edges: edges,
|
|
469
|
+
map_layers: mapLayers,
|
|
470
|
+
substrate_definitions: substrates,
|
|
471
|
+
metadata_attachments: metadataAttachments,
|
|
472
|
+
place_edge_affordances: edgeAffordances,
|
|
473
|
+
edge_states: edgeStates,
|
|
474
|
+
spatial_niche_attachments: niches,
|
|
475
|
+
niche_definitions: nicheDefs,
|
|
476
|
+
node_states: nodeStates,
|
|
477
|
+
default_extent_shape_id: defaultShape,
|
|
478
|
+
default_extent_params_json: defaultParams,
|
|
479
|
+
};
|
|
480
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers for sk-pej D-2: composing an ephemeral session-grid graph
|
|
3
|
+
* over its parent location's topology (and, via `layout.graphs`, its
|
|
4
|
+
* backdrop). No SpacetimeDB/model/layout imports — kept leaf-level so the
|
|
5
|
+
* anchor-centring math is unit-testable in isolation from row parsing.
|
|
6
|
+
*/
|
|
7
|
+
import { projectLocalPosition } from "../coords.ts";
|
|
8
|
+
|
|
9
|
+
const SESSION_GRID_ID_PREFIX = "session_grid_";
|
|
10
|
+
|
|
11
|
+
export function isEphemeralInstanceGraph(row: Record<string, unknown>, graphId: string): boolean {
|
|
12
|
+
const explicit = row.is_ephemeral_instance;
|
|
13
|
+
if (typeof explicit === "boolean") return explicit;
|
|
14
|
+
return graphId.startsWith(SESSION_GRID_ID_PREFIX);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Walk `parent_graph_id` upward from `graphId`, returning `[graphId,
|
|
19
|
+
* parent, grandparent, ...]` (child-first). Stops at the first empty/
|
|
20
|
+
* unresolvable parent or a repeated id (cycle guard); `maxDepth` bounds a
|
|
21
|
+
* malformed chain. Only ONE hop (graphId's immediate parent) is meaningful
|
|
22
|
+
* for sk-pej D-2's node/topology composition today (single-level session
|
|
23
|
+
* grids), but backdrop-row resolution (tools.ts) wants the general walk
|
|
24
|
+
* so a later >1-deep nesting case does not need a second rewrite.
|
|
25
|
+
*/
|
|
26
|
+
export function resolveGraphAncestorChain(
|
|
27
|
+
graphRows: readonly Record<string, unknown>[],
|
|
28
|
+
graphId: string,
|
|
29
|
+
maxDepth = 8,
|
|
30
|
+
): string[] {
|
|
31
|
+
const byId = new Map<string, Record<string, unknown>>();
|
|
32
|
+
for (const row of graphRows) {
|
|
33
|
+
const id = row.place_graph_id;
|
|
34
|
+
if (typeof id === "string" && id.length > 0) byId.set(id, row);
|
|
35
|
+
}
|
|
36
|
+
const chain: string[] = [];
|
|
37
|
+
const seen = new Set<string>();
|
|
38
|
+
let current: string | undefined = graphId;
|
|
39
|
+
while (
|
|
40
|
+
current !== undefined &&
|
|
41
|
+
current.length > 0 &&
|
|
42
|
+
!seen.has(current) &&
|
|
43
|
+
chain.length < maxDepth
|
|
44
|
+
) {
|
|
45
|
+
chain.push(current);
|
|
46
|
+
seen.add(current);
|
|
47
|
+
const row = byId.get(current);
|
|
48
|
+
const parent =
|
|
49
|
+
row !== undefined && typeof row.parent_graph_id === "string" ? row.parent_graph_id : "";
|
|
50
|
+
current = parent.length > 0 ? parent : undefined;
|
|
51
|
+
}
|
|
52
|
+
return chain;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// This module performs NO unit conversion. It used to convert
|
|
56
|
+
// `map_layer.grid_distance_ft` from feet; sk-yze renamed that column to
|
|
57
|
+
// `grid_distance_m` and converted its stored values, so the whole spatial
|
|
58
|
+
// model is now metre-native end to end (`place_graph_definition
|
|
59
|
+
// .metres_per_unit`, the tactical grid's `cell_size_m`, `area_shape.rs`'s
|
|
60
|
+
// `radius_m`/`range_m`/`extent_m`). If you find yourself reaching for 0.3048
|
|
61
|
+
// here again, the value you are holding is in the wrong unit at its source —
|
|
62
|
+
// fix it there.
|
|
63
|
+
|
|
64
|
+
export interface Point2D {
|
|
65
|
+
x: number;
|
|
66
|
+
y: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Real-world metres represented by ONE unit of the PARENT graph's own
|
|
71
|
+
* local coordinate space — the single metres -> [parent-local-unit]
|
|
72
|
+
* conversion `composeChildLocalToParentFrame`'s caller (model.ts) needs to
|
|
73
|
+
* resolve before calling it.
|
|
74
|
+
*
|
|
75
|
+
* Prefers the parent's OWN authored `[[map_layers]]` backdrop row, when one
|
|
76
|
+
* exists: `grid_distance_m`, metres per backdrop grid square, used as-is.
|
|
77
|
+
* This is the CORRECT source, not
|
|
78
|
+
* `place_graph_definition.metres_per_unit`, whenever the parent's own node
|
|
79
|
+
* positions were authored against that backdrop's grid — which they
|
|
80
|
+
* typically are: `render-svg.ts`'s backdrop placement adds
|
|
81
|
+
* `SpatialGraphBackdrop.image_origin_cells` directly to node `x`/`y` with no
|
|
82
|
+
* scale factor in between, i.e. the renderer already treats "one graph-local
|
|
83
|
+
* unit" and "one map_layer-declared grid square" as the SAME unit. Checked
|
|
84
|
+
* against real content: elven-mini's `tomb`/`inn` place_graph_definition
|
|
85
|
+
* rows both carry `metres_per_unit = 1.0` (packages/spellkave-srd-cc/
|
|
86
|
+
* fixtures/elven-mini/{tomb,inn}.manifest.toml) — an unmaintained default —
|
|
87
|
+
* while their node `pos_x`/`pos_y` run up to 46/54 (tomb) and their OWN
|
|
88
|
+
* map_layer rows declare `local_width_cells = 54`/`grid_distance_m = 1.524`
|
|
89
|
+
* (adventure.manifest.toml:1016-1037) — i.e. positions are backdrop-cell
|
|
90
|
+
* indices (each 1.524 m, the 5 ft square), NOT `metres_per_unit`-many
|
|
91
|
+
* metres. Trusting `metres_per_unit` there would place the composed grid at
|
|
92
|
+
* roughly 1/1.524 of its correct size. That `metres_per_unit = 1.0` is
|
|
93
|
+
* itself a live gameplay defect, filed as sk-j1a — it also mis-scales
|
|
94
|
+
* indoor range and movement checks, not just this render path.
|
|
95
|
+
*
|
|
96
|
+
* Falls back to `fallbackMetresPerUnit` (the parent's own
|
|
97
|
+
* `place_graph_definition.metres_per_unit`, already documented
|
|
98
|
+
* metres-per-unit) only when the parent authors no backdrop at all —
|
|
99
|
+
* nothing better is knowable in that case, and `metres_per_unit` is at
|
|
100
|
+
* least the field the renderer's own scale-bar chrome
|
|
101
|
+
* (`sharedMetresPerGraphUnit`) already trusts.
|
|
102
|
+
*/
|
|
103
|
+
export function metresPerParentLocalUnit(
|
|
104
|
+
parentMapLayerRow: Record<string, unknown> | undefined,
|
|
105
|
+
fallbackMetresPerUnit: number,
|
|
106
|
+
): number {
|
|
107
|
+
const gridDistanceM = parentMapLayerRow?.grid_distance_m;
|
|
108
|
+
if (typeof gridDistanceM === "number" && Number.isFinite(gridDistanceM) && gridDistanceM > 0) {
|
|
109
|
+
return gridDistanceM;
|
|
110
|
+
}
|
|
111
|
+
return fallbackMetresPerUnit > 0 ? fallbackMetresPerUnit : 1;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Bounds-derived centre of a node set in its own local coordinate space
|
|
115
|
+
* (midpoint of min/max per axis) — the reference point D-2's ORIGIN
|
|
116
|
+
* CONVENTION (bead sk-pej, operator decision 2026-08-13) centres the grid
|
|
117
|
+
* on. Rejected alternatives (per the bead): cell (0,0) on the anchor
|
|
118
|
+
* (arbitrary growth direction), grid fills the anchor node's extent
|
|
119
|
+
* (blocked — nodes have no authored extent today, sk-71u). */
|
|
120
|
+
export function localBoundsCenter(nodes: readonly Point2D[]): Point2D {
|
|
121
|
+
if (nodes.length === 0) return { x: 0, y: 0 };
|
|
122
|
+
let minX = nodes[0]!.x;
|
|
123
|
+
let maxX = nodes[0]!.x;
|
|
124
|
+
let minY = nodes[0]!.y;
|
|
125
|
+
let maxY = nodes[0]!.y;
|
|
126
|
+
for (const n of nodes) {
|
|
127
|
+
minX = Math.min(minX, n.x);
|
|
128
|
+
maxX = Math.max(maxX, n.x);
|
|
129
|
+
minY = Math.min(minY, n.y);
|
|
130
|
+
maxY = Math.max(maxY, n.y);
|
|
131
|
+
}
|
|
132
|
+
return { x: (minX + maxX) / 2, y: (minY + maxY) / 2 };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface AnchorComposeParams {
|
|
136
|
+
/** Child graph-local units -> world METRES. For a session grid this is
|
|
137
|
+
* `place_graph_definition.metres_per_unit`, which the SERVER sets to
|
|
138
|
+
* `extent.cell_size` (r_session_open.rs:387) — and `cell_size` IS
|
|
139
|
+
* already metres: fixtures author it as `1.5240000486373901`, exactly
|
|
140
|
+
* `ft(5)` via the `feet * 0.3048` helper in
|
|
141
|
+
* `packages/spellkave-srd-cc/src/chapters/spells/spell-behaviors.ts`.
|
|
142
|
+
* Metres is the world unit throughout (cf. `area_shape.rs`'s `radius_m` /
|
|
143
|
+
* `range_m` / `extent_m` and `place_graph.rs:34-36`'s own doc comment on
|
|
144
|
+
* `metres_per_unit`); feet appear in exactly one place in the whole
|
|
145
|
+
* spatial model, `map_layer.grid_distance_m` — the ARTWORK's own grid
|
|
146
|
+
* convention, not the world's. */
|
|
147
|
+
childMetresPerUnit: number;
|
|
148
|
+
/** Real-world metres represented by ONE unit of the PARENT graph's own
|
|
149
|
+
* local coordinate space — the caller (model.ts) resolves this via
|
|
150
|
+
* `metresPerParentLocalUnit` (this module) BEFORE calling this function;
|
|
151
|
+
* see that function's doc for why it usually is NOT the parent's raw
|
|
152
|
+
* `place_graph_definition.metres_per_unit`. */
|
|
153
|
+
parentMetresPerUnit: number;
|
|
154
|
+
/** Clockwise degrees (ADR-0029 convention). Session grids hardcode
|
|
155
|
+
* 0.0 today (r_session_open.rs:386) — identity rotation — but this
|
|
156
|
+
* function does not special-case that away. */
|
|
157
|
+
bearingDeg: number;
|
|
158
|
+
/** Anchor node's RAW (pre-layout-offset) position in the parent's own
|
|
159
|
+
* local frame — the parent's own `place_node.pos_x/pos_y` for the node
|
|
160
|
+
* `place_graph_definition.anchor_node_id` points at. */
|
|
161
|
+
anchor: Point2D;
|
|
162
|
+
/** Child's own local-space centre (`localBoundsCenter` over its raw,
|
|
163
|
+
* untransformed nodes). */
|
|
164
|
+
center: Point2D;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function composeChildLocalToParentFrame(
|
|
168
|
+
localX: number,
|
|
169
|
+
localY: number,
|
|
170
|
+
params: AnchorComposeParams,
|
|
171
|
+
): Point2D {
|
|
172
|
+
const unitsPerLocal =
|
|
173
|
+
params.childMetresPerUnit > 0 && params.parentMetresPerUnit > 0
|
|
174
|
+
? params.childMetresPerUnit / params.parentMetresPerUnit
|
|
175
|
+
: 1;
|
|
176
|
+
const offset = projectLocalPosition(
|
|
177
|
+
{ bearing_deg: params.bearingDeg, scale: unitsPerLocal },
|
|
178
|
+
localX - params.center.x,
|
|
179
|
+
localY - params.center.y,
|
|
180
|
+
);
|
|
181
|
+
return {
|
|
182
|
+
x: params.anchor.x + offset.east_m,
|
|
183
|
+
y: params.anchor.y + offset.north_m,
|
|
184
|
+
};
|
|
185
|
+
}
|