Super Star Trek
03 · Code

Rewriting the code: from BASIC to Perl and Lua

How I turned the 1978 BASIC listing of Super Star Trek into a faithful Perl version, then a faithful Lua version, and finally into the enhanced Lua version: the text-only game you play in the terminal of this site.

How it started

In 1983 I bought the Italian edition of a book called Zap! Pow! Boom! Arcade Games for the VIC-20, by Mark Ramshaw. It contained 30 games for the VIC-20, written in BASIC. The 8th game was called Star Trek. I typed all of it on my VIC-20 as soon as I got home and started to play. It was simple, but I immediately fell in love with it. It was maybe the best game I ever typed on that computer.

A Star Trek game for the VIC-20: a Klingon vessel approaches
Star Trek on the Commodore VIC-20, the kind of game I typed in 1983

It took me 37 years to realize that this program was an adaptation of Super Star Trek, the game written in BASIC by Bob Leedom in the 1970s (the whole story is in The story of Super Star Trek). I found out in 2020, when I bought a copy of David Ahl's BASIC Computer Games on eBay — yes, the book released in 1978. Better late than never, right?

Of course, I immediately decided to try the original Super Star Trek. Thank God, this time I didn't have to type all the code: I went to the Vintage BASIC website, downloaded the BASIC interpreter and the source code of the game, and ran it.

Leedom's version is really fantastic and, of course, much better than the reduced version I had played on my VIC-20. I know what you are thinking: it's just nostalgia. But the truth is, I had a lot of fun playing it.

The original 1978 game running on a modern computer
The original Super Star Trek, running on a modern computer: the phasers at work

The faithful versions: Perl and Lua

After playing for a while, I googled a bit to see if, during all these years, someone had rewritten the game in a more readable format. The 1978 BASIC code was cryptic, with almost no comments explaining the algorithms.

I found many game ports on the Internet, like this excellent one, but none was the original 1978 Super Star Trek. These new editions featured cloaking devices, supernovae, death rays, Romulans, and more. But I wanted the original one, so in the end I decided to do the porting myself. That's the best way to understand a program, right?

Perl

Initially, I chose to write it in Perl because it's a scripting language that can run on any Mac or Linux machine, and because Perl has a goto statement. I didn't think it was possible, at least at first, to port all the original BASIC code without using any goto.

But even the simple translation was tougher than I expected. Imagine porting 500 lines of BASIC code, packed as tightly as possible to save memory — which means each row contains several instructions separated by a colon and without any space. Look at these lines, for example:

A piece of the original BASIC listing, lines 3070 to 3450
The movement of the Enterprise in the original BASIC listing: several instructions on each line and jumps everywhere

I patiently rewrote every line, carefully trying to avoid any mistake: if you miss a character, everything can change. I started with a 1-to-1 translation, but I soon realized that I could not leave all those gotos there, otherwise I would not be able to read the code. So I began transforming some if-then-goto statements into if-then-else blocks.

If you check the code above, you can see, at line 3170, a THEN 3500, which is basically a break, because it stops the FOR loop started at the beginning of the line. (For those who never programmed in BASIC: you can write IF … THEN 3500 instead of IF … THEN GOTO 3500.) So in my code, it became a break (last in Perl).

On the next line, you will see a THEN 3360, which is in fact a continue statement (next in Perl), because it ends the current iteration. The GOTO 3370 you can see at line 3350 is another break. And so on.

It was not always so easy. Soon I realized that some gotos were jumping to unexpected places, like the middle of a function, or the middle of a THEN block. It was like a labyrinth.

Patiently identifying and separating all the code blocks was the most difficult part of the job, but it was very useful for understanding the game mechanics. While I was getting familiar with the code, I also gave meaningful names to the variables, such as “EnergyLevel” instead of “E” or “TotalKlingonShips” instead of “K3”.

The code now is very different, but I paid attention not to change the mechanics or the math behind the game algorithm. The game should play exactly like the BASIC version.

