#!/bin/sh
exec lush "$0" "$@"
!#

;; A Simple Lunar Lander

(when (or (member "-h" argv) (member "--help" argv))
  (writing "$stderr" 
   (render-brace-text 0 72
   '{<p> 
     Synopsis: ,,(basename (car argv)) [-h] [-t thrust] [-g gravity] [-m mass] [-f cx]

     A simple Lunar Lander game. 
     {<ul>
      {<li> -h: show this message}
      {<li> -t thrust: set the thrust (default: 400).
             The unit is pixels per second per second per unit of mass}
      {<li> -g gravity: set the gravity (default: 200).
             The unit is pixels per second per second}
      {<li> -m mass: set the mass (default: 1).
             The unit is arbitrary (say tons).}
      {<li> -f cx: set the "atmosphere" friction coefficient (default: 0).
             The friction force is equal to cx*velocity^2.
             The unit of cx is inverse pixels.}
     }
     The display widow is shrink-wrapped around the image unless the
     image is larger than maxwidth by maxheight (800x600 by default), 
     in which it is displayed in a scrollable window of size maxwidth be maxheight.
     The image file can be in any format that ImageMagick's <convert>
     command understands.{<br>}
     }))
  (exit 1))
 
(de get-numeric-opt (a)
  (let ((opt (member a argv)))
    (when opt
      (when (not (cadr opt))
	(writing "$stderr" (printf "missing numerical value for %s option\n" a))
	(exit 1))
      (let ((v (val (cadr opt)))) 
	(when (not v) 
	  (writing "$stderr" (printf "illegal value for %s numerical option\n" a))
	  (exit 1))
	v))))

(libload "sdlgames/lander/lander")

(setq thrust (or (get-numeric-opt "-t") 400))
(setq gravity (or (get-numeric-opt "-g") 200))
(setq mass (or (get-numeric-opt "-m") 1))
(setq friction (or (get-numeric-opt "-f") 0))
(printf "left/right arrow keys to rotate\n")
(printf "up arrow for motor\n")
(printf "'z' to shoot\n")
(printf "'q' to quit\n")
(printf "thrust=%g gravity=%g mass=%g friction=%g\n" thrust gravity mass friction)
(lander thrust gravity mass friction)





