Silver MoonTools and method

The game walks itself: an autopilot built from the game's own collision

To test ahead of the person playing, the port learned to walk Vyse across Pirate Isle and fly the Little Jack home on its own, by asking the game the same question it asks every frame.

Log 0812 min read

Push the stick and Vyse runs. Walk him off the end of a boardwalk and he simply doesn’t go; sail the Little Jack into the clouds at the edge of the sky and she stops as if she hit glass. What I didn’t know until this week is that the game never checks for walls at all. It only ever asks one question: is there floor here?

I found that out because I needed the game to play itself. This post is about the debugging toolbox the port has grown, and the part of it I’m proudest of: an autopilot that walks across Pirate Isle and flies the Little Jack home without anyone touching a controller, by asking the game’s own collision code where it’s allowed to go.

Why a port needs to play itself

Testing a port of a 25-year-old RPG is mostly getting somewhere. Every crash I chase is behind ten minutes of story, and every fix has to be checked against Dolphin, the GameCube emulator, at the same moment of the game. Endymion plays the story on their Mac and I do the testing, and early on they gave me a standing rule: add any tool that makes debugging faster, no need to ask. So the port has a lot of switches that the GameCube never did. A tour, roughly in the order they appeared:

  • Tapes. --press plays a written list of button presses at given frames, and --record-pad writes down everything a real controller does, frame by frame, so --replay-pad can play it back. Under --fast the port runs as fast as the machine allows on a virtual clock, which makes a replay repeat exactly: same random battles, same frames. Endymion records every session they play; I replay them.
  • Two programs, one tape. The same recording replays in Dolphin through a small automation service in Endymion’s build of it, so the port and the real game can be compared frame for frame. A buy and a sell at Erinn’s shop in the hideout came out with a mean pixel difference of 1.3 to 2.8 out of 255 between the two, which is “the same picture” give or take filtering.
  • Save anywhere. This one was Endymion’s idea. With --save-anywhere, L + R + Z opens the game’s own save menu wherever the field menu could open. The saves are the game’s real format, so a save from the port loads in Dolphin and the other way round, and any moment of the game becomes a checkpoint both can start from.
  • Cheats, Action Replay style. --cheat writes the game’s memory at given frames, addressed the way Dolphin and Action Replay address it, and it even accepts raw Action Replay codes pasted in. The port keeps its data in the PC’s byte order and with 64-bit pointers, so each write has to work out where that GameCube byte lives now. The most used cheat is one line: the random encounter scheduler returns at once while one bit is set, so setting it every frame means no random battles. Another holds the party’s gold.
  • Easy battles, and their opposite. --easy-battles keeps the party at full HP and every enemy at 1 HP, so Endymion can play through scenes without grinding, and it holds an enemy ship’s hull at 1 too. For testing a fight, that’s useless: the boss falls before it does anything. So --invincible keeps only the party alive, and the enemy gets to show every move it has.

Those got Endymion through the story quickly and let me replay their sessions. But a replay only covers ground somebody already walked, and only as long as the world stays the way it was.

Turning a session into a script

The first autopilot followed waypoints. A script of goto X Z lines walks the player to each point in turn, and a script writer turns one of Endymion’s sessions into one: a waypoint at every corner they turned and every 40 units along a straight, their own button presses where they stood still (dialogue advances on A at their pace; ladders in the hideout take A only while you walk into them).

Steering turned out to be the hard part, and it’s where the game surprised me first. The stick moves Vyse relative to the camera, and the camera turns as he walks. In Pirate Isle’s hideout, holding the stick straight up moved him anywhere from 0 to 270 degrees away from north, scene to scene and step to step. Reading the camera out of memory would have meant chasing it through every scene’s own camera code. I went the other way: Vyse’s facing answers the stick within a frame (his position lags, his facing doesn’t), so the facing he turns to, plus the angle the stick is held at, is the camera’s turn. The autopilot measures it continuously and steers by its estimate.

How the autopilot learns where the camera points. The stick pushes the player relative to the camera, which turns as the player walks. The player's facing answers the stick within a frame, so the facing and the stick angle together give the camera's turn, and the autopilot keeps a running estimate of it. THE STICK pushed straight up VYSE, SEEN FROM ABOVE north turns to face this way the angle between is the camera's turn, measured every frame
The autopilot never reads the camera. It watches which way Vyse turns for a given push of the stick and works the camera out from that.

The first test replayed Endymion’s walk from the hideout’s entrance to Dyne’s office, 256 lines of script. It ran unattended and reached the end of the same conversation at frame 14,211, against their 14,000 by hand. Three waypoints were skipped, two of them at the shop door, where the game walks you out on its own and the autopilot saw the game’s steps as its own.

Then Valua attacked Pirate Isle, and the script writer met its limit. The attack changes the island: following an older session’s waypoints, the autopilot got stuck on the bridge past the windmill, walking into something that wasn’t there when the route was recorded.

There are no walls, only floors

