Mass Battle Frameworkv1.20.0

确定性锁步:只传走法,不传棋盘 Deterministic Lockstep: Send the Moves, Not the Board

NETWORK & REPLICATION

锁步的原理、K 缓冲与失步恢复 · 2026-12-01The idea, the K-buffer and desync recovery · 2026-12-01

系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →

先看这盘棋 The Game First

两个人在不同城市下棋,从不互发棋盘照片,只报走法:"马走 D5""象吃 C4"。同样的开局、同样的规则、同样的走法顺序,两边的棋盘在任何一步都必然一样——根本不用检查对方的棋盘。确定性锁步就是这么工作的:网络上只跑"走法"(玩家输入命令)和一份"棋谱指纹"(每帧的状态哈希),几万个单位的位置、血量、行为一个都不单独同步。

Two people play chess from different cities and never send each other photos of the board — only moves: "knight to D5", "bishop takes C4". Same opening, same rules, same order of moves, and the boards are identical at every step, with nobody inspecting the other's board. Deterministic lockstep works the same way: only the moves (player input commands) and a fingerprint of the position (a per-tick state hash) travel the wire; the position, health and behaviour of tens of thousands of units are never replicated one by one.

这一下带宽就跟"有几个玩家"成正比,跟"场上多少单位"没关系。五万个单位,网络负担和五百个一样。

That makes bandwidth proportional to the number of players and unrelated to the number of units — fifty thousand units cost the network the same as five hundred.

三根支柱 Three Pillars

支柱一,模拟本身逐位可复现。固定步长的 tick,随机数全部由实体自己的 UID 播种,不读渲染帧、不读墙钟、不依赖容器的遍历顺序。同样的输入,两边算出来的每一位都一样——"差不多就行"在这儿行不通,每一个 float 都得对上。

Pillar one: the simulation is bit-reproducible. Fixed-step ticks, every random stream seeded from the entity's own UID, no reads of render frames or wall-clock time, no dependence on container iteration order. Same input, and every bit of both results agrees — not "close enough", every float has to match.

支柱二,所有端按同一个顺序吃命令。服务器给每条命令排一个执行 tick 和一个同 tick 内的序号,全部端按(执行 tick, 序号)的唯一全序执行——命令什么时候到达、网络怎么抖,都改不了这个顺序。

Pillar two: every peer consumes commands in the same order. The server stamps each command with an execution tick and an order number within that tick, and all peers execute by the single total order (execution tick, order number) — arrival time and network jitter cannot change it.

支柱三,每帧对一次哈希。把全网单位的关键字段折成一个数,服务器广播、各端比对。锁步不保证两边一致,它负责发现不一致;发现不了才是灾难,发现了就有一条恢复路径。

Pillar three: compare a hash every frame. The networked units' key fields fold into one number; the server broadcasts it and every peer compares. Lockstep does not guarantee agreement — it detects disagreement. Silent divergence would be the disaster; a detected one has a recovery path.

动手玩一下 Play With It

下面的时间线把这套节奏整个摆了出来:客户端 0(上)发输入,服务器(中)排进 K 缓冲,再广播给两个客户端(下)。把延迟调大或者把客户端推到滞后,就能看到命令迟到(红色 LATE)之后的一整条恢复链:暂停 → 快照 → 追帧。这是 Mass Battle 失步恢复的真实行为模型,不是示意图。

The timeline below lays the whole rhythm out: client 0 (top) sends input, the server (middle) schedules it through the K-buffer, then broadcasts to both clients (bottom). Raise the delay or push a client out of step and you will see what follows a late command (red LATE): pause → snapshot → fast-forward. This is the actual behaviour model of Mass Battle's desync recovery, not an illustration.

客户端 0Client 0
服务器Server
客户端 1Client 1
输入Input
广播Broadcast
准时 / 快照On-time / Snapshot
迟到LATE
🔄 重同步Resync
| |

怎么玩:① 点「发送输入」,看黄色输入包上行、服务器排期、紫色广播下行、两端在自己的执行 tick 上点亮标记;② 把延迟调到 300ms 以上再发——广播到达时该 tick 已经过去,标记变红,落后的那头随即暂停、拿快照、6 倍速追帧;③ 用「推到滞后 / 推到超前」直接制造偏差,看调速带(faster / slower / PAUSED / FAST-FWD)——这就是客户端四档调速的直观版;④ 把 K 调小、延迟加大,迟到更容易复现。

How to play: ① click "Send Input" — watch the yellow packet go up, the server schedule it, purple broadcasts come down, and both peers light the marker on their execution tick; ② set the delay past 300ms and send again — the broadcast lands after that tick has passed, the marker turns red, and the lagging peer pauses, takes a snapshot and fast-forwards at 6×; ③ use "Push Behind / Push Ahead" to create the offset directly and watch the pacing bands (faster / slower / PAUSED / FAST-FWD) — a hands-on view of the client's pacing tiers; ④ lower K and raise the delay to reproduce lateness more easily.

