F5: Simulation core and player command stream
Add the fixed-tick Simulation, the EntityRegistry with never-reused IDs, SimRng's named seeded streams, and the 14-byte PlayerCommand used by humans, bots and the network. state_hash is a SHA-256 over a canonical encoding of the state. Movement is a flat placeholder until the v0.2 character controller. Tests replay a 24-player command log over 10,000 ticks and scan src/sim for unseeded randomness and presentation code. Closes #5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,9 +6,11 @@ const FORBIDDEN: Array[String] = [
|
||||
"Node2D", "Node3D", "MeshInstance3D", "Camera3D", "Control", "CanvasItem", "Viewport",
|
||||
"SceneTree", "get_tree", "Input.",
|
||||
]
|
||||
const NONDETERMINISTIC: Array[String] = [
|
||||
"randf(", "randi(", "randf_range(", "randi_range(", "randomize(", "Time.", "OS.get_ticks",
|
||||
]
|
||||
const NONDETERMINISTIC: Array[String] = ["Time.", "OS.get_ticks"]
|
||||
## Global random calls. A method call on a seeded stream (stream.randf()) is allowed.
|
||||
const GLOBAL_RANDOM: String = "(?<![\\w.])(randf|randi|randf_range|randi_range|randfn|randomize|rand_from_seed)\\("
|
||||
## The one script allowed to create a generator, because it seeds them.
|
||||
const RNG_OWNER: String = "res://src/sim/sim_rng.gd"
|
||||
|
||||
|
||||
func test_simulation_has_no_presentation_dependency() -> void:
|
||||
@@ -19,16 +21,51 @@ func test_simulation_has_no_unseeded_randomness() -> void:
|
||||
_scan(NONDETERMINISTIC)
|
||||
|
||||
|
||||
func test_simulation_makes_no_global_random_calls() -> void:
|
||||
for path: String in _scripts_under("res://src/sim"):
|
||||
for line: String in _code_lines(path):
|
||||
if _global_random().search(line) != null:
|
||||
fail("%s makes a global random call: %s" % [path, line.strip_edges()])
|
||||
|
||||
|
||||
func test_only_sim_rng_creates_generators() -> void:
|
||||
for path: String in _scripts_under("res://src/sim"):
|
||||
if path == RNG_OWNER:
|
||||
continue
|
||||
for line: String in _code_lines(path):
|
||||
if line.contains("RandomNumberGenerator.new"):
|
||||
fail("%s creates its own generator: %s" % [path, line.strip_edges()])
|
||||
|
||||
|
||||
func test_scanner_tells_global_calls_from_seeded_ones() -> void:
|
||||
var pattern: RegEx = _global_random()
|
||||
assert_not_null(pattern.search("var x: float = randf()"), "global call must match")
|
||||
assert_not_null(pattern.search("foo(randi_range(1, 2))"), "nested global call must match")
|
||||
assert_null(pattern.search("var x: float = stream.randf()"), "seeded call must not match")
|
||||
assert_null(pattern.search("my_randf(1)"), "other identifiers must not match")
|
||||
|
||||
|
||||
func _global_random() -> RegEx:
|
||||
var pattern: RegEx = RegEx.new()
|
||||
pattern.compile(GLOBAL_RANDOM)
|
||||
return pattern
|
||||
|
||||
|
||||
func _code_lines(path: String) -> Array[String]:
|
||||
var lines: Array[String] = []
|
||||
for line: String in FileAccess.get_file_as_string(path).split("\n"):
|
||||
lines.append(line.get_slice("#", 0))
|
||||
return lines
|
||||
|
||||
|
||||
func _scan(needles: Array[String]) -> void:
|
||||
var paths: Array[String] = _scripts_under("res://src/sim")
|
||||
assert_true(not paths.is_empty(), "expected scripts in src/sim")
|
||||
for path: String in paths:
|
||||
var source: String = FileAccess.get_file_as_string(path)
|
||||
for needle: String in needles:
|
||||
# Comments may mention these; only code counts.
|
||||
for line: String in source.split("\n"):
|
||||
var code: String = line.get_slice("#", 0)
|
||||
if code.contains(needle):
|
||||
for line: String in _code_lines(path):
|
||||
if line.contains(needle):
|
||||
fail("%s uses %s: %s" % [path, needle, line.strip_edges()])
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
extends TestCase
|
||||
|
||||
|
||||
func test_ids_start_at_one_and_none_is_never_used() -> void:
|
||||
var registry: EntityRegistry = EntityRegistry.new()
|
||||
assert_eq(registry.create(1).id, 1)
|
||||
assert_eq(EntityRegistry.NONE, 0)
|
||||
assert_null(registry.find(EntityRegistry.NONE))
|
||||
|
||||
|
||||
func test_ids_are_never_reused() -> void:
|
||||
var registry: EntityRegistry = EntityRegistry.new()
|
||||
var first: int = registry.create(1).id
|
||||
assert_true(registry.remove(first))
|
||||
assert_false(registry.contains(first))
|
||||
assert_ne(registry.create(1).id, first)
|
||||
|
||||
|
||||
func test_iteration_is_in_ascending_id_order_after_removals() -> void:
|
||||
var registry: EntityRegistry = EntityRegistry.new()
|
||||
for _index: int in 5:
|
||||
registry.create(1)
|
||||
registry.remove(2)
|
||||
registry.create(1)
|
||||
var ids: Array[int] = []
|
||||
for entity: SimEntity in registry.entities():
|
||||
ids.append(entity.id)
|
||||
assert_eq(ids, [1, 3, 4, 5, 6] as Array[int])
|
||||
|
||||
|
||||
func test_removing_while_iterating_is_safe() -> void:
|
||||
var registry: EntityRegistry = EntityRegistry.new()
|
||||
for _index: int in 4:
|
||||
registry.create(1)
|
||||
for entity: SimEntity in registry.entities():
|
||||
registry.remove(entity.id)
|
||||
assert_eq(registry.count(), 0)
|
||||
@@ -0,0 +1 @@
|
||||
uid://boob5j7i4dnrr
|
||||
@@ -0,0 +1,74 @@
|
||||
extends TestCase
|
||||
|
||||
|
||||
func test_round_trip_preserves_every_field() -> void:
|
||||
var command: PlayerCommand = PlayerCommand.new()
|
||||
command.tick = 4_000_000_000
|
||||
command.move_x = -127
|
||||
command.move_y = 90
|
||||
command.yaw = 65535
|
||||
command.pitch = -32767
|
||||
command.buttons = PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_BUILD | 0x8000
|
||||
command.selection = 200
|
||||
command.material = 3
|
||||
|
||||
var decoded: PlayerCommand = PlayerCommand.decode(command.encode())
|
||||
assert_not_null(decoded)
|
||||
assert_eq(decoded.tick, 4_000_000_000)
|
||||
assert_eq(decoded.move_x, -127)
|
||||
assert_eq(decoded.move_y, 90)
|
||||
assert_eq(decoded.yaw, 65535)
|
||||
assert_eq(decoded.pitch, -32767)
|
||||
assert_eq(decoded.buttons, command.buttons)
|
||||
assert_eq(decoded.selection, 200)
|
||||
assert_eq(decoded.material, 3)
|
||||
|
||||
|
||||
func test_encoding_is_compact_and_fixed_size() -> void:
|
||||
assert_eq(PlayerCommand.new().encode().size(), PlayerCommand.ENCODED_SIZE)
|
||||
assert_eq(PlayerCommand.ENCODED_SIZE, 14)
|
||||
|
||||
|
||||
func test_decode_at_an_offset() -> void:
|
||||
var first: PlayerCommand = PlayerCommand.new()
|
||||
first.tick = 7
|
||||
var second: PlayerCommand = PlayerCommand.new()
|
||||
second.tick = 8
|
||||
var stream: PackedByteArray = first.encode() + second.encode()
|
||||
assert_eq(PlayerCommand.decode(stream, PlayerCommand.ENCODED_SIZE).tick, 8)
|
||||
|
||||
|
||||
func test_truncated_buffer_returns_null_without_error() -> void:
|
||||
var bytes: PackedByteArray = PlayerCommand.new().encode()
|
||||
bytes.resize(PlayerCommand.ENCODED_SIZE - 1)
|
||||
assert_null(PlayerCommand.decode(bytes))
|
||||
assert_null(PlayerCommand.decode(PackedByteArray()))
|
||||
assert_null(PlayerCommand.decode(PlayerCommand.new().encode(), 1))
|
||||
assert_null(PlayerCommand.decode(PlayerCommand.new().encode(), -1))
|
||||
|
||||
|
||||
func test_hostile_wire_values_are_clamped() -> void:
|
||||
var bytes: PackedByteArray = PlayerCommand.new().encode()
|
||||
bytes.encode_s8(4, -128)
|
||||
bytes.encode_s16(8, -32768)
|
||||
var decoded: PlayerCommand = PlayerCommand.decode(bytes)
|
||||
assert_eq(decoded.move_x, -PlayerCommand.AXIS_MAX)
|
||||
assert_eq(decoded.pitch, -PlayerCommand.PITCH_MAX)
|
||||
|
||||
|
||||
func test_from_input_clamps_and_wraps() -> void:
|
||||
var command: PlayerCommand = PlayerCommand.from_input(1, Vector2(5.0, -5.0), -PI * 0.5, 9.0)
|
||||
assert_eq(command.move_x, PlayerCommand.AXIS_MAX)
|
||||
assert_eq(command.move_y, -PlayerCommand.AXIS_MAX)
|
||||
assert_eq(command.yaw, PlayerCommand.YAW_STEPS * 3 / 4)
|
||||
assert_eq(command.pitch, PlayerCommand.PITCH_MAX)
|
||||
|
||||
|
||||
func test_input_helpers_read_back() -> void:
|
||||
var command: PlayerCommand = PlayerCommand.from_input(
|
||||
1, Vector2(0.5, 0.0), PI, 0.0, PlayerCommand.BUTTON_JUMP
|
||||
)
|
||||
assert_near(command.yaw_radians(), PI, 0.001)
|
||||
assert_near(command.move_vector().x, 0.5, 0.01)
|
||||
assert_true(command.is_pressed(PlayerCommand.BUTTON_JUMP))
|
||||
assert_false(command.is_pressed(PlayerCommand.BUTTON_FIRE))
|
||||
@@ -0,0 +1 @@
|
||||
uid://w38lfvpvwmym
|
||||
@@ -0,0 +1,35 @@
|
||||
extends TestCase
|
||||
|
||||
|
||||
func test_same_seed_and_name_give_the_same_rolls() -> void:
|
||||
assert_eq(_rolls(SimRng.new(42), "loot"), _rolls(SimRng.new(42), "loot"))
|
||||
|
||||
|
||||
func test_different_seeds_give_different_rolls() -> void:
|
||||
assert_ne(_rolls(SimRng.new(42), "loot"), _rolls(SimRng.new(43), "loot"))
|
||||
|
||||
|
||||
func test_different_streams_give_different_rolls() -> void:
|
||||
var rng: SimRng = SimRng.new(42)
|
||||
assert_ne(_rolls(rng, "loot"), _rolls(rng, "weapons"))
|
||||
|
||||
|
||||
func test_extra_rolls_in_one_stream_do_not_disturb_another() -> void:
|
||||
var noisy: SimRng = SimRng.new(42)
|
||||
_rolls(noisy, "loot")
|
||||
assert_eq(_rolls(noisy, "weapons"), _rolls(SimRng.new(42), "weapons"))
|
||||
|
||||
|
||||
func test_stream_names_are_sorted() -> void:
|
||||
var rng: SimRng = SimRng.new(1)
|
||||
rng.stream("zebra")
|
||||
rng.stream("alpha")
|
||||
assert_eq(rng.stream_names(), ["alpha", "zebra"] as Array[String])
|
||||
|
||||
|
||||
func _rolls(rng: SimRng, stream_name: String) -> Array[int]:
|
||||
var results: Array[int] = []
|
||||
var stream: RandomNumberGenerator = rng.stream(stream_name)
|
||||
for _index: int in 16:
|
||||
results.append(stream.randi())
|
||||
return results
|
||||
@@ -0,0 +1 @@
|
||||
uid://b12mib1013p6k
|
||||
@@ -0,0 +1,152 @@
|
||||
extends TestCase
|
||||
|
||||
const SEED: int = 123456789
|
||||
const PLAYERS: int = Tuning.SOLO_PLAYERS
|
||||
const TICKS: int = 10_000
|
||||
|
||||
|
||||
func test_same_seed_and_log_produce_the_same_hash_over_ten_thousand_ticks() -> void:
|
||||
var log_bytes: PackedByteArray = _build_command_log(SEED, PLAYERS, TICKS)
|
||||
var first: String = _replay(SEED, log_bytes)
|
||||
var second: String = _replay(SEED, log_bytes)
|
||||
assert_eq(first, second)
|
||||
assert_eq(first.length(), 64, "expected a SHA-256 hex digest")
|
||||
|
||||
|
||||
func test_a_different_seed_changes_the_hash() -> void:
|
||||
var log_bytes: PackedByteArray = _build_command_log(SEED, 4, 200)
|
||||
assert_ne(_replay(SEED, log_bytes, 4), _replay(SEED + 1, log_bytes, 4))
|
||||
|
||||
|
||||
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
|
||||
assert_ne(_replay(SEED, log_bytes, 4), _replay(SEED, changed, 4))
|
||||
|
||||
|
||||
func test_submission_order_across_players_does_not_matter() -> void:
|
||||
var a: Simulation = _simulation(SEED, 2)
|
||||
var b: Simulation = _simulation(SEED, 2)
|
||||
var cmd_one: PlayerCommand = PlayerCommand.from_input(0, Vector2.RIGHT, 0.5, 0.0)
|
||||
var cmd_two: PlayerCommand = PlayerCommand.from_input(0, Vector2.UP, 1.5, 0.0)
|
||||
a.submit_command(1, cmd_one)
|
||||
a.submit_command(2, cmd_two)
|
||||
b.submit_command(2, cmd_two)
|
||||
b.submit_command(1, cmd_one)
|
||||
a.step()
|
||||
b.step()
|
||||
assert_eq(a.state_hash(), b.state_hash())
|
||||
|
||||
|
||||
func test_missing_command_keeps_the_last_look_and_stops_moving() -> void:
|
||||
var sim: Simulation = _simulation(SEED, 1)
|
||||
sim.submit_command(1, PlayerCommand.from_input(0, Vector2(0.0, 1.0), 1.0, 0.25))
|
||||
sim.step()
|
||||
var player: SimEntity = sim.entities.find(1)
|
||||
var yaw: int = player.yaw
|
||||
var position: Vector3 = player.position
|
||||
assert_ne(yaw, 0)
|
||||
sim.step()
|
||||
assert_eq(player.yaw, yaw)
|
||||
assert_eq(player.position, position)
|
||||
|
||||
|
||||
func test_movement_speed_matches_tuning() -> void:
|
||||
var sim: Simulation = _simulation(SEED, 1)
|
||||
var player: SimEntity = sim.entities.find(1)
|
||||
# Start from the middle of the world so clamping can't interfere.
|
||||
player.position = Vector3.ONE * 500.0
|
||||
player.position.y = 0.0
|
||||
var start: Vector3 = player.position
|
||||
sim.submit_command(1, PlayerCommand.from_input(0, Vector2(0.0, 1.0), 0.0, 0.0))
|
||||
sim.step()
|
||||
assert_near(start.distance_to(player.position), Tuning.WALK_SPEED / Tuning.TICK_RATE_HZ, 0.001)
|
||||
|
||||
|
||||
func test_stale_and_far_future_commands_are_rejected() -> void:
|
||||
var sim: Simulation = _simulation(SEED, 1)
|
||||
sim.step()
|
||||
assert_false(sim.submit_command(1, PlayerCommand.from_input(0, Vector2.ZERO, 0.0, 0.0)))
|
||||
var too_far: int = sim.tick + Tuning.MAX_COMMAND_LEAD_TICKS + 1
|
||||
assert_false(sim.submit_command(1, PlayerCommand.from_input(too_far, Vector2.ZERO, 0.0, 0.0)))
|
||||
assert_true(sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0)))
|
||||
|
||||
|
||||
func test_commands_for_unknown_players_are_rejected() -> void:
|
||||
var sim: Simulation = _simulation(SEED, 1)
|
||||
assert_false(sim.submit_command(99, PlayerCommand.from_input(0, Vector2.ZERO, 0.0, 0.0)))
|
||||
|
||||
|
||||
func test_removed_player_is_gone_and_id_is_not_reused() -> void:
|
||||
var sim: Simulation = _simulation(SEED, 2)
|
||||
sim.remove_player(1)
|
||||
assert_false(sim.entities.contains(1))
|
||||
assert_false(sim.submit_command(1, PlayerCommand.from_input(0, Vector2.ZERO, 0.0, 0.0)))
|
||||
assert_eq(sim.add_player(), 3)
|
||||
sim.step()
|
||||
|
||||
|
||||
func test_positions_stay_inside_the_world() -> void:
|
||||
var sim: Simulation = _simulation(SEED, 1)
|
||||
var player: SimEntity = sim.entities.find(1)
|
||||
player.position = Vector3(Tuning.WORLD_SIZE_METRES, 0.0, Tuning.WORLD_SIZE_METRES)
|
||||
for at_tick: int in 30:
|
||||
sim.submit_command(1, PlayerCommand.from_input(at_tick, Vector2(1.0, 1.0), 0.8, 0.0))
|
||||
sim.step()
|
||||
assert_true(player.position.x <= Tuning.WORLD_SIZE_METRES)
|
||||
assert_true(player.position.z <= Tuning.WORLD_SIZE_METRES)
|
||||
assert_true(player.position.x >= 0.0 and player.position.z >= 0.0)
|
||||
|
||||
|
||||
func test_simulation_needs_no_scene_tree() -> void:
|
||||
# Simulation is a plain object: it steps with no Node and no tree involved.
|
||||
var sim: Variant = Simulation.new(SEED)
|
||||
assert_false(sim is Node)
|
||||
(sim as Simulation).add_player()
|
||||
(sim as Simulation).step()
|
||||
assert_eq((sim as Simulation).tick, 1)
|
||||
|
||||
|
||||
func _simulation(match_seed: int, players: int) -> Simulation:
|
||||
var sim: Simulation = Simulation.new(match_seed)
|
||||
for _index: int in players:
|
||||
sim.add_player()
|
||||
return sim
|
||||
|
||||
|
||||
## Builds a log of ticks * players encoded commands, in a compact byte array.
|
||||
func _build_command_log(log_seed: int, players: int, ticks: int) -> PackedByteArray:
|
||||
var random: RandomNumberGenerator = RandomNumberGenerator.new()
|
||||
random.seed = log_seed
|
||||
var bytes: PackedByteArray = PackedByteArray()
|
||||
bytes.resize(players * ticks * PlayerCommand.ENCODED_SIZE)
|
||||
var offset: int = 0
|
||||
for at_tick: int in ticks:
|
||||
for _player: int in players:
|
||||
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.pitch = random.randi_range(-32767, 32767)
|
||||
command.buttons = random.randi_range(0, 255)
|
||||
var encoded: PackedByteArray = command.encode()
|
||||
for index: int in encoded.size():
|
||||
bytes[offset + index] = encoded[index]
|
||||
offset += PlayerCommand.ENCODED_SIZE
|
||||
return bytes
|
||||
|
||||
|
||||
func _replay(match_seed: int, log_bytes: PackedByteArray, players: int = PLAYERS) -> String:
|
||||
var sim: Simulation = _simulation(match_seed, players)
|
||||
var per_tick: int = players * PlayerCommand.ENCODED_SIZE
|
||||
var ticks: int = log_bytes.size() / per_tick
|
||||
for at_tick: int in ticks:
|
||||
for player: int in players:
|
||||
var offset: int = at_tick * per_tick + player * PlayerCommand.ENCODED_SIZE
|
||||
var command: PlayerCommand = PlayerCommand.decode(log_bytes, offset)
|
||||
sim.submit_command(player + 1, command)
|
||||
sim.step()
|
||||
return sim.state_hash()
|
||||
@@ -0,0 +1 @@
|
||||
uid://b3x1unn67dxq6
|
||||
Reference in New Issue
Block a user