feat: implement client-side prediction and reconciliation system

Add client-side prediction (t_p1_pred) with:

- scripts/network/snapshot.gd — lightweight snapshot resource with
  to_dict/from_dict serialization for RPC. Stores position (Vector3),
  rotation (Quaternion), velocity (Vector3), grounded (bool).

- scripts/network/client_prediction.gd — prediction/reconciliation
  controller. 64-entry ring buffer of local snapshots, sends inputs to
  server each physics tick (128Hz, ENet channel 0), detects misprediction
  on server state arrival, rewinds and re-applies unconfirmed inputs.
  Also supports remote player interpolation.

- scripts/network/network_manager.gd — new RPC endpoints for client
  prediction: send_client_input (client->server, ch 0) and
  send_server_state (server->client, ch 1). New signals for routing.

- client/characters/character/fps_character_controller.gd — prediction
  hooks in _physics_process: on_before_tick() captures pre-input
  snapshot, on_after_tick() sends input to server. Client prediction
  path uses local movement (instant feedback) instead of reading from
  SimulationServer entity.

Architecture:
  Each tick: client applies input → predicts new state locally
  → sends input to server → server returns authoritative state
  → client compares and reconciles if mismatch.
This commit is contained in:
2026-07-01 20:21:16 -04:00
parent 2452aba0d7
commit f6d69545c9
4 changed files with 461 additions and 21 deletions
@@ -88,6 +88,12 @@ var _input_sequence: int = 0
## Cached input dictionary (avoids alloc per frame).
var _input_dict: Dictionary = {}
## Prediction controller reference (set by ClientPrediction system).
## When non-null and prediction_enabled, the controller uses client-side
## prediction with local movement for instant feedback instead of
## reading position from the SimulationServer entity directly.
var _prediction: Node = null
## Mouse capture state.
var _mouse_captured: bool = false
var _mouse_clicked_this_frame: bool = false
@@ -193,6 +199,12 @@ func _unhandled_input(event: InputEvent) -> void:
# ---------------------------------------------------------------------------
func _physics_process(delta: float) -> void:
# PREDICTION HOOK: capture pre-input state snapshot.
# Must be called BEFORE any input processing or movement so the
# snapshot represents the state at the start of this tick.
if _prediction and _prediction.prediction_enabled:
_prediction.on_before_tick()
# 1. Capture input state
var input_dir := _get_move_direction()
var jump_pressed := Input.is_action_just_pressed(&"jump")
@@ -249,19 +261,24 @@ func _physics_process(delta: float) -> void:
if should_fire:
_server.fire_weapon(entity_id)
# 6. Send input to simulation server
if _server != null and entity_id >= 0:
# Build input for SimulationServer (mutate cached dict to avoid alloc)
_input_dict["move_direction"] = input_dir
_input_dict["look_yaw"] = rad_to_deg(_yaw)
_input_dict["look_pitch"] = rad_to_deg(_pitch)
_input_dict["jump"] = jump_pressed
_input_dict["crouch"] = _crouch_active
_input_dict["sprint"] = _sprint_active
_input_dict["shoot"] = should_fire
_input_dict["aim"] = aim_pressed
_input_dict["input_sequence"] = _input_sequence
# 6. Build input dictionary (all modes — mutate cached dict to avoid alloc)
_input_dict["move_direction"] = input_dir
_input_dict["look_yaw"] = rad_to_deg(_yaw)
_input_dict["look_pitch"] = rad_to_deg(_pitch)
_input_dict["jump"] = jump_pressed
_input_dict["crouch"] = _crouch_active
_input_dict["sprint"] = _sprint_active
_input_dict["shoot"] = should_fire
_input_dict["aim"] = aim_pressed
_input_dict["input_sequence"] = _input_sequence
# 6b. Route input to appropriate handler.
if _prediction and _prediction.prediction_enabled:
# Client prediction: input sending is handled by the prediction
# controller's on_after_tick() call at the end of this function.
pass
elif _server != null and entity_id >= 0:
# Listen server: send to local SimulationServer.
_server.apply_input(entity_id, _input_dict)
# Check for hit feedback from last tick
@@ -271,16 +288,20 @@ func _physics_process(delta: float) -> void:
var hit_pos := Vector3.ZERO # approximate — we don't have exact world hit position from server yet
_weapon.on_hit_confirmed(hit_pos, hit_result.get("damage", 0.0), hit_result.get("killed", false))
_input_sequence += 1
_input_sequence += 1
# 7. Read server state back (for local simulation / listen server)
# The SimulationServer.tick() call happens in the game manager / network layer.
# Here we optionally read the entity's canonical position from the server.
# NOTE: On a dedicated client, position comes from the network replication
# layer (server snapshot), not from direct server entity access.
# On a listen server (host+client same process) or in standalone mode,
# reading position directly from the server entity is correct.
if _server != null and entity_id >= 0:
# 7. Update position.
# Architecture:
# - Client prediction: do local CharacterBody3D movement (instant feedback),
# the prediction controller handles reconciliation when authoritative
# server state arrives.
# - Listen server: read authoritative position from SimulationServer entity.
# - Standalone (no server): fallback CharacterBody3D movement.
if _prediction and _prediction.prediction_enabled:
# Client prediction path — local movement for instant feedback.
_move_local(input_dir, delta, jump_pressed)
elif _server != null and entity_id >= 0:
# Listen server: read from SimulationServer entity.
var entity = _server.get_entity(entity_id)
if entity != null and entity.is_alive():
# Server position is ground truth — apply it to the visual body
@@ -290,6 +311,12 @@ func _physics_process(delta: float) -> void:
# Standalone mode: do local CharacterBody3D physics
_move_local(input_dir, delta, jump_pressed)
# PREDICTION HOOK: post-tick — send input to server via RPC.
# Must be called AFTER local movement so the prediction controller
# can send the input that produced this tick's movement.
if _prediction and _prediction.prediction_enabled:
_prediction.on_after_tick(_input_dict)
# ---------------------------------------------------------------------------
# Local movement (standalone / no-server fallback)
@@ -395,6 +422,13 @@ func set_entity_id(id: int) -> void:
entity_id = id
## Set the prediction controller for client-side prediction/reconciliation.
## The prediction node must have on_before_tick() / on_after_tick() methods.
## Typically a ClientPrediction instance added as a child or sibling.
func set_prediction(prediction_node: Node) -> void:
_prediction = prediction_node
## Get current crouch amount (0.0 = standing, 1.0 = fully crouched).
func get_crouch_amount() -> float:
return _crouch_current