proof-engine 0.1.1

A mathematical rendering engine for Rust. Every visual is the output of a mathematical function.
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
//! Combo system — input buffering, combo chains, and hit-confirm windows.

use std::collections::VecDeque;

// ── ComboInput ────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ComboInput {
    LightAttack,
    HeavyAttack,
    Special,
    Dodge,
    Block,
    Jump,
    Ability1,
    Ability2,
    Ability3,
    Ability4,
    Direction(ComboDirection),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ComboDirection {
    Forward,
    Backward,
    Up,
    Down,
    Neutral,
}

impl ComboInput {
    pub fn name(self) -> &'static str {
        match self {
            ComboInput::LightAttack  => "Light",
            ComboInput::HeavyAttack  => "Heavy",
            ComboInput::Special      => "Special",
            ComboInput::Dodge        => "Dodge",
            ComboInput::Block        => "Block",
            ComboInput::Jump         => "Jump",
            ComboInput::Ability1     => "Skill1",
            ComboInput::Ability2     => "Skill2",
            ComboInput::Ability3     => "Skill3",
            ComboInput::Ability4     => "Skill4",
            ComboInput::Direction(_) => "Dir",
        }
    }
}

// ── BufferedInput ─────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct BufferedInput {
    pub input:     ComboInput,
    pub timestamp: f64,
    pub consumed:  bool,
}

/// Ring buffer of recent inputs for combo detection.
#[derive(Debug, Clone)]
pub struct InputBuffer {
    inputs:          VecDeque<BufferedInput>,
    max_size:        usize,
    buffer_window:   f64,  // seconds — inputs older than this are expired
    current_time:    f64,
}

impl InputBuffer {
    pub fn new(max_size: usize, buffer_window: f64) -> Self {
        Self {
            inputs: VecDeque::with_capacity(max_size),
            max_size,
            buffer_window,
            current_time: 0.0,
        }
    }

    pub fn update(&mut self, dt: f32) {
        self.current_time += dt as f64;
        // Expire old inputs
        while let Some(front) = self.inputs.front() {
            if self.current_time - front.timestamp > self.buffer_window {
                self.inputs.pop_front();
            } else {
                break;
            }
        }
    }

    pub fn push(&mut self, input: ComboInput) {
        if self.inputs.len() >= self.max_size {
            self.inputs.pop_front();
        }
        self.inputs.push_back(BufferedInput {
            input,
            timestamp: self.current_time,
            consumed: false,
        });
    }

    pub fn consume_next_unconsumed(&mut self) -> Option<ComboInput> {
        for entry in &mut self.inputs {
            if !entry.consumed {
                entry.consumed = true;
                return Some(entry.input);
            }
        }
        None
    }

    pub fn peek_sequence(&self, len: usize) -> Vec<ComboInput> {
        self.inputs.iter()
            .filter(|e| !e.consumed)
            .map(|e| e.input)
            .take(len)
            .collect()
    }

    pub fn clear(&mut self) {
        self.inputs.clear();
    }

    pub fn len(&self) -> usize {
        self.inputs.iter().filter(|e| !e.consumed).count()
    }

    pub fn is_empty(&self) -> bool { self.len() == 0 }
}

// ── ComboLink ─────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct ComboLink {
    pub input:         ComboInput,
    /// Maximum seconds after the previous hit's confirm window opens
    pub time_window:   f32,
    /// Minimum seconds (prevents mashing being too easy)
    pub min_delay:     f32,
}

impl ComboLink {
    pub fn new(input: ComboInput, window: f32) -> Self {
        Self { input, time_window: window, min_delay: 0.0 }
    }

    pub fn with_min_delay(mut self, d: f32) -> Self { self.min_delay = d; self }
}

// ── ComboHit ──────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct ComboHit {
    pub animation_id:     String,
    pub damage_multiplier: f32,
    pub hitstop_duration:  f32,  // freeze frames on hit
    pub launch:            bool,
    pub knockback_force:   f32,
    pub can_cancel_into:   Vec<String>,  // combo IDs you can cancel into
    pub hit_confirm_start: f32,   // when the hitbox is active (relative to hit time)
    pub hit_confirm_end:   f32,
    pub glyph:             char,
    pub element:           Option<super::Element>,
}

impl ComboHit {
    pub fn new(anim: impl Into<String>, dmg_mult: f32) -> Self {
        Self {
            animation_id: anim.into(),
            damage_multiplier: dmg_mult,
            hitstop_duration: 0.1,
            launch: false,
            knockback_force: 0.0,
            can_cancel_into: Vec::new(),
            hit_confirm_start: 0.3,
            hit_confirm_end: 0.5,
            glyph: '',
            element: None,
        }
    }

