Fix the damage pass in an ECS game loop

Review the AI's codeMedium15 min

Your task

Your game uses an entity component system (ECS): an entity is an id, and each kind of data, such as health, lives in its own component store. The old damage pass removed the Health component from the store it was iterating, so under heavy area damage units sometimes skipped their death. The fix sums all damage first, then decides deaths in a separate pass.

An assistant wrote resolve_damage(healths, hits). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • healths: healths[i] is entity i's current health.
  • hits: the damage events this frame, each [entity, amount].
  • Return the ids of the entities that die this frame.

Rules the tests check:

  • An entity with health 0 or less has no Health component, so this system ignores it and every hit on it.
  • Skip a hit with fewer than 2 items, an entity id that is not an index of healths, or an amount of 0 or less. Items after the first two are ignored.
  • Sum the remaining hits per entity before deciding anything.
  • An entity dies when its total damage is at least its health.
  • Return every entity that dies, each once, in ascending id order.

Examples

resolve_damage(healths: list[int], hits: list[list[int]]) -> list[int]
Example 1
Inputresolve_damage([10, 10, 10], [[0, 4], [0, 7], [2, 10]])
Output[0, 2]
Example 2
Inputresolve_damage([5], [])
Output[]
Example 3
Inputresolve_damage([3, 3], [[1, 1], [1, 1], [1, 1], [1, 1]])
Output[1]