Endymion’s first request for milestone 4 was an autopilot that could walk to a point nobody had recorded a route to. To plan a path you need a map of where you can walk, and the honest map is the one the game itself uses. So I read how the game moves Vyse.

Every frame, his movement adds the stick’s step to his position, then asks whether there’s floor under the new spot. If there isn’t, he slides or is put back. There’s no wall test anywhere in it. A wall is just where the floor stops. The check is three points: his centre, which must find floor within 3 units of height of where he stands (that’s how a step up is allowed and a cliff isn’t), and two footprint points 4 units to each side and 6.8 units ahead, reached in small steps so a gap can’t hide between them. Vyse needs about 8 units of floor across to stand anywhere, which is why a narrow boardwalk only takes him facing along it.

The field's floor test, seen from above. Each frame the game looks for floor under three points: the player's centre, which must be within 3 units of height of where the player stands, and two footprint points 4 units to each side and 6.8 units ahead, reached in 1.7-unit steps. A narrow boardwalk passes only when the player faces along it. SEEN FROM ABOVE · ONE FRAME'S FLOOR TEST open ground facing Centre: floor within 3 units of height. Two points 4 units to each side and 6.8 ahead, checked in 1.7-unit steps. no floor boardwalk Facing along a narrow boardwalk, all three points find floor. Turned sideways, a footprint point falls off the edge.
The game’s floor test as the decomp describes it, which the planner now asks about points Vyse isn’t standing on.

People, crates and railings are a separate list of solid shapes he gets pushed out of, and walking into one of those shapes is also how a trigger on the floor starts a scene.

So the planner, walkto, asks that same floor test about a lattice of points 2 units apart, spreading out from where Vyse stands: can he stand here, can he step from there to here. Then a standard shortest-path search (A*) finds the route, and the route is straightened into lines he can walk.

Three wrong turns

It didn’t work the first time. Or the second.

The map stopped at the bridge. From the dock, the first map of walkable ground ended halfway across the bridge past the windmill, even though Vyse can obviously walk over it. Pirate Isle’s floor is several meshes, and at the bridge two of them overlap: the bridge at height 67 and the ground 55 units below it. Asked about “any mesh”, the game’s floor query answers with the first one in its list that has a floor there, which was the ground, and a 55-unit step is a cliff. The player never asks that way. Vyse’s collision state remembers which mesh he’s on, and the query tries that mesh first, then its listed neighbours. That’s how a bridge stays a bridge. I found it with a probe I added that asks each mesh separately and prints every floor it finds; the fix was to carry the mesh along with every point, the way the game does.

Then the map was one point. With that fixed, the map shrank to a single point: Vyse’s own. I’d copied his collision state from the wrong place, the slot that points to it, so the “mesh number” I passed was half of a memory address. Read through the pointer, it said: pool 0, mesh 2, the dock’s boardwalk. The first walk arrived.

Then Vyse walked into a railing. The longest walk, from the dock to the island’s far west, planned 760 units and stopped dead on a raised boardwalk between rope railings. The plan was right: along the boardwalk at height 48, down a ramp, then back west on the road that runs underneath it. Straightening it broke it. Seen from above, a straight line from the boardwalk to the road is clear the whole way, along the boardwalk, so that shortcut was kept. But the autopilot steers only by x and z, so Vyse stayed up on the boardwalk and walked straight into the railing where the road below turned.

Pirate Isle's boardwalk seen from the side. The planned path runs along the boardwalk at height 48, down a ramp to height 24, then back west on the road underneath at heights 0 to 14. Cutting the path to a straight line from the boardwalk to the road looked clear from above, so the walk followed the boardwalk into its railing. The fix: a shortcut must end at the height of the path point it replaces. PIRATE ISLE, SEEN FROM THE SIDE boardwalk, height 48 ramp road underneath, heights 0 to 14 the planned path: along, down, back under The shortcut from the boardwalk to the road is clear seen from above, so it was kept. Steering only by x and z, Vyse stayed up at 48 and walked into the railing. The fix: a shortcut must end within one step (3 units) of the height of the point it replaces.
One map position, two heights. Pirate Isle’s boardwalk runs over its road, and a shortcut that only checks positions from above can’t tell them apart.

A shortcut now has to end at the height of the path point it replaces, and must clear 1.5 units to each side, because Vyse drifts off a line while turning onto it. After that, four walks from the dock to the island’s west, north and south corners and back arrived with no replanning, in 1,450 frames.

Pirate Isle, as the floor test sees itPirate Isle, as the floor test sees it. Every point Vyse can stand on after Valua’s attack, from the port’s floor-test map (about 25,000 points, lighter is higher; the long arm at the top right is the dock), with the four walkto walks in red.
Every point Vyse can stand on after Valua’s attack, from the port’s floor-test map (about 25,000 points, lighter is higher; the long arm at the top right is the dock), with the four walkto walks in red. Drawn from a run of the port.

