Skip to content

Seed AI RNG deterministically to prevent replay desync#1956

Open
FlexApex wants to merge 1 commit into
Return-To-The-Roots:masterfrom
FlexApex:fix-ai-rng-determinism
Open

Seed AI RNG deterministically to prevent replay desync#1956
FlexApex wants to merge 1 commit into
Return-To-The-Roots:masterfrom
FlexApex:fix-ai-rng-determinism

Conversation

@FlexApex

Copy link
Copy Markdown

Problem

The AI uses a non-deterministic random source, which breaks replay determinism. Two distinct issues:

  1. AI::getRandomGenerator() is never seeded from the game seed. It is lazily seeded from std::random_device on first use, so in replay mode it starts from a different state than during the original match. Any AI decision that draws from it diverges, desyncing the replay.
  2. AIPlayerJH::TrySeaAttack() allocates its own generatorstd::mt19937(std::random_device()()) — on every call, which is non-deterministic by construction.

The net effect: any replay in which an AI evaluates a sea attack desyncs ("The played replay is not in sync with the original match"), even though the simulation is otherwise correct.

Fix

  • Seed AI::getRandomGenerator() from random_init in GameClient::StartGame(), right where RANDOM.Init(random_init) is already called — so replay and live runs share the same AI RNG sequence.
  • Replace the local std::mt19937 in TrySeaAttack() with the shared AI::getRandomGenerator().

3 lines across 2 files; no behavioural change to a live game beyond making the AI RNG reproducible.

How it was found

Surfaced while running headless replay verification on AI-battle recordings (related to the headless replay player in #1950) — a desync verifier flagged AI sea-attack frames as the first point of divergence, with the RNG checksum being the differing field.

The AI used two separate non-deterministic random sources:
- AI::getRandomGenerator() in random.cpp was seeded with std::random_device
  on first use, so it started at a different state in replay mode than during
  the original game run, causing AI decisions to diverge
- CheckSeaAttack in AIPlayerJH.cpp allocated its own std::mt19937 seeded
  with std::random_device()() every call, doubly breaking determinism

Fix: seed AI::getRandomGenerator() from the game's random_init at StartGame
(same point where RANDOM.Init is called), so replay and live game use the same
AI RNG sequence. Replace the local mt19937 with the shared AI generator.
@Flamefire

Copy link
Copy Markdown
Member

any replay in which an AI evaluates a sea attack desyncs

There is no AI in replays. So how exactly have you seen errors?

}
}
auto prng = std::mt19937(std::random_device()());
auto& prng = AI::getRandomGenerator();

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.

This one looks correct though

@FlexApex

Copy link
Copy Markdown
Author

any replay in which an AI evaluates a sea attack desyncs

There is no AI in replays. So how exactly have you seen errors?

In my own fork I use ai-battle with a small eval framework to improve the AI:
See recent commits here: https://github.com/FlexApex/s25client/commits/master
There it lead to an .rpl desync.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants