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