Lunar Lander on the Arduino

SAY WHAT???

Yeah, I know. I’m not sure from where I got the idea but I found the
source code to a Lunar Lander game in the book ‘101 BASIC Computer
Games’ by David Ahl at (the now defunct) Creative Computers magazine.
Having copious spare time on my hands, because I’m currently
unemployed, I converted the old BASIC program to C for the Arduino.
Now I remember why I don’t do BASIC anymore… even on a Parallax
chip. :o

It’s not the greatest C code but that BASIC stuff didn’t have a lick of
comments so I just tried to get it to run pretty much like the sample
output despite the sauce on all that spaghetti. It’s purely text w/ no
graphics of any kind, even ASCII, and it’s a bit finicky about your burn
rate input . Still, it does appear to work on a '328 and is small enough
that it should work on a '168 as well. Wanna make it work on a Tiny85?
Knock yerself out!!!

A scanned page from the book is here:
http://www.atariarchives.org/basicgames/showpage.php?page=106
and much clearer source code is here:
http://www.vintage-basic.net/bcg/lunar.bas

My code follows. Give it a try for some retro-'puting.
How deep is YOUR crater??? :smiley: Enjoy!

-Rusty-

void setup()
{
    Serial.begin(115200);
    delay(1000);
    Serial.println("\t\t\t\t LUNAR");
    Serial.println("\t       CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
    Serial.println("\n\n");
    Serial.println("THIS IS A COMPUTER SIMULATION OF AN APOLLO LUNAR");
    Serial.println("LANDING CAPSULE.\n\n");
    Serial.println("THE ON-BOARD COMPUTER HAS FAILED (IT WAS MADE BY");
    Serial.println("XEROX) SO YOU HAVE TO LAND THE CAPSULE MANUALLY.");
}

float L, A, V, M, N, G, Z, K, T, S, W, I, J;

float input(char *p)
{
    int ans;
    byte n;

restart:
    ans = 0;
    if (p == NULL)
        Serial.print("? ");
    else
        Serial.print(p);
    
    for (;;) {
        if (Serial.available()) {
            n = Serial.read();
            Serial.print(n, BYTE);
            if ((n == '\r') || (n == '\n')) {
                Serial.println();
                break;
            }
            else if (n == '\010') {     // backspace
               Serial.print(" \010");
               ans /= 10;
            }
            else if (isdigit(n)) {
                ans = ans*10 + (n - '0');
            }
            else {
                Serial.println("\n\rERROR: BAD INPUT.  TRY AGAIN");
                goto restart;
            }
        }
    }
    return (float)ans;
}


void landing()
{
    W = 3600.0 * V;
    Serial.print("ON MOON AT ");
    Serial.print(L);
    Serial.print(" SECONDS - IMPACT VELOCITY ");
    Serial.print(W);
    Serial.println(" MPH");
    
    if (W <= 1.2)
        Serial.println("PERFECT LANDING!");
    else if (W <= 10.0)
        Serial.println("GOOD LANDING (COULD BE BETTER)");
    else if (W <= 60.0) {
        Serial.println("CRAFT DAMAGE... YOU'RE STRANDED HERE UNTIL A RESCUE");
        Serial.println("PARTY ARRIVES.  HOPE YOU HAVE ENOUGH OXYGEN!");
    } else {
        Serial.println("SORRY THERE WERE NO SURVIVORS. YOU BLEW IT!");
        Serial.print("IN FACT, YOU BLASTED A NEW LUNAR CRATER ");
        Serial.print(W*0.277);
        Serial.println(" FEET DEEP!");
    }
    
    Serial.println("\n\n\nTRY AGAIN?");
}

void sub420()
{
    float q, q2, q3, q4, q5;
    
    q  = S * K/M;
    q2 = q * q;
    q3 = q * q2;
    q4 = q * q3;
    q5 = q * q4;
    
    J=V+G*S+Z*(-q - (q2/2.0) - (q3/3.0) - (q4/4.0) - (q5/5.0));
    I=A-G*S*S/2.0-V*S+Z*S*(q/2.0 + q2/6.0 + q3/12.0 + q4/20.0 + q5/30.0);
}

void sub330()
{
    L += S;
    T -= S;
    M -= S*K;
    A = I;
    V = J;
}

void sub340() {
    float D;
    for ( ;S >= 5.0e-3; ) { // line 340
        D = V + sqrt(V*V+2.0*A*(G-Z*K/M));
        S = 2.0 * A / D;
        sub420();
        sub330();
    }
}

void loop()
{
    char line[80];

    Serial.println("\nSET BURN RATE OF RETRO ROCKETS TO ANY VALUE BETWEEN");
    Serial.println("0 (FREE FALL) AND 200 (MAXIMUM BURN) POUNDS PER SECOND.");
    Serial.println("SET NEW BURN RATE EVERY 10 SECONDS.\n");
    Serial.println("CAPSULE WEIGHT 32,500 LBS; FUEL WEIGHT 16,500 LBS.");
    Serial.println("\n\n\nGOOD LUCK");
    L=0;
    Serial.println("\nSEC\tMI + FT \tMPH\tLB FUEL\t\tBURN RATE\n");
    A=120.0; V=1.0; M=33000.0; N=16500.0; G=1.0e-3; Z=1.8;

    for (;;) {                        // line 150
        sprintf(line, " %-d\t %d  %-4d\t %d\t %d\t\t",
                         (int)L, (int)floor(A),
                          (int)floor(5280.0*(A-floor(A))),
                      (int)(3600.0*V), (int)(M-N));
        Serial.print(line);
        K = input(NULL);
        T = 10.0;

        for (;;) {                    // line 160
            if ((M-N) < 1.0e-3) {
                Serial.print("FUEL OUT AT ");
                Serial.print(L);
                Serial.println(" SECONDS");
                S = (-V + sqrt(V*V + 2*A*G))/G;
                V += G*S;
                L += S;
                landing();
                return;      // Game over, Man!
            }
    
            if (T < 1.0e-3)  // line 170
                break;
                
            S = T;
            if (M < (N+S*K))
                S = (M-N)/K;

            sub420();
            if (I <= 0.0) {
                sub340();
                landing();
                return;
            }
            
            if ((V <= 0.0) || (J >= 0.0)) {
                sub330();
                continue;     // back to line 160
            }
        
            // line 370
            do {
                W = (1.0 - M*G/(Z*K))/2.0;
                S = M*V/(Z*K*(W+sqrt(W*W+V/Z))) + 0.05;
                sub420();
                
                if (I <= 0.0) {
                    sub340();
                    landing();
                    return;
                }

                sub330();
                if (J > 0.0)
                    break;
            } while (V > 0.0);
        }
    }
}

Nice one. Now someone has to modify this with a physical device to represent the lander.

Gordon

Great stuff... I've been thinking about doing something like this myself!

MUGWUMP.. CHOMP.. HAMARABI.. I have a stash of code that hasn't seen the light of day for decades. I'm not sure how many were ever printed, much less how many still exist (though I'm sure the code's actually still out there on the net).. but I have an original copy of DEC's original "Game Programming in PDP BASIC"- and I've been thinking about porting those great little throwacks. Somehow, I don't think we can get a MORIA instance going on an AVR though.. well, then again... hmm...

Great job!

Nice one. Now someone has to modify this with a physical device to represent the lander.

Gordon

You're in luck: http://www.lushprojects.com/lunarlander/

Heh - this is pretty nice; I've got all three of David Ahl's books (there's a third book, can't remember the title - that's more rare than the yellow and red books you normally find; it has a blue cover).

That takes some guts converting that old spaghetti code BASIC to C; I've done something similar before to Python (a text adventure game) - its a great way to learn a new language (I was learning Python at the time).

If you are ever able to get the version of Star Trek in that book up and going, you will have performed a heroic feat; the length alone is insane, but its also pretty memory intensive. I'm not sure the standard Arduino has the RAM for it (the code would probably fit, though). As a kid, I once tried to make it fit on my TRS-80 Color Computer 2, and wasn't very successful; but I attribute that more to my lack of experience than the machine.

Awesome work...

;D

Cr0sh, Thanks. I just looked at the source code for that StarTrek game. YOW!!! I may be crazy but I'm not insane!!! :o I think I'd more likely port the Unix version than get myself entangled in that mess. Thanks, but no thanks.

-Rusty-

You're in luck: http://www.lushprojects.com/lunarlander/

That one is awesome! (especially the 3D graphics :P)

Cr0sh: I'm intrigued... to what Star Trek game are you referring?

Cr0sh: I'm intrigued... to what Star Trek game are you referring?

In David Ahl's "Basic Computer Games" (Microcomputer Edition - there were other editions available - some were part of the documentation for certain minicomputers at the time, such as DEC VAX, IIRC), I am referencing the paperback yellow cover version; on pages 157-163 is a version called "Super Star Trek"; the "Star Trek" games were really popular, since the late 1960's - played on (likely) teletype systems, written for various systems. This version presented in the book, was taken from an earlier book called "101 Basic Computer Games" (also by Ahl), which he title "SPACWR" (Space War, which he notes is an incorrect name). Probably one of (if not -the-) earliest computer-based real-time-strategy games...

Another good one to convert would be the game "Seabattle" from pages 143-149 in Ahl's book "More Basic Computer Games" (red cover). This one "simulated" being in command of a submarine sinking ships around an island. Its too bad there aren't any 80x24/25 character LCDs available; all sorts of cool games could be made. At least we have things like the TVOut library, and the various other TV/VGA converters available.

Finally, I wanted to plug the third book by Ahl; contrary to what I wrote earlier, it doesn't have a blue cover, but I think of it as the "blue book"; the cover is a blue border around a yellow background. Its entitled "Big Computer Games" (there's only 12 such games, in 115 pages). Strangely enough, many of these games seem smaller than even "Seabattle", let alone "Super Star Trek" - although a few are big (such as "Cribbage", "Lost & Forgotten Island", "Monster Combat" (which faintly resembles a stripped down version of Rogue), and "Streets of the City"). The games are all written in a version of Microsoft BASIC, so are fairly easy to convert to other BASICs (and if you have a familiarity with MS BASIC - to other languages as well; I have found Python to be a good match for conversion use).

Hmm - I've been thinking about entering the next "Retrochallenge" (http://retrochallenge.org/); perhaps a conversion of one or two of these larger games to Python might be a good way to try out the contest, among other possibilities on my plate (I have a ton of them, unfortunately!)...

:)

