@shipload/sdk 1.0.0-next.6 → 1.0.0-next.61

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.
Files changed (156) hide show
  1. package/lib/scan.d.ts +52 -0
  2. package/lib/scan.js +162 -0
  3. package/lib/scan.js.map +1 -0
  4. package/lib/scan.m.js +152 -0
  5. package/lib/scan.m.js.map +1 -0
  6. package/lib/shipload.d.ts +3601 -1364
  7. package/lib/shipload.js +15671 -5261
  8. package/lib/shipload.js.map +1 -1
  9. package/lib/shipload.m.js +15392 -5209
  10. package/lib/shipload.m.js.map +1 -1
  11. package/lib/testing.d.ts +1173 -0
  12. package/lib/testing.js +4971 -0
  13. package/lib/testing.js.map +1 -0
  14. package/lib/testing.m.js +4965 -0
  15. package/lib/testing.m.js.map +1 -0
  16. package/package.json +20 -2
  17. package/src/capabilities/craftable.test.ts +82 -0
  18. package/src/capabilities/craftable.ts +82 -0
  19. package/src/capabilities/crafting.test.ts +35 -0
  20. package/src/capabilities/crafting.ts +34 -7
  21. package/src/capabilities/gathering.test.ts +29 -0
  22. package/src/capabilities/gathering.ts +37 -19
  23. package/src/capabilities/hauling.ts +0 -5
  24. package/src/capabilities/index.ts +0 -1
  25. package/src/capabilities/modules.ts +27 -30
  26. package/src/capabilities/movement.ts +1 -1
  27. package/src/capabilities/storage.ts +16 -1
  28. package/src/contracts/platform.ts +231 -3
  29. package/src/contracts/server.ts +1518 -501
  30. package/src/coordinates/address.ts +88 -0
  31. package/src/coordinates/constants.test.ts +15 -0
  32. package/src/coordinates/constants.ts +23 -0
  33. package/src/coordinates/index.ts +15 -0
  34. package/src/coordinates/memo.test.ts +47 -0
  35. package/src/coordinates/memo.ts +20 -0
  36. package/src/coordinates/permutation.ts +77 -0
  37. package/src/coordinates/regions.ts +48 -0
  38. package/src/coordinates/sectors.ts +115 -0
  39. package/src/data/capabilities.ts +49 -18
  40. package/src/data/capability-formulas.ts +26 -14
  41. package/src/data/catalog.ts +7 -8
  42. package/src/data/colors.ts +14 -48
  43. package/src/data/entities.json +380 -15
  44. package/src/data/item-ids.ts +51 -13
  45. package/src/data/items.json +318 -77
  46. package/src/data/kind-registry.json +195 -0
  47. package/src/data/kind-registry.ts +182 -0
  48. package/src/data/metadata.ts +291 -51
  49. package/src/data/recipes-runtime.ts +6 -23
  50. package/src/data/recipes.json +2260 -173
  51. package/src/data/tiers.ts +9 -6
  52. package/src/derivation/build-methods.test.ts +13 -0
  53. package/src/derivation/build-methods.ts +48 -0
  54. package/src/derivation/capabilities.test.ts +348 -0
  55. package/src/derivation/capabilities.ts +693 -0
  56. package/src/derivation/capability-mappings.ts +60 -16
  57. package/src/derivation/crafting.test.ts +17 -0
  58. package/src/derivation/crafting.ts +57 -34
  59. package/src/derivation/index.ts +27 -2
  60. package/src/derivation/recipe-usage.test.ts +90 -0
  61. package/src/derivation/recipe-usage.ts +141 -0
  62. package/src/derivation/reserve-regen.ts +34 -0
  63. package/src/derivation/resources.ts +9 -1
  64. package/src/derivation/rollups.test.ts +55 -0
  65. package/src/derivation/rollups.ts +72 -0
  66. package/src/derivation/stars.test.ts +67 -0
  67. package/src/derivation/stars.ts +25 -0
  68. package/src/derivation/stat-scaling.ts +12 -0
  69. package/src/derivation/stats.ts +6 -6
  70. package/src/derivation/stratum.ts +26 -22
  71. package/src/derivation/tiers.ts +40 -7
  72. package/src/derivation/upgrades.ts +14 -0
  73. package/src/derivation/wormhole.ts +154 -0
  74. package/src/entities/entity.ts +102 -0
  75. package/src/entities/gamestate.ts +3 -28
  76. package/src/entities/makers.ts +141 -135
  77. package/src/entities/slot-multiplier.ts +43 -0
  78. package/src/errors.ts +12 -16
  79. package/src/format.ts +26 -4
  80. package/src/index-module.ts +333 -35
  81. package/src/managers/actions.ts +668 -103
  82. package/src/managers/base.ts +6 -2
  83. package/src/managers/cluster.test.ts +39 -0
  84. package/src/managers/cluster.ts +53 -0
  85. package/src/managers/construction-types.ts +80 -0
  86. package/src/managers/construction.ts +443 -0
  87. package/src/managers/context.ts +29 -1
  88. package/src/managers/coordinates.ts +14 -0
  89. package/src/managers/entities.ts +27 -66
  90. package/src/managers/epochs.ts +40 -0
  91. package/src/managers/index.ts +20 -1
  92. package/src/managers/locations.ts +25 -29
  93. package/src/managers/nft.test.ts +14 -0
  94. package/src/managers/nft.ts +70 -0
  95. package/src/managers/players.ts +25 -0
  96. package/src/managers/plot.ts +122 -0
  97. package/src/nft/atomicassets.abi.json +1342 -0
  98. package/src/nft/atomicassets.ts +237 -0
  99. package/src/nft/atomicdata.ts +130 -0
  100. package/src/nft/buildImmutableData.ts +388 -0
  101. package/src/nft/description.ts +215 -99
  102. package/src/nft/index.ts +3 -0
  103. package/src/planner/index.ts +242 -0
  104. package/src/planner/planner.test.ts +334 -0
  105. package/src/resolution/describe-module.ts +43 -25
  106. package/src/resolution/display-name.ts +38 -10
  107. package/src/resolution/resolve-item.test.ts +48 -0
  108. package/src/resolution/resolve-item.ts +112 -58
  109. package/src/scan/index.ts +244 -0
  110. package/src/scan/scan-wasm.base64.ts +2 -0
  111. package/src/scheduling/accessor.ts +65 -23
  112. package/src/scheduling/availability.test.ts +204 -0
  113. package/src/scheduling/availability.ts +211 -0
  114. package/src/scheduling/cancel.test.ts +542 -0
  115. package/src/scheduling/cancel.ts +254 -0
  116. package/src/scheduling/cluster-stock.test.ts +31 -0
  117. package/src/scheduling/cluster-stock.ts +15 -0
  118. package/src/scheduling/energy.ts +47 -0
  119. package/src/scheduling/idle-resolve.ts +45 -0
  120. package/src/scheduling/jobs.ts +71 -0
  121. package/src/scheduling/lane-core.ts +128 -0
  122. package/src/scheduling/lanes.test.ts +250 -0
  123. package/src/scheduling/lanes.ts +227 -0
  124. package/src/scheduling/projection.ts +257 -133
  125. package/src/scheduling/schedule.test.ts +119 -0
  126. package/src/scheduling/schedule.ts +257 -117
  127. package/src/scheduling/task-cargo.test.ts +44 -0
  128. package/src/scheduling/task-cargo.ts +46 -0
  129. package/src/scheduling/unwrap.test.ts +86 -0
  130. package/src/scheduling/unwrap.ts +190 -0
  131. package/src/shipload.ts +26 -1
  132. package/src/subscriptions/manager.cluster.test.ts +51 -0
  133. package/src/subscriptions/manager.ts +245 -172
  134. package/src/subscriptions/mappers.ts +5 -8
  135. package/src/subscriptions/types.ts +28 -3
  136. package/src/testing/catalog-hash.ts +19 -0
  137. package/src/testing/index.ts +2 -0
  138. package/src/testing/projection-parity.ts +167 -0
  139. package/src/travel/reach.ts +21 -0
  140. package/src/travel/route-planner.ts +288 -0
  141. package/src/travel/route-simulator.ts +170 -0
  142. package/src/travel/travel.ts +188 -122
  143. package/src/types/capabilities.ts +37 -8
  144. package/src/types/entity.ts +3 -3
  145. package/src/types/index.ts +0 -1
  146. package/src/types.ts +51 -16
  147. package/src/utils/cargo.test.ts +14 -0
  148. package/src/utils/cargo.ts +29 -0
  149. package/src/utils/display-name.ts +70 -0
  150. package/src/utils/system.ts +15 -16
  151. package/src/capabilities/loading.ts +0 -8
  152. package/src/entities/container.ts +0 -123
  153. package/src/entities/ship-deploy.ts +0 -295
  154. package/src/entities/ship.ts +0 -221
  155. package/src/entities/warehouse.ts +0 -127
  156. package/src/types/entity-traits.ts +0 -69
