Teaching an agent to duel in a game simulator I rebuilt from source
Can a reinforcement-learning agent learn a fast 1v1 shooter-and-sword duel from nothing, in a simulator that matches the real game frame for frame?
I re-implemented the rules of GunZ: The Duel as a headless, deterministic C++ simulator, checked each rule against the real client at 250 FPS, and trained agents with self-play PPO. Over 33 training runs and 4 drills in two days, the agent went from standing still to beating every scripted and earlier learned opponent, including the sword-only agent from the sixth run that had topped every evaluation since. Most of the runs failed, and each failure is in the journal below with what it taught.
- game rules verified against the real client
- 23 / 29
- simulated ticks per second (~900x real time)
- ~230k
- training runs and aim drills
- 33 + 4
- best Elo, above every agent and baseline (random play = 1000)
- 1595
One evaluation (after a33): 39 players, 40 matches per pair from the map's spawns, 29,640 matches. Elo is anchored at random play = 1000; bars are 95% bootstrap intervals. A selection of the players is shown.
Each run's best checkpoint against a6 in the same evaluation, 40 matches each (win 1, draw 0.5), so each score is good to about ±0.15. The dashed line is an even match. Earlier gun agents scored 0.00–0.46, mostly by drawing rather than fighting.
Environment
- A C++ re-implementation of the game's movement, collision, katana, guard, shotguns, wall moves and match rules, each one written from the client's source and cited by file and function. No game code or assets are copied.
- A rule counts as verified only when a parity test matches it against recorded traces from the real client at 250 FPS, with the tolerance and sample size recorded. 23 of 29 rules pass; the rest are marked unverified, never loosened to pass.
- Deterministic by design: fixed tick, seeded random numbers, no wall clock, no rendering. The same seed and actions give the same state hash.
- A Python environment (PettingZoo API) over a C interface, stepping 32 matches in parallel on a pool of worker threads. 1v1 Team Deathmatch on one map (Hall), 150 HP / 150 AP, a katana and two shotguns.
Approach
- PPO with a shared actor-critic MLP: seven movement and attack keys, a weapon slot, and continuous turn and pitch, one decision every 4 ticks (62.5 per second). Reward for damage, kills and rounds, plus potential-based shaping for distance, facing and aim.
- Opponents mixed per match: scripted baselines (a target that stands still, a strafer that circles, a chaser that runs in with the katana, random play), the agent's own snapshots, and frozen earlier agents.
- Each run is evaluated in a round-robin pool of every earlier best checkpoint: 40 matches per pair from the map's real spawns, Elo anchored at random play = 1000, 95% bootstrap intervals. Results are compared only within one table, because Elo is relative to the pool.
- Every run has a written hypothesis and success test before it starts, and a result entry after, including the failures. Training and evaluation run unattended through a local job agent that writes a report; replays of the best checkpoint are recorded after every run.
What it showed
- Curriculum beat reward tuning. Every reward tweak aimed at a missing skill (aim reward, range band, draw cost) failed; what worked was changing who the agent plays and where matches start.
- When a run failed, a small controlled drill found the real blocker faster than another full run. The aim drills showed aiming was learnable, and later showed the shooter only led moving targets.
- Warm starts are fragile: a 1.8% press probability on new keys was enough to erase a learned skill, and a KL anchor applied everywhere froze the agent. Applying the anchor only to the matches that need the skill fixed it.
- Training against one strong opponent causes forgetting; a small league of the current best agents fixed it, and refreshing the league each round kept the rating climbing.
- Training numbers can look fine while the policy gets worse, so the checkpoint is chosen by a separate evaluation, not by the training curve.
Limits
- One map, 1v1 only. The project's goal is 2v2 and then 4v4, which needs team rules and multi-agent training.
- Six rules are not yet verified against the client, including the match rules (rounds, death, respawn). Network latency is not modelled.
- Elo is relative to each evaluation's pool. With 40 matches per pair, a head-to-head score is only good to about ±0.15, and most runs have two seeds.
- Every opponent is scripted or another agent. No human has played the agent yet, so its strength against people is unknown.
- The late decay seen in one a31 seed is not explained.
The final agent against a6, the sword-only champion of the first stage, from the map's spawns. a6 weaves in with jumps and guard; the agent holds about 350–550 units away, switching between its two shotguns, and wins with 60 HP left.
Top-down, long axis left to right. Filled dot with a cyan ring: the trained agent; hollow dot: its opponent. Lines show facing, faint lines the last 2 seconds of movement, and short strokes shotgun pellets (cyan when the agent hits). The floor plan is a simplified drawing: darker bands are the raised walkways along each side, with steps down to the sunken centre.
Journal
Rebuilding the gameNov 17
- SimulatorPort the rules, then prove them against the real clientWorked
- Tried
- Ported collision, movement, jumping, dashes, katana combos and guard, shotgun pellets, aim, wall runs and match rules from the client's source into C++, and built a timing lab that records the real client's frame-by-frame traces for scripted inputs.
- Why
- An agent trained on rules that are slightly wrong learns habits that fail in the real game, so parity comes before training.
- Result
- 23 of 29 rules pass their parity tests at 250 FPS with recorded tolerances (for example, sliding along a wall within 0.73 units over 12 traces). The match rules and player push-apart are implemented but not yet verified.
Stage 1: the katanaNov 18
- a1, a2Full self-play from scratchFailed
- Tried
- All keys and weapons, mostly against its own snapshots; a2 added distance and facing shaping.
- Why
- The simplest baseline: see whether self-play alone finds fighting.
- Result
- Nothing learned. Every match against random play and itself was a draw, every match against the chaser a loss. Two agents that never attack only teach each other to draw.
- a3, a4Katana only, with scripted opponentsPartly
- Tried
- Masked out the guns and the confusing keys, and mixed in a still target and the chaser. a4 repeated a3 with ten times the budget.
- Why
- Give the agent opponents that reward attacking, and fewer ways to cancel its own attacks.
- Result
- a3 learned to fight (beat the chaser 81% of the time). a4, the same setup, collapsed after 100 updates when its action noise shrank to almost nothing. One success in two runs is not a result.
- a5–a7Seed sweepWorked
- Tried
- The a3 setup on three more seeds.
- Why
- Measure how often the recipe works before building on it.
- Result
- 4 of 4 seeds learned (Wilson 95% interval 0.51–1.00, so it may still fail up to half the time). a6 beat random play and the chaser in every evaluation match and became the reference opponent for the rest of the project.
Stage 2: the shotgunsNov 18
- a8Add the shotguns to the katana agentFailed
- Tried
- Warm-started from a6 with the two shotguns unlocked.
- Why
- Reuse the movement and timing a6 had already learned.
- Result
- No gain, and later checkpoints got worse: the last one won only 15% against its own starting point. Switching weapons was punished before aiming was learned.
- a9–a11Shotguns onlyFailed
- Tried
- Locked the katana, then removed the chaser, then added an aim reward.
- Why
- Isolate the gun skill.
- Result
- Three failures, three causes. a9 exposed a bug: 30 observation features that never varied in stage 1 had a near-zero variance, so holding a gun pushed them to the clip limit (fixed). a10 aimed far too high; a11's aim reward made it overshoot low and stop approaching. The lesson was to stop adding variants and test one thing.
- d1, d2A controlled aim drillWorked
- Tried
- Scripted the movement and facing, and let the agent learn only pitch and when to fire, against a still target.
- Why
- Check whether PPO can learn the aim at all.
- Result
- Both drills learned to aim within 7 minutes. Aiming was not the blocker; getting into range facing the target was.
- a12–a14A range curriculumPartly
- Tried
- Started matches close together and moved the start apart each time the agent kept winning, up to the full spawn distance; then a mixed field of opponents.
- Why
- The agents never found the target from the real spawns, 4,250 units apart.
- Result
- a13 reached the full distance and killed a still target from the spawns in about 8 seconds. Against the chaser it hit 5 of 120 pellets: it could hit a target that stands still, not one that closes in and circles.
Hitting a moving targetNov 18–19
- d3A moving-target drillWorked
- Tried
- An agent that never moves, learning turn, pitch and firing against a new strafer and the chaser.
- Why
- Test whether hitting movers is learnable in this simulator at all.
- Result
- Yes: 99% against the strafer and 81% against the chaser in 12 minutes.
- a15–a18Four ways to beat the chaser from the a13 lineFailed
- Tried
- Moving targets in the mix, a reward for keeping a 300–600 unit band, a chaser with reduced health that grows as the agent wins, and close starts against the chaser.
- Why
- Close the chaser gap without losing the approach.
- Result
- All four failed (0–17% against the chaser). Backing off cannot work: in the game, backward speed is 450 against a run speed of 630. A probe showed one point-blank blast does 156 damage, so the gun was fine; the agent aimed at the ceiling and its feet. That lineage was stuck.
- a19–a21Restart from the drill shooterFailed
- Tried
- Used d3 as the start and unlocked movement. a20 added a KL anchor to the drill policy and a value warm-up; a21 added a cost for draws.
- Why
- d3 already hit movers; it needed to learn to walk.
- Result
- a19 lost the skill immediately. Diagnosis: new keys started at a 1.8% press probability, about one press per second, enough to wreck the warm start (the frozen start scored 32% against the chaser with that setting and 81% with a lower one). The fix kept the start, but the agents stood still and drew; the draw cost did not change that.
- a22, a23, d4Grow the range from close, and fix the drillPartly
- Tried
- The range curriculum on top of a20's recipe; then a curriculum counted on the still target only; then a drill that adds a still target.
- Why
- Teach the approach without forgetting the shot.
- Result
- Found a curriculum bug: the chaser and strafer walk to the agent, so their wins were promoting the range (fixed and tested). Then found that d3 was 0.24 rad off on targets that do not move: it had learned to lead moving targets only. The new drill d4 hits both.
Walking and shootingNov 19
- a24, a25The recipe from the new drillPartly
- Tried
- The a23 recipe starting from d4.
- Why
- d4 is the first shooter that hits both still and moving targets.
- Result
- The first gun agent that walks to its target and wins from the real spawns. But the chaser score decayed as the walk was learned (72–79% early, 9–17% at the end), the same late decay seen in eight earlier seeds.
- a26, a27Anchor only where the skill is neededWorked
- Tried
- Kept the KL anchor to the drill only on chaser matches, so the agent is free elsewhere; then started half of the chaser matches far apart.
- Why
- Protect the chaser skill without freezing the walk.
- Result
- The decay weakened in a26 and was gone in a27. a27 became the first gun agent level with the scripted chaser in Elo (1213 against 1223), winning 64–67% against it from the real spawns.
- a28Train from the map's real spawnsFailed
- Tried
- A quarter of the chaser matches started from the map's own spawn points.
- Why
- Close the gap between training starts and evaluation starts.
- Result
- No measurable gain (50% head to head with a27). The gap was not the limit.
A league against the best agentNov 19
- a29Train against the frozen katana championPartly
- Tried
- Added a new opponent kind: frozen checkpoints of earlier agents. a6 took 35% of matches.
- Why
- a6 beat every gun agent 90–95% of the time, and no gun agent had ever trained against it.
- Result
- The score against a6 rose from 8% to 34% in training and 24% in evaluation. But the late checkpoints forgot how to beat the earlier gun agents (25–35%), because a6 was the only learned opponent left.
- a30A small leagueWorked
- Tried
- a6 at about 22% of matches, with the two previous best gun agents as the rest of the league.
- Why
- Keep the climb against a6 while the earlier agents stop the forgetting.
- Result
- The first agent rated above a6: 1493 (1462–1522) against 1424 (1398–1450), intervals apart. It beat every other player at 69% or more, with a6 still slightly ahead head to head (42%).
- a31, a32Refresh the league each roundWorked
- Tried
- Each round starts from the best checkpoint, and the league is replaced by the agents it scored lowest against, with a6 kept.
- Why
- Train against whatever the current best still loses to.
- Result
- a31 beat a6 72% head to head; a32 rated 1583 (1556–1609), clear of a31's best at 1479 (1457–1509) in the same pool. One a31 seed decayed late (1257) while its training numbers still looked fine; the evaluation's checkpoint choice caught it, but the cause is open.
- a33One more round: still ahead, by lessWorked
- Tried
- Same recipe from a32's best, with its three closest rivals as the league.
- Why
- Check whether the league rounds are still paying off.
- Result
- The best checkpoint rated 1595 (1572–1623), clear of a32's best at 1526 (1502–1552) in the same pool, and beat every player head to head (a6 57%, a32's best 62%). In replays it won all eight test matches, three of them against a6. But the gain per round shrank (+126, +104, +69 Elo over the previous best), so the league has levelled off.
Next
- Agent B: warm-start an agent from about 380 recorded human matches (behavior cloning), then train it with the same PPO, budget and evaluation pool, and report the comparison with its uncertainty, including if the difference is not significant.
- Export simulated matches as game replays, so agents can be watched in the real client.
- Verify the remaining rules, then move to 2v2 with team rules.
- C++
- Python
- PyTorch
- PPO
- Self-play
- PettingZoo
- ctypes
- Elo + bootstrap
- PowerShell