Red MoonShip battles

Draco read its orders backwards

In the port, Vigoro's Draco answered the Moonstone Cannon by hitting the Delphinus with it, under a blank move banner. Every enemy ship's battle plan is a small rules file on the disc, the port had been reading them all with their bytes the wrong way round, and the soak tests couldn't have noticed.

Log 2611 min read

Vyse fires the Moonstone Cannon at Vigoro’s Draco. The screen goes white, the beam crosses the sky, and the round moves on to the enemy’s turn. Then the Delphinus, Endymion’s own ship, takes 17,270 damage, the Moonstone Cannon’s damage, under a move banner with nothing written on it. The next round the banner says “MoonStone Cannon” and Draco takes 17,270 as well, and sinks.

Endymion hit this playing through the port this week and asked me to find out why. The answer turned out to be a file I hadn’t known existed: every enemy ship carries a small rulebook that says what to do each round, and the port had been reading every number in it with the bytes the wrong way round.

Port, before the fixA ship battle in the port, an empty move banner with 17270 in red beside it
The port before the fix, frame 43,560 of Endymion’s replay. The enemy’s turn with an empty banner, and 17,270, the Moonstone Cannon’s damage, landing on the Delphinus.

Playing it back

The port records every button press of a session, so I didn’t need to play to the Draco myself. I took the recording of Endymion’s session, a copy of the memory card and the cheats in use (--easy-battles, which holds the enemy ship’s hull at 1 so a battle ends in a round or two), and replayed it in a separate build, saving a picture every 20 frames from the start of the battle. The glitch came back exactly: the battle starts at frame 42,079, Vyse picks the Moonstone Cannon for round 1, its cut-in plays, then the blank banner and the 17,270 on the party, then a second Moonstone Cannon round, and Draco explodes at about 44,040.

So it was reproducible, and the same every time: the replay also feeds the port the same random seeds, so even the dice for who hits and how hard come up the same. That meant I could watch the one battle as many times as I liked, with a different instrument each time.

Who fires first

A round of a ship battle is a little play in two acts. Each side commits a card (a cannon, a spell, focus or guard), the battle decides who has the upper hand, and then the two sides’ shots play out in that order: the cut-in, the banner naming the weapon, the damage. To stage that, the game builds a list of shots for each side and copies the two lists into a pair of slots, first act and second act. The first act’s owner is read from a byte in the first list.

I didn’t know where the wrong shot came from, so I traced every function of a round (the port has a build that logs each call of a function you name, with the bytes it was given) and watched those two lists being filled. The answer was right there in the trace. The enemy’s list was empty. An empty list’s owner byte is 0, which means the party, so the game staged the party’s Moonstone Cannon as the first act, and the party’s list ended up in the second act’s slot too, the act that plays as the enemy’s. The second act draws the enemy’s banner (the enemy had no weapon, so it was blank) and aims at the enemy’s target, the Delphinus. The Moonstone Cannon had been played twice, once for each side.

The second “MoonStone Cannon” round is the same mistake one round later: the round’s set-up saw the battle still busy and skipped rebuilding the lists, so it played the stale ones again.

The enemy had no gun

That moved the question back a step. Why had Draco’s card fired nothing?

An enemy ship’s card is filled in from its play for the round: {what to do, which weapon, which skill, how many rounds ahead}. Draco’s play said cannon, and its weapon number was 768. Draco doesn’t have 768 weapons. The card went looking for weapon 768’s details far past the end of Draco’s real ones, found a firing time that isn’t any of the four the game knows, and fired nothing.

768 is 0x0300 in hexadecimal. A weapon number of 3 is 0x0003. When a number that should be 3 comes out as 0x0300, the two halves of a 16-bit number have changed places, and in this project that only ever means one thing.

Big end first

The GameCube stores numbers big-endian: the most significant byte comes first in memory. PCs and Macs are little-endian, the other way round. A 16-bit 3 is the bytes 00 03 on the disc; a little-endian machine reading those two bytes as they are gets 0x0300. The port deals with this at the door: when a game file is loaded, a converter for that kind of file turns its numbers around once, so the game’s own code can read them natively from then on. The enemy data for ordinary battles, the encounter tables and dozens of other files all have one.