@@ -0,0 +1,211 @@
1
+ import type {ServerContract} from '../contracts'
2
+ import {HoldKind, TaskType} from '../types'
3
+ import * as schedule from './schedule'
4
+
5
+ type Task = ServerContract.Types.task
6
+ type Coupling = ServerContract.Types.coupling
7
+ type CargoItem = ServerContract.Types.cargo_item
8
+ type ModuleEntry = ServerContract.Types.module_entry
9
+
10
+ export interface CargoEffect {
11
+ added: CargoItem[]
12
+ removed: CargoItem[]
13
+ }
14
+
15
+ export interface AvailabilityInput extends schedule.ScheduleData {
16
+ cargo: CargoItem[]
17
+ }
18
+
19
+ export interface CargoInput {
20
+ itemId: number
21
+ stats: bigint
22
+ modules?: ModuleEntry[]
23
+ quantity: number
24
+ }
25
+
26
+ export interface IncomingSource {
27
+ holdId: string
28
+ until: Date
29
+ items: CargoItem[]
30
+ }
31
+
32
+ const INCOMING_COUPLING_KINDS = new Set<number>([HoldKind.PUSH, HoldKind.GATHER, HoldKind.FLIGHT])
33
+
34
+ // Mirrors is_incoming_hold_kind: PUSH/GATHER/FLIGHT couplings deliver to the counterpart.
35
+ export function isIncomingCouplingKind(kind: number): boolean {
36
+ return INCOMING_COUPLING_KINDS.has(kind)
37
+ }
38
+
39
+ // Mirrors has_source_hold: a HOLD_SOURCE coupling marks an escrowed clustercraft.
40
+ export function hasSourceCoupling(task: Task): boolean {
41
+ return task.couplings.some((c) => c.kind.toNumber() === HoldKind.SOURCE)
42
+ }
43
+
44
+ // Mirrors has_incoming_coupling: any PUSH/GATHER/FLIGHT coupling delivers output to a counterpart.
45
+ export function hasIncomingCoupling(task: Task): boolean {
46
+ return task.couplings.some((c) => isIncomingCouplingKind(c.kind.toNumber()))
47
+ }
48
+
49
+ // A CRAFT task's output ownership: clustered escrows inputs off other members; own-output lands on the crafter.
50
+ export function craftCargoOwnership(task: Task): {clustered: boolean; ownOutput: boolean} {
51
+ const clustered = hasSourceCoupling(task)
52
+ return {
53
+ clustered,
54
+ ownOutput: clustered ? !hasIncomingCoupling(task) : task.couplings.length === 0,
55
+ }
56
+ }
57
+
58
+ // Mirrors calc_counterpart_delivery: incoming-kind couplings only; a coupled CRAFT yields its output slot.
59
+ export function calcCounterpartDelivery(task: Task, coupling: Coupling): CargoItem[] {
60
+ if (!isIncomingCouplingKind(coupling.kind.toNumber())) return []
61
+ if (task.type.toNumber() === TaskType.CRAFT) {
62
+ return task.cargo.length === 0 ? [] : [task.cargo[task.cargo.length - 1]]
63
+ }
64
+ return task.cargo
65
+ }
66
+
67
+ export function taskCargoEffect(task: Task): CargoEffect {
68
+ switch (task.type.toNumber()) {
69
+ case TaskType.LOAD:
70
+ case TaskType.UNWRAP:
71
+ case TaskType.UNDEPLOY:
72
+ return {added: task.cargo, removed: []}
73
+ case TaskType.UNLOAD:
74
+ case TaskType.UPGRADE:
75
+ return {added: [], removed: task.cargo}
76
+ case TaskType.GATHER:
77
+ return task.couplings.length > 0
78
+ ? {added: [], removed: []}
79
+ : {added: task.cargo, removed: []}
80
+ case TaskType.CRAFT: {
81
+ if (task.cargo.length === 0) return {added: [], removed: []}
82
+ const {clustered, ownOutput} = craftCargoOwnership(task)
83
+ return {
84
+ added: ownOutput ? [task.cargo[task.cargo.length - 1]] : [],
85
+ removed: clustered ? [] : task.cargo.slice(0, -1),
86
+ }
87
+ }
88
+ default:
89
+ return {added: [], removed: []}
90
+ }
91
+ }
92
+
93
+ function refKey(itemId: number, stats: bigint, modules: ModuleEntry[], entityId: string): string {
94
+ const base = `${itemId}:${stats.toString()}`
95
+ const normalizedEntityId = entityId !== '0' ? entityId : ''
96
+ if (modules.length === 0 && normalizedEntityId === '') return base
97
+ return `${base}:modules=${JSON.stringify(modules)}:entity=${normalizedEntityId}`
98
+ }
99
+
100
+ export function cargoKey(item: CargoItem): string {
101
+ return refKey(
102
+ item.item_id.toNumber(),
103
+ BigInt(item.stats.toString()),
104
+ item.modules ?? [],
105
+ item.entity_id?.toString() ?? ''
106
+ )
107
+ }
108
+
109
+ export function cargoInputKey(input: CargoInput): string {
110
+ return refKey(input.itemId, input.stats, input.modules ?? [], '')
111
+ }
112
+
113
+ function cargoQuantity(item: CargoItem): bigint {
114
+ return BigInt(item.quantity.toString())
115
+ }
116
+
117
+ export function projectedCargoAvailableAt(
118
+ entity: AvailabilityInput,
119
+ at: Date,
120
+ incoming: readonly IncomingSource[] = []
121
+ ): Map<string, bigint> {
122
+ const avail = new Map<string, bigint>()
123
+
124
+ for (const item of entity.cargo) {
125
+ const key = cargoKey(item)
126
+ avail.set(key, (avail.get(key) ?? 0n) + cargoQuantity(item))
127
+ }
128
+
129
+ // Every scheduled task reserves inputs against the unsettled cargo base, even already-elapsed ones.
130
+ const tasks = schedule.orderedTasks(entity)
131
+
132
+ for (const ordered of tasks) {
133
+ if (ordered.completesAt.getTime() >= at.getTime()) continue
134
+
135
+ for (const item of taskCargoEffect(ordered.task).added) {
136
+ const key = cargoKey(item)
137
+ avail.set(key, (avail.get(key) ?? 0n) + cargoQuantity(item))
138
+ }
139
+ }
140
+
141
+ for (const src of incoming) {
142
+ if (src.until.getTime() >= at.getTime()) continue
143
+ for (const item of src.items) {
144
+ const key = cargoKey(item)
145
+ avail.set(key, (avail.get(key) ?? 0n) + cargoQuantity(item))
146
+ }
147
+ }
148
+
149
+ for (const ordered of tasks) {
150
+ for (const item of taskCargoEffect(ordered.task).removed) {
151
+ const key = cargoKey(item)
152
+ const current = avail.get(key) ?? 0n
153
+ const quantity = cargoQuantity(item)
154
+ avail.set(key, current > quantity ? current - quantity : 0n)
155
+ }
156
+ }
157
+
158
+ return avail
159
+ }
160
+
161
+ // Earliest-sufficient candidate (epoch, own-lane producer completions, incoming untils); else the latest candidate.
162
+ export function cargoReadyAt(
163
+ entity: AvailabilityInput,
164
+ inputs: readonly CargoInput[],
165
+ incoming: readonly IncomingSource[] = []
166
+ ): Date {
167
+ const demand = new Map<string, bigint>()
168
+ for (const input of inputs) {
169
+ const key = cargoInputKey(input)
170
+ demand.set(key, (demand.get(key) ?? 0n) + BigInt(input.quantity))
171
+ }
172
+
173
+ const candidates: number[] = [0]
174
+ for (const ordered of schedule.orderedTasks(entity)) {
175
+ for (const item of taskCargoEffect(ordered.task).added) {
176
+ if (demand.has(cargoKey(item))) {
177
+ candidates.push(ordered.completesAt.getTime())
178
+ break
179
+ }
180
+ }
181
+ }
182
+ for (const src of incoming) {
183
+ if (src.items.some((item) => demand.has(cargoKey(item)))) {
184
+ candidates.push(src.until.getTime())
185
+ }
186
+ }
187
+ candidates.sort((a, b) => a - b)
188
+
189
+ for (const candidateMs of candidates) {
190
+ // +1ms mirrors the contract's candidate+1µs probe: inclusive at the candidate instant.
191
+ const available = projectedCargoAvailableAt(entity, new Date(candidateMs + 1), incoming)
192
+ let sufficient = true
193
+ for (const [key, quantity] of demand) {
194
+ if ((available.get(key) ?? 0n) < quantity) {
195
+ sufficient = false
196
+ break
197
+ }
198
+ }
199
+ if (sufficient) return new Date(candidateMs)
200
+ }
201
+ return new Date(candidates[candidates.length - 1])
202
+ }
203
+
204
+ export function availableForItem(avail: Map<string, bigint>, itemId: number): bigint {
205
+ const prefix = `${itemId}:`
206
+ let total = 0n
207
+ for (const [key, quantity] of avail) {
208
+ if (key.startsWith(prefix)) total += quantity
209
+ }
210
+ return total
211
+ }