xref: /csrg-svn/games/atc/graphics.c (revision 60740)
141166Sbostic /*-
2*60740Sbostic  * Copyright (c) 1990, 1993
3*60740Sbostic  *	The Regents of the University of California.  All rights reserved.
441166Sbostic  *
541166Sbostic  * This code is derived from software contributed to Berkeley by
641166Sbostic  * Ed James.
741166Sbostic  *
841166Sbostic  * %sccs.include.redist.c%
941166Sbostic  */
1041166Sbostic 
1141159Sbostic /*
1241159Sbostic  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
1341159Sbostic  *
1441159Sbostic  * Copy permission is hereby granted provided that this notice is
1541159Sbostic  * retained on all partial or complete copies.
1641159Sbostic  *
1741159Sbostic  * For more info on this and all of my stuff, mail edjames@berkeley.edu.
1841159Sbostic  */
1941159Sbostic 
2041166Sbostic #ifndef lint
21*60740Sbostic static char sccsid[] = "@(#)graphics.c	8.1 (Berkeley) 05/31/93";
2241166Sbostic #endif /* not lint */
2341166Sbostic 
2441159Sbostic #include "include.h"
2541159Sbostic #ifdef SYSV
2641159Sbostic #include <errno.h>
2741159Sbostic #endif
2841159Sbostic 
2941159Sbostic #define C_TOPBOTTOM		'-'
3041159Sbostic #define C_LEFTRIGHT		'|'
3141159Sbostic #define C_AIRPORT		'='
3241159Sbostic #define C_LINE			'+'
3341159Sbostic #define C_BACKROUND		'.'
3441159Sbostic #define C_BEACON		'*'
3541159Sbostic #define C_CREDIT		'*'
3641159Sbostic 
3741159Sbostic WINDOW	*radar, *cleanradar, *credit, *input, *planes;
3841159Sbostic 
getAChar()3941159Sbostic getAChar()
4041159Sbostic {
4141159Sbostic #ifdef BSD
4241159Sbostic 	return (getchar());
4341159Sbostic #endif
4441159Sbostic #ifdef SYSV
4541159Sbostic 	int c;
4641159Sbostic 
4741159Sbostic 	while ((c = getchar()) == -1 && errno == EINTR) ;
4841159Sbostic 	return(c);
4941159Sbostic #endif
5041159Sbostic }
5141159Sbostic 
erase_all()5241159Sbostic erase_all()
5341159Sbostic {
5441159Sbostic 	PLANE	*pp;
5541159Sbostic 
5641159Sbostic 	for (pp = air.head; pp != NULL; pp = pp->next) {
5741159Sbostic 		wmove(cleanradar, pp->ypos, pp->xpos * 2);
5841159Sbostic 		wmove(radar, pp->ypos, pp->xpos * 2);
5941159Sbostic 		waddch(radar, winch(cleanradar));
6041159Sbostic 		wmove(cleanradar, pp->ypos, pp->xpos * 2 + 1);
6141159Sbostic 		wmove(radar, pp->ypos, pp->xpos * 2 + 1);
6241159Sbostic 		waddch(radar, winch(cleanradar));
6341159Sbostic 	}
6441159Sbostic }
6541159Sbostic 
draw_all()6641159Sbostic draw_all()
6741159Sbostic {
6841159Sbostic 	PLANE	*pp;
6941159Sbostic 
7041159Sbostic 	for (pp = air.head; pp != NULL; pp = pp->next) {
7141159Sbostic 		if (pp->status == S_MARKED)
7241159Sbostic 			wstandout(radar);
7341159Sbostic 		wmove(radar, pp->ypos, pp->xpos * 2);
7441159Sbostic 		waddch(radar, name(pp));
7541159Sbostic 		waddch(radar, '0' + pp->altitude);
7641159Sbostic 		if (pp->status == S_MARKED)
7741159Sbostic 			wstandend(radar);
7841159Sbostic 	}
7941159Sbostic 	wrefresh(radar);
8041159Sbostic 	planewin();
8141159Sbostic 	wrefresh(input);		/* return cursor */
8241159Sbostic 	fflush(stdout);
8341159Sbostic }
8441159Sbostic 
init_gr()8541159Sbostic init_gr()
8641159Sbostic {
8741159Sbostic 	static char	buffer[BUFSIZ];
8841159Sbostic 
8941159Sbostic 	initscr();
9041159Sbostic 	setbuf(stdout, buffer);
9141159Sbostic 	input = newwin(INPUT_LINES, COLS - PLANE_COLS, LINES - INPUT_LINES, 0);
9241159Sbostic 	credit = newwin(INPUT_LINES, PLANE_COLS, LINES - INPUT_LINES,
9341159Sbostic 		COLS - PLANE_COLS);
9441159Sbostic 	planes = newwin(LINES - INPUT_LINES, PLANE_COLS, 0, COLS - PLANE_COLS);
9541159Sbostic }
9641159Sbostic 
setup_screen(scp)9741159Sbostic setup_screen(scp)
9841159Sbostic 	C_SCREEN	*scp;
9941159Sbostic {
10041159Sbostic 	register int	i, j;
10141159Sbostic 	char		str[3], *airstr;
10241159Sbostic 
10341159Sbostic 	str[2] = '\0';
10441159Sbostic 
10541159Sbostic 	if (radar != NULL)
10641159Sbostic 		delwin(radar);
10741159Sbostic 	radar = newwin(scp->height, scp->width * 2, 0, 0);
10841159Sbostic 
10941159Sbostic 	if (cleanradar != NULL)
11041159Sbostic 		delwin(cleanradar);
11141159Sbostic 	cleanradar = newwin(scp->height, scp->width * 2, 0, 0);
11241159Sbostic 
11341159Sbostic 	/* minus one here to prevent a scroll */
11441159Sbostic 	for (i = 0; i < PLANE_COLS - 1; i++) {
11541159Sbostic 		wmove(credit, 0, i);
11641159Sbostic 		waddch(credit, C_CREDIT);
11741159Sbostic 		wmove(credit, INPUT_LINES - 1, i);
11841159Sbostic 		waddch(credit, C_CREDIT);
11941159Sbostic 	}
12041159Sbostic 	wmove(credit, INPUT_LINES / 2, 1);
12141159Sbostic 	waddstr(credit, AUTHOR_STR);
12241159Sbostic 
12341159Sbostic 	for (i = 1; i < scp->height - 1; i++) {
12441159Sbostic 		for (j = 1; j < scp->width - 1; j++) {
12541159Sbostic 			wmove(radar, i, j * 2);
12641159Sbostic 			waddch(radar, C_BACKROUND);
12741159Sbostic 		}
12841159Sbostic 	}
12941159Sbostic 
13041159Sbostic 	/*
13141159Sbostic 	 * Draw the lines first, since people like to draw lines
13241159Sbostic 	 * through beacons and exit points.
13341159Sbostic 	 */
13441159Sbostic 	str[0] = C_LINE;
13541159Sbostic 	for (i = 0; i < scp->num_lines; i++) {
13641159Sbostic 		str[1] = ' ';
13741159Sbostic 		draw_line(radar, scp->line[i].p1.x, scp->line[i].p1.y,
13841159Sbostic 			scp->line[i].p2.x, scp->line[i].p2.y, str);
13941159Sbostic 	}
14041159Sbostic 
14141159Sbostic 	str[0] = C_TOPBOTTOM;
14241159Sbostic 	str[1] = C_TOPBOTTOM;
14341159Sbostic 	wmove(radar, 0, 0);
14441159Sbostic 	for (i = 0; i < scp->width - 1; i++)
14541159Sbostic 		waddstr(radar, str);
14641159Sbostic 	waddch(radar, C_TOPBOTTOM);
14741159Sbostic 
14841159Sbostic 	str[0] = C_TOPBOTTOM;
14941159Sbostic 	str[1] = C_TOPBOTTOM;
15041159Sbostic 	wmove(radar, scp->height - 1, 0);
15141159Sbostic 	for (i = 0; i < scp->width - 1; i++)
15241159Sbostic 		waddstr(radar, str);
15341159Sbostic 	waddch(radar, C_TOPBOTTOM);
15441159Sbostic 
15541159Sbostic 	for (i = 1; i < scp->height - 1; i++) {
15641159Sbostic 		wmove(radar, i, 0);
15741159Sbostic 		waddch(radar, C_LEFTRIGHT);
15841159Sbostic 		wmove(radar, i, (scp->width - 1) * 2);
15941159Sbostic 		waddch(radar, C_LEFTRIGHT);
16041159Sbostic 	}
16141159Sbostic 
16241159Sbostic 	str[0] = C_BEACON;
16341159Sbostic 	for (i = 0; i < scp->num_beacons; i++) {
16441159Sbostic 		str[1] = '0' + i;
16541159Sbostic 		wmove(radar, scp->beacon[i].y, scp->beacon[i].x * 2);
16641159Sbostic 		waddstr(radar, str);
16741159Sbostic 	}
16841159Sbostic 
16941159Sbostic 	for (i = 0; i < scp->num_exits; i++) {
17041159Sbostic 		wmove(radar, scp->exit[i].y, scp->exit[i].x * 2);
17141159Sbostic 		waddch(radar, '0' + i);
17241159Sbostic 	}
17341159Sbostic 
17441159Sbostic 	airstr = "^?>?v?<?";
17541159Sbostic 	for (i = 0; i < scp->num_airports; i++) {
17641159Sbostic 		str[0] = airstr[scp->airport[i].dir];
17741159Sbostic 		str[1] = '0' + i;
17841159Sbostic 		wmove(radar, scp->airport[i].y, scp->airport[i].x * 2);
17941159Sbostic 		waddstr(radar, str);
18041159Sbostic 	}
18141159Sbostic 
18241159Sbostic 	overwrite(radar, cleanradar);
18341159Sbostic 	wrefresh(radar);
18441159Sbostic 	wrefresh(credit);
18541159Sbostic 	fflush(stdout);
18641159Sbostic }
18741159Sbostic 
draw_line(w,x,y,lx,ly,s)18841159Sbostic draw_line(w, x, y, lx, ly, s)
18941159Sbostic 	WINDOW	*w;
19041159Sbostic 	char	*s;
19141159Sbostic {
19241159Sbostic 	int	dx, dy;
19341159Sbostic 
19441159Sbostic 	dx = SGN(lx - x);
19541159Sbostic 	dy = SGN(ly - y);
19641159Sbostic 	for (;;) {
19741159Sbostic 		wmove(w, y, x * 2);
19841159Sbostic 		waddstr(w, s);
19941159Sbostic 		if (x == lx && y == ly)
20041159Sbostic 			break;
20141159Sbostic 		x += dx;
20241159Sbostic 		y += dy;
20341159Sbostic 	}
20441159Sbostic }
20541159Sbostic 
ioclrtoeol(pos)20641159Sbostic ioclrtoeol(pos)
20741159Sbostic {
20841159Sbostic 	wmove(input, 0, pos);
20941159Sbostic 	wclrtoeol(input);
21041159Sbostic 	wrefresh(input);
21141159Sbostic 	fflush(stdout);
21241159Sbostic }
21341159Sbostic 
iomove(pos)21441159Sbostic iomove(pos)
21541159Sbostic {
21641159Sbostic 	wmove(input, 0, pos);
21741159Sbostic 	wrefresh(input);
21841159Sbostic 	fflush(stdout);
21941159Sbostic }
22041159Sbostic 
ioaddstr(pos,str)22141159Sbostic ioaddstr(pos, str)
22241159Sbostic 	char	*str;
22341159Sbostic {
22441159Sbostic 	wmove(input, 0, pos);
22541159Sbostic 	waddstr(input, str);
22641159Sbostic 	wrefresh(input);
22741159Sbostic 	fflush(stdout);
22841159Sbostic }
22941159Sbostic 
ioclrtobot()23041159Sbostic ioclrtobot()
23141159Sbostic {
23241159Sbostic 	wclrtobot(input);
23341159Sbostic 	wrefresh(input);
23441159Sbostic 	fflush(stdout);
23541159Sbostic }
23641159Sbostic 
ioerror(pos,len,str)23741159Sbostic ioerror(pos, len, str)
23841159Sbostic 	char	*str;
23941159Sbostic {
24041159Sbostic 	int	i;
24141159Sbostic 
24241159Sbostic 	wmove(input, 1, pos);
24341159Sbostic 	for (i = 0; i < len; i++)
24441159Sbostic 		waddch(input, '^');
24541159Sbostic 	wmove(input, 2, 0);
24641159Sbostic 	waddstr(input, str);
24741159Sbostic 	wrefresh(input);
24841159Sbostic 	fflush(stdout);
24941159Sbostic }
25041159Sbostic 
quit()25141159Sbostic quit()
25241159Sbostic {
25341159Sbostic 	int			c, y, x;
25441159Sbostic #ifdef BSD
25541159Sbostic 	struct itimerval	itv;
25641159Sbostic #endif
25741159Sbostic 
25841159Sbostic 	getyx(input, y, x);
25941159Sbostic 	wmove(input, 2, 0);
26041159Sbostic 	waddstr(input, "Really quit? (y/n) ");
26141159Sbostic 	wclrtobot(input);
26241159Sbostic 	wrefresh(input);
26341159Sbostic 	fflush(stdout);
26441159Sbostic 
26541159Sbostic 	c = getchar();
26641159Sbostic 	if (c == EOF || c == 'y') {
26741159Sbostic 		/* disable timer */
26841159Sbostic #ifdef BSD
26941159Sbostic 		itv.it_value.tv_sec = 0;
27041159Sbostic 		itv.it_value.tv_usec = 0;
27141159Sbostic 		setitimer(ITIMER_REAL, &itv, NULL);
27241159Sbostic #endif
27341159Sbostic #ifdef SYSV
27441159Sbostic 		alarm(0);
27541159Sbostic #endif
27641159Sbostic 		fflush(stdout);
27741159Sbostic 		clear();
27841159Sbostic 		refresh();
27941159Sbostic 		endwin();
28041159Sbostic 		log_score(0);
28141159Sbostic 		exit(0);
28241159Sbostic 	}
28341159Sbostic 	wmove(input, 2, 0);
28441159Sbostic 	wclrtobot(input);
28541159Sbostic 	wmove(input, y, x);
28641159Sbostic 	wrefresh(input);
28741159Sbostic 	fflush(stdout);
28841159Sbostic 	return;
28941159Sbostic }
29041159Sbostic 
planewin()29141159Sbostic planewin()
29241159Sbostic {
29341159Sbostic 	PLANE	*pp;
29441159Sbostic 	char	*command();
29541159Sbostic 	int	warning = 0;
29641159Sbostic 
29741159Sbostic #ifdef BSD
29841159Sbostic 	wclear(planes);
29941159Sbostic #endif
30041159Sbostic 
30141159Sbostic 	wmove(planes, 0,0);
30241159Sbostic 
30341159Sbostic #ifdef SYSV
30441159Sbostic 	wclrtobot(planes);
30541159Sbostic #endif
30645433Sbostic 	wprintw(planes, "Time: %-4d Safe: %d", clck, safe_planes);
30741159Sbostic 	wmove(planes, 2, 0);
30841159Sbostic 
30941159Sbostic 	waddstr(planes, "pl dt  comm");
31041159Sbostic 	for (pp = air.head; pp != NULL; pp = pp->next) {
31141159Sbostic 		if (waddch(planes, '\n') == ERR) {
31241159Sbostic 			warning++;
31341159Sbostic 			break;
31441159Sbostic 		}
31541159Sbostic 		waddstr(planes, command(pp));
31641159Sbostic 	}
31741159Sbostic 	waddch(planes, '\n');
31841159Sbostic 	for (pp = ground.head; pp != NULL; pp = pp->next) {
31941159Sbostic 		if (waddch(planes, '\n') == ERR) {
32041159Sbostic 			warning++;
32141159Sbostic 			break;
32241159Sbostic 		}
32341159Sbostic 		waddstr(planes, command(pp));
32441159Sbostic 	}
32541159Sbostic 	if (warning) {
32641159Sbostic 		wmove(planes, LINES - INPUT_LINES - 1, 0);
32741159Sbostic 		waddstr(planes, "---- more ----");
32841159Sbostic 		wclrtoeol(planes);
32941159Sbostic 	}
33041159Sbostic 	wrefresh(planes);
33141159Sbostic 	fflush(stdout);
33241159Sbostic }
33341159Sbostic 
loser(p,s)33441159Sbostic loser(p, s)
33541159Sbostic 	PLANE	*p;
33641159Sbostic 	char	*s;
33741159Sbostic {
33841159Sbostic 	int			c;
33941159Sbostic #ifdef BSD
34041159Sbostic 	struct itimerval	itv;
34141159Sbostic #endif
34241159Sbostic 
34341159Sbostic 	/* disable timer */
34441159Sbostic #ifdef BSD
34541159Sbostic 	itv.it_value.tv_sec = 0;
34641159Sbostic 	itv.it_value.tv_usec = 0;
34741159Sbostic 	setitimer(ITIMER_REAL, &itv, NULL);
34841159Sbostic #endif
34941159Sbostic #ifdef SYSV
35041159Sbostic 	alarm(0);
35141159Sbostic #endif
35241159Sbostic 
35341159Sbostic 	wmove(input, 0, 0);
35441159Sbostic 	wclrtobot(input);
35541159Sbostic 	wprintw(input, "Plane '%c' %s\n\nHit space for top players list...",
35641159Sbostic 		name(p), s);
35741159Sbostic 	wrefresh(input);
35841159Sbostic 	fflush(stdout);
35941159Sbostic 	while ((c = getchar()) != EOF && c != ' ')
36041159Sbostic 		;
36141159Sbostic 	clear();	/* move to top of screen */
36241159Sbostic 	refresh();
36341159Sbostic 	endwin();
36441159Sbostic 	log_score(0);
36541159Sbostic 	exit(0);
36641159Sbostic }
36741159Sbostic 
redraw()36841159Sbostic redraw()
36941159Sbostic {
37041159Sbostic 	clear();
37141159Sbostic 	refresh();
37241159Sbostic 
37341159Sbostic 	touchwin(radar);
37441159Sbostic 	wrefresh(radar);
37541159Sbostic 	touchwin(planes);
37641159Sbostic 	wrefresh(planes);
37741159Sbostic 	touchwin(credit);
37841159Sbostic 	wrefresh(credit);
37941159Sbostic 
38041159Sbostic 	/* refresh input last to get cursor in right place */
38141159Sbostic 	touchwin(input);
38241159Sbostic 	wrefresh(input);
38341159Sbostic 	fflush(stdout);
38441159Sbostic }
38541159Sbostic 
38641159Sbostic 
done_screen()38741159Sbostic done_screen()
38841159Sbostic {
38941159Sbostic 	clear();
39041159Sbostic 	refresh();
39141159Sbostic 	endwin();	  /* clean up curses */
39241159Sbostic }
393