Zh/Lag Compensation: Difference between revisions

From Valve Developer Community
< Zh
Jump to navigation Jump to search
(Created page with "{{subst:#if: Translation of 'Lag Compensation' to '中文' via Template:LanguageBar buttons * * * * * * * * * * * * * * * * * * * * * * * * * * * *...")
 
(→‎配置: Fixed cvar table)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{subst:#if:|||{{LAuto/t}}
{{wip}}{{translate}}
 
--- 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}}
 
}}{{wip}}{{translate}}
{{LanguageBar}}
{{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]].''
: ''你可能在寻找{{L|Latency Compensating Methods in Client/Server In-game Protocol Design and Optimization|Yahn Bernier 2001年关于游戏引擎网络设计的论文}}。''
 
[[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.
[[File:Lag compensation.jpg|thumb|历史客户端命中框(红色)与服务器回溯命中框(蓝色)对比。]]


By default only players are rewound, and one second of location/animation history is kept.
'''延迟补偿'''是服务器在处理{{L|usercmd}}时,利用玩家延迟回退时间的机制,以便重现玩家发送指令时的视野状态。结合{{L|prediction|预测}}技术,延迟补偿能有效对抗网络延迟,从攻击者视角几乎消除其影响。更详细的解释请参阅{{L|Source Multiplayer Networking#Lag compensation|《Source多人游戏网络机制}}文章。


{{note|[[CBasePlayer]] does not use lag compensation; it must be implemented by a derived class. Valve's networked player classes do this already.}}
默认情况下仅对玩家进行回溯,且保留1秒的位置/动画历史数据。


{{tip|Since {{l4d2}}, ''all'' entities can be lag compensated using the <code>LagCompensate</code> {{not in FGD}} keyvalue. Use this sparingly!}}
{{note|{{L|CBasePlayer}}未使用延迟补偿功能,需通过派生类实现。Valve的网络化玩家类已内置此功能。}}


== Configuration ==
{{tip|自{{l4d2}}起,''所有''实体均可通过<code>LagCompensate</code> {{not in FGD}}键值启用延迟补偿。请谨慎使用!}}
{{varcom|start}}
{{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.}}
{{varcom|cl_lagcomp_errorcheck||int|Deprecated? In theory displays error information for the given player index, in practice is not referenced anywhere.}}
{{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?}}}}
{{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.}}


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|sv_unlag||bool|Enable/disable lag compensation entirely.}}
{{Varcom|start}}
{{varcom|sv_maxunlag||float|Number of seconds to store player positions for. Default (and maximum) is 1.}}
{{Varcom|cl_lagcompensation||bool|允许客户端告知服务器是否禁用自身指令的延迟补偿。''非作弊''指令。}}
{{varcom|end}}
{{Varcom|cl_lagcomp_errorcheck||int|已弃用?理论上显示指定玩家索引的错误信息,实际无任何引用。}}
{{Varcom|sv_lagcompensationforcerestore||bool|作弊指令。禁用后,游戏会检查每个实体在未回溯的当前世界中是否能占据回溯位置。若不能,则回退至最后有效位置而非目标位置。{{todo|为何需要这种机制?}}}}
{{Varcom|sv_showlagcompensation||bool|作弊指令。当玩家触发延迟补偿时显示回溯后的{{L|hitbox|命中框}}。取代了旧版<code>sv_showhitboxes</code>命令,避免补偿未生效时显示命中框造成的混淆。}}
{{Varcom|end}}
开发者服务器版本(使用<code>ALLOW_DEVELOPMENT_CVARS</code>编译)或通过服务器插件(如[https://www.sourcemod.net/ SourceMod])可启用隐藏控制台命令,获得额外指令:
{{Varcom|start}}
{{Varcom|sv_unlag||bool|全局启用/禁用延迟补偿。}}
{{Varcom|sv_maxunlag||float|存储玩家位置的历史时长(秒)。默认(及最大值)为1。}}
{{Varcom|end}}


== Invocation ==
== 调用方法 ==


Simply wrap whatever code you want lag compensated within two calls to the <code>lagcompensation</code> object:
只需将要进行延迟补偿的代码包裹在<code>lagcompensation</code>对象的两个调用之间:


<source lang=cpp>
<source lang=cpp>
Line 50: Line 41:
lagcompensation->StartLagCompensation( this, this->GetCurrentCommand() );
lagcompensation->StartLagCompensation( this, this->GetCurrentCommand() );


// Alien Swarm and later (See also sub-section below)
// Alien Swarm及后续版本(参见下方子章节)
lagcompensation->StartLagCompensation( this, LAG_COMPENSATE_HITBOXES );
lagcompensation->StartLagCompensation( this, LAG_COMPENSATE_HITBOXES );


Line 60: Line 51:
</source>
</source>


Time will be wound back on the call to <code>StartLagCompensation()</code>, and wound forward again on the call to <code>FinishLagCompensation()</code>.
时间将在调用<code>StartLagCompensation()</code>时回退,在<code>FinishLagCompensation()</code>时恢复。


== Lag Compensation Type ==
== 延迟补偿类型 ==


Starting with Alien Swarm, you can optimise lag compensation with these settings:
自Alien Swarm起,可通过以下设置优化延迟补偿:


; <code>LAG_COMPENSATE_BOUNDS</code>
; <code>LAG_COMPENSATE_BOUNDS</code>
: Rewind entities' locations only, skipping animations.
: 仅回溯实体位置,跳过动画处理。
; <code>LAG_COMPENSATE_HITBOXES</code>
; <code>LAG_COMPENSATE_HITBOXES</code>
: Rewind both locations and [[hitbox]]es. This is the standard setting.
: 同时回溯位置和{{L|hitbox|命中框}}。此为标准设置。
; <code>LAG_COMPENSATE_HITBOXES_ALONG_RAY</code>
; <code>LAG_COMPENSATE_HITBOXES_ALONG_RAY</code>
: Rewind hitboxes if an entity is hit by a ray defined by the "weaponPos" <code>StartLagCompensation()</code> overload. Otherwise rewind bounds only.
: 当实体被"weaponPos"参数重载的<code>StartLagCompensation()</code>定义射线击中时,回溯命中框。否则仅回溯位置。


These setting chosen applies to ''all'' entities affected by the rewind.
所选设置将作用于''所有''受回溯影响的实体。


== Implementation ==
== 实现细节 ==


{{todo|Supporting lag compensation on new entities.}}
{{todo|如何为新增实体实现延迟补偿支持。}}


== See also ==
== 另请参阅 ==


* [[Source Multiplayer Networking#Lag compensation]]
* {{L|Source Multiplayer Networking#Lag compensation|《Source多人游戏网络机制:延迟补偿》}}
* [[NPC Lag Compensation]]
* {{L|NPC Lag Compensation|NPC延迟补偿}}
* [[Prediction]]
* {{L|Prediction|预测}}
* [[Interpolation]]
* {{L|Interpolation|插值}}


[[Category:Networking]]
{{ACategory|Networking}}

Latest revision as of 16:01, 21 April 2025

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)
你可能在寻找Yahn Bernier 2001年关于游戏引擎网络设计的论文(en)
历史客户端命中框(红色)与服务器回溯命中框(蓝色)对比。

延迟补偿是服务器在处理usercmd(en)时,利用玩家延迟回退时间的机制,以便重现玩家发送指令时的视野状态。结合预测(en)技术,延迟补偿能有效对抗网络延迟,从攻击者视角几乎消除其影响。更详细的解释请参阅《Source多人游戏网络机制(en)文章。

默认情况下仅对玩家进行回溯,且保留1秒的位置/动画历史数据。

Note.png注意:CBasePlayer(en)未使用延迟补偿功能,需通过派生类实现。Valve的网络化玩家类已内置此功能。
Tip.png提示:求生之路2起,所有实体均可通过LagCompensate 不存在于FGD!键值启用延迟补偿。请谨慎使用!

配置

控制台变量/命令 参数或默认值 描述符 效果
cl_lagcompensation bool 允许客户端告知服务器是否禁用自身指令的延迟补偿。非作弊指令。
cl_lagcomp_errorcheck int 已弃用?理论上显示指定玩家索引的错误信息,实际无任何引用。
sv_lagcompensationforcerestore bool 作弊指令。禁用后,游戏会检查每个实体在未回溯的当前世界中是否能占据回溯位置。若不能,则回退至最后有效位置而非目标位置。
待完善: 为何需要这种机制?
sv_showlagcompensation bool 作弊指令。当玩家触发延迟补偿时显示回溯后的命中框(en)。取代了旧版sv_showhitboxes命令,避免补偿未生效时显示命中框造成的混淆。

开发者服务器版本(使用ALLOW_DEVELOPMENT_CVARS编译)或通过服务器插件(如SourceMod)可启用隐藏控制台命令,获得额外指令:

控制台变量/命令 参数或默认值 描述符 效果
sv_unlag bool 全局启用/禁用延迟补偿。
sv_maxunlag float 存储玩家位置的历史时长(秒)。默认(及最大值)为1。

调用方法

只需将要进行延迟补偿的代码包裹在lagcompensation对象的两个调用之间:

#ifdef GAME_DLL

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

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

	// Alien Swarm及后续版本(参见下方子章节)
	lagcompensation->StartLagCompensation( this, LAG_COMPENSATE_HITBOXES );

	BaseClass::FireBullets(info);

	lagcompensation->FinishLagCompensation( this );
}
#endif

时间将在调用StartLagCompensation()时回退,在FinishLagCompensation()时恢复。

延迟补偿类型

自Alien Swarm起,可通过以下设置优化延迟补偿:

LAG_COMPENSATE_BOUNDS
仅回溯实体位置,跳过动画处理。
LAG_COMPENSATE_HITBOXES
同时回溯位置和命中框(en)。此为标准设置。
LAG_COMPENSATE_HITBOXES_ALONG_RAY
当实体被"weaponPos"参数重载的StartLagCompensation()定义射线击中时,回溯命中框。否则仅回溯位置。

所选设置将作用于所有受回溯影响的实体。

实现细节

待完善: 如何为新增实体实现延迟补偿支持。

另请参阅