The client's side of a networked match, in src/net/: - ClientGame takes the server's notices and snapshots, generates the island from the seed and answers with commands. It works on packets, so it runs over a NetClient or a test's simulated link. - ClientClock decides which tick the next command is for, from the server's echo of the newest command tick it has received, holding three commands waiting at the server. - ClientPrediction runs each command on a copy of the client's own player with the server's movement code, and compares every snapshot with what it predicted. A difference is replayed from the server's state and eased in. - SnapshotInterpolator draws everyone else four ticks in the past, between snapshots, on a clock steered gently by arrivals. Snapshots now carry the client's own block: exact position and velocity, the water around the player, stamina and state flags, so a prediction can match the server to the bit. The command echo is now the newest tick received rather than taken. Protocol version 2. Snapshots go out every second tick. The interval was three ticks, which is 10 a second, not the 20 the documents claimed; 15 a second is the nearest even spacing and what interpolation is tuned for. Drop.ride() holds the barge ride for the server and the client alike, and NetGame's joining, input and snapshot steps can be called without a socket. Measured in test/net/test_prediction_link.gd over a simulated link with 100 ms each way, 20 ms jitter and 2% loss: no corrections to own movement in about 1,400 snapshots per client, and the other player drawn 0.4 mm on average from where a perfect link would draw them. Closes #34 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
126 lines
4.8 KiB
GDScript
126 lines
4.8 KiB
GDScript
## The start of a match: a sky barge drifts across the island on a seeded straight route,
|
|
## carrying every player. Each one leaps when they choose, falls, opens a wing-sail
|
|
## (by hand, or automatically near the ground) and steers down. Anyone still aboard
|
|
## when the barge reaches the end of its route is thrown off.
|
|
##
|
|
## The route is drawn from the "drop" stream: a heading, and a point near the island's
|
|
## middle that the route passes through, so it always crosses land. Movement handles
|
|
## falling and gliding; this owns the barge, boarding and leaping.
|
|
class_name Drop
|
|
extends RefCounted
|
|
|
|
## Player drop states, in SimEntity.drop_state.
|
|
const NONE: int = 0
|
|
const ABOARD: int = 1
|
|
const FREEFALL: int = 2
|
|
const GLIDING: int = 3
|
|
|
|
## Where the route starts and ends, at barge altitude.
|
|
var start: Vector3 = Vector3.ZERO
|
|
var finish: Vector3 = Vector3.ZERO
|
|
## Ticks the barge takes from start to finish.
|
|
var duration_ticks: int = 0
|
|
## Simulation tick the drop began on.
|
|
var began_tick: int = 0
|
|
## True once the barge has reached the end and thrown everyone off.
|
|
var finished: bool = false
|
|
|
|
|
|
## Plans the route for `island` from the simulation's "drop" stream.
|
|
static func plan(island_ground: SimGround, rng: SimRng, tick: int) -> Drop:
|
|
var drop: Drop = Drop.new()
|
|
var stream: RandomNumberGenerator = rng.stream("drop")
|
|
var size: float = Tuning.WORLD_SIZE_METRES
|
|
var centre: Vector2 = Vector2(size, size) * 0.5
|
|
var heading: float = stream.randf() * TAU
|
|
var jitter_angle: float = stream.randf() * TAU
|
|
var jitter: float = sqrt(stream.randf()) * Tuning.BARGE_CENTRE_JITTER
|
|
var through: Vector2 = centre + Vector2(cos(jitter_angle), sin(jitter_angle)) * jitter
|
|
# Pull the crossing point onto land if the jitter put it over water.
|
|
var tries: int = 0
|
|
while island_ground.terrain_height(through.x, through.y) <= Tuning.ISLAND_BEACH_TOP and tries < 16:
|
|
through = through.lerp(centre, 0.5)
|
|
tries += 1
|
|
var direction: Vector2 = Vector2(cos(heading), sin(heading))
|
|
var margin: float = Tuning.BARGE_ROUTE_MARGIN
|
|
var back: float = _distance_to_edge(through, -direction, margin, size)
|
|
var ahead: float = _distance_to_edge(through, direction, margin, size)
|
|
var a: Vector2 = through - direction * back
|
|
var b: Vector2 = through + direction * ahead
|
|
drop.start = Vector3(a.x, Tuning.BARGE_ALTITUDE, a.y)
|
|
drop.finish = Vector3(b.x, Tuning.BARGE_ALTITUDE, b.y)
|
|
drop.duration_ticks = maxi(ceili(a.distance_to(b) / Tuning.BARGE_SPEED * Tuning.TICK_RATE_HZ), 1)
|
|
drop.began_tick = tick
|
|
return drop
|
|
|
|
|
|
## How far along the route the barge is at `tick`, from 0 to 1.
|
|
func progress(tick: int) -> float:
|
|
return clampf(float(tick - began_tick) / duration_ticks, 0.0, 1.0)
|
|
|
|
|
|
func barge_position(tick: int) -> Vector3:
|
|
return start.lerp(finish, progress(tick))
|
|
|
|
|
|
func barge_velocity() -> Vector3:
|
|
return (finish - start) / (float(duration_ticks) / Tuning.TICK_RATE_HZ)
|
|
|
|
|
|
## True once the barge is at the end of its route.
|
|
func is_over(tick: int) -> bool:
|
|
return tick - began_tick >= duration_ticks
|
|
|
|
|
|
## Points along the route every `step` metres, at ground level, for tests and maps.
|
|
func route_samples(step: float) -> PackedVector2Array:
|
|
var points: PackedVector2Array = PackedVector2Array()
|
|
var a: Vector2 = Vector2(start.x, start.z)
|
|
var b: Vector2 = Vector2(finish.x, finish.z)
|
|
var count: int = maxi(ceili(a.distance_to(b) / step), 1)
|
|
for index: int in count + 1:
|
|
points.append(a.lerp(b, float(index) / count))
|
|
return points
|
|
|
|
|
|
## Puts a player aboard: carried with the barge, not moving on their own.
|
|
static func board(player: SimEntity, at: Vector3) -> void:
|
|
player.drop_state = ABOARD
|
|
player.position = at
|
|
player.velocity = Vector3.ZERO
|
|
player.on_ground = false
|
|
player.swimming = false
|
|
|
|
|
|
## Throws a player off the barge into freefall, keeping some of the barge's speed.
|
|
func leap(player: SimEntity, tick: int) -> void:
|
|
player.drop_state = FREEFALL
|
|
player.position = barge_position(tick)
|
|
var carried: Vector3 = barge_velocity() * Tuning.LEAP_CARRY
|
|
player.velocity = Vector3(carried.x, 0.0, carried.z)
|
|
player.on_ground = false
|
|
|
|
|
|
## A tick aboard the barge: jump leaps off, anything else rides along. The server and a
|
|
## client predicting its own player both come through here.
|
|
func ride(player: SimEntity, command: PlayerCommand, tick: int) -> void:
|
|
if command != null:
|
|
player.yaw = command.yaw
|
|
player.pitch = command.pitch
|
|
if command.is_pressed(PlayerCommand.BUTTON_JUMP):
|
|
leap(player, tick)
|
|
return
|
|
player.position = barge_position(tick + 1)
|
|
|
|
|
|
## Distance from `from` along unit `direction` to the square [margin, size - margin].
|
|
static func _distance_to_edge(from: Vector2, direction: Vector2, margin: float, size: float) -> float:
|
|
var best: float = INF
|
|
for axis: int in 2:
|
|
var d: float = direction[axis]
|
|
if absf(d) < 0.000001:
|
|
continue
|
|
var edge: float = (size - margin) if d > 0.0 else margin
|
|
best = minf(best, (edge - from[axis]) / d)
|
|
return maxf(best, 0.0)
|