@shipload/sdk 0.7.1 → 2.0.0-rc10

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 (79) hide show
  1. package/lib/shipload.d.ts +2203 -288
  2. package/lib/shipload.js +8441 -2210
  3. package/lib/shipload.js.map +1 -1
  4. package/lib/shipload.m.js +8160 -2167
  5. package/lib/shipload.m.js.map +1 -1
  6. package/package.json +7 -6
  7. package/src/capabilities/crafting.ts +26 -0
  8. package/src/capabilities/extraction.ts +36 -0
  9. package/src/capabilities/guards.ts +38 -0
  10. package/src/capabilities/hauling.ts +22 -0
  11. package/src/capabilities/index.ts +8 -0
  12. package/src/capabilities/loading.ts +8 -0
  13. package/src/capabilities/modules.ts +57 -0
  14. package/src/capabilities/movement.ts +29 -0
  15. package/src/capabilities/storage.ts +72 -0
  16. package/src/contracts/server.ts +1217 -254
  17. package/src/data/capabilities.ts +408 -0
  18. package/src/data/categories.ts +58 -0
  19. package/src/data/colors.ts +52 -0
  20. package/src/data/items.json +17 -0
  21. package/src/data/locations.ts +53 -0
  22. package/src/data/nebula-adjectives.json +211 -0
  23. package/src/data/nebula-nouns.json +151 -0
  24. package/src/data/recipes.ts +571 -0
  25. package/src/data/syllables.json +1790 -0
  26. package/src/data/tiers.ts +45 -0
  27. package/src/derivation/crafting.ts +197 -0
  28. package/src/derivation/index.ts +28 -0
  29. package/src/derivation/location-size.ts +15 -0
  30. package/src/derivation/resources.ts +142 -0
  31. package/src/derivation/stats.ts +146 -0
  32. package/src/derivation/stratum.ts +118 -0
  33. package/src/entities/cargo-utils.ts +84 -0
  34. package/src/entities/container.ts +106 -0
  35. package/src/entities/entity-inventory.ts +39 -0
  36. package/src/entities/gamestate.ts +152 -0
  37. package/src/entities/inventory-accessor.ts +42 -0
  38. package/src/entities/location.ts +60 -0
  39. package/src/entities/makers.ts +72 -0
  40. package/src/entities/player.ts +15 -0
  41. package/src/entities/ship-deploy.ts +263 -0
  42. package/src/entities/ship.ts +199 -0
  43. package/src/entities/warehouse.ts +91 -0
  44. package/src/errors.ts +46 -9
  45. package/src/index-module.ts +302 -7
  46. package/src/managers/actions.ts +197 -0
  47. package/src/managers/base.ts +25 -0
  48. package/src/managers/context.ts +95 -0
  49. package/src/managers/entities.ts +103 -0
  50. package/src/managers/epochs.ts +47 -0
  51. package/src/managers/index.ts +8 -0
  52. package/src/managers/locations.ts +39 -0
  53. package/src/managers/players.ts +13 -0
  54. package/src/market/items.ts +30 -0
  55. package/src/nft/description.ts +175 -0
  56. package/src/nft/deserializers.ts +81 -0
  57. package/src/nft/index.ts +2 -0
  58. package/src/resolution/resolve-item.ts +313 -0
  59. package/src/scheduling/accessor.ts +82 -0
  60. package/src/{epoch.ts → scheduling/epoch.ts} +1 -1
  61. package/src/scheduling/projection.ts +322 -0
  62. package/src/scheduling/schedule.ts +179 -0
  63. package/src/shipload.ts +36 -159
  64. package/src/travel/travel.ts +499 -0
  65. package/src/types/capabilities.ts +71 -0
  66. package/src/types/entity-traits.ts +69 -0
  67. package/src/types/entity.ts +39 -0
  68. package/src/types/index.ts +3 -0
  69. package/src/types.ts +113 -35
  70. package/src/{hash.ts → utils/hash.ts} +1 -1
  71. package/src/utils/system.ts +155 -0
  72. package/src/goods.ts +0 -124
  73. package/src/market.ts +0 -214
  74. package/src/rolls.ts +0 -8
  75. package/src/ship.ts +0 -36
  76. package/src/state.ts +0 -0
  77. package/src/syllables.ts +0 -1184
  78. package/src/system.ts +0 -37
  79. package/src/travel.ts +0 -259
@@ -0,0 +1,499 @@
1
+ /**
2
+ * Travel calculations for ship movement, energy usage, and flight times.
3
+ *
4
+ * Functions prefixed with `calc_` are contract-parity functions that mirror
5
+ * the C++ implementation in the server contract (schedule.cpp, ship.cpp).
6
+ * These use snake_case intentionally to match the contract naming convention
7
+ * and signal that they must produce identical results to the on-chain code.
8
+ *
9
+ * Functions prefixed with `calculate` are higher-level SDK helpers that may
10
+ * combine multiple contract calculations for convenience.
11
+ */
12
+
13
+ import {
14
+ Checksum256,
15
+ Int64,
16
+ Int64Type,
17
+ UInt16,
18
+ UInt32,
19
+ UInt32Type,
20
+ UInt64,
21
+ UInt64Type,
22
+ } from '@wharfkit/antelope'
23
+
24
+ import {ServerContract} from '../contracts'
25
+ import {
26
+ BASE_ORBITAL_MASS,
27
+ CargoMassInfo,
28
+ Distance,
29
+ MAX_ORBITAL_ALTITUDE,
30
+ MIN_ORBITAL_ALTITUDE,
31
+ PRECISION,
32
+ ShipLike,
33
+ TaskType,
34
+ } from '../types'
35
+ import {getItem} from '../market/items'
36
+ import {hasSystem} from '../utils/system'
37
+
38
+ export function calc_orbital_altitude(mass: number): number {
39
+ if (mass <= BASE_ORBITAL_MASS) {
40
+ return MIN_ORBITAL_ALTITUDE
41
+ }
42
+
43
+ const ratio = mass / BASE_ORBITAL_MASS
44
+ const capRatio = 10.0
45
+ let scale = Math.log(ratio) / Math.log(capRatio)
46
+ scale = Math.min(scale, 1.0)
47
+
48
+ return MIN_ORBITAL_ALTITUDE + Math.floor((MAX_ORBITAL_ALTITUDE - MIN_ORBITAL_ALTITUDE) * scale)
49
+ }
50
+
51
+ export function distanceBetweenCoordinates(
52
+ origin: ServerContract.ActionParams.Type.coordinates,
53
+ destination: ServerContract.ActionParams.Type.coordinates
54
+ ): UInt64 {
55
+ return distanceBetweenPoints(origin.x, origin.y, destination.x, destination.y)
56
+ }
57
+
58
+ export function distanceBetweenPoints(
59
+ x1: Int64Type,
60
+ y1: Int64Type,
61
+ x2: Int64Type,
62
+ y2: Int64Type
63
+ ): UInt64 {
64
+ const x = Math.pow(x1 - x2, 2)
65
+ const y = Math.pow(y1 - y2, 2)
66
+ return UInt64.from(Math.sqrt(x + y) * PRECISION)
67
+ }
68
+
69
+ export function lerp(
70
+ origin: ServerContract.ActionParams.Type.coordinates,
71
+ destination: ServerContract.ActionParams.Type.coordinates,
72
+ time: number
73
+ ): ServerContract.ActionParams.Type.coordinates {
74
+ return {
75
+ x: (1 - time) * Number(origin.x) + time * Number(destination.x),
76
+ y: (1 - time) * Number(origin.y) + time * Number(destination.y),
77
+ }
78
+ }
79
+
80
+ export function rotation(
81
+ origin: ServerContract.ActionParams.Type.coordinates,
82
+ destination: ServerContract.ActionParams.Type.coordinates
83
+ ) {
84
+ return Math.atan2(destination.y - origin.y, destination.x - origin.x) * (180 / Math.PI) + 90
85
+ }
86
+
87
+ export function findNearbyPlanets(
88
+ seed: Checksum256,
89
+ origin: ServerContract.ActionParams.Type.coordinates,
90
+ maxDistance: UInt64Type = 20 * PRECISION
91
+ ): Distance[] {
92
+ const nearbySystems: Distance[] = []
93
+
94
+ const max = UInt64.from(maxDistance / PRECISION)
95
+ const xMin = Int64.from(origin.x).subtracting(max)
96
+ const xMax = Int64.from(origin.x).adding(max)
97
+ const yMin = Int64.from(origin.y).subtracting(max)
98
+ const yMax = Int64.from(origin.y).adding(max)
99
+
100
+ for (let x = Number(xMin); x <= Number(xMax); x++) {
101
+ for (let y = Number(yMin); y <= Number(yMax); y++) {
102
+ const samePlace = x === Number(origin.x) && y === Number(origin.y)
103
+ if (!samePlace) {
104
+ const distance = distanceBetweenPoints(origin.x, origin.y, x, y)
105
+ if (Number(distance) <= Number(maxDistance)) {
106
+ const system = hasSystem(seed, {x, y})
107
+ if (system) {
108
+ nearbySystems.push({origin, destination: {x, y}, distance})
109
+ }
110
+ }
111
+ }
112
+ }
113
+ }
114
+
115
+ return nearbySystems
116
+ }
117
+
118
+ export function calc_rechargetime(
119
+ capacity: UInt32Type,
120
+ energy: UInt32Type,
121
+ recharge: UInt32Type
122
+ ): UInt32 {
123
+ const cap = UInt32.from(capacity)
124
+ const eng = UInt32.from(energy)
125
+ if (eng.gte(cap)) return UInt32.zero
126
+ return cap.subtracting(eng).dividing(recharge)
127
+ }
128
+
129
+ export function calc_ship_rechargetime(ship: ShipLike): UInt32 {
130
+ if (!ship.generator) return UInt32.from(0)
131
+ return calc_rechargetime(
132
+ ship.generator.capacity,
133
+ ship.energy ?? UInt16.from(0),
134
+ ship.generator.recharge
135
+ )
136
+ }
137
+
138
+ export function calc_flighttime(distance: UInt64Type, acceleration: number): UInt32 {
139
+ return UInt32.from(2 * Math.sqrt(Number(distance) / acceleration))
140
+ }
141
+
142
+ export function calc_loader_flighttime(ship: ShipLike, mass: UInt64, altitude?: number): UInt32 {
143
+ const z = altitude ?? ship.coordinates.z?.toNumber() ?? calc_orbital_altitude(Number(mass))
144
+ return calc_flighttime(z, calc_loader_acceleration(ship, mass))
145
+ }
146
+
147
+ export function calc_loader_acceleration(ship: ShipLike, mass: UInt64): number {
148
+ const thrust = ship.loaders ? Number(ship.loaders.thrust) : 0
149
+ const loaderMass = ship.loaders ? Number(ship.loaders.mass) : 0
150
+ return calc_acceleration(thrust, Number(mass) + loaderMass)
151
+ }
152
+
153
+ export function calc_ship_flighttime(ship: ShipLike, mass: UInt64, distance: UInt64): UInt32 {
154
+ const acceleration = calc_ship_acceleration(ship, mass)
155
+ return calc_flighttime(distance, acceleration)
156
+ }
157
+
158
+ export function calc_ship_acceleration(ship: ShipLike, mass: UInt64): number {
159
+ const thrust = ship.engines ? Number(ship.engines.thrust) : 0
160
+ return calc_acceleration(thrust, Number(mass))
161
+ }
162
+
163
+ export function calc_acceleration(thrust: number, mass: number): number {
164
+ return (thrust / mass) * PRECISION
165
+ }
166
+
167
+ export function calc_ship_mass(ship: ShipLike, cargos: CargoMassInfo[]): UInt64 {
168
+ const mass = UInt64.from(0)
169
+
170
+ mass.add(ship.hullmass)
171
+
172
+ if (ship.loaders && ship.loaders.quantity.gt(UInt32.zero)) {
173
+ mass.add(ship.loaders.mass.multiplying(ship.loaders.quantity))
174
+ }
175
+
176
+ for (const cargo of cargos) {
177
+ mass.add(getItem(cargo.item_id).mass.multiplying(cargo.quantity))
178
+ }
179
+
180
+ return mass
181
+ }
182
+
183
+ export function calc_energyusage(distance: UInt64Type, drain: UInt32Type): UInt32 {
184
+ return UInt64.from(distance).dividing(PRECISION).multiplying(drain)
185
+ }
186
+
187
+ export function calculateTransferTime(
188
+ ship: ShipLike,
189
+ cargos: CargoMassInfo[],
190
+ quantities?: Map<number, number>
191
+ ): UInt32 {
192
+ let mass = UInt64.from(0)
193
+
194
+ for (const cargo of cargos) {
195
+ const qty = quantities?.get(Number(cargo.item_id)) ?? 0
196
+ if (qty > 0) {
197
+ const good_mass = getItem(cargo.item_id).mass
198
+ const cargo_mass = good_mass.multiplying(qty)
199
+ mass = UInt64.from(mass).adding(cargo_mass)
200
+ }
201
+ }
202
+
203
+ if (mass.equals(UInt64.zero)) {
204
+ return UInt32.from(0)
205
+ }
206
+
207
+ if (!ship.loaders) return UInt32.from(0)
208
+ mass = UInt64.from(mass).adding(ship.loaders.mass)
209
+ const transfer_time = calc_loader_flighttime(ship, mass)
210
+ return transfer_time.dividing(ship.loaders.quantity)
211
+ }
212
+
213
+ export function calculateRefuelingTime(ship: ShipLike): UInt32 {
214
+ return calc_ship_rechargetime(ship)
215
+ }
216
+
217
+ export function calculateFlightTime(
218
+ ship: ShipLike,
219
+ cargos: CargoMassInfo[],
220
+ distance: UInt64Type
221
+ ): UInt32 {
222
+ const mass = calc_ship_mass(ship, cargos)
223
+ return calc_ship_flighttime(ship, mass, distance)
224
+ }
225
+
226
+ export interface LoadTimeBreakdown {
227
+ unloadTime: number
228
+ loadTime: number
229
+ totalTime: number
230
+ unloadMass: number
231
+ loadMass: number
232
+ }
233
+
234
+ export function calculateLoadTimeBreakdown(
235
+ ship: ShipLike,
236
+ cargos: CargoMassInfo[],
237
+ loadQuantities?: Map<number, number>,
238
+ unloadQuantities?: Map<number, number>
239
+ ): LoadTimeBreakdown {
240
+ let mass_unload = UInt64.from(0)
241
+ let mass_load = UInt64.from(0)
242
+
243
+ for (const cargo of cargos) {
244
+ const goodId = Number(cargo.item_id)
245
+ const loadQty = loadQuantities?.get(goodId) ?? 0
246
+ const unloadQty = unloadQuantities?.get(goodId) ?? 0
247
+
248
+ if (loadQty > 0 || unloadQty > 0) {
249
+ const good = getItem(cargo.item_id)
250
+
251
+ if (loadQty > 0) {
252
+ const cargo_mass = good.mass.multiplying(loadQty)
253
+ mass_load = UInt64.from(mass_load).adding(cargo_mass)
254
+ }
255
+ if (unloadQty > 0) {
256
+ const cargo_mass = good.mass.multiplying(unloadQty)
257
+ mass_unload = UInt64.from(mass_unload).adding(cargo_mass)
258
+ }
259
+ }
260
+ }
261
+
262
+ let unloadTime = 0
263
+ let loadTime = 0
264
+
265
+ if (mass_unload.gt(UInt64.zero) && ship.loaders) {
266
+ const totalMass = UInt64.from(mass_unload).adding(ship.loaders.mass)
267
+ unloadTime = Number(calc_loader_flighttime(ship, totalMass))
268
+ }
269
+
270
+ if (mass_load.gt(UInt64.zero) && ship.loaders) {
271
+ const totalMass = UInt64.from(mass_load).adding(ship.loaders.mass)
272
+ loadTime = Number(calc_loader_flighttime(ship, totalMass))
273
+ }
274
+
275
+ const numLoaders = ship.loaders ? Number(ship.loaders.quantity) : 0
276
+ const totalTime = numLoaders > 0 ? (unloadTime + loadTime) / numLoaders : 0
277
+ const unloadTimePerLoader = numLoaders > 0 ? unloadTime / numLoaders : 0
278
+ const loadTimePerLoader = numLoaders > 0 ? loadTime / numLoaders : 0
279
+
280
+ return {
281
+ unloadTime: unloadTimePerLoader,
282
+ loadTime: loadTimePerLoader,
283
+ totalTime,
284
+ unloadMass: Number(mass_unload),
285
+ loadMass: Number(mass_load),
286
+ }
287
+ }
288
+
289
+ export interface EstimatedTravelTime {
290
+ flightTime: UInt32
291
+ rechargeTime: UInt32
292
+ loadTime: UInt32
293
+ unloadTime: UInt32
294
+ total: UInt32
295
+ }
296
+
297
+ export interface EstimateTravelTimeOptions {
298
+ needsRecharge?: boolean
299
+ loadMass?: UInt32Type
300
+ unloadMass?: UInt32Type
301
+ }
302
+
303
+ export function estimateTravelTime(
304
+ ship: ShipLike,
305
+ travelMass: UInt64Type,
306
+ distance: UInt64Type,
307
+ options: EstimateTravelTimeOptions = {}
308
+ ): EstimatedTravelTime {
309
+ const {needsRecharge = false, loadMass, unloadMass} = options
310
+
311
+ const flightTime = calc_ship_flighttime(ship, UInt64.from(travelMass), UInt64.from(distance))
312
+ const rechargeTime = needsRecharge ? calc_ship_rechargetime(ship) : UInt32.zero
313
+
314
+ let loadTime = UInt32.zero
315
+ let unloadTime = UInt32.zero
316
+
317
+ if (
318
+ loadMass &&
319
+ UInt32.from(loadMass).gt(UInt32.zero) &&
320
+ ship.loaders &&
321
+ ship.loaders.quantity.gt(UInt32.zero)
322
+ ) {
323
+ const totalMass = UInt64.from(loadMass).adding(ship.loaders.mass)
324
+ loadTime = calc_loader_flighttime(ship, totalMass).dividing(ship.loaders.quantity)
325
+ }
326
+
327
+ if (
328
+ unloadMass &&
329
+ UInt32.from(unloadMass).gt(UInt32.zero) &&
330
+ ship.loaders &&
331
+ ship.loaders.quantity.gt(UInt32.zero)
332
+ ) {
333
+ const totalMass = UInt64.from(unloadMass).adding(ship.loaders.mass)
334
+ unloadTime = calc_loader_flighttime(ship, totalMass).dividing(ship.loaders.quantity)
335
+ }
336
+
337
+ return {
338
+ flightTime,
339
+ rechargeTime,
340
+ loadTime,
341
+ unloadTime,
342
+ total: flightTime.adding(rechargeTime).adding(loadTime).adding(unloadTime),
343
+ }
344
+ }
345
+
346
+ export function estimateDealTravelTime(
347
+ ship: ShipLike,
348
+ shipMass: UInt64Type,
349
+ distance: UInt64Type,
350
+ loadMass: UInt32Type
351
+ ): UInt32 {
352
+ const needsRecharge = !hasEnergyForDistance(ship, distance)
353
+ const estimate = estimateTravelTime(ship, shipMass, distance, {
354
+ needsRecharge,
355
+ loadMass,
356
+ })
357
+ return estimate.total
358
+ }
359
+
360
+ export function hasEnergyForDistance(ship: ShipLike, distance: UInt64Type): boolean {
361
+ if (!ship.engines) return false
362
+ const energyNeeded = UInt64.from(distance).dividing(PRECISION).multiplying(ship.engines.drain)
363
+ return UInt64.from(ship.energy ?? 0).gte(energyNeeded)
364
+ }
365
+
366
+ export interface TransferEntity {
367
+ location: {z?: {toNumber(): number} | number}
368
+ loaders?: {
369
+ thrust: {toNumber(): number} | number
370
+ mass: {toNumber(): number} | number
371
+ quantity: {toNumber(): number} | number
372
+ }
373
+ }
374
+
375
+ export interface HasScheduleAndLocation {
376
+ coordinates: ServerContract.ActionParams.Type.coordinates
377
+ schedule?: ServerContract.Types.schedule
378
+ }
379
+
380
+ export function getFlightOrigin(
381
+ entity: HasScheduleAndLocation,
382
+ flightTaskIndex: number
383
+ ): ServerContract.ActionParams.Type.coordinates {
384
+ if (!entity.schedule) return entity.coordinates
385
+
386
+ let origin = entity.coordinates
387
+ for (let i = 0; i < flightTaskIndex && i < entity.schedule.tasks.length; i++) {
388
+ const task = entity.schedule.tasks[i]
389
+ if (task.type.equals(TaskType.TRAVEL) && task.coordinates) {
390
+ origin = task.coordinates
391
+ }
392
+ }
393
+ return origin
394
+ }
395
+
396
+ export function getDestinationLocation(
397
+ entity: HasScheduleAndLocation
398
+ ): ServerContract.ActionParams.Type.coordinates | undefined {
399
+ if (!entity.schedule) return undefined
400
+
401
+ for (let i = entity.schedule.tasks.length - 1; i >= 0; i--) {
402
+ const task = entity.schedule.tasks[i]
403
+ if (task.type.equals(TaskType.TRAVEL) && task.coordinates) {
404
+ return task.coordinates
405
+ }
406
+ }
407
+ return undefined
408
+ }
409
+
410
+ export function getPositionAt(
411
+ entity: HasScheduleAndLocation,
412
+ taskIndex: number,
413
+ taskProgress: number
414
+ ): ServerContract.ActionParams.Type.coordinates {
415
+ if (!entity.schedule || entity.schedule.tasks.length === 0 || taskIndex < 0) {
416
+ return entity.coordinates
417
+ }
418
+
419
+ const task = entity.schedule.tasks[taskIndex]
420
+
421
+ if (!task.type.equals(TaskType.TRAVEL) || !task.coordinates) {
422
+ return getFlightOrigin(entity, taskIndex)
423
+ }
424
+
425
+ const origin = getFlightOrigin(entity, taskIndex)
426
+ const destination = task.coordinates
427
+
428
+ const interpolated = lerp(origin, destination, taskProgress)
429
+ return {
430
+ x: Math.round(interpolated.x),
431
+ y: Math.round(interpolated.y),
432
+ }
433
+ }
434
+
435
+ export function calc_transfer_duration(
436
+ source: TransferEntity,
437
+ dest: TransferEntity,
438
+ cargoMass: number
439
+ ): number {
440
+ if (cargoMass === 0) {
441
+ return 0
442
+ }
443
+
444
+ let totalThrust = 0
445
+ let totalLoaderMass = 0
446
+ let totalQuantity = 0
447
+
448
+ if (source.loaders) {
449
+ const thrust =
450
+ typeof source.loaders.thrust === 'number'
451
+ ? source.loaders.thrust
452
+ : source.loaders.thrust.toNumber()
453
+ const mass =
454
+ typeof source.loaders.mass === 'number'
455
+ ? source.loaders.mass
456
+ : source.loaders.mass.toNumber()
457
+ const qty =
458
+ typeof source.loaders.quantity === 'number'
459
+ ? source.loaders.quantity
460
+ : source.loaders.quantity.toNumber()
461
+ totalThrust += thrust * qty
462
+ totalLoaderMass += mass * qty
463
+ totalQuantity += qty
464
+ }
465
+
466
+ if (dest.loaders) {
467
+ const thrust =
468
+ typeof dest.loaders.thrust === 'number'
469
+ ? dest.loaders.thrust
470
+ : dest.loaders.thrust.toNumber()
471
+ const mass =
472
+ typeof dest.loaders.mass === 'number' ? dest.loaders.mass : dest.loaders.mass.toNumber()
473
+ const qty =
474
+ typeof dest.loaders.quantity === 'number'
475
+ ? dest.loaders.quantity
476
+ : dest.loaders.quantity.toNumber()
477
+ totalThrust += thrust * qty
478
+ totalLoaderMass += mass * qty
479
+ totalQuantity += qty
480
+ }
481
+
482
+ if (totalThrust === 0 || totalQuantity === 0) {
483
+ return 0
484
+ }
485
+
486
+ const sourceZ =
487
+ typeof source.location.z === 'number'
488
+ ? source.location.z
489
+ : source.location.z?.toNumber() ?? 0
490
+ const destZ =
491
+ typeof dest.location.z === 'number' ? dest.location.z : dest.location.z?.toNumber() ?? 0
492
+ const distance = Math.abs(sourceZ - destZ)
493
+
494
+ const totalMass = cargoMass + totalLoaderMass
495
+ const acceleration = calc_acceleration(totalThrust, totalMass)
496
+ const flightTime = 2 * Math.sqrt(distance / acceleration)
497
+
498
+ return Math.floor(flightTime / totalQuantity)
499
+ }
@@ -0,0 +1,71 @@
1
+ import {Name, UInt16, UInt32} from '@wharfkit/antelope'
2
+ import {ServerContract} from '../contracts'
3
+
4
+ export interface MovementCapability {
5
+ engines: ServerContract.Types.movement_stats
6
+ generator: ServerContract.Types.energy_stats
7
+ }
8
+
9
+ export interface EnergyCapability {
10
+ energy: UInt16
11
+ }
12
+
13
+ export interface StorageCapability {
14
+ capacity: UInt32
15
+ cargomass: UInt32
16
+ cargo: ServerContract.Types.cargo_item[]
17
+ }
18
+
19
+ export interface LoaderCapability {
20
+ loaders: ServerContract.Types.loader_stats
21
+ }
22
+
23
+ export interface ExtractorCapability {
24
+ extractor: ServerContract.Types.extractor_stats
25
+ }
26
+
27
+ export interface MassCapability {
28
+ hullmass: UInt32
29
+ }
30
+
31
+ export interface ScheduleCapability {
32
+ schedule?: ServerContract.Types.schedule
33
+ }
34
+
35
+ export interface EntityCapabilities {
36
+ hullmass?: UInt32
37
+ capacity?: UInt32
38
+ engines?: ServerContract.Types.movement_stats
39
+ generator?: ServerContract.Types.energy_stats
40
+ loaders?: ServerContract.Types.loader_stats
41
+ extractor?: ServerContract.Types.extractor_stats
42
+ crafter?: ServerContract.Types.crafter_stats
43
+ }
44
+
45
+ export interface EntityState {
46
+ owner: Name
47
+ location: ServerContract.Types.coordinates
48
+ energy?: UInt16
49
+ cargomass: UInt32
50
+ cargo: ServerContract.Types.cargo_item[]
51
+ }
52
+
53
+ export function capsHasMovement(caps: EntityCapabilities): boolean {
54
+ return caps.engines !== undefined && caps.generator !== undefined
55
+ }
56
+
57
+ export function capsHasStorage(caps: EntityCapabilities): boolean {
58
+ return caps.capacity !== undefined
59
+ }
60
+
61
+ export function capsHasLoaders(caps: EntityCapabilities): boolean {
62
+ return caps.loaders !== undefined
63
+ }
64
+
65
+ export function capsHasExtractor(caps: EntityCapabilities): boolean {
66
+ return caps.extractor !== undefined
67
+ }
68
+
69
+ export function capsHasMass(caps: EntityCapabilities): boolean {
70
+ return caps.hullmass !== undefined
71
+ }
@@ -0,0 +1,69 @@
1
+ import {Name} from '@wharfkit/antelope'
2
+
3
+ export const ENTITY_SHIP = Name.from('ship')
4
+ export const ENTITY_WAREHOUSE = Name.from('warehouse')
5
+ export const ENTITY_CONTAINER = Name.from('container')
6
+
7
+ export type EntityTypeName = 'ship' | 'warehouse' | 'container'
8
+
9
+ export interface EntityTraits {
10
+ typeName: Name
11
+ isMovable: boolean
12
+ hasEnergy: boolean
13
+ hasLoaders: boolean
14
+ notFoundError: string
15
+ }
16
+
17
+ export const shipTraits: EntityTraits = {
18
+ typeName: ENTITY_SHIP,
19
+ isMovable: true,
20
+ hasEnergy: true,
21
+ hasLoaders: true,
22
+
23
+ notFoundError: 'ship not found',
24
+ }
25
+
26
+ export const warehouseTraits: EntityTraits = {
27
+ typeName: ENTITY_WAREHOUSE,
28
+ isMovable: false,
29
+ hasEnergy: false,
30
+ hasLoaders: true,
31
+
32
+ notFoundError: 'warehouse not found',
33
+ }
34
+
35
+ export const containerTraits: EntityTraits = {
36
+ typeName: ENTITY_CONTAINER,
37
+ isMovable: true,
38
+ hasEnergy: false,
39
+ hasLoaders: false,
40
+
41
+ notFoundError: 'container not found',
42
+ }
43
+
44
+ export function getEntityTraits(entityType: Name | EntityTypeName): EntityTraits {
45
+ const typeName = typeof entityType === 'string' ? entityType : entityType.toString()
46
+
47
+ switch (typeName) {
48
+ case 'ship':
49
+ return shipTraits
50
+ case 'warehouse':
51
+ return warehouseTraits
52
+ case 'container':
53
+ return containerTraits
54
+ default:
55
+ throw new Error(`Unknown entity type: ${typeName}`)
56
+ }
57
+ }
58
+
59
+ export function isShip(entity: {type?: Name}): boolean {
60
+ return entity.type?.equals(ENTITY_SHIP) ?? false
61
+ }
62
+
63
+ export function isWarehouse(entity: {type?: Name}): boolean {
64
+ return entity.type?.equals(ENTITY_WAREHOUSE) ?? false
65
+ }
66
+
67
+ export function isContainer(entity: {type?: Name}): boolean {
68
+ return entity.type?.equals(ENTITY_CONTAINER) ?? false
69
+ }
@@ -0,0 +1,39 @@
1
+ import {Name, UInt64} from '@wharfkit/antelope'
2
+ import {ServerContract} from '../contracts'
3
+ import {Coordinates} from '../types'
4
+ import {
5
+ EnergyCapability,
6
+ LoaderCapability,
7
+ MassCapability,
8
+ MovementCapability,
9
+ ScheduleCapability,
10
+ StorageCapability,
11
+ } from './capabilities'
12
+
13
+ export interface Entity {
14
+ id: UInt64
15
+ type: Name
16
+ owner: Name
17
+ entity_name: string
18
+ location: Coordinates | ServerContract.Types.coordinates
19
+ }
20
+
21
+ export type ShipEntity = Entity &
22
+ Partial<MovementCapability> &
23
+ Partial<EnergyCapability> &
24
+ StorageCapability &
25
+ Partial<LoaderCapability> &
26
+ MassCapability &
27
+ ScheduleCapability & {
28
+ extractor?: ServerContract.Types.extractor_stats
29
+ }
30
+
31
+ export type WarehouseEntity = Entity &
32
+ StorageCapability &
33
+ Partial<LoaderCapability> &
34
+ MassCapability &
35
+ ScheduleCapability
36
+
37
+ export type ContainerEntity = Entity & StorageCapability & MassCapability & ScheduleCapability
38
+
39
+ export type AnyEntity = ShipEntity | WarehouseEntity | ContainerEntity
@@ -0,0 +1,3 @@
1
+ export * from './capabilities'
2
+ export * from './entity'
3
+ export * from './entity-traits'