@shipload/sdk 1.0.0-next.3 → 1.0.0-next.31

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 (99) hide show
  1. package/lib/shipload.d.ts +1849 -961
  2. package/lib/shipload.js +9089 -4854
  3. package/lib/shipload.js.map +1 -1
  4. package/lib/shipload.m.js +8958 -4805
  5. package/lib/shipload.m.js.map +1 -1
  6. package/lib/testing.d.ts +856 -0
  7. package/lib/testing.js +3739 -0
  8. package/lib/testing.js.map +1 -0
  9. package/lib/testing.m.js +3733 -0
  10. package/lib/testing.m.js.map +1 -0
  11. package/package.json +15 -2
  12. package/src/capabilities/craftable.ts +51 -0
  13. package/src/capabilities/crafting.test.ts +7 -0
  14. package/src/capabilities/crafting.ts +3 -3
  15. package/src/capabilities/gathering.ts +17 -7
  16. package/src/capabilities/index.ts +0 -1
  17. package/src/capabilities/modules.ts +6 -0
  18. package/src/capabilities/storage.ts +16 -1
  19. package/src/contracts/platform.ts +231 -3
  20. package/src/contracts/server.ts +816 -471
  21. package/src/data/capabilities.ts +14 -329
  22. package/src/data/capability-formulas.ts +76 -0
  23. package/src/data/catalog.ts +0 -5
  24. package/src/data/colors.ts +14 -47
  25. package/src/data/entities.json +46 -10
  26. package/src/data/item-ids.ts +15 -12
  27. package/src/data/items.json +302 -38
  28. package/src/data/kind-registry.json +85 -0
  29. package/src/data/kind-registry.ts +150 -0
  30. package/src/data/metadata.ts +100 -31
  31. package/src/data/recipes-runtime.ts +3 -23
  32. package/src/data/recipes.json +250 -113
  33. package/src/derivation/build-methods.ts +45 -0
  34. package/src/derivation/capabilities.ts +415 -0
  35. package/src/derivation/capability-mappings.ts +117 -0
  36. package/src/derivation/crafting.ts +23 -24
  37. package/src/derivation/index.ts +17 -2
  38. package/src/derivation/reserve-regen.ts +34 -0
  39. package/src/derivation/resources.ts +125 -38
  40. package/src/derivation/stars.test.ts +51 -0
  41. package/src/derivation/stars.ts +15 -0
  42. package/src/derivation/stats.ts +6 -6
  43. package/src/derivation/stratum.ts +15 -19
  44. package/src/derivation/tiers.ts +28 -7
  45. package/src/entities/entity.ts +98 -0
  46. package/src/entities/gamestate.ts +3 -28
  47. package/src/entities/makers.ts +91 -136
  48. package/src/entities/slot-multiplier.ts +39 -0
  49. package/src/errors.ts +10 -15
  50. package/src/format.ts +26 -4
  51. package/src/index-module.ts +189 -47
  52. package/src/managers/actions.ts +252 -83
  53. package/src/managers/base.ts +6 -2
  54. package/src/managers/construction-types.ts +79 -0
  55. package/src/managers/construction.ts +396 -0
  56. package/src/managers/context.ts +11 -1
  57. package/src/managers/entities.ts +18 -66
  58. package/src/managers/epochs.ts +40 -0
  59. package/src/managers/index.ts +17 -1
  60. package/src/managers/locations.ts +25 -29
  61. package/src/managers/nft.ts +28 -0
  62. package/src/managers/plot.ts +127 -0
  63. package/src/nft/atomicassets.abi.json +1342 -0
  64. package/src/nft/atomicassets.ts +237 -0
  65. package/src/nft/atomicdata.ts +130 -0
  66. package/src/nft/buildImmutableData.ts +321 -0
  67. package/src/nft/description.ts +37 -15
  68. package/src/nft/index.ts +3 -0
  69. package/src/resolution/describe-module.ts +5 -8
  70. package/src/resolution/display-name.ts +38 -10
  71. package/src/resolution/resolve-item.ts +22 -20
  72. package/src/scheduling/accessor.ts +68 -22
  73. package/src/scheduling/availability.ts +108 -0
  74. package/src/scheduling/energy.ts +48 -0
  75. package/src/scheduling/lane-core.ts +130 -0
  76. package/src/scheduling/lanes.ts +60 -0
  77. package/src/scheduling/projection.ts +121 -94
  78. package/src/scheduling/schedule.ts +237 -103
  79. package/src/scheduling/task-cargo.ts +46 -0
  80. package/src/shipload.ts +16 -1
  81. package/src/subscriptions/manager.ts +40 -6
  82. package/src/subscriptions/mappers.ts +3 -8
  83. package/src/subscriptions/types.ts +3 -2
  84. package/src/testing/catalog-hash.ts +19 -0
  85. package/src/testing/index.ts +2 -0
  86. package/src/testing/projection-parity.ts +143 -0
  87. package/src/travel/travel.ts +90 -13
  88. package/src/types/capabilities.ts +1 -0
  89. package/src/types/index.ts +0 -1
  90. package/src/types.ts +19 -12
  91. package/src/utils/cargo.ts +27 -0
  92. package/src/utils/display-name.ts +70 -0
  93. package/src/utils/system.ts +25 -24
  94. package/src/capabilities/loading.ts +0 -8
  95. package/src/entities/container.ts +0 -108
  96. package/src/entities/ship-deploy.ts +0 -258
  97. package/src/entities/ship.ts +0 -204
  98. package/src/entities/warehouse.ts +0 -119
  99. package/src/types/entity-traits.ts +0 -69
