home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / missile.icn < prev    next >
Text File  |  2000-07-29  |  10KB  |  332 lines

  1. ############################################################################
  2. #
  3. #    File:     missile.icn
  4. #
  5. #    Subject:  Program to play missile command game
  6. #
  7. #    Author:   Chris Tenaglia
  8. #
  9. #    Date:     June 14, 1994
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  Here is a cheap attempt at a Missile Command game.
  18. #
  19. #  I've run it under Icon V8.7 under VMS, Unix, and V8.5 under MS-DOS.
  20. #  
  21. #  Here are some things you'll need to know. There is a delay() procedure
  22. #  that keeps the game running at a steady pace. delay() is built into
  23. #  V8.7 on VMS and unix. Under DOS you'll need to roll your own.
  24. #  The program also uses ansi escape sequences. Also to play use 7, 8, and 9
  25. #  to launch a # missile. 7 is leftward, 8 is straight, and 9 is right. A bug
  26. #  in the Ultrix version (kbhit() and getch()) requires double pressing the
  27. #  buttons. I think q will quit the game early.
  28. #  
  29. #  Have Fun!
  30. #  
  31. ############################################################################
  32. #
  33. #  Links:  random
  34. #
  35. ############################################################################
  36.  
  37. link random
  38.  
  39. global bonus,             # bonus missile threshhold
  40.        score,             # number of missiles shot down
  41.        munitions,         # munitions supply (# of defensive missiles)
  42.        missilef,          # whether enemy missile is launched flag
  43.        missilex,          # x position of enemy missile
  44.        missiley,          # y position of enemy missile
  45.        incm,              # x increment of enemy missile
  46.        abmf,              # whether defensive missile fired flag
  47.        abmx,              # x position of defensive missile
  48.        abmy,              # y position of defensive missile
  49.        abmix              # x increment of defensive missle
  50.  
  51. procedure main()
  52.   infrastructure()        # set up defaults, globals, and munitions
  53.   banner()                # output initial banner
  54.   repeat
  55.     {
  56.     draw_base()           # initially draw base
  57.     repeat
  58.       {
  59.       enemy_launch()      # possible enemy attack
  60.       friendly_fire()     # possible defensive attack
  61.       animate()           # draw action if any
  62.       sense_status()      # sense status
  63.       delay(1000)         # pace the game
  64.       }
  65.     }
  66.   stop("\7\e[0m",at(12,24),"Game Over. \e[5mInsert another quarter.\e[0m\e[?25h\e=")
  67.   end
  68.  
  69. #
  70. # set up all the initial defaults
  71. #
  72. procedure infrastructure()
  73.   bonus    := 22
  74.   missilef := 0
  75.   missilex := 0
  76.   missiley := 0
  77.   incm     := 0
  78.   abmf     := 0
  79.   abmx     := 0
  80.   abmy     := 0
  81.   score    := 0
  82.   randomize()
  83.   munitions:= 10 + ?5
  84.   end
  85.  
  86. #
  87. # draw the initial environment
  88. #
  89. procedure draw_base()
  90.   write("\e[?25l\e>\e[?5l\e[0;1;33;44m\e[2J\e[H                 S.D.I. OUTPOST        [TACTICAL SITUATION DISPLAY]")
  91.   writes(at(23,1),repl("#",79))
  92.   writes(at(24,1),repl("=",79))
  93.   writes(at(24,39),"/ \\",at(23,40),"^")
  94.   writes(at(24,5)," Missiles Left : ",munitions," ")
  95.   writes(at(24,60)," Score : ",score," ")
  96.   end
  97.  
  98. #
  99. # check and occasionally launch a missile
  100. #
  101. procedure enemy_launch()
  102.   (?50 = 33) | fail
  103.   if missilef = 1 then fail
  104.   missilex := 1
  105.   missiley := 1 + ?10
  106.   missilef := 1
  107.   incm     := ?3                                                  
  108.   end
  109.  
  110. #
  111. # coordinate launch of defensive missiles
  112. #
  113. procedure friendly_fire()
  114.   local ambf, press
  115.  
  116.   kbhit() | fail
  117.   press := getch()
  118.   if abmf = 1 then
  119.     {
  120.     case press of
  121.       {
  122.       "1" | "4" | "7" | "l" | "L" : abmix := -2
  123.       "2" | "5" | "8" | "s" | "S" : abmix :=  0
  124.       "3" | "6" | "9" | "r" | "R" : abmix :=  2
  125.       "q" | "Q" | "\e"                  : stop("\e[2J\e[H")
  126.       default : writes("\7")
  127.       }
  128.     } else {
  129.     ambf :=  1
  130.     abmx := 40
  131.     abmy := 22
  132.     case press of
  133.       {
  134.       "1" | "4" | "7" | "l" | "L" : abmix := -2
  135.       "2" | "5" | "8" | "s" | "S" : abmix :=  0
  136.       "3" | "6" | "9" | "r" | "R" : abmix :=  2
  137.       "q" | "Q" | "\e": stop("\e[2J\e[H",at(12,24),"Game Over. \e[5mInsert another quarter.\e[0m\e[?25h\e=")
  138.       default : {
  139.                 writes("\7")
  140.                 fail
  141.                 }
  142.       }
  143.     if munitions <= 0 then
  144.       stop(at(12,24),"Game Over. \e[5mInsert Another Quarter!\e[0m\e=\e[?25h")
  145.     munitions -:= 1
  146.     abmf       := 1
  147.     writes(at(24,5)," Missiles Left : ",munitions," ")
  148.     }
  149.   end
  150.  
  151. #
  152. # fly the missiles
  153. #
  154. procedure animate()
  155.   local old_missilez
  156.  
  157.   static  old_abmx,
  158.           old_abmy,
  159.           old_missilex,
  160.           old_missiley
  161.  
  162.   initial {
  163.           old_abmx     := 0
  164.           old_abmy     := 0
  165.           old_missilez := 0
  166.           old_missiley := 0
  167.           }
  168.  
  169.   #
  170.   # move the defensive missile if launched
  171.   #
  172.   if abmf = 1 then
  173.     {
  174.     writes(at(abmy,abmx),"*",at(old_abmy,old_abmx)," ")
  175.     old_abmx := abmx
  176.     old_abmy := abmy
  177.     abmx    +:= abmix
  178.     abmy    -:= 1
  179.     if abmy < 2 then
  180.       {
  181.       writes(at(old_abmy,old_abmx)," ")
  182.       abmf := 0
  183.       abmx := 0
  184.       abmy := 0
  185.       }
  186.     }
  187.  
  188.   #
  189.   # move the offensive missile if launched
  190.   #
  191.   if missilef = 1 then
  192.     {
  193.     writes(at(missiley,missilex),"   =>")
  194.     missilex +:= incm
  195.     if missilex > 76 then
  196.       {
  197.       writes(at(missiley,76),"\e[K")
  198.       missilef := 0                                 
  199.       missilex := 0
  200.       missiley := 0
  201.       incm     := 0
  202.       }
  203.     }
  204.   end
  205.  
  206. #
  207. # sense for hits and handle explosions
  208. #
  209. procedure sense_status()
  210.   local j
  211.   static  junk
  212.   initial junk := ["=%!*@",
  213.                    "%^&(!",
  214.                    "(@^$^",
  215.                    "*)@%$",
  216.                    "@&%^(#"]
  217.   if missilef=1 & abmf=1 then
  218.     {
  219.     if abmy=missiley & (missilex < abmx < missilex+6) then
  220.       {
  221.       every 1 to 3 do
  222.         {
  223.         writes(at(abmy,abmx-4),"\e[?5h<<<<>>>>")  ; delay(2000)  # reverse screen
  224.         writes(at(abmy,abmx-4),"\e[?5l>>>><<<<")  ; delay(2000)  # normal  screen
  225.         }
  226.       every j := abmy to 22 do
  227.         {
  228.         writes(at(j,abmx-3),?junk)
  229.         delay(1000)
  230.         }
  231.       if abmx > 67 then abmx := 67   # handle edge of screen problem
  232.       writes(at(23,abmx-3),"********")              ; delay(1000)
  233.       writes(at(22,abmx-3),"\e[?5h||||||||")        ; delay(1000)
  234.       writes(at(21,abmx-5),"\e[?5l. . . . . . .")   ; delay(1000)
  235.       every j := 20 to abmy by -1 do writes(at(j,abmx-6),"\e[K")
  236.       wait(2)
  237.       score   +:= incm * (15 - missiley)
  238.       if score > bonus then
  239.         {
  240.         writes(at(12,30),"\7\e[5mBONUS MISSILE EARNED!\e[0m")
  241.         bonus     +:= 33
  242.         munitions +:= 1
  243.         delay(30000)
  244.         }
  245.       draw_base()
  246.       abmf     := 0
  247.       abmx     := 0
  248.       abmy     := 0
  249.       missilef := 0
  250.       missilex := 0
  251.       missiley := 0
  252.       }
  253.     }
  254.   end
  255.                     
  256. #
  257. # output initial banner for this game
  258. #             
  259. procedure banner()
  260.   write("\e[0;1;33;44m\e[2J\e[H                                                                 ")
  261.   write("                                                                 ")
  262.   write("###############################################################################")
  263.   write("                                                                 ")
  264.   write("             ***   *   *  *****  ****    ***    ****  *****      ")
  265.   write("           *   *  *   *    *    *   *  *   *  *        *         ")
  266.   write("          *   *  *   *    *    ****   *   *   ***     *          ")
  267.   write("         *   *  *   *    *    *      *   *      *    *           ")
  268.   write("         ***    ***     *    *       ***   ****     *            ")
  269.   write("                                                                 ")
  270.   write("                ****          ****          ***                  ")
  271.   write("              *              *   *          *                    ")
  272.   write("              ****          *   *          *                     ")
  273.   write("                 *         *   *          *                      ")
  274.   write("            ****   **     ****   **     ***  **                  ")
  275.   write("                                                                 ")
  276.   write("                                                                 ")
  277.   write("###############################################################################")
  278.   wait(3)
  279.   end
  280.  
  281. #
  282. # move cursor to specified screen position
  283. #
  284. procedure at(row,column)
  285.   return "\e[" || row || ";" || column || "f"
  286.   end
  287.  
  288. #
  289. # procedure to wait n seconds
  290. #
  291. procedure wait(n)
  292.   delay(n * 10000)
  293.   return
  294. ##  secs := &clock[-2:0] + n
  295. ##  if secs > 58 then secs -:= 60
  296. ##  repeat
  297. ##    {
  298. ##    now := &clock[-2:0]
  299. ##    if now > secs then break
  300. ##    }
  301. ##  return
  302.   end
  303.  
  304. ############################################################################
  305. #                                                                #
  306. # This procedure pulls all the elements (tokens) out of a line   #
  307. # buffer and returns them in a list. a variable named 'chars'    #
  308. # can be statically defined here or global. It is a cset that    #
  309. # contains the valid characters that can compose the elements    #
  310. # one wishes to extract.                                         #
  311. #                                                                #
  312. ############################################################################
  313. procedure parse(line,delims)
  314.   local tokens
  315.   static chars
  316.   chars  := &cset -- delims
  317.   tokens := []
  318.   line ? while tab(upto(chars)) do put(tokens,tab(many(chars)))
  319.   return tokens
  320.   end
  321.  
  322. ############################################################################
  323. #                                                                #
  324. # This procedure is terribly handy in prompting and getting      #
  325. # an input string                                                #
  326. #                                                                #
  327. ############################################################################
  328. procedure input(prompt)
  329.   writes(prompt)
  330.   return read()
  331.   end
  332.