@shipload/sdk 1.0.0-next.11 → 1.0.0-next.12

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.
@@ -1,221 +0,0 @@
1
- import {type UInt16, type UInt16Type, UInt32, UInt64, type UInt64Type} from '@wharfkit/antelope'
2
- import {ServerContract} from '../contracts'
3
- import {Coordinates, type CoordinatesType} from '../types'
4
- import {
5
- type FloatPosition,
6
- getDestinationLocation,
7
- getInterpolatedPosition,
8
- getPositionAt,
9
- getFlightOrigin as travelGetFlightOrigin,
10
- } from '../travel/travel'
11
- import {
12
- type ProjectedEntity,
13
- projectFromCurrentState as sharedProjectFromCurrentState,
14
- projectFromCurrentStateAt as sharedProjectFromCurrentStateAt,
15
- } from '../scheduling/projection'
16
- import {Location} from './location'
17
- import {ScheduleAccessor} from '../scheduling/accessor'
18
- import {InventoryAccessor} from './inventory-accessor'
19
- import type {EntityInventory} from './entity-inventory'
20
- import {
21
- energyPercent as calcEnergyPercent,
22
- needsRecharge as calcNeedsRecharge,
23
- hasEnergyForDistance,
24
- maxTravelDistance,
25
- } from '../capabilities/movement'
26
- import * as schedule from '../scheduling/schedule'
27
-
28
- export interface PackedModuleInput {
29
- itemId: UInt16Type
30
- stats: UInt64Type
31
- }
32
-
33
- export interface ShipStateInput {
34
- id: UInt64Type
35
- owner: string
36
- name: string
37
- coordinates: CoordinatesType | {x: number; y: number; z?: number}
38
- hullmass?: number
39
- capacity?: number
40
- energy?: number
41
- modules?: PackedModuleInput[]
42
- schedule?: ServerContract.Types.schedule
43
- cargo?: ServerContract.Types.cargo_item[]
44
- }
45
-
46
- type MovementEntity = {
47
- engines: ServerContract.Types.movement_stats
48
- generator: ServerContract.Types.energy_stats
49
- energy: UInt16
50
- }
51
-
52
- export class Ship extends ServerContract.Types.entity_info {
53
- private _sched?: ScheduleAccessor
54
- private _inv?: InventoryAccessor
55
-
56
- get name(): string {
57
- return this.entity_name
58
- }
59
-
60
- get entityClass(): 'orbital' {
61
- return 'orbital'
62
- }
63
-
64
- get canUndeploy(): boolean {
65
- return true
66
- }
67
-
68
- get inv(): InventoryAccessor {
69
- this._inv ??= new InventoryAccessor(this)
70
- return this._inv
71
- }
72
-
73
- get inventory(): EntityInventory[] {
74
- return this.inv.items
75
- }
76
-
77
- get sched(): ScheduleAccessor {
78
- this._sched ??= new ScheduleAccessor(this)
79
- return this._sched
80
- }
81
-
82
- get maxDistance(): UInt32 {
83
- if (!this.generator || !this.engines) return UInt32.from(0)
84
- return maxTravelDistance(this as MovementEntity)
85
- }
86
-
87
- get isIdle(): boolean {
88
- return this.is_idle
89
- }
90
-
91
- getFlightOrigin(flightTaskIndex: number): Coordinates {
92
- return Coordinates.from(travelGetFlightOrigin(this, flightTaskIndex))
93
- }
94
-
95
- destinationLocation(): Coordinates | undefined {
96
- const dest = getDestinationLocation(this)
97
- return dest ? Coordinates.from(dest) : undefined
98
- }
99
-
100
- /** Chain-tile coordinates at `now`. For smooth visual position use interpolatedPositionAt. */
101
- positionAt(now: Date): Coordinates {
102
- const taskIndex = this.sched.currentTaskIndex(now)
103
- const progress = this.sched.currentTaskProgress(now)
104
- return Coordinates.from(getPositionAt(this, taskIndex, progress))
105
- }
106
-
107
- interpolatedPositionAt(now: Date): FloatPosition {
108
- const taskIndex = this.sched.currentTaskIndex(now)
109
- const progress = this.sched.currentTaskProgressFloat(now)
110
- return getInterpolatedPosition(this, taskIndex, progress)
111
- }
112
-
113
- isInFlight(now: Date): boolean {
114
- return schedule.isInFlight(this, now)
115
- }
116
-
117
- isRecharging(now: Date): boolean {
118
- return schedule.isRecharging(this, now)
119
- }
120
-
121
- isLoading(now: Date): boolean {
122
- return schedule.isLoading(this, now)
123
- }
124
-
125
- isUnloading(now: Date): boolean {
126
- return schedule.isUnloading(this, now)
127
- }
128
-
129
- isGathering(now: Date): boolean {
130
- return schedule.isGathering(this, now)
131
- }
132
-
133
- get hasEngines(): boolean {
134
- return this.engines !== undefined
135
- }
136
-
137
- get hasGenerator(): boolean {
138
- return this.generator !== undefined
139
- }
140
-
141
- get hasGatherer(): boolean {
142
- return this.gatherer !== undefined
143
- }
144
-
145
- get hasWarp(): boolean {
146
- return this.warp !== undefined
147
- }
148
-
149
- project(): ProjectedEntity {
150
- return sharedProjectFromCurrentState(this)
151
- }
152
-
153
- projectAt(now: Date): ProjectedEntity {
154
- return sharedProjectFromCurrentStateAt(this, now)
155
- }
156
-
157
- get location(): Location {
158
- return Location.from(this.coordinates)
159
- }
160
-
161
- get totalCargoMass(): UInt64 {
162
- return this.inv.totalMass
163
- }
164
-
165
- get totalMass(): UInt64 {
166
- let mass = UInt64.from(this.hullmass ?? 0).adding(this.totalCargoMass)
167
- if (this.loaders) {
168
- mass = mass.adding(UInt64.from(this.loaders.mass).multiplying(this.loaders.quantity))
169
- }
170
- return mass
171
- }
172
-
173
- get maxCapacity(): UInt64 {
174
- return UInt64.from(this.capacity)
175
- }
176
-
177
- hasSpace(goodMass: UInt64, quantity: number): boolean {
178
- return this.totalMass.adding(goodMass.multiplying(quantity)).lte(this.maxCapacity)
179
- }
180
-
181
- get availableCapacity(): UInt64 {
182
- return this.totalMass.gte(this.maxCapacity)
183
- ? UInt64.from(0)
184
- : this.maxCapacity.subtracting(this.totalMass)
185
- }
186
-
187
- getCargoForItem(goodId: UInt64Type): EntityInventory | undefined {
188
- return this.inv.forItem(goodId)
189
- }
190
-
191
- get sellableCargo(): EntityInventory[] {
192
- return this.inv.sellable
193
- }
194
-
195
- get hasSellableCargo(): boolean {
196
- return this.inv.hasSellable
197
- }
198
-
199
- get sellableGoodsCount(): number {
200
- return this.inv.sellableCount
201
- }
202
-
203
- get isFull(): boolean {
204
- return this.totalMass.gte(this.maxCapacity)
205
- }
206
-
207
- get energyPercent(): number {
208
- if (!this.generator || this.energy === undefined) return 0
209
- return calcEnergyPercent(this as MovementEntity)
210
- }
211
-
212
- get needsRecharge(): boolean {
213
- if (!this.generator || this.energy === undefined) return false
214
- return calcNeedsRecharge(this as MovementEntity)
215
- }
216
-
217
- hasEnergyFor(distance: UInt64): boolean {
218
- if (!this.engines || !this.generator || this.energy === undefined) return false
219
- return hasEnergyForDistance(this as MovementEntity, distance)
220
- }
221
- }
@@ -1,136 +0,0 @@
1
- import {UInt64, type UInt64Type} from '@wharfkit/antelope'
2
- import {ServerContract} from '../contracts'
3
- import type {CoordinatesType} from '../types'
4
- import {Location} from './location'
5
- import {ScheduleAccessor} from '../scheduling/accessor'
6
- import {InventoryAccessor} from './inventory-accessor'
7
- import type {EntityInventory} from './entity-inventory'
8
- import * as schedule from '../scheduling/schedule'
9
- import type {PackedModuleInput} from './ship'
10
- import {decodeCraftedItemStats} from '../derivation/crafting'
11
- import {getModuleCapabilityType, MODULE_LOADER} from '../capabilities/modules'
12
- import {computeLoaderCapabilities} from './ship-deploy'
13
- import {applySlotMultiplier, clampUint16, getSlotAmp, type InstalledModule} from './slot-multiplier'
14
- import type {EntitySlot} from '../data/recipes-runtime'
15
-
16
- export interface WarehouseStateInput {
17
- id: UInt64Type
18
- owner: string
19
- name: string
20
- coordinates: CoordinatesType | {x: number; y: number; z?: number}
21
- hullmass?: number
22
- capacity: number
23
- modules?: PackedModuleInput[]
24
- schedule?: ServerContract.Types.schedule
25
- cargo?: ServerContract.Types.cargo_item[]
26
- }
27
-
28
- export class Warehouse extends ServerContract.Types.entity_info {
29
- private _sched?: ScheduleAccessor
30
- private _inv?: InventoryAccessor
31
-
32
- get name(): string {
33
- return this.entity_name
34
- }
35
-
36
- get entityClass(): 'planetary' {
37
- return 'planetary'
38
- }
39
-
40
- get canDemolish(): boolean {
41
- return true
42
- }
43
-
44
- get inv(): InventoryAccessor {
45
- this._inv ??= new InventoryAccessor(this)
46
- return this._inv
47
- }
48
-
49
- get inventory(): EntityInventory[] {
50
- return this.inv.items
51
- }
52
-
53
- get sched(): ScheduleAccessor {
54
- this._sched ??= new ScheduleAccessor(this)
55
- return this._sched
56
- }
57
-
58
- get isIdle(): boolean {
59
- return this.is_idle
60
- }
61
-
62
- isLoading(now: Date): boolean {
63
- return schedule.isLoading(this, now)
64
- }
65
-
66
- isUnloading(now: Date): boolean {
67
- return schedule.isUnloading(this, now)
68
- }
69
-
70
- get location(): Location {
71
- return Location.from(this.coordinates)
72
- }
73
-
74
- get totalCargoMass(): UInt64 {
75
- return this.inv.totalMass
76
- }
77
-
78
- get maxCapacity(): UInt64 {
79
- return UInt64.from(this.capacity)
80
- }
81
-
82
- get availableCapacity(): UInt64 {
83
- const cargo = this.totalCargoMass
84
- return cargo.gte(this.maxCapacity) ? UInt64.from(0) : this.maxCapacity.subtracting(cargo)
85
- }
86
-
87
- hasSpace(goodMass: UInt64, quantity: number): boolean {
88
- return this.totalCargoMass.adding(goodMass.multiplying(quantity)).lte(this.maxCapacity)
89
- }
90
-
91
- get isFull(): boolean {
92
- return this.totalCargoMass.gte(this.maxCapacity)
93
- }
94
-
95
- getCargoForItem(goodId: UInt64Type): EntityInventory | undefined {
96
- return this.inv.forItem(goodId)
97
- }
98
-
99
- get orbitalAltitude(): number {
100
- return this.coordinates.z?.toNumber() || 0
101
- }
102
-
103
- get totalMass(): UInt64 {
104
- const hull = this.hullmass ? UInt64.from(this.hullmass) : UInt64.from(0)
105
- return hull.adding(this.totalCargoMass)
106
- }
107
- }
108
-
109
- export function computeWarehouseCapabilities(
110
- modules: InstalledModule[],
111
- layout: EntitySlot[]
112
- ): {
113
- loaders?: {mass: number; thrust: number; quantity: number}
114
- } {
115
- const warehouse: {loaders?: {mass: number; thrust: number; quantity: number}} = {}
116
-
117
- const loaderModules = modules.filter((m) => getModuleCapabilityType(m.itemId) === MODULE_LOADER)
118
- if (loaderModules.length > 0) {
119
- let totalMass = 0
120
- let totalThrust = 0
121
- let totalQuantity = 0
122
- for (const m of loaderModules) {
123
- const caps = computeLoaderCapabilities(decodeCraftedItemStats(m.itemId, m.stats))
124
- totalMass += caps.mass
125
- totalThrust += applySlotMultiplier(caps.thrust, getSlotAmp(layout, m.slotIndex))
126
- totalQuantity += caps.quantity
127
- }
128
- warehouse.loaders = {
129
- mass: totalMass,
130
- thrust: clampUint16(totalThrust),
131
- quantity: totalQuantity,
132
- }
133
- }
134
-
135
- return warehouse
136
- }
@@ -1,171 +0,0 @@
1
- import {Name} from '@wharfkit/antelope'
2
- import {
3
- ITEM_CONTAINER_T1_PACKED,
4
- ITEM_CONTAINER_T2_PACKED,
5
- ITEM_EXTRACTOR_T1_PACKED,
6
- ITEM_FACTORY_T1_PACKED,
7
- ITEM_SHIP_T1_PACKED,
8
- ITEM_WAREHOUSE_T1_PACKED,
9
- } from '../data/item-ids'
10
-
11
- export const ENTITY_SHIP = Name.from('ship')
12
- export const ENTITY_WAREHOUSE = Name.from('warehouse')
13
- export const ENTITY_EXTRACTOR = Name.from('extractor')
14
- export const ENTITY_FACTORY = Name.from('factory')
15
- export const ENTITY_CONTAINER = Name.from('container')
16
- export const ENTITY_NEXUS = Name.from('nexus')
17
-
18
- export enum EntityClass {
19
- OrbitalVessel = 0,
20
- PlanetaryStructure = 1,
21
- }
22
-
23
- export function getEntityClass(entityType: Name | EntityTypeName): EntityClass {
24
- const typeName = typeof entityType === 'string' ? entityType : entityType.toString()
25
- switch (typeName) {
26
- case 'ship':
27
- case 'container':
28
- case 'nexus':
29
- return EntityClass.OrbitalVessel
30
- case 'warehouse':
31
- case 'extractor':
32
- case 'factory':
33
- return EntityClass.PlanetaryStructure
34
- default:
35
- throw new Error(`Entity type has no class: ${typeName}`)
36
- }
37
- }
38
-
39
- export function getPackedEntityType(itemId: number): Name | null {
40
- switch (itemId) {
41
- case ITEM_SHIP_T1_PACKED:
42
- return ENTITY_SHIP
43
- case ITEM_CONTAINER_T1_PACKED:
44
- case ITEM_CONTAINER_T2_PACKED:
45
- return ENTITY_CONTAINER
46
- case ITEM_WAREHOUSE_T1_PACKED:
47
- return ENTITY_WAREHOUSE
48
- case ITEM_EXTRACTOR_T1_PACKED:
49
- return ENTITY_EXTRACTOR
50
- case ITEM_FACTORY_T1_PACKED:
51
- return ENTITY_FACTORY
52
- default:
53
- return null
54
- }
55
- }
56
-
57
- export type EntityTypeName = 'ship' | 'warehouse' | 'extractor' | 'factory' | 'container' | 'nexus'
58
-
59
- export interface EntityTraits {
60
- typeName: Name
61
- isMovable: boolean
62
- hasEnergy: boolean
63
- hasLoaders: boolean
64
- hasModules: boolean
65
- notFoundError: string
66
- }
67
-
68
- export const shipTraits: EntityTraits = {
69
- typeName: ENTITY_SHIP,
70
- isMovable: true,
71
- hasEnergy: true,
72
- hasLoaders: true,
73
- hasModules: true,
74
-
75
- notFoundError: 'ship not found',
76
- }
77
-
78
- export const warehouseTraits: EntityTraits = {
79
- typeName: ENTITY_WAREHOUSE,
80
- isMovable: false,
81
- hasEnergy: false,
82
- hasLoaders: true,
83
- hasModules: true,
84
-
85
- notFoundError: 'warehouse not found',
86
- }
87
-
88
- export const extractorTraits: EntityTraits = {
89
- typeName: ENTITY_EXTRACTOR,
90
- isMovable: false,
91
- hasEnergy: true,
92
- hasLoaders: false,
93
- hasModules: true,
94
-
95
- notFoundError: 'extractor not found',
96
- }
97
-
98
- export const factoryTraits: EntityTraits = {
99
- typeName: ENTITY_FACTORY,
100
- isMovable: false,
101
- hasEnergy: true,
102
- hasLoaders: false,
103
- hasModules: true,
104
-
105
- notFoundError: 'factory not found',
106
- }
107
-
108
- export const containerTraits: EntityTraits = {
109
- typeName: ENTITY_CONTAINER,
110
- isMovable: true,
111
- hasEnergy: false,
112
- hasLoaders: false,
113
- hasModules: false,
114
-
115
- notFoundError: 'container not found',
116
- }
117
-
118
- export const nexusTraits: EntityTraits = {
119
- typeName: ENTITY_NEXUS,
120
- isMovable: false,
121
- hasEnergy: false,
122
- hasLoaders: false,
123
- hasModules: false,
124
-
125
- notFoundError: 'nexus not found',
126
- }
127
-
128
- export function getEntityTraits(entityType: Name | EntityTypeName): EntityTraits {
129
- const typeName = typeof entityType === 'string' ? entityType : entityType.toString()
130
-
131
- switch (typeName) {
132
- case 'ship':
133
- return shipTraits
134
- case 'warehouse':
135
- return warehouseTraits
136
- case 'extractor':
137
- return extractorTraits
138
- case 'factory':
139
- return factoryTraits
140
- case 'container':
141
- return containerTraits
142
- case 'nexus':
143
- return nexusTraits
144
- default:
145
- throw new Error(`Unknown entity type: ${typeName}`)
146
- }
147
- }
148
-
149
- export function isShip(entity: {type?: Name}): boolean {
150
- return entity.type?.equals(ENTITY_SHIP) ?? false
151
- }
152
-
153
- export function isWarehouse(entity: {type?: Name}): boolean {
154
- return entity.type?.equals(ENTITY_WAREHOUSE) ?? false
155
- }
156
-
157
- export function isExtractor(entity: {type?: Name}): boolean {
158
- return entity.type?.equals(ENTITY_EXTRACTOR) ?? false
159
- }
160
-
161
- export function isFactory(entity: {type?: Name}): boolean {
162
- return entity.type?.equals(ENTITY_FACTORY) ?? false
163
- }
164
-
165
- export function isContainer(entity: {type?: Name}): boolean {
166
- return entity.type?.equals(ENTITY_CONTAINER) ?? false
167
- }
168
-
169
- export function isNexus(entity: {type?: Name}): boolean {
170
- return entity.type?.equals(ENTITY_NEXUS) ?? false
171
- }