Wednesday, 18 February 2009

Erlang Text Elite

This last weekend, I somehow came to think about the wonderful old space trading game Elite, and the way it managed to cram a vast universe of planets with names, stats, and fun descriptions (along with the 3D space flight simulation, which was an amazing feat in itself) into the tiny 8-bit home computers of old. Only minutes later, I had found Ian Bell's Text Elite web page, where Ian, one of the two original authors of the game, has published a reconstruction in C of the main universe-generating algorithm, along with a simple text interface to actually let you "play", i.e., buy, sell, and jump between planets. Great!

So I rewrote it in Erlang. And had a lot of fun doing it. Apart from the nostalgic kick, there were two points with the exercise: First of all, I just wanted to see the main algorithm as cleanly implemented as possible. But second, I wanted to see what it would be like to rewrite a piece of classic game code that was very clearly not written with functional programming in mind (though of course already cleaned up a bit by being rewritten in C from the original 6502 assembler). I think I like how the result turned out; it's quite clean and readable, and the data flows are easy to follow. I also found and fixed a bug in the "goat soup" function.

The code can be found on GitHub, along with the two scripts for testing found on Ian's page. You can run them like this:
erl -noshell -s txtelite main -s init stop < sinclair.txt
(To play yourself, just skip the last "< sinclair.txt" part.) Of course, you need to compile the txtelite.erl file first. If you're new to Erlang, this is how:
erlc txtelite.erl
It's good to be back on Lave again!

No comments: