Island.generate(seed) builds a 513 x 513 heightfield (2 m spacing, the whole 1,024 m world) from the match seed's "worldgen" stream: a warped coastline, a coastal shelf and interior dome, two to four summits joined by ridges, rolling and rocky detail, and flattened marsh in wet lowland. Land is scaled so the tallest summit lands between ISLAND_PEAK_MIN and ISLAND_PEAK_MAX, under the last tide. Each sample gets an elevation band (sea, beach, marsh, forest, highland, summit) for terrain materials. - Simulation generates the island unless a test supplies its own ground, and spawns players on dry land until the drop arrives. - The headless server logs world_hash in its start line. - Island.heightmap_hash() is SHA-256 over millimetre heights and bands. - tools/island_map.gd draws a top-down map with tide contours. - New island constants in tuning.gd; GDD gains an island section. Tests: a 100-seed sweep checks generation time (slowest 617 ms against 5 s), land area, at least two distinct high points, walkable land, bands, and that no more than 3% of dry land is cut off at sea levels 0 and 8 m (worst seen 0.87%). A separate server process given only the seed must log the same heightmap hash. Closes #9 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
276 lines
11 KiB
GDScript
276 lines
11 KiB
GDScript
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, SimGround.flat())
|
|
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
|