You might ask, why would anyone do that on a game written in 1978? I know, it's crazy. But I wanted to finish it, I wanted to make the code “mine”. And I wanted to clean up the mess: it was tough to go to sleep with the code unfinished, leaving the mess behind. In the end, I made it, and I was really proud of it.

Lua

After the Perl version, I decided to port the game to Lua. To do this, I basically checked all the code again from scratch. In Perl, I had used a lot of next (aka continue) and last. But Lua doesn't have a continue statement, so I had to rewrite most of the loops. It was useful in the end: I have to admit the code is much better now, and I know every part of it very well.

The same movement loop in the Lua version
The same loop in Lua: readable names, and comments that explain the tricks of the original

Both versions, the Perl one and the Lua one, are 100% faithful to the BASIC original, and they play exactly like the 1978 game.

The faithful Lua version at the beginning of a mission
The faithful version at the start of a mission: the orders, the Enterprise in ASCII art and the first sector
The end of a mission in the faithful Lua version
The end of a mission: the faithful version prints even the typos of the 1978 text (“THEN LAST”, “FDERATION”)

The source code of the Perl version, the Lua version and the enhanced Lua version is free to download from my GitHub.

The enhanced Lua version

After the 100% faithful Lua conversion, I decided to add some improvements. Not really to the gameplay, but to the overall experience. My main goal was to make the original cast more “present”. After that, I decided to improve the code with trigonometric functions and other things. I call this version the TOS version, after the original series.

The cast of the original Star Trek series on the bridge of the Enterprise
The original cast on the bridge: my goal was to make them more present in the game

Game messages

Spock, Scott, Chekov, and the others make a brief appearance in the original game, with some occasional messages. I thought it would be fun if all the game messages were not invented, but taken from the original transcripts of the Star Trek episodes. So I downloaded the transcripts of all the episodes of the 3 seasons of the series. Then I opened the game code and, for each message, I searched through the text to find something that the original characters had said and that could be appropriate. In most cases, I was able to use the lines without changing anything. In some cases, I had to adapt them a bit, trying to maintain the character's style. So now, when you use the phasers, you will read:

KIRK: Fire phasers, Mister Chekov. (from the episode Obsession)

CHEKOV: Main phasers armed and ready, sir. (The Trouble With Tribbles)

If the phasers don't damage the Klingons, Chekov will say:

CHEKOV: Phasers ineffectual, sir. (Obsession)

When you use the long-range sensors:

KIRK: Long-range sensor scan, Mister Sulu. (The Omega Glory)

SULU: Sectors surrounding 7,7 charted and examined. (Who Mourns for Adonais?, adapted a bit)

When you find a Klingon, you will see:

KIRK: All hands, battle stations. Red alert. I repeat, red alert. (Arena)

Here is the same moment, the arrival in a sector with a Klingon, in the faithful version and in the enhanced one:

The faithful Lua version when entering a sector with Klingons
Faithful version: “NOW ENTERING ‘REGULUS I’ QUADRANT…” and “COMBAT AREA”
The enhanced Lua version when entering a sector with Klingons
Enhanced version: Sulu announces the sector and Kirk calls the red alert, in the words of the episodes

And of course, you have the captain's log at the beginning. Now you can really “hear” the original voices. I find it pretty cool.

Trigonometric functions

The original game code didn't use any trigonometric functions, probably because sin() and cos() were not available in BASIC when the original Star Trek game was written. The code uses some clever math tricks to calculate the course between 2 points and the vector of the movement of the Enterprise.

The course calculation is not 100% accurate, but considering that the screen is small (only 8×8 cells), the error is negligible. In fact, my torpedoes never missed a target because of this. In any case, I rewrote this part using math.atan2, mostly because I want the code to be safe if I decide to increase the screen size in a new version. There is no impact on the gameplay.

The movement of the Enterprise, instead, is heavily impacted by the introduction of trigonometry. Let's see why.

