Game AI before "AI": state machines, steering and A* on tiny hardware
Where our AI thread starts: computer-driven trucks on a Game Boy Advance, a console companion's path-finding and A* on a 2005 phone, and why those patterns still wrap AI today.
Long before "AI" meant a model, it meant the code that made a computer opponent seem to think. Between 2002 and 2005 we wrote a lot of it: trucks that chased a flag across 3D terrain on a Game Boy Advance, a robot dog that followed its owner through a console game, and peasants who found their way around a map on a mobile phone.
None of it learned anything. All of it had to feel intelligent, run in a sliver of each frame and behave the same way every time a tester played it. This post is where the AI thread in this series begins, and the patterns in it are still the ones we wrap around language models today.
The problem
Game AI has a harder brief than it looks:
- A tiny budget. The renderer gets most of each frame. The AI gets what's left, on a CPU without floating point or a divide instruction.
- It has to read as intent. A player forgives an opponent that's beatable. They don't forgive one that drives into a wall or freezes in place.
- It has to be debuggable. When a tester says "the truck did something weird", you need to know what it was trying to do, and why.
Three tools cover most of that: state machines for deciding, steering for moving, and search for finding a way around obstacles.
Architecture
The same shape runs through all three projects:
every frame, per actor:
sense where is the target, the flag, the next checkpoint?
decide state machine: which state am I in, should I change?
act the state's function steers, fires or requests a path
(search) path-finder answers "how do I get there?" when asked Keeping "decide" separate from "act" is what makes the behaviour readable. Every actor is always in exactly one named state, and you can print it.
The engineering
CloudKarts: trucks with a state table
CloudKarts was a monster-truck game built on our Game Boy Advance 3D engine, with a capture-the-flag mode against three computer-driven trucks. Each truck was an actor with a state machine, and the states were data: two tables of function pointers, one run when a state is entered and one run every frame while it's active.
const pStateAction_Actor StateActionTable_ENEMY[NUM_STATES_ENEMY]=
{
State_ENEMY_Init,
State_ENEMY_Spawn,
State_ENEMY_Dieing,
State_ENEMY_Dead,
State_ENEMY_Idle,
State_ENEMY_Chase,
State_ENEMY_GoHome,
}; States were bit flags, so moving between them was a matter of switching one flag off and the next one on. Adding a behaviour meant adding a state and its two functions, not untangling a growing if statement.
The rules of the game sat in a per-frame update. Each truck followed the terrain's height field. The truck holding the flag drove a circuit of checkpoints, each one lighting up as it was reached. Any other truck that reached the flag inside a 600-unit sphere took it: the old carrier switched to chasing, the new one started running checkpoints and a "flag stolen" sound played. A 60-frame grace period stopped the flag being snatched straight back, which kept the mode from turning into a scrum.
Steering was deliberately simple. A chasing truck took the vector to the flag, normalised it and moved a fixed distance along it, then turned to face that way:
s32 length = Fast_Distance_3D(vx,vy,vz);
if (length != 0)
{
vx = 700*vx/length;
vz = 700*vz/length;
pAct->pObj->x -= vx;
pAct->pObj->z -= vz;
delta_A = (ArcTan2(-vx,vz)>>8)-64; The carrier moved at 750 units a frame and the chasers at 700, so a truck with the flag could slowly pull away unless the others cut across its circuit. Small numbers like that are where game AI's "personality" really lives. A radar in the corner of the screen drew all three trucks in their own colours, which doubled as a debugging view of what the AI was doing.
There was no path-finding in CloudKarts. Open terrain didn't need it: seek-and-steer was enough, and it cost almost nothing.
Goddard: following a player through a console game
From 2002 to 2004 at THQ Studio Australia we were lead AI developer for Goddard, Jimmy's robot dog in Jimmy Neutron: Boy Genius on PlayStation 2, Xbox and GameCube. Goddard had to follow Jimmy through the game, using AI and path-finding to get around the world rather than through it.
That code isn't in our archive, so we won't show any. The problem is a familiar one, though. A companion has to work out a route through level geometry to wherever the player is, keep re-planning as the player moves, and never look lost. It's close to what map apps do on phones today when they find directions and re-route as you go, and it's harder than chasing a flag across open ground, because the companion is on screen and in the player's way the whole time.
Mobile War: A* on a 2005 phone
In 2005 we built Mobile War, a real-time strategy prototype for Java phones: peasants, buildings, town halls and fog of war on a 32×32 tile map. Units needed real paths around trees, water and buildings, on handsets with tiny heaps and slow CPUs.
We used A*, with the usual integer costs: 10 for a straight step and 14 for a diagonal, which approximates √2 without floating point. Diagonals were only allowed when both neighbouring tiles were walkable, so units never cut the corner of a building. The map was a flat byte array indexed by x + (y << 5), and every search node was allocated once, up front, so a search created no garbage for the phone's collector to stall on.
The interesting part was scheduling. The first version found a path in one blocking loop. The game version moved the search onto its own thread at normal priority, below the game loop, and gave units a small state machine for asking:
case STATE_WAITING_TO_REQUEST_PATH:
if(m_gm.m_astar.m_state == AStar.STATE_IDLE)
{
m_gm.m_astar.FindPath(this, m_pos.x>>12, m_pos.y>>12, m_target.x>>12, m_target.y>>12);
m_state = STATE_WAITING_FOR_PATH;
}
break;
case STATE_WAITING_FOR_PATH:
if(m_gm.m_astar.m_state == AStar.STATE_HAS_PATH)
{
m_moveCount = m_gm.m_astar.CreateMoveList(this,m_moveList);
GetNextTarget();
m_moveIndex = 0;
m_state = STATE_MOVING_ALONG_PATH;
} One shared path-finder served every unit, one search at a time. A unit that changed its mind cancelled its pending search, and a unit whose goal was unreachable picked a nearby target instead of standing still. The game kept animating while paths were being found.
It had honest limits. The next node to expand was found by scanning the whole grid rather than keeping a priority queue. On a 1,024-tile map that was cheap enough, and simpler than a heap on a phone; on a bigger map it would have been the first thing to change.
Where AI fits
This is the AI in this post, and it's also the start of a thread. Later in this series it runs through gesture recognition, machine-learning forecasts, computer vision and generative models running on devices. The tools change. Some of the structure doesn't.
A modern AI assistant that takes actions is, underneath, a loop of sense, decide and act. The language model is very good at "decide". Everything around it is still engineering we'd recognise from these games: named states, so you know what the assistant is doing; a limited set of actions, so it can't do what it shouldn't; time and cost budgets; and a fallback when the plan fails, like the peasant who picks a nearer tile. We wrote about the "actions" half in MCP, explained.
What it taught us
Leading the AI on a console game taught us that AI work is mostly about people: designers who need numbers they can tune, testers who need to see what an actor is thinking, and players who need to read its intent. The radar, the named states and the per-truck speeds all served those people as much as the code.
It also taught us to choose the simplest tool that works. CloudKarts didn't need path-finding, so it didn't have it. Mobile War did, so it got A* on its own thread. Matching the technique to the problem, instead of to what's fashionable, is the same judgement we apply now when a client asks whether a problem needs a large model, a small one, or no model at all.
Why it still matters
Clients rarely need a robot dog. They do need AI that behaves predictably, stays inside clear limits and can explain what it's doing. That's what we build in our AI assistants work, and the Game Boy Advance and console case study shows where the habit started.