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 videogame. 1978 Super StarTrek If you don’t know what I’m talking about, it’s an old text-only game, a sort of early example of , 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. turn-based space strategy sim You control the Enterprise entering commands at the prompt: to move the ship, to fire with the phasers, 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 . Otherwise, know that it was created in 1974 and became hugely popular when it was published, in an improved version written by , in the 1978 book . NAV PHA LRS here Bob Leedom 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 by Mark Ramshaw. It contained 30 games for the VIC-20, written in BASIC. The 8th game was called . 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. Zap! Pow! Boom! : Arcade Games for the VIC-20 Star Trek 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 ’s BASIC COMPUTER GAMES on eBay — yes, a book that was released in 1978.. better late than never! David Ahl 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 , I downloaded the BASIC interpreter, the source code of the game, and I ran it. Vintage Basic website 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. 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. Don’t you believe me? See . here I found many game ports on the Internet, like , 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? this excellent one 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 statement. I didn’t think it was possible, at least initially, to port all the original BASIC code without using any . goto 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: X2=C(C1, )+(C(C1+ , )-C(C1, ))*(C1- (C1)):Q4=Q1:Q5=Q2 FORI= TON:S1=S1+X1:S2=S2+X2:IFS1< ORS1>= ORS2< ORS2>= THEN3500 S8= (S1)* + (S2)* - :IFMID$(Q$,S8, )= THEN3360 S1= (S1-X1):S2= (S2-X2): ; ;S1; ;S2; :GOTO3370 NEXTI:S1= (S1):S2= (S2) A$= :Z1= (S1):Z2= (S2):GOSUB8670:GOSUB3910:T8= IFW1< THENT8=. * ( *W1) T=T+T8:IFT>T0+T9THEN6220 3140 2 1 2 2 INT 3170 1 1 9 1 9 3240 INT 24 INT 3 26 2 " " 3320 INT INT PRINT "WARP ENGINES SHUT DOWN AT " 3350 PRINT "SECTOR" "," "DUE TO BAD NAVAGATION" 3360 INT INT 3370 "<*>" INT INT 1 3430 1 1 INT 10 3450 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 there; otherwise, I would not be able to read it. So I began transforming some , into blocks. goto if-then-goto if-then-else If you check the code above, you can see, at line 3170, a " ", that is basically a 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 (" " in Perl). THEN3500 break, break last On the next line, you will see a which is, in fact, a statement ( in Perl), because it ends the current iteration. The you can see at line 3350 is another . And so on. THEN3360 continue next GOTO3370 break It was not always so easy. Soon I realized that some jumping to unexpected places, like the middle of a function, or the middle of a block. It was like a labyrinth. gotos were then 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 “ ” instead of “E” or “ ” instead of “K3”. EnergyLevel TotalKlingonShips You might ask, 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. why would anyone do that on a game written in 1978? 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 (aka ) and . But LUA doesn’t have a 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. next continue last continue 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. Now, If you are ready to play, or you just want to check the code, go to my to find both the LUA and Perl versions. Or you can go to my . GitHub account personal website What's next 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