    pub fn heavy(anim: impl Into<String>, dmg_mult: f32) -> Self {
        Self {
            animation_id: anim.into(),
            damage_multiplier: dmg_mult,
            hitstop_duration: 0.2,
            launch: false,
            knockback_force: 5.0,
            can_cancel_into: Vec::new(),
            hit_confirm_start: 0.4,
            hit_confirm_end: 0.7,
            glyph: '',
            element: None,
        }
    }

    pub fn launcher(anim: impl Into<String>) -> Self {
        Self {
            animation_id: anim.into(),
            damage_multiplier: 0.8,
            hitstop_duration: 0.15,
            launch: true,
            knockback_force: 12.0,
            can_cancel_into: Vec::new(),
            hit_confirm_start: 0.35,
            hit_confirm_end: 0.6,
            glyph: '',
            element: None,
        }
    }

    pub fn with_element(mut self, el: super::Element) -> Self { self.element = Some(el); self }
    pub fn with_cancel(mut self, combo_id: impl Into<String>) -> Self {
        self.can_cancel_into.push(combo_id.into()); self
    }
}

// ── Combo ─────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct Combo {
    pub id:     String,
    pub name:   String,
    pub links:  Vec<ComboLink>,  // input sequence to execute this combo
    pub hits:   Vec<ComboHit>,   // one per step in the chain
    pub ender:  Option<ComboEnder>,
    pub requires_airborne: bool,
    pub requires_grounded: bool,
    pub priority: u32,  // higher = checked first
}

#[derive(Debug, Clone)]
pub struct ComboEnder {
    pub name: String,
    pub damage_multiplier: f32,
    pub special_effect: Option<String>,
}

impl Combo {
    pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            links: Vec::new(),
            hits: Vec::new(),
            ender: None,
            requires_airborne: false,
            requires_grounded: false,
            priority: 0,
        }
    }

    pub fn add_link(mut self, input: ComboInput, window: f32) -> Self {
        self.links.push(ComboLink::new(input, window));
        self
    }

    pub fn add_hit(mut self, hit: ComboHit) -> Self {
        self.hits.push(hit);
        self
    }

    pub fn with_ender(mut self, name: impl Into<String>, dmg_mult: f32) -> Self {
        self.ender = Some(ComboEnder { name: name.into(), damage_multiplier: dmg_mult, special_effect: None });
        self
    }

    pub fn aerial(mut self) -> Self { self.requires_airborne = true; self }
    pub fn grounded(mut self) -> Self { self.requires_grounded = true; self }
    pub fn with_priority(mut self, p: u32) -> Self { self.priority = p; self }

    pub fn total_damage_multiplier(&self) -> f32 {
        let base: f32 = self.hits.iter().map(|h| h.damage_multiplier).sum();
        let ender_mult = self.ender.as_ref().map(|e| e.damage_multiplier).unwrap_or(1.0);
        base * ender_mult
    }

    pub fn input_sequence(&self) -> Vec<ComboInput> {
        self.links.iter().map(|l| l.input).collect()
    }

    /// Check if a given input history matches this combo's sequence.
    pub fn matches_sequence(&self, inputs: &[ComboInput]) -> bool {
        if inputs.len() < self.links.len() { return false; }
        let start = inputs.len() - self.links.len();
        inputs[start..].iter().zip(self.links.iter()).all(|(inp, link)| *inp == link.input)
    }
}

// ── ComboDatabase ─────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Default)]
pub struct ComboDatabase {
    combos: Vec<Combo>,
}

impl ComboDatabase {
    pub fn new() -> Self { Self { combos: Vec::new() } }

    pub fn register(&mut self, combo: Combo) {
        self.combos.push(combo);
        // Sort by priority descending, then by length (longer = more specific)
        self.combos.sort_by(|a, b| {
            b.priority.cmp(&a.priority).then(b.links.len().cmp(&a.links.len()))
        });
    }

    pub fn find_matching(&self, inputs: &[ComboInput], airborne: bool, grounded: bool) -> Option<&Combo> {
        self.combos.iter().find(|c| {
            c.matches_sequence(inputs)
                && (!c.requires_airborne || airborne)
                && (!c.requires_grounded || grounded)
        })
    }

    pub fn get(&self, id: &str) -> Option<&Combo> {
        self.combos.iter().find(|c| c.id == id)
    }

    pub fn len(&self) -> usize { self.combos.len() }

