]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/graphics.c
clean up import
[bsdgames-darwin.git] / atc / graphics.c
1 /* $NetBSD: graphics.c,v 1.3 1995/03/21 15:04:04 cgd 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@berkeley.edu.
46 */
47
48 #ifndef lint
49 #if 0
50 static char sccsid[] = "@(#)graphics.c 8.1 (Berkeley) 5/31/93";
51 #else
52 static char rcsid[] = "$NetBSD: graphics.c,v 1.3 1995/03/21 15:04:04 cgd Exp $";
53 #endif
54 #endif /* not lint */
55
56 #include "include.h"
57 #ifdef SYSV
58 #include <errno.h>
59 #endif
60
61 #define C_TOPBOTTOM '-'
62 #define C_LEFTRIGHT '|'
63 #define C_AIRPORT '='
64 #define C_LINE '+'
65 #define C_BACKROUND '.'
66 #define C_BEACON '*'
67 #define C_CREDIT '*'
68
69 WINDOW *radar, *cleanradar, *credit, *input, *planes;
70
71 getAChar()
72 {
73 #ifdef BSD
74 return (getchar());
75 #endif
76 #ifdef SYSV
77 int c;
78
79 while ((c = getchar()) == -1 && errno == EINTR) ;
80 return(c);
81 #endif
82 }
83
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 draw_all()
99 {
100 PLANE *pp;
101
102 for (pp = air.head; pp != NULL; pp = pp->next) {
103 if (pp->status == S_MARKED)
104 wstandout(radar);
105 wmove(radar, pp->ypos, pp->xpos * 2);
106 waddch(radar, name(pp));
107 waddch(radar, '0' + pp->altitude);
108 if (pp->status == S_MARKED)
109 wstandend(radar);
110 }
111 wrefresh(radar);
112 planewin();
113 wrefresh(input); /* return cursor */
114 fflush(stdout);
115 }
116
117 init_gr()
118 {
119 static char buffer[BUFSIZ];
120
121 initscr();
122 setbuf(stdout, buffer);
123 input = newwin(INPUT_LINES, COLS - PLANE_COLS, LINES - INPUT_LINES, 0);
124 credit = newwin(INPUT_LINES, PLANE_COLS, LINES - INPUT_LINES,
125 COLS - PLANE_COLS);
126 planes = newwin(LINES - INPUT_LINES, PLANE_COLS, 0, COLS - PLANE_COLS);
127 }
128
129 setup_screen(scp)
130 C_SCREEN *scp;
131 {
132 register int i, j;
133 char str[3], *airstr;
134
135 str[2] = '\0';
136
137 if (radar != NULL)
138 delwin(radar);
139 radar = newwin(scp->height, scp->width * 2, 0, 0);
140
141 if (cleanradar != NULL)
142 delwin(cleanradar);
143 cleanradar = newwin(scp->height, scp->width * 2, 0, 0);
144
145 /* minus one here to prevent a scroll */
146 for (i = 0; i < PLANE_COLS - 1; i++) {
147 wmove(credit, 0, i);
148 waddch(credit, C_CREDIT);
149 wmove(credit, INPUT_LINES - 1, i);
150 waddch(credit, C_CREDIT);
151 }
152 wmove(credit, INPUT_LINES / 2, 1);
153 waddstr(credit, AUTHOR_STR);
154
155 for (i = 1; i < scp->height - 1; i++) {
156 for (j = 1; j < scp->width - 1; j++) {
157 wmove(radar, i, j * 2);
158 waddch(radar, C_BACKROUND);
159 }
160 }
161
162 /*
163 * Draw the lines first, since people like to draw lines
164 * through beacons and exit points.
165 */
166 str[0] = C_LINE;
167 for (i = 0; i < scp->num_lines; i++) {
168 str[1] = ' ';
169 draw_line(radar, scp->line[i].p1.x, scp->line[i].p1.y,
170 scp->line[i].p2.x, scp->line[i].p2.y, str);
171 }
172
173 str[0] = C_TOPBOTTOM;
174 str[1] = C_TOPBOTTOM;
175 wmove(radar, 0, 0);
176 for (i = 0; i < scp->width - 1; i++)
177 waddstr(radar, str);
178 waddch(radar, C_TOPBOTTOM);
179
180 str[0] = C_TOPBOTTOM;
181 str[1] = C_TOPBOTTOM;
182 wmove(radar, scp->height - 1, 0);
183 for (i = 0; i < scp->width - 1; i++)
184 waddstr(radar, str);
185 waddch(radar, C_TOPBOTTOM);
186
187 for (i = 1; i < scp->height - 1; i++) {
188 wmove(radar, i, 0);
189 waddch(radar, C_LEFTRIGHT);
190 wmove(radar, i, (scp->width - 1) * 2);
191 waddch(radar, C_LEFTRIGHT);
192 }
193
194 str[0] = C_BEACON;
195 for (i = 0; i < scp->num_beacons; i++) {
196 str[1] = '0' + i;
197 wmove(radar, scp->beacon[i].y, scp->beacon[i].x * 2);
198 waddstr(radar, str);
199 }
200
201 for (i = 0; i < scp->num_exits; i++) {
202 wmove(radar, scp->exit[i].y, scp->exit[i].x * 2);
203 waddch(radar, '0' + i);
204 }
205
206 airstr = "^?>?v?<?";
207 for (i = 0; i < scp->num_airports; i++) {
208 str[0] = airstr[scp->airport[i].dir];
209 str[1] = '0' + i;
210 wmove(radar, scp->airport[i].y, scp->airport[i].x * 2);
211 waddstr(radar, str);
212 }
213
214 overwrite(radar, cleanradar);
215 wrefresh(radar);
216 wrefresh(credit);
217 fflush(stdout);
218 }
219
220 draw_line(w, x, y, lx, ly, s)
221 WINDOW *w;
222 char *s;
223 {
224 int dx, dy;
225
226 dx = SGN(lx - x);
227 dy = SGN(ly - y);
228 for (;;) {
229 wmove(w, y, x * 2);
230 waddstr(w, s);
231 if (x == lx && y == ly)
232 break;
233 x += dx;
234 y += dy;
235 }
236 }
237
238 ioclrtoeol(pos)
239 {
240 wmove(input, 0, pos);
241 wclrtoeol(input);
242 wrefresh(input);
243 fflush(stdout);
244 }
245
246 iomove(pos)
247 {
248 wmove(input, 0, pos);
249 wrefresh(input);
250 fflush(stdout);
251 }
252
253 ioaddstr(pos, str)
254 char *str;
255 {
256 wmove(input, 0, pos);
257 waddstr(input, str);
258 wrefresh(input);
259 fflush(stdout);
260 }
261
262 ioclrtobot()
263 {
264 wclrtobot(input);
265 wrefresh(input);
266 fflush(stdout);
267 }
268
269 ioerror(pos, len, str)
270 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 quit()
284 {
285 int c, y, x;
286 #ifdef BSD
287 struct itimerval itv;
288 #endif
289
290 getyx(input, y, x);
291 wmove(input, 2, 0);
292 waddstr(input, "Really quit? (y/n) ");
293 wclrtobot(input);
294 wrefresh(input);
295 fflush(stdout);
296
297 c = getchar();
298 if (c == EOF || c == 'y') {
299 /* disable timer */
300 #ifdef BSD
301 itv.it_value.tv_sec = 0;
302 itv.it_value.tv_usec = 0;
303 setitimer(ITIMER_REAL, &itv, NULL);
304 #endif
305 #ifdef SYSV
306 alarm(0);
307 #endif
308 fflush(stdout);
309 clear();
310 refresh();
311 endwin();
312 log_score(0);
313 exit(0);
314 }
315 wmove(input, 2, 0);
316 wclrtobot(input);
317 wmove(input, y, x);
318 wrefresh(input);
319 fflush(stdout);
320 return;
321 }
322
323 planewin()
324 {
325 PLANE *pp;
326 char *command();
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 loser(p, s)
367 PLANE *p;
368 char *s;
369 {
370 int c;
371 #ifdef BSD
372 struct itimerval itv;
373 #endif
374
375 /* disable timer */
376 #ifdef BSD
377 itv.it_value.tv_sec = 0;
378 itv.it_value.tv_usec = 0;
379 setitimer(ITIMER_REAL, &itv, NULL);
380 #endif
381 #ifdef SYSV
382 alarm(0);
383 #endif
384
385 wmove(input, 0, 0);
386 wclrtobot(input);
387 wprintw(input, "Plane '%c' %s\n\nHit space for top players list...",
388 name(p), s);
389 wrefresh(input);
390 fflush(stdout);
391 while ((c = getchar()) != EOF && c != ' ')
392 ;
393 clear(); /* move to top of screen */
394 refresh();
395 endwin();
396 log_score(0);
397 exit(0);
398 }
399
400 redraw()
401 {
402 clear();
403 refresh();
404
405 touchwin(radar);
406 wrefresh(radar);
407 touchwin(planes);
408 wrefresh(planes);
409 touchwin(credit);
410 wrefresh(credit);
411
412 /* refresh input last to get cursor in right place */
413 touchwin(input);
414 wrefresh(input);
415 fflush(stdout);
416 }
417
418
419 done_screen()
420 {
421 clear();
422 refresh();
423 endwin(); /* clean up curses */
424 }