code wiki / (root) / nx_netpredict.nx

nx_netpredict.nx source

↩ module page · 28 lines · 1858 B

1// nx_netpredict.nx -- CLIENT-SIDE PREDICTION + SERVER RECONCILIATION, the warzone-netcode core that hides RTT. The 2// client applies local input IMMEDIATELY (predicts) so its own creature responds in <100ms regardless of the 150ms 3// ocean RTT; it tags each input with a sequence number and sends them to the authoritative server. The server returns 4// its authoritative state stamped with the last input it processed; the client snaps to that and REPLAYS the still- 5// pending (un-acked) inputs on top (reconciliation). If the prediction was right, reconciled == predicted -> no visible 6// correction. If the server diverged (an event the client couldn't predict), reconciliation converges the client to the 7// authoritative truth. Client predict, client replay, and the server all run the SAME deterministic step. Cited model: 8// Valve/Gambetta/Overwatch. Pure integer. license_tier: ORIGINAL 9 10// the one deterministic sim step, run identically on client-predict, client-replay, and server 11func np_step(pos: i64, input: i64) -> i64 { return pos + input } 12 13// apply inputs[from..to) to pos 14func np_apply(pos: i64, inputs: *i64, from: i64, to: i64) -> i64 { 15 var p: i64 = pos 16 var i: i64 = from 17 while i < to { p = np_step(p, inputs[i]); i = i + 1 } 18 return p 19} 20// PREDICT: from the last confirmed pos, apply ALL issued inputs -- local-only, no network wait => zero input-to-feedback 21// latency (the RTT is hidden). total = number of inputs the client has issued. 22func np_predict(confirmed_pos: i64, inputs: *i64, total: i64) -> i64 { 23 return np_apply(confirmed_pos, inputs, 0, total) 24} 25// RECONCILE: the server sent auth_pos having processed `acked` inputs; replay the pending inputs [acked..total) on top. 26func np_reconcile(auth_pos: i64, acked: i64, inputs: *i64, total: i64) -> i64 { 27 return np_apply(auth_pos, inputs, acked, total) 28}