graphics.c revision 1.19 1 /* $NetBSD: graphics.c,v 1.19 2015/06/19 06:02:31 dholland 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 (at) 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.19 2015/06/19 06:02:31 dholland Exp $");
50 #endif
51 #endif /* not lint */
52
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <curses.h>
56 #include <errno.h>
57 #include <err.h>
58
59 #include "def.h"
60 #include "struct.h"
61 #include "extern.h"
62 #include "tunable.h"
63
64 #define C_TOPBOTTOM '-'
65 #define C_LEFTRIGHT '|'
66 #define C_AIRPORT '='
67 #define C_LINE '+'
68 #define C_BACKROUND '.'
69 #define C_BEACON '*'
70 #define C_CREDIT '*'
71
72 static void draw_line(WINDOW *, int, int, int, int, const char *);
73
74 static WINDOW *radar, *cleanradar, *credit, *input, *planes;
75
76 int
77 getAChar(void)
78 {
79 int c;
80
81 errno = 0;
82 while ((c = getchar()) == EOF && errno == EINTR) {
83 errno = 0;
84 clearerr(stdin);
85 }
86 return(c);
87 }
88
89 void
90 erase_all(void)
91 {
92 PLANE *pp;
93
94 for (pp = air.head; pp != NULL; pp = pp->next) {
95 (void)wmove(cleanradar, pp->ypos, pp->xpos * 2);
96 (void)wmove(radar, pp->ypos, pp->xpos * 2);
97 (void)waddch(radar, winch(cleanradar));
98 (void)wmove(cleanradar, pp->ypos, pp->xpos * 2 + 1);
99 (void)wmove(radar, pp->ypos, pp->xpos * 2 + 1);
100 (void)waddch(radar, winch(cleanradar));
101 }
102 }
103
104 void
105 draw_all(void)
106 {
107 PLANE *pp;
108
109 for (pp = air.head; pp != NULL; pp = pp->next) {
110 if (pp->status == S_MARKED)
111 (void)wstandout(radar);
112 (void)wmove(radar, pp->ypos, pp->xpos * 2);
113 (void)waddch(radar, name(pp));
114 (void)waddch(radar, '0' + pp->altitude);
115 if (pp->status == S_MARKED)
116 (void)wstandend(radar);
117 }
118 (void)wrefresh(radar);
119 (void)planewin();
120 (void)wrefresh(input); /* return cursor */
121 (void)fflush(stdout);
122 }
123
124 void
125 init_gr(void)
126 {
127 static char buffer[BUFSIZ];
128
129 if (!initscr())
130 errx(0, "couldn't initialize screen");
131 setbuf(stdout, buffer);
132 input = newwin(INPUT_LINES, COLS - PLANE_COLS, LINES - INPUT_LINES, 0);
133 credit = newwin(INPUT_LINES, PLANE_COLS, LINES - INPUT_LINES,
134 COLS - PLANE_COLS);
135 planes = newwin(LINES - INPUT_LINES, PLANE_COLS, 0, COLS - PLANE_COLS);
136 }
137
138 void
139 setup_screen(const C_SCREEN *scp)
140 {
141 int i, j;
142 unsigned iu;
143 char str[3];
144 const char *airstr;
145
146 str[2] = '\0';
147
148 if (radar != NULL)
149 (void)delwin(radar);
150 radar = newwin(scp->height, scp->width * 2, 0, 0);
151
152 if (cleanradar != NULL)
153 (void)delwin(cleanradar);
154 cleanradar = newwin(scp->height, scp->width * 2, 0, 0);
155
156 /* minus one here to prevent a scroll */
157 for (i = 0; i < PLANE_COLS - 1; i++) {
158 (void)wmove(credit, 0, i);
159 (void)waddch(credit, C_CREDIT);
160 (void)wmove(credit, INPUT_LINES - 1, i);
161 (void)waddch(credit, C_CREDIT);
162 }
163 (void)wmove(credit, INPUT_LINES / 2, 1);
164 (void)waddstr(credit, AUTHOR_STR);
165
166 for (i = 1; i < scp->height - 1; i++) {
167 for (j = 1; j < scp->width - 1; j++) {
168 (void)wmove(radar, i, j * 2);
169 (void)waddch(radar, C_BACKROUND);
170 }
171 }
172
173 /*
174 * Draw the lines first, since people like to draw lines
175 * through beacons and exit points.
176 */
177 str[0] = C_LINE;
178 for (iu = 0; iu < scp->num_lines; iu++) {
179 str[1] = ' ';
180 draw_line(radar, scp->line[iu].p1.x, scp->line[iu].p1.y,
181 scp->line[iu].p2.x, scp->line[iu].p2.y, str);
182 }
183
184 str[0] = C_TOPBOTTOM;
185 str[1] = C_TOPBOTTOM;
186 (void)wmove(radar, 0, 0);
187 for (i = 0; i < scp->width - 1; i++)
188 (void)waddstr(radar, str);
189 (void)waddch(radar, C_TOPBOTTOM);
190
191 str[0] = C_TOPBOTTOM;
192 str[1] = C_TOPBOTTOM;
193 (void)wmove(radar, scp->height - 1, 0);
194 for (i = 0; i < scp->width - 1; i++)
195 (void)waddstr(radar, str);
196 (void)waddch(radar, C_TOPBOTTOM);
197
198 for (i = 1; i < scp->height - 1; i++) {
199 (void)wmove(radar, i, 0);
200 (void)waddch(radar, C_LEFTRIGHT);
201 (void)wmove(radar, i, (scp->width - 1) * 2);
202 (void)waddch(radar, C_LEFTRIGHT);
203 }
204
205 str[0] = C_BEACON;
206 for (iu = 0; iu < scp->num_beacons; iu++) {
207 str[1] = '0' + iu;
208 (void)wmove(radar, scp->beacon[iu].y, scp->beacon[iu].x * 2);
209 (void)waddstr(radar, str);
210 }
211
212 for (iu = 0; iu < scp->num_exits; iu++) {
213 (void)wmove(radar, scp->exit[iu].y, scp->exit[iu].x * 2);
214 (void)waddch(radar, '0' + iu);
215 }
216
217 airstr = "^?>?v?<?";
218 for (iu = 0; iu < scp->num_airports; iu++) {
219 str[0] = airstr[scp->airport[iu].dir];
220 str[1] = '0' + iu;
221 (void)wmove(radar, scp->airport[iu].y, scp->airport[iu].x * 2);
222 (void)waddstr(radar, str);
223 }
224
225 (void)overwrite(radar, cleanradar);
226 (void)wrefresh(radar);
227 (void)wrefresh(credit);
228 (void)fflush(stdout);
229 }
230
231 static void
232 draw_line(WINDOW *w, int x, int y, int lx, int ly, const char *s)
233 {
234 int dx, dy;
235
236 dx = SGN(lx - x);
237 dy = SGN(ly - y);
238 for (;;) {
239 (void)wmove(w, y, x * 2);
240 (void)waddstr(w, s);
241 if (x == lx && y == ly)
242 break;
243 x += dx;
244 y += dy;
245 }
246 }
247
248 void
249 ioclrtoeol(int pos)
250 {
251 (void)wmove(input, 0, pos);
252 (void)wclrtoeol(input);
253 (void)wrefresh(input);
254 (void)fflush(stdout);
255 }
256
257 void
258 iomove(int pos)
259 {
260 (void)wmove(input, 0, pos);
261 (void)wrefresh(input);
262 (void)fflush(stdout);
263 }
264
265 void
266 ioaddstr(int pos, const char *str)
267 {
268 (void)wmove(input, 0, pos);
269 (void)waddstr(input, str);
270 (void)wrefresh(input);
271 (void)fflush(stdout);
272 }
273
274 void
275 ioclrtobot(void)
276 {
277 (void)wclrtobot(input);
278 (void)wrefresh(input);
279 (void)fflush(stdout);
280 }
281
282 void
283 ioerror(int pos, int len, const char *str)
284 {
285 int i;
286
287 (void)wmove(input, 1, pos);
288 for (i = 0; i < len; i++)
289 (void)waddch(input, '^');
290 (void)wmove(input, 2, 0);
291 (void)waddstr(input, str);
292 (void)wrefresh(input);
293 (void)fflush(stdout);
294 }
295
296 /* ARGSUSED */
297 void
298 quit(int dummy __unused)
299 {
300 int c, y, x;
301 #ifdef BSD
302 struct itimerval itv;
303 #endif
304
305 getyx(input, y, x);
306 (void)wmove(input, 2, 0);
307 (void)waddstr(input, "Really quit? (y/n) ");
308 (void)wclrtobot(input);
309 (void)wrefresh(input);
310 (void)fflush(stdout);
311
312 c = getchar();
313 if (c == EOF || c == 'y') {
314 /* disable timer */
315 #ifdef BSD
316 itv.it_value.tv_sec = 0;
317 itv.it_value.tv_usec = 0;
318 (void)setitimer(ITIMER_REAL, &itv, NULL);
319 #endif
320 #ifdef SYSV
321 alarm(0);
322 #endif
323 (void)fflush(stdout);
324 (void)clear();
325 (void)refresh();
326 (void)endwin();
327 (void)log_score(0);
328 exit(0);
329 }
330 (void)wmove(input, 2, 0);
331 (void)wclrtobot(input);
332 (void)wmove(input, y, x);
333 (void)wrefresh(input);
334 (void)fflush(stdout);
335 }
336
337 void
338 planewin(void)
339 {
340 PLANE *pp;
341 int warning = 0;
342
343 #ifdef BSD
344 (void)wclear(planes);
345 #endif
346
347 (void)wmove(planes, 0,0);
348
349 #ifdef SYSV
350 wclrtobot(planes);
351 #endif
352 (void)wprintw(planes, "Time: %-4d Safe: %d", clck, safe_planes);
353 (void)wmove(planes, 2, 0);
354
355 (void)waddstr(planes, "pl dt comm");
356 for (pp = air.head; pp != NULL; pp = pp->next) {
357 if (waddch(planes, '\n') == ERR) {
358 warning++;
359 break;
360 }
361 (void)waddstr(planes, command(pp));
362 }
363 (void)waddch(planes, '\n');
364 for (pp = ground.head; pp != NULL; pp = pp->next) {
365 if (waddch(planes, '\n') == ERR) {
366 warning++;
367 break;
368 }
369 (void)waddstr(planes, command(pp));
370 }
371 if (warning) {
372 (void)wmove(planes, LINES - INPUT_LINES - 1, 0);
373 (void)waddstr(planes, "---- more ----");
374 (void)wclrtoeol(planes);
375 }
376 (void)wrefresh(planes);
377 (void)fflush(stdout);
378 }
379
380 void
381 loser(const PLANE *p, const char *s)
382 {
383 int c;
384 #ifdef BSD
385 struct itimerval itv;
386 #endif
387
388 /* disable timer */
389 #ifdef BSD
390 itv.it_value.tv_sec = 0;
391 itv.it_value.tv_usec = 0;
392 (void)setitimer(ITIMER_REAL, &itv, NULL);
393 #endif
394 #ifdef SYSV
395 alarm(0);
396 #endif
397
398 (void)wmove(input, 0, 0);
399 (void)wclrtobot(input);
400 /* p may be NULL if we ran out of memory */
401 if (p == NULL)
402 (void)wprintw(input, "%s\n\nHit space for top players list...",
403 s);
404 else {
405 (void)wprintw(input, "Plane '%c' %s\n\n", name(p), s);
406 (void)wprintw(input, "Hit space for top players list...");
407 }
408 (void)wrefresh(input);
409 (void)fflush(stdout);
410 while ((c = getchar()) != EOF && c != ' ')
411 ;
412 (void)clear(); /* move to top of screen */
413 (void)refresh();
414 (void)endwin();
415 (void)log_score(0);
416 exit(0);
417 }
418
419 void
420 redraw(void)
421 {
422 (void)clear();
423 (void)refresh();
424
425 (void)touchwin(radar);
426 (void)wrefresh(radar);
427 (void)touchwin(planes);
428 (void)wrefresh(planes);
429 (void)touchwin(credit);
430 (void)wrefresh(credit);
431
432 /* refresh input last to get cursor in right place */
433 (void)touchwin(input);
434 (void)wrefresh(input);
435 (void)fflush(stdout);
436 }
437
438 void
439 done_screen(void)
440 {
441 (void)clear();
442 (void)refresh();
443 (void)endwin(); /* clean up curses */
444 }
445