xref: /netbsd-src/games/atc/graphics.c (revision f9967d10c997b30f59931b55be2ff010c84270ef)
1 /*	$NetBSD: graphics.c,v 1.11 2005/07/01 00:48:34 jmc Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Ed James.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
37  *
38  * Copy permission is hereby granted provided that this notice is
39  * retained on all partial or complete copies.
40  *
41  * For more info on this and all of my stuff, mail edjames@berkeley.edu.
42  */
43 
44 #include <sys/cdefs.h>
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)graphics.c	8.1 (Berkeley) 5/31/93";
48 #else
49 __RCSID("$NetBSD: graphics.c,v 1.11 2005/07/01 00:48:34 jmc Exp $");
50 #endif
51 #endif /* not lint */
52 
53 #include "include.h"
54 
55 #define C_TOPBOTTOM		'-'
56 #define C_LEFTRIGHT		'|'
57 #define C_AIRPORT		'='
58 #define C_LINE			'+'
59 #define C_BACKROUND		'.'
60 #define C_BEACON		'*'
61 #define C_CREDIT		'*'
62 
63 WINDOW	*radar, *cleanradar, *credit, *input, *planes;
64 
65 int
66 getAChar(void)
67 {
68 	int c;
69 
70 	errno = 0;
71 	while ((c = getchar()) == EOF && errno == EINTR) {
72 		errno = 0;
73 		clearerr(stdin);
74 	}
75 	return(c);
76 }
77 
78 void
79 erase_all(void)
80 {
81 	PLANE	*pp;
82 
83 	for (pp = air.head; pp != NULL; pp = pp->next) {
84 		wmove(cleanradar, pp->ypos, pp->xpos * 2);
85 		wmove(radar, pp->ypos, pp->xpos * 2);
86 		waddch(radar, winch(cleanradar));
87 		wmove(cleanradar, pp->ypos, pp->xpos * 2 + 1);
88 		wmove(radar, pp->ypos, pp->xpos * 2 + 1);
89 		waddch(radar, winch(cleanradar));
90 	}
91 }
92 
93 void
94 draw_all(void)
95 {
96 	PLANE	*pp;
97 
98 	for (pp = air.head; pp != NULL; pp = pp->next) {
99 		if (pp->status == S_MARKED)
100 			wstandout(radar);
101 		wmove(radar, pp->ypos, pp->xpos * 2);
102 		waddch(radar, name(pp));
103 		waddch(radar, '0' + pp->altitude);
104 		if (pp->status == S_MARKED)
105 			wstandend(radar);
106 	}
107 	wrefresh(radar);
108 	planewin();
109 	wrefresh(input);		/* return cursor */
110 	fflush(stdout);
111 }
112 
113 void
114 init_gr(void)
115 {
116 	static char	buffer[BUFSIZ];
117 
118 	initscr();
119 	setbuf(stdout, buffer);
120 	input = newwin(INPUT_LINES, COLS - PLANE_COLS, LINES - INPUT_LINES, 0);
121 	credit = newwin(INPUT_LINES, PLANE_COLS, LINES - INPUT_LINES,
122 		COLS - PLANE_COLS);
123 	planes = newwin(LINES - INPUT_LINES, PLANE_COLS, 0, COLS - PLANE_COLS);
124 }
125 
126 void
127 setup_screen(const C_SCREEN *scp)
128 {
129 	int	i, j;
130 	char	str[3];
131 	const char *airstr;
132 
133 	str[2] = '\0';
134 
135 	if (radar != NULL)
136 		delwin(radar);
137 	radar = newwin(scp->height, scp->width * 2, 0, 0);
138 
139 	if (cleanradar != NULL)
140 		delwin(cleanradar);
141 	cleanradar = newwin(scp->height, scp->width * 2, 0, 0);
142 
143 	/* minus one here to prevent a scroll */
144 	for (i = 0; i < PLANE_COLS - 1; i++) {
145 		wmove(credit, 0, i);
146 		waddch(credit, C_CREDIT);
147 		wmove(credit, INPUT_LINES - 1, i);
148 		waddch(credit, C_CREDIT);
149 	}
150 	wmove(credit, INPUT_LINES / 2, 1);
151 	waddstr(credit, AUTHOR_STR);
152 
153 	for (i = 1; i < scp->height - 1; i++) {
154 		for (j = 1; j < scp->width - 1; j++) {
155 			wmove(radar, i, j * 2);
156 			waddch(radar, C_BACKROUND);
157 		}
158 	}
159 
160 	/*
161 	 * Draw the lines first, since people like to draw lines
162 	 * through beacons and exit points.
163 	 */
164 	str[0] = C_LINE;
165 	for (i = 0; i < scp->num_lines; i++) {
166 		str[1] = ' ';
167 		draw_line(radar, scp->line[i].p1.x, scp->line[i].p1.y,
168 			scp->line[i].p2.x, scp->line[i].p2.y, str);
169 	}
170 
171 	str[0] = C_TOPBOTTOM;
172 	str[1] = C_TOPBOTTOM;
173 	wmove(radar, 0, 0);
174 	for (i = 0; i < scp->width - 1; i++)
175 		waddstr(radar, str);
176 	waddch(radar, C_TOPBOTTOM);
177 
178 	str[0] = C_TOPBOTTOM;
179 	str[1] = C_TOPBOTTOM;
180 	wmove(radar, scp->height - 1, 0);
181 	for (i = 0; i < scp->width - 1; i++)
182 		waddstr(radar, str);
183 	waddch(radar, C_TOPBOTTOM);
184 
185 	for (i = 1; i < scp->height - 1; i++) {
186 		wmove(radar, i, 0);
187 		waddch(radar, C_LEFTRIGHT);
188 		wmove(radar, i, (scp->width - 1) * 2);
189 		waddch(radar, C_LEFTRIGHT);
190 	}
191 
192 	str[0] = C_BEACON;
193 	for (i = 0; i < scp->num_beacons; i++) {
194 		str[1] = '0' + i;
195 		wmove(radar, scp->beacon[i].y, scp->beacon[i].x * 2);
196 		waddstr(radar, str);
197 	}
198 
199 	for (i = 0; i < scp->num_exits; i++) {
200 		wmove(radar, scp->exit[i].y, scp->exit[i].x * 2);
201 		waddch(radar, '0' + i);
202 	}
203 
204 	airstr = "^?>?v?<?";
205 	for (i = 0; i < scp->num_airports; i++) {
206 		str[0] = airstr[scp->airport[i].dir];
207 		str[1] = '0' + i;
208 		wmove(radar, scp->airport[i].y, scp->airport[i].x * 2);
209 		waddstr(radar, str);
210 	}
211 
212 	overwrite(radar, cleanradar);
213 	wrefresh(radar);
214 	wrefresh(credit);
215 	fflush(stdout);
216 }
217 
218 void
219 draw_line(WINDOW *w, int x, int y, int lx, int ly, const char *s)
220 {
221 	int	dx, dy;
222 
223 	dx = SGN(lx - x);
224 	dy = SGN(ly - y);
225 	for (;;) {
226 		wmove(w, y, x * 2);
227 		waddstr(w, s);
228 		if (x == lx && y == ly)
229 			break;
230 		x += dx;
231 		y += dy;
232 	}
233 }
234 
235 void
236 ioclrtoeol(int pos)
237 {
238 	wmove(input, 0, pos);
239 	wclrtoeol(input);
240 	wrefresh(input);
241 	fflush(stdout);
242 }
243 
244 void
245 iomove(int pos)
246 {
247 	wmove(input, 0, pos);
248 	wrefresh(input);
249 	fflush(stdout);
250 }
251 
252 void
253 ioaddstr(int pos, const char *str)
254 {
255 	wmove(input, 0, pos);
256 	waddstr(input, str);
257 	wrefresh(input);
258 	fflush(stdout);
259 }
260 
261 void
262 ioclrtobot(void)
263 {
264 	wclrtobot(input);
265 	wrefresh(input);
266 	fflush(stdout);
267 }
268 
269 void
270 ioerror(int pos, int len, const char *str)
271 {
272 	int	i;
273 
274 	wmove(input, 1, pos);
275 	for (i = 0; i < len; i++)
276 		waddch(input, '^');
277 	wmove(input, 2, 0);
278 	waddstr(input, str);
279 	wrefresh(input);
280 	fflush(stdout);
281 }
282 
283 void
284 quit(int dummy __attribute__((__unused__)))
285 {
286 	int			c, y, x;
287 #ifdef BSD
288 	struct itimerval	itv;
289 #endif
290 
291 	getyx(input, y, x);
292 	wmove(input, 2, 0);
293 	waddstr(input, "Really quit? (y/n) ");
294 	wclrtobot(input);
295 	wrefresh(input);
296 	fflush(stdout);
297 
298 	c = getchar();
299 	if (c == EOF || c == 'y') {
300 		/* disable timer */
301 #ifdef BSD
302 		itv.it_value.tv_sec = 0;
303 		itv.it_value.tv_usec = 0;
304 		setitimer(ITIMER_REAL, &itv, NULL);
305 #endif
306 #ifdef SYSV
307 		alarm(0);
308 #endif
309 		fflush(stdout);
310 		clear();
311 		refresh();
312 		endwin();
313 		log_score(0);
314 		exit(0);
315 	}
316 	wmove(input, 2, 0);
317 	wclrtobot(input);
318 	wmove(input, y, x);
319 	wrefresh(input);
320 	fflush(stdout);
321 }
322 
323 void
324 planewin(void)
325 {
326 	PLANE	*pp;
327 	int	warning = 0;
328 
329 #ifdef BSD
330 	wclear(planes);
331 #endif
332 
333 	wmove(planes, 0,0);
334 
335 #ifdef SYSV
336 	wclrtobot(planes);
337 #endif
338 	wprintw(planes, "Time: %-4d Safe: %d", clck, safe_planes);
339 	wmove(planes, 2, 0);
340 
341 	waddstr(planes, "pl dt  comm");
342 	for (pp = air.head; pp != NULL; pp = pp->next) {
343 		if (waddch(planes, '\n') == ERR) {
344 			warning++;
345 			break;
346 		}
347 		waddstr(planes, command(pp));
348 	}
349 	waddch(planes, '\n');
350 	for (pp = ground.head; pp != NULL; pp = pp->next) {
351 		if (waddch(planes, '\n') == ERR) {
352 			warning++;
353 			break;
354 		}
355 		waddstr(planes, command(pp));
356 	}
357 	if (warning) {
358 		wmove(planes, LINES - INPUT_LINES - 1, 0);
359 		waddstr(planes, "---- more ----");
360 		wclrtoeol(planes);
361 	}
362 	wrefresh(planes);
363 	fflush(stdout);
364 }
365 
366 void
367 loser(const PLANE *p, const char *s)
368 {
369 	int			c;
370 #ifdef BSD
371 	struct itimerval	itv;
372 #endif
373 
374 	/* disable timer */
375 #ifdef BSD
376 	itv.it_value.tv_sec = 0;
377 	itv.it_value.tv_usec = 0;
378 	setitimer(ITIMER_REAL, &itv, NULL);
379 #endif
380 #ifdef SYSV
381 	alarm(0);
382 #endif
383 
384 	wmove(input, 0, 0);
385 	wclrtobot(input);
386 	/* p may be NULL if we ran out of memory */
387 	if (p == NULL)
388 		wprintw(input, "%s\n\nHit space for top players list...", s);
389 	else
390 		wprintw(input, "Plane '%c' %s\n\n", name(p), s);
391 		wprintw(input, "Hit space for top players list...");
392 	wrefresh(input);
393 	fflush(stdout);
394 	while ((c = getchar()) != EOF && c != ' ')
395 		;
396 	clear();	/* move to top of screen */
397 	refresh();
398 	endwin();
399 	log_score(0);
400 	exit(0);
401 }
402 
403 void
404 redraw(void)
405 {
406 	clear();
407 	refresh();
408 
409 	touchwin(radar);
410 	wrefresh(radar);
411 	touchwin(planes);
412 	wrefresh(planes);
413 	touchwin(credit);
414 	wrefresh(credit);
415 
416 	/* refresh input last to get cursor in right place */
417 	touchwin(input);
418 	wrefresh(input);
419 	fflush(stdout);
420 }
421 
422 void
423 done_screen(void)
424 {
425 	clear();
426 	refresh();
427 	endwin();	  /* clean up curses */
428 }
429