Any pics/videos?

Cr0sh:

Thanks! And thanks for a detailed answer! I've never seen any of the books you mention though.

Cool!!

I also have on mind this other code, from the guide-System of the Apollo missions:

http://en.wikipedia.org/wiki/Apollo_Guidance_Computer

and here are the links to the code: http://googlecode.blogspot.com/2009/07/apollo-11-missions-40th-anniversary-one.html

Enjoy!

Thanks! And thanks for a detailed answer! I've never seen any of the books you mention though.

They've been out of print for 20 years or more; you can almost always find the first book (the "yellow" book) in a used bookstore somewhere. Sometimes you can also find the second "red" cover book. The third one seems something of a rarity; I've only seen one copy of it (one of which I own) - and I got that off Ebay. That doesn't mean its impossible to find, I just haven't looked that hard (maybe Abe Books or Alibris might turn up something).

The books are really only worthwhile for antique microcomputer enthusiasts (and for the first two books, those who like the robot drawings of George Beker - http://www.bekerbots.com/); the code is a wonderful collection of old BASIC code, at a time when BASIC (for the enthusiast who was lucky enough to own their own microcomputer) was the language to use (I am sure if the resources were there, these same enthusiasts would've been writing C programs, but that wouldn't be possible on most personal computers until around the mid-1980s - and even then, it was pretty expensive to buy a compiler).

I was only a kid then, but I enjoyed these books, and the BASIC code that flowed in a community that was mostly linked by magazines and snail mail (and the occasional user group if you lived in a large enough place - I unfortunately didn't)...

:)

If you are really keen…

http://www.dunnington.u-net.com/public/startrek/

I just tried this little gem as a sketch, with a little “massaging” it might work
http://outserv.cactus.org/~nystrom/startrek.c

I still have a copy of the “Dilithium Press” book, “Starship Simulation”,
which back in the day would have been a bitch to implement.
Most of the book is taken up with a kind of “pseudo-code” listing of the whole program flow, module by module.

The basic idea is all the stations on the Enterprise bridge are implemented, whether that be knobs and switches or touchscreens.
There would be a simulation controller, sort of like a dungeon master controlling the simulation.

These days we could do it with a few Arduino’s, big LCD’s, either straight serial, XBee, Bluetooth, Ethernet or WiFi.

Since I got my Telemate shield and one of those big ass 160x128 blue and white LCD’s Sparkfun sell I’ve been working on a “Classic” Strek!

Seriously if this looks familiar, it’s time for a prostate/diabetes/blood pressure check! :wink:

±–±–±–±–±–±–±–±–+

  • O-E +
  • ±< +
  • ±< +
    ±–±–±–±–±–±–±–±–+
    Command

Seriously if this looks familiar, it's time for a prostate/diabetes/blood pressure check!

It just isn't the same without the chatter and thumping of the teletype.

sigh, I know! My parents made me move my ASR33 out to my shed, then I had to sound proof the shed when the neighbours complained! Especially when I had my SYM-1 (which I still have and use!) decoding RTTY. Rueters and AAP news services! I don't have the ASR 33 telly type any more, but I have a copy of Star Trek on paper tape!

You know what? I think I will write a STREK for my Mega! Tellymate shield for the main viewer, big LCD for helm control, buttons and switches for controls!

By the way, does anyone remember "Phasor Blast", I think Mattel made them.

CrOsh, dude, I had both the yellow and the red books!

I banged out most of those programs in TiBASIC on a Ti99-4A. And saved them to a cassette tape.

Kids today with their all free compilers, and wiz bang hi res graphics...and get off my cyber-lawn!

Does anyone remember when Byte used to publish code for HP programmable calculators as bar codes/ That was cool and saved a lot of typing!

It's funny in way that with the Arduino platform we've gone back to around the same memory footprint as an Apple II, Commodore Pet, TRS-80 etc. A tad faster though!

Does anyone remember when Byte used to publish code for HP programmable calculators as bar codes

It wasn't just HP: they were promoting "Paperbytes" as a solution for lots of machines. Too bad we didn't have cheap inkjets back then: would've beat the bleep out of cassettes.

Now all we need is for someone to convert Zork to be playable in the serial monitor window! :P