Red MoonShip battles

The same numbers, the other way round

Replaying Endymion's ship battles against Dolphin, the port and the real game drew the same random numbers and still played rounds in the opposite order: one line of C that two compilers read in different directions. On the way, three ship parts that had been drawn off-screen since the first ship battle.

Log 279 min read

Frame 11,801 of Endymion’s session, round 1 against De Loco’s Chameleon. In the port, Little Jack fires its Main Cannon. In Dolphin, playing the same button presses from the same random seed, the Chameleon fires its Type D Torpedo first, and the Main Cannon waits its turn.

Both programs had dealt the turn from the same position in the same sequence of random numbers, and both enemy ships had chosen the same four cards. The battle was the same in every way I could measure except which ship went first. That turned out to be one line of C that the GameCube’s compiler and the port’s compiler read in opposite directions.

Port, beforeLittle Jack's Main Cannon firing first, the banner reading Main Cannon
DolphinThe Chameleon's Type D Torpedo firing first, the banner reading Type D Torpedo
Round 1 against the Chameleon at frame 11,801 of the same replay. Left, the port before the fix: Little Jack’s Main Cannon. Right, Dolphin: the Chameleon’s Type D Torpedo, with the Main Cannon after it.

Re-checking every ship battle

After the rules-file fix in Log 26, every enemy ship had its full set of moves for the first time, and Endymion asked me to replay each ship battle they had played against Dolphin. The setup was the one Log 26 ended on: the party held at full health, the enemy left at full health so the fight lasts long enough to use everything it has, and a record of each side’s card in each round, in the port and in the real game. One rule came with the job: no difference gets written off as luck until the random numbers say so.

That rule needs a word about where the luck comes from. The game’s random numbers aren’t random at all. They form one long sequence, each number worked out from the one before it, starting from a seed. The replay hands the port and Dolphin the same seed, so both walk the same sequence; the only thing that can differ is how far along each one is. So for every “random” choice I can ask: which number in the sequence did each side use?

To answer that I put a breakpoint in Dolphin on the instruction right after an enemy rule rolls its dice. Each time it’s hit, Dolphin reports the roll and the sequence’s state, and the state tells me the roll’s position. The port logs the same thing from its own copy of the code.

Mostly, a frame of hesitation

Thirteen ship battles in all. Most of the differences had a dull explanation, and it was the replay’s fault, not the game’s. While the battle waits at the command menu, it takes 16 numbers from the sequence every frame (6 in some battles). The menu closes on an A press, and Dolphin’s replay sometimes lands that press a frame or three later than the port did. That leaves Dolphin 16, 32 or 48 numbers further along when the enemy rolls, so a “60% chance” rule reads a different number and can take its other branch. I replayed one battle twice in Dolphin and got a difference of 0 the first time and 16 the second, from the same recording. The game itself never drew a different number for the same position.

Battle 509, against the Chameleon, had no such excuse. Its first rolls were at the same positions on both sides with the same values, and the first three turns played the same cards. Yet by turn 4 Dolphin was 434 numbers ahead.

Following the numbers

The roll breakpoint only shows the numbers the enemy’s rules use. I wanted all of them, so I ran Dolphin again, recording the sequence’s position at every frame and taking a picture at the same frames as the port, and lined the two lists up. (This run’s menu happened to close a frame late, so Dolphin started the turn 16 numbers along. That moves where each draw falls, not how many draws each action takes.)

Each action took exactly the same handful of numbers on both sides. The Main Cannon’s sparks and smoke took 65, then 124, then 56, then 78, in the port and in Dolphin alike. But in the port that burst came 81 frames after the turn began, and in Dolphin over 300 frames later, after the torpedo’s smaller one. Same pieces, other order. The pictures said the same thing: the round’s two acts had swapped.

Who goes first

Each round of a ship battle starts with a clash that decides which ship has the upper hand, and that ship acts first. The decomp (the game’s code rebuilt as C) shows how. Each ship has a rating (the escape roll uses the same number, so it’s probably the ship’s speed, but I haven’t confirmed that). Each ship adds a random number from 0 up to a quarter of the two ratings added together, and the higher total wins. In C it’s one line:

if (left + rand() % range >= right + rand() % range)

That line calls rand() twice, and C deliberately doesn’t say which side of the >= is worked out first. A compiler may do either. The two answers give the same numbers to different ships.

A round's clash takes two numbers in a row from the game's random sequence, a then b. On the GameCube the first, a, goes to the enemy ship and the second, b, to Little Jack. In the port before the fix, a went to Little Jack and b to the enemy. Each ship adds its number to its rating, and the higher total acts first. ONE ROUND'S CLASH: TWO NUMBERS IN A ROW FROM THE RANDOM SEQUENCE … a b … 1st draw 2nd draw Each is cut down to 0 … a quarter of the two ships' ratings added together GAMECUBE · CODEWARRIOR TOOK THE RIGHT-HAND SIDE FIRST Chameleon: rating + a Little Jack: rating + b THE PORT BEFORE THE FIX · CLANG TOOK THE LEFT-HAND SIDE FIRST Chameleon: rating + b Little Jack: rating + a The higher total wins the round's upper hand and acts first. Same numbers, other owners.
A round’s clash takes two numbers in a row from the sequence. The GameCube’s compiler worked out the right-hand side first, so the first number went to the enemy; the port’s compiler worked out the left first and gave it to Little Jack.

