# Wrapping Up the Mars Lander

In my last few articles ("Let's Go to Mars with Martian Lander" and "Mars Lander, Take II: Crashing onto the Surface"), I've been building a variant on the classic video game Lunar Lander, with a few simplifications and one big change: Martian gravity instead of lunar gravity. The moon is 1/6th of Earth's gravity; whereas Mars is about 1/3 of Earth's gravity, which makes flying a lander in for a soft descent a bit more exciting.

The tricky one might be to simulate a black hole, but that's easy to do by having a really, really big gravitational value, but not so easy to land safely. It's not hugely interesting, actually, unless you're working on the script to Interstellar 2 perhaps.

The starting parameters of the game have Martian gravity set to 3.722 meters/sec/sec, and the spaceship enters the atmosphere at an altitude of 500 meters (about 1/3 mile). Do the math, and that means players have just more than 15 seconds to avoid crashing onto the Martian surface.

### Creating the Interface

My last article (in the October 2016 issue of LJ) ended with a demonstration of code that offered second-by-second data on what was basically free fall through the Martian atmosphere:

``````
1 seconds: speed: -3.722 m/s altitude: 496.278 meters.
2 seconds: speed: -7.444 m/s altitude: 488.834 meters.
3 seconds: speed: -11.166 m/s altitude: 477.668 meters.
4 seconds: speed: -14.888 m/s altitude: 462.780 meters.
5 seconds: speed: -18.610 m/s altitude: 444.170 meters.
```
```

That's not a great way to land unless you're in a really, really well padded couch. My first stab at adding an interesting interface is to stop each second and offer users the chance to specify whether they want to fire their retro-rockets and how much fuel to burn for the subsequent second.

Burn your fuel too early, and you could end up shooting off into space or level out just to plummet to the surface anyway. In this first version, however, the user will have unlimited fuel (though in real life it'd be limited, and the vessel would lighten up, decreasing gravitational pull, as the fuel was burned).

Here's the core of the code:

``````
echo "\$time seconds into flight: speed: \$speed m/s \
and altitude: \$altitude meters."
echo -n "Fire retro rockets? (burn rate: 0-100): "
if [ -z "\$thrust" ] ; then
thrust=0
fi
```
```

The last few lines allow the player simply to press Enter and have that be the equivalent of a zero—easy enough. Now let's try to land the darn spaceship:

``````
Time: 1: Speed: -3.722 m/s and altitude: 496.278 meters.
Fire retro rockets? (burn rate: 0-100):

Time: 2: Speed: -7.444 m/s and altitude: 488.834 meters.
Fire retro rockets? (burn rate: 0-100):

Time: 3: Speed: -11.166 m/s and altitude: 477.668 meters.
Fire retro rockets? (burn rate: 0-100):

Time: 4: Speed: -14.888 m/s and altitude: 462.780 meters.
Fire retro rockets? (burn rate: 0-100): 15

Time: 5: Speed: -3.610 m/s and altitude: 459.170 meters.
Fire retro rockets? (burn rate: 0-100):

Time: 6: Speed: -7.332 m/s and altitude: 451.838 meters.
Fire retro rockets? (burn rate: 0-100):

Time: 7: Speed: -11.054 m/s and altitude: 440.784 meters.
Fire retro rockets? (burn rate: 0-100):

Time: 8: Speed: -14.776 m/s and altitude: 426.008 meters.
Fire retro rockets? (burn rate: 0-100): 15

Time: 9: Speed: -3.498 m/s and altitude: 422.510 meters.
Fire retro rockets? (burn rate: 0-100):

Time: 10: Speed: -7.220 m/s and altitude: 415.290 meters.
Fire retro rockets? (burn rate: 0-100):

Time: 11: Speed: -10.942 m/s and altitude: 404.348 meters.
Fire retro rockets? (burn rate: 0-100):

Time: 12: Speed: -14.664 m/s and altitude: 389.684 meters.
Fire retro rockets? (burn rate: 0-100): 15

Time: 13: Speed: -3.386 m/s and altitude: 386.298 meters.
Fire retro rockets? (burn rate: 0-100):
```
```

Notice here that I am being conservative with the fuel, firing the thrusters at 4, 8 and 12 seconds. This allows me to be 13 seconds into the descent and have a speed of only 3.386 m/s, while dropping from 500 meters to 386 meters. If the fuel holds out, this isn't a bad landing strategy!

______________________

Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.