Files
CosmicClash/Game/scripts/ball.gd
T
Josh Creek 3d6906b981 feat(multiplayer): Phase 5 tasks 5.2-5.5 - clock, kickoff, goals, full time
Implements the rest of the §6.2 lifecycle on top of 5.1's state machine.

5.3 kickoff: the server resets every body and broadcasts the RESULTING
transforms, never a seed - §1's locked decision, because shared-seed
determinism needs both sides to consume the RNG stream in identical
order forever and the first randf() added to the reset path desyncs
silently. Countdown is derived from server_tick on both peers, and a
kickoff that lands after its own resume tick applies immediately and
skips the countdown rather than scheduling into the past.

5.4 goals: goal_scored(scoring_team, score, goal_tick, resume_tick).
Score is authoritative at sensor time, before any presentation. The
reset moved OUT of the sensor path and into the kickoff at resume_tick,
which is what stops the server resetting while clients are still
mid-celebration. Engine.time_scale is never touched.

5.2 clock: tick-derived, no Timer and no _process polling. The goal
pause shifts the absolute end_tick by (resume_tick - goal_tick) rather
than pausing anything, so no float drift accumulates across goals.

5.5 full time: clock expiry -> FULL_TIME -> sudden death on a draw or
RESULTS, golden goal in overtime, then LOBBY on both peers - clients
return to the lobby, not the main menu. get_tree().paused is never used.

Four bugs found and fixed while building this, each by a failing run
rather than by inspection:

- Tick order was load-bearing: _update_kickoff_countdown() clears the
  same _kickoff_resume_tick that _update_match_state() reads to leave
  WARMUP, so running the countdown first wiped the transition condition
  and the match sat frozen in WARMUP forever.
- _apply_match_state resets _state_deadline_tick on every transition, so
  a GOAL_PAUSE deadline assigned before _set_match_state was wiped and
  the match never resumed. Deadlines are now owned by _apply_match_state.
- Freezing "all bodies" is wrong on a client. Remote ships and the ball
  are permanently FREEZE_MODE_KINEMATIC and transform-driven; freezing
  them all unfroze the remote ones on the way back out, so they fell
  under gravity while the interpolator fought them - 210 hard snaps and
  an infinite p99. A client now freezes only the one body it simulates.
- A frozen body never runs _integrate_forces, so the queued kickoff
  teleport was stranded by an immediate set_deferred("freeze", true).
  Freeze now happens on a strictly later tick, the same pattern Phase 2
  used for _pending_reset_gen_bump_tick.

Prediction and reconciliation are suspended while the match is not live:
during a countdown or goal pause the local ship is frozen on both peers,
and running delta transport over those frozen states produced a p95
position error of 2.4e10 m. Input keeps flowing so the server's jitter
buffer does not starve into `stalled`.

Also fixed: a kickoff can arrive before match_config, and body order is
slot order - applying it early placed the BALL at positions[0], on top
of the first ship, which the ball-cam reported as "target vector can't
be zero" 95 times. It is now held until the roster exists.

Test changes: the ball-contact scenario steered by a hand-tuned fixed
heading, which 5.3 broke because kickoff applies KICKOFF_YAW_JITTER - it
flew past the ball in 3/3 runs. It now closes the loop on the actual
bearing using real input actions. Assertions that read a frozen ship
(freeze, thrust) are gated on the match being live, and the hooks now
survive the scene teardown at RESULTS instead of hanging on freed
objects for the full timeout.

Regression: 81 unit tests; free-flight LAN p99 0.143m and 80±20ms, both
0 hard snaps; transition gate 0.00%; ball contact 3/3; two-bot CI.
2026-08-21 10:01:39 +01:00

152 lines
6.0 KiB
GDScript

