fix(runner): disambiguation suffix skips ids already taken by explicit combatants

This commit is contained in:
2026-09-20 17:33:35 +02:00
parent de9309ca43
commit e9c2a63122
2 changed files with 35 additions and 4 deletions
+11 -4
View File
@@ -109,12 +109,16 @@ def _first_fit_anchor(
def build_states(spec: EncounterSpec) -> list[CombatantState]:
"""Fresh states for one battle; duplicate combatant ids get -N suffixes.
The suffix keeps incrementing until the candidate id is not already
taken, so an explicit id like ``goblin-2`` never collides with a
disambiguated duplicate of ``goblin`` (the engine keys stats by id).
Combatants of a side fill the zone's cells in (row, col) order. A side with
more combatants than zone cells, or a zone with no cells, is a spec error.
Creatures larger than 1x1 occupy an n x n footprint anchored at their cell.
"""
states: list[CombatantState] = []
seen: dict[str, int] = {}
taken: set[str] = set()
grid = Grid.from_spec(spec.map)
for side in spec.sides:
cells = zone_cells(spec.map, side.zone)
@@ -137,9 +141,12 @@ def build_states(spec: EncounterSpec) -> list[CombatantState]:
f"{combatant.id!r} ({combatant.size}, {n}x{n})"
)
raise ValueError(msg)
count = seen.get(combatant.id, 0) + 1
seen[combatant.id] = count
cid = combatant.id if count == 1 else f"{combatant.id}-{count}"
cid = combatant.id
suffix = 2
while cid in taken:
cid = f"{combatant.id}-{suffix}"
suffix += 1
taken.add(cid)
effective = (
combatant
if cid == combatant.id