@crowdedkingdoms/crowdyjs 8.6.0 → 8.7.0

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,156 @@
1
+ /**
2
+ * Engine wire registry — the client mirror of the server's
3
+ * `crowdy-game-kit-core::wire` (the single source of truth for the actor
4
+ * pose layout and flag-bit registry used by compute-module game engines).
5
+ * Parity with the Rust crate is checked by the kit unit suite: any change
6
+ * here must land in kit-core first.
7
+ *
8
+ * 48-byte little-endian pose: pos f32 x3 (0..11), yaw/pitch f32 (12..19),
9
+ * velocity f32 x3 (20..31), flags u8 (32), held u8 (33), 34-35 reserved,
10
+ * updated_at f64 ms (36..43), 44-47 reserved. Payloads may append opaque
11
+ * UTF-8 suffix bytes (engines put the entity's container id there).
12
+ */
13
+ import { decodeBase64, encodeBase64 } from '../utils.js';
14
+ export const POSE_BYTES = 48;
15
+ /** Flag-bit registry. Bits 0-3 are platform-reserved; games may use 4-7. */
16
+ export const FLAG_GROUNDED = 0b0001;
17
+ export const FLAG_MOB = 0b0010;
18
+ export const FLAG_NPC = 0b0100;
19
+ export const FLAG_RESERVED3 = 0b1000;
20
+ /** Encode a pose (suffix appended when set). */
21
+ export function encodeEnginePose(pose) {
22
+ const suffix = pose.suffix != null ? new TextEncoder().encode(pose.suffix) : null;
23
+ const bytes = new Uint8Array(POSE_BYTES + (suffix?.length ?? 0));
24
+ const view = new DataView(bytes.buffer);
25
+ view.setFloat32(0, pose.x ?? 0, true);
26
+ view.setFloat32(4, pose.y ?? 0, true);
27
+ view.setFloat32(8, pose.z ?? 0, true);
28
+ view.setFloat32(12, pose.yaw ?? 0, true);
29
+ view.setFloat32(16, pose.pitch ?? 0, true);
30
+ view.setFloat32(20, pose.velX ?? 0, true);
31
+ view.setFloat32(24, pose.velY ?? 0, true);
32
+ view.setFloat32(28, pose.velZ ?? 0, true);
33
+ view.setUint8(32, pose.flags ?? 0);
34
+ view.setUint8(33, pose.held ?? 0);
35
+ view.setFloat64(36, pose.updatedAtMs ?? 0, true);
36
+ if (suffix)
37
+ bytes.set(suffix, POSE_BYTES);
38
+ return bytes;
39
+ }
40
+ /**
41
+ * Decode the leading 48 bytes (tolerates longer payloads — the suffix is
42
+ * extracted). Returns null for short or non-finite payloads.
43
+ */
44
+ export function decodeEnginePose(bytes) {
45
+ if (bytes.length < POSE_BYTES)
46
+ return null;
47
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
48
+ const pose = {
49
+ x: view.getFloat32(0, true),
50
+ y: view.getFloat32(4, true),
51
+ z: view.getFloat32(8, true),
52
+ yaw: view.getFloat32(12, true),
53
+ pitch: view.getFloat32(16, true),
54
+ velX: view.getFloat32(20, true),
55
+ velY: view.getFloat32(24, true),
56
+ velZ: view.getFloat32(28, true),
57
+ flags: view.getUint8(32),
58
+ held: view.getUint8(33),
59
+ updatedAtMs: view.getFloat64(36, true),
60
+ suffix: poseSuffix(bytes),
61
+ };
62
+ if (!Number.isFinite(pose.x) || !Number.isFinite(pose.y) || !Number.isFinite(pose.z)) {
63
+ return null;
64
+ }
65
+ return pose;
66
+ }
67
+ /** The UTF-8 suffix after the pose (engine container ids), if any. */
68
+ export function poseSuffix(bytes) {
69
+ if (bytes.length <= POSE_BYTES)
70
+ return null;
71
+ const text = new TextDecoder().decode(bytes.subarray(POSE_BYTES)).trim();
72
+ return text.length > 0 ? text : null;
73
+ }
74
+ /**
75
+ * A {@link StateCodec} for engine poses over the base64 wire form — plug it
76
+ * into `attachRemoteActors` / `createWorldSession` actor config. Undecodable
77
+ * payloads yield a zeroed pose with `flags: 0` (they land in the players
78
+ * lane predicate's care, same as unknown custom states).
79
+ */
80
+ export const enginePoseCodec = {
81
+ encode: (pose) => encodeBase64(encodeEnginePose(pose)),
82
+ decode: (data) => decodeEnginePose(decodeBase64(data)) ?? {
83
+ x: 0,
84
+ y: 0,
85
+ z: 0,
86
+ yaw: 0,
87
+ pitch: 0,
88
+ velX: 0,
89
+ velY: 0,
90
+ velZ: 0,
91
+ flags: 0,
92
+ held: 0,
93
+ updatedAtMs: 0,
94
+ suffix: null,
95
+ },
96
+ };
97
+ /**
98
+ * Ready-made lane predicates for `createWorldSession` when the world runs
99
+ * compute-module engines: `players` (no engine flag), `mobs` (FLAG_MOB) and
100
+ * `npcs` (FLAG_NPC) — what Blocks with Friends hand-wired in its Phase 9
101
+ * adoption. Spread extra lanes on top as needed.
102
+ */
103
+ export function engineLanes() {
104
+ return {
105
+ mobs: (state) => (state.flags & FLAG_MOB) !== 0,
106
+ npcs: (state) => (state.flags & FLAG_NPC) !== 0,
107
+ players: (state) => (state.flags & (FLAG_MOB | FLAG_NPC)) === 0,
108
+ };
109
+ }
110
+ // ---------------------------------------------------------------------------
111
+ // Server-event payloads ([u16 LE event type][state bytes], state = JSON)
112
+ // ---------------------------------------------------------------------------
113
+ /** Contact damage decided by a mob/combat engine (kit-play referee). */
114
+ export const EVENT_CONTACT_DAMAGE = 77;
115
+ /** Weather/season transition from a world engine (kit-sim weather). */
116
+ export const EVENT_WEATHER = 90;
117
+ /** Split an engine server-event payload into its type + JSON body. */
118
+ export function parseEngineEvent(bytes) {
119
+ if (bytes.length < 2)
120
+ return null;
121
+ const eventType = bytes[0] | (bytes[1] << 8);
122
+ let body = {};
123
+ if (bytes.length > 2) {
124
+ try {
125
+ body = JSON.parse(new TextDecoder().decode(bytes.subarray(2)));
126
+ }
127
+ catch {
128
+ return null;
129
+ }
130
+ }
131
+ return { eventType, body };
132
+ }
133
+ /** Parse a contact-damage event; null when the payload is another type. */
134
+ export function parseContactDamage(bytes) {
135
+ const parsed = parseEngineEvent(bytes);
136
+ if (!parsed || parsed.eventType !== EVENT_CONTACT_DAMAGE)
137
+ return null;
138
+ return {
139
+ targetUuid: String(parsed.body.targetUuid ?? ''),
140
+ damage: Number(parsed.body.damage ?? 0),
141
+ mobId: String(parsed.body.mobId ?? ''),
142
+ mobName: String(parsed.body.mobName ?? ''),
143
+ };
144
+ }
145
+ /** Parse a weather event; null when the payload is another type. */
146
+ export function parseWeatherEvent(bytes) {
147
+ const parsed = parseEngineEvent(bytes);
148
+ if (!parsed || parsed.eventType !== EVENT_WEATHER)
149
+ return null;
150
+ return {
151
+ weather: String(parsed.body.weather ?? ''),
152
+ sinceMs: Number(parsed.body.sinceMs ?? 0),
153
+ untilMs: Number(parsed.body.untilMs ?? 0),
154
+ body: parsed.body,
155
+ };
156
+ }
@@ -1,10 +1,27 @@
1
1
  import type { GameModelAPI } from '../domains/gameModel.js';
