From 70e88cec8a279efe0337680fd4479409d6aab889 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 20:24:02 -0500 Subject: [PATCH] W1: Third-person character controller and camera Simulation (src/sim/): - SimGround holds the terrain heightfield (bilinear heights, normals) and axis-aligned box solids, with support, body-overlap, point and ray queries. The island generator and building will fill it. - Movement steps a player per tick: walk, sprint (forward only), crouch (lower body, slower, no jump), jump with exact constant-gravity integration, air control, step-up onto ledges up to STEP_HEIGHT, ground snapping on downhill walks, slopes over MAX_SLOPE_DEGREES that block walking and jumping uphill and make players slide (speed capped), ceiling bumps, sliding along walls, and fall damage from impact speed. - Gravity now applies on ticks with no command. on_ground and crouching are part of the state hash. New movement numbers are in Tuning. - Fixed the strafe direction: right is forward x up in Godot's frame, (-cos yaw, 0, sin yaw). Documented the look convention. Client (src/client/): - CameraRig places an over-the-shoulder camera with an eased aim-down-sights blend (closer, narrower FOV), casting against the same SimGround: pivot to shoulder, shoulder back, then a straight line from the pivot, with a final clearance pull-in so the near plane never enters geometry. - InputSettings: mouse sensitivity, aim sensitivity scale and invert-Y, clamped and saved to user://settings.cfg. PlayerCamera applies both. Tests: 46 new across movement, ground queries, camera and settings, including a 24,480-view scripted camera sweep through five arenas (wall, corridor, low roof, corner, steep bank) that checks the eye and every near-plane corner stay out of geometry with a clear line to the pivot. The hash test now flips a high stick bit, because a 1/127 nudge to an airborne player moves them less than the hash's millimetre. Capture scenarios camera, camera_wall and camera_aim. Closes #8 Co-Authored-By: Claude Opus 5.5 --- docs/ARCHITECTURE.md | 6 + src/client/camera_rig.gd | 92 +++++++++ src/client/camera_rig.gd.uid | 1 + src/client/input_settings.gd | 60 ++++++ src/client/input_settings.gd.uid | 1 + src/client/player_camera.gd | 42 ++++ src/client/player_camera.gd.uid | 1 + src/sim/movement.gd | 153 ++++++++++++++ src/sim/movement.gd.uid | 1 + src/sim/sim_entity.gd | 3 + src/sim/sim_ground.gd | 195 ++++++++++++++++++ src/sim/sim_ground.gd.uid | 1 + src/sim/simulation.gd | 30 +-- src/sim/tuning.gd | 18 ++ test/capture/camera.tscn | 6 + test/capture/camera_aim.tscn | 8 + test/capture/camera_scene.gd | 26 +++ test/capture/camera_scene.gd.uid | 1 + test/capture/camera_wall.tscn | 9 + test/capture/smoke_scene.gd | 11 + test/client/test_camera_rig.gd | 134 ++++++++++++ test/client/test_camera_rig.gd.uid | 1 + test/client/test_input_settings.gd | 68 ++++++ test/client/test_input_settings.gd.uid | 1 + test/sim/test_movement.gd | 275 +++++++++++++++++++++++++ test/sim/test_movement.gd.uid | 1 + test/sim/test_sim_ground.gd | 85 ++++++++ test/sim/test_sim_ground.gd.uid | 1 + test/sim/test_simulation.gd | 5 +- 29 files changed, 1213 insertions(+), 23 deletions(-) create mode 100644 src/client/camera_rig.gd create mode 100644 src/client/camera_rig.gd.uid create mode 100644 src/client/input_settings.gd create mode 100644 src/client/input_settings.gd.uid create mode 100644 src/client/player_camera.gd create mode 100644 src/client/player_camera.gd.uid create mode 100644 src/sim/movement.gd create mode 100644 src/sim/movement.gd.uid create mode 100644 src/sim/sim_ground.gd create mode 100644 src/sim/sim_ground.gd.uid create mode 100644 test/capture/camera.tscn create mode 100644 test/capture/camera_aim.tscn create mode 100644 test/capture/camera_scene.gd create mode 100644 test/capture/camera_scene.gd.uid create mode 100644 test/capture/camera_wall.tscn create mode 100644 test/client/test_camera_rig.gd create mode 100644 test/client/test_camera_rig.gd.uid create mode 100644 test/client/test_input_settings.gd create mode 100644 test/client/test_input_settings.gd.uid create mode 100644 test/sim/test_movement.gd create mode 100644 test/sim/test_movement.gd.uid create mode 100644 test/sim/test_sim_ground.gd create mode 100644 test/sim/test_sim_ground.gd.uid diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 124d928..99d9c4c 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -55,6 +55,12 @@ Clients stamp commands for the tick they predict, and each packet repeats recent commands to survive loss. The server applies the command for each tick, or a neutral command with the player's last look direction if none has arrived. +**Look convention.** At yaw 0 the player faces +z; forward is `(sin yaw, 0, cos yaw)`. +Right is forward × up, `(-cos yaw, 0, sin yaw)`, matching Godot's right-handed, y-up +frame, so turning right *decreases* yaw. Positive pitch looks up. `Movement` and the +client's `CameraRig` both follow this, and a test checks that strafing right moves +towards the camera's screen-right. + The binary encoding is a fixed 14 bytes per command, little-endian, in the order of the table above. See `src/sim/player_command.gd`. Decoding a truncated buffer returns nothing, and out-of-range values are clamped, so bad input can't reach the simulation. diff --git a/src/client/camera_rig.gd b/src/client/camera_rig.gd new file mode 100644 index 0000000..c1a2f9a --- /dev/null +++ b/src/client/camera_rig.gd @@ -0,0 +1,92 @@ +## Over-the-shoulder third-person camera placement, with collision and aim-down-sights. +## Pure logic: it reads the same SimGround the simulation uses, so the camera can't see +## through anything the player can't walk through. PlayerCamera applies the result. +class_name CameraRig +extends RefCounted + +const PIVOT_HEIGHT: float = 1.6 +const CROUCH_PIVOT_HEIGHT: float = 1.05 +const SHOULDER: float = 0.6 +const AIM_SHOULDER: float = 0.8 +const DISTANCE: float = 3.4 +const AIM_DISTANCE: float = 1.8 +const FOV: float = 72.0 +const AIM_FOV: float = 48.0 +## Clearance kept between the camera and any surface, larger than the near plane's +## half-extent so the near plane never cuts into geometry. +const MARGIN: float = 0.25 +const NEAR: float = 0.05 +## Minimum distance from any surface, enough to keep the whole near plane outside it. +const CLEARANCE: float = 0.1 +const CLEAR_STEPS: int = 16 +## Seconds to move most of the way between hip and aim. +const AIM_BLEND_SECONDS: float = 0.12 + +## 0 at the hip, 1 fully aimed. Advanced by `update_aim`. +var aim_blend: float = 0.0 + + +## Eases aim_blend towards aiming or not over `delta` seconds. +func update_aim(aiming: bool, delta: float) -> void: + var target: float = 1.0 if aiming else 0.0 + var rate: float = 1.0 - exp(-delta * 3.0 / AIM_BLEND_SECONDS) + aim_blend = lerpf(aim_blend, target, rate) + if absf(aim_blend - target) < 0.001: + aim_blend = target + + +## The look direction for a yaw and pitch, matching the simulation's convention that +## forward at yaw 0 is +z and positive pitch looks up. +static func look_direction(yaw: float, pitch: float) -> Vector3: + return Vector3(sin(yaw) * cos(pitch), sin(pitch), cos(yaw) * cos(pitch)) + + +## The point the camera orbits, above the player's feet. +static func pivot(feet: Vector3, crouching: bool) -> Vector3: + return feet + Vector3.UP * (CROUCH_PIVOT_HEIGHT if crouching else PIVOT_HEIGHT) + + +## Solves the camera transform for a player. The camera looks along the aim direction +## from behind and to the right of the pivot, pulled in where geometry is in the way. +func solve(feet: Vector3, yaw: float, pitch: float, crouching: bool, ground: SimGround) -> Transform3D: + var look: Vector3 = look_direction(yaw, pitch) + var right: Vector3 = Vector3(-cos(yaw), 0.0, sin(yaw)) + var shoulder: float = lerpf(SHOULDER, AIM_SHOULDER, aim_blend) + var distance: float = lerpf(DISTANCE, AIM_DISTANCE, aim_blend) + + var origin: Vector3 = pivot(feet, crouching) + var beside: Vector3 = _cast(ground, origin, origin + right * shoulder) + var eye: Vector3 = _cast(ground, beside, beside - look * distance) + # The two legs can each be clear while the camera ends up over a wall top, where the + # wall hides the player. Keep a clear straight line back to the pivot as well. + eye = _cast(ground, origin, eye) + eye = _clear(ground, eye, origin) + var basis: Basis = Basis.looking_at(look, Vector3.UP if absf(look.y) < 0.999 else Vector3.FORWARD) + return Transform3D(basis, eye) + + +func fov() -> float: + return lerpf(FOV, AIM_FOV, aim_blend) + + +## Moves from `from` towards `to`, stopping MARGIN short of the first surface. +static func _cast(ground: SimGround, from: Vector3, to: Vector3) -> Vector3: + var length: float = from.distance_to(to) + if length <= 0.0: + return from + var direction: Vector3 = (to - from) / length + # Cast MARGIN past the end, so an end point just short of a wall is pulled in too. + var reach: float = length + MARGIN + var hit: float = ground.raycast(from, from + direction * reach) + return from + direction * clampf(hit * reach - MARGIN, 0.0, length) + + +## A ray can graze a surface it never hits. If the eye ended up within the near plane's +## clearance of anything, slide it back towards `safe` until it's clear. +static func _clear(ground: SimGround, eye: Vector3, safe: Vector3) -> Vector3: + var point: Vector3 = eye + for step: int in CLEAR_STEPS: + if not ground.point_inside(point, CLEARANCE): + return point + point = eye.lerp(safe, float(step + 1) / CLEAR_STEPS) + return safe diff --git a/src/client/camera_rig.gd.uid b/src/client/camera_rig.gd.uid new file mode 100644 index 0000000..c108f4a --- /dev/null +++ b/src/client/camera_rig.gd.uid @@ -0,0 +1 @@ +uid://bffma7fh66ygi diff --git a/src/client/input_settings.gd b/src/client/input_settings.gd new file mode 100644 index 0000000..fcafcec --- /dev/null +++ b/src/client/input_settings.gd @@ -0,0 +1,60 @@ +## Player-adjustable look settings, saved in the user's config file. +class_name InputSettings +extends RefCounted + +const DEFAULT_PATH: String = "user://settings.cfg" +const SECTION: String = "input" +## Radians of turn per pixel of mouse motion at sensitivity 1.0. +const RADIANS_PER_PIXEL: float = 0.0025 +const MIN_SENSITIVITY: float = 0.05 +const MAX_SENSITIVITY: float = 10.0 +## Looking straight up or down flips the camera basis, so stop just short. +const PITCH_LIMIT: float = deg_to_rad(85.0) + +var mouse_sensitivity: float = 1.0: + set(value): + mouse_sensitivity = clampf(value, MIN_SENSITIVITY, MAX_SENSITIVITY) +## Multiplies the sensitivity while aiming down sights. +var aim_sensitivity_scale: float = 0.6: + set(value): + aim_sensitivity_scale = clampf(value, 0.1, 2.0) +var invert_y: bool = false + + +## Applies a mouse motion to a look direction. Returns Vector2(yaw, pitch) in radians. +## Moving the mouse right turns right; moving it up looks up, unless invert_y is set. +func apply_mouse(relative: Vector2, yaw: float, pitch: float, aiming: bool) -> Vector2: + var scale: float = RADIANS_PER_PIXEL * mouse_sensitivity + if aiming: + scale *= aim_sensitivity_scale + var vertical: float = relative.y if invert_y else -relative.y + var new_yaw: float = fposmod(yaw - relative.x * scale, TAU) + var new_pitch: float = clampf(pitch + vertical * scale, -PITCH_LIMIT, PITCH_LIMIT) + return Vector2(new_yaw, new_pitch) + + +func save(path: String = DEFAULT_PATH) -> Error: + var config: ConfigFile = ConfigFile.new() + config.load(path) # Keep other sections; a missing file is fine. + config.set_value(SECTION, "mouse_sensitivity", mouse_sensitivity) + config.set_value(SECTION, "aim_sensitivity_scale", aim_sensitivity_scale) + config.set_value(SECTION, "invert_y", invert_y) + return config.save(path) + + +## Loads saved settings. Missing or malformed values keep their defaults. +static func load_from(path: String = DEFAULT_PATH) -> InputSettings: + var settings: InputSettings = InputSettings.new() + var config: ConfigFile = ConfigFile.new() + if config.load(path) != OK: + return settings + var sensitivity: Variant = config.get_value(SECTION, "mouse_sensitivity", 1.0) + if sensitivity is float or sensitivity is int: + settings.mouse_sensitivity = float(sensitivity) + var aim_scale: Variant = config.get_value(SECTION, "aim_sensitivity_scale", 0.6) + if aim_scale is float or aim_scale is int: + settings.aim_sensitivity_scale = float(aim_scale) + var invert: Variant = config.get_value(SECTION, "invert_y", false) + if invert is bool: + settings.invert_y = invert + return settings diff --git a/src/client/input_settings.gd.uid b/src/client/input_settings.gd.uid new file mode 100644 index 0000000..b09e783 --- /dev/null +++ b/src/client/input_settings.gd.uid @@ -0,0 +1 @@ +uid://bn4kidspsub2m diff --git a/src/client/player_camera.gd b/src/client/player_camera.gd new file mode 100644 index 0000000..fe7b00d --- /dev/null +++ b/src/client/player_camera.gd @@ -0,0 +1,42 @@ +## The local player's third-person camera. Turns mouse motion into a look direction using +## InputSettings, and places itself each frame with CameraRig, so it never clips through +## terrain or structures. The input layer reads `yaw` and `pitch` when it builds the +## player's commands. +class_name PlayerCamera +extends Camera3D + +var settings: InputSettings = InputSettings.new() +var rig: CameraRig = CameraRig.new() +var ground: SimGround = SimGround.flat() +## Radians, in the simulation's convention (see docs/ARCHITECTURE.md). +var yaw: float = 0.0 +var pitch: float = 0.0 +var aiming: bool = false +## Where the followed player's feet are, and whether they're crouching. Set by whoever +## owns the player's presentation, from interpolated snapshots. +var target_feet: Vector3 = Vector3.ZERO +var target_crouching: bool = false + + +func _ready() -> void: + near = CameraRig.NEAR + fov = CameraRig.FOV + + +func _unhandled_input(event: InputEvent) -> void: + var motion: InputEventMouseMotion = event as InputEventMouseMotion + if motion != null and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED: + var look: Vector2 = settings.apply_mouse(motion.relative, yaw, pitch, aiming) + yaw = look.x + pitch = look.y + + +func _process(delta: float) -> void: + rig.update_aim(aiming, delta) + snap() + + +## Places the camera for the current target and look direction immediately. +func snap() -> void: + global_transform = rig.solve(target_feet, yaw, pitch, target_crouching, ground) + fov = rig.fov() diff --git a/src/client/player_camera.gd.uid b/src/client/player_camera.gd.uid new file mode 100644 index 0000000..9707f18 --- /dev/null +++ b/src/client/player_camera.gd.uid @@ -0,0 +1 @@ +uid://bkvavm0kwaxr5 diff --git a/src/sim/movement.gd b/src/sim/movement.gd new file mode 100644 index 0000000..bc4ba50 --- /dev/null +++ b/src/sim/movement.gd @@ -0,0 +1,153 @@ +## Command-driven player movement: walk, sprint, crouch, jump, gravity, slopes, +## step-up, sliding and fall damage. Deterministic: it depends only on the entity, the +## command and the ground. +class_name Movement +extends RefCounted + +const DT: float = 1.0 / Tuning.TICK_RATE_HZ + + +## Advances one player by one tick. `command` may be null, meaning no input this tick: +## the player keeps looking the same way and stops trying to move, but still falls. +static func step(player: SimEntity, command: PlayerCommand, ground: SimGround) -> void: + var wish: Vector3 = Vector3.ZERO + var wants_jump: bool = false + player.crouching = false + if command != null: + player.yaw = command.yaw + player.pitch = command.pitch + player.crouching = command.is_pressed(PlayerCommand.BUTTON_CROUCH) + wants_jump = command.is_pressed(PlayerCommand.BUTTON_JUMP) and not player.crouching + wish = _wish_velocity(command, player.crouching) + + var velocity: Vector3 = player.velocity + if player.on_ground: + velocity.x = wish.x + velocity.z = wish.z + velocity.y = 0.0 + if wants_jump: + velocity.y = Tuning.JUMP_VELOCITY + player.on_ground = false + else: + velocity.x += (wish.x - velocity.x) * Tuning.AIR_CONTROL + velocity.z += (wish.z - velocity.z) * Tuning.AIR_CONTROL + + velocity = _move_horizontally(player, velocity, ground) + _move_vertically(player, velocity, ground) + + +## Height of the player's body, which shrinks while crouching. +static func body_height(player: SimEntity) -> float: + return Tuning.CROUCH_HEIGHT if player.crouching else Tuning.PLAYER_HEIGHT + + +static func _wish_velocity(command: PlayerCommand, crouching: bool) -> Vector3: + var move: Vector2 = command.move_vector() + if move.length() > 1.0: + move = move.normalized() + var angle: float = command.yaw_radians() + var forward: Vector3 = Vector3(sin(angle), 0.0, cos(angle)) + # Right is forward x up in Godot's right-handed, y-up frame. + var right: Vector3 = Vector3(-cos(angle), 0.0, sin(angle)) + var speed: float = Tuning.WALK_SPEED + if crouching: + speed = Tuning.CROUCH_SPEED + elif command.is_pressed(PlayerCommand.BUTTON_SPRINT) and move.y > 0.0: + speed = Tuning.SPRINT_SPEED + return (right * move.x + forward * move.y) * speed + + +## Moves along x and z, sliding along whatever blocks the way. Returns the velocity with +## blocked components removed. +static func _move_horizontally(player: SimEntity, velocity: Vector3, ground: SimGround) -> Vector3: + var delta: Vector3 = Vector3(velocity.x, 0.0, velocity.z) * DT + if delta.is_zero_approx(): + return velocity + var result: Vector3 = velocity + if _can_move_to(player, player.position + delta, ground): + player.position += delta + elif _can_move_to(player, player.position + Vector3(delta.x, 0.0, 0.0), ground): + player.position.x += delta.x + result.z = 0.0 + elif _can_move_to(player, player.position + Vector3(0.0, 0.0, delta.z), ground): + player.position.z += delta.z + result.x = 0.0 + else: + result.x = 0.0 + result.z = 0.0 + var size: float = Tuning.WORLD_SIZE_METRES + player.position.x = clampf(player.position.x, 0.0, size) + player.position.z = clampf(player.position.z, 0.0, size) + return result + + +static func _can_move_to(player: SimEntity, target: Vector3, ground: SimGround) -> bool: + var step: float = Tuning.STEP_HEIGHT if player.on_ground else 0.0 + if ground.body_blocked(target, Tuning.PLAYER_RADIUS, body_height(player), step): + return false + # Moving uphill into terrain that's too steep is blocked, on foot or in the air, so + # nobody can climb a cliff by pushing into it. Downhill is always allowed. + var rise: float = ground.terrain_height(target.x, target.z) - player.position.y + if rise > 0.0 and not ground.walkable_terrain(target.x, target.z): + if not ground.support_is_solid(target.x, target.z, player.position.y + step): + return false + return true + + +static func _move_vertically(player: SimEntity, velocity: Vector3, ground: SimGround) -> void: + var feet: Vector3 = player.position + if player.on_ground: + var reach: float = feet.y + Tuning.STEP_HEIGHT + var support: float = ground.support_height(feet.x, feet.z, reach) + if support >= feet.y - Tuning.GROUND_SNAP: + player.position.y = support + player.velocity = Vector3(velocity.x, 0.0, velocity.z) + if not _walkable_support(ground, player.position): + player.on_ground = false + return + # Walked off a ledge: start falling from here. + player.on_ground = false + + # Airborne. Exact integration under constant gravity. + var new_y: float = feet.y + velocity.y * DT - 0.5 * Tuning.GRAVITY * DT * DT + velocity.y -= Tuning.GRAVITY * DT + var head_room: Vector3 = Vector3(feet.x, new_y, feet.z) + if velocity.y > 0.0 and ground.body_blocked(head_room, Tuning.PLAYER_RADIUS, body_height(player), 0.0): + # Bumped a ceiling. + new_y = feet.y + velocity.y = 0.0 + player.position.y = new_y + + var support: float = ground.support_height(feet.x, feet.z, maxf(feet.y, new_y)) + if new_y > support: + player.velocity = velocity + return + player.position.y = support + if _walkable_support(ground, player.position): + _land(player, -velocity.y) + velocity.y = 0.0 + player.on_ground = true + else: + # Too steep to stand on: keep only the motion along the slope. + var normal: Vector3 = ground.terrain_normal(feet.x, feet.z) + var into: float = velocity.dot(normal) + if into < 0.0: + velocity -= normal * into + velocity = velocity.limit_length(Tuning.SLIDE_MAX_SPEED) + player.velocity = velocity + + +static func _walkable_support(ground: SimGround, feet: Vector3) -> bool: + if ground.support_is_solid(feet.x, feet.z, feet.y + 0.001): + return true + return ground.walkable_terrain(feet.x, feet.z) + + +## Applies fall damage for landing at `impact_speed` metres per second, downward. +static func _land(player: SimEntity, impact_speed: float) -> void: + if impact_speed <= 0.0: + return + var drop: float = impact_speed * impact_speed / (2.0 * Tuning.GRAVITY) + var excess: float = drop - Tuning.FALL_DAMAGE_SAFE_METRES + if excess > 0.0: + player.health = maxi(player.health - roundi(excess * Tuning.FALL_DAMAGE_PER_METRE), 0) diff --git a/src/sim/movement.gd.uid b/src/sim/movement.gd.uid new file mode 100644 index 0000000..8fd2be3 --- /dev/null +++ b/src/sim/movement.gd.uid @@ -0,0 +1 @@ +uid://b4wtn2is3k3td diff --git a/src/sim/sim_entity.gd b/src/sim/sim_entity.gd index c4e0100..284b655 100644 --- a/src/sim/sim_entity.gd +++ b/src/sim/sim_entity.gd @@ -11,6 +11,9 @@ var velocity: Vector3 = Vector3.ZERO var yaw: int = 0 var pitch: int = 0 var health: int = 0 +## Standing on walkable ground. False while jumping, falling or sliding. +var on_ground: bool = false +var crouching: bool = false func _init(entity_id: int, entity_archetype: int) -> void: diff --git a/src/sim/sim_ground.gd b/src/sim/sim_ground.gd new file mode 100644 index 0000000..751a160 --- /dev/null +++ b/src/sim/sim_ground.gd @@ -0,0 +1,195 @@ +## Everything solid that players stand on, walk into and see through: a terrain +## heightfield plus axis-aligned box solids (structures, rocks). The island generator +## fills the heightfield; building adds and removes solids. +## +## All queries are pure functions of the stored data, so the server, client prediction +## and the camera all agree. +class_name SimGround +extends RefCounted + +## Ray-marching step for terrain, in metres. Small enough for camera-length rays. +const RAY_STEP: float = 0.05 +const RAY_REFINE_STEPS: int = 10 +const _MARGIN_OFFSETS: Array[Vector2] = [ + Vector2(1.0, 0.0), Vector2(-1.0, 0.0), Vector2(0.0, 1.0), Vector2(0.0, -1.0), + Vector2(0.7071, 0.7071), Vector2(0.7071, -0.7071), Vector2(-0.7071, 0.7071), Vector2(-0.7071, -0.7071), +] + +## Heights in row-major order: index = z * samples + x, `cell` metres apart, starting +## at the world origin. Outside the grid the nearest edge sample is used. +var heights: PackedFloat32Array = PackedFloat32Array() +var samples: int = 0 +var cell: float = 1.0 +var solids: Array[AABB] = [] + + +## A flat world at `height`. +static func flat(height: float = 0.0) -> SimGround: + var ground: SimGround = SimGround.new() + ground.samples = 2 + ground.cell = Tuning.WORLD_SIZE_METRES + ground.heights = PackedFloat32Array([height, height, height, height]) + return ground + + +## A heightfield of `side` x `side` samples spaced `spacing` metres apart. +static func from_heights(values: PackedFloat32Array, side: int, spacing: float) -> SimGround: + assert(side >= 2 and values.size() == side * side and spacing > 0.0) + var ground: SimGround = SimGround.new() + ground.heights = values + ground.samples = side + ground.cell = spacing + return ground + + +func add_solid(box: AABB) -> void: + solids.append(box.abs()) + + +## Terrain height at (x, z), bilinearly interpolated. +func terrain_height(x: float, z: float) -> float: + var limit: float = float(samples - 1) + var gx: float = clampf(x / cell, 0.0, limit) + var gz: float = clampf(z / cell, 0.0, limit) + var x0: int = mini(floori(gx), samples - 2) + var z0: int = mini(floori(gz), samples - 2) + var fx: float = gx - x0 + var fz: float = gz - z0 + var row: int = z0 * samples + var h00: float = heights[row + x0] + var h10: float = heights[row + x0 + 1] + var h01: float = heights[row + samples + x0] + var h11: float = heights[row + samples + x0 + 1] + return lerpf(lerpf(h00, h10, fx), lerpf(h01, h11, fx), fz) + + +## Upward terrain normal at (x, z), from central differences. +func terrain_normal(x: float, z: float) -> Vector3: + var d: float = minf(cell * 0.5, 0.5) + var dx: float = terrain_height(x + d, z) - terrain_height(x - d, z) + var dz: float = terrain_height(x, z + d) - terrain_height(x, z - d) + return Vector3(-dx, 2.0 * d, -dz).normalized() + + +## True if the terrain at (x, z) is gentle enough to stand on. +func walkable_terrain(x: float, z: float) -> bool: + return terrain_normal(x, z).y >= cos(deg_to_rad(Tuning.MAX_SLOPE_DEGREES)) + + +## The highest surface under (x, z) that is no higher than `max_y`: the terrain, or the +## top of a solid. Solid tops are always walkable. +func support_height(x: float, z: float, max_y: float) -> float: + var best: float = terrain_height(x, z) + for box: AABB in solids: + var top: float = box.end.y + if top > best and top <= max_y and _inside_footprint(box, x, z, 0.0): + best = top + return best + + +## True when the best support under (x, z) up to `max_y` is the top of a solid. +func support_is_solid(x: float, z: float, max_y: float) -> bool: + var terrain: float = terrain_height(x, z) + for box: AABB in solids: + var top: float = box.end.y + if top > terrain and top <= max_y and _inside_footprint(box, x, z, 0.0): + return true + return false + + +## True if a player body at `feet` overlaps a solid. Solids whose top is within +## `step` of the feet don't count, so they can be stepped onto. +func body_blocked(feet: Vector3, radius: float, height: float, step: float) -> bool: + for box: AABB in solids: + if box.end.y <= feet.y + step or box.position.y >= feet.y + height: + continue + if _inside_footprint(box, feet.x, feet.z, radius): + return true + return false + + +## Casts from `from` to `to` against terrain and solids. Returns the fraction of the way +## to the first hit, or 1.0 if nothing is hit. A ray starting inside something returns 0. +func raycast(from: Vector3, to: Vector3) -> float: + var nearest: float = 1.0 + for box: AABB in solids: + nearest = minf(nearest, _ray_box(from, to, box)) + return minf(nearest, _ray_terrain(from, to, nearest)) + + +## True if `point` is inside the terrain or a solid, or within `margin` of one. For the +## terrain, heights are also checked `margin` away on each side, so steep ground counts +## its sideways distance and not just the height under the point. +func point_inside(point: Vector3, margin: float = 0.0) -> bool: + if point.y < terrain_height(point.x, point.z) + margin: + return true + if margin > 0.0: + for offset: Vector2 in _MARGIN_OFFSETS: + var probe: Vector2 = offset * margin + if point.y < terrain_height(point.x + probe.x, point.z + probe.y): + return true + for box: AABB in solids: + if box.grow(margin).has_point(point): + return true + return false + + +func _inside_footprint(box: AABB, x: float, z: float, radius: float) -> bool: + return ( + x > box.position.x - radius and x < box.end.x + radius + and z > box.position.z - radius and z < box.end.z + radius + ) + + +## Slab test. Returns the entry fraction in [0, 1], or 1.0 for a miss. +func _ray_box(from: Vector3, to: Vector3, box: AABB) -> float: + var direction: Vector3 = to - from + var enter: float = 0.0 + var leave: float = 1.0 + for axis: int in 3: + var start: float = from[axis] + var span: float = direction[axis] + var low: float = box.position[axis] + var high: float = box.end[axis] + if absf(span) < 0.000001: + if start < low or start > high: + return 1.0 + continue + var t1: float = (low - start) / span + var t2: float = (high - start) / span + enter = maxf(enter, minf(t1, t2)) + leave = minf(leave, maxf(t1, t2)) + if enter > leave: + return 1.0 + return enter + + +## Marches the ray up to `limit`, then refines the first crossing below the terrain. +func _ray_terrain(from: Vector3, to: Vector3, limit: float) -> float: + var length: float = from.distance_to(to) + if length <= 0.0: + return 0.0 if _below_terrain(from) else 1.0 + if _below_terrain(from): + return 0.0 + var step: float = RAY_STEP / length + var previous: float = 0.0 + var t: float = step + while previous < limit: + t = minf(t, limit) + if _below_terrain(from.lerp(to, t)): + var low: float = previous + var high: float = t + for _index: int in RAY_REFINE_STEPS: + var middle: float = (low + high) * 0.5 + if _below_terrain(from.lerp(to, middle)): + high = middle + else: + low = middle + return low + previous = t + t += step + return 1.0 + + +func _below_terrain(point: Vector3) -> bool: + return point.y < terrain_height(point.x, point.z) diff --git a/src/sim/sim_ground.gd.uid b/src/sim/sim_ground.gd.uid new file mode 100644 index 0000000..c2d2068 --- /dev/null +++ b/src/sim/sim_ground.gd.uid @@ -0,0 +1 @@ +uid://oxycp26a8vmr diff --git a/src/sim/simulation.gd b/src/sim/simulation.gd index fe9be10..9cb4396 100644 --- a/src/sim/simulation.gd +++ b/src/sim/simulation.gd @@ -1,7 +1,7 @@ ## The authoritative fixed-tick game state. It takes commands and a seed and nothing ## else: no scene tree, no wall clock, no unseeded randomness. ## -## Movement here is a flat placeholder until the character controller lands in v0.2. +## Movement rules live in Movement; the world's solid shape lives in `ground`. class_name Simulation extends RefCounted @@ -10,6 +10,8 @@ const SECONDS_PER_TICK: float = 1.0 / Tuning.TICK_RATE_HZ var tick: int = 0 var entities: EntityRegistry = EntityRegistry.new() var rng: SimRng +## Terrain and solids. Flat until the island generator provides a heightfield. +var ground: SimGround = SimGround.flat(0.0) ## Player entity ID -> { command tick -> PlayerCommand } var _pending: Dictionary = {} @@ -26,7 +28,10 @@ func add_player() -> int: var player: SimEntity = entities.create(SimEntity.ARCHETYPE_PLAYER) var spawn: RandomNumberGenerator = rng.stream("spawn") var size: float = Tuning.WORLD_SIZE_METRES - player.position = Vector3(spawn.randf() * size, 0.0, spawn.randf() * size) + var x: float = spawn.randf() * size + var z: float = spawn.randf() * size + player.position = Vector3(x, ground.terrain_height(x, z), z) + player.on_ground = true player.health = Tuning.PLAYER_MAX_HEALTH _players.append(player.id) _pending[player.id] = {} @@ -63,25 +68,7 @@ func step() -> void: func _apply_command(player: SimEntity, command: PlayerCommand) -> void: - # With no command this tick, the player keeps looking the same way and stands still. - if command == null: - return - player.yaw = command.yaw - player.pitch = command.pitch - - var move: Vector2 = command.move_vector() - if move.length() > 1.0: - move = move.normalized() - var angle: float = command.yaw_radians() - var forward: Vector3 = Vector3(sin(angle), 0.0, cos(angle)) - var right: Vector3 = Vector3(cos(angle), 0.0, -sin(angle)) - var speed: float = ( - Tuning.SPRINT_SPEED if command.is_pressed(PlayerCommand.BUTTON_SPRINT) - else Tuning.WALK_SPEED - ) - var delta: Vector3 = (right * move.x + forward * move.y) * speed * SECONDS_PER_TICK - var size: float = Tuning.WORLD_SIZE_METRES - player.position = (player.position + delta).clamp(Vector3.ZERO, Vector3(size, 0.0, size)) + Movement.step(player, command, ground) ## SHA-256 hex digest of a canonical encoding of the whole state. @@ -101,6 +88,7 @@ func state_hash() -> String: buffer.put_32(entity.health) buffer.put_32(entity.yaw) buffer.put_32(entity.pitch) + buffer.put_u8((1 if entity.on_ground else 0) | (2 if entity.crouching else 0)) for value: float in [entity.position.x, entity.position.y, entity.position.z]: buffer.put_64(roundi(value * 1000.0)) for value: float in [entity.velocity.x, entity.velocity.y, entity.velocity.z]: diff --git a/src/sim/tuning.gd b/src/sim/tuning.gd index 94bc9b6..4dde8b7 100644 --- a/src/sim/tuning.gd +++ b/src/sim/tuning.gd @@ -30,6 +30,24 @@ const SWIM_SPEED: float = 2.0 const CROUCH_SPEED: float = 2.0 const JUMP_VELOCITY: float = 7.5 const GRAVITY: float = 20.0 +## Fraction of the gap between current and wanted horizontal velocity closed per tick +## while airborne. +const AIR_CONTROL: float = 0.08 +## Ledges up to this height are stepped onto without jumping. +const STEP_HEIGHT: float = 0.45 +## Walking keeps the feet on ground that drops away by up to this much per tick. +const GROUND_SNAP: float = 0.35 +## Steeper ground can't be walked up or stood on; players slide down it. +const MAX_SLOPE_DEGREES: float = 46.0 +## Top speed sliding down ground too steep to stand on. +const SLIDE_MAX_SPEED: float = 8.0 +const PLAYER_RADIUS: float = 0.4 +const PLAYER_HEIGHT: float = 1.8 +const CROUCH_HEIGHT: float = 1.2 +## Landing from a drop of this many metres or less does no damage. Measured from impact +## speed, so sliding down a slope is gentler than falling the same height. +const FALL_DAMAGE_SAFE_METRES: float = 6.0 +const FALL_DAMAGE_PER_METRE: float = 8.0 # The tide. `hold` and `rise` are seconds, `level` is metres above the starting sea, # `drown_dps` is damage per second underwater once breath runs out. The last level diff --git a/test/capture/camera.tscn b/test/capture/camera.tscn new file mode 100644 index 0000000..b9b7025 --- /dev/null +++ b/test/capture/camera.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/camera_scene.gd" id="1"] + +[node name="CaptureCamera" type="Node3D"] +script = ExtResource("1") diff --git a/test/capture/camera_aim.tscn b/test/capture/camera_aim.tscn new file mode 100644 index 0000000..e565abe --- /dev/null +++ b/test/capture/camera_aim.tscn @@ -0,0 +1,8 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/camera_scene.gd" id="1"] + +[node name="CaptureCameraAim" type="Node3D"] +script = ExtResource("1") +yaw_degrees = 20.0 +aiming = true diff --git a/test/capture/camera_scene.gd b/test/capture/camera_scene.gd new file mode 100644 index 0000000..89a7d21 --- /dev/null +++ b/test/capture/camera_scene.gd @@ -0,0 +1,26 @@ +## Capture scenario for the third-person camera: the shore scene with a stand-in player, +## viewed through PlayerCamera and CameraRig as in the game. +extends "res://test/capture/smoke_scene.gd" + +@export var feet: Vector3 = Vector3(1.0, 0.0, 0.5) +@export var yaw_degrees: float = 0.0 +@export var pitch_degrees: float = -8.0 +@export var aiming: bool = false + + +func _add_camera() -> void: + var body: CapsuleMesh = CapsuleMesh.new() + body.radius = Tuning.PLAYER_RADIUS + body.height = Tuning.PLAYER_HEIGHT + _place(body, feet + Vector3.UP * Tuning.PLAYER_HEIGHT * 0.5, Color(0.2, 0.42, 0.36)) + var camera: PlayerCamera = PlayerCamera.new() + camera.ground = ground + camera.target_feet = feet + camera.yaw = deg_to_rad(yaw_degrees) + camera.pitch = deg_to_rad(pitch_degrees) + camera.aiming = aiming + if aiming: + camera.rig.aim_blend = 1.0 + add_child(camera) + camera.make_current() + camera.snap() diff --git a/test/capture/camera_scene.gd.uid b/test/capture/camera_scene.gd.uid new file mode 100644 index 0000000..c71a016 --- /dev/null +++ b/test/capture/camera_scene.gd.uid @@ -0,0 +1 @@ +uid://cxokowai701cm diff --git a/test/capture/camera_wall.tscn b/test/capture/camera_wall.tscn new file mode 100644 index 0000000..f7832c4 --- /dev/null +++ b/test/capture/camera_wall.tscn @@ -0,0 +1,9 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/camera_scene.gd" id="1"] + +[node name="CaptureCameraWall" type="Node3D"] +script = ExtResource("1") +feet = Vector3(-1.5, 0, 2.4) +yaw_degrees = 180.0 +pitch_degrees = -5.0 diff --git a/test/capture/smoke_scene.gd b/test/capture/smoke_scene.gd index 18dc6c7..550dcb0 100644 --- a/test/capture/smoke_scene.gd +++ b/test/capture/smoke_scene.gd @@ -2,6 +2,9 @@ ## post, under a procedural sky. Everything is built in code, so there are no assets. extends Node3D +## The solid shape of the scene, for scenarios that need collision (the camera rig). +var ground: SimGround = SimGround.flat(0.0) + func _ready() -> void: var environment: Environment = Environment.new() @@ -35,6 +38,11 @@ func _ready() -> void: _cylinder(0.35, 5.0, Vector3(5.0, 2.5, -2.0), Color(0.55, 0.36, 0.22)) _cylinder(0.6, 0.8, Vector3(5.0, 5.4, -2.0), Color(0.72, 0.22, 0.18)) + _add_camera() + + +## Override to view the scene differently. +func _add_camera() -> void: var camera: Camera3D = Camera3D.new() camera.position = Vector3(-6.0, 6.0, -16.0) add_child(camera) @@ -46,9 +54,12 @@ func _box(size: Vector3, at: Vector3, colour: Color) -> void: var mesh: BoxMesh = BoxMesh.new() mesh.size = size _place(mesh, at, colour) + if at.y + size.y * 0.5 > 0.0: + ground.add_solid(AABB(at - size * 0.5, size)) func _cylinder(radius: float, height: float, at: Vector3, colour: Color) -> void: + ground.add_solid(AABB(at - Vector3(radius, height * 0.5, radius), Vector3(radius * 2.0, height, radius * 2.0))) var mesh: CylinderMesh = CylinderMesh.new() mesh.top_radius = radius mesh.bottom_radius = radius diff --git a/test/client/test_camera_rig.gd b/test/client/test_camera_rig.gd new file mode 100644 index 0000000..490d7d1 --- /dev/null +++ b/test/client/test_camera_rig.gd @@ -0,0 +1,134 @@ +extends TestCase + +const ASPECT: float = 16.0 / 9.0 + + +func test_open_ground_places_camera_behind_and_right() -> void: + var rig: CameraRig = CameraRig.new() + var feet: Vector3 = Vector3(100.0, 0.0, 100.0) + var view: Transform3D = rig.solve(feet, 0.0, 0.0, false, SimGround.flat()) + var pivot: Vector3 = CameraRig.pivot(feet, false) + # Yaw 0 faces +z, so the camera sits on -z, and screen-right is -x. + var expected: Vector3 = pivot + Vector3(-CameraRig.SHOULDER, 0.0, -CameraRig.DISTANCE) + assert_true(view.origin.distance_to(expected) < 0.001, "camera at %s" % view.origin) + assert_true((-view.basis.z).distance_to(Vector3(0.0, 0.0, 1.0)) < 0.001, "camera must look along +z") + + +func test_strafing_right_moves_towards_screen_right() -> void: + for yaw: float in [0.0, 1.0, 2.5, 4.0, 5.5]: + var view: Transform3D = CameraRig.new().solve(Vector3(500.0, 0.0, 500.0), yaw, 0.0, false, SimGround.flat()) + var player: SimEntity = SimEntity.new(1, SimEntity.ARCHETYPE_PLAYER) + player.position = Vector3(500.0, 0.0, 500.0) + player.on_ground = true + Movement.step(player, PlayerCommand.from_input(0, Vector2(1.0, 0.0), yaw, 0.0), SimGround.flat()) + var moved: Vector3 = (player.position - Vector3(500.0, 0.0, 500.0)).normalized() + assert_true(moved.dot(view.basis.x) > 0.99, "yaw %f: strafe right went %s" % [yaw, moved]) + # And walking forward goes where the camera looks. + var walker: SimEntity = SimEntity.new(2, SimEntity.ARCHETYPE_PLAYER) + walker.position = Vector3(500.0, 0.0, 500.0) + walker.on_ground = true + Movement.step(walker, PlayerCommand.from_input(0, Vector2(0.0, 1.0), yaw, 0.0), SimGround.flat()) + var ahead: Vector3 = (walker.position - Vector3(500.0, 0.0, 500.0)).normalized() + assert_true(ahead.dot(-view.basis.z) > 0.99, "yaw %f: forward went %s" % [yaw, ahead]) + + +func test_aiming_pulls_in_and_narrows() -> void: + var rig: CameraRig = CameraRig.new() + var feet: Vector3 = Vector3(100.0, 0.0, 100.0) + var hip: Transform3D = rig.solve(feet, 0.0, 0.0, false, SimGround.flat()) + assert_eq(rig.fov(), CameraRig.FOV) + for _index: int in 60: + rig.update_aim(true, 1.0 / 60.0) + assert_eq(rig.aim_blend, 1.0) + var aimed: Transform3D = rig.solve(feet, 0.0, 0.0, false, SimGround.flat()) + var pivot: Vector3 = CameraRig.pivot(feet, false) + assert_true(aimed.origin.distance_to(pivot) < hip.origin.distance_to(pivot)) + assert_eq(rig.fov(), CameraRig.AIM_FOV) + rig.update_aim(false, 0.05) + assert_true(rig.aim_blend > 0.0 and rig.aim_blend < 1.0, "aim should ease, not snap") + + +func test_wall_behind_pulls_the_camera_in() -> void: + var ground: SimGround = SimGround.flat() + ground.add_solid(AABB(Vector3(90.0, 0.0, 98.0), Vector3(20.0, 4.0, 1.0))) + var view: Transform3D = CameraRig.new().solve(Vector3(100.0, 0.0, 100.0), 0.0, 0.0, false, ground) + assert_true(view.origin.z > 99.0 + CameraRig.CLEARANCE - 0.001, "camera at z %f is in the wall" % view.origin.z) + + +func test_scripted_sweep_never_clips() -> void: + var checked: int = 0 + for arena: Array in _arenas(): + var ground: SimGround = arena[0] + var feet: Vector3 = arena[1] + for crouching: bool in [false, true]: + for blend: float in [0.0, 1.0]: + var rig: CameraRig = CameraRig.new() + rig.aim_blend = blend + for yaw_step: int in 72: + for pitch_step: int in 17: + var yaw: float = yaw_step * TAU / 72.0 + var pitch: float = deg_to_rad(-80.0 + pitch_step * 10.0) + var view: Transform3D = rig.solve(feet, yaw, pitch, crouching, ground) + var problem: String = _clipping(ground, view, rig.fov(), CameraRig.pivot(feet, crouching)) + checked += 1 + if not problem.is_empty(): + fail("%s at yaw %d° pitch %d° crouch %s aim %s: %s" % [ + arena[2], roundi(rad_to_deg(yaw)), roundi(rad_to_deg(pitch)), + crouching, blend, problem, + ]) + return + assert_eq(checked, 5 * 2 * 2 * 72 * 17) + + +## Returns why a view clips, or "" if it doesn't: the eye or a near-plane corner is +## inside geometry, or the line from the pivot to the eye passes through it. +func _clipping(ground: SimGround, view: Transform3D, fov_degrees: float, pivot: Vector3) -> String: + if ground.point_inside(view.origin): + return "eye inside geometry at %s" % view.origin + var half_height: float = CameraRig.NEAR * tan(deg_to_rad(fov_degrees) * 0.5) + var half_width: float = half_height * ASPECT + var centre: Vector3 = view.origin - view.basis.z * CameraRig.NEAR + for sx: float in [-1.0, 1.0]: + for sy: float in [-1.0, 1.0]: + var corner: Vector3 = centre + view.basis.x * half_width * sx + view.basis.y * half_height * sy + if ground.point_inside(corner): + return "near-plane corner inside geometry at %s" % corner + if ground.raycast(pivot, view.origin) < 1.0: + return "line of sight from the pivot is blocked" + return "" + + +## [ground, feet, name] for each place the sweep is run. +func _arenas() -> Array[Array]: + var arenas: Array[Array] = [] + + var wall: SimGround = SimGround.flat() + wall.add_solid(AABB(Vector3(90.0, 0.0, 100.0 + Tuning.PLAYER_RADIUS), Vector3(20.0, 4.0, 0.5))) + arenas.append([wall, Vector3(100.0, 0.0, 100.0), "against a wall"]) + + var corridor: SimGround = SimGround.flat() + corridor.add_solid(AABB(Vector3(98.0, 0.0, 80.0), Vector3(1.5, 4.0, 40.0))) + corridor.add_solid(AABB(Vector3(100.5, 0.0, 80.0), Vector3(1.5, 4.0, 40.0))) + arenas.append([corridor, Vector3(100.0, 0.0, 100.0), "in a narrow corridor"]) + + var roof: SimGround = SimGround.flat() + roof.add_solid(AABB(Vector3(90.0, 2.2, 90.0), Vector3(20.0, 0.3, 20.0))) + arenas.append([roof, Vector3(100.0, 0.0, 100.0), "under a low roof"]) + + var corner: SimGround = SimGround.flat() + corner.add_solid(AABB(Vector3(100.5, 0.0, 90.0), Vector3(3.0, 4.0, 20.0))) + corner.add_solid(AABB(Vector3(90.0, 0.0, 100.5), Vector3(20.0, 4.0, 3.0))) + arenas.append([corner, Vector3(100.0, 0.0, 100.0), "in a corner"]) + + var side: int = 64 + var values: PackedFloat32Array = PackedFloat32Array() + values.resize(side * side) + for z: int in side: + for x: int in side: + # A steep bank rising to the east, with ripples. + values[z * side + x] = maxf(float(x) - 30.0, 0.0) * 1.8 + 0.4 * sin(z * 0.9) + var hill: SimGround = SimGround.from_heights(values, side, 1.0) + var foot: Vector3 = Vector3(29.6, 0.0, 32.0) + foot.y = hill.terrain_height(foot.x, foot.z) + arenas.append([hill, foot, "at the foot of a steep bank"]) + return arenas diff --git a/test/client/test_camera_rig.gd.uid b/test/client/test_camera_rig.gd.uid new file mode 100644 index 0000000..630d570 --- /dev/null +++ b/test/client/test_camera_rig.gd.uid @@ -0,0 +1 @@ +uid://dsempouxfbdom diff --git a/test/client/test_input_settings.gd b/test/client/test_input_settings.gd new file mode 100644 index 0000000..c515b69 --- /dev/null +++ b/test/client/test_input_settings.gd @@ -0,0 +1,68 @@ +extends TestCase + +const PATH: String = "user://test_input_settings.cfg" + + +func after_each() -> void: + if FileAccess.file_exists(PATH): + DirAccess.remove_absolute(ProjectSettings.globalize_path(PATH)) + + +func test_sensitivity_scales_turning() -> void: + var settings: InputSettings = InputSettings.new() + var slow: Vector2 = settings.apply_mouse(Vector2(100.0, 0.0), 1.0, 0.0, false) + settings.mouse_sensitivity = 2.0 + var fast: Vector2 = settings.apply_mouse(Vector2(100.0, 0.0), 1.0, 0.0, false) + assert_near(1.0 - slow.x, 100.0 * InputSettings.RADIANS_PER_PIXEL, 0.00001) + assert_near(1.0 - fast.x, 2.0 * (1.0 - slow.x), 0.00001) + + +func test_mouse_right_turns_right_and_up_looks_up() -> void: + var settings: InputSettings = InputSettings.new() + var look: Vector2 = settings.apply_mouse(Vector2(10.0, -10.0), 1.0, 0.0, false) + assert_true(look.x < 1.0, "turning right decreases yaw") + assert_true(look.y > 0.0, "mouse up looks up") + settings.invert_y = true + assert_true(settings.apply_mouse(Vector2(0.0, -10.0), 1.0, 0.0, false).y < 0.0) + + +func test_aiming_slows_the_mouse() -> void: + var settings: InputSettings = InputSettings.new() + var hip: Vector2 = settings.apply_mouse(Vector2(50.0, 0.0), 1.0, 0.0, false) + var aimed: Vector2 = settings.apply_mouse(Vector2(50.0, 0.0), 1.0, 0.0, true) + assert_near(1.0 - aimed.x, (1.0 - hip.x) * settings.aim_sensitivity_scale, 0.00001) + + +func test_pitch_is_clamped_and_yaw_wraps() -> void: + var settings: InputSettings = InputSettings.new() + var look: Vector2 = settings.apply_mouse(Vector2(-10.0, -100000.0), TAU - 0.001, 0.0, false) + assert_near(look.y, InputSettings.PITCH_LIMIT, 0.00001) + assert_true(look.x >= 0.0 and look.x < TAU) + + +func test_sensitivity_is_clamped() -> void: + var settings: InputSettings = InputSettings.new() + settings.mouse_sensitivity = 0.0 + assert_eq(settings.mouse_sensitivity, InputSettings.MIN_SENSITIVITY) + settings.mouse_sensitivity = 1000.0 + assert_eq(settings.mouse_sensitivity, InputSettings.MAX_SENSITIVITY) + + +func test_settings_round_trip_through_the_config_file() -> void: + var settings: InputSettings = InputSettings.new() + settings.mouse_sensitivity = 2.75 + settings.aim_sensitivity_scale = 0.4 + settings.invert_y = true + assert_eq(settings.save(PATH), OK) + var loaded: InputSettings = InputSettings.load_from(PATH) + assert_near(loaded.mouse_sensitivity, 2.75, 0.0001) + assert_near(loaded.aim_sensitivity_scale, 0.4, 0.0001) + assert_true(loaded.invert_y) + + +func test_missing_or_bad_file_gives_defaults() -> void: + assert_eq(InputSettings.load_from("user://does_not_exist.cfg").mouse_sensitivity, 1.0) + var config: ConfigFile = ConfigFile.new() + config.set_value(InputSettings.SECTION, "mouse_sensitivity", "fast") + config.save(PATH) + assert_eq(InputSettings.load_from(PATH).mouse_sensitivity, 1.0) diff --git a/test/client/test_input_settings.gd.uid b/test/client/test_input_settings.gd.uid new file mode 100644 index 0000000..3a683f2 --- /dev/null +++ b/test/client/test_input_settings.gd.uid @@ -0,0 +1 @@ +uid://c517c7silhnhv diff --git a/test/sim/test_movement.gd b/test/sim/test_movement.gd new file mode 100644 index 0000000..8fa45e9 --- /dev/null +++ b/test/sim/test_movement.gd @@ -0,0 +1,275 @@ +extends TestCase + +## Yaw that faces +x, since forward is (sin yaw, 0, cos yaw). +const EAST: float = PI * 0.5 +const NORTH: float = 0.0 + +var _tick: int = 0 + + +func before_each() -> void: + _tick = 0 + + +func _player(at: Vector3, on_ground: bool = true) -> SimEntity: + var player: SimEntity = SimEntity.new(1, SimEntity.ARCHETYPE_PLAYER) + player.position = at + player.health = Tuning.PLAYER_MAX_HEALTH + player.on_ground = on_ground + return player + + +func _command(move: Vector2, yaw: float, buttons: int = 0) -> PlayerCommand: + var command: PlayerCommand = PlayerCommand.from_input(_tick, move, yaw, 0.0, buttons) + _tick += 1 + return command + + +func _run(player: SimEntity, ground: SimGround, ticks: int, move: Vector2, yaw: float, buttons: int = 0) -> void: + for _index: int in ticks: + Movement.step(player, _command(move, yaw, buttons), ground) + + +## A heightfield that is flat up to x = `start`, then rises at `degrees` along +x. +func _ramp(degrees: float, start: float = 10.0) -> SimGround: + var side: int = 64 + var values: PackedFloat32Array = PackedFloat32Array() + values.resize(side * side) + var rise: float = tan(deg_to_rad(degrees)) + for z: int in side: + for x: int in side: + values[z * side + x] = maxf(float(x) - start, 0.0) * rise + return SimGround.from_heights(values, side, 1.0) + + +func test_walk_sprint_and_crouch_speeds() -> void: + var ground: SimGround = SimGround.flat() + var cases: Array[Array] = [ + [0, Tuning.WALK_SPEED], + [PlayerCommand.BUTTON_SPRINT, Tuning.SPRINT_SPEED], + [PlayerCommand.BUTTON_CROUCH, Tuning.CROUCH_SPEED], + [PlayerCommand.BUTTON_CROUCH | PlayerCommand.BUTTON_SPRINT, Tuning.CROUCH_SPEED], + ] + for entry: Array in cases: + var player: SimEntity = _player(Vector3(100.0, 0.0, 100.0)) + _run(player, ground, Tuning.TICK_RATE_HZ, Vector2(0.0, 1.0), EAST, entry[0]) + assert_near(player.position.x - 100.0, entry[1], 0.01, "buttons %d" % entry[0]) + assert_near(player.position.z, 100.0, 0.001) + assert_eq(player.position.y, 0.0) + + +func test_sprint_only_applies_moving_forward() -> void: + var player: SimEntity = _player(Vector3(100.0, 0.0, 100.0)) + _run(player, SimGround.flat(), Tuning.TICK_RATE_HZ, Vector2(1.0, 0.0), NORTH, PlayerCommand.BUTTON_SPRINT) + assert_near(player.position.distance_to(Vector3(100.0, 0.0, 100.0)), Tuning.WALK_SPEED, 0.01) + + +func test_diagonal_is_no_faster_than_straight() -> void: + var player: SimEntity = _player(Vector3(100.0, 0.0, 100.0)) + _run(player, SimGround.flat(), Tuning.TICK_RATE_HZ, Vector2(1.0, 1.0), NORTH) + assert_near(player.position.distance_to(Vector3(100.0, 0.0, 100.0)), Tuning.WALK_SPEED, 0.05) + + +func test_jump_height_and_airtime() -> void: + var player: SimEntity = _player(Vector3(100.0, 0.0, 100.0)) + var ground: SimGround = SimGround.flat() + Movement.step(player, _command(Vector2.ZERO, NORTH, PlayerCommand.BUTTON_JUMP), ground) + assert_false(player.on_ground) + var peak: float = player.position.y + var ticks_in_air: int = 1 + while not player.on_ground and ticks_in_air < 200: + Movement.step(player, _command(Vector2.ZERO, NORTH), ground) + peak = maxf(peak, player.position.y) + ticks_in_air += 1 + var expected_peak: float = Tuning.JUMP_VELOCITY * Tuning.JUMP_VELOCITY / (2.0 * Tuning.GRAVITY) + assert_near(peak, expected_peak, 0.01, "jump height") + var expected_ticks: float = 2.0 * Tuning.JUMP_VELOCITY / Tuning.GRAVITY * Tuning.TICK_RATE_HZ + assert_near(ticks_in_air, expected_ticks, 1.5, "time in the air") + assert_eq(player.position.y, 0.0) + assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH, "a jump on flat ground must not hurt") + + +func test_no_jump_while_crouched_or_in_the_air() -> void: + var ground: SimGround = SimGround.flat() + var crouched: SimEntity = _player(Vector3(100.0, 0.0, 100.0)) + Movement.step(crouched, _command(Vector2.ZERO, NORTH, PlayerCommand.BUTTON_JUMP | PlayerCommand.BUTTON_CROUCH), ground) + assert_true(crouched.on_ground) + assert_eq(crouched.position.y, 0.0) + + var jumper: SimEntity = _player(Vector3(100.0, 0.0, 100.0)) + Movement.step(jumper, _command(Vector2.ZERO, NORTH, PlayerCommand.BUTTON_JUMP), ground) + var rising: float = jumper.velocity.y + Movement.step(jumper, _command(Vector2.ZERO, NORTH, PlayerCommand.BUTTON_JUMP), ground) + assert_true(jumper.velocity.y < rising, "a second jump in the air must not add speed") + + +func test_without_a_command_an_airborne_player_still_falls() -> void: + var player: SimEntity = _player(Vector3(100.0, 3.0, 100.0), false) + for _index: int in 30: + Movement.step(player, null, SimGround.flat()) + assert_true(player.on_ground) + assert_eq(player.position.y, 0.0) + + +func test_walks_up_a_gentle_slope() -> void: + var player: SimEntity = _player(Vector3(5.0, 0.0, 20.0)) + _run(player, _ramp(30.0), Tuning.TICK_RATE_HZ * 5, Vector2(0.0, 1.0), EAST) + assert_true(player.position.x > 20.0, "should have climbed well up the slope, at x %f" % player.position.x) + assert_near(player.position.y, (player.position.x - 10.0) * tan(deg_to_rad(30.0)), 0.05) + assert_true(player.on_ground) + + +func test_walks_down_a_gentle_slope_without_leaving_the_ground() -> void: + var player: SimEntity = _player(Vector3(30.0, 20.0 * tan(deg_to_rad(30.0)), 20.0)) + for _index: int in Tuning.TICK_RATE_HZ * 4: + Movement.step(player, _command(Vector2(0.0, 1.0), -EAST), _ramp(30.0)) + assert_true(player.on_ground, "lost the ground at x %f" % player.position.x) + assert_true(player.position.x < 15.0) + + +func test_too_steep_a_slope_blocks_walking_up() -> void: + var player: SimEntity = _player(Vector3(5.0, 0.0, 20.0)) + _run(player, _ramp(60.0), Tuning.TICK_RATE_HZ * 5, Vector2(0.0, 1.0), EAST, PlayerCommand.BUTTON_SPRINT) + assert_true(player.position.x < 11.0, "climbed a 60 degree slope to x %f" % player.position.x) + assert_true(player.position.y < 1.0) + + +func test_cannot_climb_a_steep_slope_by_jumping_into_it() -> void: + var player: SimEntity = _player(Vector3(5.0, 0.0, 20.0)) + var ground: SimGround = _ramp(60.0) + for _index: int in Tuning.TICK_RATE_HZ * 6: + Movement.step(player, _command(Vector2(0.0, 1.0), EAST, PlayerCommand.BUTTON_JUMP), ground) + assert_true(player.position.y < 3.0, "jumped up a cliff to height %f" % player.position.y) + + +func test_slides_down_ground_too_steep_to_stand_on() -> void: + var ground: SimGround = _ramp(60.0) + var x: float = 20.0 + var player: SimEntity = _player(Vector3(x, ground.terrain_height(x, 20.0), 20.0)) + for _index: int in Tuning.TICK_RATE_HZ * 5: + Movement.step(player, null, ground) + assert_true(player.position.x < 10.5, "should have slid to the foot, at x %f" % player.position.x) + assert_true(player.on_ground) + assert_eq(player.health, Tuning.PLAYER_MAX_HEALTH, "a capped slide must not hurt") + + +func test_steps_up_low_ledges_but_not_high_ones() -> void: + var low: SimGround = SimGround.flat() + low.add_solid(AABB(Vector3(10.0, 0.0, 0.0), Vector3(4.0, Tuning.STEP_HEIGHT - 0.05, 40.0))) + var player: SimEntity = _player(Vector3(5.0, 0.0, 20.0)) + _run(player, low, Tuning.TICK_RATE_HZ * 2, Vector2(0.0, 1.0), EAST) + assert_true(player.position.x > 10.0) + assert_near(player.position.y, Tuning.STEP_HEIGHT - 0.05, 0.001) + + var high: SimGround = SimGround.flat() + high.add_solid(AABB(Vector3(10.0, 0.0, 0.0), Vector3(4.0, 1.0, 40.0))) + var blocked: SimEntity = _player(Vector3(5.0, 0.0, 20.0)) + _run(blocked, high, Tuning.TICK_RATE_HZ * 2, Vector2(0.0, 1.0), EAST) + assert_near(blocked.position.x, 10.0 - Tuning.PLAYER_RADIUS, 0.15) + assert_eq(blocked.position.y, 0.0) + + +func test_jumps_onto_a_ledge_too_high_to_step() -> void: + var ground: SimGround = SimGround.flat() + ground.add_solid(AABB(Vector3(10.0, 0.0, 0.0), Vector3(4.0, 1.0, 40.0))) + var player: SimEntity = _player(Vector3(8.0, 0.0, 20.0)) + _run(player, ground, 1, Vector2(0.0, 1.0), EAST, PlayerCommand.BUTTON_JUMP) + _run(player, ground, Tuning.TICK_RATE_HZ, Vector2(0.0, 1.0), EAST) + assert_true(player.position.x > 10.0) + assert_near(player.position.y, 1.0, 0.001) + + +func test_slides_along_a_wall() -> void: + var ground: SimGround = SimGround.flat() + ground.add_solid(AABB(Vector3(10.0, 0.0, 0.0), Vector3(1.0, 3.0, 100.0))) + var player: SimEntity = _player(Vector3(9.0, 0.0, 20.0)) + # North-east, into the wall: x stops, z keeps going. + _run(player, ground, Tuning.TICK_RATE_HZ, Vector2(0.0, 1.0), PI * 0.25) + assert_true(player.position.x <= 10.0 - Tuning.PLAYER_RADIUS + 0.001) + assert_true(player.position.z > 22.0) + + +func test_ceiling_stops_a_jump() -> void: + var ground: SimGround = SimGround.flat() + ground.add_solid(AABB(Vector3(95.0, 2.2, 95.0), Vector3(10.0, 0.5, 10.0))) + var player: SimEntity = _player(Vector3(100.0, 0.0, 100.0)) + var top: float = 0.0 + for _index: int in 40: + Movement.step(player, _command(Vector2.ZERO, NORTH, PlayerCommand.BUTTON_JUMP), ground) + top = maxf(top, player.position.y + Tuning.PLAYER_HEIGHT) + assert_true(top <= 2.2 + 0.001, "head reached %f, through the ceiling" % top) + + +func test_crouching_fits_under_a_low_gap() -> void: + var ground: SimGround = SimGround.flat() + ground.add_solid(AABB(Vector3(10.0, 1.5, 0.0), Vector3(4.0, 1.0, 40.0))) + var standing: SimEntity = _player(Vector3(5.0, 0.0, 20.0)) + _run(standing, ground, Tuning.TICK_RATE_HZ * 3, Vector2(0.0, 1.0), EAST) + assert_true(standing.position.x < 10.0) + var crouched: SimEntity = _player(Vector3(5.0, 0.0, 20.0)) + _run(crouched, ground, Tuning.TICK_RATE_HZ * 5, Vector2(0.0, 1.0), EAST, PlayerCommand.BUTTON_CROUCH) + assert_true(crouched.position.x > 14.0) + + +func test_fall_damage() -> void: + var cases: Array[Array] = [[3.0, 0], [6.0, 0], [10.0, 32], [16.0, 80]] + for entry: Array in cases: + var player: SimEntity = _player(Vector3(100.0, entry[0], 100.0), false) + for _index: int in 90: + Movement.step(player, null, SimGround.flat()) + var damage: int = Tuning.PLAYER_MAX_HEALTH - player.health + assert_near(damage, entry[1], 3.0, "drop of %f m" % entry[0]) + assert_true(player.on_ground) + + +func test_fall_damage_never_goes_below_zero_health() -> void: + var player: SimEntity = _player(Vector3(100.0, 80.0, 100.0), false) + for _index: int in 200: + Movement.step(player, null, SimGround.flat()) + assert_eq(player.health, 0) + + +func test_stands_on_a_solid_and_falls_off_its_edge() -> void: + var ground: SimGround = SimGround.flat() + ground.add_solid(AABB(Vector3(0.0, 0.0, 0.0), Vector3(10.0, 3.0, 40.0))) + var player: SimEntity = _player(Vector3(5.0, 3.0, 20.0)) + _run(player, ground, Tuning.TICK_RATE_HZ * 3, Vector2(0.0, 1.0), EAST) + assert_true(player.position.x > 10.0) + assert_eq(player.position.y, 0.0) + assert_true(player.on_ground) + + +func test_same_commands_on_rough_ground_are_deterministic() -> void: + var first: Simulation = _rough_simulation() + var second: Simulation = _rough_simulation() + var random: RandomNumberGenerator = RandomNumberGenerator.new() + random.seed = 77 + for at_tick: int in 1500: + for id: int in [1, 2, 3, 4]: + var command: PlayerCommand = PlayerCommand.new() + command.tick = at_tick + command.move_x = random.randi_range(-127, 127) + command.move_y = random.randi_range(-127, 127) + command.yaw = random.randi_range(0, 65535) + command.buttons = random.randi_range(0, 31) + first.submit_command(id, command) + second.submit_command(id, command) + first.step() + second.step() + assert_eq(first.state_hash(), second.state_hash()) + + +func _rough_simulation() -> Simulation: + var sim: Simulation = Simulation.new(4242) + var side: int = 65 + var values: PackedFloat32Array = PackedFloat32Array() + values.resize(side * side) + for z: int in side: + for x: int in side: + values[z * side + x] = 6.0 * sin(x * 0.31) * cos(z * 0.23) + 0.08 * x + sim.ground = SimGround.from_heights(values, side, 16.0) + for index: int in 12: + sim.ground.add_solid(AABB(Vector3(index * 70.0, 0.0, 500.0), Vector3(4.0, 3.0 + index, 4.0))) + for _index: int in 4: + sim.add_player() + return sim diff --git a/test/sim/test_movement.gd.uid b/test/sim/test_movement.gd.uid new file mode 100644 index 0000000..4b51991 --- /dev/null +++ b/test/sim/test_movement.gd.uid @@ -0,0 +1 @@ +uid://p72o0ughjt1a diff --git a/test/sim/test_sim_ground.gd b/test/sim/test_sim_ground.gd new file mode 100644 index 0000000..80fbeb4 --- /dev/null +++ b/test/sim/test_sim_ground.gd @@ -0,0 +1,85 @@ +extends TestCase + + +func _tilted() -> SimGround: + # h = 0.5 * x, a plane rising along +x. + var values: PackedFloat32Array = PackedFloat32Array() + for z: int in 4: + for x: int in 4: + values.append(0.5 * x * 2.0) + return SimGround.from_heights(values, 4, 2.0) + + +func test_flat_ground() -> void: + var ground: SimGround = SimGround.flat(3.0) + assert_eq(ground.terrain_height(10.0, 900.0), 3.0) + assert_true(ground.terrain_normal(10.0, 10.0).is_equal_approx(Vector3.UP)) + assert_true(ground.walkable_terrain(10.0, 10.0)) + + +func test_interpolation_and_edges() -> void: + var ground: SimGround = _tilted() + assert_near(ground.terrain_height(1.0, 1.0), 0.5, 0.0001) + assert_near(ground.terrain_height(3.0, 5.0), 1.5, 0.0001) + # Outside the grid, the edge sample is used. + assert_near(ground.terrain_height(-5.0, 2.0), 0.0, 0.0001) + assert_near(ground.terrain_height(100.0, 2.0), 3.0, 0.0001) + + +func test_normal_and_walkability() -> void: + var ground: SimGround = _tilted() + var normal: Vector3 = ground.terrain_normal(3.0, 3.0) + assert_near(rad_to_deg(normal.angle_to(Vector3.UP)), rad_to_deg(atan(0.5)), 0.01) + assert_true(normal.x < 0.0, "the normal leans away from the rise") + assert_true(ground.walkable_terrain(3.0, 3.0)) + + +func test_support_from_solids() -> void: + var ground: SimGround = SimGround.flat() + ground.add_solid(AABB(Vector3(0.0, 0.0, 0.0), Vector3(2.0, 1.0, 2.0))) + ground.add_solid(AABB(Vector3(0.0, 5.0, 0.0), Vector3(2.0, 1.0, 2.0))) + assert_eq(ground.support_height(1.0, 1.0, 1.2), 1.0) + assert_eq(ground.support_height(1.0, 1.0, 10.0), 6.0) + assert_eq(ground.support_height(1.0, 1.0, 0.5), 0.0) + assert_eq(ground.support_height(3.0, 1.0, 10.0), 0.0) + assert_true(ground.support_is_solid(1.0, 1.0, 1.2)) + assert_false(ground.support_is_solid(3.0, 1.0, 1.2)) + + +func test_negative_size_solids_are_normalised() -> void: + var ground: SimGround = SimGround.flat() + ground.add_solid(AABB(Vector3(2.0, 1.0, 2.0), Vector3(-2.0, -1.0, -2.0))) + assert_eq(ground.support_height(1.0, 1.0, 5.0), 1.0) + + +func test_raycast_against_a_box() -> void: + var ground: SimGround = SimGround.flat(-100.0) + ground.add_solid(AABB(Vector3(4.0, -1.0, -1.0), Vector3(2.0, 2.0, 2.0))) + assert_near(ground.raycast(Vector3.ZERO, Vector3(10.0, 0.0, 0.0)), 0.4, 0.0001) + assert_eq(ground.raycast(Vector3.ZERO, Vector3(3.0, 0.0, 0.0)), 1.0) + assert_eq(ground.raycast(Vector3(0.0, 5.0, 0.0), Vector3(10.0, 5.0, 0.0)), 1.0) + assert_eq(ground.raycast(Vector3(5.0, 0.0, 0.0), Vector3(10.0, 0.0, 0.0)), 0.0, "starting inside") + + +func test_raycast_against_terrain() -> void: + var ground: SimGround = SimGround.flat(0.0) + var hit: float = ground.raycast(Vector3(0.0, 2.0, 0.0), Vector3(0.0, -2.0, 0.0)) + assert_near(hit, 0.5, 0.001) + assert_eq(ground.raycast(Vector3(0.0, 2.0, 0.0), Vector3(5.0, 1.0, 0.0)), 1.0) + assert_eq(ground.raycast(Vector3(0.0, -1.0, 0.0), Vector3(0.0, 3.0, 0.0)), 0.0, "starting below") + # A ray grazing up a slope finds where it enters. + var tilted: SimGround = _tilted() + var slope_hit: float = tilted.raycast(Vector3(0.0, 2.0, 2.0), Vector3(6.0, 2.0, 2.0)) + assert_near(slope_hit * 6.0, 4.0, 0.01) + + +func test_body_blocked_respects_step_and_height() -> void: + var ground: SimGround = SimGround.flat() + ground.add_solid(AABB(Vector3(0.0, 0.0, 0.0), Vector3(2.0, 0.4, 2.0))) + ground.add_solid(AABB(Vector3(10.0, 2.0, 0.0), Vector3(2.0, 1.0, 2.0))) + assert_false(ground.body_blocked(Vector3(1.0, 0.0, 1.0), 0.4, 1.8, 0.45), "low step") + assert_true(ground.body_blocked(Vector3(1.0, 0.0, 1.0), 0.4, 1.8, 0.0)) + assert_true(ground.body_blocked(Vector3(2.3, 0.0, 1.0), 0.4, 1.8, 0.0), "radius overlaps") + assert_false(ground.body_blocked(Vector3(2.5, 0.0, 1.0), 0.4, 1.8, 0.0)) + assert_false(ground.body_blocked(Vector3(11.0, 0.0, 1.0), 0.4, 1.8, 0.0), "fits under") + assert_true(ground.body_blocked(Vector3(11.0, 0.0, 1.0), 0.4, 2.5, 0.0)) diff --git a/test/sim/test_sim_ground.gd.uid b/test/sim/test_sim_ground.gd.uid new file mode 100644 index 0000000..5ea6356 --- /dev/null +++ b/test/sim/test_sim_ground.gd.uid @@ -0,0 +1 @@ +uid://b2yuyo1vyam3j diff --git a/test/sim/test_simulation.gd b/test/sim/test_simulation.gd index bbd8af8..81cb743 100644 --- a/test/sim/test_simulation.gd +++ b/test/sim/test_simulation.gd @@ -21,8 +21,9 @@ func test_a_different_seed_changes_the_hash() -> void: func test_a_single_changed_command_changes_the_hash() -> void: var log_bytes: PackedByteArray = _build_command_log(SEED, 4, 200) var changed: PackedByteArray = log_bytes.duplicate() - # Flip one bit of move_x in the middle of the log. - changed[changed.size() / 2 / PlayerCommand.ENCODED_SIZE * PlayerCommand.ENCODED_SIZE + 4] ^= 1 + # Flip a high bit of move_x in the middle of the log. The lowest bit is a 1/127 nudge + # of the stick, which moves an airborne player less than the hash's millimetre. + changed[changed.size() / 2 / PlayerCommand.ENCODED_SIZE * PlayerCommand.ENCODED_SIZE + 4] ^= 0x40 assert_ne(_replay(SEED, log_bytes, 4), _replay(SEED, changed, 4))