class_name Ball
extends RigidBody3D
# Physics ball. Floor gravity is untouched (gravity_scale in ball.tscn); this
# only adds the wall/ceiling "surface pull" (see ArenaBoundary.get_surface_pull)
# so the ball can cling near a wall for dribbling or hang against the ceiling
# for ceiling shots, plus a safety speed clamp (the ball previously had none).
@export_group("Surface Pull")
@export var wall_pull_strength = 4.0 # Weaker than the ship's — assist, not adherence
@export var wall_pull_range = 2.0
@export var ceiling_pull_strength = 5.5 # Stays below the ball's effective gravity (0.8 * 9.8)
@export var ceiling_pull_range = 2.0
# Kept close to ship_observations.gd's BALL_SPEED_SCALE (30.0) so this feature
# doesn't push ball velocity further out of the range trained policies expect.
const MAX_SPEED := 32.0
var _boundary: ArenaBoundary
var _trail: GPUParticles3D
@onready var visual: Node3D = $Visual
var _pending_teleport: Transform3D
var _has_pending_teleport := false
var _pending_teleport_linear_velocity := Vector3.ZERO
var _pending_teleport_angular_velocity := Vector3.ZERO
var _pending_teleport_has_velocity := false
# Queues an authoritative teleport, applied at the top of the next
# _integrate_forces — the only Jolt-safe place to write state.transform
# directly (see GameMode._reset_body / task 0.15) — instead of racing the
# physics step via set_deferred("global_transform", ...).
func queue_teleport(to: Transform3D) -> void:
_pending_teleport = to
_has_pending_teleport = true
_pending_teleport_has_velocity = false
# Kept parallel to Ship's network correction hook. A locally predicted ball
# must resume from the authoritative velocity after a correction; gameplay
# resets still deliberately use queue_teleport() and zero both velocities.
# The queued-but-not-yet-applied teleport target, or null when none is
# pending. queue_teleport() defers the actual write to the next
# _integrate_forces (task 0.15), so global_transform still reads the OLD pose
# in between — anything that needs to broadcast where a body is ABOUT to be
# (networked_match.gd's kickoff) must read this instead, or it ships the
# pre-reset position and corrects it a tick later.
func get_pending_teleport():
return _pending_teleport if _has_pending_teleport else null
func queue_teleport_with_velocity(to: Transform3D, new_linear_velocity: Vector3, new_angular_velocity: Vector3) -> void:
_pending_teleport = to
_pending_teleport_linear_velocity = new_linear_velocity
_pending_teleport_angular_velocity = new_angular_velocity
_pending_teleport_has_velocity = true
_has_pending_teleport = true
# -1 = use the real linear_velocity (default; see _physics_process below).
# A frozen remote ball (Phase 4) holds zero velocity — Godot/Jolt zeroes and
# ignores velocity writes on frozen bodies — so the trail needs a
# presentation-only speed fed in from outside instead of reading physics
# state that will never reflect the ball's true remote motion.
var _visual_speed_override: float = -1.0
# Prediction correction hook: exactly like Ship's visual offset, but kept
# here so a locally predicted ball can move its collider to authority while
# the mesh catches up over a short presentation-only decay.
var net_visual_offset := Vector3.ZERO
const NET_VISUAL_OFFSET_DECAY := 0.88
const MAX_NET_VISUAL_OFFSET := 0.4
func set_visual_speed(speed: float) -> void:
_visual_speed_override = speed
func _ready() -> void:
_boundary = get_tree().get_first_node_in_group("arena_boundary")
if DisplayServer.get_name() == "headless":
# _integrate_forces remains active; only the render-side trail updater is
# disabled across the many parallel RL environments.
set_physics_process(false)
else:
_build_trail()
func _physics_process(_delta: float) -> void:
if net_visual_offset != Vector3.ZERO:
net_visual_offset = net_visual_offset.limit_length(MAX_NET_VISUAL_OFFSET)
net_visual_offset *= pow(NET_VISUAL_OFFSET_DECAY, _delta * 60.0)
if net_visual_offset.length_squared() < 0.0001:
net_visual_offset = Vector3.ZERO
visual.position = net_visual_offset
if _trail:
var speed := _visual_speed_override if _visual_speed_override >= 0.0 else linear_velocity.length()
var speed_ratio := clampf(speed / MAX_SPEED, 0.0, 1.0)
_trail.emitting = speed_ratio > 0.12
_trail.amount_ratio = smoothstep(0.12, 1.0, speed_ratio)
func _build_trail() -> void:
var mat := StandardMaterial3D.new()
mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
mat.billboard_mode = BaseMaterial3D.BILLBOARD_ENABLED
mat.albedo_color = Color(0.55, 0.85, 1.0, 0.42)
mat.emission_enabled = true
mat.emission = Color(0.35, 0.72, 1.0)
mat.emission_energy_multiplier = 1.8
var quad := QuadMesh.new()
quad.size = Vector2(0.22, 0.22)
quad.material = mat
var process := ParticleProcessMaterial.new()
process.emission_shape = ParticleProcessMaterial.EMISSION_SHAPE_SPHERE
process.emission_sphere_radius = 0.35
process.gravity = Vector3.ZERO
process.scale_min = 0.35
process.scale_max = 1.0
_trail = GPUParticles3D.new()
_trail.name = "BallTrail"
_trail.amount = 48
_trail.lifetime = 0.48
_trail.local_coords = false
_trail.process_material = process
_trail.draw_pass_1 = quad
_trail.visibility_aabb = AABB(Vector3(-18, -18, -18), Vector3(36, 36, 36))
_trail.emitting = false
add_child(_trail)
func _integrate_forces(state: PhysicsDirectBodyState3D) -> void:
if _has_pending_teleport:
_has_pending_teleport = false
state.transform = _pending_teleport
state.linear_velocity = _pending_teleport_linear_velocity if _pending_teleport_has_velocity else Vector3.ZERO
state.angular_velocity = _pending_teleport_angular_velocity if _pending_teleport_has_velocity else Vector3.ZERO
_pending_teleport_has_velocity = false
reset_physics_interpolation()
if _boundary:
var pull := _boundary.get_surface_pull(
global_position, wall_pull_strength, wall_pull_range,
ceiling_pull_strength, ceiling_pull_range
)
state.apply_central_force(pull * mass)
if state.linear_velocity.length() > MAX_SPEED:
state.linear_velocity = state.linear_velocity.normalized() * MAX_SPEED