_MM_IKKE

[PAWN] Anti Lag System explained

Jul 22nd, 2012
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. Tutorial on how to make something lag-proof. Calculations done by me, scripting is up to you!
  2.  
  3.  
  4.  
  5. A player is moving. His position in his client is 'O'. He lags, so the server gets a previous position, the position of the player <ping> milliseconds earlier. This is position 'P'. Position P is also the position you get using GetPlayerPos
  6.  
  7. P-------------------O
  8.  
  9. The player reaches a velocity 'V'
  10.  
  11. P------------------O------V
  12.  
  13. When in the velocity, nothing happens. The server doesn't know yet he's in the velocity.
  14.  
  15. P-----------------O
  16. V
  17.  
  18. The server sends the velocity to the player when point 'P' is at 'V' (definition of P: position the server retrieves at the moment the player is at 'O')
  19.  
  20. P----------------O
  21. V
  22. >server sends velocity
  23.  
  24. The velocity reaches the player after <ping> milliseconds.
  25.  
  26. V----------------P----------------O
  27. >player gets velocity
  28.  
  29. The player is - in his client - much further.
  30.  
  31.  
  32. We can adjust this by calculating the O. If we multiply his ping by his velocity, we get the distance between 'P' and 'O'. We'll name this 'R'.
  33.  
  34. >> R = P + ping*velocity
  35.  
  36. P-------------------O
  37. R
  38.  
  39. The player reaches a velocity 'V'
  40.  
  41. P------------------O------V
  42. R
  43.  
  44. When in the velocity, the server sends the velocity due to the adjusted position 'R'
  45.  
  46. P------------------O
  47. R
  48. V
  49. >server sends velocity
  50.  
  51. The velocity reaches the player after <ping> milliseconds. This is (when the ping remains the same) the same distance as |PO|.
  52.  
  53. P------------------O
  54. V R
  55. >player gets velocity
  56.  
  57. The player is - in his client - a little further.
  58.  
  59. We can calculate yet another position, his future position 'F'.
  60.  
  61. >> F = R + ping*velocity = P + ping*velocity*2
  62.  
  63. P-------------------O-------------------F
  64. R
  65.  
  66. The player gets close to a velocity 'V'
  67.  
  68. P-------------------O-------------------F---V
  69. R
  70.  
  71. When his future position is in 'V', we let the server send him the velocity
  72.  
  73. P-------------------O-------------------F
  74. R V
  75. >server sends velocity
  76.  
  77. The player recieves the velocity |PO| = |OF| later.
  78.  
  79. P-------------------O-------------------F
  80. R
  81. V
  82. >player gets the velocity
  83.  
  84. This is exactly how we want it.
  85.  
  86. To do this, we'll need to adjust GetPlayerPos by changing the callbacks and functions.
  87. Though I'm not sure if you can call the real function within the adjusted functions.
  88.  
  89. eg
  90. GetPlayerPos(playerid, X, Y, Z) {
  91. GetPlayerPos(playerid, x, y, z); // the default player pos -> this is where I doubt it'll work
  92. GetPlayerVelocity(playerid, vx, vy, vz);
  93. new p = GetPlayerPing(playerid);
  94. X = x + vx*p*2;
  95. Y = y + vy*p*2;
  96. Z = z + vz*p*2;
  97. }
  98.  
  99. The problem is other callbacks/functions use GetPlayerPos internally. An example is: IsPlayerInRangeOfPoint.
  100.  
  101. IsPlayerInRangeOfPoint(playerid, Float:range, Float:x, Float:y, Float:z)
  102. -> internally it gets the player position using the playerid.
  103.  
  104. This can be adjusted as well, though it'll be a lot harder. Another option would be to adjust the range instead of the player position - that'll use a lot less resources and won't blow my mind during the calculations of creating a sphere (because that's what the function does).
  105.  
  106. If we set the range
  107. >>range = default range + ping*velocity*2;
  108.  
  109. I think that will work as well. You can adjust most functions using this method.
  110.  
  111. OnPlayerEnterCheckpoint however and other related callbacks will be a bigger problem though, as they use nothing but the playerid.
  112.  
  113. --------------------------------------------------------------
  114.  
  115. Testing showed that the theoretical value 2 (one to send the info to the server and one to get the server info) is incorrect. Apparently some more stuff happens. The value is estimated between 40 and 50. Try on people with a high ping and check what works out best.
  116.  
  117. >>range = default range + ping*velocity*40;
  118. >>range = default range + ping*velocity*50;
Advertisement
Add Comment
Please, Sign In to add comment