← Soren Nguia

Clash-Royale Simulation Suite

Perceptionframes → card classesPolicyaction selectionDeterministic tickinteger-only · 20 tpsTelemetrytraces · replays · evalnext observationagentenvironment
Example 1 of 3
Match 0412 · tick 1,284
Golem push, double-lane pressure
golembaby-dragonmega-minion▸ elixir 7tower 100%opp 62%
Deterministic re-run from the seed reproduces this frame bit-for-bit.

A deterministic, integer-only Rust testbed for agent evaluation and RL over Clash Royale - perception, simulation, and code-QA in one crate.

20 tpsbit-reproducible loop125+heterogeneous entity types42.5 msp99 @ 3,000 entities97.89%perception val accuracy

Author

  • Soren Nguia1

Affiliation

  1. Independent Researcher (Seoul)

Started

Jan. 2026

Abstract

The Clash-Royale Simulation Suite is a deterministic, integer-only Rust environment that models the game as a ground-truth testbed for agent evaluation and reinforcement learning. A fixed-point tick loop runs at 20 ticks per second and is bit-reproducible from a seed, so a recorded match replays frame-for-frame. It models 125+ heterogeneous entity types declared in JSON policies rather than hard-coded in Rust, which keeps the rule set auditable and lets the same engine drive both single-match RL and large headless evaluation batches.

Around that core sit two supporting systems: a perception pipeline that reads real match frames into card classes over ONNX Runtime with CoreML/ANE acceleration, and a data engine that mines and de-duplicates training frames at video-rate. The whole crate is exposed to Python via PyO3, and its own source is kept honest by a code-QA layer wired to an MCP code knowledge graph. Every headline number below is a measured result with its measurement context stated, not a projection.

42.5 ms
p99 latency @ 3,000 entities
24.743 ms
p99, 5,000 matches in parallel
22.3 FPS
batch-mode perception, full match
440,056
frames mined in 33 min

The Problem

Evaluating an agent - or training one with RL - needs an environment that is fast, reproducible, and gives an unambiguous ground truth for what actually happened on each step. A live game client provides none of these: it is non-deterministic, wall-clock bound, and its internal state is not observable. Floating-point physics makes the problem worse, because rounding differences accumulate across ticks and two “identical” runs diverge, which destroys the paired comparisons that credit assignment and regression testing depend on.

The suite answers this by rebuilding the mechanics as an integer-only state machine. There are no floats on the hot path, so a match is a pure function of its seed and action stream. That single property is what makes 10,000 matches comparable, makes a regression corpus meaningful, and lets telemetry be trusted as ground truth rather than an approximation.

System Overview

The simulator is organised as a data-driven core: entity behaviour lives in JSON policies, and the Rust engine interprets them against an integer world state each tick. The “125+ entity types” headline is a deliberately conservative floor on distinctly-behaving units; the JSON data underneath it currently declares 214 characters, 108 buildings, 27 spells, 169 projectiles and 140 area-effect objects. The diagram below is the suite’s own architecture figure.

Simulator architecture: JSON entity policies interpreted by an integer-only Rust tick loop, exposed to Python via PyO3
Simulator architecture - 125+ entity types declared in JSON, stepped by the deterministic integer tick loop and surfaced to Python through PyO3.

The Deterministic Tick Loop

Integer determinism

State advances at 20 tps in fixed-point integer arithmetic. Because the loop contains no floating-point operations, the same seed and action sequence always produce the same trajectory, bit-for-bit, across machines. This is the contract every other component leans on: recorded matches replay exactly, paired A/B rollouts are genuinely paired, and any divergence in output is a real behavioural change rather than numerical noise.

Scale & throughput

Two measured regimes bound the engine, both from the most recent benchmark, taken after the engine’s mechanics expanded, on a single Apple M1 Pro core. At 20 tps a tick has a 50 ms budget, and each regime is quoted against it. In the stress regime, a single match holding 3,000 concurrent entities sustains a p99 step time of 42.5 ms - 85% of budget. In the batch regime, 5,000 matches stepped in parallel have a batch p99 of 24.743 ms - 49.5% of the same budget - and 10,000 matches cost 3.94 µs each (mean per match, per tick), with a batch p99 of 53.151 ms, which is over the budget. Both batch points are measured directly. The 9,445-match single-core ceiling at 20 Hz is an estimate from linear interpolation between those two measured points, not a direct run at 9,445.

The defensible claim is not that batching is free: the batch cost grows with the number of matches and crosses the 50 ms tick budget between 5,000 and 10,000 of them. An earlier, smaller engine snapshot measured the 10,000-match batch at p99 = 36.7 ms; the figures on this page are the later benchmark of the current, larger engine, which is the one that matters for current-engine claims.

5,000 matches fit inside the 20 Hz budget on one core (24.743 ms p99); 10,000 do not (53.151 ms), and the interpolated ceiling is about 9,445 matches per core - an estimate, not a direct run.

Perception Pipeline

