@shipload/sdk 1.0.0-next.8 → 1.0.0-next.9

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.
@@ -2,12 +2,14 @@ import {Name} from '@wharfkit/antelope'
2
2
  import {
3
3
  ITEM_CONTAINER_T1_PACKED,
4
4
  ITEM_CONTAINER_T2_PACKED,
5
+ ITEM_EXTRACTOR_T1_PACKED,
5
6
  ITEM_SHIP_T1_PACKED,
6
7
  ITEM_WAREHOUSE_T1_PACKED,
7
8
  } from '../data/item-ids'
8
9
 
9
10
  export const ENTITY_SHIP = Name.from('ship')
10
11
  export const ENTITY_WAREHOUSE = Name.from('warehouse')
12
+ export const ENTITY_EXTRACTOR = Name.from('extractor')
11
13
  export const ENTITY_CONTAINER = Name.from('container')
12
14
 
13
15
  export enum EntityClass {
@@ -22,6 +24,7 @@ export function getEntityClass(entityType: Name | EntityTypeName): EntityClass {
22
24
  case 'container':
23
25
  return EntityClass.OrbitalVessel
24
26
  case 'warehouse':
27
+ case 'extractor':
25
28
  return EntityClass.PlanetaryStructure
26
29
  default:
27
30
  throw new Error(`Entity type has no class: ${typeName}`)
@@ -37,12 +40,14 @@ export function getPackedEntityType(itemId: number): Name | null {
37
40
  return ENTITY_CONTAINER
38
41
  case ITEM_WAREHOUSE_T1_PACKED:
39
42
  return ENTITY_WAREHOUSE
43
+ case ITEM_EXTRACTOR_T1_PACKED:
44
+ return ENTITY_EXTRACTOR
40
45
  default:
41
46
  return null
42
47
  }
43
48
  }
44
49
 
45
- export type EntityTypeName = 'ship' | 'warehouse' | 'container'
50
+ export type EntityTypeName = 'ship' | 'warehouse' | 'extractor' | 'container'
46
51
 
47
52
  export interface EntityTraits {
48
53
  typeName: Name
@@ -70,6 +75,15 @@ export const warehouseTraits: EntityTraits = {
70
75
  notFoundError: 'warehouse not found',
71
76
  }
72
77
 
78
+ export const extractorTraits: EntityTraits = {
79
+ typeName: ENTITY_EXTRACTOR,
80
+ isMovable: false,
81
+ hasEnergy: true,
82
+ hasLoaders: false,
83
+
84
+ notFoundError: 'extractor not found',
85
+ }
86
+
73
87
  export const containerTraits: EntityTraits = {
74
88
  typeName: ENTITY_CONTAINER,
75
89
  isMovable: true,
@@ -87,6 +101,8 @@ export function getEntityTraits(entityType: Name | EntityTypeName): EntityTraits
87
101
  return shipTraits
88
102
  case 'warehouse':
89
103
  return warehouseTraits
104
+ case 'extractor':
105
+ return extractorTraits
90
106
  case 'container':
91
107
  return containerTraits
92
108
  default:
@@ -102,6 +118,10 @@ export function isWarehouse(entity: {type?: Name}): boolean {
102
118
  return entity.type?.equals(ENTITY_WAREHOUSE) ?? false
103
119
  }
104
120
 
121
+ export function isExtractor(entity: {type?: Name}): boolean {
122
+ return entity.type?.equals(ENTITY_EXTRACTOR) ?? false
123
+ }
124
+
105
125
  export function isContainer(entity: {type?: Name}): boolean {
106
126
  return entity.type?.equals(ENTITY_CONTAINER) ?? false
107
127
  }
package/src/types.ts CHANGED
@@ -12,7 +12,7 @@ import {ServerContract} from './contracts'
12
12
  export const PRECISION = 10000
13
13
  export const CRAFT_ENERGY_DIVISOR = 150000
14
14
 
15
- export const WAREHOUSE_Z = 500
15
+ export const PLANETARY_STRUCTURE_Z = 0
16
16
 
17
17
  export const CONTAINER_Z = 300
18
18
 
@@ -1,151 +0,0 @@
1
- import type {ServerContract} from '../contracts'
2
- import {TaskType} from '../types'
3
-
4
- export type PredictedCargoTarget = {kind: 'existing'; rowId: bigint} | {kind: 'new'; label: string}
5
-
6
- export interface PredictedCargoAddition {
7
- item_id: number
8
- stats: bigint
9
- modules: ServerContract.Types.module_entry[]
10
- quantity: number
11
- target: PredictedCargoTarget
12
- }
13
-
14
- export interface TaskCargoEffect {
15
- additions: PredictedCargoAddition[]
16
- }
17
-
18
- interface StackState {
19
- item_id: number
20
- stats: bigint
21
- modules: ServerContract.Types.module_entry[]
22
- quantity: number
23
- target: PredictedCargoTarget
24
- }
25
-
26
- function modulesEqual(
27
- a: ServerContract.Types.module_entry[],
28
- b: ServerContract.Types.module_entry[]
29
- ): boolean {
30
- if (a.length !== b.length) return false
31
- for (let i = 0; i < a.length; i++) {
32
- const ai = a[i]
33
- const bi = b[i]
34
- if (Number(ai.type) !== Number(bi.type)) return false
35
- const aInst = ai.installed
36
- const bInst = bi.installed
37
- if (!aInst && !bInst) continue
38
- if (!aInst || !bInst) return false
39
- if (Number(aInst.item_id) !== Number(bInst.item_id)) return false
40
- if (BigInt(aInst.stats.toString()) !== BigInt(bInst.stats.toString())) return false
41
- }
42
- return true
43
- }
44
-
45
- function findStack(
46
- state: StackState[],
47
- item_id: number,
48
- stats: bigint,
49
- modules: ServerContract.Types.module_entry[]
50
- ): number {
51
- for (let i = 0; i < state.length; i++) {
52
- const s = state[i]
53
- if (s.item_id !== item_id) continue
54
- if (s.stats !== stats) continue
55
- if (!modulesEqual(s.modules, modules)) continue
56
- return i
57
- }
58
- return -1
59
- }
60
-
61
- function applyAddition(
62
- state: StackState[],
63
- item: ServerContract.Types.cargo_item,
64
- nextNewLabel: () => string
65
- ): PredictedCargoAddition {
66
- const item_id = Number(item.item_id)
67
- const stats = BigInt(item.stats.toString())
68
- const modules = item.modules ?? []
69
- const quantity = Number(item.quantity)
70
-
71
- const idx = findStack(state, item_id, stats, modules)
72
- if (idx === -1) {
73
- const target: PredictedCargoTarget = {kind: 'new', label: nextNewLabel()}
74
- state.push({item_id, stats, modules, quantity, target})
75
- return {item_id, stats, modules, quantity, target}
76
- }
77
- state[idx].quantity += quantity
78
- return {item_id, stats, modules, quantity, target: state[idx].target}
79
- }
80
-
81
- function applyRemoval(state: StackState[], item: ServerContract.Types.cargo_item): void {
82
- const item_id = Number(item.item_id)
83
- const stats = BigInt(item.stats.toString())
84
- const modules = item.modules ?? []
85
- const quantity = Number(item.quantity)
86
-
87
- const idx = findStack(state, item_id, stats, modules)
88
- if (idx === -1) return
89
- state[idx].quantity -= quantity
90
- if (state[idx].quantity <= 0) state.splice(idx, 1)
91
- }
92
-
93
- export function predictTaskCargoEffects(
94
- cargo: readonly ServerContract.Types.cargo_view[],
95
- tasks: readonly ServerContract.Types.task[]
96
- ): TaskCargoEffect[] {
97
- const state: StackState[] = cargo.map((c) => ({
98
- item_id: Number(c.item_id),
99
- stats: BigInt(c.stats.toString()),
100
- modules: c.modules ?? [],
101
- quantity: Number(c.quantity),
102
- target: {kind: 'existing', rowId: BigInt(c.id.toString())},
103
- }))
104
-
105
- let newCounter = 0
106
- const nextNewLabel = () => {
107
- newCounter += 1
108
- return `new#${newCounter}`
109
- }
110
-
111
- const effects: TaskCargoEffect[] = []
112
-
113
- for (const task of tasks) {
114
- const type = Number(task.type)
115
- const items = task.cargo ?? []
116
- const additions: PredictedCargoAddition[] = []
117
-
118
- switch (type) {
119
- case TaskType.LOAD:
120
- case TaskType.UNWRAP: {
121
- for (const item of items) additions.push(applyAddition(state, item, nextNewLabel))
122
- break
123
- }
124
- case TaskType.GATHER: {
125
- if (!task.entitytarget) {
126
- for (const item of items)
127
- additions.push(applyAddition(state, item, nextNewLabel))
128
- }
129
- break
130
- }
131
- case TaskType.UNLOAD:
132
- case TaskType.WRAP: {
133
- for (const item of items) applyRemoval(state, item)
134
- break
135
- }
136
- case TaskType.CRAFT: {
137
- if (items.length > 0) {
138
- for (let i = 0; i < items.length - 1; i++) applyRemoval(state, items[i])
139
- additions.push(applyAddition(state, items[items.length - 1], nextNewLabel))
140
- }
141
- break
142
- }
143
- default:
144
- break
145
- }
146
-
147
- effects.push({additions})
148
- }
149
-
150
- return effects
151
- }