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