I love retro games, retro programming and pastry making
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 manage the ship energy carefully, 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.
If you are curious about this game's story, how it looks, 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.
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 played on that computer.
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 ran 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, the Klingons won, but hey, it was fun.
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. Don’t you believe me? See here.
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:
3140 X2=C(C1,2)+(C(C1+1,2)-C(C1,2))*(C1-INT(C1)):Q4=Q1:Q5=Q2 3170 FORI=1TON:S1=S1+X1:S2=S2+X2:IFS1<1ORS1>=9ORS2<1ORS2>=9THEN3500 3240 S8=INT(S1)*24+INT(S2)*3-26:IFMID$(Q$,S8,2)=" "THEN3360 3320 S1=INT(S1-X1):S2=INT(S2-X2):PRINT"WARP ENGINES SHUT DOWN AT "; 3350 PRINT"SECTOR";S1;",";S2;"DUE TO BAD NAVAGATION":GOTO3370 3360 NEXTI:S1=INT(S1):S2=INT(S2) 3370 A$="<*>":Z1=INT(S1):Z2=INT(S2):GOSUB8670:GOSUB3910:T8=1 3430 IFW1<1THENT8=.1*INT(10*W1) 3450 T=T+T8:IFT>T0+T9THEN6220
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 "
", 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<line-number"> instead of "IF - THEN GOTO <line-number>"). So in my code, it became a break ("
" in Perl).
On the next line, you will see a
which is, in fact, a
in Perl), because it ends the current iteration. The
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
. 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.
While coding, I left a lot of comments, for me and anyone who wants to understand the program. You will find things like this.
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.
I think I'd like to continue working on it, but I'm not sure how. There's no point in adding more features, many others did it already, and better than me for sure. 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? Is it possible 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!
Also published on: http://emabolo.com/article/i-rewrote-the-1978-text-only-super-star-trek-game
Create your free account to unlock your custom reading experience.