Lunar lander - Eleven source code

options
{
  app_name     = "Lunar lander";
  package_name = "lander";
}

statesafe var elapsedTime   = 0;
statesafe var height        = 1000;
statesafe var velocity      = 50;
statesafe var fuelRemaining = 150;

statesafe var burnAmount  = 0;
statesafe var newVelocity = 0;
statesafe var delta = 0;

statesafe enum { yes "Yes"; no "No"; } wantInstructions;

display
{
  print ("LUNAR LANDER");
  print ("By Dave Ahl (translation from BASIC to ELEVEN by Joe Morrison)");
  edit ("Do you want instructions?", wantInstructions);
}

if (wantInstructions eq "yes")
{
  display
    {
      print ("LUNAR LANDER INSTRUCTIONS");

      print ("You are landing on the moon and and have taken over manual
	      control 1000 feet above a good landing spot. You have a
	      downward velocity of 50 feet/sec. 150 units of fuel remain.");

      print ("Here are the rules that govern your APOLLO space-craft:");

      print ("(1) After each second, the height, velocity, and remaining
              fuel will be reported via DIGBY, your on-board computer.");

      print ("(2) After each report, enter the number of units of fuel you
	      wish to burn during the next second. Each unit of fuel will
	      slow your descent by 1 foot/sec.");

      print ("(3) The maximum thrust of your engine is 30 feet/sec/sec or
	      30 units of fuel per second.");

      print ("(4) When you contact the lunar surface. your descent engine
	      will automatically shut down and you will be given a report
	      of your landing speed and remaining fuel.");

      print ("(5) If you run out of fuel, you will no longer be prompted
              to enter the number of units to burn each second. Your second
              by second reports will continue until you contact the lunar
	      surface.");
    }
}

display
{
  print ("LUNAR LANDER");
  print ("Beginning landing procedure..........");
  print ("DIGBY wishes you good luck !!!!!!!");
}

while (height > 0)
{
  if (fuelRemaining > 0)
    {
      display
        {
	  print ("Status of your APOLLO spacecraft:");

	  print ("Time: ",   elapsedTime, " seconds");
	  print ("Height: ", height, " feet");
	  print ("Speed: ",  velocity);
	  print ("Fuel: ",   fuelRemaining);

	  edit ("Enter fuel burn amount: ", burnAmount);
	}

      if (burnAmount < 0)		{ burnAmount = 0; }
      if (burnAmount > 30) 		{ burnAmount = 30; }
      if (burnAmount > fuelRemaining)   { burnAmount = fuelRemaining; }
    }
  else
    {
      display
        {
	  print ("Status of your APOLLO spacecraft:");

	  print ("Time: ",   elapsedTime, " seconds");
	  print ("Height: ", height, " feet");
	  print ("Speed: ",  velocity);
	  print ("Fuel: ",   fuelRemaining);

	  print ("**** OUT OF FUEL ****");
        }

      burnAmount = 0;
    }

  newVelocity   = velocity - burnAmount + 5;
  fuelRemaining = fuelRemaining - burnAmount;
  height        = height - (velocity + newVelocity) * 0.5;
  elapsedTime   = elapsedTime + 1;
  velocity      = newVelocity;    
}

/* Touchdown. Calculate landing parameters. */

elapsedTime = elapsedTime - 1;
velocity = velocity - 5 + burnAmount;
height = height + (velocity + newVelocity) * 0.5;

if (burnAmount == 5)
{
  delta = height / velocity;
}
else
{
  delta = (sqr (velocity * velocity + height * (10 - burnAmount * 2)) - velocity) / (5 - burnAmount);
}

newVelocity = velocity + (5 - burnAmount) * delta;

display
{
  print ("***** CONTACT *****");

  print ("Touchdown at ", elapsedTime + delta, " seconds.");
  print ("Landing velocity = ", newVelocity, " feet/sec.");
  print (fuelRemaining, " units of fuel remaining.");

  if (newVelocity <= 0)
    {
      print ("Congratulations! A perfect landing!!");
      print ("Your license will be renewed.............later.");
    }
  else if (newVelocity < 2)
    {
      print ("A little bumpy.");
    }
  else if (newVelocity < 5)
    {
      print ("You blew it!!!!!!");
      print ("Your family will be notified..............by post.");
    }
  else if (newVelocity < 10)
    {
      print ("Your ship is a heap of junk !!!!!");
      print ("Your family will be notified..............by post.");
    }
  else if (newVelocity < 30)
    {
      print ("You blasted a huge crater !!!!!");
      print ("Your family will be notified..............by post.");
    }
  else if (newVelocity < 50)
    {
      print ("Your ship is a wreck !!!!!");
      print ("Your family will be notified..............by post.");
    }
  else
    {
      print ("You totalled an entire mountain !!!!!");
      print ("Your family will be notified..............by post.");
    }
}