迟到了怎么办 When a Command Is Late

命令不能在"还没到"的时候执行,也不能丢。服务器的做法是先算好这条命令该在哪一 tick 执行,然后等——等所有端都确认收到,再到点执行。这段等待就是 K:K 越大,能容忍的网络抖动越大,操作到生效的延迟也越长;K 越小越跟手,但更容易迟到。迟到一旦发生,落后的端不会硬算,它暂停,拿一份经过哈希验证的快照回到已知正确的状态,再以几倍速把中间缺的 tick 补回来。玩家看到的是画面卡一下然后恢复正常,而不是两边越走越不一样。

A command cannot execute before it has arrived, and it must not be dropped. The server decides which tick each command executes on, then waits until every peer has confirmed receipt before that tick runs. That waiting window is K: bigger K tolerates more network jitter and adds more input-to-effect latency; smaller K feels snappier and goes late more easily. When lateness happens, the lagging peer does not guess — it pauses, takes a hash-validated snapshot back to a known-good state, and replays the missing ticks at several times speed. What the player sees is a short hitch, not two worlds drifting apart.

恢复分两级:先试本地快照——客户端自己也一直在存,回滚时完全不打扰服务器;本地那份不可用或者追不回来,才向服务器要一份完整的,全量传过来、重建、再追。追到一半发现落后太多(几百 tick),就放弃硬追,直接要一份新快照跳到前排——倍速硬追几百 tick 要好几分钟,不值得。

Recovery has two tiers. First the local snapshot — clients keep their own, and rolling back to it never disturbs the server. Only if the local copy is unusable or the replay does not converge does the peer ask the server for a full snapshot: transferred, rebuilt, then fast-forwarded. If the replay falls a few hundred ticks behind, it gives up chasing and takes a fresh snapshot near the front — hard-chasing hundreds of ticks at 6× takes minutes and is not worth it.

失步了也不慌 A Desync Is Not the End

万一没迟到也出了分歧——某些代码悄悄读了不该读的东西,两边算出不同的结果——哈希会在几 tick 内对不上。检测是双向的:服务器广播它的哈希,客户端也把哈希带在心跳里回报。短促的毛刺会被防抖窗口压掉,真分歧则确认失步:服务器先隔离这个客户端(它的输入不再算数),客户端按上面的路径恢复,恢复完再放回来。哈希失配从不踢人;唯一会被踢出去的情况是心跳停滞太久,那说明这台机器已经不算在玩了。

If divergence happens anyway — some code quietly read something it should not, and the two sides compute different results — the hashes stop matching within a few ticks. Detection runs both ways: the server broadcasts its hash, and clients report theirs in their heartbeats. Brief blips are absorbed by a debounce window; a real divergence is confirmed as a desync: the server quarantines that client (its inputs stop counting), the client recovers by the path above, and only then rejoins. A hash mismatch never kicks anyone; the only kick is a heartbeat that stalls for too long, which means that machine is no longer playing.

代价 What It Costs

这套东西不便宜。整个模拟代码都得守确定性纪律:不能用没播种的随机、不能读只在本端存在的值、不能依赖容器的遍历顺序——每一条都属于"在你这台机器上是好的"那种错误,只有两边的哈希对不上时才现形。快照系统也要专门建:世界要能被装进一个载荷,池里休眠的对象也得算数。换来的是开头那个数:带宽只跟玩家数走,五万单位零额外流量。

None of this comes cheap. The whole simulation has to keep the determinism discipline: no unseeded randomness, no reads of values that exist on one peer only, no dependence on container order — each of those is the "works on my machine" kind of bug that only surfaces when the two hashes disagree. The snapshot system has to be built too: the world must fit into one payload, and dormant objects in pools count. What you get is the number from the opening: bandwidth follows player count, and fifty thousand units add nothing.

一句话 In One Line

确定性锁步把"同步世界"换成"同步输入":同样的规则、同样的顺序、同样的输入,两边各自算出同一个世界,再用哈希互相盯着。它省下的是整场战争的带宽,付出的是全部模拟代码的确定性纪律,和一套永远准备好接住失步的恢复机器。

Deterministic lockstep trades "synchronise the world" for "synchronise the inputs": same rules, same order, same inputs, each peer derives the same world on its own, and hashes keep watch on each other. What it saves is the bandwidth of the whole war; what it costs is determinism discipline across every line of simulation code, plus a recovery machine always ready to catch a desync.

Leroy Works · Mass Battle Framework 作者
联系:Discord · QQ 群 916358710 · Fab · 全部联系方式

Leroy Works — author of Mass Battle Framework
Contact: Discord · QQ group 916358710 · Fab · all channels