dreamwell-engine 1.0.0

Dreamwell pure-logic engine library — transforms, hierarchy, canon pipeline, spatial math, hashing, tile rules, validation, waymark schema, material/lighting descriptors. No SpacetimeDB dependency.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// InputFrame — per-frame input snapshot consumed by game logic.
// The exchange currency between platform adapters and game systems.
// Produced once per frame by the input system, consumed by movement, combat, UI, etc.

use super::action::InputAction;

/// Per-frame input snapshot. Immutable once built. Consumed by game systems.
///
/// Separates input collection (platform-specific) from input consumption (game logic).
/// Adapters write to InputFrameBuilder, then finalize to InputFrame.
#[derive(Debug, Clone)]
pub struct InputFrame {
    /// Actions active this frame (just pressed or held).
    pub actions: Vec<InputAction>,

    /// Movement vector: [x, y] in [-1, 1]. Composite of WASD/stick/dpad.
    /// x: negative=left, positive=right. y: negative=backward, positive=forward.
    pub movement: [f32; 2],

    /// Camera look delta: [dx, dy] in pixels or normalized units.
    /// dx: negative=look left, positive=look right.
    /// dy: negative=look up, positive=look down.
    pub look_delta: [f32; 2],

    /// Scroll wheel delta (positive=zoom in / scroll up).
    pub scroll_delta: f32,

    /// Cursor position in window coordinates [x, y].
    pub cursor_position: [f32; 2],

    /// Cursor delta since last frame [dx, dy].
    pub cursor_delta: [f32; 2],

    /// Frame timestamp (monotonic, seconds since start).
    pub timestamp: f32,
}

impl InputFrame {
    /// Whether a specific action is active this frame.
    pub fn has_action(&self, action: InputAction) -> bool {
        self.actions.contains(&action)
    }

    /// Whether any movement input is present.
    pub fn has_movement(&self) -> bool {
        self.movement[0] != 0.0 || self.movement[1] != 0.0
    }

    /// Whether any look/rotation input is present.
    pub fn has_look(&self) -> bool {
        self.look_delta[0] != 0.0 || self.look_delta[1] != 0.0
    }

    /// Whether any scroll input is present.
    pub fn has_scroll(&self) -> bool {
        self.scroll_delta != 0.0
    }
}

impl Default for InputFrame {
    fn default() -> Self {
        Self {
            actions: Vec::new(),
            movement: [0.0; 2],
            look_delta: [0.0; 2],
            scroll_delta: 0.0,
            cursor_position: [0.0; 2],
            cursor_delta: [0.0; 2],
            timestamp: 0.0,
        }
    }
}

/// Builder for constructing an InputFrame. Used by platform adapters.
pub struct InputFrameBuilder {
    actions: Vec<InputAction>,
    movement: [f32; 2],
    look_delta: [f32; 2],
    scroll_delta: f32,
    cursor_position: [f32; 2],
    cursor_delta: [f32; 2],
    timestamp: f32,
}

impl InputFrameBuilder {
    pub fn new(timestamp: f32) -> Self {
        Self {
            actions: Vec::new(),
            movement: [0.0; 2],
            look_delta: [0.0; 2],
            scroll_delta: 0.0,
            cursor_position: [0.0; 2],
            cursor_delta: [0.0; 2],
            timestamp,
        }
    }

    /// Add an action to the frame.
    pub fn action(mut self, action: InputAction) -> Self {
        if !self.actions.contains(&action) {
            self.actions.push(action);
        }
        self
    }

    /// Set movement vector.
    pub fn movement(mut self, x: f32, y: f32) -> Self {
        self.movement = [x, y];
        self
    }

    /// Set look delta.
    pub fn look(mut self, dx: f32, dy: f32) -> Self {
        self.look_delta = [dx, dy];
        self
    }

    /// Set scroll delta.
    pub fn scroll(mut self, delta: f32) -> Self {
        self.scroll_delta = delta;
        self
    }

    /// Set cursor position.
    pub fn cursor(mut self, x: f32, y: f32) -> Self {
        self.cursor_position = [x, y];
        self
    }

    /// Set cursor delta.
    pub fn cursor_delta(mut self, dx: f32, dy: f32) -> Self {
        self.cursor_delta = [dx, dy];
        self
    }

