xref: /csrg-svn/games/atc/graphics.c (revision 41159)
1*41159Sbostic /*
2*41159Sbostic  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
3*41159Sbostic  *
4*41159Sbostic  * Copy permission is hereby granted provided that this notice is
5*41159Sbostic  * retained on all partial or complete copies.
6*41159Sbostic  *
7*41159Sbostic  * For more info on this and all of my stuff, mail edjames@berkeley.edu.
8*41159Sbostic  */
9*41159Sbostic 
10*41159Sbostic #include "include.h"
11*41159Sbostic #ifdef SYSV
12*41159Sbostic #include <errno.h>
13*41159Sbostic #endif
14*41159Sbostic 
15*41159Sbostic #define C_TOPBOTTOM		'-'
16*41159Sbostic #define C_LEFTRIGHT		'|'
17*41159Sbostic #define C_AIRPORT		'='
18*41159Sbostic #define C_LINE			'+'
19*41159Sbostic #define C_BACKROUND		'.'
20*41159Sbostic #define C_BEACON		'*'
21*41159Sbostic #define C_CREDIT		'*'
22*41159Sbostic 
23*41159Sbostic WINDOW	*radar, *cleanradar, *credit, *input, *planes;
24*41159Sbostic 
25*41159Sbostic getAChar()
26*41159Sbostic {
27*41159Sbostic #ifdef BSD
28*41159Sbostic 	return (getchar());
29*41159Sbostic #endif
30*41159Sbostic #ifdef SYSV
31*41159Sbostic 	int c;
32*41159Sbostic 
33*41159Sbostic 	while ((c = getchar()) == -1 && errno == EINTR) ;
34*41159Sbostic 	return(c);
35*41159Sbostic #endif
36*41159Sbostic }
37*41159Sbostic 
38*41159Sbostic erase_all()
39*41159Sbostic {
40*41159Sbostic 	PLANE	*pp;
41*41159Sbostic 
42*41159Sbostic 	for (pp = air.head; pp != NULL; pp = pp->next) {
43*41159Sbostic 		wmove(cleanradar, pp->ypos, pp->xpos * 2);
44*41159Sbostic 		wmove(radar, pp->ypos, pp->xpos * 2);
45*41159Sbostic 		waddch(radar, winch(cleanradar));
46*41159Sbostic 		wmove(cleanradar, pp->ypos, pp->xpos * 2 + 1);
47*41159Sbostic 		wmove(radar, pp->ypos, pp->xpos * 2 + 1);
48*41159Sbostic 		waddch(radar, winch(cleanradar));
49*41159Sbostic 	}
50*41159Sbostic }
51*41159Sbostic 
52*41159Sbostic draw_all()
53*41159Sbostic {
54*41159Sbostic 	PLANE	*pp;
55*41159Sbostic 
56*41159Sbostic 	for (pp = air.head; pp != NULL; pp = pp->next) {
57*41159Sbostic 		if (pp->status == S_MARKED)
58*41159Sbostic 			wstandout(radar);
59*41159Sbostic 		wmove(radar, pp->ypos, pp->xpos * 2);
60*41159Sbostic 		waddch(radar, name(pp));
61*41159Sbostic 		waddch(radar, '0' + pp->altitude);
62*41159Sbostic 		if (pp->status == S_MARKED)
63*41159Sbostic 			wstandend(radar);
64*41159Sbostic 	}
65*41159Sbostic 	wrefresh(radar);
66*41159Sbostic 	planewin();
67*41159Sbostic 	wrefresh(input);		/* return cursor */
68*41159Sbostic 	fflush(stdout);
69*41159Sbostic }
70*41159Sbostic 
71*41159Sbostic init_gr()
72*41159Sbostic {
73*41159Sbostic 	static char	buffer[BUFSIZ];
74*41159Sbostic 
75*41159Sbostic 	initscr();
76*41159Sbostic 	setbuf(stdout, buffer);
77*41159Sbostic 	input = newwin(INPUT_LINES, COLS - PLANE_COLS, LINES - INPUT_LINES, 0);
78*41159Sbostic 	credit = newwin(INPUT_LINES, PLANE_COLS, LINES - INPUT_LINES,
79*41159Sbostic 		COLS - PLANE_COLS);
80*41159Sbostic 	planes = newwin(LINES - INPUT_LINES, PLANE_COLS, 0, COLS - PLANE_COLS);
81*41159Sbostic }
82*41159Sbostic 
83*41159Sbostic setup_screen(scp)
84*41159Sbostic 	C_SCREEN	*scp;
85*41159Sbostic {
86*41159Sbostic 	register int	i, j;
87*41159Sbostic 	char		str[3], *airstr;
88*41159Sbostic 
89*41159Sbostic 	str[2] = '\0';
90*41159Sbostic 
91*41159Sbostic 	if (radar != NULL)
92*41159Sbostic 		delwin(radar);
93*41159Sbostic 	radar = newwin(scp->height, scp->width * 2, 0, 0);
94*41159Sbostic 
95*41159Sbostic 	if (cleanradar != NULL)
96*41159Sbostic 		delwin(cleanradar);
97*41159Sbostic 	cleanradar = newwin(scp->height, scp->width * 2, 0, 0);
98*41159Sbostic 
99*41159Sbostic 	/* minus one here to prevent a scroll */
100*41159Sbostic 	for (i = 0; i < PLANE_COLS - 1; i++) {
101*41159Sbostic 		wmove(credit, 0, i);
102*41159Sbostic 		waddch(credit, C_CREDIT);
103*41159Sbostic 		wmove(credit, INPUT_LINES - 1, i);
104*41159Sbostic 		waddch(credit, C_CREDIT);
105*41159Sbostic 	}
106*41159Sbostic 	wmove(credit, INPUT_LINES / 2, 1);
107*41159Sbostic 	waddstr(credit, AUTHOR_STR);
108*41159Sbostic 
109*41159Sbostic 	for (i = 1; i < scp->height - 1; i++) {
110*41159Sbostic 		for (j = 1; j < scp->width - 1; j++) {
111*41159Sbostic 			wmove(radar, i, j * 2);
112*41159Sbostic 			waddch(radar, C_BACKROUND);
113*41159Sbostic 		}
114*41159Sbostic 	}
115*41159Sbostic 
116*41159Sbostic 	/*
117*41159Sbostic 	 * Draw the lines first, since people like to draw lines
118*41159Sbostic 	 * through beacons and exit points.
119*41159Sbostic 	 */
120*41159Sbostic 	str[0] = C_LINE;
121*41159Sbostic 	for (i = 0; i < scp->num_lines; i++) {
122*41159Sbostic 		str[1] = ' ';
123*41159Sbostic 		draw_line(radar, scp->line[i].p1.x, scp->line[i].p1.y,
124*41159Sbostic 			scp->line[i].p2.x, scp->line[i].p2.y, str);
125*41159Sbostic 	}
126*41159Sbostic 
127*41159Sbostic 	str[0] = C_TOPBOTTOM;
128*41159Sbostic 	str[1] = C_TOPBOTTOM;
129*41159Sbostic 	wmove(radar, 0, 0);
130*41159Sbostic 	for (i = 0; i < scp->width - 1; i++)
131*41159Sbostic 		waddstr(radar, str);
132*41159Sbostic 	waddch(radar, C_TOPBOTTOM);
133*41159Sbostic 
134*41159Sbostic 	str[0] = C_TOPBOTTOM;
135*41159Sbostic 	str[1] = C_TOPBOTTOM;
136*41159Sbostic 	wmove(radar, scp->height - 1, 0);
137*41159Sbostic 	for (i = 0; i < scp->width - 1; i++)
138*41159Sbostic 		waddstr(radar, str);
139*41159Sbostic 	waddch(radar, C_TOPBOTTOM);
140*41159Sbostic 
141*41159Sbostic 	for (i = 1; i < scp->height - 1; i++) {
142*41159Sbostic 		wmove(radar, i, 0);
143*41159Sbostic 		waddch(radar, C_LEFTRIGHT);
144*41159Sbostic 		wmove(radar, i, (scp->width - 1) * 2);
145*41159Sbostic 		waddch(radar, C_LEFTRIGHT);
146*41159Sbostic 	}
147*41159Sbostic 
148*41159Sbostic 	str[0] = C_BEACON;
149*41159Sbostic 	for (i = 0; i < scp->num_beacons; i++) {
150*41159Sbostic 		str[1] = '0' + i;
151*41159Sbostic 		wmove(radar, scp->beacon[i].y, scp->beacon[i].x * 2);
152*41159Sbostic 		waddstr(radar, str);
153*41159Sbostic 	}
154*41159Sbostic 
155*41159Sbostic 	for (i = 0; i < scp->num_exits; i++) {
156*41159Sbostic 		wmove(radar, scp->exit[i].y, scp->exit[i].x * 2);
157*41159Sbostic 		waddch(radar, '0' + i);
158*41159Sbostic 	}
159*41159Sbostic 
160*41159Sbostic 	airstr = "^?>?v?<?";
161*41159Sbostic 	for (i = 0; i < scp->num_airports; i++) {
162*41159Sbostic 		str[0] = airstr[scp->airport[i].dir];
163*41159Sbostic 		str[1] = '0' + i;
164*41159Sbostic 		wmove(radar, scp->airport[i].y, scp->airport[i].x * 2);
165*41159Sbostic 		waddstr(radar, str);
166*41159Sbostic 	}
167*41159Sbostic 
168*41159Sbostic 	overwrite(radar, cleanradar);
169*41159Sbostic 	wrefresh(radar);
170*41159Sbostic 	wrefresh(credit);
171*41159Sbostic 	fflush(stdout);
172*41159Sbostic }
173*41159Sbostic 
174*41159Sbostic draw_line(w, x, y, lx, ly, s)
175*41159Sbostic 	WINDOW	*w;
176*41159Sbostic 	char	*s;
177*41159Sbostic {
178*41159Sbostic 	int	dx, dy;
179*41159Sbostic 
180*41159Sbostic 	dx = SGN(lx - x);
181*41159Sbostic 	dy = SGN(ly - y);
182*41159Sbostic 	for (;;) {
183*41159Sbostic 		wmove(w, y, x * 2);
184*41159Sbostic 		waddstr(w, s);
185*41159Sbostic 		if (x == lx && y == ly)
186*41159Sbostic 			break;
187*41159Sbostic 		x += dx;
188*41159Sbostic 		y += dy;
189*41159Sbostic 	}
190*41159Sbostic }
191*41159Sbostic 
192*41159Sbostic ioclrtoeol(pos)
193*41159Sbostic {
194*41159Sbostic 	wmove(input, 0, pos);
195*41159Sbostic 	wclrtoeol(input);
196*41159Sbostic 	wrefresh(input);
197*41159Sbostic 	fflush(stdout);
198*41159Sbostic }
199*41159Sbostic 
200*41159Sbostic iomove(pos)
201*41159Sbostic {
202*41159Sbostic 	wmove(input, 0, pos);
203*41159Sbostic 	wrefresh(input);
204*41159Sbostic 	fflush(stdout);
205*41159Sbostic }
206*41159Sbostic 
207*41159Sbostic ioaddstr(pos, str)
208*41159Sbostic 	char	*str;
209*41159Sbostic {
210*41159Sbostic 	wmove(input, 0, pos);
211*41159Sbostic 	waddstr(input, str);
212*41159Sbostic 	wrefresh(input);
213*41159Sbostic 	fflush(stdout);
214*41159Sbostic }
215*41159Sbostic 
216*41159Sbostic ioclrtobot()
217*41159Sbostic {
218*41159Sbostic 	wclrtobot(input);
219*41159Sbostic 	wrefresh(input);
220*41159Sbostic 	fflush(stdout);
221*41159Sbostic }
222*41159Sbostic 
223*41159Sbostic ioerror(pos, len, str)
224*41159Sbostic 	char	*str;
225*41159Sbostic {
226*41159Sbostic 	int	i;
227*41159Sbostic 
228*41159Sbostic 	wmove(input, 1, pos);
229*41159Sbostic 	for (i = 0; i < len; i++)
230*41159Sbostic 		waddch(input, '^');
231*41159Sbostic 	wmove(input, 2, 0);
232*41159Sbostic 	waddstr(input, str);
233*41159Sbostic 	wrefresh(input);
234*41159Sbostic 	fflush(stdout);
235*41159Sbostic }
236*41159Sbostic 
237*41159Sbostic quit()
238*41159Sbostic {
239*41159Sbostic 	int			c, y, x;
240*41159Sbostic #ifdef BSD
241*41159Sbostic 	struct itimerval	itv;
242*41159Sbostic #endif
243*41159Sbostic 
244*41159Sbostic 	getyx(input, y, x);
245*41159Sbostic 	wmove(input, 2, 0);
246*41159Sbostic 	waddstr(input, "Really quit? (y/n) ");
247*41159Sbostic 	wclrtobot(input);
248*41159Sbostic 	wrefresh(input);
249*41159Sbostic 	fflush(stdout);
250*41159Sbostic 
251*41159Sbostic 	c = getchar();
252*41159Sbostic 	if (c == EOF || c == 'y') {
253*41159Sbostic 		/* disable timer */
254*41159Sbostic #ifdef BSD
255*41159Sbostic 		itv.it_value.tv_sec = 0;
256*41159Sbostic 		itv.it_value.tv_usec = 0;
257*41159Sbostic 		setitimer(ITIMER_REAL, &itv, NULL);
258*41159Sbostic #endif
259*41159Sbostic #ifdef SYSV
260*41159Sbostic 		alarm(0);
261*41159Sbostic #endif
262*41159Sbostic 		fflush(stdout);
263*41159Sbostic 		clear();
264*41159Sbostic 		refresh();
265*41159Sbostic 		endwin();
266*41159Sbostic 		log_score(0);
267*41159Sbostic 		exit(0);
268*41159Sbostic 	}
269*41159Sbostic 	wmove(input, 2, 0);
270*41159Sbostic 	wclrtobot(input);
271*41159Sbostic 	wmove(input, y, x);
272*41159Sbostic 	wrefresh(input);
273*41159Sbostic 	fflush(stdout);
274*41159Sbostic 	return;
275*41159Sbostic }
276*41159Sbostic 
277*41159Sbostic planewin()
278*41159Sbostic {
279*41159Sbostic 	PLANE	*pp;
280*41159Sbostic 	char	*command();
281*41159Sbostic 	int	warning = 0;
282*41159Sbostic 
283*41159Sbostic #ifdef BSD
284*41159Sbostic 	wclear(planes);
285*41159Sbostic #endif
286*41159Sbostic 
287*41159Sbostic 	wmove(planes, 0,0);
288*41159Sbostic 
289*41159Sbostic #ifdef SYSV
290*41159Sbostic 	wclrtobot(planes);
291*41159Sbostic #endif
292*41159Sbostic 	wprintw(planes, "Time: %-4d Safe: %d", clock, safe_planes);
293*41159Sbostic 	wmove(planes, 2, 0);
294*41159Sbostic 
295*41159Sbostic 	waddstr(planes, "pl dt  comm");
296*41159Sbostic 	for (pp = air.head; pp != NULL; pp = pp->next) {
297*41159Sbostic 		if (waddch(planes, '\n') == ERR) {
298*41159Sbostic 			warning++;
299*41159Sbostic 			break;
300*41159Sbostic 		}
301*41159Sbostic 		waddstr(planes, command(pp));
302*41159Sbostic 	}
303*41159Sbostic 	waddch(planes, '\n');
304*41159Sbostic 	for (pp = ground.head; pp != NULL; pp = pp->next) {
305*41159Sbostic 		if (waddch(planes, '\n') == ERR) {
306*41159Sbostic 			warning++;
307*41159Sbostic 			break;
308*41159Sbostic 		}
309*41159Sbostic 		waddstr(planes, command(pp));
310*41159Sbostic 	}
311*41159Sbostic 	if (warning) {
312*41159Sbostic 		wmove(planes, LINES - INPUT_LINES - 1, 0);
313*41159Sbostic 		waddstr(planes, "---- more ----");
314*41159Sbostic 		wclrtoeol(planes);
315*41159Sbostic 	}
316*41159Sbostic 	wrefresh(planes);
317*41159Sbostic 	fflush(stdout);
318*41159Sbostic }
319*41159Sbostic 
320*41159Sbostic loser(p, s)
321*41159Sbostic 	PLANE	*p;
322*41159Sbostic 	char	*s;
323*41159Sbostic {
324*41159Sbostic 	int			c;
325*41159Sbostic #ifdef BSD
326*41159Sbostic 	struct itimerval	itv;
327*41159Sbostic #endif
328*41159Sbostic 
329*41159Sbostic 	/* disable timer */
330*41159Sbostic #ifdef BSD
331*41159Sbostic 	itv.it_value.tv_sec = 0;
332*41159Sbostic 	itv.it_value.tv_usec = 0;
333*41159Sbostic 	setitimer(ITIMER_REAL, &itv, NULL);
334*41159Sbostic #endif
335*41159Sbostic #ifdef SYSV
336*41159Sbostic 	alarm(0);
337*41159Sbostic #endif
338*41159Sbostic 
339*41159Sbostic 	wmove(input, 0, 0);
340*41159Sbostic 	wclrtobot(input);
341*41159Sbostic 	wprintw(input, "Plane '%c' %s\n\nHit space for top players list...",
342*41159Sbostic 		name(p), s);
343*41159Sbostic 	wrefresh(input);
344*41159Sbostic 	fflush(stdout);
345*41159Sbostic 	while ((c = getchar()) != EOF && c != ' ')
346*41159Sbostic 		;
347*41159Sbostic 	clear();	/* move to top of screen */
348*41159Sbostic 	refresh();
349*41159Sbostic 	endwin();
350*41159Sbostic 	log_score(0);
351*41159Sbostic 	exit(0);
352*41159Sbostic }
353*41159Sbostic 
354*41159Sbostic redraw()
355*41159Sbostic {
356*41159Sbostic 	clear();
357*41159Sbostic 	refresh();
358*41159Sbostic 
359*41159Sbostic 	touchwin(radar);
360*41159Sbostic 	wrefresh(radar);
361*41159Sbostic 	touchwin(planes);
362*41159Sbostic 	wrefresh(planes);
363*41159Sbostic 	touchwin(credit);
364*41159Sbostic 	wrefresh(credit);
365*41159Sbostic 
366*41159Sbostic 	/* refresh input last to get cursor in right place */
367*41159Sbostic 	touchwin(input);
368*41159Sbostic 	wrefresh(input);
369*41159Sbostic 	fflush(stdout);
370*41159Sbostic }
371*41159Sbostic 
372*41159Sbostic 
373*41159Sbostic done_screen()
374*41159Sbostic {
375*41159Sbostic 	clear();
376*41159Sbostic 	refresh();
377*41159Sbostic 	endwin();	  /* clean up curses */
378*41159Sbostic }
379