home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / games / rain / rain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-08  |  6.1 KB  |  246 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)rain.c    5.6 (Berkeley) 2/28/91";
  42. #endif /* not lint */
  43.  
  44. /*
  45.  * rain 11/3/1980 EPS/CITHEP
  46.  * cc rain.c -o rain -O -ltermlib
  47.  */
  48.  
  49. #include <sys/types.h>
  50. #include <stdio.h>
  51. #ifdef USG
  52. #include <termio.h>
  53. #else
  54. #include <sgtty.h>
  55. #endif
  56. #include <signal.h>
  57.  
  58. #define    cursor(c, r)    tputs(tgoto(CM, c, r), 1, fputchar)
  59.  
  60. #ifdef USG
  61. static struct termio sg, old_tty;
  62. #else
  63. static struct sgttyb sg, old_tty;
  64. #endif
  65.  
  66. int    fputchar();
  67. char    *LL, *TE, *tgoto();
  68.  
  69. main(argc, argv)
  70.     int argc;
  71.     char **argv;
  72. {
  73.     extern short ospeed;
  74.     extern char *UP;
  75.     register int x, y, j;
  76.     register char *CM, *BC, *DN, *ND, *term;
  77.     char *TI, *tcp, *mp, tcb[100],
  78.         *malloc(), *getenv(), *strcpy(), *tgetstr();
  79.     long cols, lines, random();
  80.     int xpos[5], ypos[5];
  81.     static void onsig();
  82.  
  83.     if (!(term = getenv("TERM"))) {
  84.         fprintf(stderr, "%s: TERM: parameter not set\n", *argv);
  85.         exit(1);
  86.     }
  87.     if (!(mp = malloc((u_int)1024))) {
  88.         fprintf(stderr, "%s: out of space.\n", *argv);
  89.         exit(1);
  90.     }
  91.     if (tgetent(mp, term) <= 0) {
  92.         fprintf(stderr, "%s: %s: unknown terminal type\n", *argv, term);
  93.         exit(1);
  94.     }
  95.     tcp = tcb;
  96.     if (!(CM = tgetstr("cm", &tcp))) {
  97.         fprintf(stderr, "%s: terminal not capable of cursor motion\n", *argv);
  98.         exit(1);
  99.     }
  100.     if (!(BC = tgetstr("bc", &tcp)))
  101.         BC = "\b";
  102.     if (!(DN = tgetstr("dn", &tcp)))
  103.         DN = "\n";
  104.     if (!(ND = tgetstr("nd", &tcp)))
  105.         ND = " ";
  106.     if ((cols = tgetnum("co")) == -1)
  107.         cols = 80;
  108.     if ((lines = tgetnum("li")) == -1)
  109.         lines = 24;
  110.     cols -= 4;
  111.     lines -= 4;
  112.     TE = tgetstr("te", &tcp);
  113.     TI = tgetstr("ti", &tcp);
  114.     UP = tgetstr("up", &tcp);
  115.     if (!(LL = tgetstr("ll", &tcp))) {
  116.         if (!(LL = malloc((u_int)10))) {
  117.             fprintf(stderr, "%s: out of space.\n", *argv);
  118.             exit(1);
  119.         }
  120.         (void)strcpy(LL, tgoto(CM, 0, 23));
  121.     }
  122. #ifdef USG
  123.     ioctl(1, TCGETA, &sg);
  124.     ospeed = sg.c_cflag&CBAUD;
  125. #else
  126.     gtty(1, &sg);
  127.     ospeed = sg.sg_ospeed;
  128. #endif
  129.     (void)signal(SIGHUP, onsig);
  130.     (void)signal(SIGINT, onsig);
  131.     (void)signal(SIGQUIT, onsig);
  132.     (void)signal(SIGSTOP, onsig);
  133.     (void)signal(SIGTSTP, onsig);
  134.     (void)signal(SIGTERM, onsig);
  135. #ifdef USG
  136.     ioctl(1, TCGETA, &old_tty);    /* save tty bits for exit */
  137.     ioctl(1, TCGETA, &sg);
  138.     sg.c_iflag &= ~ICRNL;
  139.     sg.c_oflag &= ~ONLCR;
  140.     sg.c_lflag &= ~ECHO;
  141.     ioctl(1, TCSETAW, &sg);
  142. #else
  143.     gtty(1, &old_tty);        /* save tty bits for exit */
  144.     gtty(1, &sg);
  145.     sg.sg_flags &= ~(CRMOD|ECHO);
  146.     stty(1, &sg);
  147. #endif
  148.     if (TI)
  149.         tputs(TI, 1, fputchar);
  150.     tputs(tgetstr("cl", &tcp), 1, fputchar);
  151.     (void)fflush(stdout);
  152.     for (j = 4; j >= 0; --j) {
  153.         xpos[j] = random() % cols + 2;
  154.         ypos[j] = random() % lines + 2;
  155.     }
  156.     for (j = 0;;) {
  157.         x = random() % cols + 2;
  158.         y = random() % lines + 2;
  159.         cursor(x, y);
  160.         fputchar('.');
  161.         cursor(xpos[j], ypos[j]);
  162.         fputchar('o');
  163.         if (!j--)
  164.             j = 4;
  165.         cursor(xpos[j], ypos[j]);
  166.         fputchar('O');
  167.         if (!j--)
  168.             j = 4;
  169.         cursor(xpos[j], ypos[j] - 1);
  170.         fputchar('-');
  171.         tputs(DN, 1, fputchar);
  172.         tputs(BC, 1, fputchar);
  173.         tputs(BC, 1, fputchar);
  174.         fputs("|.|", stdout);
  175.         tputs(DN, 1, fputchar);
  176.         tputs(BC, 1, fputchar);
  177.         tputs(BC, 1, fputchar);
  178.         fputchar('-');
  179.         if (!j--)
  180.             j = 4;
  181.         cursor(xpos[j], ypos[j] - 2);
  182.         fputchar('-');
  183.         tputs(DN, 1, fputchar);
  184.         tputs(BC, 1, fputchar);
  185.         tputs(BC, 1, fputchar);
  186.         fputs("/ \\", stdout);
  187.         cursor(xpos[j] - 2, ypos[j]);
  188.         fputs("| O |", stdout);
  189.         cursor(xpos[j] - 1, ypos[j] + 1);
  190.         fputs("\\ /", stdout);
  191.         tputs(DN, 1, fputchar);
  192.         tputs(BC, 1, fputchar);
  193.         tputs(BC, 1, fputchar);
  194.         fputchar('-');
  195.         if (!j--)
  196.             j = 4;
  197.         cursor(xpos[j], ypos[j] - 2);
  198.         fputchar(' ');
  199.         tputs(DN, 1, fputchar);
  200.         tputs(BC, 1, fputchar);
  201.         tputs(BC, 1, fputchar);
  202.         fputchar(' ');
  203.         tputs(ND, 1, fputchar);
  204.         fputchar(' ');
  205.         cursor(xpos[j] - 2, ypos[j]);
  206.         fputchar(' ');
  207.         tputs(ND, 1, fputchar);
  208.         fputchar(' ');
  209.         tputs(ND, 1, fputchar);
  210.         fputchar(' ');
  211.         cursor(xpos[j] - 1, ypos[j] + 1);
  212.         fputchar(' ');
  213.         tputs(ND, 1, fputchar);
  214.         fputchar(' ');
  215.         tputs(DN, 1, fputchar);
  216.         tputs(BC, 1, fputchar);
  217.         tputs(BC, 1, fputchar);
  218.         fputchar(' ');
  219.         xpos[j] = x;
  220.         ypos[j] = y;
  221.         (void)fflush(stdout);
  222.     }
  223. }
  224.  
  225. static void
  226. onsig()
  227. {
  228.     tputs(LL, 1, fputchar);
  229.     if (TE)
  230.         tputs(TE, 1, fputchar);
  231.     (void)fflush(stdout);
  232. #ifdef USG
  233.     ioctl(1, TCSETAW, &old_tty);
  234. #else
  235.     stty(1, &old_tty);
  236. #endif
  237.     exit(0);
  238. }
  239.  
  240. static
  241. fputchar(c)
  242.     char c;
  243. {
  244.     putchar(c);
  245. }
  246.