Games rebuilt for the browser · Part 2 of 2
Porting San Andreas to a browser tab
web-gta recreates Grand Theft Auto: San Andreas in a browser tab. It streams the original 900 MB world as you drive and runs the game’s original mission scripts. The interesting problems are less about drawing San Andreas than about feeding it: getting a world that size to the right place on time, on hardware as small as a phone, without a car at highway speed outrunning what’s loaded.
The game’s map, models, scripts, and audio belong to Rockstar, so none of them appear on this page. Instead, every figure runs the port’s own TypeScript on small inputs made up for this post.
What runs where
Keeping an open world smooth in a browser starts with keeping its 900 MB off the thread that draws frames. The main thread runs the simulation at a fixed 60 steps a second (CJ, traffic, pedestrians, police, and the mission script interpreter), along with Rapier physics compiled to WebAssembly and a three.js renderer capped at 30 frames a second, the same limit the original PC game’s frame limiter used. Some of the game’s effects count frames rather than seconds, so matching the original rate keeps them looking right.
Everything that touches storage or the network lives in background workers. A chunk fetcher downloads, verifies, caches, and unpacks the world’s 1,943 chunks, each named by the hash of its contents. A world streamer turns them into building placements, models, and collision meshes for the area around you.
Most gameplay is ported from gta-reversed, the community’s C++ reconstruction of the PC game. Where gta-reversed only has a stub, the port cross-references the disassembled iOS version to recover the missing logic.
Streaming the map
The world arrives in windows: 180 units of full detail, surrounded by low-detail models out to 1,000 units. A small state machine, the region follower, watches a point just ahead of the car. Once that point drifts 30% of a radius from the current window’s center, it asks the worker for the next window. If the car gets 85% of the way to the edge before the new window arrives, the game pauses on a streaming hold rather than let CJ drive off the end of the collision mesh.
On the made-up straight road above, at 70 m/s with 2-second windows, each window lands just in time and the car never stops. At 2.1 seconds, that extra tenth of a second pushes the car past the 85% line, and the holds start repeating.
That cliff showed up in the real game. Players reported freezes on fast highways, so a scripted probe drove a 90-second diagonal across the map at 70 m/s. It logged 53 streaming holds, 28.6 seconds of standing still. The cause was in the streaming worker: it decoded every model in each new window and only then threw away the ones already in memory. Skipping already-loaded models before decoding, and caching collision data, cut window loads from 1.3–1.9 seconds to 60–350 milliseconds. The same drive now has one 1.4-second hold, at the start. The probe also turned up a leak of Rapier physics bodies that no unit test had caught.
Running the original scripts
San Andreas’s story missions, side jobs, cutscene timing, and ambient events are all compiled into Rockstar’s SCM bytecode: main.scm plus roughly two hundred mission scripts loaded on demand. web-gta runs that original bytecode. Each script thread steps through instructions (a two-byte opcode followed by type-tagged arguments) until it reaches a WAIT, then sleeps until the game clock catches up. Set the wait to a full second and the thread spends nearly all its time asleep, which is what most of the game’s background threads are doing at any moment.
The interpreter is the small part. The real work is everything behind the opcodes. The original scripts use 1,570 distinct commands in 416,669 places, and each command needs an engine system behind it to spawn a car, give a pedestrian a task, or start a phone call. When a thread hits one of the 127 commands that aren’t ported yet, the interpreter logs the skip, reports “nothing found” for its outputs, and lets the mission carry on instead of crashing the script.
Traffic from the original arithmetic
CCarCtrl::SlowCarDownForOtherCar for whatever gap the grey one leaves.Ambient traffic runs a direct port of CCarCtrl::SlowCarDownForOtherCar. When another car enters the zone ahead, the follower caps its target speed at about 62% of cruise and slows in proportion to the closing gap.
Right behind a bumper, three branches of the original formula produce a quirk you can watch above. A car stopped 8.5 units behind the lead car stays put: it’s in the outer stop zone. Put it one unit closer and it falls into a middle branch that sets a crawl speed of 1, so it creeps forward until it reaches the inner stop zone at 5.9 units. Being closer is what makes it move. Both zones come straight from the branching arithmetic, ported here from reVC, the community’s reconstruction of Vice City, San Andreas’s predecessor.
Fitting on a phone
A desktop open-world game in a mobile browser hits three limits at once: how many pixels the GPU can fill, heat, and memory. To protect the frame rate, an adaptive-resolution controller watches the median frame time over three seconds and steps down a resolution ladder (100% → 85% → 72% → 60%) whenever rendering stays below 26 frames a second.
On a phone limited by its GPU, stepping down to 85% brings back smooth frames immediately. On a phone limited by its CPU, fewer pixels make no difference, so the controller undoes the step after one window and backs off, waiting twice as long before each retry rather than blurring the screen for nothing.
The PC release stores textures in DXT, a compressed format few mobile GPUs can read directly, so the port decodes them on the CPU as chunks arrive. Decoding to 16-bit colour (RGB565, or RGB5A1 when a block uses DXT1’s transparency mode) instead of 32-bit cut texture memory around Grove Street in half, from 93 MB to 46 MB. Swap the block’s two endpoint colours to see that mode switch on: when the first endpoint is no larger than the second (c0 <= c1), index 3 means transparent.
Finally, a thumb on glass has no resistance to push against, so the touch stick uses the iOS version’s curved steering. The first third of the stick’s travel turns the wheels only a fifth of the way, for small lane corrections at speed, and the outer range opens up to full lock.
The census in the script figure is the honest measure of where the port stands. Every one of the 127 unported commands is a gap a mission can fall into, and each has to be filled the same way the traffic and streaming code were: from the original arithmetic, checked against the original game.