Commit Graph

2358 Commits

Author SHA1 Message Date
scamiv c4779ad100 Merge branch 'sab' into Atomic-SAB 2025-11-26 18:42:54 +01:00
scamiv ea7d5089b8 Size SAB ring buffer by world tile count 2025-11-26 18:41:47 +01:00
scamiv 30c3a20cb8 Merge branch 'sab' into Atomic-SAB 2025-11-26 17:47:35 +01:00
scamiv ecabf3577e Use dirty flags to coalesce tile updates in SAB ring
- Extend SharedTileRing to include a shared dirtyFlags buffer alongside header and data
- Pass shared dirty buffer through WorkerClient/WorkerMessages and initialize views in Worker.worker
- In SAB mode, mark tiles dirty via Atomics.compareExchange before enqueuing to ensure each tile is queued at most once until processed
- On the main thread, clear dirty flags when draining the ring and build packedTileUpdates from distinct tile refs
- Keep non-SAB behaviour unchanged while reducing ring pressure and making overflows reflect true backlog, not duplicate updates
2025-11-26 17:31:36 +01:00
scamiv 7b969a7231 Revert "dedup tileRef for tileUpdateSink(tileRef)"
This reverts commit 08a2ff906b.
2025-11-26 17:12:24 +01:00
scamiv 75d7b09ea5 Revert "Merge branch 'sab' into Atomic-SAB"
This reverts commit d61046c3aa, reversing
changes made to a839cedfc3.
2025-11-26 17:03:17 +01:00
scamiv d61046c3aa Merge branch 'sab' into Atomic-SAB 2025-11-26 16:44:44 +01:00
scamiv 08a2ff906b dedup tileRef for tileUpdateSink(tileRef) 2025-11-26 16:41:27 +01:00
scamiv a839cedfc3 Merge branch 'sab' into Atomic-SAB 2025-11-26 15:57:33 +01:00
scamiv 6793871cf3 fix performance overlay 2025-11-26 15:56:34 +01:00
scamiv 905e42d34d test debug logging 2025-11-26 15:45:12 +01:00
scamiv 0fe4a5e004 Merge branch 'sab' into Atomic-SAB 2025-11-26 15:23:59 +01:00
scamiv 0983b17acb fix sab detection 2025-11-26 15:22:34 +01:00
scamiv 5d13ce1abb Merge branch 'sab' into Atomic-SAB 2025-11-26 15:00:33 +01:00
scamiv bb16333801 mergeGameUpdates fix batch.length === 0 return case 2025-11-26 14:57:52 +01:00
scamiv 540663cd56 Merge branch 'sab' into Atomic-SAB 2025-11-26 14:48:59 +01:00
scamiv ff0533fd1b add more stats to perf overlay 2025-11-26 14:46:05 +01:00
scamiv 8159222612 Change the ring buffer to Uint32Array
Store only TileRef instead of packed tile+state values
2025-11-26 14:36:11 +01:00
scamiv 69354be8cc Merge branch 'sab' into Atomic-SAB 2025-11-26 00:48:36 +01:00
scamiv a2a89aa981 Use SharedArrayBuffer tile state and ring buffer for worker updates
- Share GameMapImpl tile state between worker and main via SharedArrayBuffer
- Add SAB-backed tile update ring buffer to stream tile changes instead of postMessage payloads
- Wire shared state/ring through WorkerClient, Worker.worker, GameRunner, and ClientGameRunner
- Update GameView to skip updateTile when shared state is enabled and consume tile refs from the ring
2025-11-26 00:45:06 +01:00
scamiv bb28f4c0c9 Merge branch 'sab' into Atomic-SAB 2025-11-25 21:41:41 +01:00
scamiv cb7c94efb8 SAB+Atomics refactor
Added src/core/worker/SharedTileRing.ts, which defines a SharedArrayBuffer-backed ring buffer (SharedTileRingBuffers/SharedTileRingViews) and helpers pushTileUpdate (worker-side writer) and drainTileUpdates (main-thread reader) using Atomics.

Extended GameRunner (src/core/GameRunner.ts) with an optional tileUpdateSink?: (update: bigint) => void; when provided, tile updates are sent to the sink instead of being packed into GameUpdateViewData.packedTileUpdates (those become an empty BigUint64Array in this mode).

Extended the worker protocol (src/core/worker/WorkerMessages.ts) so the init message can optionally carry sharedTileRingHeader and sharedTileRingData (the two SABs for the ring).

