-
Notifications
You must be signed in to change notification settings - Fork 0
/
PredictionObject.cs
543 lines (466 loc) · 21 KB
/
PredictionObject.cs
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
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
using System;
public class PredictionObject<InputPayload, StatePayload>: NetworkBehaviour where InputPayload : struct, IPayLoad where StatePayload : struct, IPayLoad
{
// Shared
[HideInInspector] public byte ObjectID {get; private set;} = 0;
[HideInInspector] public ushort OwnerID {get; private set;} = 0;
private PayLoadBuffer<InputPayload> inputQueue;
private StatePayload[] stateBuffer;
private InputPayload[] inputBuffer;
//Client
private StatePayload latestServerState;
public StatePayload lastProcessedState;
public bool NeededToReconcile {get; private set;}
public bool DidReceiveState {get; private set;}
//Server
[HideInInspector] public bool DidReceiveInput {get; private set;}
private Message currentDefaultWorldState;
public ClientRpcParams sendParams {get; private set;}
//Interpolation
public Interpolator interpolator;
// =========== Setup ==============
public override void OnNetworkSpawn()
{
stateBuffer = new StatePayload[PredictionManager.BUFFER_SIZE];
inputBuffer = new InputPayload[PredictionManager.BUFFER_SIZE];
inputQueue = new PayLoadBuffer<InputPayload>(25, 5, 3, 100);
DidReceiveInput = false;
NeededToReconcile = false;
OwnerID = Convert.ToUInt16(OwnerClientId);
ObjectID = (byte)NetworkObjectId;
sendParams = new ClientRpcParams {
Send = new ClientRpcSendParams
{
TargetClientIds = new ulong[]{OwnerID}
}
};
if(IsServer) {
PredictionManager.Singleton.OnInputs += ServerHandleTick;
PredictionManager.Singleton.OnSendState += OnSendState;
}
else {
PredictionManager.Singleton.OnInputs += ClientHandleTick;
PredictionManager.Singleton.OnReconcileInputs += ForceSetInput;
PredictionManager.Singleton.OnSetState += SetToPast;
PredictionManager.Singleton.OnShouldRecon += NeedToReconcile;
}
PredictionManager.Singleton.OnState += StoreClientState;
PredictionManager.Singleton.OnComplieState += ReceiveCompileState;
PredictionManager.Singleton.OnSendInput += SendReceiveInput;
PredictionManager.Singleton.OnPreSimulation += OnPreSimulation;
PredictionManager.Singleton.OnPostSimulation += OnPostSimulation;
if (interpolator != null) {
interpolator.IsOwner = IsOwner;
interpolator.IsServer = IsServer;
}
OnPredictionSpawn();
base.OnNetworkSpawn();
}
/// <summary>
/// Runs when the object spawns
/// </summary>
public virtual void OnPredictionSpawn() {}
/// <summary>
/// Runs when the object is destroyed
/// </summary>
public virtual void OnPredictionDespawn() {}
public override void OnNetworkDespawn() {
if(IsServer) {
PredictionManager.Singleton.OnInputs -= ServerHandleTick;
PredictionManager.Singleton.OnSendState -= OnSendState;
}
else {
PredictionManager.Singleton.OnInputs -= ClientHandleTick;
PredictionManager.Singleton.OnReconcileInputs -= ForceSetInput;
PredictionManager.Singleton.OnSetState -= SetToPast;
PredictionManager.Singleton.OnShouldRecon -= NeedToReconcile;
}
PredictionManager.Singleton.OnState -= StoreClientState;
PredictionManager.Singleton.OnComplieState -= ReceiveCompileState;
PredictionManager.Singleton.OnSendInput -= SendReceiveInput;
PredictionManager.Singleton.OnPreSimulation -= OnPreSimulation;
PredictionManager.Singleton.OnPostSimulation -= OnPostSimulation;
OnPredictionDespawn();
base.OnNetworkDespawn();
}
// ============ Simulation Short Cuts =============
/// <summary>
/// Runs before the physics simulation
/// </summary>
/// <param name="DidRunPhysics">if the physics was able to run</param>
public virtual void OnPreSimulation(bool DidRunPhysics) {}
/// <summary>
/// Runs after the physics simulation
/// </summary>
/// <param name="DidRunPhysics">if the physics was able to run</param>
public virtual void OnPostSimulation(bool DidRunPhysics) {}
// ============ Client Logic =============
/// <summary>
/// Gets the new client input, stores it and applies the input
/// </summary>
/// <returns>void</returns>
void ClientHandleTick()
{
if (IsOwner) {
int bufferIndex = PredictionTimer.Singleton.tick % PredictionManager.BUFFER_SIZE;
DidReceiveInput = true;
InputPayload newInput = GetClientInput();
inputBuffer[bufferIndex] = newInput;
SetInput(newInput);
}
else {
ClientHandleOtherClients();
}
}
/// <summary>
/// Applies and stores all the other client inputs (the one that are owned by other clients)
/// </summary>
/// <returns>void</returns>
void ClientHandleOtherClients() {
while(inputQueue.Size() > 0) {
if (inputQueue.Size() > 0) SetInput(inputQueue.Dequeue());
}
inputBuffer[PredictionTimer.Singleton.tick % PredictionManager.BUFFER_SIZE] = inputQueue.GetLastItem();
}
// =========== Client Logic Section 2: Reconcile Logic ============
/// <summary>
/// Decides if this object needs to be reconciled
/// If Reconciliation is needed returns the tick to rewind to otherwise returns -1.
/// </summary>
/// <returns>int - the tick to rewind to</returns>
int NeedToReconcile() {
NeededToReconcile = false;
DidReceiveState = false;
// Ensure that the new state is not equal to the default state and doesn't equal the last state
if (!latestServerState.Equals(default(StatePayload)) &&
(lastProcessedState.Equals(default(StatePayload)) ||
!latestServerState.Equals(lastProcessedState)))
{
// Enure the new state is not in the past
if (lastProcessedState.Tick > latestServerState.Tick) return -1;
DidReceiveState = true;
int serverStateBufferIndex = latestServerState.Tick % PredictionManager.BUFFER_SIZE;
lastProcessedState = latestServerState;
// Run only if prediction of all Clients is enabled (if this is enabled it is very expensive)
if (!PredictionManager.Singleton.PredictAllClients && !IsOwner) {
SetState(lastProcessedState);
return -1;
}
// A user defined function sets the criteria of what defines an desync and when Reconciliation is needed
bool isRecon = ShouldReconcile(lastProcessedState, stateBuffer[serverStateBufferIndex]);
if (isRecon) {
NeededToReconcile = true;
return lastProcessedState.Tick;
}
}
return -1;
}
/// <summary>
/// A user defined method that determines the criteria for Reconciliation.
/// This can be done by comparing the latestServerState with the ClientState at that time.
/// </summary>
/// <param name="latestServerState"> the latest server state received from the server</param>
/// <param name="ClientState"> the client state that happened at a similar time to the received server state</param>
/// <returns>boolean - should the server Reconcile</returns>
public virtual bool ShouldReconcile(StatePayload latestServerState, StatePayload ClientState) {
return true;
}
/// <summary>
/// Sets the latestServerState to the current state and saves it to the buffer at the specified tick
/// </summary>
/// <param name="tick">the tick to save the latestServerState at the corresponding index in the buffer</param>
/// <returns>void</returns>
public void SetToPast(int tick)
{
int bufferIndex = tick % PredictionManager.BUFFER_SIZE;
if (!latestServerState.Equals(default(StatePayload)) &&
!lastProcessedState.Equals(default(StatePayload)))
{
stateBuffer[bufferIndex] = lastProcessedState;
SetState(lastProcessedState);
}
if (IsServer) {
SetState(stateBuffer[bufferIndex]);
}
}
/// <summary>
/// Forces setting the input to a past one by a specified tick.
/// </summary>
/// <param name="tick">The tick for which to set the currnet input to.</param>
/// <returns>void</returns>
void ForceSetInput(int tick) {
if (IsOwner) {
SetInput(inputBuffer[tick % PredictionManager.BUFFER_SIZE]);
}
else {
ClientHandleOtherClients();
}
}
// ========== Server Logic ==========
/// <summary>
/// Run's the server logic every tick
/// </summary>
/// <returns>void</returns>
void ServerHandleTick()
{
if (IsOwner) {
HandleServerPlayer();
}
else {
HandleOtherClients();
}
}
/// <summary>
/// Empties the buffer and Applies the inputs of all the clients (except the server's) in a way that stops
/// the buffer from increasing which reduces latency.
/// </summary>
/// <returns>void</returns>
void HandleOtherClients() {
List<InputPayload> newInputs = inputQueue.DequeueToMaintain();
DidReceiveInput = newInputs.Count > 0? true: false;
for (int i = 0; i < newInputs.Count; i++)
{
SetInput(newInputs[i]);
}
inputBuffer[PredictionTimer.Singleton.tick % PredictionManager.BUFFER_SIZE] = inputQueue.GetLastItem();
}
/// <summary>
/// Gets the new input for the server player and applies the input
/// </summary>
/// <returns>void</returns>
void HandleServerPlayer() {
DidReceiveInput = true;
InputPayload newInput = GetClientInput();
inputBuffer[PredictionTimer.Singleton.tick % PredictionManager.BUFFER_SIZE] = newInput;
SetInput(newInput);
inputQueue.Enqueue(newInput);
inputQueue.Dequeue();
}
// ========= Setting and Getting Inputs =========
/// <summary>
/// Gets the player inputs. This will only run on the client that owns the object
/// </summary>
/// <returns>InputPayload</returns>
public virtual InputPayload GetInput() {
return new InputPayload();
}
/// <summary>
/// Gets the current client state.
/// Guaranteed to set the tick and object ID
/// </summary>
/// <returns>StatePayload - the current state</returns>
InputPayload GetClientInput() {
InputPayload inputPayload = GetInput();
inputPayload.Tick = PredictionTimer.Singleton.tick;
inputPayload.ObjectID = ObjectID;
return inputPayload;
}
/// <summary>
/// Sets the player inputs. This will run on all machines.
/// NOTE: it is only guaranteed to run on the client that owns the object
/// </summary>
/// <returns>void</returns>
public virtual void SetInput(InputPayload input) {}
/// <summary>
/// Gets the player input at a specified tick
/// </summary>
/// <param name="tick">The tick to get the input at</param>
/// <returns>InputPayload</returns>
public InputPayload GetInputAtTick(int tick) {
return inputBuffer[tick % PredictionManager.BUFFER_SIZE];
}
// ========= Sending and Receiving Inputs ==========
/// <summary>
/// If the functions is called on the server it will call a user defined function to apply the client inputs
/// On the client it will get the current and previous inputs and add them to the input message through a user defined function
/// </summary>
/// <param name="sender">The object calling the function (this can be null)</param>
/// <param name="message">Where the inputs are added and applied from</param>
/// <returns>void</returns>
void SendReceiveInput(Message message) {
if (IsServer) {
ReceiveClientInputs(message);
}
else if (IsOwner) {
// redundent inputs and from the past as because of packet loss the server may have not gotten a previous input
// to save bandwidth we don't send redundent inputs confirmed by the server
int RedundentInputsCount = PredictionTimer.Singleton.tick - latestServerState.Tick;
// to save bandwidth we ensure that the redundent inputs don't surpass the MaxRedundentInputs
if (RedundentInputsCount > PredictionManager.Singleton.MaxRedundentInputs) {
RedundentInputsCount = PredictionManager.Singleton.MaxRedundentInputs;
}
// add all redundent inputs to a list then send them to a client defined functions to be added to the input message
List<InputPayload> RedundentInputs = new List<InputPayload>();
for (int i = 0; i < RedundentInputsCount; i++)
{
RedundentInputs.Add(inputBuffer[(PredictionTimer.Singleton.tick - i) % PredictionManager.BUFFER_SIZE]);
}
SendClientInputsToServer(RedundentInputs, message);
}
}
/// <summary>
/// Adds the client inputs to a buffer where they can be applied over time
/// </summary>
/// <param name="inputs">The inputs to be added to the buffer</param>
/// <param name="tick">the tick received from the client</param>
/// <param name="serverDeltaTime">the ping between client and server in ticks</param>
/// <returns>void</returns>
public void AddClientInputs(List<InputPayload> inputs, int tick) {
inputQueue.EnqueueRedundentItems(inputs, tick, PredictionManager.Singleton.MaxRedundentInputs);
}
/// <summary>
/// Adds the client inputs to the input message
/// </summary>
/// <param name="inputs">The inputs to be added to the message</param>
/// <param name="sender">the message that will be sent to the server</param>
/// <returns>void</returns>
public virtual void SendClientInputsToServer(List<InputPayload> inputs, Message sender) {}
/// <summary>
/// Applies the inputs using the AddClientInputs() method
/// </summary>
/// <param name="receiver">the input message received from the client</param>
/// <returns>void</returns>
public virtual void ReceiveClientInputs(Message receiver) {}
// ========= Setting and Getting States =========
/// <summary>
/// Gets the state at a specified tick
/// </summary>
/// <param name="tick">the input message received from the client</param>
/// <returns>StatePayload - The state at that tick</returns>
public StatePayload GetStateAtTick(int tick) {
return stateBuffer[tick % PredictionManager.BUFFER_SIZE];
}
/// <summary>
/// sets the current state to a previous state a specified tick
/// Basically rewinds to a previous tick
/// </summary>
/// <param name="tick">the tick to rewind to</param>
/// <returns>void</returns>
void StoreClientState(int tick) {
stateBuffer[tick % PredictionManager.BUFFER_SIZE] = GetState();
stateBuffer[tick % PredictionManager.BUFFER_SIZE].Tick = tick;
stateBuffer[tick % PredictionManager.BUFFER_SIZE].ObjectID = ObjectID;
SetState(stateBuffer[tick % PredictionManager.BUFFER_SIZE]);
}
/// <summary>
/// Gets the current client state.
/// Guaranteed to set the tick and object ID
/// </summary>
/// <returns>StatePayload - the current state</returns>
StatePayload GetClientState() {
StatePayload statePayload = GetState();
statePayload.Tick = inputQueue.GetLastItem().Tick;
statePayload.ObjectID = ObjectID;
return statePayload;
}
/// <summary>
/// Gets the current client state
/// </summary>
/// <returns>StatePayload - the current state</returns>
public virtual StatePayload GetState() {
return new StatePayload();
}
/// <summary>
/// Sets the current state
/// </summary>
/// <param name="statePayload">The state to set the current state to</param>
/// <returns>void</returns>
public virtual void SetState(StatePayload statePayload) {}
// ========= Sending and Receiving States ==========
/// <summary>
/// On the server the function will compile all the states and inputs into one state message
/// On the client it will apply the states received to the ojects through a user defined function
/// </summary>
/// <param name="sender">The object calling the function (this can be null)</param>
/// <param name="message">Where the states are compiled and applied from</param>
/// <returns>void</returns>
void ReceiveCompileState(Message message) {
if (IsServer) {
CompileServerState(GetClientState(), inputQueue.GetLastItems(), message);
currentDefaultWorldState = message;
}
else {
ReceiveServerState(message);
}
}
/// <summary>
/// Creates customized messages defined by the user that are sent to the client.
/// This helps save bandwidth each client gets only what they need
/// </summary>
/// <param name="sender">The object calling the function (this can be null)</param>
/// <param name="messages">Customized state Messages for each client</param>
/// <returns>void</returns>
void OnSendState(List<Message> messages) {
// Loop through the current state message list to see if this object's Client ID is in it.
// If it is call the user defined function SortState() to customize the medthod to lower bandwidth
for (int i = 0; i < messages.Count; i++)
{
if (messages[i].OwnerID == OwnerID) {
SortStateToSendToClient(currentDefaultWorldState, messages[i]);
return;
}
}
// We the state message does nor exist for this object's Client, we create it and add it to the list
Message holder = new Message
{
OwnerID = OwnerID,
tick = inputQueue.GetLastItem().Tick,
sendParams = sendParams
};
SortStateToSendToClient(currentDefaultWorldState, holder);
messages.Add(holder);
}
/// <summary>
/// Adds only relevant information from the defaultWorldState to the sender to save bandwidth.
/// For example is if a prediction object is out of sight of a client, don't send state information for that object.
/// </summary>
/// <param name="defaultWorldState">The defualt state containing all the states/inputs of every prediction object</param>
/// <param name="sender">The customized state to send to the client to save bandwidth</param>
/// <returns>void</returns>
public virtual void SortStateToSendToClient(Message defaultWorldState, Message sender) {}
/// <summary>
/// Adds the client inputs to the state or/and inputs to the state message
/// </summary>
/// <param name="statePayload">The state to be added to the message</param>
/// <param name="inputPayloads">The inputs to be added to the message</param>
/// <param name="sender">the message that will be sent to the clients</param>
/// <returns>void</returns>
public virtual void CompileServerState(StatePayload statePayload, List<InputPayload> inputPayloads, Message sender) {}
/// <summary>
/// Applies the states using the ApplyServerState() method
/// Applies the inputs using the ApplyServerInputs() method
/// </summary>
/// <param name="receiver">the state message received from the client</param>
/// <returns>void</returns>
public virtual void ReceiveServerState(Message receiver) {}
/// <summary>
/// Applies the new server state if the client has desynced
/// </summary>
/// <param name="state">the state to apply</param>
/// <param name="tick">the server tick</param>
/// <returns>void</returns>
public void ApplyServerState(StatePayload state, int tick) {
if (!state.Equals(default(StatePayload))) {
state.Tick = tick;
latestServerState = state;
}
}
/// <summary>
/// Applies the new server input from the server for objects that this client doesn't own
/// </summary>
/// <param name="state">the input to apply</param>
/// <param name="tick">the server tick</param>
/// <returns>void</returns>
public void ApplyServerInputs(List<InputPayload> inputPayloads, int tick) {
if (IsServer || IsOwner) return;
for (int i = 0; i < inputPayloads.Count; i++)
{
InputPayload newInput = inputPayloads[i];
newInput.Tick = tick;
inputQueue.Enqueue(newInput);
}
}
}