This is an AI I built to play the
classic vector arcade game Tempest. Now,
it wasn't programmed to play Tempest. It
learned how from scratch using deep Q
reinforcement learning. This means the
AI started with no knowledge of the game
and through trial and error figured out
how to navigate Tempest's chaotic
spinning playfield like a pro. It's a
bit like teaching a robot to ride a bike
by letting it fall off a few thousand
times until it learns not to fall off.
Today, I'm pulling back the curtain to
show you how to build this AI from the
ground up. We'll walk through the
process step by step, from the basics of
reinforcement learning to a system that
can master Tempest, just like assembling
a machine out in the shop. By the end,
you'll have a working AI that can tackle
one of the toughest arcade games around.
And better yet, you'll not only have the
tools, but you'll also understand the
principles behind them. Now, instead of
getting bogged down in the details of
neural network math like back
propagation and gradient descent, we'll
focus on the fun parts, building a
learning system that actually works.
We'll leverage powerful libraries like
PyTorch and Stable Baselines 3 to make
this happen. So, you can see real
results without actually needing a PhD
in mathematics. PyTorch will handle the
neural network heavy lifting, while
Stable Baselines 3 provides pre-built
reinforcement learning algorithms,
making our job way easier. But don't
worry, you won't be left in the dark.
We'll still cover everything you need to
know and to understand how the system
works, but without losing sight of the
big picture. Think of as using a
high-powered toolkit to build a race
car. You don't need to forge every nut
and bolt yourself, but you need to know
how the engine works and what makes it
roar. You might wonder why I chose
Tempest for this AI project. It's not
because Tempest is an easy game. Far
from it. Tempest is a beast to master
with fast-paced action and complex
patterns. Its spinning 3D playfield and
relentless enemies make every second a
test of skill and reflexes. But here's
the thing. I've been hooked on it for
more than 40 years, ever since I was a
kid dropping quarters in the arcade. And
I even got my own first machine back in
1987. I've also since set the world
record on it. So, when I decided to dive
into AI, Tempest was the obvious pick.
Most humans are notoriously bad at the
game, and it's a tough challenge for an
AI to crack, which is precisely why it's
perfect for this project. It's a chance
to push AI to its limits and to learn
something amazing in the process. So,
let's see how we can teach an AI to play
like a pro. As we dive into building our
AI system, I'll be drawing and
continually updating a block diagram as
we go to show you exactly how the pieces
all fit together. Think of it as a crude
blueprint for our AI. This diagram will
evolve as we add components, making it
easier to visualize the flow of data and
decisions. First up, the box for the
game Tempest itself. But what will we
actually use for Tempest itself? Well,
this isn't some Code Bullet video where
I write a simple version of a trivial
game and then a simple AI to master it.
Now, don't get me wrong, Code Bullet is
great. He's entertaining and fascinating
to watch. In fact, here's a 3second
sample. Are you not entertained? Of
course you are. But did you learn
anything? No. Code Bullet style is
awesome for inspiration, but we're going
deeper. Building a real AI for a real
arcade classic. So, let's get busy
drawing some boxes so we can figure out
how this thing is actually going to
work. Running Tempest on its original
1981 arcade hardware sounds cool, but it
would be a nightmare of wiring and
memory access headaches. Instead, we'll
use the MIAME emulator to run the exact
same game code, which perfectly mimics
the Tempest hardware. MAME, or the
multiple arcade machine emulator, is a
free open- source tool that recreates
the behavior of the arcade hardware in
software, letting us run Tempest's
original 1981 code with pixel perfect
accuracy. This gives us our first key
piece. A window into the game's world,
letting us see the action unfold. But
here's the big question. How do we get
the AI to see and understand what's
happening in Tempest's chaotic
vector-based universe? One idea is to
treat the AI like a human player. Point
a camera at the screen, capture the
1080p display, and feed those million
pixels per frame into the AI 30 times a
second. Sounds wild, right? Well, it's
been done for simpler games like Pong,
but Tempest's fast-paced madness is a
whole different beast. Each frame,
swirling vectors, and glowing enemies
would overwhelm the AI with raw data,
requiring massive computational power to
process it in real time. And processing
that much visual data would be like
trying to learn calculus during a
thunderstorm on a roller coaster. It's
possible, but it's way too intense for
my project. So, what if we skip pixels
and grab the game's actual vector line
data instead? Because Tempest display is
just a few hundred lines per frame, not
millions of pixels. It's tempting, but
the AI would still need to decode those
vectors into meaningful objects, like
figuring out which lines are enemies or
the playfield or your ship. And this
would require complex computer vision
techniques to interpret the raw vector
coordinates, adding another layer of
difficulty. After all, I'm just one guy
with a couple of computers, so we need
to simplify. And here's where it gets
exciting. We're going to hack directly
into Tempest's brain. The game runs on a
6502 processor with just 2 kilobyt of
RAM, tiny by anybody's standards. The
simplicity is to our advantage. The
entire game state is stored in a small
predictable chunk of memory. We could
grab the entire memory state each frame,
but that's like handing the AI a raw
blueprint without a guide at all. And so
instead, I went full detective mode,
reverse engineering Tempest's original
game code and mapping out its memory. In
doing so, I uncovered exactly where the
game stores critical data like the
positions, the types, and the depths of
the main seven enemies on screen. I also
located the four shots, all the pending
enemies, the level shapes, and so on.
Ultimately, I'm confident that I was
able to find all the necessary game
parameters in memory needed to provide
any player with complete insight into
what is happening on the screen. By
tapping into these memory tables, we'll
give our AI a crystal clear view of the
game state. No guesswork, just the raw
truth. Think of it as giving the AI a
cheat sheet that summarizes everything
happening in the game from enemy
positions to the player's status in a
format that it can easily understand.
And this is how we'll teach our AI to
dominate Tempest. And it all starts with
giving the AI a consistent
representation of the game state. Let's
look at a concrete example of how we'll
pull live data from Tempest's running
code to feed our AI. The MAME emulator
is our secret weapon because it features
built-in scripting that can provide
direct access to the game's memory.
Picture it like having a backstage pass
to Tempest Brain. Every frame, Maim
calls our custom script, which sneaks
into the emulated hardware's memory map
and grabs the raw bits and bites that
reveal the game's current state. The Mim
scripting engine uses the Lua language.
But don't let that intimidate you. I
didn't know Lua either when I started,
but it's straightforward to learn. If
you've ever tinkered with any scripting
language like Python or really even
Basic, which it's very much like, you'll
pick up Lua in no time. It's like the
userfriendly cousin of programming
languages. and I'll walk you through
every step when we actually get there.
Take the player's lives as an example.
In Tempest, you start with three lives
and the game's over when you lose your
last one. Let's peek at the actual
Tempest code that runs when you lose a
life. You don't need to be an assembly
language guru to spot the instruction
that decrements the P1 lives variable.
It's right there in the code. The trick
is in knowing where P1 lives lives in
memory. And all we need to do is check
the assembly listing file that gets
generated when I assemble the code. and
we find that it's stored at address 48
in hex. Armed with that address, our
script can then check the number of
lives anytime we want instantly. To read
a bite of memory out of the game, our
script simply calls read8 with the
address in question, and the memory
contents are returned to the caller.
It's like reaching into the game's
filing cabinet and pulling out exactly
the document that we need. And this
single bite of data tells our AI whether
the player is thriving or on the brink
of game over. And it's just one little
tiny piece of the puzzle. And this is
how we'll give our AI the real-time
pulse of the game state, setting the
stage for to learn and challenge
Tempest. Next, we roll up our sleeves
and repeat this memory hacking trick to
uncover as many game details as possible
for our AI. My first stab at this pulled
out about 125 key parameters from
Tempest memory, which sounds fairly
extensive, right? However, I continued
to dig and the latest version of my code
now extracts 316 values every frame, all
of which are fed directly to the AI.
It's like giving the AI X-ray vision
into the game soul. And here's a taste
of what I'm grabbing. Enemy segment
positions, their depths, the shot
positions and their depths, pulsar
lanes, player shot segments and
positions, top rail enemies, pending
enemies and their segments, tons more.
Each of these parameters is just a piece
of the Tempest puzzle, and together they
form a very complete picture of the game
state that our AI can use to make smart
decisions. And don't worry, we're not
cheating by giving the AI any insider
secrets that the human players can't
see. Every single value we pull from
memory is something you could figure out
just by watching the screen. For
example, an enemy's position in memory
corresponds to its visible location on
the playfield, and the player's lives
are shown on the score display. So, this
is about leveling the playfield,
armoring our AI with the same info that
a pro gamer would use, and setting it up
to learn Tempest like a champ. We'll
expand our block diagram by adding a new
component, the mystical AI itself,
implemented here as a Python application
that we're going to develop. This setup
creates two distinct processes that
collaborate seamlessly. Think of them as
two workers on an assembly line, each
handling a specialized task, but working
together to produce the final product, a
Tempest playing AI. On the left, we have
the MAME emulator running a Lewis script
to extract the game's parameters. On the
right, our Python application hosts the
neural network forming the AI's core.
The Louiswis script acts like a scout
gathering intelligence from the game,
while the Python app is the brains
making the decisions based on that
intel. Information flows between these
processes each and every Tempest game
frame. The Louiswis script collects game
parameters like enemy positions, player
status, and all that stuff, and the shot
trajectories, and well, everything, and
it sends it to the Python AI
application. The AI processes those
inputs and feeds them into its neural
network and generates the optimal player
action for the moment, such as moving
the player's ship or firing a shot. This
decision is sent back across the socket
to the Lewis script where the control
inputs are later applied. And this back
and forth happens 60 times a second,
ensuring the AI responds to the game's
fast-paced action in real time. For now,
understand that this exchange parameters
out, control inputs in, happens every
frame. Initially, I set up communication
between the Lewis script running in Maim
and the Python AI application using
pipes. Pipes are a straightforward way
to shuttle data between two processes on
the same machine, acting like a direct
pipeline where one process writes data
and the other one reads it. In this
case, the Lewis script would package up
Tempest game parameters like the
positions and player status and so on
and send them through the pipe to the
Python AI. The AI would process these,
decide on the best action, like move
left or fire, and then send that control
input back through the pipe. It was
simple and worked well for a single M
instance talking to a single AI
instance. However, as I dug deeper into
training the AI, I realized I had a
major limitation. To train the deep Q
reinforcement learning model
effectively, I needed to expose it to a
ton of gameplay data. Running just one
main instance was way too slow. It would
take ages to generate enough game states
for the AI to learn Tempest complex
patterns. So the solution was to run
multiple main instances simultaneously,
each feeding game data to the single AI.
This would let the AI learn from dozens
of games at once, speeding up the
training dramatically. But pipes weren't
really built for that. They're designed
more for onetoone communication on the
same machine, and managing dozens of
pipes from multiple MAM instances would
be a bit of a nightmare. So this led me
to switch to sockets for communication.
Sockets are far more flexible. They
allow multiple clients, which in this
case are the MAME instances running
Lewis scripts to connect to a single
server, which in this case is the Python
AI application. Unlike pipes, sockets
can operate well over a network, meaning
that the MAME instances don't even need
to be on the same computer as the AI.
And this was a game changer because I
could spin up dozens of MAME instances,
each emulating Tempest and extracting
game parameters, and have them all send
data to my one good central AI server,
which processes the inputs and responds
with control decisions. The socketbased
system works like a busy restaurant
kitchen. Each main instance is like a
chef preparing a dish, which is the game
parameters, which gets sent to the head
chef, which is the AI, via a network.
The head chef tastes each dish, decides
what's needed next, in this case, the
player's action, and sends those
instructions back to the main chefs.
Because sockets support multiple
connections, I can have as many main
chefs as my hardware allows, all feeding
the AI simultaneously.
In practice, I often run main instances
on separate machines like a cluster of
old laptops or a cloud server, while the
AI runs on the Dell workstation that
they thankfully provided me to do some
of this training. It features dual
Nvidia RTX 6000 GPUs, and I think it's a
Dell 7985. It features the Thread Ripper
795 96 core CPU, so it's got plenty of
horsepower for this task. Thanks, Dell.
And it does have to go back. I should be
clear. I don't get to keep it. This
distributes the computational load and
makes trending faster and more
efficient. Implementing sockets also
brought other perks. For one, it's
easier to debug and monitor. I can log
the data flowing through the socket
connections to ensure the game
parameters and control inputs are
correct. Plus, sockets are pretty
robust. If a main instance crashes or
disconnects, the AI server keeps
running, ready to accept new connections
from clients as they come and go. This
flexibility means I can scale the system
up or down as needed, whether I am
training with 10 main instances or 50.
To make this work, I set up the Python
AI as a TCP server listening for
incoming connections from MAMEim
clients. Each MAME client runs a Lewis
script that connects to this server and
sends packets of game parameters every
frame, as we said about 60 times a
second, and waits for the AI's response.
The data packets are lightweight, just
the 316 game parameters that I extract
from Tempest memory, plus a bit of out
of band data encoded efficiently to
minimize the network overhead. The AI
processes these packets, runs them
through the neural network, and then
sends back a small response with the
chosen action, and this back and forth
happens seamlessly even across multiple
machines, ensuring that the AI stays in
sync with every game instance. This
socket-based approach not only solved
the training bottleneck, but also made
the system more modular and futurep
proof. If I want to tweak the AI or to
add new features like supporting other
games, I can do so without overhauling
any of the communication system. It's a
robust foundation for building a Tempest
dominating AI. And it's exciting to see
dozens or even a hundred virtual arcade
machines working together to teach the
AI to play like a pro. So now on our
block diagram, we have two logically
separate but connected pieces. On the
left, we have MAME with a Louisis script
diligently extracting game parameters
like enemy positions and shot
trajectories. Every frame, 60 times a
second, it bundles those into a tidy
packet and sends them over the socket to
the Python AI. On the right, the Python
AI with its neural network powerhouse
internal to it, receives these packets,
feeds them into the deep Q model, and
then spits out the optimal player action
like move right or super Zap. This
action is sent back through the socket
to MAMEim where it is applied to the
game state, closing the loop. The setup
is elegant, but we're still sketching
the blueprint. It's time to descend into
the depths and look at how we actually
extract and pack the game parameters on
the Lewis side. We'll dive into the
nitty-gritty of reading Tempest memory
and formatting those 316 parameters for
the AI. And then it'll be time to
process them on the Python side, select
an action, and return it. After that,
we'll need to figure out how to inject
those actions, like moving the player
ship or firing a shot back into Tempest
control system. We'll also craft an
expert system to nudge the AI in the
right direction during its early
training when it's stumbling around like
a newborn. This training wheels approach
will jumpst start the learning process,
helping the AI to grasp Tempest basics
faster. With these components in place,
we'll let the AI loose, running about 50
million frames of Tempest gameplay.
Think thousands and thousands of hours
of arcade action to hone its skills. How
are we going to do all that? Well, have
you ever been watching an episode of The
Facts of Life and you realize that
you're more than 20 minutes into the
show and they have no hope of wrapping
it up in time? Well, it's a little bit
like that because I'm out of time here
as well. And that's why this is just the
first in a series of episodes. So rather
than giving you a 20inut overview
summary, I want to continue in depth.
And don't worry, every question will be
answered and every secret will be
revealed in the next installments. Join
me here next time in the shop and we'll
roll up our sleeves and fire up chat GPT
to crank out some slick Lua and Python
code. We'll build this system piece by
piece from the memory hacking scripts to
the neural network itself. So hit that
subscribe button and turn on
notifications because you don't want to
miss the moment when we bring this AI
online. Thanks for joining me out here
in the shop today. In the meantime, and
in between time, hope to see you next
time right here in Dave's Garage. Do it,
Lyn. Do it.