Rev0verDrive

Ue4 PS Ping

Aug 2nd, 2021
636
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void APlayerState::UpdatePing(float InPing)
  2. {
  3.     QUICK_SCOPE_CYCLE_COUNTER(STAT_PlayerState_UpdatePing);
  4.  
  5.     // Limit the size of the ping, to avoid overflowing PingBucket values
  6.     InPing = FMath::Min(1.1f, InPing);
  7.  
  8.     float CurTime = GetWorld()->RealTimeSeconds;
  9.  
  10.     float InPingInMs = InPing * 1000.f;
  11.  
  12.     if ((CurTime - CurPingBucketTimestamp) >= 1.f)
  13.     {
  14.         // Trigger ping recalculation now, while all buckets are 'full'
  15.         //  (misses the latest ping update, but averages a full 4 seconds data)
  16.         RecalculateAvgPing();
  17.  
  18.         CurPingBucket = (CurPingBucket + 1) % UE_ARRAY_COUNT(PingBucket);
  19.         CurPingBucketTimestamp = CurTime;
  20.  
  21.  
  22.         PingBucket[CurPingBucket].PingSum = FMath::FloorToInt(InPingInMs);
  23.         PingBucket[CurPingBucket].PingCount = 1;
  24.  
  25.         PingBucketV2[CurPingBucket] = PingAvgDataV2();
  26.     }
  27.     // Limit the number of pings we accept per-bucket, to avoid overflowing PingBucket values
  28.     else if (PingBucket[CurPingBucket].PingCount < 7)
  29.     {
  30.         PingBucket[CurPingBucket].PingSum += FMath::FloorToInt(InPingInMs);
  31.         PingBucket[CurPingBucket].PingCount++;
  32.     }
  33.  
  34.     TArray<uint16>& CurrentBucketPingValues = PingBucketV2[CurPingBucket].PingValues;
  35.  
  36.     // This makes sure we will actually add the ping value to the list, since much of the time the new ping value will be higher than
  37.     // what is already there.
  38.     if (InPingInMs < CurrentBucketPingValues[PingAvgDataV2::MAX_PING_VALUES_SIZE - 1])
  39.     {
  40.         for (int32 i = 0; i < PingAvgDataV2::MAX_PING_VALUES_SIZE; i++)
  41.         {
  42.             // This will keep the list of ping values we're currently using sorted - this will insert
  43.             // a ping value if it is less than any in current list and remove the new max value.
  44.             if (InPingInMs < CurrentBucketPingValues[i])
  45.             {
  46.                 CurrentBucketPingValues.Insert(FMath::FloorToInt(InPingInMs), i);
  47.                 CurrentBucketPingValues.RemoveAt(PingAvgDataV2::MAX_PING_VALUES_SIZE);
  48.                 break;
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54. void APlayerState::RecalculateAvgPing()
  55. {
  56.     int32 Sum = 0;
  57.     int32 Count = 0;
  58.  
  59.     for (uint8 i=0; i<UE_ARRAY_COUNT(PingBucket); i++)
  60.     {
  61.         Sum += PingBucket[i].PingSum;
  62.         Count += PingBucket[i].PingCount;
  63.     }
  64.  
  65.     int32 SumV2 = 0;
  66.     int32 NumValidValues = 0;
  67.     for (; NumValidValues < PingAvgDataV2::MAX_PING_VALUES_SIZE; NumValidValues++)
  68.     {
  69.         if (PingBucketV2[CurPingBucket].PingValues[NumValidValues] != MAX_uint16)
  70.         {
  71.             SumV2 += PingBucketV2[CurPingBucket].PingValues[NumValidValues];
  72.         }
  73.     }
  74.  
  75.     // Use NumValidValues instead of MAX_PING_VALUES_SIZE in case there are fewer valid values.
  76.     PingBucketV2[CurPingBucket].AvgPingV2 = NumValidValues > 0 ? SumV2 / NumValidValues : MAX_flt;
  77.  
  78.     float AvgSumV2 = 0.0f;
  79.     for (int32 i = 0; i < UE_ARRAY_COUNT(PingBucketV2); i++)
  80.     {
  81.         AvgSumV2 += PingBucketV2[i].AvgPingV2;
  82.     }
  83.  
  84.     ExactPingV2 = AvgSumV2 / UE_ARRAY_COUNT(PingBucketV2);
  85.  
  86.     // Calculate the average, and divide it by 4 to optimize replication
  87.     ExactPing = (Count > 0 ? ((float)Sum / (float)Count) : 0.f);
  88.  
  89.     if (bShouldUpdateReplicatedPing || !HasAuthority())
  90.     {
  91.         SetPing(FMath::Min(255, (int32)(ExactPing * 0.25f)));
  92.     }
  93. }
RAW Paste Data