xref: /csrg-svn/games/rain/rain.c (revision 60837)
121200Sdist /*
2*60837Sbostic  * Copyright (c) 1980, 1993
3*60837Sbostic  *	The Regents of the University of California.  All rights reserved.
433199Sbostic  *
542588Sbostic  * %sccs.include.redist.c%
621200Sdist  */
78869Smckusick 
821200Sdist #ifndef lint
9*60837Sbostic static char copyright[] =
10*60837Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*60837Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1233199Sbostic #endif /* not lint */
138869Smckusick 
1421200Sdist #ifndef lint
15*60837Sbostic static char sccsid[] = "@(#)rain.c	8.1 (Berkeley) 05/31/93";
1633199Sbostic #endif /* not lint */
1721200Sdist 
1833199Sbostic /*
1933199Sbostic  * rain 11/3/1980 EPS/CITHEP
2033199Sbostic  * cc rain.c -o rain -O -ltermlib
2133199Sbostic  */
2223869Smckusick 
2333199Sbostic #include <sys/types.h>
248869Smckusick #include <stdio.h>
2523869Smckusick #ifdef USG
2623869Smckusick #include <termio.h>
2723869Smckusick #else
288869Smckusick #include <sgtty.h>
2923869Smckusick #endif
308869Smckusick #include <signal.h>
3133199Sbostic 
3233199Sbostic #define	cursor(c, r)	tputs(tgoto(CM, c, r), 1, fputchar)
3333199Sbostic 
3423869Smckusick #ifdef USG
3533199Sbostic static struct termio sg, old_tty;
3623869Smckusick #else
3733199Sbostic static struct sgttyb sg, old_tty;
3823869Smckusick #endif
3933199Sbostic 
4033199Sbostic int	fputchar();
4133199Sbostic char	*LL, *TE, *tgoto();
4233199Sbostic 
main(argc,argv)4333199Sbostic main(argc, argv)
4433199Sbostic 	int argc;
4533199Sbostic 	char **argv;
468869Smckusick {
4733199Sbostic 	extern short ospeed;
4833199Sbostic 	extern char *UP;
4933199Sbostic 	register int x, y, j;
5033199Sbostic 	register char *CM, *BC, *DN, *ND, *term;
5133199Sbostic 	char *TI, *tcp, *mp, tcb[100],
5233199Sbostic 		*malloc(), *getenv(), *strcpy(), *tgetstr();
5333199Sbostic 	long cols, lines, random();
5446756Sbostic 	int xpos[5], ypos[5];
5546756Sbostic 	static void onsig();
5623869Smckusick 
5733199Sbostic 	if (!(term = getenv("TERM"))) {
5833199Sbostic 		fprintf(stderr, "%s: TERM: parameter not set\n", *argv);
5933199Sbostic 		exit(1);
6033199Sbostic 	}
6133199Sbostic 	if (!(mp = malloc((u_int)1024))) {
6233199Sbostic 		fprintf(stderr, "%s: out of space.\n", *argv);
6333199Sbostic 		exit(1);
6433199Sbostic 	}
6533199Sbostic 	if (tgetent(mp, term) <= 0) {
6633199Sbostic 		fprintf(stderr, "%s: %s: unknown terminal type\n", *argv, term);
6733199Sbostic 		exit(1);
6833199Sbostic 	}
6933199Sbostic 	tcp = tcb;
7033199Sbostic 	if (!(CM = tgetstr("cm", &tcp))) {
7133199Sbostic 		fprintf(stderr, "%s: terminal not capable of cursor motion\n", *argv);
7233199Sbostic 		exit(1);
7333199Sbostic 	}
7433199Sbostic 	if (!(BC = tgetstr("bc", &tcp)))
7533199Sbostic 		BC = "\b";
7633199Sbostic 	if (!(DN = tgetstr("dn", &tcp)))
7733199Sbostic 		DN = "\n";
7833199Sbostic 	if (!(ND = tgetstr("nd", &tcp)))
7933199Sbostic 		ND = " ";
8033199Sbostic 	if ((cols = tgetnum("co")) == -1)
8133199Sbostic 		cols = 80;
8233199Sbostic 	if ((lines = tgetnum("li")) == -1)
8333199Sbostic 		lines = 24;
8433199Sbostic 	cols -= 4;
8533199Sbostic 	lines -= 4;
8633199Sbostic 	TE = tgetstr("te", &tcp);
8733199Sbostic 	TI = tgetstr("ti", &tcp);
8833199Sbostic 	UP = tgetstr("up", &tcp);
8933199Sbostic 	if (!(LL = tgetstr("ll", &tcp))) {
9033199Sbostic 		if (!(LL = malloc((u_int)10))) {
9133199Sbostic 			fprintf(stderr, "%s: out of space.\n", *argv);
9233199Sbostic 			exit(1);
9333199Sbostic 		}
9433199Sbostic 		(void)strcpy(LL, tgoto(CM, 0, 23));
9533199Sbostic 	}
9623869Smckusick #ifdef USG
9733199Sbostic 	ioctl(1, TCGETA, &sg);
9833199Sbostic 	ospeed = sg.c_cflag&CBAUD;
9923869Smckusick #else
10033199Sbostic 	gtty(1, &sg);
10133199Sbostic 	ospeed = sg.sg_ospeed;
10223869Smckusick #endif
10333199Sbostic 	(void)signal(SIGHUP, onsig);
10433199Sbostic 	(void)signal(SIGINT, onsig);
10533199Sbostic 	(void)signal(SIGQUIT, onsig);
10633199Sbostic 	(void)signal(SIGSTOP, onsig);
10733199Sbostic 	(void)signal(SIGTSTP, onsig);
10833199Sbostic 	(void)signal(SIGTERM, onsig);
10923869Smckusick #ifdef USG
11033199Sbostic 	ioctl(1, TCGETA, &old_tty);	/* save tty bits for exit */
11133199Sbostic 	ioctl(1, TCGETA, &sg);
11233199Sbostic 	sg.c_iflag &= ~ICRNL;
11333199Sbostic 	sg.c_oflag &= ~ONLCR;
11433199Sbostic 	sg.c_lflag &= ~ECHO;
11533199Sbostic 	ioctl(1, TCSETAW, &sg);
11623869Smckusick #else
11733199Sbostic 	gtty(1, &old_tty);		/* save tty bits for exit */
11833199Sbostic 	gtty(1, &sg);
11933199Sbostic 	sg.sg_flags &= ~(CRMOD|ECHO);
12033199Sbostic 	stty(1, &sg);
12123869Smckusick #endif
12233199Sbostic 	if (TI)
12333199Sbostic 		tputs(TI, 1, fputchar);
12433199Sbostic 	tputs(tgetstr("cl", &tcp), 1, fputchar);
12533199Sbostic 	(void)fflush(stdout);
12633199Sbostic 	for (j = 4; j >= 0; --j) {
12733199Sbostic 		xpos[j] = random() % cols + 2;
12833199Sbostic 		ypos[j] = random() % lines + 2;
12933199Sbostic 	}
13033199Sbostic 	for (j = 0;;) {
13133199Sbostic 		x = random() % cols + 2;
13233199Sbostic 		y = random() % lines + 2;
13333199Sbostic 		cursor(x, y);
13433199Sbostic 		fputchar('.');
13533199Sbostic 		cursor(xpos[j], ypos[j]);
13633199Sbostic 		fputchar('o');
13733199Sbostic 		if (!j--)
13833199Sbostic 			j = 4;
13933199Sbostic 		cursor(xpos[j], ypos[j]);
14033199Sbostic 		fputchar('O');
14133199Sbostic 		if (!j--)
14233199Sbostic 			j = 4;
14333199Sbostic 		cursor(xpos[j], ypos[j] - 1);
14433199Sbostic 		fputchar('-');
14533199Sbostic 		tputs(DN, 1, fputchar);
14633199Sbostic 		tputs(BC, 1, fputchar);
14733199Sbostic 		tputs(BC, 1, fputchar);
14833199Sbostic 		fputs("|.|", stdout);
14933199Sbostic 		tputs(DN, 1, fputchar);
15033199Sbostic 		tputs(BC, 1, fputchar);
15133199Sbostic 		tputs(BC, 1, fputchar);
15233199Sbostic 		fputchar('-');
15333199Sbostic 		if (!j--)
15433199Sbostic 			j = 4;
15533199Sbostic 		cursor(xpos[j], ypos[j] - 2);
15633199Sbostic 		fputchar('-');
15733199Sbostic 		tputs(DN, 1, fputchar);
15833199Sbostic 		tputs(BC, 1, fputchar);
15933199Sbostic 		tputs(BC, 1, fputchar);
16033199Sbostic 		fputs("/ \\", stdout);
16133199Sbostic 		cursor(xpos[j] - 2, ypos[j]);
16233199Sbostic 		fputs("| O |", stdout);
16333199Sbostic 		cursor(xpos[j] - 1, ypos[j] + 1);
16433199Sbostic 		fputs("\\ /", stdout);
16533199Sbostic 		tputs(DN, 1, fputchar);
16633199Sbostic 		tputs(BC, 1, fputchar);
16733199Sbostic 		tputs(BC, 1, fputchar);
16833199Sbostic 		fputchar('-');
16933199Sbostic 		if (!j--)
17033199Sbostic 			j = 4;
17133199Sbostic 		cursor(xpos[j], ypos[j] - 2);
17233199Sbostic 		fputchar(' ');
17333199Sbostic 		tputs(DN, 1, fputchar);
17433199Sbostic 		tputs(BC, 1, fputchar);
17533199Sbostic 		tputs(BC, 1, fputchar);
17633199Sbostic 		fputchar(' ');
17733199Sbostic 		tputs(ND, 1, fputchar);
17833199Sbostic 		fputchar(' ');
17933199Sbostic 		cursor(xpos[j] - 2, ypos[j]);
18033199Sbostic 		fputchar(' ');
18133199Sbostic 		tputs(ND, 1, fputchar);
18233199Sbostic 		fputchar(' ');
18333199Sbostic 		tputs(ND, 1, fputchar);
18433199Sbostic 		fputchar(' ');
18533199Sbostic 		cursor(xpos[j] - 1, ypos[j] + 1);
18633199Sbostic 		fputchar(' ');
18733199Sbostic 		tputs(ND, 1, fputchar);
18833199Sbostic 		fputchar(' ');
18933199Sbostic 		tputs(DN, 1, fputchar);
19033199Sbostic 		tputs(BC, 1, fputchar);
19133199Sbostic 		tputs(BC, 1, fputchar);
19233199Sbostic 		fputchar(' ');
19333199Sbostic 		xpos[j] = x;
19433199Sbostic 		ypos[j] = y;
19533199Sbostic 		(void)fflush(stdout);
19633199Sbostic 	}
1978869Smckusick }
19833199Sbostic 
19946756Sbostic static void
onsig()20033199Sbostic onsig()
2018869Smckusick {
20233199Sbostic 	tputs(LL, 1, fputchar);
20333199Sbostic 	if (TE)
20433199Sbostic 		tputs(TE, 1, fputchar);
20533199Sbostic 	(void)fflush(stdout);
20623869Smckusick #ifdef USG
20733199Sbostic 	ioctl(1, TCSETAW, &old_tty);
20823869Smckusick #else
20933199Sbostic 	stty(1, &old_tty);
21023869Smckusick #endif
21133199Sbostic 	exit(0);
2128869Smckusick }
21333199Sbostic 
21457831Storek int
fputchar(c)2158869Smckusick fputchar(c)
21657831Storek 	int c;
2178869Smckusick {
21833199Sbostic 	putchar(c);
2198869Smckusick }
220