extends SceneTree const MainScene: PackedScene = preload("res://main/main.tscn") const PlayerScene: PackedScene = preload("res://player/player.tscn") const TEST_PORT: int = 18171 func _initialize() -> void: call_deferred("_run") func _run() -> void: await _validate_latency_smoothing() var arguments: PackedStringArray = OS.get_cmdline_user_args() if arguments.has("unit"): print("Movement latency smoothing validation: PASS") quit() return if arguments.has("host"): await _run_host() return if arguments.has("client"): await _run_client() return push_error("Movement multiplayer validation needs host or client mode.") quit(1) func _validate_latency_smoothing() -> void: var avatar := PlayerScene.instantiate() as Player root.add_child(avatar) await process_frame avatar.set_process(false) avatar.set_physics_process(false) _validate_compact_input_encoding() _validate_compact_snapshot_encoding() _validate_compact_animation_encoding() _validate_animation_action_ordering(avatar) _validate_transit_estimation() _validate_remote_snapshot_smoothing(avatar) _validate_remote_locomotion_playback_recovery(avatar) _validate_reliable_jump_intent(avatar) _validate_airborne_sitting(avatar) _validate_stale_input_expiry(avatar) avatar.queue_free() await process_frame func _validate_compact_input_encoding() -> void: var input: Dictionary = _movement_input( 12, true, true, true, &"strike", 4, true, ) var encoded: Array = NetworkSession._encode_movement_input(input) assert(encoded.size() == NetworkSession.MOVEMENT_INPUT_FIELD_COUNT) assert(var_to_bytes(encoded).size() < var_to_bytes(input).size()) assert(NetworkSession._decode_movement_input(encoded) == input) assert(NetworkSession._decode_movement_input(encoded.slice(0, 3)).is_empty()) var unknown_flags: Array = encoded.duplicate() unknown_flags[3] = 1 << 12 assert(NetworkSession._decode_movement_input(unknown_flags).is_empty()) func _validate_compact_snapshot_encoding() -> void: var moving_snapshot: Dictionary = _network_snapshot( Vector3.ZERO, Vector3(4.5, 0.0, 0.0), 1, ) assert(NetworkSession.MOVEMENT_SNAPSHOT_BATCH_SIZE == 8) assert(NetworkSession.OWNER_SNAPSHOT_DIVISOR == 3) var encoded_snapshots: Array = [] for _peer: int in NetworkSession.MOVEMENT_SNAPSHOT_BATCH_SIZE: encoded_snapshots.append( NetworkSession._encode_movement_snapshot(moving_snapshot) ) assert(var_to_bytes(encoded_snapshots).size() < 1200) var expected_snapshot: Dictionary = moving_snapshot.duplicate(true) expected_snapshot.erase("animation_state") assert( NetworkSession._decode_movement_snapshot(encoded_snapshots[0]) == expected_snapshot ) assert( NetworkSession._decode_movement_snapshot( encoded_snapshots[0].slice(0, 4) ).is_empty() ) var invalid_snapshot_flags: Array = encoded_snapshots[0].duplicate() invalid_snapshot_flags[5] = 1 << 12 assert( NetworkSession._decode_movement_snapshot( invalid_snapshot_flags ).is_empty() ) func _validate_compact_animation_encoding() -> void: var paused_state := NetworkPlayerAnimationProtocol.make_state( NetworkPlayerAnimationProtocol.LOCOMOTION_IDLE, true, &"strike", 7, 0.5, true, ) var encoded: Array = NetworkSession._encode_movement_animation( 2, paused_state, ) assert(encoded.size() == NetworkSession.MOVEMENT_ANIMATION_FIELD_COUNT) var decoded: Dictionary = NetworkSession._decode_movement_animation(encoded) assert(int(decoded.get("peer_id", 0)) == 2) assert(Dictionary(decoded.get("state", {})) == paused_state) var advanced_state: Dictionary = paused_state.duplicate(true) advanced_state["action"]["elapsed"] = 0.75 assert( NetworkSession._movement_animation_signature(advanced_state) == NetworkSession._movement_animation_signature(paused_state) ) var action: Dictionary = paused_state["action"] var encoded_action: Array = ( NetworkSession._encode_movement_animation_action(action, true) ) assert( encoded_action.size() == NetworkSession.MOVEMENT_ANIMATION_ACTION_FIELD_COUNT ) var decoded_action: Dictionary = ( NetworkSession._decode_movement_animation_action(encoded_action) ) assert(Dictionary(decoded_action.get("action", {})) == action) assert(bool(decoded_action.get("sitting", false))) assert( NetworkSession._decode_movement_animation_action( encoded_action.slice(0, 2) ).is_empty() ) func _validate_airborne_sitting(avatar: Player) -> void: avatar.reset_network_movement_state() avatar.set("_sit_after_landing", true) assert(avatar.get_network_sitting_intent()) assert(not avatar.get_network_sitting_state()) assert(not bool(avatar.make_network_snapshot(2)["sitting"])) assert(bool(avatar.capture_network_input(1)["sitting"])) # Even a malformed or stale reconciliation that marks an airborne avatar as # seated must not bypass gravity and freeze it in place. avatar.set("_sit_after_landing", false) avatar.call("_set_sitting", true) avatar.velocity = Vector3(0.0, 4.0, 0.0) avatar.call("_simulate_movement_physics", 0.1) assert(not avatar.is_sitting()) assert(bool(avatar.get("_sit_after_landing"))) assert(avatar.velocity.y < 4.0) avatar.reset_network_movement_state() func _validate_animation_action_ordering(avatar: Player) -> void: var draw := NetworkPlayerAnimationProtocol.make_action_state( &"draw", 5, 0.2 ) avatar.apply_authoritative_network_animation_action(draw) assert(StringName(str(avatar.get("_animation_action_id"))) == &"draw") var cleared := NetworkPlayerAnimationProtocol.make_action_state( &"", 6, 0.0 ) avatar.apply_authoritative_network_animation_action(cleared) assert(StringName(str(avatar.get("_animation_action_id"))).is_empty()) avatar.apply_authoritative_network_animation_action(draw) assert(StringName(str(avatar.get("_animation_action_id"))).is_empty()) avatar.apply_authoritative_network_animation_action( NetworkPlayerAnimationProtocol.make_action_state(&"strike", 6, 0.0) ) assert(StringName(str(avatar.get("_animation_action_id"))).is_empty()) var strike := NetworkPlayerAnimationProtocol.make_action_state( &"strike", 7, 0.2 ) avatar.apply_authoritative_network_animation_action(strike) avatar.apply_authoritative_network_animation_action( NetworkPlayerAnimationProtocol.make_action_state( &"strike", 8, 0.4, true ) ) avatar.apply_authoritative_network_animation_action(strike) assert(bool(avatar.get("_animation_action_paused"))) avatar.apply_authoritative_network_animation_action( NetworkPlayerAnimationProtocol.make_action_state(&"", 9, 0.0) ) avatar.configure_network_remote(false) avatar.apply_network_animation_state( NetworkPlayerAnimationProtocol.make_state( NetworkPlayerAnimationProtocol.LOCOMOTION_IDLE, true, &"strike", 9, 0.4, true, ) ) avatar.apply_network_animation_state( NetworkPlayerAnimationProtocol.make_state( NetworkPlayerAnimationProtocol.LOCOMOTION_WALKING, true, &"draw", 8, 0.1, ) ) assert( StringName(str(avatar.get("_network_target_animation_action_id"))) == &"strike" ) avatar.apply_network_animation_state( NetworkPlayerAnimationProtocol.make_state( NetworkPlayerAnimationProtocol.LOCOMOTION_IDLE, true, &"", 10, ) ) func _validate_transit_estimation() -> void: assert(is_equal_approx( Player.resolve_local_prediction_transit_seconds( 10, 16, 1.0 / 30.0, 0.075, ), 0.075, )) # Six outstanding 30 Hz inputs span about 200 ms round trip. The fallback # must use only the approximately 100 ms one-way half of that gap. assert(is_equal_approx( Player.resolve_local_prediction_transit_seconds( 10, 16, 1.0 / 30.0, -1.0, ), 0.1, )) assert(is_equal_approx( Player.resolve_local_prediction_transit_seconds( 1, 100, 1.0 / 30.0, -1.0, ), Player.LOCAL_PREDICTION_EXTRAPOLATION_LIMIT_SECONDS, )) func _validate_remote_snapshot_smoothing(avatar: Player) -> void: avatar.configure_network_remote(false) var moving_snapshot: Dictionary = _network_snapshot( Vector3.ZERO, Vector3(4.5, 0.0, 0.0), 1, ) avatar.push_network_snapshot(moving_snapshot, 0.1) assert(is_equal_approx(avatar.global_position.x, 0.45)) assert(is_equal_approx( float(avatar.get("_network_target_position").x), 0.45, )) avatar.call("_update_network_interpolation", 0.12) var before_delayed_snapshot: Vector3 = avatar.global_position var delayed_snapshot: Dictionary = _network_snapshot( Vector3(0.54, 0.0, 0.0), Vector3(4.5, 0.0, 0.0), 2, ) avatar.push_network_snapshot(delayed_snapshot, 0.1) # Snapshot receipt updates the target without teleporting a presented # remote avatar, even after a jittered packet interval. assert(avatar.global_position == before_delayed_snapshot) assert(float(avatar.get("_network_snapshot_jitter")) > 0.0) avatar.call("_update_network_interpolation", 1.0 / 60.0) assert( avatar.global_position.distance_to(before_delayed_snapshot) < 0.12 ) # Simulate a burst of dropped snapshots. Extrapolation must stop at the # tight limit instead of allowing the remote avatar to run indefinitely. for _step: int in 20: avatar.call("_update_network_interpolation", 1.0 / 30.0) assert(is_equal_approx( float(avatar.get("_network_snapshot_age")), Player.NETWORK_EXTRAPOLATION_LIMIT_SECONDS, )) var maximum_extrapolated_x: float = ( 0.54 + 4.5 * (0.1 + Player.NETWORK_EXTRAPOLATION_LIMIT_SECONDS) ) assert(avatar.global_position.x <= maximum_extrapolated_x + 0.1) avatar.set_local_control(true) avatar.global_position = Vector3(0.8, 0.0, 0.0) avatar.apply_local_prediction_correction( moving_snapshot, 2, 1.0 / 30.0, 0.1, 0.1, ) # Ordinary host/client disagreement is expected while a packet is in flight. # It must never tug the locally controlled body or camera around. assert(is_equal_approx(avatar.global_position.x, 0.8)) assert( int(avatar.get("_local_prediction_soft_corrections")) == 0 ) assert( int(avatar.get("_local_prediction_hard_corrections")) == 0 ) # A larger but still plausible mismatch must persist across multiple audits # before a small correction is allowed. Preserve both visible character and # camera positions while the collision body catches up. avatar.global_position = Vector3(3.0, 0.0, 0.0) var visual_position: Vector3 = avatar.get_node("Visuals").global_position var camera_position: Vector3 = avatar.get_node("CameraYaw").global_position var drift_snapshot: Dictionary = _network_snapshot( Vector3.ZERO, Vector3.ZERO, 20, ) for _audit: int in 3: avatar.apply_local_prediction_correction( drift_snapshot, 20, 1.0 / 30.0, 0.0, 0.1, ) assert(is_equal_approx(avatar.global_position.x, 3.0)) avatar.apply_local_prediction_correction( drift_snapshot, 20, 1.0 / 30.0, 0.0, 0.1, ) assert(avatar.global_position.x < 3.0) assert(avatar.global_position.x >= 2.85 - 0.001) assert(avatar.get_node("Visuals").global_position == visual_position) assert(avatar.get_node("CameraYaw").global_position == camera_position) assert( int(avatar.get("_local_prediction_soft_corrections")) == 1 ) # Genuine divergence still recovers immediately, as do the separate reliable # teleport and water-recovery paths used by gameplay transitions. avatar.global_position = Vector3(10.0, 0.0, 0.0) avatar.apply_local_prediction_correction( drift_snapshot, 20, 1.0 / 30.0, 0.0, 0.1, ) assert(avatar.global_position == Vector3.ZERO) assert( int(avatar.get("_local_prediction_hard_corrections")) == 1 ) func _validate_remote_locomotion_playback_recovery(avatar: Player) -> void: avatar.configure_network_remote(false) avatar.push_network_snapshot( _network_snapshot(Vector3.ZERO, Vector3(4.5, 0.0, 0.0), 21) ) avatar.call("_update_character_animation") var animation_player := avatar.get_node( "Visuals/CharacterRig/AnimationPlayer" ) as AnimationPlayer assert(animation_player.assigned_animation == &"running") assert(animation_player.is_playing()) # Reproduce issue #111: locomotion replication still says RUNNING (and the # dust therefore still emits), but the rig playback has fallen idle. The # local presentation pass must repair that state without another packet. animation_player.stop() assert(not animation_player.is_playing()) assert( int(avatar.get("_network_target_locomotion_state")) == Player.LocomotionState.RUNNING ) avatar.call("_update_character_animation") assert(animation_player.assigned_animation == &"running") assert(animation_player.is_playing()) func _validate_reliable_jump_intent(avatar: Player) -> void: avatar.set_local_control(true) avatar.call("_queue_local_network_jump_intent") var first: Dictionary = avatar.capture_network_input(20) var repeated: Dictionary = avatar.capture_network_input(21) assert(bool(first["jump"])) assert(bool(repeated["jump"])) assert(int(avatar.get("_local_network_jump_intent_sequence")) == 20) avatar.apply_local_prediction_correction( _network_snapshot(avatar.global_position, Vector3.ZERO, 20), 20, 1.0 / 30.0, 0.0, 0.1, ) assert(not bool(avatar.capture_network_input(22)["jump"])) avatar.configure_network_remote(true) var first_host_jump: Dictionary = _movement_input(30, false, false) first_host_jump["jump"] = true avatar.apply_authoritative_network_input(first_host_jump) assert(bool(avatar.get("_network_jump_pending"))) avatar.set("_network_jump_pending", false) var repeated_host_jump: Dictionary = _movement_input(31, false, false) repeated_host_jump["jump"] = true avatar.apply_authoritative_network_input(repeated_host_jump) assert(not bool(avatar.get("_network_jump_pending"))) avatar.apply_authoritative_network_input( _movement_input(32, false, false) ) var next_host_jump: Dictionary = _movement_input(33, false, false) next_host_jump["jump"] = true avatar.apply_authoritative_network_input(next_host_jump) assert(bool(avatar.get("_network_jump_pending"))) avatar.reset_network_movement_state() assert(not bool(avatar.get("_network_jump_pending"))) assert(not bool(avatar.get("_local_network_jump_intent_pending"))) assert(int(avatar.get("_last_network_input_sequence")) == 0) func _validate_stale_input_expiry(avatar: Player) -> void: avatar.configure_network_remote(true) var high_latency_timeout := ( Player.resolve_network_input_stale_timeout_seconds(400) ) assert(high_latency_timeout > Player.NETWORK_INPUT_STALE_TIMEOUT_SECONDS) avatar.apply_authoritative_network_input( _movement_input(40, true), high_latency_timeout, ) assert((avatar.get("_network_axis") as Vector2).length_squared() > 0.0) avatar.call( "_update_network_input_freshness", Player.NETWORK_INPUT_STALE_TIMEOUT_SECONDS + 0.01, ) assert(not bool(avatar.get("_network_input_stale"))) avatar.call( "_update_network_input_freshness", high_latency_timeout, ) assert((avatar.get("_network_axis") as Vector2) == Vector2.ZERO) assert(not bool(avatar.get("_network_sprint"))) assert(bool(avatar.get("_network_input_stale"))) avatar.apply_authoritative_network_input(_movement_input(41, false)) assert(not bool(avatar.get("_network_input_stale"))) assert((avatar.get("_network_axis") as Vector2).length_squared() > 0.0) func _network_snapshot( position: Vector3, velocity: Vector3, acknowledged_input: int, ) -> Dictionary: return { "peer_id": 2, "acknowledged_input": acknowledged_input, "position": [position.x, position.y, position.z], "velocity": [velocity.x, velocity.y, velocity.z], "visual_yaw": 0.0, "grounded": true, "animation_state": NetworkPlayerAnimationProtocol.make_state( NetworkPlayerAnimationProtocol.LOCOMOTION_RUNNING, true, ), "sitting": false, "casting": false, } func _run_host() -> void: var main: Node = await _create_initialized_main() var session := main.get_node("%NetworkSession") as NetworkSession var save_manager := main.get("_save_manager") as PlayerSaveManager assert(session.set_host_world( WorldLayout.STARTER_ISLAND, NetworkProtocol.DEFAULT_WORLD_SEED, )) assert(session.start_private_host(TEST_PORT)) assert(save_manager.initialize_new_game( NetworkProtocol.DEFAULT_WORLD_SEED, WorldLayout.STARTER_ISLAND, )) main.call("_enter_gameplay") for _frame: int in 4: await physics_frame assert(session.set_host_open(true)) var remote_peer_id: int = await _wait_for_remote_peer(session) assert(remote_peer_id > 1) # Authentication completes before the client has necessarily created and # begun observing the host avatar. Give its gameplay scene a bounded moment # to settle before emitting the one-shot locomotion/action sequence. await create_timer(3.0).timeout var player := main.get("_player") as Player player.configure_network_remote(true) player.apply_authoritative_network_input(_movement_input(1, true)) await create_timer(2.5).timeout player.apply_authoritative_network_input(_movement_input(2, false)) await create_timer(2.5).timeout player.apply_authoritative_network_input( _movement_input(3, false, true, true) ) await create_timer(2.0).timeout player.apply_authoritative_network_input( _movement_input(4, false, false, true) ) await create_timer(2.0).timeout player.apply_authoritative_network_input( _movement_input(5, false, false, true, &"draw", 1) ) await create_timer(1.0).timeout player.apply_authoritative_network_input( _movement_input(6, false, false, true, &"strike", 2, true) ) await create_timer(2.0).timeout player.apply_authoritative_network_input( _movement_input(7, false, false, false, &"", 3) ) var disconnect_deadline: int = Time.get_ticks_msec() + 8000 while ( Time.get_ticks_msec() < disconnect_deadline and session.is_authenticated_peer(remote_peer_id) ): await process_frame assert(not session.is_authenticated_peer(remote_peer_id)) print("Movement multiplayer host validation: PASS") session.disconnect_session("") main.queue_free() for _frame: int in 4: await process_frame await create_timer(0.1).timeout quit() func _run_client() -> void: var main: Node = await _create_initialized_main() main.call( "_on_title_join_game_requested", "127.0.0.1:%d" % TEST_PORT, ) var session := main.get_node("%NetworkSession") as NetworkSession var join_deadline: int = Time.get_ticks_msec() + 20000 while Time.get_ticks_msec() < join_deadline: await process_frame if session.state == NetworkSession.State.VERIFYING_SERVER_IDENTITY: main.call("_confirm_server_trust") if session.is_joined_client() and bool(main.get("_gameplay_started")): break assert(session.is_joined_client()) var spawn_service := main.get_node( "%PlayerSpawnService" ) as PlayerSpawnService var host_avatar: Player = await _wait_for_avatar(spawn_service, 1) assert(host_avatar != null) var animation_player := host_avatar.get_node( "Visuals/CharacterRig/AnimationPlayer" ) as AnimationPlayer var sprint_dust := host_avatar.get_node("%SprintDust") as SprintDustTrail var saw_running: bool = false var saw_walking: bool = false var saw_sneaking: bool = false var saw_idle_sneak: bool = false var saw_net_draw: bool = false var saw_net_strike: bool = false var saw_net_strike_paused: bool = false var saw_animation_advance: bool = false var saw_dust: bool = false var previous_animation: StringName = &"" var previous_animation_position: float = -1.0 var observation_deadline: int = Time.get_ticks_msec() + 18000 while Time.get_ticks_msec() < observation_deadline: await process_frame var current_animation: StringName = animation_player.current_animation saw_running = saw_running or current_animation.begins_with("running") saw_walking = saw_walking or current_animation.begins_with("walking") saw_sneaking = saw_sneaking or current_animation == &"sneaking" saw_idle_sneak = saw_idle_sneak or current_animation == &"idle_sneak" saw_net_draw = saw_net_draw or current_animation == &"draw" saw_net_strike = ( saw_net_strike or current_animation == &"strike" or animation_player.assigned_animation == &"strike" ) saw_net_strike_paused = ( saw_net_strike_paused or bool(host_avatar.get( "_network_target_animation_action_paused" )) ) var current_position: float = ( animation_player.current_animation_position ) if ( current_animation == previous_animation and previous_animation_position >= 0.0 and absf(current_position - previous_animation_position) > 0.001 ): saw_animation_advance = true previous_animation = current_animation previous_animation_position = current_position saw_dust = saw_dust or sprint_dust.get_active_puff_count() > 0 if ( saw_running and saw_walking and saw_sneaking and saw_idle_sneak and saw_net_draw and saw_net_strike and saw_net_strike_paused and saw_animation_advance and saw_dust ): break assert(saw_running) assert(saw_walking) assert(saw_sneaking) assert(saw_idle_sneak) assert(saw_net_draw) assert(saw_net_strike) assert(saw_net_strike_paused) assert(saw_animation_advance) assert(saw_dust) print("Movement multiplayer client validation: PASS") session.disconnect_session("") main.queue_free() for _frame: int in 4: await process_frame await create_timer(0.1).timeout quit() func _movement_input( sequence: int, sprinting: bool, moving: bool = true, sneaking: bool = false, action_id: StringName = &"", action_sequence: int = 0, action_paused: bool = false, ) -> Dictionary: return { "sequence": sequence, "axis": [0.0, -1.0] if moving else [0.0, 0.0], "camera_yaw": 0.0, "jump": false, "sprint": sprinting, "sneak": sneaking, "slow_walk": false, "sitting": false, "casting": false, "animation_action": NetworkPlayerAnimationProtocol.make_action_state( action_id, action_sequence, 0.0, action_paused, ), } func _create_initialized_main() -> Node: root.size = Vector2i(1280, 720) var main: Node = MainScene.instantiate() root.add_child(main) for _frame: int in 4: await process_frame if not bool(main.get("_application_initialized")): main.call("_activate_selected_data_path", "", true) for _frame: int in 8: await process_frame assert(bool(main.get("_application_initialized"))) return main func _wait_for_remote_peer(session: NetworkSession) -> int: var deadline: int = Time.get_ticks_msec() + 20000 while Time.get_ticks_msec() < deadline: await process_frame for peer_id: int in session.get_authenticated_peer_ids(): if peer_id != session.get_local_peer_id(): return peer_id return 0 func _wait_for_avatar( spawn_service: PlayerSpawnService, peer_id: int, ) -> Player: var deadline: int = Time.get_ticks_msec() + 8000 while Time.get_ticks_msec() < deadline: await process_frame var avatar := spawn_service.get_avatar(peer_id) as Player if avatar != null: return avatar return null