Lag Compensation: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
mNo edit summary
(→‎Configuration: Fixed cvar table)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{LanguageBar}}
: ''You may be looking for [[Latency Compensating Methods in Client/Server In-game Protocol Design and Optimization|Yahn Bernier's 2001 paper on game engine networking]].''
: ''You may be looking for [[Latency Compensating Methods in Client/Server In-game Protocol Design and Optimization|Yahn Bernier's 2001 paper on game engine networking]].''


[[File:Lag compensation.jpg|thumb|Historic client hitboxes (red) versus rewound server hitboxes (blue).]]
[[File:Lag compensation.jpg|thumb|Historic client hitboxes (red) versus rewound server hitboxes (blue).]]


'''Lag compensation''' is the notion of the server using a player's latency to rewind time when processing a [[usercmd]], in order to see what the player saw when the command was sent. In combination with [[prediction]], lag compensation can help to combat network latency to the point of almost eliminating it from the perspective of an attacker. For a more detailed explanation see the [[Source Multiplayer Networking#Lag compensation|Source Multiplayer Networking]] article.
'''Lag compensation''' is the notion of the server using a player's latency to rewind time when processing a [[usercmd]], in order to see what the player saw when the command was sent. In combination with [[prediction]], lag compensation can help to combat network latency to the point of almost eliminating it from the perspective of an attacker. For a more detailed explanation, see the [[Source Multiplayer Networking#Lag compensation|Source Multiplayer Networking]] article.


By default only players are rewound, and one second of location/animation history is kept.
By default only players are rewound, and one second of location/animation history is kept.


{{note|<code>[[CBasePlayer]]</code> does not use lag compensation; it must be implemented by a derived class. Valve's networked player classes do this already.}}
{{note|[[CBasePlayer]] does not use lag compensation; it must be implemented by a derived class. Valve's networked player classes do this already.}}


{{tip|In {{l4d2}} Left 4 Dead 2, [[prop_physics]] entities can be lag compensated. Do this sparingly!}}
{{tip|Since {{l4d2}}, ''all'' entities can be lag compensated using the <code>LagCompensate</code> {{not in FGD}} keyvalue. Use this sparingly!}}


== Configuration ==
== Configuration ==
 
{{Varcom|start}}
{{IO|cl_lagcompensation|param=bool|Allows clients to tell the server that they don't want their own commands lag compensated. This is ''not'' a cheat.}}
{{Varcom|cl_lagcompensation||bool|Allows clients to tell the server that they don't want their own commands lag compensated. This is ''not'' a cheat.}}
{{IO|cl_lagcomp_errorcheck|param=int|Deprecated? In theory displays error information for the given player index, in practice is not referenced anywhere.}}
{{Varcom|cl_lagcomp_errorcheck||int|Deprecated? In theory displays error information for the given player index, in practice is not referenced anywhere.}}
{{IO|sv_lagcompensationforcerestore|param=bool|Cheat. Disabling this causes the game to check if each entity can still occupy its rewind position in the current, un-rewound world. If it can't, it is rewound to the last valid position instead of the desired one. {{todo|Why would anyone want that to happen?}}}}
{{Varcom|sv_lagcompensationforcerestore||bool|Cheat. Disabling this causes the game to check if each entity can still occupy its rewind position in the current, un-rewound world. If it can't, it is rewound to the last valid position instead of the desired one. {{todo|Why would anyone want that to happen?}}}}
{{IO|sv_showlagcompensation|param=bool|Cheat. Displays rewound [[hitbox]]es whenever a player is lag compensated. This fills the role of the old <code>sv_showhitboxes</code> command, only without the confusion caused by the hitboxes also being shown when compensation is not in effect.}}
{{Varcom|sv_showlagcompensation||bool|Cheat. Displays rewound [[hitbox]]es whenever a player is lag compensated. This fills the role of the old <code>sv_showhitboxes</code> command, only without the confusion caused by the hitboxes also being shown when compensation is not in effect.}}
 
{{Varcom|end}}
Developer server builds (those compiled with <code>ALLOW_DEVELOPMENT_CVARS</code>) have some extra commands:
Developer server builds (those compiled with <code>ALLOW_DEVELOPMENT_CVARS</code>), or by using server plugins such as [https://www.sourcemod.net/ SourceMod] (to enable hidden console commands) have some extra commands:
 
{{Varcom|start}}
{{IO|sv_unlag|param=bool|Enable/disable lag compensation entirely.}}
{{Varcom|sv_unlag||bool|Enable/disable lag compensation entirely.}}
{{IO|sv_maxunlag|param=float|Number of seconds to store player positions for. Default (and maximum) is 1.}}
{{Varcom|sv_maxunlag||float|Number of seconds to store player positions for. Default (and maximum) is 1.}}
{{Varcom|end}}


== Invocation ==
== Invocation ==
Line 37: Line 40:
lagcompensation->StartLagCompensation( this, this->GetCurrentCommand() );
lagcompensation->StartLagCompensation( this, this->GetCurrentCommand() );


// Alien Swarm and later (see also sub-section below)
// Alien Swarm and later (See also sub-section below)
lagcompensation->StartLagCompensation( this, LAG_COMPENSATE_HITBOXES );
lagcompensation->StartLagCompensation( this, LAG_COMPENSATE_HITBOXES );



Latest revision as of 15:59, 21 April 2025

English (en)Español (es)中文 (zh)Translate (Translate)
You may be looking for Yahn Bernier's 2001 paper on game engine networking.
Historic client hitboxes (red) versus rewound server hitboxes (blue).

Lag compensation is the notion of the server using a player's latency to rewind time when processing a usercmd, in order to see what the player saw when the command was sent. In combination with prediction, lag compensation can help to combat network latency to the point of almost eliminating it from the perspective of an attacker. For a more detailed explanation, see the Source Multiplayer Networking article.

By default only players are rewound, and one second of location/animation history is kept.

Note.pngNote:CBasePlayer does not use lag compensation; it must be implemented by a derived class. Valve's networked player classes do this already.
Tip.pngTip:Since Left 4 Dead 2, all entities can be lag compensated using the LagCompensate !FGD keyvalue. Use this sparingly!

Configuration

Cvar/Command Parameters or default value Descriptor Effect
cl_lagcompensation bool Allows clients to tell the server that they don't want their own commands lag compensated. This is not a cheat.
cl_lagcomp_errorcheck int Deprecated? In theory displays error information for the given player index, in practice is not referenced anywhere.
sv_lagcompensationforcerestore bool Cheat. Disabling this causes the game to check if each entity can still occupy its rewind position in the current, un-rewound world. If it can't, it is rewound to the last valid position instead of the desired one.
Todo: Why would anyone want that to happen?
sv_showlagcompensation bool Cheat. Displays rewound hitboxes whenever a player is lag compensated. This fills the role of the old sv_showhitboxes command, only without the confusion caused by the hitboxes also being shown when compensation is not in effect.

Developer server builds (those compiled with ALLOW_DEVELOPMENT_CVARS), or by using server plugins such as SourceMod (to enable hidden console commands) have some extra commands:

Cvar/Command Parameters or default value Descriptor Effect
sv_unlag bool Enable/disable lag compensation entirely.
sv_maxunlag float Number of seconds to store player positions for. Default (and maximum) is 1.

Invocation

Simply wrap whatever code you want lag compensated within two calls to the lagcompensation object:

#ifdef GAME_DLL

#include "..\server\ilagcompensationmanager.h"

void CMyPlayer::FireBullets ( const FireBulletsInfo_t &info )
{
	// Source 2007
	lagcompensation->StartLagCompensation( this, this->GetCurrentCommand() );

	// Alien Swarm and later (See also sub-section below)
	lagcompensation->StartLagCompensation( this, LAG_COMPENSATE_HITBOXES );

	BaseClass::FireBullets(info);

	lagcompensation->FinishLagCompensation( this );
}
#endif

Time will be wound back on the call to StartLagCompensation(), and wound forward again on the call to FinishLagCompensation().

Lag Compensation Type

Starting with Alien Swarm, you can optimise lag compensation with these settings:

LAG_COMPENSATE_BOUNDS
Rewind entities' locations only, skipping animations.
LAG_COMPENSATE_HITBOXES
Rewind both locations and hitboxes. This is the standard setting.
LAG_COMPENSATE_HITBOXES_ALONG_RAY
Rewind hitboxes if an entity is hit by a ray defined by the "weaponPos" StartLagCompensation() overload. Otherwise rewind bounds only.

These setting chosen applies to all entities affected by the rewind.

Implementation

Todo: Supporting lag compensation on new entities.

See also