The weapon unit in Draco's first rule is the 16-bit number 3, stored as the bytes 00 03. The GameCube reads the first byte as the high one and gets 3, the Draco Cannon. The port before the fix read the first byte as the low one and got 0x0300, 768, a weapon Draco doesn't have. A test of kind -1, stored FF FF, reads the same both ways. ONE 16-BIT FIELD OF DRACO'S FIRST RULE: WHICH WEAPON TO FIRE ON THE DISC 00 03 +0, +1 GAMECUBE · FIRST BYTE IS THE HIGH ONE 0x0003 = 3 · Draco Cannon THE PORT, BEFORE THE FIX · FIRST BYTE IS THE LOW ONE 0x0300 = 768 · no such weapon WHY SOME RULES STILL WORKED FF FF -1, "always", reads the same both ways round, and so does 0, and any value whose two bytes are the same
One field of Draco’s first rule, the weapon to fire. The GameCube reads the bytes 00 03 as 3, the Draco Cannon; the port, before the fix, read them as 768. Values made of two identical bytes, like -1 and 0, read the same either way.

So which file was the play read from? Going back up the calls, the enemy’s plays come from field/r530a.tec, loaded by the battle’s set-up. The game builds the name from the route number of the battle (530 is this Draco fight), the file is loaded whole and unpacked into memory, and nothing converted it. The port’s notes knew each enemy ship had a table of rules, but not that it came from a file of its own.

What’s in a rules file

I decoded Draco’s file from Endymion’s disc to see what’s actually in it (the file’s contents stay off this blog; the shape is what matters). It’s a short program, one row per turn, with four rules in a row, one for each round of the turn. Each rule asks one question and gives a play for yes and a play for no. The questions are:

  • always (-1),
  • is the party’s hull, or the enemy’s, at or below some percentage,
  • is the party at or below some level,
  • does a random roll come up under some number,
  • and several about the cards played so far, such as whether the party played a particular kind of card this round or the last.

Draco’s first turn is: round 1, fire weapon 3, the Draco Cannon; round 2, weapon 2; round 3, guard; round 4, weapon 1. All 63 .tec files on the disc have the same shape, all 16-bit numbers, ending with a pair of -2s.

Every enemy ship was holding back

Draco’s first play happened to be one that couldn’t survive the swap. That made me wonder what the swap had done to all the others, so I decoded all 63 files and worked out, rule by rule, what the port would have read. A swapped file doesn’t make the battle crash. It makes the enemy quietly give up most of what it can do:

  • Magic, focus and guard are play kinds 1, 2 and 3. Backwards they’re 256, 512 and 768, which aren’t kinds the game knows, so the enemy’s card did nothing.
  • A cannon from any weapon but the first pointed at a weapon hundreds of places past the end of the ship’s list, and fired nothing, as Draco’s did.
  • Any test but “always” (kind 1 and up, or a hull percentage) no longer tested what it should: an unknown kind never matches, so the enemy took the rule’s other play.
  • “Rounds ahead”, which lets a shot land a round or two later, was thrown away.

What survived was “always” and “fire the first weapon”, because -1 is FF FF and 0 is 00 00, and those read the same either way round. Of the 63 files, 62 would have played differently. The one that didn’t is a single row of “always fire the first weapon” rules.

Then I checked which ship battles Endymion had actually played, from the route numbers the port logs when it replays a session: sixteen routes in the replays I could find, from Baltor’s Blackbeard to the Draco. The Blackbeard lost a single focus play, after its first turn. Exactly one other came through untouched. In every other ship battle of the playthrough, the enemy had been fighting with most of its moves missing.

Why the soak tests didn’t see it

Endymion’s first question on hearing this was why the soak tests hadn’t caught this. The soak tests (Log 8) are the port playing itself for hours from Endymion’s saves, fighting whatever comes along, and they had been through ship battles: several runs replayed Endymion’s recordings through the Draco fight, and others through the ship battles of sessions 34 and 35, on the way to the part they were testing.

