关于 synchronization-algorithms
面向多人游戏同步开发与排障的参考技能,介绍客户端预测、插值、回滚和锁步技术,并按游戏类型提供选择指导。
主要功能
- 01客户端预测与校正
- 02插值缓冲
- 03回滚网络同步
- 04锁步同步
- 05按游戏类型选择技术
适用场景
- 01为游戏选择同步方法
- 02减少橡皮筋效应和校正问题
- 03诊断多人游戏状态不同步
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pluginagentmarketplace/ custom-plugin-game-developer --skill synchronization-algorithms安装后在 Claude Code 中输入/synchronization-algorithms确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
assets/sync_config.yamlreferences/SYNC_GUIDE.mdscripts/sync_helper.py详细介绍
多人同步
同步技术
┌─────────────────────────────────────────────────────────────┐
│ SYNC TECHNIQUES OVERVIEW │
├─────────────────────────────────────────────────────────────┤
│ CLIENT PREDICTION: │
│ → Execute input locally before server confirms │
│ → Feels responsive, requires reconciliation │
│ Best for: FPS, action games │
├─────────────────────────────────────────────────────────────┤
│ INTERPOLATION: │
│ → Display positions between known states │
│ → Smooth visuals, adds latency │
│ Best for: Other players, NPCs │
├─────────────────────────────────────────────────────────────┤
│ ROLLBACK NETCODE: │
│ → Rewind game state on correction │
│ → Re-simulate with corrected data │
│ Best for: Fighting games, precise timing │
├─────────────────────────────────────────────────────────────┤
│ LOCKSTEP: │
│ → All clients advance together │
│ → Deterministic, waits for slowest │
│ Best for: RTS, turn-based │
└─────────────────────────────────────────────────────────────┘
客户端预测与协调
PREDICTION FLOW:
┌─────────────────────────────────────────────────────────────┐
│ Frame N: │
│ 1. Capture input │
│ 2. Predict result locally (immediate response) │
│ 3. Store input + predicted state │
│ 4. Send input to server │
│ │
│ Frame N+RTT: │
│ 5. Receive server state for Frame N │
│ 6. Compare with stored prediction │
│ 7. If mismatch: RECONCILE │
│ a. Snap to server state │
│ b. Re-apply all inputs since Frame N │
│ c. Smooth correction to avoid visual pop │
└─────────────────────────────────────────────────────────────┘
// ✅ Production-Ready: Prediction Buffer
public class PredictionBuffer
{
private const int BUFFER_SIZE = 128;
private readonly PredictionEntry[] _buffer = new PredictionEntry[BUFFER_SIZE];
public void Store(uint tick, InputPayload input, PlayerState predictedState)
{
int index = (int)(tick % BUFFER_SIZE);
_buffer[index] = new PredictionEntry
{
Tick = tick,
Input = input,
PredictedState = predictedState
};
}
public void Reconcile(uint serverTick, PlayerState serverState)
{
int index = (int)(serverTick % BUFFER_SIZE);
var entry = _buffer[index];
if (entry.Tick != serverTick) return; // Stale data
float error = Vector3.Distance(entry.PredictedState.Position, serverState.Position);
if (error < 0.01f) return; // Within tolerance
// Misprediction detected - reconcile
PlayerState currentState = serverState;
// Re-simulate all inputs since server tick
for (uint t = serverTick + 1; t <= CurrentTick; t++)
{
int idx = (int)(t % BUFFER_SIZE);
if (_buffer[idx].Tick == t)
{
currentState = SimulateInput(currentState, _buffer[idx].Input);
}
}
// Apply corrected state (with smoothing)
ApplyCorrectedState(currentState);
}
}
插值
INTERPOLATION BUFFER:
┌─────────────────────────────────────────────────────────────┐
│ RECEIVE: [State T-100ms] [State T-50ms] [State T-now] │
│ │
│ RENDER: Display interpolated position between T-100ms │
│ and T-50ms based on current render time │
│ │
│ WHY: Always have two states to interpolate between │
│ (render behind real-time by buffer amount) │
│ │
│ BUFFER SIZE: │
│ • Too small: Choppy when packets delayed │
│ • Too large: Everything feels delayed │
│ • Typical: 100-200ms for other players │
└─────────────────────────────────────────────────────────────┘
回滚网络代码
ROLLBACK FLOW (Fighting Games):
┌─────────────────────────────────────────────────────────────┐
│ 1. Execute frame with predicted opponent input │
│ 2. Store complete game state snapshot │
│ 3. When actual input arrives: │
│ a. If matches prediction: continue │
│ b. If mismatch: │
│ - Load snapshot from that frame │
│ - Re-simulate all frames with correct input │
│ - "Rollback" visual to corrected state │
│ 4. Hide rollbacks with animation tricks │
└─────────────────────────────────────────────────────────────┘
ROLLBACK LIMITS:
• Max rollback: 7-8 frames (~116ms at 60fps)
• Beyond: Game stutters or desyncs
• Input delay trade-off: 0-3 frames pre-delay
Lockstep 同步
LOCKSTEP (RTS Games):
┌─────────────────────────────────────────────────────────────┐
│ Frame 0: All clients send inputs │
│ Wait for all inputs │
│ Execute deterministically │
│ │
│ Frame 1: Repeat │
│ │
│ REQUIREMENTS: │
│ • Deterministic simulation (fixed-point math) │
│ • Synchronized RNG seeds │
│ • Identical execution order │
│ │
│ PROS: Minimal bandwidth (only inputs) │
│ CONS: Latency = slowest player, input delay │
└─────────────────────────────────────────────────────────────┘
🔧 故障排除
┌─────────────────────────────────────────────────────────────┐
│ PROBLEM: Visible rubber-banding │
├─────────────────────────────────────────────────────────────┤
│ SOLUTIONS: │
│ → Increase interpolation buffer │
│ → Smooth reconciliation (lerp, not snap) │
│ → Add visual damping │
│ → Check for consistent tick rate │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ PROBLEM: Clients desyncing │
├─────────────────────────────────────────────────────────────┤
│ SOLUTIONS: │
│ → Use fixed-point math │
│ → Sync random number seeds │
│ → Periodic full-state resync │
│ → Add state hash verification │
│ → Check floating-point determinism │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ PROBLEM: Too many rollbacks (fighting games) │
├─────────────────────────────────────────────────────────────┤
│ SOLUTIONS: │
│ → Add input delay frames (1-3) │
│ → Better input prediction │
│ → Limit max rollback frames │
│ → Disconnect players with bad connections │
└─────────────────────────────────────────────────────────────┘
技术选择
| 游戏类型 | 主要技术 | 次要技术 | 延迟预算 |
|---|---|---|---|
| FPS | 预测 | 插值 | 50-100ms |
| 格斗游戏 | 回滚 | 输入延迟 | 50-80ms |
| RTS | Lockstep | - | 200-500ms |
| MMO | 插值 | 预测 | 100-200ms |
| 赛车 | 预测 | 外推 | 50-100ms |
使用此技能:构建多人游戏系统、优化网络代码或修复不同步问题时。
评论
支持 Markdown