| 1 | /*- |
| 2 | * Copyright (c) 1990, 1993 |
| 3 | * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
| 31 | * |
| 32 | * @(#)graphics.c 8.1 (Berkeley) 5/31/93 |
| 33 | * $FreeBSD: src/games/atc/graphics.c,v 1.7 1999/11/30 03:48:19 billf Exp $ |
| 34 | * $DragonFly: src/games/atc/graphics.c,v 1.3 2006/08/08 15:03:02 pavalos Exp $ |
| 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@berkeley.edu. |
| 44 | */ |
| 45 | |
| 46 | #include <stdlib.h> |
| 47 | #include "include.h" |
| 48 | #include <errno.h> |
| 49 | |
| 50 | #define C_TOPBOTTOM '-' |
| 51 | #define C_LEFTRIGHT '|' |
| 52 | #define C_AIRPORT '=' |
| 53 | #define C_LINE '+' |
| 54 | #define C_BACKROUND '.' |
| 55 | #define C_BEACON '*' |
| 56 | #define C_CREDIT '*' |
| 57 | |
| 58 | static void draw_line(WINDOW *, int, int, int, int, const char *); |
| 59 | |
| 60 | WINDOW *radar, *cleanradar, *credit, *input, *planes; |
| 61 | |
| 62 | int |
| 63 | getAChar(void) |
| 64 | { |
| 65 | int c; |
| 66 | |
| 67 | errno = 0; |
| 68 | while ((c = getchar()) == -1 && errno == EINTR) { |
| 69 | errno = 0; |
| 70 | clearerr(stdin); |
| 71 | } |
| 72 | return(c); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | erase_all(void) |
| 77 | { |
| 78 | PLANE *pp; |
| 79 | |
| 80 | for (pp = air.head; pp != NULL; pp = pp->next) { |
| 81 | wmove(cleanradar, pp->ypos, pp->xpos * 2); |
| 82 | wmove(radar, pp->ypos, pp->xpos * 2); |
| 83 | waddch(radar, winch(cleanradar)); |
| 84 | wmove(cleanradar, pp->ypos, pp->xpos * 2 + 1); |
| 85 | wmove(radar, pp->ypos, pp->xpos * 2 + 1); |
| 86 | waddch(radar, winch(cleanradar)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | draw_all(void) |
| 92 | { |
| 93 | PLANE *pp; |
| 94 | |
| 95 | for (pp = air.head; pp != NULL; pp = pp->next) { |
| 96 | if (pp->status == S_MARKED) |
| 97 | wstandout(radar); |
| 98 | wmove(radar, pp->ypos, pp->xpos * 2); |
| 99 | waddch(radar, name(pp)); |
| 100 | waddch(radar, '0' + pp->altitude); |
| 101 | if (pp->status == S_MARKED) |
| 102 | wstandend(radar); |
| 103 | } |
| 104 | wrefresh(radar); |
| 105 | planewin(); |
| 106 | wrefresh(input); /* return cursor */ |
| 107 | fflush(stdout); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | init_gr(void) |
| 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 | void |
| 124 | setup_screen(const C_SCREEN *scp) |
| 125 | { |
| 126 | int i, j; |
| 127 | char str[3]; |
| 128 | const char *airstr; |
| 129 | |
| 130 | str[2] = '\0'; |
| 131 | |
| 132 | if (radar != NULL) |
| 133 | delwin(radar); |
| 134 | radar = newwin(scp->height, scp->width * 2, 0, 0); |
| 135 | |
| 136 | if (cleanradar != NULL) |
| 137 | delwin(cleanradar); |
| 138 | cleanradar = newwin(scp->height, scp->width * 2, 0, 0); |
| 139 | |
| 140 | /* minus one here to prevent a scroll */ |
| 141 | for (i = 0; i < PLANE_COLS - 1; i++) { |
| 142 | wmove(credit, 0, i); |
| 143 | waddch(credit, C_CREDIT); |
| 144 | wmove(credit, INPUT_LINES - 1, i); |
| 145 | waddch(credit, C_CREDIT); |
| 146 | } |
| 147 | wmove(credit, INPUT_LINES / 2, 1); |
| 148 | waddstr(credit, AUTHOR_STR); |
| 149 | |
| 150 | for (i = 1; i < scp->height - 1; i++) { |
| 151 | for (j = 1; j < scp->width - 1; j++) { |
| 152 | wmove(radar, i, j * 2); |
| 153 | waddch(radar, C_BACKROUND); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Draw the lines first, since people like to draw lines |
| 159 | * through beacons and exit points. |
| 160 | */ |
| 161 | str[0] = C_LINE; |
| 162 | for (i = 0; i < scp->num_lines; i++) { |
| 163 | str[1] = ' '; |
| 164 | draw_line(radar, scp->line[i].p1.x, scp->line[i].p1.y, |
| 165 | scp->line[i].p2.x, scp->line[i].p2.y, str); |
| 166 | } |
| 167 | |
| 168 | str[0] = C_TOPBOTTOM; |
| 169 | str[1] = C_TOPBOTTOM; |
| 170 | wmove(radar, 0, 0); |
| 171 | for (i = 0; i < scp->width - 1; i++) |
| 172 | waddstr(radar, str); |
| 173 | waddch(radar, C_TOPBOTTOM); |
| 174 | |
| 175 | str[0] = C_TOPBOTTOM; |
| 176 | str[1] = C_TOPBOTTOM; |
| 177 | wmove(radar, scp->height - 1, 0); |
| 178 | for (i = 0; i < scp->width - 1; i++) |
| 179 | waddstr(radar, str); |
| 180 | waddch(radar, C_TOPBOTTOM); |
| 181 | |
| 182 | for (i = 1; i < scp->height - 1; i++) { |
| 183 | wmove(radar, i, 0); |
| 184 | waddch(radar, C_LEFTRIGHT); |
| 185 | wmove(radar, i, (scp->width - 1) * 2); |
| 186 | waddch(radar, C_LEFTRIGHT); |
| 187 | } |
| 188 | |
| 189 | str[0] = C_BEACON; |
| 190 | for (i = 0; i < scp->num_beacons; i++) { |
| 191 | str[1] = '0' + i; |
| 192 | wmove(radar, scp->beacon[i].y, scp->beacon[i].x * 2); |
| 193 | waddstr(radar, str); |
| 194 | } |
| 195 | |
| 196 | for (i = 0; i < scp->num_exits; i++) { |
| 197 | wmove(radar, scp->exit[i].y, scp->exit[i].x * 2); |
| 198 | waddch(radar, '0' + i); |
| 199 | } |
| 200 | |
| 201 | airstr = "^?>?v?<?"; |
| 202 | for (i = 0; i < scp->num_airports; i++) { |
| 203 | str[0] = airstr[scp->airport[i].dir]; |
| 204 | str[1] = '0' + i; |
| 205 | wmove(radar, scp->airport[i].y, scp->airport[i].x * 2); |
| 206 | waddstr(radar, str); |
| 207 | } |
| 208 | |
| 209 | overwrite(radar, cleanradar); |
| 210 | wrefresh(radar); |
| 211 | wrefresh(credit); |
| 212 | fflush(stdout); |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | draw_line(WINDOW *w, int x, int y, int lx, int ly, const 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 | void |
| 233 | ioclrtoeol(int pos) |
| 234 | { |
| 235 | wmove(input, 0, pos); |
| 236 | wclrtoeol(input); |
| 237 | wrefresh(input); |
| 238 | fflush(stdout); |
| 239 | } |
| 240 | |
| 241 | void |
| 242 | iomove(int pos) |
| 243 | { |
| 244 | wmove(input, 0, pos); |
| 245 | wrefresh(input); |
| 246 | fflush(stdout); |
| 247 | } |
| 248 | |
| 249 | void |
| 250 | ioaddstr(int pos, const char *str) |
| 251 | { |
| 252 | wmove(input, 0, pos); |
| 253 | waddstr(input, str); |
| 254 | wrefresh(input); |
| 255 | fflush(stdout); |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | ioclrtobot(void) |
| 260 | { |
| 261 | wclrtobot(input); |
| 262 | wrefresh(input); |
| 263 | fflush(stdout); |
| 264 | } |
| 265 | |
| 266 | void |
| 267 | ioerror(int pos, int len, const char *str) |
| 268 | { |
| 269 | int i; |
| 270 | |
| 271 | wmove(input, 1, pos); |
| 272 | for (i = 0; i < len; i++) |
| 273 | waddch(input, '^'); |
| 274 | wmove(input, 2, 0); |
| 275 | waddstr(input, str); |
| 276 | wrefresh(input); |
| 277 | fflush(stdout); |
| 278 | } |
| 279 | |
| 280 | void |
| 281 | quit(void) |
| 282 | { |
| 283 | int c, y, x; |
| 284 | struct itimerval itv; |
| 285 | |
| 286 | getyx(input, y, x); |
| 287 | wmove(input, 2, 0); |
| 288 | waddstr(input, "Really quit? (y/n) "); |
| 289 | wclrtobot(input); |
| 290 | wrefresh(input); |
| 291 | fflush(stdout); |
| 292 | |
| 293 | c = getchar(); |
| 294 | if (c == EOF || c == 'y') { |
| 295 | /* disable timer */ |
| 296 | itv.it_value.tv_sec = 0; |
| 297 | itv.it_value.tv_usec = 0; |
| 298 | setitimer(ITIMER_REAL, &itv, NULL); |
| 299 | fflush(stdout); |
| 300 | clear(); |
| 301 | refresh(); |
| 302 | endwin(); |
| 303 | log_score(0); |
| 304 | exit(0); |
| 305 | } |
| 306 | wmove(input, 2, 0); |
| 307 | wclrtobot(input); |
| 308 | wmove(input, y, x); |
| 309 | wrefresh(input); |
| 310 | fflush(stdout); |
| 311 | } |
| 312 | |
| 313 | void |
| 314 | planewin(void) |
| 315 | { |
| 316 | PLANE *pp; |
| 317 | int warning = 0; |
| 318 | |
| 319 | wclear(planes); |
| 320 | wmove(planes, 0, 0); |
| 321 | wprintw(planes, "Time: %-4d Safe: %d", clck, safe_planes); |
| 322 | wmove(planes, 2, 0); |
| 323 | waddstr(planes, "pl dt comm"); |
| 324 | for (pp = air.head; pp != NULL; pp = pp->next) { |
| 325 | if (waddch(planes, '\n') == ERR) { |
| 326 | warning++; |
| 327 | break; |
| 328 | } |
| 329 | waddstr(planes, command(pp)); |
| 330 | } |
| 331 | waddch(planes, '\n'); |
| 332 | for (pp = ground.head; pp != NULL; pp = pp->next) { |
| 333 | if (waddch(planes, '\n') == ERR) { |
| 334 | warning++; |
| 335 | break; |
| 336 | } |
| 337 | waddstr(planes, command(pp)); |
| 338 | } |
| 339 | if (warning) { |
| 340 | wmove(planes, LINES - INPUT_LINES - 1, 0); |
| 341 | waddstr(planes, "---- more ----"); |
| 342 | wclrtoeol(planes); |
| 343 | } |
| 344 | wrefresh(planes); |
| 345 | fflush(stdout); |
| 346 | } |
| 347 | |
| 348 | void |
| 349 | loser(const PLANE *p, const char *s) |
| 350 | { |
| 351 | int c; |
| 352 | struct itimerval itv; |
| 353 | |
| 354 | /* disable timer */ |
| 355 | itv.it_value.tv_sec = 0; |
| 356 | itv.it_value.tv_usec = 0; |
| 357 | setitimer(ITIMER_REAL, &itv, NULL); |
| 358 | |
| 359 | wmove(input, 0, 0); |
| 360 | wclrtobot(input); |
| 361 | wprintw(input, "Plane '%c' %s\n\nHit space for top players list...", |
| 362 | name(p), s); |
| 363 | wrefresh(input); |
| 364 | fflush(stdout); |
| 365 | while ((c = getchar()) != EOF && c != ' ') |
| 366 | ; |
| 367 | clear(); /* move to top of screen */ |
| 368 | refresh(); |
| 369 | endwin(); |
| 370 | log_score(0); |
| 371 | exit(0); |
| 372 | } |
| 373 | |
| 374 | void |
| 375 | redraw(void) |
| 376 | { |
| 377 | clear(); |
| 378 | refresh(); |
| 379 | |
| 380 | touchwin(radar); |
| 381 | wrefresh(radar); |
| 382 | touchwin(planes); |
| 383 | wrefresh(planes); |
| 384 | touchwin(credit); |
| 385 | wrefresh(credit); |
| 386 | |
| 387 | /* refresh input last to get cursor in right place */ |
| 388 | touchwin(input); |
| 389 | wrefresh(input); |
| 390 | fflush(stdout); |
| 391 | } |
| 392 | |
| 393 | void |
| 394 | done_screen(void) |
| 395 | { |
| 396 | clear(); |
| 397 | refresh(); |
| 398 | endwin(); /* clean up curses */ |
| 399 | } |