Skip to content

Fix TribeSpawner crash on invalid nation coordinates - #4847

Merged
evanpelle merged 3 commits into
mainfrom
fix/tribespawner-invalid-coords
Aug 3, 2026
Merged

Fix TribeSpawner crash on invalid nation coordinates#4847
evanpelle merged 3 commits into
mainfrom
fix/tribespawner-invalid-coords

Conversation

@FloPinguin

Copy link
Copy Markdown
Contributor

Description:

The TribeSpawner constructor called gs.ref(c.x, c.y) on all nation cells without checking if coordinates are valid. If a map has nation coordinates outside its bounds (e.g. the Scandinavia map has "Tver" at x=2007 on a 2006-wide image), the game would crash with Error: Invalid coordinates during map load.

Added a gs.isValidCoord() filter before building the nationTiles set, matching the existing defensive pattern in spawnPositionedTribe().

Please complete the following:

  • I have added screenshots for all UI updates
  • I process any text displayed to the user through translateText() and I've added it to the en.json file
  • I have added relevant tests to the test directory

Please put your Discord username so you can be contacted if a bug or regression is found:

FloPinguin

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 50b5d30f-dee2-49b3-9fc2-1c33b5534335

📥 Commits

Reviewing files that changed from the base of the PR and between 94aebf8 and abad23d.

📒 Files selected for processing (1)
  • tests/core/execution/TribeSpawner.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/core/execution/TribeSpawner.test.ts

Walkthrough

TribeSpawner filters invalid nation cell coordinates before creating nation tiles. Tests cover invalid cells, occupied valid tiles, random spawning, and warning-backed fallback.

Changes

Tribe spawning validation

Layer / File(s) Summary
Nation cell validation and spawning coverage
src/core/execution/TribeSpawner.ts, tests/core/execution/TribeSpawner.test.ts
The constructor excludes invalid nation cells before creating nationTiles. Tests confirm that invalid cells do not throw, valid nation tiles block positioned tribes, and blocked placement uses random fallback with a warning.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Poem

Invalid cells stay outside,
Valid tiles mark space occupied.
Blocked positions warn and bend,
Random spawning starts again.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description check ✅ Passed The description clearly explains the invalid-coordinate crash, the defensive fix, and the related tests.
Title check ✅ Passed The title clearly and concisely identifies the TribeSpawner crash caused by invalid nation coordinates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/core/execution/TribeSpawner.ts`:
- Around line 25-29: Add focused tests for the construction path in TribeSpawner
that cover both an out-of-bounds nation cell being ignored without throwing and
a valid nation cell preventing positioned tribe spawning. Reuse the existing
test fixtures and spawning APIs, and assert each behavior directly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 46baaadc-04c1-4f47-bfc3-7f3184f79d80

📥 Commits

Reviewing files that changed from the base of the PR and between 31809e5 and dd31031.

📒 Files selected for processing (1)
  • src/core/execution/TribeSpawner.ts

Comment on lines +25 to +29
this.nationTiles = new Set(
nationCells
.filter((c) => gs.isValidCoord(c.x, c.y))
.map((c) => gs.ref(c.x, c.y)),
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Add tests for invalid and valid nation cells.

This file matches src/core/**/*.ts. The repository guideline requires tests for every change in this directory. Add focused tests that verify:

  • An out-of-bounds nation cell does not crash construction.
  • A valid nation cell still blocks positioned tribe spawning.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/core/execution/TribeSpawner.ts` around lines 25 - 29, Add focused tests
for the construction path in TribeSpawner that cover both an out-of-bounds
nation cell being ignored without throwing and a valid nation cell preventing
positioned tribe spawning. Reuse the existing test fixtures and spawning APIs,
and assert each behavior directly.

Source: Coding guidelines

@github-project-automation github-project-automation Bot moved this from Triage to Development in OpenFront Release Management Aug 3, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/core/execution/TribeSpawner.test.ts`:
- Around line 383-387: Update the tile selection in the test “valid nation cell
prevents positioned tribe from occupying that tile” to use an unowned land tile
by requiring !game.hasOwner(t) in the findLandTile predicate. Keep the existing
land and impassable checks, ensuring the test reaches the nationTiles blocking
logic in TribeSpawner.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 15a0f9ce-9544-4c85-be85-415d744ebd23

📥 Commits

Reviewing files that changed from the base of the PR and between dd31031 and 94aebf8.

📒 Files selected for processing (1)
  • tests/core/execution/TribeSpawner.test.ts

Comment on lines +383 to +387
test("valid nation cell prevents positioned tribe from occupying that tile", async () => {
const game = await setup("plains", { bots: 1, gameMap: GameMapType.Asia });
const tile = findLandTile(game);
const x = game.x(tile);
const y = game.y(tile);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Select an unowned tile for this test.

Line 385 uses findLandTile, but the helper checks only land and impassable state. src/core/execution/TribeSpawner.ts also rejects owned tiles before it checks nationTiles.has(tile). If this tile has an owner, lines 399-405 pass without testing nation-cell blocking.

Update findLandTile to require !game.hasOwner(t).

Proposed test isolation fix
-      if (game.isLand(t) && !game.isImpassable(t)) return t;
+      if (
+        game.isLand(t) &&
+        !game.isImpassable(t) &&
+        !game.hasOwner(t)
+      ) {
+        return t;
+      }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/core/execution/TribeSpawner.test.ts` around lines 383 - 387, Update the
tile selection in the test “valid nation cell prevents positioned tribe from
occupying that tile” to use an unowned land tile by requiring !game.hasOwner(t)
in the findLandTile predicate. Keep the existing land and impassable checks,
ensuring the test reaches the nationTiles blocking logic in TribeSpawner.

@evanpelle
evanpelle merged commit deeb3e5 into main Aug 3, 2026
13 checks passed
@evanpelle
evanpelle deleted the fix/tribespawner-invalid-coords branch August 3, 2026 15:13
@github-project-automation github-project-automation Bot moved this from Development to Complete in OpenFront Release Management Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bugfix Fixes a bug

Projects

Status: Complete

Development

Successfully merging this pull request may close these issues.

2 participants