@shipload/sdk 2.0.0-rc6 → 2.0.0-rc7

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.
@@ -0,0 +1,242 @@
1
+ import {UInt16, UInt64} from '@wharfkit/antelope'
2
+ import type {UInt16Type, UInt64Type} from '@wharfkit/antelope'
3
+ import type {ResourceCategory, ResourceTier} from '../types'
4
+ import {getItem} from '../market/items'
5
+ import {getComponentById, getModuleRecipeByItemId, getEntityRecipeByItemId} from '../data/recipes'
6
+ import {isModuleItem, getModuleCapabilityType, MODULE_ENGINE, MODULE_GENERATOR, MODULE_EXTRACTOR, MODULE_LOADER, MODULE_CRAFTER} from '../capabilities/modules'
7
+ import {decodeCraftedItemStats} from '../derivation/crafting'
8
+ import {deriveResourceStats} from '../derivation/stratum'
9
+ import {getStatDefinitions} from '../derivation/stats'
10
+ import {computeShipHullCapabilities, computeEngineCapabilities, computeGeneratorCapabilities, computeExtractorCapabilities, computeLoaderCapabilities, computeManufacturingCapabilities} from '../entities/ship-deploy'
11
+ import {computeContainerCapabilities} from '../entities/container'
12
+ import {categoryColors, categoryIcons, componentIcon, moduleIcon} from '../data/colors'
13
+ import {ServerContract} from '../contracts'
14
+
15
+ export interface ResolvedItemStat {
16
+ key: string
17
+ label: string
18
+ abbreviation: string
19
+ value: number
20
+ color: string
21
+ category: ResourceCategory
22
+ inverted?: boolean
23
+ }
24
+
25
+ export interface ResolvedAttributeGroup {
26
+ capability: string
27
+ attributes: {label: string; value: number}[]
28
+ }
29
+
30
+ export type ResolvedItemType = 'resource' | 'component' | 'module' | 'entity'
31
+
32
+ export interface ResolvedItem {
33
+ itemId: number
34
+ name: string
35
+ icon: string
36
+ category?: ResourceCategory
37
+ tier: ResourceTier
38
+ mass: number
39
+ itemType: ResolvedItemType
40
+ stats?: ResolvedItemStat[]
41
+ attributes?: ResolvedAttributeGroup[]
42
+ }
43
+
44
+ function toNum(v: UInt16Type): number {
45
+ return Number(UInt16.from(v).value.toString())
46
+ }
47
+
48
+ function toBigSeed(v: UInt64Type): bigint {
49
+ return BigInt(UInt64.from(v).toString())
50
+ }
51
+
52
+ function resolveResource(id: number, seed?: UInt64Type): ResolvedItem {
53
+ const item = getItem(id)
54
+ const cat = item.category
55
+ let stats: ResolvedItemStat[] | undefined
56
+ if (seed !== undefined) {
57
+ const derived = deriveResourceStats(toBigSeed(seed))
58
+ const defs = getStatDefinitions(cat)
59
+ const values = [derived.stat1, derived.stat2, derived.stat3]
60
+ stats = defs.map((d, i) => ({
61
+ key: d.key,
62
+ label: d.label,
63
+ abbreviation: d.abbreviation,
64
+ value: values[i] ?? 0,
65
+ color: categoryColors[cat],
66
+ category: cat,
67
+ inverted: d.inverted,
68
+ }))
69
+ }
70
+ return {
71
+ itemId: id,
72
+ name: String(item.name),
73
+ icon: categoryIcons[cat] ?? '⬡',
74
+ category: cat,
75
+ tier: item.tier,
76
+ mass: Number(item.mass.value.toString()),
77
+ itemType: 'resource',
78
+ stats,
79
+ }
80
+ }
81
+
82
+ function resolveComponent(id: number, seed?: UInt64Type): ResolvedItem {
83
+ const comp = getComponentById(id)!
84
+ let stats: ResolvedItemStat[] | undefined
85
+ if (seed !== undefined) {
86
+ const decoded = decodeCraftedItemStats(id, toBigSeed(seed))
87
+ stats = Object.entries(decoded).map(([key, value]) => {
88
+ const allDefs = getStatDefinitions('metal')
89
+ .concat(getStatDefinitions('precious'))
90
+ .concat(getStatDefinitions('gas'))
91
+ .concat(getStatDefinitions('mineral'))
92
+ .concat(getStatDefinitions('organic'))
93
+ const def = allDefs.find((d) => d.key === key)
94
+ const statDef = comp.stats.find((s) => s.key === key)
95
+ const cat = (statDef?.source ?? 'metal') as ResourceCategory
96
+ return {
97
+ key,
98
+ label: def?.label ?? key,
99
+ abbreviation: def?.abbreviation ?? key.slice(0, 3).toUpperCase(),
100
+ value,
101
+ color: categoryColors[cat],
102
+ category: cat,
103
+ inverted: def?.inverted,
104
+ }
105
+ })
106
+ }
107
+ return {
108
+ itemId: id,
109
+ name: comp.name,
110
+ icon: componentIcon,
111
+ tier: 't1' as ResourceTier,
112
+ mass: comp.mass,
113
+ itemType: 'component',
114
+ stats,
115
+ }
116
+ }
117
+
118
+ function computeCapabilityGroup(moduleType: number, stats: Record<string, number>): ResolvedAttributeGroup | undefined {
119
+ switch (moduleType) {
120
+ case MODULE_ENGINE: {
121
+ const caps = computeEngineCapabilities(stats)
122
+ return {capability: 'Engine', attributes: [
123
+ {label: 'Thrust', value: caps.thrust},
124
+ {label: 'Drain', value: caps.drain},
125
+ ]}
126
+ }
127
+ case MODULE_GENERATOR: {
128
+ const caps = computeGeneratorCapabilities(stats)
129
+ return {capability: 'Generator', attributes: [
130
+ {label: 'Capacity', value: caps.capacity},
131
+ {label: 'Recharge', value: caps.recharge},
132
+ ]}
133
+ }
134
+ case MODULE_EXTRACTOR: {
135
+ const caps = computeExtractorCapabilities(stats)
136
+ return {capability: 'Extractor', attributes: [
137
+ {label: 'Rate', value: caps.rate},
138
+ {label: 'Drain', value: caps.drain},
139
+ {label: 'Depth', value: caps.depth},
140
+ {label: 'Drill', value: caps.drill},
141
+ ]}
142
+ }
143
+ case MODULE_LOADER: {
144
+ const caps = computeLoaderCapabilities(stats)
145
+ return {capability: 'Loader', attributes: [
146
+ {label: 'Mass', value: caps.mass},
147
+ {label: 'Thrust', value: caps.thrust},
148
+ {label: 'Quantity', value: caps.quantity},
149
+ ]}
150
+ }
151
+ case MODULE_CRAFTER: {
152
+ const caps = computeManufacturingCapabilities(stats)
153
+ return {capability: 'Manufacturing', attributes: [
154
+ {label: 'Speed', value: caps.speed},
155
+ {label: 'Drain', value: caps.drain},
156
+ ]}
157
+ }
158
+ default:
159
+ return undefined
160
+ }
161
+ }
162
+
163
+ function resolveModule(id: number, seed?: UInt64Type): ResolvedItem {
164
+ const recipe = getModuleRecipeByItemId(id)!
165
+ let attributes: ResolvedAttributeGroup[] | undefined
166
+ if (seed !== undefined) {
167
+ const stats = decodeCraftedItemStats(id, toBigSeed(seed))
168
+ const modType = getModuleCapabilityType(id)
169
+ const group = computeCapabilityGroup(modType, stats)
170
+ if (group) attributes = [group]
171
+ }
172
+ return {
173
+ itemId: id,
174
+ name: recipe.name,
175
+ icon: moduleIcon,
176
+ tier: 't1' as ResourceTier,
177
+ mass: 0,
178
+ itemType: 'module',
179
+ attributes,
180
+ }
181
+ }
182
+
183
+ function resolveEntity(id: number, seed?: UInt64Type, modules?: ServerContract.Types.module_entry[]): ResolvedItem {
184
+ const recipe = getEntityRecipeByItemId(id)!
185
+ let attributes: ResolvedAttributeGroup[] | undefined
186
+ if (seed !== undefined) {
187
+ const stats = decodeCraftedItemStats(id, toBigSeed(seed))
188
+ attributes = []
189
+
190
+ const isShip = recipe.id === 'ship-t1'
191
+ if (isShip) {
192
+ const hullCaps = computeShipHullCapabilities(stats)
193
+ attributes.push({capability: 'Hull', attributes: [
194
+ {label: 'Mass', value: hullCaps.hullmass},
195
+ {label: 'Capacity', value: hullCaps.capacity},
196
+ ]})
197
+ } else {
198
+ const containerCaps = computeContainerCapabilities(stats)
199
+ attributes.push({capability: 'Hull', attributes: [
200
+ {label: 'Mass', value: containerCaps.hullmass},
201
+ {label: 'Capacity', value: containerCaps.capacity},
202
+ ]})
203
+ }
204
+
205
+ if (modules) {
206
+ for (const mod of modules) {
207
+ if (!mod.installed) continue
208
+ const modItemId = Number(mod.installed.item_id.value.toString())
209
+ const modSeed = BigInt(mod.installed.seed.toString())
210
+ const modStats = decodeCraftedItemStats(modItemId, modSeed)
211
+ const modType = getModuleCapabilityType(modItemId)
212
+ const group = computeCapabilityGroup(modType, modStats)
213
+ if (group) attributes.push(group)
214
+ }
215
+ }
216
+ }
217
+ return {
218
+ itemId: id,
219
+ name: recipe.name,
220
+ icon: componentIcon,
221
+ tier: 't1' as ResourceTier,
222
+ mass: 0,
223
+ itemType: 'entity',
224
+ attributes,
225
+ }
226
+ }
227
+
228
+ export function resolveItem(
229
+ itemId: UInt16Type,
230
+ seed?: UInt64Type,
231
+ modules?: ServerContract.Types.module_entry[]
232
+ ): ResolvedItem {
233
+ const id = toNum(itemId)
234
+
235
+ if (isModuleItem(id)) return resolveModule(id, seed)
236
+
237
+ if (getComponentById(id)) return resolveComponent(id, seed)
238
+
239
+ if (getEntityRecipeByItemId(id)) return resolveEntity(id, seed, modules)
240
+
241
+ return resolveResource(id, seed)
242
+ }
@@ -24,7 +24,7 @@ import {ServerContract} from '../contracts'
24
24
  import {
25
25
  CargoMassInfo,
26
26
  Distance,
27
- INITIAL_SHIP_MASS,
27
+ BASE_ORBITAL_MASS,
28
28
  MAX_ORBITAL_ALTITUDE,
29
29
  MIN_ORBITAL_ALTITUDE,
30
30
  PRECISION,
@@ -35,11 +35,11 @@ import {getItem} from '../market/items'
35
35
  import {hasSystem} from '../utils/system'
36
36
 
37
37
  export function calc_orbital_altitude(mass: number): number {
38
- if (mass <= INITIAL_SHIP_MASS) {
38
+ if (mass <= BASE_ORBITAL_MASS) {
39
39
  return MIN_ORBITAL_ALTITUDE
40
40
  }
41
41
 
42
- const ratio = mass / INITIAL_SHIP_MASS
42
+ const ratio = mass / BASE_ORBITAL_MASS
43
43
  const capRatio = 10.0
44
44
  let scale = Math.log(ratio) / Math.log(capRatio)
45
45
  scale = Math.min(scale, 1.0)
@@ -19,10 +19,10 @@ export interface Entity {
19
19
  }
20
20
 
21
21
  export type ShipEntity = Entity &
22
- MovementCapability &
23
- EnergyCapability &
22
+ Partial<MovementCapability> &
23
+ Partial<EnergyCapability> &
24
24
  StorageCapability &
25
- LoaderCapability &
25
+ Partial<LoaderCapability> &
26
26
  MassCapability &
27
27
  ScheduleCapability & {
28
28
  extractor?: ServerContract.Types.extractor_stats
package/src/types.ts CHANGED
@@ -12,19 +12,6 @@ import {ServerContract} from './contracts'
12
12
 
13
13
  export const PRECISION = 10000
14
14
 
15
- export const INITIAL_SHIP_GENERATOR_CAPACITY = 350
16
- export const INITIAL_SHIP_DRAIN = 25
17
- export const INITIAL_SHIP_ENERGY = 350
18
- export const INITIAL_SHIP_HULLMASS = 100000
19
- export const INITIAL_SHIP_CAPACITY = 500000
20
- export const INITIAL_SHIP_Z = 800
21
- export const INITIAL_SHIP_RECHARGE = 10
22
- export const INITIAL_SHIP_THRUST = 250
23
-
24
- export const INITIAL_LOADER_MASS = 1000
25
- export const INITIAL_LOADER_QUANTITY = 1
26
- export const INITIAL_LOADER_THRUST = 1
27
-
28
15
  export const WAREHOUSE_Z = 500
29
16
  export const INITIAL_WAREHOUSE_CAPACITY = 10000000
30
17
 
@@ -37,11 +24,7 @@ export const TRAVEL_MAX_DURATION = 86400
37
24
  export const MIN_ORBITAL_ALTITUDE = 800
38
25
  export const MAX_ORBITAL_ALTITUDE = 3000
39
26
 
40
- export const INITIAL_SHIP_MASS = 500000
41
-
42
- export const INITIAL_EXTRACTOR_RATE = 700
43
- export const INITIAL_EXTRACTOR_DRAIN = 2500
44
- export const INITIAL_EXTRACTOR_EFFICIENCY = 5000
27
+ export const BASE_ORBITAL_MASS = 100000
45
28
 
46
29
  export interface ShipLike {
47
30
  coordinates: ServerContract.Types.coordinates