class_name AdaptiveInputDepthController extends RefCounted # Client-only policy for choosing whether the server's input jitter buffer may # run at depth zero. It deliberately does not change server buffering, action # encoding, or bot behavior; NetworkedMatch pins --test-bot clients at depth 1. const TARGET_DEPTH_SAFE := 1 const TARGET_DEPTH_LOW_LATENCY := 0 const CLEAN_JITTER_MS := 3.0 const EXIT_JITTER_MS := 5.0 const REQUIRED_STABLE_TICKS := 240 const REENTRY_COOLDOWN_TICKS := 120 var target_depth := TARGET_DEPTH_SAFE var stable_low_jitter_ticks := 0 var cooldown_ticks := 0 func update(rtt_ms: float, jitter_ms: float, advertised_depth: int) -> int: if cooldown_ticks > 0: cooldown_ticks -= 1 # -2 is a genuine server starvation sentinel. -1 means no header yet and # must not be mistaken for starvation. if advertised_depth < -1 or jitter_ms > EXIT_JITTER_MS: target_depth = TARGET_DEPTH_SAFE stable_low_jitter_ticks = 0 cooldown_ticks = REENTRY_COOLDOWN_TICKS return target_depth if rtt_ms >= 0.0 and jitter_ms < CLEAN_JITTER_MS: stable_low_jitter_ticks += 1 if stable_low_jitter_ticks >= REQUIRED_STABLE_TICKS and cooldown_ticks == 0: target_depth = TARGET_DEPTH_LOW_LATENCY else: stable_low_jitter_ticks = 0 target_depth = TARGET_DEPTH_SAFE return target_depth