# Code Confuse (FPS.Sample NetworkPrediction)

Discussion in 'FPS.Sample Game' started by Leo_Li, Jun 18, 2019.

1. ### Leo_Li

Joined:
Dec 24, 2014
Posts:
2
Hi, I am a developer trying to read your FPS source code. Something make me so confuse about your (NetworkPrediction.cs) . I have try lots of times to understand your purpose , but still confuse. Let me paste some code :
long d1 = vl0 - pl0;
long d2 = vl0 - vl1;
d1 = d1 > 0 ? d1 : -d1;
d2 = d2 > 0 ? d2 : -d2;
if(d1 < d2)
{
long pl = vl1 + (vl0 - vl1) * frac / 16;// (timel - timel1) / (timel0 - timel1);
predictionLikelyWrong = pl0 != vl0;
prediction = (uint)pl;
}
else
{
predictionLikelyWrong = (baseline0 != baseline1) && (baseline1 != baseline2);
prediction = baseline0;
}

.... what the hell ? what that means ? And every snapshot , you will use this to decide whether the snapshot data field should igonre. Can you give some explanation for us?

2. ### Aquastar

Joined:
Jul 17, 2013
Posts:
23
I am not sure if I am right, but let me try.

1. Calculate pl0 (prediction long 0) by the extrapolation of (timel0 - timel2) / (timel1 - timel2).
2. Decide if it is linear (or less) by d1 and d2.
3. If 2 is true, then extrapolate the value of (timel - timel1) / (timel0 - timel1) as the predicted value pl.
4. Mark the field changed if prediction 0 is not equal to the baseline value 0.

In short, the code tried to predict values by extrapolate between 3 baselines. There are 2 predicted values. One is for check if values are linear or less, and the other one is the actually predicted value (to be put in outputData).

Leo_Li and AggressiveMastery like this.
3. ### AggressiveMastery

Joined:
Nov 19, 2018
Posts:
143
Nice reply @Aquastar , that sounds like what I remember from the networking video. Leo, have you watched this? I believe it talks about how they do prediction a bit.

Leo_Li likes this.
4. ### Leo_Li

Joined:
Dec 24, 2014
Posts:
2
@Aquastar @
oh, thanks!! I think i got it. The final message is the difference between prediction and real delta value. Therefore, it must insure the two side (sender & receiver) must hold the same baselines datas.
However, as I read the code in detail. I found another thing made me confuse:

- In the Server side (NetworkServer.cs), the baseline datas is get by the direct correspond sequence, which is fine :
// Newly spawned entities might not have earlier baselines initially
if (snapshot1Baseline != snapshot0Baseline && entity.snapshots.Exists(snapshot1Baseline))
{
num_baselines = 2;
baseline1 = entity.snapshots[snapshot1Baseline].start;
time1 = server.m_Snapshots[snapshot1Baseline % server.m_Snapshots.Length].serverTime;

if (snapshot2Baseline != snapshot1Baseline && entity.snapshots.Exists(snapshot2Baseline))
{
num_baselines = 3;
baseline2 = entity.snapshots[snapshot2Baseline].start;
//time2 = entity.snapshots[snapshot2Baseline].serverTime;
time2 = server.m_Snapshots[snapshot2Baseline % server.m_Snapshots.Length].serverTime;
}
}
- But in the Client side, the baseline datas was got by the function "GetMax", which made me confuse is that a risk to make the two side baselines different?
if (baseSequence1 != baseSequence)
{
baseline1 = info.baselines.FindMax(baseSequence1);
if (baseline1 != null)
{
num_baselines = 2;
baseline1Time = snapshots[baseSequence1].serverTime;
}
if (baseSequence2 != baseSequence1)
{
baseline2 = info.baselines.FindMax(baseSequence2);
if (baseline2 != null)
{
num_baselines = 3;
baseline2Time = snapshots[baseSequence2].serverTime;
}
}
}