@shipload/sdk 1.0.0-next.41 → 1.0.0-next.42

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.
@@ -169,9 +169,38 @@ function reconstruct(cameFrom: Map<string, Coord>, origin: Coord, dest: Coord):
169
169
  return {ok: true, waypoints, legs: waypoints.length, totalDistance}
170
170
  }
171
171
 
172
+ export interface ScanProvider {
173
+ getLocationType(seedHex: string, x: number, y: number): number
174
+ systemsInBox(
175
+ seedHex: string,
176
+ xMin: number,
177
+ yMin: number,
178
+ xMax: number,
179
+ yMax: number
180
+ ): {x: number; y: number; locType: number}[]
181
+ }
182
+
183
+ let scanProvider: ScanProvider | null = null
184
+ const graphCache = new Map<string, SystemGraph>()
185
+
186
+ // Inject a fast (e.g. wasm) location-type backend; null restores the pure-JS path. Clears the graph cache.
187
+ export function setScanProvider(provider: ScanProvider | null): void {
188
+ scanProvider = provider
189
+ graphCache.clear()
190
+ }
191
+
172
192
  export function sdkSystemGraph(seed: Checksum256Type): SystemGraph {
173
193
  const s = Checksum256.from(seed)
174
- // Travelable nodes mirror the contract's is_travelable: systems plus wormhole mouths.
194
+ const seedHex = s.toString()
195
+ const cached = graphCache.get(seedHex)
196
+ if (cached) return cached
197
+ const graph = scanProvider ? wasmSystemGraph(s, seedHex, scanProvider) : jsSystemGraph(s)
198
+ graphCache.set(seedHex, graph)
199
+ return graph
200
+ }
201
+
202
+ // Travelable nodes mirror the contract's is_travelable: systems plus wormhole mouths.
203
+ function jsSystemGraph(s: Checksum256): SystemGraph {
175
204
  return {
176
205
  hasSystem: (c) => hasSystem(s, {x: c.x, y: c.y}) || wormholeAt(s, c.x, c.y) !== null,
177
206
  nearby: (c, reachTiles) => {
@@ -194,3 +223,55 @@ export function sdkSystemGraph(seed: Checksum256Type): SystemGraph {
194
223
  },
195
224
  }
196
225
  }
226
+
227
+ const SCAN_BUCKET = 48
228
+
229
+ function wasmSystemGraph(s: Checksum256, seedHex: string, scan: ScanProvider): SystemGraph {
230
+ // Per-bucket system cache: reused across the overlapping node queries A* makes (and across routes).
231
+ const bucketCache = new Map<string, {x: number; y: number}[]>()
232
+ const bucketSystems = (bx: number, by: number): {x: number; y: number}[] => {
233
+ const k = `${bx},${by}`
234
+ let v = bucketCache.get(k)
235
+ if (v === undefined) {
236
+ const xMin = bx * SCAN_BUCKET
237
+ const yMin = by * SCAN_BUCKET
238
+ v = scan
239
+ .systemsInBox(seedHex, xMin, yMin, xMin + SCAN_BUCKET - 1, yMin + SCAN_BUCKET - 1)
240
+ .map((cell) => ({x: cell.x, y: cell.y}))
241
+ bucketCache.set(k, v)
242
+ }
243
+ return v
244
+ }
245
+ return {
246
+ hasSystem: (c) =>
247
+ scan.getLocationType(seedHex, c.x, c.y) !== 0 || wormholeAt(s, c.x, c.y) !== null,
248
+ nearby: (c, reachTiles) => {
249
+ const r = Math.floor(reachTiles)
250
+ const seen = new Set<string>([`${c.x},${c.y}`])
251
+ const out: Neighbor[] = []
252
+ const bx0 = Math.floor((c.x - r) / SCAN_BUCKET)
253
+ const bx1 = Math.floor((c.x + r) / SCAN_BUCKET)
254
+ const by0 = Math.floor((c.y - r) / SCAN_BUCKET)
255
+ const by1 = Math.floor((c.y + r) / SCAN_BUCKET)
256
+ for (let bx = bx0; bx <= bx1; bx++) {
257
+ for (let by = by0; by <= by1; by++) {
258
+ for (const cell of bucketSystems(bx, by)) {
259
+ const dist = Math.hypot(cell.x - c.x, cell.y - c.y)
260
+ if (dist > reachTiles) continue
261
+ const k = `${cell.x},${cell.y}`
262
+ if (seen.has(k)) continue
263
+ seen.add(k)
264
+ out.push({coord: {x: cell.x, y: cell.y}, dist})
265
+ }
266
+ }
267
+ }
268
+ for (const coord of nearbyWormholes(s, c.x, c.y, reachTiles)) {
269
+ const k = `${coord.x},${coord.y}`
270
+ if (seen.has(k)) continue
271
+ seen.add(k)
272
+ out.push({coord, dist: Math.hypot(coord.x - c.x, coord.y - c.y)})
273
+ }
274
+ return out
275
+ },
276
+ }
277
+ }