← Home

Training a neural network to play Counter-Strike 1.6 from pixels

2026-12-02

I have logged around 5,000 hours in Counter-Strike and peaked in the top 5% of players worldwide. Standard game bots cheat by reading exact player coordinates from server memory and adding random aim noise so they feel beatable. I wanted to train a neural network on my Mac Mini that plays CS 1.6 under human constraints: seeing only the rendered screen pixels and outputting mouse and keyboard commands at 25 Hz, scoped to 1v1 rifle duels on aim_map.

You can play a 15-round match against the model in your browser at cs.dhaiwat.com, and the training code and weights are open source on GitHub.

A lockstep CS 1.6 engine for RL

OS screen capture and synthetic mouse events run at 1x real time and drop frames under load, which is far too slow and noisy for reinforcement learning. I patched the open-source Xash3D FWGS engine (cs16-client with ReGameDLL_CS) to step deterministically in lockstep: wait for an action, advance simulation, render offscreen at 640x480, and pass RGB frames and inputs across POSIX shared memory. On an M4 Pro Mac Mini, the server steps at ~150x real time and a rendering client runs headlessly at 10 to 18x real time.

Why behavioral cloning scored zero kills

My first attempt was pure imitation learning. I parsed 14.6 hours (1.32 million frames) of professional first-person POV demos and trained a 12.3M-parameter IMPALA CNN + LSTM policy to predict the player's next keys and mouse movement.

Validation loss dropped well on walking, crouching, and movement keys, while mouse yaw and pitch barely improved (5% over predicting the marginal baseline). Put into an actual deathmatch against the easiest bots, the policy scored 0 kills. It strafed and crouched with pro-like cadence while drifting into walls and shooting the floor. Two things went wrong: pros never walk face-first into walls, so demos contain zero examples of how to recover once you drift off course, and brief mouse flicks carry very little gradient compared to holding movement keys. At 160x120 downsampled resolution, distant enemies were also only a couple of pixels wide.

Foveated vision and recurrent PPO

To keep training fast on a single Mac without losing long-range detail, I gave the network foveated vision: a low-resolution view of the full screen for spatial awareness paired with a sharp crop around the crosshair. Starting from there, I trained with recurrent PPO across a staged curriculum, using privileged scripted teachers (an A* navigator and an aimer that read game state only during training, never during play) alongside kill and damage rewards:

At 0 ms observation delay, that checkpoint beat me 61 to 27 over 88 rounds. That score comes with a clear asterisk: seeing frames instantly lets the model fire ~160 ms after an enemy appears on screen, which is faster than human reaction time.

Adding a 160 ms reaction delay and target leading

To make the fight fairer, I delayed every frame shown to the policy in 40 ms steps up to 160 ms, promoting checkpoints only when they passed 300-round greedy evaluation exams (and rolling back updates when noisy sampling hid regressions in greedy play).

At 160 ms, a new flaw showed up: against anyone who kept strafing, the model aimed ~3 degrees behind them, shooting at where they had been 160 ms ago. A single crosshair crop has no motion cue. Stacking two consecutive crosshair frames let the CNN read target velocity directly from pixels and lead moving opponents. After porting the headless engine bridge to Linux + Mesa to finish this phase on a rented GPU box, median time-to-kill on strafing targets dropped from 2.97s to 0.91s, and win rate against opponents that strafe and shoot back jumped from 39.6% to 69.2%.

Running the arena in the browser

To let people play against it without installing Half-Life or Python, I exported the policy to ONNX on WebGPU and compiled two Xash3D WebAssembly instances into one browser tab at cs.dhaiwat.com, connected over an in-memory loopback network. One instance renders your game; the second runs hidden in the background to render the bot's own pixel view 25 times a second and feed WebGPU inference locally on your machine.

← Home