@@ -0,0 +1,237 @@
1
+ import {
2
+ ABI,
3
+ type ABIDef,
4
+ Action,
5
+ type APIClient,
6
+ type NameType,
7
+ Name,
8
+ PermissionLevel,
9
+ UInt64,
10
+ } from '@wharfkit/antelope'
11
+ import {deserializeAtomicData, type SchemaField} from './atomicdata'
12
+ import {deserializeAsset, type NFTCargoItem, type NFTModuleSlot} from './deserializers'
13
+ import type {ImmutableEntry} from './buildImmutableData'
14
+ import atomicAssetsAbi from './atomicassets.abi.json'
15
+
16
+ const PLACEHOLDER_AUTH = PermissionLevel.from({
17
+ actor: '............1',
18
+ permission: '............2',
19
+ })
20
+
21
+ export const ATOMICASSETS_ACCOUNT = 'atomicassets'
22
+ export const SHIPLOAD_COLLECTION = 'shipload'
23
+
24
+ export const ATOMICASSETS_ABI = ABI.from(atomicAssetsAbi as ABIDef)
25
+
26
+ const ATOMIC_ATTRIBUTE_VARIANT_NAME =
27
+ 'variant_int8_int16_int32_int64_uint8_uint16_uint32_uint64_float32_float64_string_INT8_VEC_INT16_VEC_INT32_VEC_INT64_VEC_UINT8_VEC_UINT16_VEC_UINT32_VEC_UINT64_VEC_FLOAT_VEC_DOUBLE_VEC_STRING_VEC'
28
+
29
+ const MINTASSET_ABI_DEF: ABIDef = {
30
+ version: 'eosio::abi/1.2',
31
+ types: [
32
+ {new_type_name: 'ATOMIC_ATTRIBUTE', type: ATOMIC_ATTRIBUTE_VARIANT_NAME},
33
+ {new_type_name: 'ATTRIBUTE_MAP', type: 'pair_string_ATOMIC_ATTRIBUTE[]'},
34
+ {new_type_name: 'INT8_VEC', type: 'bytes'},
35
+ {new_type_name: 'INT16_VEC', type: 'int16[]'},
36
+ {new_type_name: 'INT32_VEC', type: 'int32[]'},
37
+ {new_type_name: 'INT64_VEC', type: 'int64[]'},
38
+ {new_type_name: 'UINT8_VEC', type: 'bytes'},
39
+ {new_type_name: 'UINT16_VEC', type: 'uint16[]'},
40
+ {new_type_name: 'UINT32_VEC', type: 'uint32[]'},
41
+ {new_type_name: 'UINT64_VEC', type: 'uint64[]'},
42
+ {new_type_name: 'FLOAT_VEC', type: 'float32[]'},
43
+ {new_type_name: 'DOUBLE_VEC', type: 'float64[]'},
44
+ {new_type_name: 'STRING_VEC', type: 'string[]'},
45
+ ],
46
+ structs: [
47
+ {
48
+ name: 'pair_string_ATOMIC_ATTRIBUTE',
49
+ base: '',
50
+ fields: [
51
+ {name: 'first', type: 'string'},
52
+ {name: 'second', type: 'ATOMIC_ATTRIBUTE'},
53
+ ],
54
+ },
55
+ {
56
+ name: 'mintasset',
57
+ base: '',
58
+ fields: [
59
+ {name: 'authorized_minter', type: 'name'},
60
+ {name: 'collection_name', type: 'name'},
61
+ {name: 'schema_name', type: 'name'},
62
+ {name: 'template_id', type: 'int32'},
63
+ {name: 'new_asset_owner', type: 'name'},
64
+ {name: 'immutable_data', type: 'ATTRIBUTE_MAP'},
65
+ {name: 'mutable_data', type: 'ATTRIBUTE_MAP'},
66
+ {name: 'tokens_to_back', type: 'asset[]'},
67
+ ],
68
+ },
69
+ ],
70
+ actions: [{name: 'mintasset', type: 'mintasset', ricardian_contract: ''}],
71
+ variants: [
72
+ {
73
+ name: ATOMIC_ATTRIBUTE_VARIANT_NAME,
74
+ types: [
75
+ 'int8',
76
+ 'int16',
77
+ 'int32',
78
+ 'int64',
79
+ 'uint8',
80
+ 'uint16',
81
+ 'uint32',
82
+ 'uint64',
83
+ 'float32',
84
+ 'float64',
85
+ 'string',
86
+ 'INT8_VEC',
87
+ 'INT16_VEC',
88
+ 'INT32_VEC',
89
+ 'INT64_VEC',
90
+ 'UINT8_VEC',
91
+ 'UINT16_VEC',
92
+ 'UINT32_VEC',
93
+ 'UINT64_VEC',
94
+ 'FLOAT_VEC',
95
+ 'DOUBLE_VEC',
96
+ 'STRING_VEC',
97
+ ],
98
+ },
99
+ ],
100
+ }
101
+
102
+ export interface MintAssetParams {
103
+ authorizedMinter: NameType
104
+ collectionName: NameType
105
+ schemaName: NameType
106
+ templateId: number
107
+ newAssetOwner: NameType
108
+ immutableData: ImmutableEntry[]
109
+ }
110
+
111
+ const MINTASSET_ABI = ABI.from(MINTASSET_ABI_DEF)
112
+
113
+ export function buildMintAssetAction(params: MintAssetParams): Action {
114
+ return Action.from(
115
+ {
116
+ account: Name.from(ATOMICASSETS_ACCOUNT),
117
+ name: Name.from('mintasset'),
118
+ authorization: [PLACEHOLDER_AUTH],
119
+ data: {
120
+ authorized_minter: Name.from(params.authorizedMinter),
121
+ collection_name: Name.from(params.collectionName),
122
+ schema_name: Name.from(params.schemaName),
123
+ template_id: params.templateId,
124
+ new_asset_owner: Name.from(params.newAssetOwner),
125
+ immutable_data: params.immutableData,
126
+ mutable_data: [],
127
+ tokens_to_back: [],
128
+ },
129
+ },
130
+ MINTASSET_ABI
131
+ )
132
+ }
133
+
134
+ export interface AtomicAssetRow {
135
+ asset_id: string
136
+ collection_name: string
137
+ schema_name: string
138
+ template_id: number
139
+ ram_payer?: string
140
+ backed_tokens?: string[]
141
+ immutable_serialized_data: string | number[]
142
+ mutable_serialized_data?: string | number[]
143
+ }
144
+
145
+ export interface AtomicSchemaRow {
146
+ schema_name: string
147
+ format: SchemaField[]
148
+ }
149
+
150
+ export interface FetchAssetsOptions {
151
+ collection?: NameType
152
+ pageSize?: number
153
+ account?: NameType
154
+ }
155
+
156
+ export async function fetchAtomicAssetsForOwner(
157
+ client: APIClient,
158
+ owner: NameType,
159
+ opts: FetchAssetsOptions = {}
160
+ ): Promise<AtomicAssetRow[]> {
161
+ const collection = opts.collection ? String(Name.from(opts.collection)) : undefined
162
+ const pageSize = opts.pageSize ?? 1000
163
+ const account = Name.from(opts.account ?? ATOMICASSETS_ACCOUNT)
164
+ const out: AtomicAssetRow[] = []
165
+ let lower: UInt64 | undefined
166
+ while (true) {
167
+ const res = await client.v1.chain.get_table_rows({
168
+ code: account,
169
+ scope: String(Name.from(owner)),
170
+ table: Name.from('assets'),
171
+ limit: pageSize,
172
+ lower_bound: lower,
173
+ json: true,
174
+ })
175
+ for (const row of res.rows as AtomicAssetRow[]) {
176
+ if (!collection || row.collection_name === collection) out.push(row)
177
+ }
178
+ if (!res.more) break
179
+ lower = UInt64.from(String(res.next_key))
180
+ }
181
+ return out
182
+ }
183
+
184
+ export async function fetchAtomicSchemas(
185
+ client: APIClient,
186
+ collection: NameType,
187
+ account: NameType = ATOMICASSETS_ACCOUNT
188
+ ): Promise<AtomicSchemaRow[]> {
189
+ const out: AtomicSchemaRow[] = []
190
+ let lower: Name | undefined
191
+ while (true) {
192
+ const res = await client.v1.chain.get_table_rows({
193
+ code: Name.from(account),
194
+ scope: String(Name.from(collection)),
195
+ table: Name.from('schemas'),
196
+ limit: 100,
197
+ lower_bound: lower,
198
+ json: true,
199
+ })
200
+ for (const row of res.rows as AtomicSchemaRow[]) out.push(row)
201
+ if (!res.more) break
202
+ lower = Name.from(String(res.next_key))
203
+ }
204
+ return out
205
+ }
206
+
207
+ export interface DecodedAtomicAsset {
208
+ asset_id: bigint
209
+ schema_name: string
210
+ template_id: number
211
+ item_id: number
212
+ quantity: number
213
+ stats: string
214
+ origin_x: bigint
215
+ origin_y: bigint
216
+ modules?: NFTModuleSlot[]
217
+ }
218
+
219
+ export function decodeAtomicAsset(
220
+ asset: AtomicAssetRow,
221
+ schemaFormat: SchemaField[],
222
+ itemId: number
223
+ ): DecodedAtomicAsset {
224
+ const data = deserializeAtomicData(asset.immutable_serialized_data, schemaFormat)
225
+ const cargo: NFTCargoItem = deserializeAsset(data, itemId)
226
+ return {
227
+ asset_id: BigInt(String(asset.asset_id)),
228
+ schema_name: String(asset.schema_name),
229
+ template_id: Number(asset.template_id),
230
+ item_id: cargo.item_id,
231
+ quantity: cargo.quantity,
232
+ stats: cargo.stats,
233
+ origin_x: BigInt(String(data.origin_x ?? 0)),
234
+ origin_y: BigInt(String(data.origin_y ?? 0)),
235
+ modules: cargo.modules,
236
+ }
237
+ }
@@ -0,0 +1,130 @@
1
+ export interface SchemaField {
2
+ name: string
3
+ type: string
4
+ }
5
+
6
+ export type RawData =
7
+ | Uint8Array
8
+ | string
9
+ | number[]
10
+ | {immutable_serialized_data: Uint8Array | string | number[]}
11
+
12
+ export function deserializeAtomicData(
13
+ data: RawData,
14
+ schema: SchemaField[]
15
+ ): Record<string, unknown> {
16
+ let rawData: Uint8Array | string | number[]
17
+ if (data && typeof data === 'object' && 'immutable_serialized_data' in (data as object)) {
18
+ rawData = (data as {immutable_serialized_data: Uint8Array | string | number[]})
19
+ .immutable_serialized_data
20
+ } else {
21
+ rawData = data as Uint8Array | string | number[]
22
+ }
23
+
24
+ let bytes: Uint8Array
25
+ if (typeof rawData === 'string') {
26
+ const hex = rawData
27
+ bytes = new Uint8Array(hex.length / 2)
28
+ for (let i = 0; i < hex.length; i += 2) {
29
+ bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16)
30
+ }
31
+ } else if (Array.isArray(rawData)) {
32
+ bytes = new Uint8Array(rawData)
33
+ } else {
34
+ bytes = rawData
35
+ }
36
+
37
+ let offset = 0
38
+
39
+ function readVarint(): number {
40
+ let result = 0
41
+ let multiplier = 1
42
+ while (bytes[offset] >= 128) {
43
+ result += (bytes[offset] - 128) * multiplier
44
+ offset++
45
+ multiplier *= 128
46
+ }
47
+ result += bytes[offset] * multiplier
48
+ offset++
49
+ return result
50
+ }
51
+
52
+ function readVarint64(): bigint {
53
+ let result = 0n
54
+ let multiplier = 1n
55
+ while (bytes[offset] >= 128) {
56
+ result += BigInt(bytes[offset] - 128) * multiplier
57
+ offset++
58
+ multiplier *= 128n
59
+ }
60
+ result += BigInt(bytes[offset]) * multiplier
61
+ offset++
62
+ return result
63
+ }
64
+
65
+ function readZigzagInt64(): bigint {
66
+ const unsigned = readVarint64()
67
+ if (unsigned % 2n === 0n) {
68
+ return unsigned / 2n
69
+ } else {
70
+ return -(unsigned / 2n) - 1n
71
+ }
72
+ }
73
+
74
+ function readString(): string {
75
+ const length = readVarint()
76
+ const str = new TextDecoder().decode(bytes.slice(offset, offset + length))
77
+ offset += length
78
+ return str
79
+ }
80
+
81
+ const RESERVED = 4
82
+ const result: Record<string, unknown> = {}
83
+
84
+ while (offset < bytes.length) {
85
+ const fieldIndex = readVarint() - RESERVED
86
+ const field = schema[fieldIndex]
87
+ if (!field) break
88
+
89
+ switch (field.type) {
90
+ case 'uint16':
91
+ result[field.name] = readVarint()
92
+ break
93
+ case 'uint32':
94
+ result[field.name] = readVarint()
95
+ break
96
+ case 'uint64':
97
+ result[field.name] = readVarint64()
98
+ break
99
+ case 'int32':
100
+ result[field.name] = readZigzagInt64()
101
+ break
102
+ case 'int64':
103
+ result[field.name] = readZigzagInt64()
104
+ break
105
+ case 'string':
106
+ case 'image':
107
+ case 'ipfs':
108
+ result[field.name] = readString()
109
+ break
110
+ case 'uint16[]': {
111
+ const len = readVarint()
112
+ const arr: number[] = []
113
+ for (let i = 0; i < len; i++) arr.push(readVarint())
114
+ result[field.name] = arr
115
+ break
116
+ }
117
+ case 'uint64[]': {
118
+ const len = readVarint()
119
+ const arr: bigint[] = []
120
+ for (let i = 0; i < len; i++) arr.push(readVarint64())
121
+ result[field.name] = arr
122
+ break
123
+ }
124
+ default:
125
+ throw new Error(`Unknown type: ${field.type}`)
126
+ }
127
+ }
128
+
129
+ return result
130
+ }
@@ -0,0 +1,321 @@
1
+ import {Serializer} from '@wharfkit/antelope'
2
+ import {getItem} from '../data/catalog'
3
+ import {
4
+ getModuleCapabilityType,
5
+ MODULE_CRAFTER,
6
+ MODULE_ENGINE,
7
+ MODULE_GATHERER,
8
+ MODULE_GENERATOR,
9
+ MODULE_HAULER,
10
+ MODULE_LOADER,
11
+ MODULE_STORAGE,
12
+ MODULE_WARP,
13
+ } from '../capabilities/modules'
14
+ import {decodeStat, decodeCraftedItemStats} from '../derivation/crafting'
15
+ import {getStatDefinitions} from '../derivation/stats'
16
+ import type {ResourceCategory} from '../types'
17
+ import {Types as ServerTypes} from '../contracts/server'
18
+ import {
19
+ buildEntityDescription,
20
+ computeCrafterDrain,
21
+ computeCrafterSpeed,
22
+ computeEngineDrain,
23
+ computeEngineThrust,
24
+ computeGathererDepth,
25
+ computeGathererDrain,
26
+ computeGathererYield,
27
+ computeGeneratorCap,
28
+ computeGeneratorRech,
29
+ computeHaulerCapacity,
30
+ computeHaulerDrain,
31
+ computeHaulerEfficiency,
32
+ computeLoaderMass,
33
+ computeLoaderThrust,
34
+ computeWarpRange,
35
+ } from './description'
36
+
37
+ export type AtomicAttributeType =
38
+ | 'string'
39
+ | 'uint8'
40
+ | 'uint16'
41
+ | 'uint32'
42
+ | 'uint64'
43
+ | 'int32'
44
+ | 'image'
45
+ | 'ipfs'
46
+ | 'UINT16_VEC'
47
+ | 'UINT64_VEC'
48
+
49
+ export interface ImmutableEntry {
50
+ first: string
51
+ second: [AtomicAttributeType, unknown]
52
+ }
53
+
54
+ export interface ImmutableModuleSlot {
55
+ type?: number | string | bigint
56
+ installed?: {item_id: number | string | bigint; stats: number | string | bigint}
57
+ }
58
+
59
+ export function moduleSlotsForImmutable(
60
+ modules: ServerTypes.module_entry[]
61
+ ): ImmutableModuleSlot[] {
62
+ return modules.map((m) => ({
63
+ type: Number(m.type.toString()),
64
+ installed: m.installed
65
+ ? {
66
+ item_id: Number(m.installed.item_id.toString()),
67
+ stats: BigInt(m.installed.stats.toString()),
68
+ }
69
+ : undefined,
70
+ }))
71
+ }
72
+
73
+ const IMAGE_HOST_URL = 'https://item.shiploadgame.com/item'
74
+
75
+ function bytesToBase64Url(bytes: Uint8Array): string {
76
+ let binary = ''
77
+ for (let i = 0; i < bytes.byteLength; i++) binary += String.fromCharCode(bytes[i]!)
78
+ const b64 = btoa(binary)
79
+ return b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
80
+ }
81
+
82
+ export function computeNftImageUrl(
83
+ item: {item_id: number; stats: bigint; modules: ImmutableModuleSlot[]; quantity: number},
84
+ originX: number,
85
+ originY: number
86
+ ): string {
87
+ const payload = ServerTypes.nft_item_payload.from({
88
+ item: {
89
+ item_id: item.item_id,
90
+ stats: String(item.stats),
91
+ modules: item.modules,
92
+ quantity: item.quantity,
93
+ },
94
+ location: {x: String(originX), y: String(originY)},
95
+ })
96
+ const bytes = Serializer.encode({object: payload}).array
97
+ return `${IMAGE_HOST_URL}/${bytesToBase64Url(bytes)}.png`
98
+ }
99
+
100
+ function commonBaseImmutable(
101
+ quantity: number,
102
+ stats: bigint,
103
+ originX: number,
104
+ originY: number,
105
+ img: string
106
+ ): ImmutableEntry[] {
107
+ return [
108
+ {first: 'quantity', second: ['uint32', quantity]},
109
+ {first: 'stats', second: ['uint64', String(stats)]},
110
+ {first: 'origin_x', second: ['int32', originX]},
111
+ {first: 'origin_y', second: ['int32', originY]},
112
+ {first: 'img', second: ['string', img]},
113
+ {first: 'deposit_amount', second: ['uint64', '0']},
114
+ {first: 'deposit_token', second: ['uint64', '0']},
115
+ {first: 'deposit_symbol', second: ['uint64', '0']},
116
+ ]
117
+ }
118
+
119
+ export function buildResourceImmutable(
120
+ itemId: number,
121
+ quantity: number,
122
+ stats: bigint,
123
+ originX: number,
124
+ originY: number
125
+ ): ImmutableEntry[] {
126
+ const item = getItem(itemId)
127
+ const cat = item.category
128
+ if (!cat) throw new Error(`Resource item ${itemId} has no category`)
129
+ const definitions = getStatDefinitions(cat as ResourceCategory)
130
+ const img = computeNftImageUrl(
131
+ {item_id: itemId, stats, modules: [], quantity},
132
+ originX,
133
+ originY
134
+ )
135
+ const base = commonBaseImmutable(quantity, stats, originX, originY, img)
136
+ base.push({first: definitions[0].key, second: ['uint16', decodeStat(stats, 0)]})
137
+ base.push({first: definitions[1].key, second: ['uint16', decodeStat(stats, 1)]})
138
+ base.push({first: definitions[2].key, second: ['uint16', decodeStat(stats, 2)]})
139
+ return base
140
+ }
141
+
142
+ export function buildComponentImmutable(
143
+ itemId: number,
144
+ quantity: number,
145
+ stats: bigint,
146
+ originX: number,
147
+ originY: number
148
+ ): ImmutableEntry[] {
149
+ const img = computeNftImageUrl(
150
+ {item_id: itemId, stats, modules: [], quantity},
151
+ originX,
152
+ originY
153
+ )
154
+ const base = commonBaseImmutable(quantity, stats, originX, originY, img)
155
+ const decoded = decodeCraftedItemStats(itemId, stats)
156
+ for (const [key, value] of Object.entries(decoded)) {
157
+ base.push({first: key, second: ['uint16', value]})
158
+ }
159
+ return base
160
+ }
161
+
162
+ export function buildModuleImmutable(
163
+ itemId: number,
164
+ quantity: number,
165
+ stats: bigint,
166
+ originX: number,
167
+ originY: number
168
+ ): ImmutableEntry[] {
169
+ const img = computeNftImageUrl(
170
+ {item_id: itemId, stats, modules: [], quantity},
171
+ originX,
172
+ originY
173
+ )
174
+ const base = commonBaseImmutable(quantity, stats, originX, originY, img)
175
+ const subtype = getModuleCapabilityType(itemId)
176
+ const item = getItem(itemId)
177
+ switch (subtype) {
178
+ case MODULE_ENGINE: {
179
+ const vol = decodeStat(stats, 0)
180
+ const thm = decodeStat(stats, 1)
181
+ base.push({first: 'volatility', second: ['uint16', vol]})
182
+ base.push({first: 'thermal', second: ['uint16', thm]})
183
+ base.push({first: 'thrust', second: ['uint32', computeEngineThrust(vol)]})
184
+ base.push({first: 'drain', second: ['uint16', computeEngineDrain(thm)]})
185
+ break
186
+ }
187
+ case MODULE_GENERATOR: {
188
+ const res = decodeStat(stats, 0)
189
+ const ref = decodeStat(stats, 1)
190
+ base.push({first: 'resonance', second: ['uint16', res]})
191
+ base.push({first: 'reflectivity', second: ['uint16', ref]})
192
+ base.push({first: 'capacity', second: ['uint16', computeGeneratorCap(res)]})
193
+ base.push({first: 'recharge', second: ['uint16', computeGeneratorRech(ref)]})
194
+ break
195
+ }
196
+ case MODULE_GATHERER: {
197
+ const str = decodeStat(stats, 0)
198
+ const tol = decodeStat(stats, 1)
199
+ const con = decodeStat(stats, 2)
200
+ const ref = decodeStat(stats, 3)
201
+ base.push({first: 'strength', second: ['uint16', str]})
202
+ base.push({first: 'tolerance', second: ['uint16', tol]})
203
+ base.push({first: 'conductivity', second: ['uint16', con]})
204
+ base.push({first: 'reflectivity', second: ['uint16', ref]})
205
+ base.push({first: 'yield', second: ['uint16', computeGathererYield(str)]})
206
+ base.push({first: 'drain', second: ['uint16', computeGathererDrain(con)]})
207
+ base.push({first: 'depth', second: ['uint16', computeGathererDepth(tol, item.tier)]})
208
+ break
209
+ }
210
+ case MODULE_LOADER: {
211
+ const fin = decodeStat(stats, 0)
212
+ const pla = decodeStat(stats, 1)
213
+ base.push({first: 'fineness', second: ['uint16', fin]})
214
+ base.push({first: 'plasticity', second: ['uint16', pla]})
215
+ base.push({first: 'mass', second: ['uint32', computeLoaderMass(fin)]})
216
+ base.push({first: 'thrust', second: ['uint16', computeLoaderThrust(pla)]})
217
+ break
218
+ }
219
+ case MODULE_WARP: {
220
+ const res = decodeStat(stats, 0)
221
+ base.push({first: 'resonance', second: ['uint16', res]})
222
+ base.push({first: 'range', second: ['uint32', computeWarpRange(res)]})
223
+ break
224
+ }
225
+ case MODULE_CRAFTER: {
226
+ const rea = decodeStat(stats, 0)
227
+ const fin = decodeStat(stats, 1)
228
+ base.push({first: 'reactivity', second: ['uint16', rea]})
229
+ base.push({first: 'fineness', second: ['uint16', fin]})
230
+ base.push({first: 'speed', second: ['uint16', computeCrafterSpeed(rea)]})
231
+ base.push({first: 'drain', second: ['uint16', computeCrafterDrain(fin)]})
232
+ break
233
+ }
234
+ case MODULE_STORAGE: {
235
+ const str = decodeStat(stats, 0)
236
+ const den = decodeStat(stats, 1)
237
+ const hrd = decodeStat(stats, 2)
238
+ const com = decodeStat(stats, 3)
239
+ const sum = str + den + hrd + com
240
+ base.push({first: 'strength', second: ['uint16', str]})
241
+ base.push({first: 'density', second: ['uint16', den]})
242
+ base.push({first: 'hardness', second: ['uint16', hrd]})
243
+ base.push({first: 'cohesion', second: ['uint16', com]})
244
+ base.push({
245
+ first: 'capacity_bonus_pct',
246
+ second: ['uint16', 10 + Math.floor((sum * 10) / 2997)],
247
+ })
248
+ break
249
+ }
250
+ case MODULE_HAULER: {
251
+ const res = decodeStat(stats, 0)
252
+ const pla = decodeStat(stats, 1)
253
+ const ref = decodeStat(stats, 2)
254
+ base.push({first: 'resonance', second: ['uint16', res]})
255
+ base.push({first: 'plasticity', second: ['uint16', pla]})
256
+ base.push({first: 'reflectivity', second: ['uint16', ref]})
257
+ base.push({first: 'capacity', second: ['uint8', computeHaulerCapacity(res)]})
258
+ base.push({first: 'efficiency', second: ['uint16', computeHaulerEfficiency(pla)]})
259
+ base.push({first: 'drain', second: ['uint16', computeHaulerDrain(ref)]})
260
+ break
261
+ }
262
+ }
263
+ return base
264
+ }
265
+
266
+ export function buildEntityImmutable(
267
+ itemId: number,
268
+ quantity: number,
269
+ stats: bigint,
270
+ originX: number,
271
+ originY: number,
272
+ modules: ImmutableModuleSlot[]
273
+ ): ImmutableEntry[] {
274
+ const moduleItems: number[] = []
275
+ const moduleStats: string[] = []
276
+ for (const m of modules) {
277
+ if (m.installed) {
278
+ moduleItems.push(Number(m.installed.item_id))
279
+ moduleStats.push(String(m.installed.stats))
280
+ } else {
281
+ moduleItems.push(0)
282
+ moduleStats.push('0')
283
+ }
284
+ }
285
+ const img = computeNftImageUrl({item_id: itemId, stats, modules, quantity}, originX, originY)
286
+ const base = commonBaseImmutable(quantity, stats, originX, originY, img)
287
+ base.push({first: 'module_items', second: ['UINT16_VEC', moduleItems]})
288
+ base.push({first: 'module_stats', second: ['UINT64_VEC', moduleStats]})
289
+ const description = buildEntityDescription(
290
+ itemId,
291
+ stats,
292
+ moduleItems,
293
+ moduleStats.map((s) => BigInt(s))
294
+ )
295
+ base.push({first: 'description', second: ['string', description]})
296
+ return base
297
+ }
298
+
299
+ export function buildImmutableData(
300
+ itemId: number,
301
+ quantity: number,
302
+ stats: bigint,
303
+ originX: number,
304
+ originY: number,
305
+ modules: ImmutableModuleSlot[] = []
306
+ ): ImmutableEntry[] {
307
+ const item = getItem(itemId)
308
+ if (item.type === 'resource') {
309
+ return buildResourceImmutable(itemId, quantity, stats, originX, originY)
310
+ }
311
+ if (item.type === 'component') {
312
+ return buildComponentImmutable(itemId, quantity, stats, originX, originY)
313
+ }
314
+ if (item.type === 'module') {
315
+ return buildModuleImmutable(itemId, quantity, stats, originX, originY)
316
+ }
317
+ if (item.type === 'entity') {
318
+ return buildEntityImmutable(itemId, quantity, stats, originX, originY, modules)
319
+ }
320
+ throw new Error(`Unsupported item type for wrap: ${item.type}`)
321
+ }