2
2
  import type { Scalars, SeedPropertyInput } from '../generated/graphql.js';
3
+ import type { EngineDetector } from './engine.js';
3
4
  import { type KitInvokeResult } from './shared.js';
5
+ import { type WeatherEvent } from './wire.js';
4
6
  /** Options for {@link WorldsimKit}. Must match the deployed worldsim blueprint. */
5
7
  export interface WorldsimKitOptions {
6
8
  /** The `typePrefix` the worldsim blueprint was deployed with. */
7
9
  typePrefix?: string;
10
+ /**
11
+ * The compute module serving `forecast` when the app runs a world engine.
12
+ * Defaults to `'world-engine'`.
13
+ */
14
+ moduleName?: string;
15
+ }
16
+ /** A world engine's forecast (current front + day phase). */
17
+ export interface KitForecast {
18
+ weather: string;
19
+ /** Day phase in [0, 1) when the engine reports it. */
20
+ dayPhase?: number;
21
+ isNight?: boolean;
22
+ /** Milliseconds until the current front rolls. */
23
+ remainingMs?: number;
24
+ body: Record<string, unknown>;
8
25
  }
9
26
  /** A parsed view of the world clock/weather state. */
10
27
  export interface KitWorldState {
@@ -56,8 +73,24 @@ export interface KitWaveSpawner {
56
73
  export declare class WorldsimKit {
57
74
  private readonly appId;
58
75
  private readonly gameModel;
76
+ private readonly engines?;
59
77
  private readonly names;
60
- constructor(appId: Scalars['BigInt']['input'], gameModel: GameModelAPI, options?: WorldsimKitOptions);
78
+ private readonly moduleName;
79
+ constructor(appId: Scalars['BigInt']['input'], gameModel: GameModelAPI, options?: WorldsimKitOptions, engines?: EngineDetector | undefined);
80
+ /** Is a world compute engine deployed + enabled (cached per session)? */
81
+ engineAvailable(): Promise<boolean>;
82
+ /**
83
+ * The world engine's `forecast` invoke: the current weather front plus
84
+ * day-phase fields. Late joiners call this once, then track transitions
85
+ * from the type-90 event stream ({@link parseWeather}).
86
+ */
87
+ forecast(): Promise<KitForecast>;
88
+ /**
89
+ * Parse a server-event payload as a world-engine weather transition
90
+ * (type 90), or null when it is another event type. Feed it your world
91
+ * session's server-event stream.
92
+ */
93
+ parseWeather(payload: Uint8Array): WeatherEvent | null;
61
94
  /**
62
95
  * Find-or-create the WorldState singleton (admin). `anchorChunk` is where
63
96
  * the clock's spatial time-changed ping is emitted.
@@ -1 +1 @@
1
- {"version":3,"file":"worldsim.d.ts","sourceRoot":"","sources":["../../src/kit/worldsim.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE1E,OAAO,EAGL,KAAK,eAAe,EACrB,MAAM,aAAa,CAAC;AAErB,mFAAmF;AACnF,MAAM,WAAW,kBAAkB;IACjC,iEAAiE;IACjE,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,sDAAsD;AACtD,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,0CAA0C;AAC1C,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC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ACnB,GAAG,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;IASpC,sDAAsD;IAChD,KAAK,CAAC,KAAK,EAAE;QACjB,WAAW,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC;QACxC,YAAY,EAAE,MAAM,CAAC;QACrB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB;;;;;;;;;;;IAmBD,oEAAoE;IAC9D,KAAK,CACT,WAAW,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,GAAG,IAAI,GAC7C,OAAO,CAAC,OAAO,EAAE,CAAC;IAmCrB;;;OAGG;IACG,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;IASlF,sEAAsE;IAChE,aAAa,CAAC,KAAK,EAAE;QACzB,WAAW,EAAE,MAAM,CAAC;QACpB,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB;;;;;;;;;;;IAeD,mEAAmE;IAC7D,QAAQ,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;IAsB3C,4EAA4E;IACtE,MAAM,CAAC,cAAc,EAAE,MAAM;;;;;;;;;;;;;;;;;;;;;;IAInC,+DAA+D;IACzD,UAAU,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAO1D"}
@@ -1,5 +1,6 @@
1
1
  import { worldsimNames } from './blueprints/index.js';
2
2
  import { kitContainerProperties, kitInvoke, } from './shared.js';
3
+ import { parseWeatherEvent } from './wire.js';
3
4
  /**
4
5
  * Runtime helpers for the {@link worldsimBlueprint} conventions: the world
5
6
  * clock/weather singleton, regenerating resource nodes players gather from,
@@ -10,10 +11,46 @@ import { kitContainerProperties, kitInvoke, } from './shared.js';
10
11
  * Obtained via `client.kit(appId).worldsim`.
11
12
  */
12
13
  export class WorldsimKit {
13
- constructor(appId, gameModel, options = {}) {
14
+ constructor(appId, gameModel, options = {}, engines) {
14
15
  this.appId = appId;
15
16
  this.gameModel = gameModel;
17
+ this.engines = engines;
16
18
  this.names = worldsimNames(options.typePrefix ?? '');
19
+ this.moduleName = options.moduleName ?? 'world-engine';
20
+ }
21
+ /** Is a world compute engine deployed + enabled (cached per session)? */
22
+ engineAvailable() {
23
+ if (!this.engines)
24
+ return Promise.resolve(false);
25
+ return this.engines.has(this.moduleName);
26
+ }
27
+ /**
28
+ * The world engine's `forecast` invoke: the current weather front plus
29
+ * day-phase fields. Late joiners call this once, then track transitions
30
+ * from the type-90 event stream ({@link parseWeather}).
31
+ */
32
+ async forecast() {
33
+ const result = this.engines
34
+ ? await this.engines.invoke(this.moduleName, 'forecast')
35
+ : { success: false, reason: 'compute domain unavailable', body: {} };
36
+ if (!result.success) {
37
+ throw new Error(`forecast unavailable: ${result.reason ?? 'engine missing'}`);
38
+ }
39
+ return {
40
+ weather: String(result.body.weather ?? ''),
41
+ dayPhase: typeof result.body.dayPhase === 'number' ? result.body.dayPhase : undefined,
42
+ isNight: typeof result.body.isNight === 'boolean' ? result.body.isNight : undefined,
43
+ remainingMs: typeof result.body.remainingMs === 'number' ? result.body.remainingMs : undefined,
44
+ body: result.body,
45
+ };
46
+ }
47
+ /**
48
+ * Parse a server-event payload as a world-engine weather transition
49
+ * (type 90), or null when it is another event type. Feed it your world
50
+ * session's server-event stream.
51
+ */
52
+ parseWeather(payload) {
53
+ return parseWeatherEvent(payload);
17
54
  }
18
55
  /**
19
56
  * Find-or-create the WorldState singleton (admin). `anchorChunk` is where
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crowdedkingdoms/crowdyjs",
3
- "version": "8.6.0",
3
+ "version": "8.7.0",
4
4
  "description": "Client SDK for Crowded Kingdoms GraphQL API with UDP proxy support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",