Lag Compensation

From Valve Developer Community
< Zh
Revision as of 05:11, 26 March 2025 by MoRanYue (talk | contribs) (Created page with "{{subst:#if: Translation of 'Lag Compensation' to '中文' via Template:LanguageBar buttons * * * * * * * * * * * * * * * * * * * * * * * * * * * *...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

{{subst:#if:|||

Important step for replacing wikilinks after you've created this page

After you click 'Edit' do what the image shows. If you can't see editing toolbar you need to enable it in 'Preferences' -> Editing -> checkbox 'Enable the editing toolbar'

Preload - Language Links Replace.jpg
参见:  {{LAuto}}

--- DON'T JUST BLINDLY DELETE THIS PART. DO REPLACE THE LINKS AND CATEGORIES. THE PICTURE SHOWS HOW TO USE IT ! ---

SEARCH FOR: \[\[(?!#|File(?:[ _]talk)?:|Image(?:[ _]talk)?:|Media:|Template(?:[ _]talk)?:|MediaWiki(?:[ _]talk)?:|Talk:|Category[ _]talk:|Project[ _]talk:|Valve[ _]Developer[ _]Community[ _]talk:|Help[ _]talk:|User(?:[ _]talk)?:|c:|commons:|Dictionary:|Google:|GoogleGroups:|IMDB:|M:|Meta:|Metawikipedia:|MW:|SdkBug:|SourceForge:|Steampowered:|W:|Wiki:|WikiBooks:|Wikipedia:|Wikiquote:|Wiktionary:|WP:)(:?(?:Category|Category|Help|Project|Valve[ _]Developer[ _]Community|Special|)(?:[^\|\]]+))(\|?.*?)\]\]

REPLACE WITH: {{subst:LAuto|$1$2}}

}}
Under construction.png
This page is actively undergoing a major edit.
As a courtesy, please do not edit this while this message is displayed.
If this page has not been edited for at least several hours to a few days, please remove this template. This message is intended to help reduce edit conflicts; please remove it between editing sessions to allow others to edit the page.

The person who added this notice will be listed in its edit history should you wish to contact them.

Info content.png
This page needs to be translated.
This page either contains information that is only partially or incorrectly translated, or there isn't a translation yet.
If this page cannot be translated for some reason, or is left untranslated for an extended period of time after this notice is posted, the page should be requested to be deleted.
Also, please make sure the article complies with the alternate languages guide.(en)
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.png注意:CBasePlayer does not use lag compensation; it must be implemented by a derived class. Valve's networked player classes do this already.
Tip.png提示:Since 求生之路2, all entities can be lag compensated using the LagCompensate 不存在于FGD! keyvalue. Use this sparingly!

Configuration

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:
控制台变量/命令 参数或默认值 描述符 效果
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.
待完善: 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.
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

待完善: Supporting lag compensation on new entities.

See also