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 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)) 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()