Fix the server movement step for a shooter
Your task
Your multiplayer shooter's server accepted each client's claimed position, so speed hacks and teleports were the protocol working as written. The fix makes the server authoritative: clients send only an input, and the server moves the player itself under a speed cap and inside the world's bounds.
An assistant wrote simulate_player_movement(intents, max_speed, dt, min_x, max_x). It is in the editor. It has a bug. Find it and fix it.
Input and output:
intents: one input per tick, in order, nominally from -1 (full left) to 1 (full right). A hacked client can send anything.max_speed: the most distance per second,dt: the tick length in seconds.min_x,max_x: the world's bounds on this axis.- Return the player's position after each tick, one per intent.
Rules the tests check:
- If
min_xis greater thanmax_x, swap them. - A tick moves the player by
intent * max_speed * dt. Ifmax_speed * dtis negative, use 0. - Clamp each intent to the range -1 to 1 first.
- The player spawns at 0, clamped into the bounds.
- After every tick, clamp the position into the bounds. The next tick starts from that clamped position.
- Answers within 0.000001 of the expected value pass.
Examples
simulate_player_movement(intents: list[float], max_speed: float, dt: float, min_x: float, max_x: float) -> list[float]Example 1
Input
simulate_player_movement([1, 1, 1], 5, 0.1, -10, 10)Output
[0.5, 1, 1.5]Example 2
Input
simulate_player_movement([999, -999], 5, 0.1, -10, 10)Output
[0.5, 0]Example 3
Input
simulate_player_movement([], 5, 0.1, -10, 10)Output
[]