GekkoNet 1.0
C/C++ Peer to Peer Game Networking SDK
Loading...
Searching...
No Matches
input.cpp
Go to the documentation of this file.
1#include "input.h"
2
3#include <algorithm>
4#include <cstdlib>
5#include <cstring>
6
22
23void Gekko::InputBuffer::Init(u8 delay, u8 input_window, u32 input_size, u32 buffer_size)
24{
25 _inputs.clear();
26
27 _input_delay = delay;
28 _input_size = input_size;
29 _buff_size = buffer_size;
30 _input_prediction_window = input_window;
31 _running_ahead = false;
32
34
37
39
40 // init GameInput array
41 _empty_input = std::make_unique<u8[]>(_input_size);
42 std::memset(_empty_input.get(), 0, _input_size);
43
44 for (u32 i = 0; i < _buff_size; i++) {
45 _inputs.push_back(std::make_unique<GameInput>());
47 }
48}
49
51{
52 if (_inputs[frame % _buff_size]->frame == GameInput::NULL_FRAME && _input_delay > 0) {
53 for (i32 i = 0; i < _input_delay; i++) {
54 AddInput(i, _empty_input.get());
55 }
56 }
57 AddInput(frame + _input_delay, input);
58}
59
61{
62 // only allow sequential input insertion
63 if (frame != _last_received_input + 1) {
64 return;
65 }
66
67 const Frame idx = frame % _buff_size;
68 if (_first_predicted_input == frame) {
69 if (!_inputs[idx]->IsEqualTo(input)) {
70 // incorrect prediction
72
73 // last prediction frame ? add correct input and reset prediction
75 _inputs[idx]->Init(frame, input, _input_size);
77 } else {
78
79 // repeat the correct inputs instead of the wrong one.
81 for (Frame i = 0; i <= diff; i++) {
82 const Frame pred_frame = _first_predicted_input + i;
83 _inputs[pred_frame % _buff_size]->Init(pred_frame, input, _input_size);
84 }
85
86 // move along this frame since the previous is verified now.
88 }
89 } else {
90
91 // correct prediction
94 } else {
96 }
97 }
98 } else {
99 // not a predicted input
100 _inputs[idx]->Init(frame, input, _input_size);
101 }
102
103 // always advance the buffer
105}
106
108{
109 // only inputs the buffer already received can be replaced.
110 if (frame < 0 || frame > _last_received_input) {
111 return;
112 }
113
114 const Frame idx = frame % _buff_size;
115 if (_inputs[idx]->frame != frame) {
116 return;
117 }
118
119 if (_inputs[idx]->IsEqualTo(input)) {
120 return;
121 }
122
123 _inputs[idx]->Init(frame, input, _input_size);
124
125 // register the frame as a misprediction so the next rollback corrects it.
126 // keep the queue sorted so the front stays the lowest incorrect frame.
127 auto it = std::lower_bound(
130 frame
131 );
132
133 if (it == _incorrent_predicted_inputs.end() || *it != frame) {
134 _incorrent_predicted_inputs.insert(it, frame);
135 }
136}
137
139{
140 // early return AddLocalInput will handle it
142 _input_delay = delay;
143 return;
144 }
145
146 // when our current delay is smaller then the new delay
147 // all we have to do is expand the delay with the last input we received
148 if (_input_delay < delay) {
149 _input_delay = delay;
150
151 Frame last_input = _last_received_input;
152 u8* prev = _inputs[last_input % _buff_size]->input.get();
153
154 for (i32 i = 1; i <= _input_delay; i++) {
155 AddInput(last_input + i, prev);
156 }
157
158 return;
159 }
160
161 // if our current delay is bigger then our new delay the plan is different.
162 // since we probably already sent the inputs up to _last_received_input
163 // which includes the previous delay we should not modify any inputs that happend before that
164 // this will probably mean that it will drop local inputs until the local machine catches up
165 if (_input_delay > delay) {
166 _input_delay = delay;
167 return;
168 }
169}
170
175
177{
178 _input_prediction_window = input_window;
179}
180
185
191
196
203
205{
206 while (!_incorrent_predicted_inputs.empty() && _incorrent_predicted_inputs.front() <= clear_limit) {
207 _incorrent_predicted_inputs.pop_front();
208 }
209}
210
211
213{
214 const u32 prev_input = PreviousFrame(frame);
216 // if the prev frame happends to be an empty frame add a dummy input
217 if (_inputs[prev_input % _buff_size]->frame == GameInput::NULL_FRAME) {
218 _inputs[frame % _buff_size]->Init(frame, _empty_input.get(), _input_size);
219 }
220 else {
221 // predict by copying the previous input
222 _inputs[frame % _buff_size]->Init(_inputs[prev_input % _buff_size].get());
223 _inputs[frame % _buff_size]->frame = frame;
224 }
225 // set prediction values
227 _last_predicted_input = frame;
228 return true;
229 }
230 else {
231 // continue predicting if the diff is within the window
233 if (_input_prediction_window > diff) {
234 _inputs[frame % _buff_size]->Init(_inputs[prev_input % _buff_size].get());
235 _inputs[frame % _buff_size]->frame = frame;
236 // move the last predicted input along with the requested frame
237 _last_predicted_input = frame;
238 return true;
239 }
240 return false;
241 }
242}
243
251
253{
254 return frame - 1 < 0 ? _buff_size - frame - 1 : frame - 1;
255}
256
258{
259 _running_ahead = running_ahead;
260}
261
262std::unique_ptr<Gekko::GameInput> Gekko::InputBuffer::GetInput(Frame frame, bool prediction)
263{
264 auto inp = std::make_unique<GameInput>();
265
266 if (_last_received_input < frame) {
267 // no input? check if we should predict the input
268 if (prediction) {
270 frame <= _last_predicted_input) {
271 // return existing prediction
272 inp->Init(_inputs[frame % _buff_size].get());
273
274 } else if (!_running_ahead && CanPredictInput() && HandleInputPrediction(frame)) {
275 // generate new prediction
276 inp->Init(_inputs[frame % _buff_size].get());
277
278 } else if (_running_ahead) {
279 // return last known input without advancing prediction state
281 if (ref != GameInput::NULL_FRAME) {
282 inp->Init(_inputs[ref % _buff_size].get());
283 }
284 }
285 }
286 return inp;
287 }
288
289 if (_inputs[frame % _buff_size]->frame != frame ||
290 _inputs[frame % _buff_size]->frame == GameInput::NULL_FRAME) {
291 return inp;
292 }
293
294 inp->Init(_inputs[frame % _buff_size].get());
295 return inp;
296}
297
299{
300 frame = other->frame;
301 input_len = other->input_len;
302
303 if (input) {
304 std::memcpy(input.get(), other->input.get(), input_len);
305 return;
306 }
307
308 input = std::make_unique<u8[]>(input_len);
309
310 if (input) {
311 std::memcpy(input.get(), other->input.get(), input_len);
312 }
313}
314
315void Gekko::GameInput::Init(Frame frame_num, u8* inp, u32 inp_len)
316{
317 frame = frame_num;
318 input_len = inp_len;
319
320 if (input) {
321 std::memcpy(input.get(), inp, input_len);
322 return;
323 }
324
325 input = std::make_unique<u8[]>(input_len);
326
327 if (input) {
328 std::memcpy(input.get(), inp, input_len);
329 }
330}
331
333{
334 return input_len != 0 && std::memcmp(input.get(), other, input_len) == 0;
335}
336
338{
339 std::memset(input.get(), 0, input_len);
341}
342
344{
346 input_len = 0;
347 input = std::unique_ptr<u8[]>();
348}
int32_t i32
Definition gekko_types.h:12
int32_t Frame
Definition gekko_types.h:18
uint8_t u8
Definition gekko_types.h:5
uint32_t u32
Definition gekko_types.h:7
bool IsEqualTo(u8 *other)
Definition input.cpp:332
Frame frame
Definition input.h:23
void Init(GameInput *other)
Definition input.cpp:298
static const i32 NULL_FRAME
Definition input.h:21
std::unique_ptr< u8[]> input
Definition input.h:25
u8 _input_prediction_window
Definition input.h:75
void SetLastReceivedFrame(Frame frame)
Definition input.cpp:197
Frame GetLastReceivedFrame()
Definition input.cpp:192
void SetDelay(u8 delay)
Definition input.cpp:138
std::unique_ptr< u8[]> _empty_input
Definition input.h:81
static const u32 DEFAULT_BUFF_SIZE
Definition input.h:31
bool _running_ahead
Definition input.h:71
void ResetPrediction()
Definition input.cpp:186
void SetRunaheadMode(bool running_ahead)
Definition input.cpp:257
Frame _last_predicted_input
Definition input.h:85
Frame _last_received_input
Definition input.h:83
void AddLocalInput(Frame frame, u8 *input)
Definition input.cpp:50
u32 PreviousFrame(Frame frame)
Definition input.cpp:252
bool HandleInputPrediction(Frame frame)
Definition input.cpp:212
bool CanPredictInput()
Definition input.cpp:244
std::deque< std::unique_ptr< GameInput > > _inputs
Definition input.h:91
void ClearIncorrectFrames(Frame clear_limit)
Definition input.cpp:204
Frame GetIncorrectPredictionFrame()
Definition input.cpp:181
std::unique_ptr< GameInput > GetInput(Frame frame, bool prediction=false)
Definition input.cpp:262
void OverwriteInput(Frame frame, u8 *input)
Definition input.cpp:107
std::deque< Frame > _incorrent_predicted_inputs
Definition input.h:89
void AddInput(Frame frame, u8 *input)
Definition input.cpp:60
Frame _first_predicted_input
Definition input.h:87
void Init(u8 delay, u8 input_window, u32 input_size, u32 buffer_size=DEFAULT_BUFF_SIZE)
Definition input.cpp:23
void SetInputPredictionWindow(u8 input_window)
Definition input.cpp:176