    /// Prebuilt combo database for a warrior-type character.
    pub fn warrior_presets() -> Self {
        let mut db = ComboDatabase::new();

        db.register(
            Combo::new("light_chain", "Light Chain")
                .add_link(ComboInput::LightAttack, 0.5)
                .add_link(ComboInput::LightAttack, 0.5)
                .add_link(ComboInput::LightAttack, 0.5)
                .add_hit(ComboHit::new("slash_1", 0.8))
                .add_hit(ComboHit::new("slash_2", 0.9))
                .add_hit(ComboHit::new("slash_3", 1.1))
                .with_ender("Final Slash", 1.3)
                .grounded()
                .with_priority(1)
        );

        db.register(
            Combo::new("heavy_opener", "Overhead Smash")
                .add_link(ComboInput::HeavyAttack, 0.8)
                .add_hit(ComboHit::heavy("overhead_smash", 1.8))
                .grounded()
                .with_priority(2)
        );

        db.register(
            Combo::new("light_into_heavy", "Rapid Crush")
                .add_link(ComboInput::LightAttack, 0.5)
                .add_link(ComboInput::LightAttack, 0.5)
                .add_link(ComboInput::HeavyAttack, 0.6)
                .add_hit(ComboHit::new("slash_1", 0.7))
                .add_hit(ComboHit::new("slash_2", 0.8))
                .add_hit(ComboHit::heavy("crush", 2.2))
                .with_ender("Shockwave", 1.5)
                .grounded()
                .with_priority(3)
        );

        db.register(
            Combo::new("launcher", "Rising Strike")
                .add_link(ComboInput::LightAttack, 0.5)
                .add_link(ComboInput::Direction(ComboDirection::Up), 0.3)
                .add_link(ComboInput::HeavyAttack, 0.5)
                .add_hit(ComboHit::new("rising_1", 0.6))
                .add_hit(ComboHit::launcher("launch_hit"))
                .grounded()
                .with_priority(4)
        );

        db.register(
            Combo::new("air_combo", "Aerial Rave")
                .add_link(ComboInput::LightAttack, 0.5)
                .add_link(ComboInput::LightAttack, 0.5)
                .add_link(ComboInput::LightAttack, 0.5)
                .add_hit(ComboHit::new("air_slash_1", 0.7))
                .add_hit(ComboHit::new("air_slash_2", 0.8))
                .add_hit(ComboHit::new("air_slash_3", 1.0))
                .with_ender("Air Slam", 2.0)
                .aerial()
                .with_priority(5)
        );

        db
    }
}

// ── ComboState ────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq)]
pub enum ComboPhase {
    Idle,
    Attacking { hit_index: usize },
    HitConfirmWindow { hit_index: usize, window_remaining: f32 },
    Hitstop { duration_remaining: f32, resume_to_index: usize },
    Ender,
    Recovery { duration_remaining: f32 },
}

#[derive(Debug, Clone)]
pub struct ComboState {
    pub active_combo:   Option<String>,  // combo ID
    pub current_phase:  ComboPhase,
    pub hit_count:      u32,
    pub total_damage:   f32,
    pub combo_timer:    f32,       // time since last hit (for timeout)
    pub combo_timeout:  f32,       // max seconds between hits
    pub input_buffer:   InputBuffer,
    pub is_airborne:    bool,
    pub is_grounded:    bool,
}

impl ComboState {
    pub fn new() -> Self {
        Self {
            active_combo: None,
            current_phase: ComboPhase::Idle,
            hit_count: 0,
            total_damage: 0.0,
            combo_timer: 0.0,
            combo_timeout: 2.0,
            input_buffer: InputBuffer::new(16, 0.3),
            is_airborne: false,
            is_grounded: true,
        }
    }

    pub fn update(&mut self, dt: f32) {
        self.input_buffer.update(dt);
        self.combo_timer += dt;

        // Timeout active combo
        if self.active_combo.is_some() && self.combo_timer > self.combo_timeout {
            self.reset();
            return;
        }

        // Advance phase timers
        match &mut self.current_phase {
            ComboPhase::HitConfirmWindow { window_remaining, .. } => {
                *window_remaining -= dt;
                if *window_remaining <= 0.0 {
                    self.current_phase = ComboPhase::Idle;
                }
            }
            ComboPhase::Hitstop { duration_remaining, resume_to_index } => {
                *duration_remaining -= dt;
                if *duration_remaining <= 0.0 {
                    let idx = *resume_to_index;
                    self.current_phase = ComboPhase::HitConfirmWindow {
                        hit_index: idx,
                        window_remaining: 0.4,
                    };
                }
            }
            ComboPhase::Recovery { duration_remaining } => {
                *duration_remaining -= dt;
                if *duration_remaining <= 0.0 {
                    self.current_phase = ComboPhase::Idle;
                }
            }
            _ => {}
        }
    }

    pub fn register_input(&mut self, input: ComboInput) {
        self.input_buffer.push(input);
        self.combo_timer = 0.0;
    }

    pub fn register_hit(&mut self, damage: f32, hit: &ComboHit) {
        self.hit_count += 1;
        self.total_damage += damage;
        let next_idx = match &self.current_phase {
            ComboPhase::Attacking { hit_index } => *hit_index + 1,
            ComboPhase::HitConfirmWindow { hit_index, .. } => *hit_index + 1,
            _ => 0,
        };
        self.current_phase = ComboPhase::Hitstop {
            duration_remaining: hit.hitstop_duration,
            resume_to_index: next_idx,
        };
    }

    pub fn is_in_combo(&self) -> bool { self.active_combo.is_some() }

    pub fn start_combo(&mut self, combo_id: String) {
        self.active_combo = Some(combo_id);
        self.current_phase = ComboPhase::Attacking { hit_index: 0 };
        self.hit_count = 0;
        self.total_damage = 0.0;
        self.combo_timer = 0.0;
    }

    pub fn end_combo(&mut self) {
        self.current_phase = ComboPhase::Recovery { duration_remaining: 0.5 };
        self.active_combo = None;
    }

    pub fn reset(&mut self) {
        self.active_combo = None;
        self.current_phase = ComboPhase::Idle;
        self.hit_count = 0;
        self.total_damage = 0.0;
        self.combo_timer = 0.0;
        self.input_buffer.clear();
    }

    pub fn can_act(&self) -> bool {
        matches!(self.current_phase,
            ComboPhase::Idle |
            ComboPhase::HitConfirmWindow { .. }
        )
    }
}

// ── ComboTracker ──────────────────────────────────────────────────────────────

/// High-level tracker that wraps the combo database and per-entity combo state.
pub struct ComboTracker {
    pub database: ComboDatabase,
}

impl ComboTracker {
    pub fn new(database: ComboDatabase) -> Self {
        Self { database }
    }

    /// Try to start a combo from the current input buffer state.
    pub fn try_start(&self, state: &mut ComboState) -> Option<&Combo> {
        let inputs = state.input_buffer.peek_sequence(8);
        if inputs.is_empty() { return None; }
        let combo = self.database.find_matching(&inputs, state.is_airborne, state.is_grounded)?;
        state.start_combo(combo.id.clone());
        Some(combo)
    }

    /// Get the current active combo (if any).
    pub fn active_combo<'a>(&'a self, state: &ComboState) -> Option<&'a Combo> {
        state.active_combo.as_ref().and_then(|id| self.database.get(id))
    }

    /// Get the current hit in the active combo.
    pub fn current_hit<'a>(&'a self, state: &ComboState) -> Option<&'a ComboHit> {
        let combo = self.active_combo(state)?;
        let hit_idx = match &state.current_phase {
            ComboPhase::Attacking { hit_index }           => *hit_index,
            ComboPhase::HitConfirmWindow { hit_index, .. } => *hit_index,
            ComboPhase::Hitstop { resume_to_index, .. }   => resume_to_index.saturating_sub(1),
            _                                              => return None,
        };
        combo.hits.get(hit_idx)
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    #[test]
    fn test_input_buffer() {
        let mut buf = InputBuffer::new(16, 1.0);
        buf.push(ComboInput::LightAttack);
        buf.push(ComboInput::LightAttack);
        buf.push(ComboInput::HeavyAttack);
        assert_eq!(buf.len(), 3);
        let seq = buf.peek_sequence(3);
        assert_eq!(seq[0], ComboInput::LightAttack);
        assert_eq!(seq[2], ComboInput::HeavyAttack);
    }

    #[test]
    fn test_combo_match() {
        let combo = Combo::new("test", "Test")
            .add_link(ComboInput::LightAttack, 0.5)
            .add_link(ComboInput::LightAttack, 0.5)
            .add_link(ComboInput::HeavyAttack, 0.6);

        let inputs = vec![ComboInput::LightAttack, ComboInput::LightAttack, ComboInput::HeavyAttack];
        assert!(combo.matches_sequence(&inputs));

        let wrong = vec![ComboInput::LightAttack, ComboInput::HeavyAttack, ComboInput::HeavyAttack];
        assert!(!combo.matches_sequence(&wrong));
    }

    #[test]
    fn test_combo_database_warrior() {
        let db = ComboDatabase::warrior_presets();
        assert!(db.len() > 0);

        let inputs = vec![
            ComboInput::LightAttack,
            ComboInput::LightAttack,
            ComboInput::LightAttack,
        ];
        let m = db.find_matching(&inputs, false, true);
        assert!(m.is_some());
    }

    #[test]
    fn test_combo_state_flow() {
        let mut state = ComboState::new();
        state.start_combo("test".to_string());
        assert!(state.is_in_combo());
        assert_eq!(state.hit_count, 0);

        let hit = ComboHit::new("slash", 1.0);
        state.register_hit(50.0, &hit);
        assert_eq!(state.hit_count, 1);
        assert!((state.total_damage - 50.0).abs() < 0.01);
    }
}