To find out which order the real game uses, I read the original machine code for the function (0x80147CC8). The first rand() result is added to the right-hand ship’s rating, the enemy’s. Then I read what the port’s compiler had made of the same line: its first result went to the left-hand ship, Little Jack or the Delphinus.

So in every ship battle the port has ever played, every clash that wasn’t settled in advance gave each ship the other’s number. Oddly, it couldn’t have made the port any easier or harder: both numbers come from the same range, so the odds of winning the upper hand were unchanged. What changed was which rounds you won. The battle was a different battle.

The fix, and a check for the next one

The fix takes the two numbers in separate statements, in the GameCube’s order: the enemy’s first, then the party’s. With that, battle 509’s turn 4 rolls are 16 numbers apart instead of 434, one frame of the replay’s menu, and round 1 plays the way Dolphin plays it.

Port, afterThe Chameleon's Type D Torpedo firing first in the port
DolphinThe Chameleon's Type D Torpedo firing first in Dolphin
Frame 11,801 again, now with the port fixed (left) and Dolphin (right): the Chameleon’s torpedo goes first in both. The rest of the round matches frame for frame, down to the Main Cannon’s 1,490 damage when it lands at frame 12,071.

Then I went looking for any more lines like it. The project has a habit for this: once a bug is understood, write the check that finds every other instance of it. The new check finds every function that takes numbers from the sequence, directly or through another function, and fails on any single statement that calls two of them. Across all the game’s code, the clash was the only one. The check now runs before every push, so a second one can’t creep in.

Parts drawn somewhere else

The sweep had turned up one more ship-battle bug before the clash. This one wasn’t hidden in the numbers at all: it had been missing from the screen in every ship battle, and nobody had noticed.

It started with Vigoro’s Draco. Dolphin drew its huge bow cannon from the battle’s opening shot onward; the port only drew it in the Draco Cannon’s close-up, which uses a separate model. A ship in battle is a hull plus a few dozen smaller pieces (guns, masts, propellers, that bow cannon), and each piece is its own little task that copies its ship’s position and heading every frame. I logged every model the port drew on one frame and every model Dolphin drew, and they matched one for one. All the pieces were being drawn. The port was just putting some of them in the wrong place.

The ship’s data block in the port holds one extra value near the start, a pointer to host memory, which pushes every field after it 12 bytes along. Three kinds of follower already knew that. Two didn’t. They read the ship’s position 12 bytes early, three numbers off, so each part was placed with the end of one matrix and the start of the next. That put the part at the middle of the map or tens of thousands of units away, out of the camera’s view.

Port, beforeVigoro's Draco seen from the bow, with no bow cannon
Port, afterVigoro's Draco seen from the bow, its long bow cannon in place
Vigoro’s Draco in the battle’s opening fly-past, frame 42,160, both from the port: before the fix (left), a bare deck; after (right), the bow cannon where Dolphin draws it.

The next question was what else those two followers carried. I went back to the earlier battles with the fix and compared them frame by frame against Dolphin: the port before, the port after, and the real game, counting the pixels that differ. Little Jack had been missing its golden bow ram in every ship battle since the port’s first, on 9 October.

Port, beforeLittle Jack from the side with a plain bow
DolphinLittle Jack from the side with its golden ram on the bow
Little Jack against the Black Pirates, the same frame of Endymion’s replay. Left, the port before the fix: no ram. Right, Dolphin: the golden ram on the bow. (Dolphin’s replay runs a few frames behind, so its spirit gauge is still on screen.)

The Delphinus had lost its lit bridge windows, so the grey hull showed through where they should be.

Port, beforeThe Delphinus's bridge, its windows dark grey
Port, afterThe Delphinus's bridge, its windows lit
The Delphinus firing its Prototype Cannon in the battle against Gregorio, both from the port: before the fix (left), bare grey frames; after (right), the lit windows. Dolphin draws them lit, as the fixed port does.

Why none of this showed up sooner

The port plays itself for hours in soak tests, and it had been through ship battles many times. Three things hid these bugs.

The soaks fought ship battles with Endymion’s cheat that holds the enemy at 1 HP, so a battle lasted a volley or two. A swapped clash in one or two rounds just looks like a coin that landed the other way.

The checks compared the port with itself. The replay suite plays a set of recorded sessions on a new build and compares every frame and every random number with the last build. That’s excellent at catching a change. But the clash bug and the missing parts had been there since before the first recording, so both builds had them, identically.

And when I did compare with Dolphin, I compared how frames were drawn: does the cannon’s beam look right, does the damage number land. I didn’t compare which ship acted first, or every corner of every frame. The ram is a small spike of gold at the far end of the ship. The sweep caught it because it compared the pictures pixel by pixel, side by side, at the same frames.

What I took from it

The decomp’s C is written to rebuild the GameCube’s exact machine code, and for that compiler, that line was fine: it always took the right-hand side first. Clang is also allowed to take the left. When a line of the decomp does two things in an order C leaves open, the machine code is what says what the game does. The port should spell that order out rather than leave the choice to its compiler.