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
use crate::{Frame, MAX_INPUT_BYTES, MAX_PLAYERS, NULL_FRAME};
pub type InputBuffer = [u8; MAX_INPUT_BYTES * MAX_PLAYERS as usize];
pub const BLANK_INPUT: GameInput = GameInput {
frame: NULL_FRAME,
buffer: [0; MAX_INPUT_BYTES * MAX_PLAYERS as usize],
size: 0,
};
fn fletcher16(data: &[u8]) -> u16 {
let mut sum1: u16 = 0;
let mut sum2: u16 = 0;
for index in 0..data.len() {
sum1 = (sum1 + data[index] as u16) % 255;
sum2 = (sum2 + sum1) % 255;
}
(sum2 << 8) | sum1
}
#[derive(Debug, Clone)]
pub struct GameState {
pub frame: Frame,
pub buffer: Option<Vec<u8>>,
pub checksum: u64,
}
impl Default for GameState {
fn default() -> Self {
Self {
frame: NULL_FRAME,
buffer: None,
checksum: 0,
}
}
}
impl GameState {
pub fn new(frame: Frame, buffer: Option<Vec<u8>>, check: Option<u64>) -> Self {
let checksum = match check {
Some(cs) => cs,
None => match &buffer {
Some(data) => fletcher16(data) as u64,
None => 0,
},
};
Self {
frame,
buffer,
checksum,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct GameInput {
pub frame: Frame,
pub size: usize,
pub buffer: InputBuffer,
}
impl Default for GameInput {
fn default() -> Self {
Self {
frame: NULL_FRAME,
size: 0,
buffer: Default::default(),
}
}
}
impl GameInput {
pub(crate) fn new(frame: Frame, size: usize) -> Self {
assert!(size > 0);
Self {
frame,
size,
buffer: Default::default(),
}
}
}
impl GameInput {
pub(crate) fn copy_input(&mut self, bytes: &[u8]) {
assert!(bytes.len() == self.size);
self.buffer[0..self.size].copy_from_slice(bytes);
}
pub(crate) fn erase_bits(&mut self) {
self.buffer.iter_mut().for_each(|m| *m = 0)
}
pub(crate) fn equal(&self, other: &Self, bitsonly: bool) -> bool {
(bitsonly || self.frame == other.frame)
&& self.size == other.size
&& self.buffer == other.buffer
}
pub fn input(&self) -> &[u8] {
&self.buffer[0..self.size]
}
}
#[cfg(test)]
mod game_input_tests {
use super::*;
use bincode;
#[test]
fn test_input_equality_bits_only() {
let fake_inputs: u32 = 5;
let input_size = std::mem::size_of::<u32>();
let serialized_inputs = bincode::serialize(&fake_inputs).unwrap();
let mut input1 = GameInput::new(0, input_size);
input1.copy_input(&serialized_inputs);
let mut input2 = GameInput::new(5, input_size);
input2.copy_input(&serialized_inputs);
assert!(input1.equal(&input2, true));
}
#[test]
fn test_input_equality_fail() {
let input_size = std::mem::size_of::<u32>();
let fake_inputs: u32 = 5;
let serialized_inputs = bincode::serialize(&fake_inputs).unwrap();
let mut input1 = GameInput::new(0, input_size);
input1.copy_input(&serialized_inputs);
let fake_inputs: u32 = 7;
let serialized_inputs = bincode::serialize(&fake_inputs).unwrap();
let mut input2 = GameInput::new(0, input_size);
input2.copy_input(&serialized_inputs);
assert!(!input1.equal(&input2, false));
}
}