How I rewrote the 1978 text-only Super Star Trek game

During the last two weeks, I spent all my free time working hard on one of the most ridiculous achievements of my life: dissecting, understanding, and rewriting the old 1978 Super StarTrek videogame.

If you don’t know what I’m talking about, it’s an old text-only game, a sort of early example of turn-based space strategy sim, written in BASIC. In this game, you are the captain of the starship Enterprise, and your mission is to scout the federation space and eliminate all the invading Klingon ships. You will have to carefully manage the ship energy, use phasers and torpedoes to destroy the Klingons, and find starbases to repair damages and replenish your energy. All of this, rendered with a few characters on screen and a lot of imagination.

You control the Enterprise entering commands at the prompt: NAV to move the ship, PHA to fire with the phasers, LRS to scan the quadrants with your long-range sensors, and so on. Despite its simplicity, it’s a great example of programming and game design.

Main screen of Super Star Trek (1978 version)

If you are curious about this game's story, how it looks like, or how to play it, I wrote another article about Super Star Trek here. Otherwise, know that it was created in 1974 and became hugely popular when it was published, in an improved version written by Bob Leedom, in the 1978 book BASIC COMPUTER GAMES.

I’m not talking about a game that was distributed on a disk. The book contained the code, and people had to type it on their computers. Since BASIC was a universal language at the time, the game code could work on many different machines.

What does this have to do with me?

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 copied it all on my VIC-20 as soon as I arrived home and started to play it. It was simple, but I immediately fell in love with it. It was maybe the best game I have ever typed on that computer.

Star Trek on the VIC-20

It took me 37 years to realize that this program was actually an adaptation of Leedom’s Super Star Trek. I discovered a few months ago when I bought a copy of David Ahl’s BASIC COMPUTER GAMES on eBay — yes, a book that was released in 1978.. better late than never!

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 just went to the Vintage Basic website, I downloaded the BASIC interpreter, the source code of the game, and I run it.

Leedom’s version is really fantastic and, of course, much better than the reduced version I had to play on my VIC-20.

I know what you are thinking; it’s just nostalgia and so on. But the reality is, I actually had a lot of fun playing it.

Picture this: at some point, after a difficult battle with three Klingon ships, both my short-range sensors and long-range sensors were damaged. The energy was almost depleted. Time was almost over, so I deviated the shields’ power to the engines — as Scott would do. Then, I tried to reach the nearest starbase using only the navigation computer. But when I entered the quadrant, the computer went off too. Damn! So I attempted to navigate blindly, hoping to dock to the starbase.. and I did it! Pity that in the end, Klingons won, but hey, it was fun.

Rewriting the code

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 code was cryptic and 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 StarTrek. These new editions featured cloaking devices, supernovae, death rays, Romulans, and more. But I wanted the original one, so I decided to do the porting myself in the end. That’s the best way to understand a program, right?

Initially, I chose to write it in Perl because it’s a scripting language that can be run from any Mac or Linux, and because Perl has a goto statement. I didn’t think it was possible, at least initially, 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 much as possible, to save memory — which means each row contains several instructions separated by a semicolon and without any space. Look at these lines, for example:

A few lines of the 1978 BASIC code

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 these goto there; otherwise, I would not be able to read it. So I began transforming some if-then-goto, into if-then-else blocks.

If you check the code above, you can see, at line 3170, a "THEN3500", that is basically a break, because it stops the "for" loop started at the beginning of the line (for the ones that never programmed in BASIC, you can type "IF THEN instead of "IF - THEN GOTO "). So in my code, it became a break ("last" in Perl).

On the next line, you will see a THEN3360 which is, in fact, a continue statement (next in Perl), because it ends the current iteration. The GOTO3370 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 game mechanics. As long as 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”.

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 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.

After the Perl version, I decided to port the game to LUA. To do this, I basically checked all the code from scratch. In Perl, I 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. But, it was useful in the end. I have to admit the code is much better now, and I know all the parts very well.

I left a lot of comments, for me and anyone who wants to understand the code. You will find things like this.

My LUA version - with a lot of comments

Clearly, the work will never be finished; the code can be improved a lot more. But for now, it’s fully working; the game looks and behaves exactly like the original one — mission accomplished.

If you are ready to play, check the link on the left to download my version of Super Star Trek. If you are a developer you can also go to my GitHub account to find both the LUA and Perl versions.

What's next

I'd like to continue working on this game, but I'm not sure how. There's no point in adding more features; many others did it already, and better than me. There's no point in adding more weapons, more enemies, and more things. I like the "classic" experience, and I don't want to change it.

Maybe what I would like to do is keep the existing mechanics, but build something "on top." Would it be possible to add a story and some adventure elements? Can I try to recreate the feeling of a real TV episode with this simple game mechanics? That would be a nice experiment. Let's see.

For now, I hope you enjoyed the article and that you will try my version of the game. Happy star trekking!

UPDATE FEB 2023: I realized I had to update this "What's next" paragraph because my work on this project is still going on. Last year I published a PICO-8 version of Super Star Trek. Recently I released Super Star Trek (1978) meets Star Trek 25th Anniversary, a conversion of Super Star Trek using the graphics of the Interplay adventure.

If you want more technical details, this post about a version that I called the TOS version contains some details about the changes/improvements I made to the original code (e.g., trigonometric functions, persistent objects, etc.). The PICO-8 and 25th versions include these improvements.

Thanks again, Mister Leedom!

If you liked this article, share it!