Skip to content

Fix crash when more than 2^24 terrain tiles are edited - #6424

Open
YassineKaibi wants to merge 3 commits into
openfrontio:mainfrom
YassineKaibi:fix/6388-terrain-edit-log-map-limit
Open

YassineKaibi wants to merge 3 commits into
openfrontio:mainfrom
YassineKaibi:fix/6388-terrain-edit-log-map-limit

Conversation

@YassineKaibi

Copy link
Copy Markdown

Resolves #6388

Description:

GameMapImpl recorded every tile whose terrain changed in a Map, which holds at most 2^24 (~16.7M) entries. World10000 has 50M tiles, and with water nukes each large crater adds roughly 32k tiles to the log, so a long game passed the limit and crashed with RangeError: Map maximum size exceeded.

The log now uses a one-bit-per-tile mask plus growable typed arrays, so it has no entry limit. First-edit order and the snapshot format are unchanged. GameImpl.addViewResync built a Set from the same log and would have hit the same limit, so it now calls a new isTerrainEdited on the map.

Tests: tests/core/game/GameMap.terrainEdits.test.ts covers edit order, original bytes, snapshot and restore round-trip, and a map with more than 2^24 edited tiles. That last test fails on main with the same RangeError and passes with this change. On my machine, npm test also fails 4 client tests (InventoryModal, PlayerName, WinModalProvisionalXp, and a WebGL perf test); they fail the same way on main without this change.

Please complete the following:

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

DIscord username

theziz_

GameMapImpl logged every tile whose terrain changed in a Map, which holds
at most 2^24 entries. World10000 has 50M tiles, so a long water-nuke game
there ran past the limit and crashed with "Map maximum size exceeded".

Keep the log in a one-bit-per-tile mask plus growable typed arrays, which
have no entry limit. The snapshot format and first-edit order are
unchanged. addViewResync built a Set from the same log and would have hit
the same limit; it now asks the map through isTerrainEdited.

Resolves openfrontio#6388
@CLAassistant

CLAassistant commented Nov 13, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3a575c38-3bb5-4ef4-872e-17c44ce0ec23
Commits

Reviewing files that changed from the base of the PR and between 19006cb and 3fbacab.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

GameMapImpl now tracks terrain edits with a bitset and typed arrays. Hash calculation and snapshots use these records, and snapshot restoration applies an updated fresh-map check. GameImpl.addViewResync checks edit status per tile. Tests cover edit order, snapshot restoration, and more than 2^24 edited tiles.

Changes

Terrain edit tracking

Layer / File(s) Summary
Record terrain edits
packages/engine-lib/src/game/GameMapImpl.ts, packages/engine/src/game/GameImpl.ts, tests/core/game/GameMap.terrainEdits.test.ts
GameMapImpl records each edited tile once, in first-edit order, using a bitset and dynamically grown typed arrays. GameImpl.addViewResync checks each tile with isTerrainEdited. Tests cover edit order and tracking more than 2^24 tiles.
Build and restore snapshots
packages/engine-lib/src/game/GameMapImpl.ts, tests/core/game/GameMap.terrainEdits.test.ts
Pristine-hash calculation and snapshots use the typed-array edit records. Snapshot restoration rejects a map with an allocated edit bitset or nonzero state. Tests verify restored terrain and pristine hash.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: evanpelle, jb940

Merge Risk: Minimal · previous review dff8b

This change removes the crash when more than 2^24 terrain tiles are edited, while keeping edit order and the snapshot format. No code risk was found. A reviewer still needs to assign a milestone to clear the failing process check.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a crash fix for edited terrain tiles exceeding the 2^24 Map entry limit. The changeset replaces the Map-based edit log in GameMapImpl with a bitset and typed arrays, w…
Description check Passed The description explains the crash cause (Map limit of 2^24 entries on World10000 with water nukes), the bitset and typed-array fix, the GameImpl.addViewResync change, and the new tests. It relates di…
Linked Issues check Passed Issue #6388 requires that the game keep running after more than 2^24 terrain tiles are edited. The reported crash comes from the Map used as the edit log in GameMapImpl (pristineTerrain). The di…
Out of Scope Changes check Passed All changed code maps to issue #6388. The edit log change in GameMapImpl.ts removes the crash. The addViewResync change in GameImpl.ts removes the same limit on the second consumer named in the …
Full details: Title check

Explanation

The title names the main change: a crash fix for edited terrain tiles exceeding the 2^24 Map entry limit. The changeset replaces the Map-based edit log in GameMapImpl with a bitset and typed arrays, which matches the title.

Full details: Description check

Explanation

The description explains the crash cause (Map limit of 2^24 entries on World10000 with water nukes), the bitset and typed-array fix, the GameImpl.addViewResync change, and the new tests. It relates directly to the changeset.

Full details: Linked Issues check

Explanation

