xref: /original-bsd/games/rain/rain.c (revision f0fd5f8a)
1 
2 static char sccsid[] = "	rain.c	4.1	82/10/24	";
3 
4 #include <stdio.h>
5 #include <sgtty.h>
6 #include <signal.h>
7 /* rain 11/3/1980 EPS/CITHEP */
8 /* cc rain.c -o rain -O -ltermlib */
9 #define cursor(col,row) tputs(tgoto(CM,col,row),1,outc)
10 outc(c)
11 {
12 	putchar(c);
13 }
14 extern char *UP;
15 extern short ospeed;
16 struct sgttyb old_tty;
17 char *LL, *TE, *TI;
18 main(argc,argv)
19 int argc;
20 char *argv[];
21 {
22     extern fputchar();
23     char *malloc();
24     char *getenv();
25     char *tgetstr(), *tgoto();
26     float ranf();
27     int onsig();
28     register int x, y, j;
29     static int xpos[5], ypos[5];
30     register char *CM, *BC, *DN, *ND;
31     char *tcp;
32     register char *term;
33     char tcb[100];
34     struct sgttyb sg;
35     setbuf(stdout,malloc(BUFSIZ));
36     if (!(term=getenv("TERM"))) {
37 	fprintf(stderr,"%s: TERM: parameter not set\n",*argv);
38 	exit(1);
39     }
40     if (tgetent(malloc(1024),term)<=0) {
41 	fprintf(stderr,"%s: %s: unknown terminal type\n",*argv,term);
42 	exit(1);
43     }
44     tcp=tcb;
45     if (!(CM=tgetstr("cm",&tcp))) {
46 	fprintf(stderr,"%s: terminal not capable of cursor motion\n",*argv);
47 	exit(1);
48     }
49     if (!(BC=tgetstr("bc",&tcp))) BC="\b";
50     if (!(DN=tgetstr("dn",&tcp))) DN="\n";
51     if (!(ND=tgetstr("nd",&tcp))) ND=" ";
52     TE=tgetstr("te",&tcp);
53     TI=tgetstr("ti",&tcp);
54     UP=tgetstr("up",&tcp);
55     if (!(LL=tgetstr("ll",&tcp))) strcpy(LL=malloc(10),tgoto(CM,0,23));
56     gtty(1, &sg);
57     ospeed=sg.sg_ospeed;
58     for (j=SIGHUP;j<=SIGTERM;j++)
59 	if (signal(j,SIG_IGN)!=SIG_IGN) signal(j,onsig);
60     gtty(1, &old_tty);	/* save tty bits for exit */
61     gtty(1, &sg);
62     sg.sg_flags&=~(CRMOD|ECHO);
63     stty(1, &sg);
64     if (TI) fputs(TI,stdout);
65     tputs(tgetstr("cl",&tcp),1,fputchar);
66     fflush(stdout);
67     for (j=5;--j>=0;) {
68 	xpos[j]=(int)(76.*ranf())+2;
69 	ypos[j]=(int)(20.*ranf())+2;
70     }
71     for (j=0;;) {
72 	x=(int)(76.*ranf())+2;
73 	y=(int)(20.*ranf())+2;
74 	cursor(x,y); fputchar('.');
75 	cursor(xpos[j],ypos[j]); fputchar('o');
76 	if (j==0) j=4; else --j;
77 	cursor(xpos[j],ypos[j]); fputchar('O');
78 	if (j==0) j=4; else --j;
79 	cursor(xpos[j],ypos[j]-1);
80 	fputchar('-');
81 	fputs(DN,stdout); fputs(BC,stdout); fputs(BC,stdout);
82 	fputs("|.|",stdout);
83 	fputs(DN,stdout); fputs(BC,stdout); fputs(BC,stdout);
84 	fputchar('-');
85 	if (j==0) j=4; else --j;
86 	cursor(xpos[j],ypos[j]-2); fputchar('-');
87 	fputs(DN,stdout); fputs(BC,stdout); fputs(BC,stdout);
88 	fputs("/ \\",stdout);
89 	cursor(xpos[j]-2,ypos[j]);
90 	fputs("| O |",stdout);
91 	cursor(xpos[j]-1,ypos[j]+1);
92 	fputs("\\ /",stdout);
93 	fputs(DN,stdout); fputs(BC,stdout); fputs(BC,stdout);
94 	fputchar('-');
95 	if (j==0) j=4; else --j;
96 	cursor(xpos[j],ypos[j]-2); fputchar(' ');
97 	fputs(DN,stdout); fputs(BC,stdout); fputs(BC,stdout);
98 	fputchar(' '); fputs(ND,stdout); fputchar(' ');
99 	cursor(xpos[j]-2,ypos[j]);
100 	fputchar(' '); fputs(ND,stdout); fputchar(' ');
101 	fputs(ND,stdout); fputchar(' ');
102 	cursor(xpos[j]-1,ypos[j]+1);
103 	fputchar(' '); fputs(ND,stdout); fputchar(' ');
104 	fputs(DN,stdout); fputs(BC,stdout); fputs(BC,stdout);
105 	fputchar(' ');
106 	xpos[j]=x; ypos[j]=y;
107 	fflush(stdout);
108     }
109 }
110 onsig(n)
111 int n;
112 {
113     struct sgttyb sg;
114     fputs(LL, stdout);
115     if (TE) fputs(TE, stdout);
116     fflush(stdout);
117     stty(1, &old_tty);
118     kill(getpid(),n);
119     _exit(0);
120 }
121 fputchar(c)
122 char c;
123 {
124     putchar(c);
125 }
126 float ranf() {
127     return((float)rand()/2147483647.);
128 }
129