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

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shipload/sdk",
3
3
  "description": "SDKs for Shipload",
4
- "version": "1.0.0-next.40",
4
+ "version": "1.0.0-next.41",
5
5
  "homepage": "https://github.com/shipload/toolkit/tree/master/packages/sdk",
6
6
  "repository": {
7
7
  "type": "git",
@@ -24,6 +24,11 @@
24
24
  "import": "./lib/testing.m.js",
25
25
  "require": "./lib/testing.js"
26
26
  },
27
+ "./scan": {
28
+ "types": "./lib/scan.d.ts",
29
+ "import": "./lib/scan.m.js",
30
+ "require": "./lib/scan.js"
31
+ },
27
32
  "./package.json": "./package.json"
28
33
  },
29
34
  "files": [
@@ -15,8 +15,8 @@ export const DEPTH_THRESHOLD_T10 = 63000
15
15
  export const LOCATION_MIN_DEPTH = 500
16
16
  export const LOCATION_MAX_DEPTH = 65535
17
17
 
18
- export const YIELD_FRACTION_SHALLOW = 0.005
19
- export const YIELD_FRACTION_DEEP = 0.001
18
+ export const YIELD_FRACTION_SHALLOW = 0.002
19
+ export const YIELD_FRACTION_DEEP = 0.0004
20
20
 
21
21
  export function yieldThresholdAt(stratum: number): number {
22
22
  const clamped = stratum > 65535 ? 65535 : stratum
@@ -0,0 +1,180 @@
1
+ import {SCAN_WASM_B64} from './scan-wasm.base64'
2
+
3
+ const stubImports: WebAssembly.Imports = new Proxy(
4
+ {},
5
+ {
6
+ get: () => new Proxy({}, {get: () => () => 0}),
7
+ }
8
+ ) as any
9
+
10
+ let inst: WebAssembly.Instance | null = null
11
+ let readyPromise: Promise<void> | null = null
12
+
13
+ function bytes(): Uint8Array {
14
+ return Uint8Array.from(atob(SCAN_WASM_B64), (c) => c.charCodeAt(0))
15
+ }
16
+
17
+ function finish(i: WebAssembly.Instance) {
18
+ const ex = i.exports as any
19
+ if (typeof ex._initialize === 'function') ex._initialize()
20
+ inst = i
21
+ }
22
+
23
+ export function scanReady(): Promise<void> {
24
+ if (inst) return Promise.resolve()
25
+ if (!readyPromise)
26
+ readyPromise = (
27
+ WebAssembly.instantiate(bytes().buffer as ArrayBuffer, stubImports) as Promise<{
28
+ instance: WebAssembly.Instance
29
+ }>
30
+ ).then((r) => finish(r.instance))
31
+ return readyPromise
32
+ }
33
+
34
+ function ex(): any {
35
+ if (!inst)
36
+ finish(
37
+ new WebAssembly.Instance(
38
+ new WebAssembly.Module(bytes().buffer as ArrayBuffer),
39
+ stubImports
40
+ )
41
+ )
42
+ return inst!.exports
43
+ }
44
+
45
+ const hex = (h: string) => Uint8Array.from(h.match(/../g)!.map((b) => parseInt(b, 16)))
46
+
47
+ export function getLocationType(gameSeed: string, x: number, y: number): number {
48
+ const e = ex()
49
+ const mem = e.memory as WebAssembly.Memory
50
+ const g = e.malloc(32)
51
+ new Uint8Array(mem.buffer, g, 32).set(hex(gameSeed))
52
+ const t = e.get_location_type(g, BigInt(x), BigInt(y))
53
+ e.free(g)
54
+ return t
55
+ }
56
+
57
+ export interface SystemCell {
58
+ x: number
59
+ y: number
60
+ locType: number
61
+ }
62
+
63
+ export interface Coord {
64
+ x: number
65
+ y: number
66
+ }
67
+
68
+ export interface Deposit {
69
+ x: number
70
+ y: number
71
+ depth: number
72
+ itemId: number
73
+ richness: number
74
+ reserve: number
75
+ stats: [number, number, number]
76
+ }
77
+
78
+ export interface DerivedCell {
79
+ location: {x: number; y: number; locType: number; subtype: number; size: number}
80
+ deposits: Deposit[]
81
+ }
82
+
83
+ export function systemsInBox(
84
+ gameSeed: string,
85
+ xMin: number,
86
+ yMin: number,
87
+ xMax: number,
88
+ yMax: number
89
+ ): SystemCell[] {
90
+ const e = ex()
91
+ const mem = e.memory as WebAssembly.Memory
92
+ const g = e.malloc(32)
93
+ new Uint8Array(mem.buffer, g, 32).set(hex(gameSeed))
94
+ let cap = 256
95
+ let out = e.malloc(cap * 12)
96
+ let n = e.systems_in_box(g, xMin, yMin, xMax, yMax, out, cap)
97
+ if (n < 0) {
98
+ e.free(out)
99
+ cap = -n
100
+ out = e.malloc(cap * 12)
101
+ n = e.systems_in_box(g, xMin, yMin, xMax, yMax, out, cap)
102
+ }
103
+ const res: SystemCell[] = []
104
+ const dv = new DataView(mem.buffer.slice(out, out + n * 12))
105
+ for (let i = 0; i < n; i++) {
106
+ const o = i * 12
107
+ res.push({
108
+ x: dv.getInt32(o, true),
109
+ y: dv.getInt32(o + 4, true),
110
+ locType: dv.getUint32(o + 8, true),
111
+ })
112
+ }
113
+ e.free(g)
114
+ e.free(out)
115
+ return res
116
+ }
117
+
118
+ export async function scanCells(
119
+ gameSeed: string,
120
+ epochSeed: string,
121
+ cells: Coord[]
122
+ ): Promise<DerivedCell[]> {
123
+ await scanReady()
124
+ const e = ex()
125
+ const mem = e.memory as WebAssembly.Memory
126
+ const write = (b: Uint8Array) => {
127
+ const p = e.malloc(b.length)
128
+ new Uint8Array(mem.buffer, p, b.length).set(b)
129
+ return p
130
+ }
131
+ const gp = write(hex(gameSeed))
132
+ const ep = write(hex(epochSeed))
133
+ const cellArr = new Int32Array(cells.length * 2)
134
+ cells.forEach((c, i) => {
135
+ cellArr[i * 2] = c.x
136
+ cellArr[i * 2 + 1] = c.y
137
+ })
138
+ const cp = write(new Uint8Array(cellArr.buffer))
139
+ const locOut = e.malloc(cells.length * 8)
140
+ let cap = Math.max(64, cells.length * 8)
141
+ let depOut = e.malloc(cap * 40)
142
+ let n = e.scan_cells(gp, ep, cp, cells.length, locOut, depOut, cap)
143
+ if (n < 0) {
144
+ e.free(depOut)
145
+ cap = -n
146
+ depOut = e.malloc(cap * 40)
147
+ n = e.scan_cells(gp, ep, cp, cells.length, locOut, depOut, cap)
148
+ }
149
+ const locView = new DataView(mem.buffer.slice(locOut, locOut + cells.length * 8))
150
+ const depView = new DataView(mem.buffer.slice(depOut, depOut + n * 40))
151
+ for (const p of [gp, ep, cp, locOut, depOut]) e.free(p)
152
+ const out: DerivedCell[] = cells.map((c, i) => ({
153
+ location: {
154
+ x: c.x,
155
+ y: c.y,
156
+ locType: locView.getUint8(i * 8),
157
+ subtype: locView.getUint8(i * 8 + 1),
158
+ size: locView.getUint32(i * 8 + 4, true),
159
+ },
160
+ deposits: [],
161
+ }))
162
+ for (let i = 0; i < n; i++) {
163
+ const o = i * 40
164
+ const ci = depView.getUint32(o, true)
165
+ out[ci].deposits.push({
166
+ x: cells[ci].x,
167
+ y: cells[ci].y,
168
+ depth: depView.getUint32(o + 4, true),
169
+ itemId: depView.getUint32(o + 8, true),
170
+ richness: depView.getUint32(o + 12, true),
171
+ reserve: depView.getFloat64(o + 32, true),
172
+ stats: [
173
+ depView.getUint32(o + 16, true),
174
+ depView.getUint32(o + 20, true),
175
+ depView.getUint32(o + 24, true),
176
+ ],
177
+ })
178
+ }
179
+ return out
180
+ }