Issue #6388 requires that the game keep running after more than 2^24 terrain tiles are edited. The reported crash comes from the Map used as the edit log in GameMapImpl (pristineTerrain). The diff to packages/engine-lib/src/game/GameMapImpl.ts replaces that Map with a one-bit-per-tile editedMask (Uint8Array of (terrain.length + 7) >>> 3 bytes) and growable typed arrays editTiles (Uint32Array) and editOriginal (Uint8Array). noteTerrainEdit records each tile once, in first-edit order, and grows capacity up to terrain.length. No entry-count limit remains. The issue also names GameImpl.addViewResync, which built a Set from the same log. The diff to packages/engine/src/game/GameImpl.ts now calls map.isTerrainEdited(tile), which reads the mask. Pristine-hash, snapshot, and restore paths read the new arrays. The snapshot field layout is unchanged. The restore guard now checks editedMask, and the other validations remain. The summary reports a new test file, tests/core/game/GameMap.terrainEdits.test.ts, with a case above 2^24 edited tiles. I did not inspect that test file. The test result is taken from the PR description and summary, not from a run. No non-coding requirements exist in the issue.

Full details: Out of Scope Changes check

Explanation

All changed code maps to issue #6388. The edit log change in GameMapImpl.ts removes the crash. The addViewResync change in GameImpl.ts removes the same limit on the second consumer named in the issue. The editedTerrainTiles() return type changes from TileRef[] to Uint32Array, which follows from the new storage and is needed by the fix. The new test file covers the changed behavior. I did not find a change unrelated to the issue in the reviewed diffs. I did not check other callers of editedTerrainTiles() for a return-type dependency. The PR description reports four client test failures that also occur on main. These come from no code change in this PR.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

A tile is marked, its first edit kept,
In typed arrays, the records rest.
Snapshots hold the bytes in view,
Hashes use the originals too.
Beyond the old limit, tracking grew.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b0ef967c-4f5a-46e6-bda0-59ded44a17cb
Commits

Reviewing files that changed from the base of the PR and between a084d23 and dff8b72.

Files selected for processing: 3

  • packages/engine-lib/src/game/GameMapImpl.ts
  • packages/engine/src/game/GameImpl.ts
  • tests/core/game/GameMap.terrainEdits.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@github-actions

Copy link
Copy Markdown
Contributor

🤖 Claude Code Review

Verdict: No issues found. Findings: 0 high, 0 medium, 0 low.

Checked for bugs and CLAUDE.md compliance. The review covered:

  • The bitmask and growable typed-array edit log in GameMapImpl (bit indexing, growth capped at the tile count, dedup on first edit).
  • pristineHash, snapshot() (it returns copies, and the wire format is unchanged), and the "already edited" guard in restoreSnapshot.
  • The isTerrainEdited change in GameImpl.addViewResync, and the editedTerrainTiles() callers whose return type changed.
  • Engine determinism and layer-boundary rules, and the test requirements.

@github-actions

Copy link
Copy Markdown
Contributor

🤖 Claude Code Review

Reviewed range: dff8b72..19006cb (incremental).

Verdict: No issues found. Checked for bugs and CLAUDE.md compliance. (0 critical, 0 major, 0 minor)

The only new commit, 19006cb, merges main into the branch. It brings in the PR-readiness workflow, a CONTRIBUTING.md change and the new map-category removals from #6422. None of these hunks touch this PR's files (GameMapImpl.ts, GameImpl.ts, GameMap.terrainEdits.test.ts). The merge doesn't change terrain data, map dimensions or anything the terrain-edit log depends on, so it doesn't affect the fix.

@github-actions

Copy link
Copy Markdown
Contributor

🤖 Claude Code Review

Reviewed range: 19006cb..3fbacab (incremental since the previous Claude review).

Verdict: No issues found. Checked for bugs and CLAUDE.md compliance. (0 critical, 0 major, 0 minor)

The only commit in this range is a merge of main into the branch. It brings in the Canada map data fix (#6430) and the desktop/Google sign-in client changes (AccountModal.ts, Auth.ts, DesktopShell.ts, en.json, plus their tests). It touches none of this PR's files (GameMapImpl.ts, GameImpl.ts, GameMap.terrainEdits.test.ts), and none of the merged changes affect the terrain-edit log or isTerrainEdited. The PR's test builds its own map and doesn't load any map data, so the new Canada binaries can't change its result.

@YassineKaibi

Copy link
Copy Markdown
Author

/ready

@Celant Celant left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice catch, and the bitmask plus dense log is the right shape for a map this size. Nothing from me.

@Celant Celant added this to the v37 milestone Nov 16, 2026
@github-project-automation github-project-automation Bot moved this from Triage to Final Review in OpenFront Release Management Nov 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Final Review

Development

Successfully merging this pull request may close these issues.

Game crashes with "Map maximum size exceeded" on World10000 with water nukes

3 participants