Game Internals › War of the Lance

War of the Lance

SSI's 1989 strategy game of the Dragonlance war, rebuilt in TypeScript to run in a browser. You can play it with the original bugs, in all their glory, or the bug-fixed version. There is also a Whitestone AI, in case you want to play as Highlord, or watch the two duke it out. All games are recorded and can be played back later.

The analysis and implementation work was done by Claude Opus 5.5, who apparently knows much more about Apple II internals than me. I did the Whitestone AI design and playtesting. Most text is Claude's, I've edited it where he got too Claudeish.

If you've played the well-known copy of War of the Lance then I should mention that this one fixes the corrupted news items, which were due to a missing sector. More details are in the disks section.

Play it in your browser

1The game

War of the Lance is a strategic wargame set in Ansalon, the world of the Dragonlance novels. One player is the Highlord, whose dragonarmies start strong in the east. The other is Whitestone, the alliance of elves, dwarves, knights and free nations. Over up to 30 turns, a season at a time, they move armies, fleets, dragons, wizards and heroes, court the neutral nations with diplomats, and send heroes on quests. Either side can be played by the computer.

SSI released it in 1989. This work is on the Apple II version.

2Opening the disks

The game comes on three disk sides: a scenario disk, which boots, and the two sides of a game disk. They aren't Apple DOS disks. SSI used its own operating system, RDOS 3.3 (by Roland Gustafsson), with a catalog of its own and files stored in one piece. The first tool was a reader for it, to get the files out.

The files turned out to be a game cut into modules, each named by one letter and loaded in turn by small BASIC loaders (V1–V6) that swap disks as needed:

ModuleWhat it does
Q, PStart of turn: news, nations falling, the turn's events, and the news screen with its animated pictures
W, A, KQuests and heroes, reinforcements and replacements, subversion
DA human player's movement phase
B, CThe computer's turn: choosing objectives, then moving
J, F, H, INaval combat; land combat orders, results, and the tactical battle display
E, SDiplomacy; status, score and initiative
U, GThe opening menu; saving games

Underneath them all sits DRAW.F, a machine-code library that draws the map, moves units on it, and answers questions such as "who is in this square?" and "which enemies are within four squares?". The whole game state is one 12 KB block of memory, $D000–$FFFF, in the Apple's extra 16 KB. A saved game is simply that block, written out as three 4 KB files.

3Compiled BASIC

Fifteen of the modules were written in Applesoft BASIC and then compiled to 6502 machine code, which is why they run fast enough. There was no source, so the next step was a decompiler: it recognises every code pattern the compiler produced and turns the machine code back into readable BASIC, with variables named as their purpose became clear. Three modules (the opening menu, saving, and quests) were never compiled and could be read as they are.

From the decompiled code, every phase of the game was written up as exact rules: movement costs, combat losses, the side bonuses from items and leaders, the naval battles and the Maelstrom, diplomacy, quests, victory points. Where the manual and the code disagree, the code is what the game does.

PC version SSI's 1990 DOS version is the same game translated line by line into QuickBASIC. Its data files are the Apple II's own, byte for byte: the same map, the same units for every nation, the same scenario and texts. Its news, diplomacy and combat follow the same formulas; the few places where it differs are noted on the Combat, Bugs and Stray reads pages.

4Exact arithmetic

Applesoft does its sums in a 5-byte floating-point format, and the compiled code calls the Apple's own ROM routines for them. Rounding happens at different points than in modern arithmetic, so a combat result computed "the same way" in ordinary maths can come out one troop different. The rebuild therefore does its arithmetic with a bit-exact copy of Applesoft's routines, in the same order as the compiled code, and uses Applesoft's own random-number generator.

5Testing against the original

To be sure the rebuild matches, it is checked against the original game itself, not against our reading of it. The test suite includes an emulated Apple //e, written for this, that runs headless. It starts from save states made in the AppleWin emulator at chosen moments of real games, types keys into the original, and stops wherever the rebuild should stop.

The tests run the rebuild and the original side by side and press the same keys in both. After every key, the two must agree on all 12 KB of game state, and on every pixel of the screen. The computer player's turn has no keys at all, so it replays exactly: from a save made while the computer was thinking, the original ends its turn byte for byte where the rebuild does.

6The rebuild

The rebuild is written in TypeScript and runs in a browser. Its game state is the original 12 KB block, read and written at the original's addresses, and its saved games are the original's files: the original reads a save disk the rebuild wrote. Each module was ported one at a time, keeping the original's variables and structure, and accepted only when it matched the original after every key. The screens are drawn the way the original draws them, pixel for pixel. That includes the map, the battle display and the animated news pictures, and the pauses run as long as the original's, measured in the 6502's own clock cycles.

The rebuild's code is all its own: it reads the map, the units, the graphics and the texts from the original's disk images, as the original does. The images in circulation have one blank sector, in the file of news texts, which garbles thirteen of the news reports: they start mid-sentence and stop short. The rebuild plays from a copy of disk 1 with that sector restored, read from a flux capture of an original boxed disk. The disks page tells the story, with the original's copy protection and how the 1989 crack removed it.

7Whole games

Once every module was in, the test was whole games. A random player starts both versions from the opening menu, picks random options, and plays to the end of the war: moving, attacking, saving games, sending diplomats and heroes. Eight games, covering a human or computer Highlord in both the scenario and the campaign, ran to GAME OVER on turn 31. The two versions agreed on every screen (up to 18,000 per game) and on every file written to the save disk.

8What we found

Matching the original this closely brings out what it really does, bugs included. A few of the findings:

  • DISK FULL only in winter. After a save fails, the save menu reads the error number, then redraws the date, which reuses the same variable for the season. So it reports a full disk only in winter; in any other season you get "IS THE SSI DATA DISKETTE INSERTED PROPERLY?".
  • The computer never falls back on Neraka. Its plan to pull every free unit back to defend its capital can never trigger: the test for it sits where it can never be true.
  • Defending dragons always use Dragon Fear when the computer commands them: it compares a random number between 0 and 1 with 50 or 75.
  • Interception stops after the first patrol. Enemy patrols do sail in to attack your fleets, but finding a square for the first one overwrites the variable that points to your unit list. For the rest of that phase, your other fleets are looked up from scratch memory instead, and get no interception check.
  • The news odds read the wrong memory. A country lookup for the news events never sets up its table, and reads the 6502's zero page as a map. Now and then that changes which news happens.

Those last two are stray reads: places where the game reads memory it never meant to. That page covers all of them, with how often each came up. Every bug found, with its effect on play and what the rebuild does about it, is listed in Bugs in the original.

The computer player's whole approach is described in The computer Highlord.

9Choices

  • Random numbers are seeded. The original draws random numbers while it waits for each key, so its sequence depends on how fast you press. The rebuild uses the same formulas and the same random-number algorithm, seeded, so a game is reproducible.
  • Stray reads are fixed. Where the original reads memory by mistake (the fleets and the news odds above, and a few rarer cases), the rebuild does what the code meant to do, unless Core bug fixes is switched off. The tests still follow the original there, so everything else stays checked. Stray reads lists each one, with what changes.
  • Bug fixes are options. Every bug found that changes the game is fixed by default, in two groups, core and rules, either of which you can switch off on the Play page to play the game as it shipped. Bugs that only touch the screen are kept. Bugs lists each one.