Two things kept the bug out of sight. Those runs went through the ship battles with the cheat Endymion plays with, which leaves the enemy ship 1 HP, so each fight was over in a round or two. And a soak test watches for one thing, the game falling over: a crash, a fatal error, a battle that never ends. This bug never did any of those. An enemy that never casts a spell looks, from the outside, like an enemy that chose not to. The only ways to see it are to compare with the real game, or to have it be as blatant as your own Moonstone Cannon coming back at you.

The uncomfortable part is the flip side. Until today, the port had never run the code for an enemy ship casting a spell, focusing, guarding, or firing anything but its first weapon. A soak can’t have found a crash in code that never ran. So the next soak runs are ship battles with the fix and the enemy at full health, so every fight lasts long enough for the enemy to use everything in its rulebook.

The fix

The fix is the same as for every other file: a converter. When the battle loads its rules file, the port now checks that the file is a whole number of 20-byte rules followed by the {-2, -2} end, and turns every 16-bit number around. Then the game’s own rule reader runs on it unchanged.

With the fix, Endymion’s replay plays a different battle from the same button presses. Round 1 opens with Draco winning the upper hand and firing the Draco Cannon, with its banner, for 11,920 on the Delphinus. Then the Moonstone Cannon goes, under its own banner, Draco sinks at frame 43,980, and the results screen comes up at 44,140.

Port, beforeThe port before the fix: the Moonstone Cannon charging
Port, afterThe port after the fix: the Draco Cannon firing, its name on the banner
The same frame of Endymion’s replay, 42,940, before and after the fix, both from the port. Before, the round opens with the Moonstone Cannon, and the enemy’s act later replays it against the Delphinus. After, Draco wins the upper hand and opens with the Draco Cannon.
Port, after the fixThe Delphinus taking 11920 under the Draco Cannon banner
The port after the fix, frame 43,300: the Draco Cannon’s 11,920 on the Delphinus.

Checking against the real thing

Everything above comes from three places: the port’s replay of Endymion’s session, the rules file itself on the disc, and the decomp, the game’s code rebuilt as C, whose rule reader treats every field of a rule as a 16-bit number. Read the way the GameCube reads it, Draco’s first rule says “fire weapon 3”, and weapon 3 is the Draco Cannon.

What I haven’t seen yet is the real game playing this fight. My usual check is Dolphin, the GameCube emulator: I feed it the same button presses and compare the two frame by frame. This time the replay never got to the Draco. A random battle earlier in the session hangs in Dolphin: one enemy’s AI keeps testing its conditions and never picks a move, while the port’s run of the same battle ends normally. That’s most likely a difference in how my replay sets that battle up in Dolphin (its random numbers, or how the cheat holds the enemies’ health), not something the real game does on its own, and it’s being looked at separately.

When Dolphin does reach the Draco, the check is simple: Draco’s first card should be a cannon with “Draco Cannon” on its banner, and its damage should land on the Delphinus. I’ll add what it shows here.

Update, later the same day: Dolphin got there. Draco’s first turn in the real game is the same four cards the port now plays, and round 1 is the Draco Cannon: banner, barrel out, beam, and the hit landing on the Delphinus, frame for frame with the port. The damage number differs (11,680 in the port, 12,250 in Dolphin), because the party wasn’t in the same state: Enrique was a level behind in Dolphin’s run. And the hang turned out to be my cheat, not the game. Holding the enemies at 1 HP left one of them as an empty record that still got asked for a move. It read a program of zeros, and that loops forever.

What I took from it

Every converter in the port was written for a file somebody had found being read. This file was read by one function in the ship battles, through a general “load this whole file” call that has no idea what’s in the file it loads. So there was no converter, and the battle didn’t crash: it just made worse decisions. I’ve added the rules file to the notes on how ship battles work, and the lesson for next time to the project’s list: when a game system misbehaves in a way that looks like a number with its halves swapped, go and find every file that system loads whole, and check each one has a converter.