Updated WorkerClient (src/core/worker/WorkerClient.ts) to accept optional SharedTileRingBuffers in its constructor and, during initialize(), include those SABs in the init message sent to Worker.worker.ts.

Updated the worker implementation (src/core/worker/Worker.worker.ts) to build sharedTileRing views from the SABs on "init" and pass a (update: bigint) => pushTileUpdate(...) sink into createGameRunner, so every tile update from the simulation goes into the shared ring instead of the message payload.

Updated the client side (src/client/ClientGameRunner.ts):
In createClientGame, when SharedArrayBuffer/Atomics are available and crossOriginIsolated is true, it allocates the ring (createSharedTileRingBuffers with capacity 262144), creates views, passes buffers into WorkerClient, and keeps the views in ClientGameRunner.
ClientGameRunner now has a tileRingViews field and, in mergeGameUpdates, if the ring is present it ignores gu.packedTileUpdates and instead drains up to MAX_TILE_UPDATES_PER_RENDER (100 000) tile updates from the SAB ring into combinedPackedTileUpdates before constructing the merged GameUpdateViewData passed to GameView.update.
If SABs are unavailable, it falls back to the original behaviour (packing gu.packedTileUpdates from messages), so the feature is opt-in and backward compatible.

Net effect: when the page is cross-origin isolated, the worker no longer sends per-tick BigUint64Array tile payloads via postMessage; it streams tile updates into a shared ring buffer, and the main thread pulls and applies them at its own pace while still batching non-tile updates and rendering exactly as before. If you’d like, I can next run the existing tests/build in your environment to sanity-check everything.
2025-11-25 21:22:13 +01:00
scamiv 97e7dfc0f2 Add performance metrics for worker and render ticks
- Introduced new metrics in ClientGameRunner to track worker simulation ticks and render tick calls per second.
- Updated TickMetricsEvent to include these new metrics.
- Enhanced PerformanceOverlay to display worker and render ticks per second, improving performance monitoring capabilities.
- Adjusted minimum FPS in GameRenderer
2025-11-25 19:21:33 +01:00
scamiv 7cf024fb6e Refactor rendering and throttle based on backlog
- Refactor rendering and metrics emission in ClientGameRunner to ensure updates occur only after all processing is complete
- Throttle renderGame() based on the current backlog
2025-11-25 18:46:25 +01:00
scamiv 6ab7136c3c add "ticks per render" metric 2025-11-25 17:44:54 +01:00
scamiv a21f066700 remove redundant logic 2025-11-25 17:40:39 +01:00
scamiv 4268f9019f ClientGameRunner: simplify catch-up loop with indexed queue
process updates in a single budgeted loop per RAF and render once
track queue head with pendingStart and compact to avoid array shifts
2025-11-24 22:54:09 +01:00
scamiv 93647399c1 Refactor slice budget calculation in ClientGameRunner to improve backlog handling. Introduced dynamic slice budget scaling based on backlog size, allowing for longer processing times when necessary while maintaining UI responsiveness. 2025-11-24 19:59:10 +01:00
VariableVince 9b125c8cfe Bugfix: nation strength undefined in only place it is used (#2498)
## Description:

In commit
https://github.com/openfrontio/OpenFrontIO/commit/bbf72bd14f7f31146c687523aea8fc0aff31bbe1#diff-ee2fcbca50d87cc09d2c7d2667210defe2e3e111239820c89c40283be5385b64
it was added that startManpower in DefaultConfig used
playerInfo.nation.strength to set the starting troops for a Nation. In
ExecutionManager, param nation of PlayerInfo was set during the
instantiation of a new FakeHumanExecution.

However in commit
https://github.com/openfrontio/OpenFrontIO/commit/d6a412aa50dd86d474d80c216fd9ba36e7426ef9#diff-2d0a5d8b171d8b504f934891025e42742e142ef0964d6e17712bfdcd30bf050c
the changes made it so that **param nation of PlayerInfo was never
set**. While startManpower in DefaultConfig still checked for
playerinfo.nation.strength. Since it was always undefined, it would use
a multiplier of 1 instead of the actual nation strength.

This PR fixes it by passing the nation strength to param nationStrength
in PlayerInfo. Removing param strength from class Nation. Strength isn't
used anywhere else so this isn't a problem and it also consolidates
human player info and nation player info even more. We could have also
used the Nation.strength directly, but that would have required more
code in addPlayers and addPlayer in GameImpl, especially for Teams
games. So this PR has the simplest solution.

- I did add a config setting useNationStrengthForStartManpower with a
comment that explains its reason for being. Namely that in the months
that startManpower didn't get to use nation strength because of the bug,
FakeHumans have become much harder to fight. Re-enabling higher starting
troops from this fix would make them even harder to fight, and i think
rebalancing is needed before that.

- Or we could decide to scrap Nation strength altogether, as it is only
ever used to set starting troops anyway. This would make map making a
little easier as a bycatch.


## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

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

tryout33
2025-11-24 10:30:18 -08:00
NOBODY 049485cd39 Fix: Correct percentage (%) placement in Persian UI (RTL handling) (#2501)
## Description:

Fixes incorrect RTL rendering where the percentage symbol (%) appeared
before the label in Persian (fa-IR).
The UI now correctly displays values as `20%`.

**Changes:**
- Updated `controlpanel.ts` to fix percentage position.
- 16 additions, 7 deletions.

**Testing:**
1. Switch UI language to Persian (fa-IR).
2. Open control panel with percentage display.
3. Confirm `%` appears after numeric value.
4. Verified no changes in LTR languages.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

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

nobodyiran
2025-11-24 10:29:08 -08:00
Rj Manhas a5cdd23c00 feat: added retaliate button (#2426)
If this PR fixes an issue, link it below. If not, delete these two
lines.
Resolves #495 

## Description:

Adds a button to quickly retaliate against a incoming attack

<img width="464" height="212" alt="image"
src="https://github.com/user-attachments/assets/4764d261-a408-4d61-a2d2-2685018aa698"
/>


## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

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

notifxy (1379678982676676639)
2025-11-24 09:34:04 -08:00
scamiv 9d63fcabe9 Implemented time-sliced catch-up on the main thread to keep input responsive.
src/client/ClientGameRunner.ts now drains pending game updates in small chunks (max 100 updates or ~8ms per slice) via requestAnimationFrame, merging and rendering per slice, and only clears the processing flag when the queue is empty.
2025-11-24 17:40:11 +01:00
scamiv 8508baee84 Clean up previous implementations
removed:
- catchUpMode and its CATCH_UP_ENTER/EXIT thresholds in ClientGameRunner
- tick metrics fields and overlay UI for inCatchUpMode and beatsPerFrame
- leftover worker heartbeat plumbing (message type + WorkerClient.sendHeartbeat) that was no longer used after self-clocking

changed:
- backlog tracking: keep serverTurnHighWater / lastProcessedTick / backlogTurns, but simplify it to just compute backlog and a backlogGrowing flag instead of driving a dedicated catch-up mode
- frame skip: adaptRenderFrequency now only increases renderEveryN when backlog > 0 and still growing; when backlog is stable/shrinking or zero, it decays renderEveryN back toward 1
- render loop: uses the backlog-aware renderEveryN unconditionally (no catch-up flag), and resets skipping completely when backlog reaches 0
- metrics/overlay: TickMetricsEvent now carries backlogTurns and renderEveryN; the performance overlay displays backlog and current “render every N frames” but no longer mentions catch-up or heartbeats

Learnings during branch development leading to this

Once the worker self-clocks, a separate “catch-up mode” and beats-per-frame knob don’t add real control; they just complicate the model.
Backlog is still a valuable signal, but it’s more effective as a quantitative input (backlog size and whether it’s growing) than as a boolean mode toggle.
Frame skipping should be driven by actual backlog pressure plus frame cost: throttle only while backlog is growing and frames are heavy, and automatically relax back to full-rate rendering once the simulation catches up.
2025-11-24 15:01:17 +01:00
scamiv b458d00157 Worker now self-clocks; no heartbeats needed
GameRunner exposes pending work via a new hasPendingTurns() so the worker can check whether more ticks need to be processed.
Worker auto-runs ticks: as soon as it initializes or receives a new turn, it calls processPendingTurns() and loops executeNextTick() while hasPendingTurns() is true. No more "heartbeat" message type; the worker no longer depends on the main thread’s RAF loop to advance the simulation.
Client main thread simplified:
Removed CATCH_UP_HEARTBEATS_PER_FRAME, the heartbeat loop, and the lastBeatsPerFrame tracking.
keepWorkerAlive now just manages frame skipping + draining. When it decides to render (based on renderEveryN), it drains pendingUpdates, merges them, updates GameView, and runs renderer.tick().
Because rendering a batch always implies draining, we restored the invariant that every GameView.update is paired with a layer tick() (no more lost incremental updates).
MAX_RENDER_EVERY_N is now 5 to keep the queue from growing too large while the worker sprints.
2025-11-23 21:03:22 +01:00
scamiv ddbd2d7b40 frameskip 2025-11-23 21:02:22 +01:00
scamiv f8ce8d71c0 Batch worker updates in client catch-up mode to reduce render cost
- Refactor worker update handling into processPendingUpdates so multiple GameUpdateViewData objects are batched per frame.
- Combine all tick updates in a batch into a single GameUpdateViewData before applying it to GameView, while still running per-tick side effects (turnComplete, hashes, backlog metrics, win saving).
- Ensure layers using updatesSinceLastTick and recentlyUpdatedTiles see all events in a batch, fixing visual artifacts during fast-forward resync.
2025-11-23 19:28:34 +01:00
scamiv d2a9506605 Add client catch-up mode
Increase worker heartbeats per frame when far behind server to fast-forward simulation.
Track backlog and expose catch-up status via TickMetricsEvent.
Extend performance overlay to display backlog turns and indicate active catch-up mode.
2025-11-23 14:07:12 +01:00
evanpelle 930a79e31c Merge branch 'v27' 2025-11-21 20:22:40 -08:00
Evan 0b651b6941 Update copyright notice & footer (#2496)
## Description:

* Update copyright notice to "OpenFront and Contributors"

* remove the "how to play" on the footer, since that will be moved to
the death screen.

* Removed css and used tailwind instead for better mobile support


Describe the PR.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

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

evan
2025-11-21 20:22:17 -08:00
scamiv 33810e41c5 Optimize edge lookup railnetwork (#2493)
## Description:

This PR optimizes how the rail network looks up railroads connecting two
stations by introducing an O(1) neighbor→railroad map on `TrainStation`.
It also updates `getOrientedRailroad` and railroad deletion to use this
new API, avoiding repeated linear scans over all railroads attached to a
station.

### What changed

- **TrainStation neighbor→railroad index**
- Added `railroadByNeighbor: Map<TrainStation, Railroad>` to
`TrainStation` for quick edge lookup.
  - Kept `railroads: Set<Railroad>` for iteration and existing APIs.
  - Updated lifecycle methods to keep both data structures in sync:
    - `addRailroad(railRoad: Railroad)` now:
      - Adds to `railroads`.
- Computes the neighbor station (`railRoad.from === this ? railRoad.to :
railRoad.from`).
      - Stores the mapping in `railroadByNeighbor`.
    - `removeRailroad(railRoad: Railroad)` now:
      - Removes from `railroads`.
      - Removes the corresponding entry from `railroadByNeighbor`.
- `clearRailroads()` now clears both `railroads` and
`railroadByNeighbor`.
- Added `getRailroadTo(station: TrainStation): Railroad | null` to
retrieve the connecting railroad in O(1).

- **Use the new API in `TrainStation` and `Railroad`**
- `TrainStation.removeNeighboringRails(station)` now calls
`removeRailroad(toRemove)` instead of manually deleting from the set,
ensuring the map stays in sync.
- `Railroad.delete(game)` now calls `from.removeRailroad(this)` and
`to.removeRailroad(this)` instead of mutating the sets directly.

- **Refactor `getOrientedRailroad` to use O(1) lookup**
- Replaced a linear scan over `from.getRailroads()` with a direct
lookup:

    ```ts
    export function getOrientedRailroad(
      from: TrainStation,
      to: TrainStation,
    ): OrientedRailroad | null {
      const railroad = from.getRailroadTo(to);
      if (!railroad) return null;
// If tiles are stored from -> to, we go forward when railroad.to === to
      const forward = railroad.to === to;
      return new OrientedRailroad(railroad, forward);
    }
    ```

  - Behavior is preserved:
- `getRailroadTo` returns the same `Railroad` instance that was
previously found by scanning `getRailroads()`.
- Direction (`forward` vs reversed) is still derived from the
`Railroad.from` / `.to` fields in the same way as before.

### Motivation

- `getOrientedRailroad` and upcoming logic both need to resolve “the
railroad between station A and station B” frequently.
- The old pattern (`for (const railroad of from.getRailroads()) { ...
}`) was:
  - O(degree) per lookup,
  - Repeated in multiple places,
- Harder to maintain as more features (like fare-based costs) touch this
code.
- Centralizing edge lookup in a dedicated `railroadByNeighbor` map makes
this:
  - **O(1)** per lookup,
  - Less error-prone (one source of truth),
- Easier to reuse from new systems (e.g. train pathfinding, fare-aware
logic).

### Impact / Risk

- **Public behavior:** No functional change in how railroads are
created, deleted, or oriented; only the lookup mechanism changed.
- **Internal invariants:** Correctness relies on:
- All railroad creations using `addRailroad` on both endpoints (already
true via `RailNetworkImpl.connect`).
- All removals (`Railroad.delete`,
`TrainStation.removeNeighboringRails`, `disconnectFromNetwork`) using
`removeRailroad` / `clearRailroads`, which this PR updates.
- **Tests:** Existing `TrainStation` tests still pass; they exercise
`addRailroad`, `removeNeighboringRails`, and `getRailroads()`, which
continue to behave the same from the outside.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

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

DISCORD_USERNAME
2025-11-21 22:33:08 +00:00
Loacky 024285389a Implement donation troops/gold between human players after forming an alliance (#2450)
Resolves #2448 

Hi team,

I've implemented and locally tested the alliance-related changes
(including unit tests and some manual simulation with multiple browser
profiles).
Unfortunately I wasn't able to perform full end-to-end testing on the
live game server with two separate machines/accounts.

If someone on the team (or another contributor) can verify the alliance
flow with two real players, that would be greatly appreciated before
merging. Happy to hop on a call or provide any clarification needed.

Thanks!

## Description:

Fixed a race condition bug where donations (troops/gold) between human
players failed after forming an alliance. The issue was caused by a
one-tick delay in `AllianceRequestReplyExecution`: the alliance
acceptance logic ran in `tick()` instead of `init()`, meaning the
alliance wasn't created until the tick after the execution was added. If
a donation execution was added in the same turn as the alliance
acceptance, it would fail the `isFriendly()` check because the alliance
didn't exist yet.

**Root cause:** When human players formed alliances via reply, the
execution model delayed alliance creation by one tick, while bots called
`accept()` directly without this delay.

**Solution:** Moved alliance acceptance logic from `tick()` to `init()`
in `AllianceRequestReplyExecution.ts`, ensuring immediate alliance
creation and eliminating race conditions with donations.

**Changes:**
- Modified
`src/core/execution/alliance/AllianceRequestReplyExecution.ts` to
process alliance replies in `init()` instead of `tick()`
- Added comprehensive test suite `tests/AllianceDonation.test.ts` with 5
test cases covering donation scenarios after alliance formation (reply
and mutual request flows)
- All existing tests pass (323 total)

## Please complete the following:

- [x] I have added screenshots for all UI updates (N/A - backend logic
fix only)
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file (N/A - no user-facing text
changes)
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced


Discord: loacky
GitHub: @LoackyBit

---------

Co-authored-by: Evan <evanpelle@gmail.com>
2025-11-21 14:25:41 -08:00
evanpelle 92372b770b Merge branch 'v26' into v27 2025-11-21 14:08:43 -08:00
evanpelle 7d7ef40ae4 Merge branch 'v26' 2025-11-21 14:08:27 -08:00
evanpelle 44920f030b Bugfix: Kick player in private lobby not working
Nginx was stripping query params when routing requests to workers, so the creatorClientID param was stripped when creating a private game. This caused the game server to not know who the lobby owner was, so it rejected the kick requests.
2025-11-21 14:06:31 -08:00
Aotumuri b0e6661c97 mls (v4.9) (#2487)
## Description:

mls for v27
Version identifier within MLS: v4.9

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

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

aotumuri
2025-11-21 11:51:14 -08:00
evanpelle 26b965468f Only open news modal if the user has seen an existing version. This prevents brand new players from seeing the news popup.
Move NewsButton into NewsModal.ts
Refactor & simplify NewsButton
2025-11-21 10:57:07 -08:00
evanpelle 9bd2b54ad7 Merge branch 'v27' 2025-11-20 20:11:56 -08:00
evanpelle c5b1bca6c6 fix z-index of homepage modals & buttons so modals always appear above the buttons 2025-11-20 19:35:07 -08:00
evanpelle cf7e658d3d Remove yenisei because there is no source in map-generate/assets so the map cannot be rebuilt 2025-11-20 19:18:37 -08:00
Lavodan f4a7788cdf Change wiki site from miraheze to openfront.wiki (#2483)
## Description:

Change the wiki site from the inactive openfront.miraheze.org to the new
openfront.wiki link.
Ideally for v27?

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

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

Lavodan
2025-11-20 16:50:10 -08:00