@shipload/sdk 0.7.1 → 2.0.0-rc1

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 (47) hide show
  1. package/lib/shipload.d.ts +1651 -226
  2. package/lib/shipload.js +4958 -1971
  3. package/lib/shipload.js.map +1 -1
  4. package/lib/shipload.m.js +4787 -1940
  5. package/lib/shipload.m.js.map +1 -1
  6. package/package.json +5 -4
  7. package/src/contracts/server.ts +585 -203
  8. package/src/data/goods.json +23 -0
  9. package/src/data/syllables.json +1184 -0
  10. package/src/entities/cargo-utils.ts +47 -0
  11. package/src/entities/entity-inventory.ts +39 -0
  12. package/src/entities/gamestate.ts +152 -0
  13. package/src/entities/location.ts +241 -0
  14. package/src/entities/player.ts +287 -0
  15. package/src/entities/ship.ts +559 -0
  16. package/src/entities/warehouse.ts +205 -0
  17. package/src/errors.ts +46 -9
  18. package/src/index-module.ts +119 -7
  19. package/src/managers/actions.ts +168 -0
  20. package/src/managers/base.ts +25 -0
  21. package/src/managers/context.ts +104 -0
  22. package/src/managers/entities.ts +86 -0
  23. package/src/managers/epochs.ts +47 -0
  24. package/src/managers/index.ts +9 -0
  25. package/src/managers/locations.ts +103 -0
  26. package/src/managers/players.ts +13 -0
  27. package/src/managers/trades.ts +119 -0
  28. package/src/market/goods.ts +31 -0
  29. package/src/{market.ts → market/market.ts} +32 -37
  30. package/src/{rolls.ts → market/rolls.ts} +3 -3
  31. package/src/{epoch.ts → scheduling/epoch.ts} +1 -1
  32. package/src/scheduling/projection.ts +218 -0
  33. package/src/scheduling/schedule.ts +155 -0
  34. package/src/shipload.ts +39 -157
  35. package/src/trading/collect.ts +939 -0
  36. package/src/trading/deal.ts +208 -0
  37. package/src/trading/trade.ts +203 -0
  38. package/src/travel/travel.ts +425 -0
  39. package/src/types.ts +60 -25
  40. package/src/utils/system.ts +27 -0
  41. package/src/goods.ts +0 -124
  42. package/src/ship.ts +0 -36
  43. package/src/state.ts +0 -0
  44. package/src/syllables.ts +0 -1184
  45. package/src/system.ts +0 -37
  46. package/src/travel.ts +0 -259
  47. /package/src/{hash.ts → utils/hash.ts} +0 -0