To connect the simulator to real play, a perception stage reads match frames into card classes. The classifier is a MobileNetV3-Small reaching 97.89% validation accuracy. The inference path was rewritten from Python into Rust over ONNX Runtime with CoreML/ANE acceleration. In batch mode - every frame sends a fixed [5, 3, 224, 224] tensor that CoreML compiles once for the ANE - that is ~9 ms per card and 22.3 FPS over an 8,702-frame match.

A second binary, gatekeeper mode, puts a per-slot state machine in front of the model. Each of the five hand-card slots is reduced to an 8×8 RGB fingerprint; a slot locks once inference returns ≥90% confidence and unlocks when its fingerprint drifts by more than 6%. Over the same match it skips 49% of inference calls - 21,304 of 43,510. Offline it is slower wall-clock, 11.6 FPS against batch mode’s 22.3, because variable batch shapes make CoreML recompile its execution plans. It pays off where call count, not throughput, is the cost: a live stream, a shared accelerator, a per-call API.

Perception pipeline, two modes: batch mode infers all 5 card slots every frame; gatekeeper mode locks unchanged slots and sends only unlocked slots to MobileNetV3-Small over ONNX Runtime with CoreML/ANE
Perception pipeline - batch mode (22.3 FPS, every slot inferred) beside gatekeeper mode (49% of calls skipped, 11.6 FPS offline).

The Data Engine

The classifier is only as good as its training set, so the suite includes a data engine that mines frames from match video. It processed 440,056 frames in 33 minutes - about 221 FPS - using zero-copy crops and rayon parallelism, then applied near-duplicate filtering for a 61.1% dataset reduction. A Real-ESRGAN upscale and a 4-stage augmentation pass expand the survivors into a ~300k-image training set.

Data engine: match video mined at 221 FPS with zero-copy crops and rayon, near-duplicate filtered for 61.1% reduction, then upscaled and augmented
Data engine - frames mined at ~221 FPS, near-duplicate filtered (61.1% reduction), then Real-ESRGAN upscaled and augmented into the training set.

Code-QA & Knowledge Graph

A codebase this size - ~88,000 lines of Rust (87,876 measured on 2026-06-11: roughly 50k of production code spread over the nine files at the crate source root, plus about 37.9k of extracted regression tests) - needs its own quality discipline. It is wired to an MCP code knowledge graph holding roughly 28,675 nodes and 35,340 edges over the full crate as of the same date, which answers blast-radius and call-chain questions before a change lands.

Correctness is pinned by 1,518 #[test] functions across 59 mod *_audit_tests blocks, gated by CI. Those tests were lifted out of the source files into src/tests/ on 2026-05-17, so they are no longer inline, and they trace back to 595 git-tracked audit files - 400 of them entity audits over 248 distinct cards.

Card data is checked against outside sources too: the Clash Royale Fandom MediaWiki API, through a 181-page offline cache, and 60+ creator-video transcripts for newer cards the wiki covers thinly. Across 10 hero cards, the transcripts surfaced 4 retroactive closures and 6 divergences still pending frame capture - none of which the wiki and JSON alone caught.

~88k
lines of Rust: ~50k prod / ~37.9k tests
28,675 / 35,340
graph nodes / edges, 2026-06-11
1,518
CI-gated tests in 59 audit modules
Codebase Memory knowledge graph browser showing functions, structs, and call edges, with node-type filters and a symbol list; the header reads 1,054 nodes / 1,731 edges over 10 indexed files
The code knowledge graph, queried for blast-radius and call-chain before a change lands. Note the header: this capture is a partial index - 1,054 nodes / 1,731 edges over only 10 indexed files, taken against the April snapshot of the crate. The full graph over the current crate is ~28,675 nodes / ~35,340 edges (2026-06-11).

Telemetry & Replays

Because every match is deterministic, telemetry is exact: a seed plus an action stream fully reconstructs a game, so the dashboard replays and inspects any recorded match without storing frames. Capturing the full state of a tick costs about 5 µs (measured 2026-05-19), so instrumentation stays cheap relative to the 50 ms tick budget. The live telemetry dashboard is available at clash-telemetry-web.vercel.app.

Telemetry dashboard: a replayed match on the left with card slots, per-frame batch-vs-gatekeeper latency graph, and cumulative skip-rate stats on the right
The telemetry dashboard - a deterministic replay frame-synced with per-frame batch-vs-gatekeeper latency and the running gatekeeper skip rate for the session shown.

Limitations

Conclusion

The Clash-Royale Simulation Suite turns a fast, chaotic game into a controlled instrument: an integer-only, bit-reproducible core that runs 5,000 parallel matches inside the 50 ms tick budget (24.743 ms p99) and 10,000 at 3.94 µs per match (53.151 ms batch p99), a Rust perception path that reads real frames at 22.3 FPS, and a data engine that feeds it - all held together by a code-QA layer that treats its own source as something to be measured, not trusted. It exists to make agent evaluation and RL over this domain honest and repeatable.

Source, write-ups, and the live dashboard are linked below.