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
use crate::{Frame, NULL_FRAME};
#[derive(Debug, Clone)]
pub(crate) struct GameState<S: Clone> {
pub frame: Frame,
pub data: Option<S>,
pub checksum: Option<u128>,
}
impl<S: Clone> Default for GameState<S> {
fn default() -> Self {
Self {
frame: NULL_FRAME,
data: None,
checksum: None,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) struct PlayerInput<I>
where
I: Copy + Clone + PartialEq + bytemuck::Pod + bytemuck::Zeroable,
{
pub frame: Frame,
pub input: I,
}
impl<I: Copy + Clone + PartialEq + bytemuck::Pod + bytemuck::Zeroable> PlayerInput<I> {
pub(crate) fn new(frame: Frame, input: I) -> Self {
Self { frame, input }
}
pub(crate) fn blank_input(frame: Frame) -> Self {
Self {
frame,
input: I::zeroed(),
}
}
pub(crate) fn equal(&self, other: &Self, input_only: bool) -> bool {
(input_only || self.frame == other.frame) && self.input == other.input
}
}
#[cfg(test)]
mod game_input_tests {
use super::*;
use bytemuck::{Pod, Zeroable};
#[repr(C)]
#[derive(Copy, Clone, PartialEq, Pod, Zeroable)]
struct TestInput {
inp: u8,
}
#[test]
fn test_input_equality() {
let input1 = PlayerInput::new(0, TestInput { inp: 5 });
let input2 = PlayerInput::new(0, TestInput { inp: 5 });
assert!(input1.equal(&input2, false));
}
#[test]
fn test_input_equality_input_only() {
let input1 = PlayerInput::new(0, TestInput { inp: 5 });
let input2 = PlayerInput::new(5, TestInput { inp: 5 });
assert!(input1.equal(&input2, true));
}
#[test]
fn test_input_equality_fail() {
let input1 = PlayerInput::new(0, TestInput { inp: 5 });
let input2 = PlayerInput::new(0, TestInput { inp: 7 });
assert!(!input1.equal(&input2, false));
}
}