    /// Finalize into an immutable InputFrame.
    pub fn build(self) -> InputFrame {
        InputFrame {
            actions: self.actions,
            movement: self.movement,
            look_delta: self.look_delta,
            scroll_delta: self.scroll_delta,
            cursor_position: self.cursor_position,
            cursor_delta: self.cursor_delta,
            timestamp: self.timestamp,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty_frame() {
        let frame = InputFrame::default();
        assert!(!frame.has_movement());
        assert!(!frame.has_look());
        assert!(!frame.has_scroll());
        assert!(frame.actions.is_empty());
    }

    #[test]
    fn builder_actions() {
        let frame = InputFrameBuilder::new(0.0)
            .action(InputAction::MoveForward)
            .action(InputAction::Sprint)
            .build();

        assert!(frame.has_action(InputAction::MoveForward));
        assert!(frame.has_action(InputAction::Sprint));
        assert!(!frame.has_action(InputAction::Jump));
    }

    #[test]
    fn builder_no_duplicate_actions() {
        let frame = InputFrameBuilder::new(0.0)
            .action(InputAction::Attack)
            .action(InputAction::Attack)
            .build();

        assert_eq!(frame.actions.len(), 1);
    }

    #[test]
    fn builder_movement() {
        let frame = InputFrameBuilder::new(0.0).movement(-1.0, 1.0).build();

        assert!(frame.has_movement());
        assert_eq!(frame.movement, [-1.0, 1.0]);
    }

    #[test]
    fn builder_look_and_scroll() {
        let frame = InputFrameBuilder::new(0.0).look(5.0, -3.0).scroll(1.5).build();

        assert!(frame.has_look());
        assert!(frame.has_scroll());
        assert_eq!(frame.look_delta, [5.0, -3.0]);
        assert_eq!(frame.scroll_delta, 1.5);
    }

    #[test]
    fn builder_cursor() {
        let frame = InputFrameBuilder::new(1.0)
            .cursor(100.0, 200.0)
            .cursor_delta(2.0, -1.0)
            .build();

        assert_eq!(frame.cursor_position, [100.0, 200.0]);
        assert_eq!(frame.cursor_delta, [2.0, -1.0]);
        assert_eq!(frame.timestamp, 1.0);
    }

    #[test]
    fn full_frame_build() {
        let frame = InputFrameBuilder::new(0.016)
            .action(InputAction::MoveForward)
            .action(InputAction::Sprint)
            .movement(0.0, 1.0)
            .look(0.0, 0.0)
            .scroll(-0.5)
            .cursor(640.0, 480.0)
            .cursor_delta(0.0, 0.0)
            .build();

        assert!(frame.has_action(InputAction::MoveForward));
        assert!(frame.has_action(InputAction::Sprint));
        assert!(frame.has_movement());
        assert!(!frame.has_look());
        assert!(frame.has_scroll());
        assert_eq!(frame.timestamp, 0.016);
    }
}

// ── InputPacket ───────────────────────────────────────────────────────
//
// Tick-stamped, simulation-ready input contract. The canonical exchange type
// between the platform input layer and the CausalComputeKernel. Produced
// once per tick from an InputFrame (binding-aware, normalized).

/// Simulation-ready input packet. Single canonical type consumed by
/// CausalComputeKernel, CausalEngineEncoder, and all downstream systems.
#[derive(Debug, Clone)]
pub struct InputPacket {
    // ── Movement (from InputFrame.movement, already binding-aware + normalized) ──
    /// Camera-relative movement in [-1,1]. x=right, y=forward.
    pub movement: [f32; 2],
    /// Camera yaw in radians, for world-space direction derivation.
    pub camera_yaw: f32,

    // ── Edge-triggered (fire once per tick) ──
    pub jump: bool,
    pub interact: bool,

    // ── Level-triggered (held state) ──
    pub sprint: bool,
    pub gather: bool,
    /// Analog primary action strength [0,1].
    pub emit_strength: f32,
    /// Form coherence target [0,1]. 1.0=Cohere, 0.0=Wave.
    pub coherence_target: f32,

    // ── Timing ──
    pub dt: f32,
    /// Monotonic seconds since session start.
    pub timestamp: f64,
    pub tick: u64,

    // ── Physics context (from previous frame) ──
    pub grounded: bool,
}

impl InputPacket {
    /// Idle packet with all zeros. For tests and no-input frames.
    pub fn idle(dt: f32, tick: u64) -> Self {
        Self {
            movement: [0.0; 2],
            camera_yaw: 0.0,
            jump: false,
            interact: false,
            sprint: false,
            gather: false,
            emit_strength: 0.0,
            coherence_target: 1.0,
            dt,
            timestamp: 0.0,
            tick,
            grounded: true,
        }
    }

    /// Bridge an InputFrame to an InputPacket with additional simulation context.
    pub fn from_frame(
        frame: &InputFrame,
        camera_yaw: f32,
        dt: f32,
        timestamp: f64,
        tick: u64,
        grounded: bool,
        coherence_target: f32,
        gather: bool,
        emit_strength: f32,
    ) -> Self {
        Self {
            movement: frame.movement,
            camera_yaw,
            jump: frame.has_action(InputAction::Jump),
            interact: frame.has_action(InputAction::Interact),
            sprint: frame.has_action(InputAction::Sprint),
            gather,
            emit_strength,
            coherence_target,
            dt,
            timestamp,
            tick,
            grounded,
        }
    }

    /// Encode active actions as u8 discriminants for Weave serialization.
    pub fn actions_as_u8(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(4);
        if self.movement[1] > 0.0 {
            out.push(0);
        } // MoveForward
        if self.movement[1] < 0.0 {
            out.push(1);
        } // MoveBackward
        if self.movement[0] < 0.0 {
            out.push(2);
        } // MoveLeft
        if self.movement[0] > 0.0 {
            out.push(3);
        } // MoveRight
        if self.sprint {
            out.push(6);
        } // Sprint
        if self.jump {
            out.push(8);
        } // Jump
        if self.interact {
            out.push(16);
        } // Interact
        if self.gather {
            out.push(100);
        } // Gather (extended)
        out
    }

    /// BLAKE3 digest of the packet for attestation chains.
    pub fn digest(&self) -> [u8; 32] {
        let mut hasher = blake3::Hasher::new();
        hasher.update(&self.movement[0].to_le_bytes());
        hasher.update(&self.movement[1].to_le_bytes());
        hasher.update(&self.camera_yaw.to_le_bytes());
        hasher.update(&[
            self.jump as u8,
            self.interact as u8,
            self.sprint as u8,
            self.gather as u8,
        ]);
        hasher.update(&self.emit_strength.to_le_bytes());
        hasher.update(&self.coherence_target.to_le_bytes());
        hasher.update(&self.dt.to_le_bytes());
        hasher.update(&self.timestamp.to_le_bytes());
        hasher.update(&self.tick.to_le_bytes());
        hasher.update(&[self.grounded as u8]);
        *hasher.finalize().as_bytes()
    }

    /// Whether any movement input is present.
    pub fn has_movement(&self) -> bool {
        self.movement[0] != 0.0 || self.movement[1] != 0.0
    }
}

#[cfg(test)]
mod input_packet_tests {
    use super::*;

    #[test]
    fn idle_packet_is_zero() {
        let p = InputPacket::idle(1.0 / 60.0, 0);
        assert!(!p.has_movement());
        assert!(!p.jump);
        assert!(!p.sprint);
        assert!(p.grounded);
        assert_eq!(p.coherence_target, 1.0);
    }

    #[test]
    fn from_frame_maps_actions() {
        let frame = InputFrameBuilder::new(0.0)
            .action(InputAction::Jump)
            .action(InputAction::Sprint)
            .movement(0.5, 1.0)
            .build();
        let p = InputPacket::from_frame(&frame, 0.0, 0.016, 1.0, 1, true, 0.8, false, 0.0);
        assert!(p.jump);
        assert!(p.sprint);
        assert!(!p.interact);
        assert_eq!(p.movement, [0.5, 1.0]);
        assert_eq!(p.coherence_target, 0.8);
    }

    #[test]
    fn actions_as_u8_encodes_movement() {
        let mut p = InputPacket::idle(0.016, 0);
        p.movement = [0.0, 1.0]; // forward
        p.sprint = true;
        let actions = p.actions_as_u8();
        assert!(actions.contains(&0)); // MoveForward
        assert!(actions.contains(&6)); // Sprint
        assert!(!actions.contains(&8)); // no Jump
    }

    #[test]
    fn digest_is_nonzero() {
        let p = InputPacket::idle(0.016, 0);
        let d = p.digest();
        assert_ne!(d, [0u8; 32]);
    }

    #[test]
    fn digest_changes_with_input() {
        let p1 = InputPacket::idle(0.016, 0);
        let mut p2 = InputPacket::idle(0.016, 0);
        p2.jump = true;
        assert_ne!(p1.digest(), p2.digest());
    }
}