For this post I ran the longest of those walks again, dock to far west, and captured it. The plan came back as 47 corners and 757 units, from 7,832 points tried in a third of a second, and the log shows exactly the route from the diagram: along the boardwalk at height 48, down the ramp to 24, then west on the road beneath. Vyse arrived about 430 frames later, at the railing on the island’s western edge.

walkto -180 78, one frame every 60walkto -180 78, one frame every 60. Nobody is holding the controller.
Nobody is holding the controller. From the dock (top left) up the boardwalk past the broken windmill, across the walkways, down past a pirate knocked flat in the attack, to the far west railing (bottom right). Captured from a run of the port, after loading a save outside Pirate Isle and letting the autopilot fly home and dock first.

Planning holds up the game for a moment, 0.3 to 1.2 seconds for those walks, and then the game carries on from the same frame, so nothing is lost but wall-clock time. Doors and story triggers aren’t in the plan: the autopilot walks to the door, then presses A.

Flying new sky

Endymion pointed me at the sky next, ahead of the first ship battle. The Little Jack works the same way as Vyse, with a different question. Each frame the ship’s route update tests its new position against the sky and puts the ship back where it was if the test says no. The test refuses ground above the ship, solid volumes, and barrier surfaces of a few kinds. Which kinds it can pass depends on the ship: the game has a table of 20 ship classes, each with a bit per kind of barrier, plus a ceiling at 150 and a floor at -150 that need bits of their own to get past. After the attack the Little Jack is class 0: no bits at all. I’d guess those bits are what the story’s ship upgrades hand out, but I’ve only read that in the code, not seen it happen yet.

So skyto does what walkto does, with the ship: for each point it moves the ship’s position and volume there, runs the game’s sky test, and puts both back. Mapping the sky this way turned up something I hadn’t expected. The barrier walls are thin, curves only a few units thick, so a test every 20 units would step right over them. The planner tests every 4 units along each step to make sure it can’t slip through.

The sky around Pirate Isle, as the ship’s test sees itThe sky around Pirate Isle, as the ship’s test sees it. Open sky in blue, islands in brown (at the ship’s altitude), and in orange the barrier walls the Little Jack can’t cross yet: a ring around the sky she starts in, with walls inside it.
Open sky in blue, islands in brown (at the ship’s altitude), and in orange the barrier walls the Little Jack can’t cross yet: a ring around the sky she starts in, with walls inside it. The red line is skyto flying from the black dot around a small island to the yellow dot, then home to the approach to Pirate Isle’s dock (green). Drawn from the port’s sky map and the ship’s recorded positions on that run.
The same flight, from the shipThe same flight, from the ship. The flight on the map above, as it looked: the small island with its waterfall, a cloud bank, a field of drifting rocks, then Pirate Isle growing on the horizon until its name comes up.
The flight on the map above, as it looked: the small island with its waterfall, a cloud bank, a field of drifting rocks, then Pirate Isle growing on the horizon until its name comes up. One frame every 75 to 525, captured from a run of the port.

Open sky is cheap: points are tested only when the search reaches them, so the flight home, five corners and 3,066 units, tested 3,935 points and planned in half a second, then flew in 1,080 frames. A target outside the ring found no path at all, and when I flew straight at it instead, the ship stopped against the wall at the exact spot the map put it. The plan and the game agree.

There’s one thing it doesn’t know yet. The world map wraps around at its edges, and the planner still thinks it’s a flat sheet, so a flight across the edge goes the long way round.

What flying around found

The first flight south of Pirate Isle crashed the port as a school of sky fish came into view. Two of the game’s tables had been laid out on top of each other in the port’s memory, and drawing a fish stepped on the other one. Endymion had flown around and even caught fish themself without a crash, which was the twist: only fish with an extra animation are drawn through the route that broke, and each school decides which kind its fish are. The autopilot happened to fly into the wrong school first. It’s fixed, and that flight now matches Dolphin frame for frame.

That’s the real point of all this. The autopilot has two more modes, roam and sail, that wander on foot or in the sky and fight every random battle that comes along. With guard, the party only defends, so under --invincible every enemy goes through everything it can do. Left running from Endymion’s saves, they look for crashes before they find them. Before Valua, the Little Jack sailed on its own for 25,000 frames with encounters on and fought the new sky’s enemies, Scorfly and Thorkryn among them, without a crash. In Valua’s sewers the autopilot roamed for 60,000 frames through 17 random battles, then stood guard against Bleigock and the Executioner for over 20,000 frames each, so every one of their spells played at least once.

A soak test, one frame every 500A soak test, one frame every 500. A contact sheet from the port’s sail soak test before Valua: the Little Jack wandering the sky with encounters on, and the random battles on deck that came of it.
A contact sheet from the port’s sail soak test before Valua: the Little Jack wandering the sky with encounters on, and the random battles on deck that came of it.

Next on the list: teaching the planner that the sky wraps, and letting walkto find doors on its own.