The distance covered by the Enterprise is 8 cells × warp speed. For example, when the ship moves to the right at warp speed 1, it covers a distance of 8 cells. A warp speed of 0.5 is 4 cells, and so on. All good as long as the movement is horizontal or vertical.

But in the original code, when the Enterprise moves at 45° — let's say north-east — at warp speed 1, it goes 8 cells up and 8 cells right, which is a distance of 11.3, not 8! If it moves north-north-east, something in the middle: 8 cells up and 4 cells right, which is a distance of 8.9. Basically, the Enterprise is faster when the direction is close to 45°.

Rewriting the code, I introduced sin() and cos() to calculate the movement, so the math is now correct: warp speed 1 is always a distance of 8, regardless of the angle.

Two grids showing how far warp speed 1 takes the Enterprise: in the original game the reach is a square, in the enhanced version it is a circle
How far warp speed 1 takes the Enterprise in each of the eight courses (the numbers are the courses of the game). In the original game the reach is a square: a diagonal course covers 11.3 cells. In the enhanced version it is a circle: always 8 cells.

The gameplay is impacted because, in the original version, when you move at warp speed 1, you always land in another sector. This is not true anymore when you move diagonally.

This makes the game more challenging. In the original game you never had to think about the cell where the Enterprise is inside the sector: a diagonal course at warp speed 1 always moved you by exactly one sector vertically and one sector horizontally, wherever you started from. Now the position of the Enterprise inside the sector matters: if you start close to the border you will cross it, but if you start far from it you may stay in the same sector. So, to decide the course and the speed, you also have to consider where the Enterprise is inside the sector.

Persistent objects

To save memory, the author decided to split the game map into sectors (called quadrants in the original version, see below) and to store only the number of stars, enemy ships, and starbases present in each one. When the Enterprise enters a sector, the program places the required objects in random locations. If there are enemy ships, it also calculates their energy. This is a pretty smart trick: this way, the entire galaxy is stored in an 8×8 array.

The side effect is that when you go back to a sector you have already visited, the stars and starbases are in different positions. Even worse, if you had reduced a Klingon to 1 point of energy but, for some reason, left the sector without destroying it, when you go back you will find a fresh new Klingon ship.

In my version, I introduced a 64×64 array to store every cell of the galaxy. There is also an array of all the Klingon ships, with their coordinates, energy level, and even their name. If you leave a sector and later go back to it, you will find the same situation.

More than an improvement to the game, this is meant to support future development and additional features (such as Klingons that move from one sector to another to hunt you).

Other changes

The original game imagines that the Enterprise is patrolling the entire galaxy, divided into 64 quadrants. Each quadrant is a portion of space with usually fewer than 10 stars, up to 3 Klingon ships, and, if you are lucky, a starbase.

Clearly, this doesn't make any sense: the galaxy has hundreds of billions of stars. In reality, the game screen is more similar to the definition of “sector” in the Star Trek lore. According to Memory Alpha, a sector is a volume of space approximately twenty light-years across. A typical sector in Federation space will contain about 6 to 10 star systems.

So I changed the game's terminology, using the word sector instead of quadrant. Obviously, the Enterprise is not patrolling the entire galaxy, but just a portion of space about 160×160 light-years wide — a bit more reasonable.

Unfortunately, despite this change of scale, the game time doesn't make any sense. The Enterprise can move from one sector to another at warp speed 1 in just 1 day. But warp speed 1 is the speed of light, so the Enterprise would take 20 years to reach the next sector, not 1 day. Simulating the real warp speed would change the game too much, because warp speed is exponential.

The version you play on this site

The enhanced Lua version is the one you play in the terminal of this web version of Super Star Trek: the mechanics are the ones described above, the lines of Kirk, Spock and the rest come from the episodes, and the code has been ported to TypeScript to run in your browser. Everything around the terminal — the HUD, the animations and the sounds — is new.

The enhanced version in the terminal of this web version
The same game, in the terminal of this site

If you want to see how it plays, press Play, or read How to play.