package/src/system.ts DELETED
@@ -1,37 +0,0 @@
1
- import {Checksum256} from '@wharfkit/antelope'
2
- import {hash512} from './hash'
3
- import {Coordinates} from './types'
4
- import {ServerContract} from './contracts'
5
- import syllables from './syllables'
6
-
7
- export function getSystemName(gameSeed: Checksum256, location: Coordinates): string {
8
- if (!hasSystem(gameSeed, location)) {
9
- throw new Error("System doesn't exist at location")
10
- }
11
- // Create a seed string using the location coordinates
12
- const seed = `${location.x}${location.y}systemName`
13
-
14
- // Hash the seed to get a consistent hash value
15
- const hash = hash512(gameSeed, seed)
16
-
17
- // Determine the number of syllables for the name (1 to 3)
18
- const syllableCount = 1 + (hash.array[0] % 3)
19
-
20
- // Use the hash to select syllables
21
- const name: string[] = []
22
- for (let i = 0; i < syllableCount; i++) {
23
- const index = hash.array[i] % syllables.length
24
- const syllable = syllables[index]
25
- name.push(i > 0 ? syllable.toLowerCase() : syllable)
26
- }
27
-
28
- return name.join('')
29
- }
30
-
31
- export function hasSystem(
32
- gameSeed: Checksum256,
33
- coordinates: ServerContract.ActionParams.Type.coordinates
34
- ): boolean {
35
- const str = ['system', coordinates.x, coordinates.y].join('-')
36
- return String(hash512(gameSeed, str)).slice(0, 2) === '00'
37
- }
package/src/travel.ts DELETED
@@ -1,259 +0,0 @@
1
- import {
2
- BlockTimestamp,
3
- Checksum256,
4
- Int64,
5
- Int64Type,
6
- UInt32,
7
- UInt32Type,
8
- UInt64,
9
- UInt64Type,
10
- } from '@wharfkit/antelope'
11
-
12
- import {PlatformContract, ServerContract} from './contracts'
13
- import {Distance, PRECISION, TRAVEL_MAXMASS_PENALTY} from './types'
14
- import {getGood} from './goods'
15
- import {hasSystem} from './system'
16
-
17
- export function travelplanDuration(travelplan: ServerContract.Types.travel_plan) {
18
- return UInt64.from(travelplan.flighttime)
19
- .adding(travelplan.rechargetime)
20
- .adding(travelplan.loadtime)
21
- .adding(travelplan.masspenalty)
22
- }
23
-
24
- export function distanceTraveled(
25
- ship: ServerContract.Types.ship_row,
26
- current: Date = new Date()
27
- ): number {
28
- if (ship.travelplan) {
29
- const {departure} = ship.travelplan
30
- const duration = travelplanDuration(ship.travelplan)
31
- return (+current - +departure.toDate()) / (Number(duration) * 1000)
32
- }
33
- return 0
34
- }
35
-
36
- export function distanceBetweenCoordinates(
37
- origin: ServerContract.ActionParams.Type.coordinates,
38
- destination: ServerContract.ActionParams.Type.coordinates
39
- ): UInt64 {
40
- return distanceBetweenPoints(origin.x, origin.y, destination.x, destination.y)
41
- }
42
-
43
- export function distanceBetweenPoints(
44
- x1: Int64Type,
45
- y1: Int64Type,
46
- x2: Int64Type,
47
- y2: Int64Type
48
- ): UInt64 {
49
- const x = Math.pow(x1 - x2, 2)
50
- const y = Math.pow(y1 - y2, 2)
51
- return UInt64.from(Math.sqrt(x + y) * PRECISION)
52
- }
53
-
54
- export function lerp(
55
- origin: ServerContract.ActionParams.Type.coordinates,
56
- destination: ServerContract.ActionParams.Type.coordinates,
57
- time: number
58
- ): ServerContract.ActionParams.Type.coordinates {
59
- return {
60
- x: (1 - time) * Number(origin.x) + time * Number(destination.x),
61
- y: (1 - time) * Number(origin.y) + time * Number(destination.y),
62
- }
63
- }
64
-
65
- export function rotation(
66
- origin: ServerContract.ActionParams.Type.coordinates,
67
- destination: ServerContract.ActionParams.Type.coordinates
68
- ) {
69
- return Math.atan2(destination.y - origin.y, destination.x - origin.x) * (180 / Math.PI) + 90
70
- }
71
-
72
- export function findNearbyPlanets(
73
- seed: Checksum256,
74
- origin: ServerContract.ActionParams.Type.coordinates,
75
- maxDistance: UInt64Type = 20 * PRECISION
76
- ): Distance[] {
77
- // console.log(String(seed), String(maxDistance), JSON.stringify(origin))
78
- const nearbySystems: Distance[] = []
79
-
80
- const max = UInt64.from(maxDistance / PRECISION)
81
- const xMin = Int64.from(origin.x).subtracting(max)
82
- const xMax = Int64.from(origin.x).adding(max)
83
- const yMin = Int64.from(origin.y).subtracting(max)
84
- const yMax = Int64.from(origin.y).adding(max)
85
-
86
- // console.log('xMin', Number(xMin))
87
- // console.log('xMax', Number(xMax))
88
- // console.log('yMin', Number(yMin))
89
- // console.log('yMax', Number(yMax))
90
-
91
- for (let x = Number(xMin); x <= Number(xMax); x++) {
92
- for (let y = Number(yMin); y <= Number(yMax); y++) {
93
- const samePlace = x === origin.x && y === origin.y
94
- if (!samePlace) {
95
- const distance = distanceBetweenPoints(origin.x, origin.y, x, y)
96
- if (Number(distance) <= Number(maxDistance)) {
97
- const system = hasSystem(seed, {x, y})
98
- if (system) {
99
- nearbySystems.push({origin, destination: {x, y}, distance})
100
- }
101
- }
102
- }
103
- }
104
- }
105
-
106
- return nearbySystems
107
- }
108
- export function travelplan(
109
- game: PlatformContract.Types.game_row,
110
- ship: ServerContract.Types.ship_row,
111
- cargos: ServerContract.Types.cargo_row[],
112
- origin: ServerContract.ActionParams.Type.coordinates,
113
- destination: ServerContract.ActionParams.Type.coordinates,
114
- recharge: boolean,
115
- alwaysValid = false
116
- ): ServerContract.Types.travel_plan {
117
- if (!alwaysValid) {
118
- const valid = hasSystem(game.config.seed, destination)
119
- if (!valid) {
120
- throw new Error('Invalid destination')
121
- }
122
- }
123
- const distance = distanceBetweenCoordinates(origin, destination)
124
- const mass = calc_ship_mass(ship, cargos) // Total mass of ship_id
125
- const loadtime = calc_ship_loadtime(ship, cargos)
126
- const flighttime = calc_ship_flighttime(ship, mass, distance)
127
- const rechargetime = recharge ? calc_ship_rechargetime(ship) : 0
128
- const masspenalty = calc_mass_penalty(ship, mass)
129
- const energyusage = calc_energyusage(distance, ship.stats.drain) // Energy usage from ship and flighttime
130
-
131
- return ServerContract.Types.travel_plan.from({
132
- departure: BlockTimestamp.fromDate(new Date()),
133
- destination,
134
- distance,
135
- loadtime,
136
- flighttime,
137
- rechargetime,
138
- masspenalty,
139
- // TODO: Remove below, used for debugging
140
- energyusage,
141
- mass,
142
- })
143
- }
144
-
145
- export function calc_mass_penalty(ship: ServerContract.Types.ship_row, mass: UInt64Type): UInt32 {
146
- const current = Number(mass)
147
- const maximum = Number(ship.stats.maxmass)
148
- if (mass > ship.stats.maxmass) {
149
- const overage = (current - maximum) / PRECISION
150
- const penalty = TRAVEL_MAXMASS_PENALTY * Math.exp(0.000005 * overage)
151
- return UInt32.from(penalty)
152
- }
153
- return UInt32.from(0)
154
- }
155
-
156
- export function calc_rechargetime(
157
- capacity: UInt32Type,
158
- energy: UInt32Type,
159
- recharge: UInt32Type
160
- ): UInt32 {
161
- return UInt32.from(capacity).subtracting(energy).dividing(recharge)
162
- }
163
-
164
- export function calc_ship_rechargetime(ship: ServerContract.Types.ship_row): UInt32 {
165
- return calc_rechargetime(ship.stats.capacity, ship.state.energy, ship.stats.recharge)
166
- }
167
-
168
- // uint32_t server::calc_ship_rechargetime(const ship_row ship)
169
- // {
170
- // return calc_rechargetime(ship.stats.capacity, ship.state.energy, ship.stats.recharge);
171
- // }
172
-
173
- export function calc_ship_loadtime(
174
- ship: ServerContract.Types.ship_row,
175
- cargos: ServerContract.Types.cargo_row[]
176
- ): UInt32 {
177
- const loadtime = UInt32.from(0)
178
-
179
- const mass_load = UInt64.from(0)
180
- const mass_unload = UInt64.from(0)
181
- for (const cargo of cargos) {
182
- const cargo_delta = Number(cargo.owned) - Number(cargo.loaded)
183
- if (cargo_delta !== 0) {
184
- const good_mass = getGood(cargo.good_id).mass
185
- const cargo_mass = good_mass.multiplying(Math.abs(cargo_delta))
186
-
187
- if (cargo_delta > 0) {
188
- mass_load.add(cargo_mass)
189
- } else {
190
- mass_unload.add(cargo_mass)
191
- }
192
- }
193
- }
194
-
195
- if (Number(mass_load) > 0 || Number(mass_unload) > 0) {
196
- mass_load.add(ship.loaders.mass)
197
- loadtime.add(calc_loader_flighttime(ship, mass_load))
198
-
199
- mass_unload.add(ship.loaders.mass)
200
- loadtime.add(calc_loader_flighttime(ship, mass_unload))
201
- }
202
-
203
- return loadtime.dividing(ship.loaders.quantity)
204
- }
205
-
206
- export function calc_flighttime(distance: UInt64Type, acceleration: number): UInt32 {
207
- return UInt32.from(2 * Math.sqrt(Number(distance) / acceleration))
208
- }
209
-
210
- export function calc_loader_flighttime(ship: ServerContract.Types.ship_row, mass: UInt64): UInt32 {
211
- return calc_flighttime(ship.stats.orbit, calc_loader_acceleration(ship, mass))
212
- }
213
-
214
- export function calc_loader_acceleration(
215
- ship: ServerContract.Types.ship_row,
216
- mass: UInt64
217
- ): number {
218
- return calc_acceleration(Number(ship.loaders.thrust), Number(mass) + Number(ship.loaders.mass))
219
- }
220
-
221
- export function calc_ship_flighttime(
222
- ship: ServerContract.Types.ship_row,
223
- mass: UInt64,
224
- distance: UInt64
225
- ): UInt32 {
226
- const acceleration = calc_ship_acceleration(ship, mass)
227
- return calc_flighttime(distance, acceleration)
228
- }
229
-
230
- export function calc_ship_acceleration(ship: ServerContract.Types.ship_row, mass: UInt64): number {
231
- return calc_acceleration(Number(ship.stats.thrust), Number(mass))
232
- }
233
-
234
- export function calc_acceleration(thrust: number, mass: number): number {
235
- return (thrust / mass) * PRECISION
236
- }
237
-
238
- export function calc_ship_mass(
239
- ship: ServerContract.Types.ship_row,
240
- cargos: ServerContract.Types.cargo_row[]
241
- ): UInt64 {
242
- const mass = UInt64.from(0)
243
-
244
- mass.add(ship.stats.mass)
245
-
246
- if (Number(ship.loaders.quantity) > 0) {
247
- mass.add(ship.loaders.mass.multiplying(ship.loaders.quantity))
248
- }
249
-
250
- for (const cargo of cargos) {
251
- mass.add(getGood(cargo.good_id).mass.multiplying(cargo.owned))
252
- }
253
-
254
- return mass
255
- }
256
-
257
- export function calc_energyusage(distance: UInt64Type, drain: UInt32Type): UInt32 {
258
- return UInt64.from(distance).dividing(PRECISION).multiplying(drain)
259
- }
File without changes