xref: /original-bsd/games/rain/rain.c (revision 57124d5e)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 char copyright[] =
9 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)rain.c	5.2 (Berkeley) 07/15/85";
15 #endif not lint
16 
17 #define BSD
18 
19 #include <stdio.h>
20 #ifdef USG
21 #include <termio.h>
22 #else
23 #include <sgtty.h>
24 #endif
25 #include <signal.h>
26 /* rain 11/3/1980 EPS/CITHEP */
27 /* cc rain.c -o rain -O -ltermlib */
28 #define cursor(col,row) tputs(tgoto(CM,col,row),1,fputchar)
29 extern char *UP;
30 extern short ospeed;
31 #ifdef USG
32 struct termio old_tty;
33 #else
34 struct sgttyb old_tty;
35 #endif
36 char *LL, *TE, *TI;
37 main(argc,argv)
38 int argc;
39 char *argv[];
40 {
41     extern fputchar();
42     char *malloc();
43     char *getenv();
44     char *tgetstr(), *tgoto();
45     float ranf();
46     int onsig();
47     register int x, y, j;
48     static int xpos[5], ypos[5];
49     register char *CM, *BC, *DN, *ND;
50     int CO, LI;
51     char *tcp;
52     register char *term;
53     char tcb[100];
54 #ifdef USG
55     struct termio sg;
56 #else
57     struct sgttyb sg;
58 #endif
59     float cols, lines;
60 
61     setbuf(stdout,malloc(BUFSIZ));
62     if (!(term=getenv("TERM"))) {
63 	fprintf(stderr,"%s: TERM: parameter not set\n",*argv);
64 	exit(1);
65     }
66     if (tgetent(malloc(1024),term)<=0) {
67 	fprintf(stderr,"%s: %s: unknown terminal type\n",*argv,term);
68 	exit(1);
69     }
70     tcp=tcb;
71     if (!(CM=tgetstr("cm",&tcp))) {
72 	fprintf(stderr,"%s: terminal not capable of cursor motion\n",*argv);
73 	exit(1);
74     }
75     if (!(BC=tgetstr("bc",&tcp))) BC="\b";
76     if (!(DN=tgetstr("dn",&tcp))) DN="\n";
77     if (!(ND=tgetstr("nd",&tcp))) ND=" ";
78     if ((CO = tgetnum("co")) == -1)
79 	CO = 80;
80     if ((LI = tgetnum("li")) == -1)
81 	LI = 24;
82     cols = CO - 4;
83     lines = LI - 4;
84     TE=tgetstr("te",&tcp);
85     TI=tgetstr("ti",&tcp);
86     UP=tgetstr("up",&tcp);
87     if (!(LL=tgetstr("ll",&tcp))) strcpy(LL=malloc(10),tgoto(CM,0,23));
88 #ifdef USG
89     ioctl(1, TCGETA, &sg);
90     ospeed=sg.c_cflag&CBAUD;
91 #else
92     gtty(1, &sg);
93     ospeed=sg.sg_ospeed;
94 #endif
95     for (j=SIGHUP;j<=SIGTERM;j++)
96 	if (signal(j,SIG_IGN)!=SIG_IGN) signal(j,onsig);
97 #ifdef USG
98     ioctl(1, TCGETA, &old_tty);	/* save tty bits for exit */
99     ioctl(1, TCGETA, &sg);
100     sg.c_iflag&=~ICRNL;
101     sg.c_oflag&=~ONLCR;
102     sg.c_lflag&=~ECHO;
103     ioctl(1, TCSETAW, &sg);
104 #else
105     gtty(1, &old_tty);	/* save tty bits for exit */
106     gtty(1, &sg);
107     sg.sg_flags&=~(CRMOD|ECHO);
108     stty(1, &sg);
109 #endif
110     if (TI) tputs(TI,1,fputchar);
111     tputs(tgetstr("cl",&tcp),1,fputchar);
112     fflush(stdout);
113     for (j=5;--j>=0;) {
114 	xpos[j]=(int)(cols*ranf())+2;
115 	ypos[j]=(int)(lines*ranf())+2;
116     }
117     for (j=0;;) {
118 	x=(int)(cols*ranf())+2;
119 	y=(int)(lines*ranf())+2;
120 	cursor(x,y); fputchar('.');
121 	cursor(xpos[j],ypos[j]); fputchar('o');
122 	if (j==0) j=4; else --j;
123 	cursor(xpos[j],ypos[j]); fputchar('O');
124 	if (j==0) j=4; else --j;
125 	cursor(xpos[j],ypos[j]-1);
126 	fputchar('-');
127 	tputs(DN,1,fputchar); tputs(BC,1,fputchar); tputs(BC,1,fputchar);
128 	fputs("|.|",stdout);
129 	tputs(DN,1,fputchar); tputs(BC,1,fputchar); tputs(BC,1,fputchar);
130 	fputchar('-');
131 	if (j==0) j=4; else --j;
132 	cursor(xpos[j],ypos[j]-2); fputchar('-');
133 	tputs(DN,1,fputchar); tputs(BC,1,fputchar); tputs(BC,1,fputchar);
134 	fputs("/ \\",stdout);
135 	cursor(xpos[j]-2,ypos[j]);
136 	fputs("| O |",stdout);
137 	cursor(xpos[j]-1,ypos[j]+1);
138 	fputs("\\ /",stdout);
139 	tputs(DN,1,fputchar); tputs(BC,1,fputchar); tputs(BC,1,fputchar);
140 	fputchar('-');
141 	if (j==0) j=4; else --j;
142 	cursor(xpos[j],ypos[j]-2); fputchar(' ');
143 	tputs(DN,1,fputchar); tputs(BC,1,fputchar); tputs(BC,1,fputchar);
144 	fputchar(' '); tputs(ND,1,fputchar); fputchar(' ');
145 	cursor(xpos[j]-2,ypos[j]);
146 	fputchar(' '); tputs(ND,1,fputchar); fputchar(' ');
147 	tputs(ND,1,fputchar); fputchar(' ');
148 	cursor(xpos[j]-1,ypos[j]+1);
149 	fputchar(' '); tputs(ND,1,fputchar); fputchar(' ');
150 	tputs(DN,1,fputchar); tputs(BC,1,fputchar); tputs(BC,1,fputchar);
151 	fputchar(' ');
152 	xpos[j]=x; ypos[j]=y;
153 	fflush(stdout);
154     }
155 }
156 onsig(n)
157 int n;
158 {
159 #ifdef USG
160     struct termio sg;
161 #else
162     struct sgttyb sg;
163 #endif
164     tputs(LL, 1, fputchar);
165     if (TE) tputs(TE, 1, fputchar);
166     fflush(stdout);
167 #ifdef USG
168     ioctl(1, TCSETAW, &old_tty);
169 #else
170     stty(1, &old_tty);
171 #endif
172     kill(getpid(),n);
173     _exit(0);
174 }
175 fputchar(c)
176 char c;
177 {
178     putchar(c);
179 }
180 float ranf() {
181 #ifdef BSD
182     return((float)rand()/2147483647.);
183 #else
184     return((float)rand()/32767.);
185 #endif
186 }
187