@shipload/sdk 1.0.0-next.34 → 1.0.0-next.36
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/lib/shipload.d.ts +398 -51
- package/lib/shipload.js +1481 -400
- package/lib/shipload.js.map +1 -1
- package/lib/shipload.m.js +1442 -401
- package/lib/shipload.m.js.map +1 -1
- package/lib/testing.d.ts +101 -20
- package/lib/testing.js +201 -57
- package/lib/testing.js.map +1 -1
- package/lib/testing.m.js +201 -57
- package/lib/testing.m.js.map +1 -1
- package/package.json +1 -1
- package/src/capabilities/crafting.ts +2 -3
- package/src/capabilities/gathering.test.ts +16 -0
- package/src/capabilities/gathering.ts +8 -11
- package/src/contracts/server.ts +147 -29
- package/src/coordinates/address.ts +88 -0
- package/src/coordinates/constants.test.ts +15 -0
- package/src/coordinates/constants.ts +23 -0
- package/src/coordinates/index.ts +15 -0
- package/src/coordinates/memo.test.ts +47 -0
- package/src/coordinates/memo.ts +20 -0
- package/src/coordinates/permutation.ts +77 -0
- package/src/coordinates/regions.ts +48 -0
- package/src/coordinates/sectors.ts +115 -0
- package/src/data/capability-formulas.ts +0 -1
- package/src/data/entities.json +4 -4
- package/src/data/items.json +5 -5
- package/src/data/recipes.json +39 -65
- package/src/derivation/capabilities.test.ts +133 -0
- package/src/derivation/capabilities.ts +66 -14
- package/src/derivation/rollups.test.ts +55 -0
- package/src/derivation/rollups.ts +56 -0
- package/src/derivation/wormhole.ts +115 -0
- package/src/entities/makers.ts +30 -3
- package/src/errors.ts +2 -0
- package/src/index-module.ts +38 -2
- package/src/managers/actions.ts +79 -5
- package/src/managers/construction.ts +6 -4
- package/src/managers/context.ts +9 -0
- package/src/managers/coordinates.ts +14 -0
- package/src/managers/plot.ts +2 -4
- package/src/nft/description.ts +25 -6
- package/src/planner/index.ts +127 -0
- package/src/planner/planner.test.ts +319 -0
- package/src/resolution/resolve-item.ts +4 -1
- package/src/scheduling/availability.ts +1 -1
- package/src/scheduling/cancel.test.ts +348 -0
- package/src/scheduling/cancel.ts +209 -0
- package/src/scheduling/lanes.test.ts +249 -0
- package/src/scheduling/lanes.ts +140 -2
- package/src/scheduling/projection.ts +75 -16
- package/src/scheduling/schedule.ts +3 -1
- package/src/shipload.ts +5 -0
- package/src/testing/projection-parity.ts +26 -2
- package/src/travel/travel.ts +116 -105
- package/src/types/capabilities.ts +23 -6
- package/src/types/entity.ts +3 -3
- package/src/types.ts +2 -1
- package/src/utils/system.ts +11 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import type {ServerContract} from '../contracts'
|
|
2
|
+
import {TaskCancelable, TaskType} from '../types'
|
|
3
|
+
import {calcCargoItemMass} from '../capabilities/storage'
|
|
4
|
+
import {taskCargoEffect, cargoKey} from './availability'
|
|
5
|
+
import * as schedule from './schedule'
|
|
6
|
+
import {validateSchedule, type Projectable} from './projection'
|
|
7
|
+
|
|
8
|
+
export enum CancelBlockReason {
|
|
9
|
+
TASK_NEVER = 'TASK_NEVER',
|
|
10
|
+
BEFORE_START_RUNNING = 'BEFORE_START_RUNNING',
|
|
11
|
+
DONE = 'DONE',
|
|
12
|
+
CONTAINS_LINKED_TASK = 'CONTAINS_LINKED_TASK',
|
|
13
|
+
WOULD_STRAND = 'WOULD_STRAND',
|
|
14
|
+
WOULD_OVERFILL = 'WOULD_OVERFILL',
|
|
15
|
+
NOT_OWNER = 'NOT_OWNER',
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type Task = InstanceType<typeof ServerContract.Types.task>
|
|
19
|
+
type EntityInfo = InstanceType<typeof ServerContract.Types.entity_info>
|
|
20
|
+
type EntityRef = InstanceType<typeof ServerContract.Types.entity_ref>
|
|
21
|
+
type CargoItem = InstanceType<typeof ServerContract.Types.cargo_item>
|
|
22
|
+
|
|
23
|
+
export interface CancelRefund {
|
|
24
|
+
giver: EntityRef
|
|
25
|
+
cargo: CargoItem[]
|
|
26
|
+
}
|
|
27
|
+
export interface CancelReleasedHold {
|
|
28
|
+
counterpart: EntityRef
|
|
29
|
+
kind: number
|
|
30
|
+
}
|
|
31
|
+
export interface CancelEffects {
|
|
32
|
+
refunds: CancelRefund[]
|
|
33
|
+
releasedHolds: CancelReleasedHold[]
|
|
34
|
+
abandonsRunning: boolean
|
|
35
|
+
keepsPlotDeposits?: {plot: EntityRef}
|
|
36
|
+
energyForfeited?: number
|
|
37
|
+
}
|
|
38
|
+
export interface CancelPlan {
|
|
39
|
+
ok: boolean
|
|
40
|
+
blockedReason?: CancelBlockReason
|
|
41
|
+
range: {count: number; taskIndices: number[]}
|
|
42
|
+
effects: CancelEffects
|
|
43
|
+
}
|
|
44
|
+
export interface CancelEligibilityInput {
|
|
45
|
+
now: Date
|
|
46
|
+
counterparts?: Map<string, EntityInfo>
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const EMPTY_EFFECTS: CancelEffects = {refunds: [], releasedHolds: [], abandonsRunning: false}
|
|
50
|
+
|
|
51
|
+
function postCancelEntity(entity: EntityInfo, laneKey: number, fromTaskIndex: number): EntityInfo {
|
|
52
|
+
const clone = (entity.constructor as typeof ServerContract.Types.entity_info).from(
|
|
53
|
+
JSON.parse(JSON.stringify(entity.toJSON()))
|
|
54
|
+
) as EntityInfo
|
|
55
|
+
const lane = clone.lanes.find((l) => l.lane_key.toNumber() === laneKey)!
|
|
56
|
+
lane.schedule.tasks = lane.schedule.tasks.slice(0, fromTaskIndex)
|
|
57
|
+
return clone
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function feasibleAfterCancel(post: EntityInfo): boolean {
|
|
61
|
+
const ordered = schedule.orderedTasks(post as unknown as Projectable)
|
|
62
|
+
const base = new Map<string, number>()
|
|
63
|
+
for (const c of post.cargo ?? []) {
|
|
64
|
+
const k = cargoKey(c)
|
|
65
|
+
base.set(k, (base.get(k) ?? 0) + c.quantity.toNumber())
|
|
66
|
+
}
|
|
67
|
+
const isConsumer = (t: Task) =>
|
|
68
|
+
t.type.toNumber() === TaskType.CRAFT || t.type.toNumber() === TaskType.UNLOAD
|
|
69
|
+
for (const self of ordered) {
|
|
70
|
+
if (!isConsumer(self.task)) continue
|
|
71
|
+
const map = new Map(base)
|
|
72
|
+
for (const other of ordered) {
|
|
73
|
+
if (other.completesAt.getTime() >= self.completesAt.getTime()) continue
|
|
74
|
+
for (const out of taskCargoEffect(other.task).added) {
|
|
75
|
+
map.set(cargoKey(out), (map.get(cargoKey(out)) ?? 0) + out.quantity.toNumber())
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
for (const other of ordered) {
|
|
79
|
+
if (other === self) continue
|
|
80
|
+
for (const inp of taskCargoEffect(other.task).removed) {
|
|
81
|
+
const cur = map.get(cargoKey(inp)) ?? 0
|
|
82
|
+
map.set(cargoKey(inp), Math.max(0, cur - inp.quantity.toNumber()))
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
for (const inp of taskCargoEffect(self.task).removed) {
|
|
86
|
+
if ((map.get(cargoKey(inp)) ?? 0) < inp.quantity.toNumber()) return false
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
validateSchedule(post as unknown as Projectable)
|
|
91
|
+
} catch {
|
|
92
|
+
return false
|
|
93
|
+
}
|
|
94
|
+
return true
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface Timing {
|
|
98
|
+
startsAt: number
|
|
99
|
+
completesAt: number
|
|
100
|
+
running: boolean
|
|
101
|
+
done: boolean
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function laneTiming(
|
|
105
|
+
lane: {schedule: {started: {toDate(): Date}; tasks: Task[]}},
|
|
106
|
+
nowMs: number
|
|
107
|
+
): Timing[] {
|
|
108
|
+
const startedMs = lane.schedule.started.toDate().getTime()
|
|
109
|
+
let endSec = 0
|
|
110
|
+
return lane.schedule.tasks.map((t) => {
|
|
111
|
+
const startsAt = startedMs + endSec * 1000
|
|
112
|
+
endSec += t.duration.toNumber()
|
|
113
|
+
const completesAt = startedMs + endSec * 1000
|
|
114
|
+
return {
|
|
115
|
+
startsAt,
|
|
116
|
+
completesAt,
|
|
117
|
+
running: nowMs >= startsAt && nowMs < completesAt,
|
|
118
|
+
done: nowMs >= completesAt,
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function cancelEligibility(
|
|
124
|
+
entity: EntityInfo,
|
|
125
|
+
laneKey: number,
|
|
126
|
+
fromTaskIndex: number,
|
|
127
|
+
input: CancelEligibilityInput
|
|
128
|
+
): CancelPlan {
|
|
129
|
+
const lane = (entity.lanes ?? []).find((l) => l.lane_key.equals(laneKey))
|
|
130
|
+
if (!lane || fromTaskIndex < 0 || fromTaskIndex >= lane.schedule.tasks.length) {
|
|
131
|
+
return {ok: false, range: {count: 0, taskIndices: []}, effects: {...EMPTY_EFFECTS}}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const tasks = lane.schedule.tasks
|
|
135
|
+
const timing = laneTiming(lane, input.now.getTime())
|
|
136
|
+
const taskIndices: number[] = []
|
|
137
|
+
for (let i = fromTaskIndex; i < tasks.length; i++) taskIndices.push(i)
|
|
138
|
+
const range = {count: taskIndices.length, taskIndices}
|
|
139
|
+
|
|
140
|
+
const block = (blockedReason: CancelBlockReason): CancelPlan => ({
|
|
141
|
+
ok: false,
|
|
142
|
+
blockedReason,
|
|
143
|
+
range,
|
|
144
|
+
effects: {...EMPTY_EFFECTS},
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
for (const i of taskIndices) {
|
|
148
|
+
const t = tasks[i]
|
|
149
|
+
if (t.entitygroup && !t.entitygroup.equals(0))
|
|
150
|
+
return block(CancelBlockReason.CONTAINS_LINKED_TASK)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
for (const i of taskIndices) {
|
|
154
|
+
const t = tasks[i]
|
|
155
|
+
if (timing[i].done) return block(CancelBlockReason.DONE)
|
|
156
|
+
if (t.cancelable.equals(TaskCancelable.NEVER) && !t.type.equals(TaskType.IDLE))
|
|
157
|
+
return block(CancelBlockReason.TASK_NEVER)
|
|
158
|
+
if (t.cancelable.equals(TaskCancelable.BEFORE_START) && timing[i].running)
|
|
159
|
+
return block(CancelBlockReason.BEFORE_START_RUNNING)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const post = postCancelEntity(entity, laneKey, fromTaskIndex)
|
|
163
|
+
if (!feasibleAfterCancel(post)) return block(CancelBlockReason.WOULD_STRAND)
|
|
164
|
+
|
|
165
|
+
const effects: CancelEffects = {refunds: [], releasedHolds: [], abandonsRunning: false}
|
|
166
|
+
let energyForfeited = 0
|
|
167
|
+
for (const i of taskIndices) {
|
|
168
|
+
const t = tasks[i]
|
|
169
|
+
if (timing[i].running && t.cancelable.equals(TaskCancelable.ALWAYS))
|
|
170
|
+
effects.abandonsRunning = true
|
|
171
|
+
if (t.energy_cost) energyForfeited += t.energy_cost.toNumber()
|
|
172
|
+
if (t.type.equals(TaskType.BUILDPLOT) && t.entitytarget)
|
|
173
|
+
effects.keepsPlotDeposits = {plot: t.entitytarget}
|
|
174
|
+
if (t.hold && t.entitytarget) {
|
|
175
|
+
const counterpart = input.counterparts?.get(t.entitytarget.entity_id.toString())
|
|
176
|
+
const hold =
|
|
177
|
+
(counterpart?.holds ?? []).find((h) => h.id.equals(t.hold!)) ??
|
|
178
|
+
(entity.holds ?? []).find((h) => h.id.equals(t.hold!))
|
|
179
|
+
if (hold) {
|
|
180
|
+
const kind = hold.kind.toNumber()
|
|
181
|
+
effects.releasedHolds.push({counterpart: t.entitytarget, kind})
|
|
182
|
+
if (kind === 1 /* HOLD_PULL */) {
|
|
183
|
+
effects.refunds.push({giver: t.entitytarget, cargo: t.cargo})
|
|
184
|
+
const giver = counterpart
|
|
185
|
+
if (giver) {
|
|
186
|
+
const returned = t.cargo.reduce(
|
|
187
|
+
(s, c) => s + calcCargoItemMass(c).toNumber(),
|
|
188
|
+
0
|
|
189
|
+
)
|
|
190
|
+
const cap = giver.capacity
|
|
191
|
+
? giver.capacity.toNumber()
|
|
192
|
+
: Number.MAX_SAFE_INTEGER
|
|
193
|
+
if (giver.cargomass.toNumber() + returned > cap) {
|
|
194
|
+
return {
|
|
195
|
+
ok: false,
|
|
196
|
+
blockedReason: CancelBlockReason.WOULD_OVERFILL,
|
|
197
|
+
range,
|
|
198
|
+
effects: {...EMPTY_EFFECTS},
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (energyForfeited > 0) effects.energyForfeited = energyForfeited
|
|
207
|
+
|
|
208
|
+
return {ok: true, range, effects}
|
|
209
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import {UInt8, UInt16, UInt64} from '@wharfkit/antelope'
|
|
2
|
+
import {expect, test} from 'bun:test'
|
|
3
|
+
import {encodeStats} from '../derivation/crafting'
|
|
4
|
+
import {computeGathererYield, computeGathererDepth, computeGathererDrain} from '../nft/description'
|
|
5
|
+
import {computeLoaderThrust, computeLoaderMass} from '../nft/description'
|
|
6
|
+
import {computeCrafterSpeed, computeCrafterDrain} from '../nft/description'
|
|
7
|
+
import {applySlotMultiplier} from '../entities/slot-multiplier'
|
|
8
|
+
import {getSlotAmp} from '../entities/slot-multiplier'
|
|
9
|
+
import {getEntityLayout} from '../data/recipes-runtime'
|
|
10
|
+
import {ITEM_GATHERER_T1, ITEM_LOADER_T1, ITEM_CRAFTER_T1} from '../data/item-ids'
|
|
11
|
+
import {
|
|
12
|
+
workerLaneKey,
|
|
13
|
+
resolveLaneGatherer,
|
|
14
|
+
resolveLaneCrafter,
|
|
15
|
+
resolveLaneLoader,
|
|
16
|
+
selectGatherLane,
|
|
17
|
+
} from './lanes'
|
|
18
|
+
import {LANE_MOBILITY} from './schedule'
|
|
19
|
+
import type {ServerContract} from '../contracts'
|
|
20
|
+
|
|
21
|
+
type ModuleEntry = ServerContract.Types.module_entry
|
|
22
|
+
type Lane = ServerContract.Types.lane
|
|
23
|
+
|
|
24
|
+
// Real gatherer-bearing entity: extractor 10203 has [generator@0, gatherer@1].
|
|
25
|
+
const EXTRACTOR_GATHERER = 10203
|
|
26
|
+
const GATHERER_SLOT_IDX = 1
|
|
27
|
+
const GATHERER_LANE_KEY = GATHERER_SLOT_IDX + 1 // = 2
|
|
28
|
+
|
|
29
|
+
function makeModuleEntry(itemId: number, stats: bigint): ModuleEntry {
|
|
30
|
+
return {
|
|
31
|
+
type: UInt8.from(0),
|
|
32
|
+
installed: {
|
|
33
|
+
item_id: UInt16.from(itemId),
|
|
34
|
+
stats: UInt64.from(stats),
|
|
35
|
+
},
|
|
36
|
+
} as unknown as ModuleEntry
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function makeBusyLane(laneKey: number): Lane {
|
|
40
|
+
return {
|
|
41
|
+
lane_key: UInt8.from(laneKey),
|
|
42
|
+
schedule: {
|
|
43
|
+
started: {toDate: () => new Date()} as any,
|
|
44
|
+
tasks: [{duration: UInt64.from(100)} as any],
|
|
45
|
+
},
|
|
46
|
+
} as unknown as Lane
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// gatherer: stats = [str=300, tol=200, con=400], tier=1
|
|
50
|
+
const GATH_STR = 300
|
|
51
|
+
const GATH_TOL = 200
|
|
52
|
+
const GATH_CON = 400
|
|
53
|
+
const gathererStats1 = encodeStats([GATH_STR, GATH_TOL, GATH_CON])
|
|
54
|
+
|
|
55
|
+
// loader: stats = [ins=300, pla=500]
|
|
56
|
+
const LOADER_INS = 300
|
|
57
|
+
const LOADER_PLA = 500
|
|
58
|
+
const loaderStats = encodeStats([LOADER_INS, LOADER_PLA])
|
|
59
|
+
|
|
60
|
+
// crafter: stats = [rea=400, fin=300]
|
|
61
|
+
const CRAFTER_REA = 400
|
|
62
|
+
const CRAFTER_FIN = 300
|
|
63
|
+
const crafterStats = encodeStats([CRAFTER_REA, CRAFTER_FIN])
|
|
64
|
+
|
|
65
|
+
// --- resolveLaneGatherer ---
|
|
66
|
+
|
|
67
|
+
test('resolveLaneGatherer reads the layout slot amp and applies it to yield (parity formula)', () => {
|
|
68
|
+
// Gatherer at the entity's real gatherer slot (index 1 => laneKey 2); amp comes from getEntityLayout(10203).
|
|
69
|
+
const modules: ModuleEntry[] = [
|
|
70
|
+
makeModuleEntry(ITEM_GATHERER_T1, gathererStats1), // slot 0 (generator slot, ignored)
|
|
71
|
+
makeModuleEntry(ITEM_GATHERER_T1, gathererStats1), // slot 1 (gatherer slot)
|
|
72
|
+
]
|
|
73
|
+
const result = resolveLaneGatherer(modules, EXTRACTOR_GATHERER, GATHERER_LANE_KEY)
|
|
74
|
+
|
|
75
|
+
const layout = getEntityLayout(EXTRACTOR_GATHERER)?.slots ?? []
|
|
76
|
+
const ampFromLayout = getSlotAmp(layout, GATHERER_SLOT_IDX)
|
|
77
|
+
|
|
78
|
+
// The resolver routes through the real layout's amp, not a hardcoded 100.
|
|
79
|
+
expect(result.outputPct).toBe(ampFromLayout)
|
|
80
|
+
// Yield equals the contract formula clamp_to_uint16(compute_gatherer_yield(str) * amp / 100).
|
|
81
|
+
expect(result.yield).toBe(applySlotMultiplier(computeGathererYield(GATH_STR), ampFromLayout))
|
|
82
|
+
expect(result.drain).toBe(computeGathererDrain(GATH_CON))
|
|
83
|
+
expect(result.depth).toBe(computeGathererDepth(GATH_TOL, 1))
|
|
84
|
+
expect(result.slotIndex).toBe(GATHERER_SLOT_IDX)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('resolveLaneGatherer amp-scaling parity holds for a non-100 amp', () => {
|
|
88
|
+
// Parity for a non-100 amp: clamp_to_uint16(value * amp / 100).
|
|
89
|
+
expect(applySlotMultiplier(computeGathererYield(GATH_STR), 80)).toBe(
|
|
90
|
+
Math.min(Math.floor((computeGathererYield(GATH_STR) * 80) / 100), 65535)
|
|
91
|
+
)
|
|
92
|
+
expect(applySlotMultiplier(computeGathererYield(GATH_STR), 120)).toBe(
|
|
93
|
+
Math.floor((computeGathererYield(GATH_STR) * 120) / 100)
|
|
94
|
+
)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test('resolveLaneGatherer throws on out-of-range laneKey', () => {
|
|
98
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_GATHERER_T1, gathererStats1)]
|
|
99
|
+
expect(() => resolveLaneGatherer(modules, ITEM_GATHERER_T1, 5)).toThrow(
|
|
100
|
+
'gatherer lane has no module'
|
|
101
|
+
)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('resolveLaneGatherer throws on laneKey=0 (slot=255 off-by-one boundary)', () => {
|
|
105
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_GATHERER_T1, gathererStats1)]
|
|
106
|
+
expect(() => resolveLaneGatherer(modules, ITEM_GATHERER_T1, 0)).toThrow(
|
|
107
|
+
'gatherer lane has no module'
|
|
108
|
+
)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
// --- resolveLaneCrafter ---
|
|
112
|
+
|
|
113
|
+
test('resolveLaneCrafter returns correct stats for slot 0 (laneKey=1)', () => {
|
|
114
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_CRAFTER_T1, crafterStats)]
|
|
115
|
+
const result = resolveLaneCrafter(modules, ITEM_CRAFTER_T1, 1)
|
|
116
|
+
const layout = getEntityLayout(ITEM_CRAFTER_T1)?.slots ?? []
|
|
117
|
+
const amp = getSlotAmp(layout, 0)
|
|
118
|
+
expect(result.slotIndex).toBe(0)
|
|
119
|
+
expect(result.speed).toBe(applySlotMultiplier(computeCrafterSpeed(CRAFTER_REA), amp))
|
|
120
|
+
expect(result.drain).toBe(computeCrafterDrain(CRAFTER_FIN))
|
|
121
|
+
expect(result.outputPct).toBe(amp)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('resolveLaneCrafter throws on out-of-range laneKey', () => {
|
|
125
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_CRAFTER_T1, crafterStats)]
|
|
126
|
+
expect(() => resolveLaneCrafter(modules, ITEM_CRAFTER_T1, 5)).toThrow(
|
|
127
|
+
'crafter lane has no module'
|
|
128
|
+
)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('resolveLaneCrafter throws on laneKey=0 (slot=255 off-by-one boundary)', () => {
|
|
132
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_CRAFTER_T1, crafterStats)]
|
|
133
|
+
expect(() => resolveLaneCrafter(modules, ITEM_CRAFTER_T1, 0)).toThrow(
|
|
134
|
+
'crafter lane has no module'
|
|
135
|
+
)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
// --- resolveLaneLoader ---
|
|
139
|
+
|
|
140
|
+
test('resolveLaneLoader returns correct stats for slot 0 (laneKey=1)', () => {
|
|
141
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_LOADER_T1, loaderStats)]
|
|
142
|
+
const result = resolveLaneLoader(modules, ITEM_LOADER_T1, 1)
|
|
143
|
+
const layout = getEntityLayout(ITEM_LOADER_T1)?.slots ?? []
|
|
144
|
+
const amp = getSlotAmp(layout, 0)
|
|
145
|
+
expect(result.valid).toBe(true)
|
|
146
|
+
expect(result.slotIndex).toBe(0)
|
|
147
|
+
expect(result.thrust).toBe(applySlotMultiplier(computeLoaderThrust(LOADER_PLA), amp))
|
|
148
|
+
expect(result.mass).toBe(computeLoaderMass(LOADER_INS))
|
|
149
|
+
expect(result.outputPct).toBe(amp)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
test('resolveLaneLoader soft-returns invalid for LANE_MOBILITY (key 0), never throws', () => {
|
|
153
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_LOADER_T1, loaderStats)]
|
|
154
|
+
const result = resolveLaneLoader(modules, ITEM_LOADER_T1, LANE_MOBILITY)
|
|
155
|
+
expect(result.valid).toBe(false)
|
|
156
|
+
expect(result.thrust).toBe(0)
|
|
157
|
+
expect(result.mass).toBe(0)
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
test('resolveLaneLoader soft-returns invalid for a missing/out-of-range module, never throws', () => {
|
|
161
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_LOADER_T1, loaderStats)]
|
|
162
|
+
const result = resolveLaneLoader(modules, ITEM_LOADER_T1, 5)
|
|
163
|
+
expect(result.valid).toBe(false)
|
|
164
|
+
expect(result.thrust).toBe(0)
|
|
165
|
+
expect(result.mass).toBe(0)
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
// --- depth-aware workerLaneKey ---
|
|
169
|
+
|
|
170
|
+
test('workerLaneKey with stratum picks first-free reaching gatherer', () => {
|
|
171
|
+
const shallowStats = encodeStats([100, 200, 100]) // tol=200 => depth=1500 for tier1
|
|
172
|
+
const deepStats = encodeStats([100, 900, 100]) // tol=900 => depth=5000 for tier1
|
|
173
|
+
const modules: ModuleEntry[] = [
|
|
174
|
+
makeModuleEntry(ITEM_GATHERER_T1, shallowStats),
|
|
175
|
+
makeModuleEntry(ITEM_GATHERER_T1, deepStats),
|
|
176
|
+
]
|
|
177
|
+
const laneKey = workerLaneKey(modules, 'gatherer', [], 2000)
|
|
178
|
+
expect(laneKey).toBe(2) // slot 1 => laneKey 2
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
test('workerLaneKey with stratum: first free reaching is preferred over busy reaching', () => {
|
|
182
|
+
const deepStats = encodeStats([100, 900, 100]) // tol=900 => depth=5000 for tier1
|
|
183
|
+
const modules: ModuleEntry[] = [
|
|
184
|
+
makeModuleEntry(ITEM_GATHERER_T1, deepStats),
|
|
185
|
+
makeModuleEntry(ITEM_GATHERER_T1, deepStats),
|
|
186
|
+
]
|
|
187
|
+
const lanes: Lane[] = [makeBusyLane(1)] // slot 0 is busy
|
|
188
|
+
const laneKey = workerLaneKey(modules, 'gatherer', lanes, 2000)
|
|
189
|
+
expect(laneKey).toBe(2) // slot 1 (laneKey=2) is free and reaching
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
test('workerLaneKey with stratum: returns lowest reaching (busy) when all reaching are busy', () => {
|
|
193
|
+
const deepStats = encodeStats([100, 900, 100]) // tol=900 => depth=5000 for tier1
|
|
194
|
+
const modules: ModuleEntry[] = [
|
|
195
|
+
makeModuleEntry(ITEM_GATHERER_T1, deepStats),
|
|
196
|
+
makeModuleEntry(ITEM_GATHERER_T1, deepStats),
|
|
197
|
+
]
|
|
198
|
+
const lanes: Lane[] = [makeBusyLane(1), makeBusyLane(2)]
|
|
199
|
+
const laneKey = workerLaneKey(modules, 'gatherer', lanes, 2000)
|
|
200
|
+
expect(laneKey).toBe(1) // lowest reaching is slot 0 => laneKey 1
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
test('workerLaneKey with stratum throws "no gatherer reaches this stratum" when none reach', () => {
|
|
204
|
+
const shallowStats = encodeStats([100, 10, 100]) // tol=10 => depth=550 for tier1
|
|
205
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_GATHERER_T1, shallowStats)]
|
|
206
|
+
expect(() => workerLaneKey(modules, 'gatherer', [], 5000)).toThrow(
|
|
207
|
+
'no gatherer reaches this stratum'
|
|
208
|
+
)
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
test('workerLaneKey without stratum still works (non-gatherer path)', () => {
|
|
212
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_CRAFTER_T1, crafterStats)]
|
|
213
|
+
const laneKey = workerLaneKey(modules, 'crafter', [])
|
|
214
|
+
expect(laneKey).toBe(1)
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
// --- selectGatherLane: both real error paths ---
|
|
218
|
+
|
|
219
|
+
test('selectGatherLane auto-pick returns the depth-aware lane', () => {
|
|
220
|
+
const shallowStats = encodeStats([100, 200, 100]) // depth=1500 for tier1
|
|
221
|
+
const deepStats = encodeStats([100, 900, 100]) // depth=5000 for tier1
|
|
222
|
+
const modules: ModuleEntry[] = [
|
|
223
|
+
makeModuleEntry(ITEM_GATHERER_T1, shallowStats),
|
|
224
|
+
makeModuleEntry(ITEM_GATHERER_T1, deepStats),
|
|
225
|
+
]
|
|
226
|
+
expect(selectGatherLane(modules, ITEM_GATHERER_T1, [], 2000)).toBe(2)
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
test('selectGatherLane auto-pick throws "no gatherer reaches this stratum" when none reach', () => {
|
|
230
|
+
const shallowStats = encodeStats([100, 10, 100]) // depth=550 for tier1
|
|
231
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_GATHERER_T1, shallowStats)]
|
|
232
|
+
expect(() => selectGatherLane(modules, ITEM_GATHERER_T1, [], 5000)).toThrow(
|
|
233
|
+
'no gatherer reaches this stratum'
|
|
234
|
+
)
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
test('selectGatherLane explicit slot returns its laneKey when stratum is within depth', () => {
|
|
238
|
+
const deepStats = encodeStats([100, 900, 100]) // depth=5000 for tier1
|
|
239
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_GATHERER_T1, deepStats)]
|
|
240
|
+
expect(selectGatherLane(modules, ITEM_GATHERER_T1, [], 2000, 0)).toBe(1) // slot 0 => laneKey 1
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
test('selectGatherLane explicit slot too shallow throws "stratum exceeds gatherer depth"', () => {
|
|
244
|
+
const shallowStats = encodeStats([100, 10, 100]) // depth=550 for tier1
|
|
245
|
+
const modules: ModuleEntry[] = [makeModuleEntry(ITEM_GATHERER_T1, shallowStats)]
|
|
246
|
+
expect(() => selectGatherLane(modules, ITEM_GATHERER_T1, [], 5000, 0)).toThrow(
|
|
247
|
+
'stratum exceeds gatherer depth'
|
|
248
|
+
)
|
|
249
|
+
})
|
package/src/scheduling/lanes.ts
CHANGED
|
@@ -1,12 +1,47 @@
|
|
|
1
1
|
import type {ServerContract} from '../contracts'
|
|
2
2
|
import {getItem} from '../data/catalog'
|
|
3
|
+
import {getEntityLayout} from '../data/recipes-runtime'
|
|
4
|
+
import {decodeStat} from '../derivation/crafting'
|
|
5
|
+
import {gathererDepthForTier} from '../derivation/capabilities'
|
|
6
|
+
import {applySlotMultiplier, getSlotAmp} from '../entities/slot-multiplier'
|
|
7
|
+
import {
|
|
8
|
+
computeGathererYield,
|
|
9
|
+
computeGathererDrain,
|
|
10
|
+
computeLoaderThrust,
|
|
11
|
+
computeLoaderMass,
|
|
12
|
+
computeCrafterSpeed,
|
|
13
|
+
computeCrafterDrain,
|
|
14
|
+
} from '../nft/description'
|
|
3
15
|
import type {ModuleType} from '../types'
|
|
4
|
-
import {getLane, type ScheduleData} from './schedule'
|
|
16
|
+
import {getLane, LANE_MOBILITY, type ScheduleData} from './schedule'
|
|
5
17
|
|
|
6
18
|
type ModuleEntry = ServerContract.Types.module_entry
|
|
7
19
|
type Lane = ServerContract.Types.lane
|
|
8
20
|
type Schedule = ServerContract.Types.schedule
|
|
9
21
|
|
|
22
|
+
export interface ResolvedGathererLane {
|
|
23
|
+
slotIndex: number
|
|
24
|
+
yield: number
|
|
25
|
+
drain: number
|
|
26
|
+
depth: number
|
|
27
|
+
outputPct: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ResolvedCrafterLane {
|
|
31
|
+
slotIndex: number
|
|
32
|
+
speed: number
|
|
33
|
+
drain: number
|
|
34
|
+
outputPct: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ResolvedLoaderLane {
|
|
38
|
+
slotIndex: number
|
|
39
|
+
thrust: number
|
|
40
|
+
mass: number
|
|
41
|
+
outputPct: number
|
|
42
|
+
valid: boolean
|
|
43
|
+
}
|
|
44
|
+
|
|
10
45
|
export function laneKeyForModule(slotIndex: number): number {
|
|
11
46
|
return slotIndex + 1
|
|
12
47
|
}
|
|
@@ -16,11 +51,114 @@ function laneIsFree(lanes: Lane[], laneKey: number): boolean {
|
|
|
16
51
|
return lane ? lane.schedule.tasks.length === 0 : true
|
|
17
52
|
}
|
|
18
53
|
|
|
54
|
+
export function resolveLaneGatherer(
|
|
55
|
+
modules: ModuleEntry[],
|
|
56
|
+
entityItemId: number,
|
|
57
|
+
laneKey: number
|
|
58
|
+
): ResolvedGathererLane {
|
|
59
|
+
const idx = laneKey - 1
|
|
60
|
+
const installed = idx >= 0 && idx < modules.length ? modules[idx].installed : undefined
|
|
61
|
+
if (!installed) throw new Error('gatherer lane has no module')
|
|
62
|
+
const item = getItem(Number(installed.item_id.value ?? installed.item_id))
|
|
63
|
+
if (item.moduleType !== 'gatherer') throw new Error('lane module is not a gatherer')
|
|
64
|
+
const stats = BigInt(installed.stats.toString())
|
|
65
|
+
const str = decodeStat(stats, 0)
|
|
66
|
+
const tol = decodeStat(stats, 1)
|
|
67
|
+
const con = decodeStat(stats, 2)
|
|
68
|
+
const layout = getEntityLayout(entityItemId)?.slots ?? []
|
|
69
|
+
const amp = getSlotAmp(layout, idx)
|
|
70
|
+
const yieldVal = applySlotMultiplier(computeGathererYield(str), amp)
|
|
71
|
+
const drain = computeGathererDrain(con)
|
|
72
|
+
const depth = gathererDepthForTier(tol, item.tier ?? 1)
|
|
73
|
+
return {slotIndex: idx, yield: yieldVal, drain, depth, outputPct: amp}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Encapsulates the gather handler's lane selection (gathering.cpp:108-112): both error paths.
|
|
77
|
+
export function selectGatherLane(
|
|
78
|
+
modules: ModuleEntry[],
|
|
79
|
+
entityItemId: number,
|
|
80
|
+
lanes: Lane[],
|
|
81
|
+
stratum: number,
|
|
82
|
+
explicitSlot?: number
|
|
83
|
+
): number {
|
|
84
|
+
if (explicitSlot !== undefined) {
|
|
85
|
+
const laneKey = laneKeyForModule(explicitSlot)
|
|
86
|
+
const lane = resolveLaneGatherer(modules, entityItemId, laneKey)
|
|
87
|
+
if (stratum > lane.depth) throw new Error('stratum exceeds gatherer depth')
|
|
88
|
+
return laneKey
|
|
89
|
+
}
|
|
90
|
+
return workerLaneKey(modules, 'gatherer', lanes, stratum)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function resolveLaneCrafter(
|
|
94
|
+
modules: ModuleEntry[],
|
|
95
|
+
entityItemId: number,
|
|
96
|
+
laneKey: number
|
|
97
|
+
): ResolvedCrafterLane {
|
|
98
|
+
const idx = laneKey - 1
|
|
99
|
+
const installed = idx >= 0 && idx < modules.length ? modules[idx].installed : undefined
|
|
100
|
+
if (!installed) throw new Error('crafter lane has no module')
|
|
101
|
+
const item = getItem(Number(installed.item_id.value ?? installed.item_id))
|
|
102
|
+
if (item.moduleType !== 'crafter') throw new Error('lane module is not a crafter')
|
|
103
|
+
const stats = BigInt(installed.stats.toString())
|
|
104
|
+
const rea = decodeStat(stats, 0)
|
|
105
|
+
const fin = decodeStat(stats, 1)
|
|
106
|
+
const layout = getEntityLayout(entityItemId)?.slots ?? []
|
|
107
|
+
const amp = getSlotAmp(layout, idx)
|
|
108
|
+
const speed = applySlotMultiplier(computeCrafterSpeed(rea), amp)
|
|
109
|
+
const drain = computeCrafterDrain(fin)
|
|
110
|
+
return {slotIndex: idx, speed, drain, outputPct: amp}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// LANE_MOBILITY or a missing module soft-returns valid=false (never throws); callers check `valid`.
|
|
114
|
+
export function resolveLaneLoader(
|
|
115
|
+
modules: ModuleEntry[],
|
|
116
|
+
entityItemId: number,
|
|
117
|
+
laneKey: number
|
|
118
|
+
): ResolvedLoaderLane {
|
|
119
|
+
if (laneKey === LANE_MOBILITY) {
|
|
120
|
+
return {slotIndex: -1, thrust: 0, mass: 0, outputPct: 0, valid: false}
|
|
121
|
+
}
|
|
122
|
+
const idx = laneKey - 1
|
|
123
|
+
const installed = idx >= 0 && idx < modules.length ? modules[idx].installed : undefined
|
|
124
|
+
if (!installed) {
|
|
125
|
+
return {slotIndex: idx, thrust: 0, mass: 0, outputPct: 0, valid: false}
|
|
126
|
+
}
|
|
127
|
+
const stats = BigInt(installed.stats.toString())
|
|
128
|
+
const ins = decodeStat(stats, 0)
|
|
129
|
+
const pla = decodeStat(stats, 1)
|
|
130
|
+
const layout = getEntityLayout(entityItemId)?.slots ?? []
|
|
131
|
+
const amp = getSlotAmp(layout, idx)
|
|
132
|
+
const thrust = applySlotMultiplier(computeLoaderThrust(pla), amp)
|
|
133
|
+
const mass = computeLoaderMass(ins)
|
|
134
|
+
return {slotIndex: idx, thrust, mass, outputPct: amp, valid: true}
|
|
135
|
+
}
|
|
136
|
+
|
|
19
137
|
export function workerLaneKey(
|
|
20
138
|
modules: ModuleEntry[],
|
|
21
139
|
moduleSubtype: ModuleType,
|
|
22
|
-
lanes: Lane[]
|
|
140
|
+
lanes: Lane[],
|
|
141
|
+
stratum?: number
|
|
23
142
|
): number {
|
|
143
|
+
if (moduleSubtype === 'gatherer' && stratum !== undefined) {
|
|
144
|
+
let lowestReaching: number | undefined
|
|
145
|
+
for (let i = 0; i < modules.length; i++) {
|
|
146
|
+
const installed = modules[i].installed
|
|
147
|
+
if (!installed) continue
|
|
148
|
+
const item = getItem(Number(installed.item_id.value ?? installed.item_id))
|
|
149
|
+
if (item.moduleType !== 'gatherer') continue
|
|
150
|
+
const stats = BigInt(installed.stats.toString())
|
|
151
|
+
const tol = decodeStat(stats, 1)
|
|
152
|
+
const depth = gathererDepthForTier(tol, item.tier ?? 1)
|
|
153
|
+
if (depth < stratum) continue
|
|
154
|
+
const laneKey = laneKeyForModule(i)
|
|
155
|
+
if (lowestReaching === undefined) lowestReaching = laneKey
|
|
156
|
+
if (laneIsFree(lanes, laneKey)) return laneKey
|
|
157
|
+
}
|
|
158
|
+
if (lowestReaching === undefined) throw new Error('no gatherer reaches this stratum')
|
|
159
|
+
return lowestReaching
|
|
160
|
+
}
|
|
161
|
+
|
|
24
162
|
const occupiedMatchingLaneKeys: number[] = []
|
|
25
163
|
|
|
26
164
|
for (let slotIndex = 0; slotIndex < modules.length; slotIndex++) {
|