Fix the deferred spawn pass in an ECS
Your task
Your game's shoot system created projectile entities while it was still iterating over the shooters, so new projectiles sometimes had garbage positions or skipped a frame. The fix records spawn commands during the loop and applies them after it, with fresh entity ids.
An assistant wrote apply_deferred_spawns(entities). It is in the editor. It has a bug. Find it and fix it.
Input and output:
entities: one row per entity,[id, x, y, wants_to_fire].- Return the entity rows after the frame.
Rules the tests check:
- Drop any row that does not have exactly 4 items. It is left out of the output too.
- Every remaining row whose
wants_to_fireis not 0 spawns one projectile at that row'sxandy. - The output is the remaining rows in input order, unchanged, followed by the new projectiles in the order of their shooters.
- A projectile row is
[id, x, y, 0]. Ids must never collide with an existing entity: the first new id is one more than the highest id among the remaining rows (or 1 if no row remains or every id is below 1), and each next projectile takes the next id up.
Examples
apply_deferred_spawns(entities: list[list[int]]) -> list[list[int]]Example 1
Input
apply_deferred_spawns([[1, 10, 20, 1], [2, 30, 40, 0]])Output
[[1, 10, 20, 1], [2, 30, 40, 0], [3, 10, 20, 0]]Example 2
Input
apply_deferred_spawns([[1, 5, 5, 1], [2, 7, 7, 1]])Output
[[1, 5, 5, 1], [2, 7, 7, 1], [3, 5, 5, 0], [4, 7, 7, 0]]