]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/graphics.c
Converted from sgtty to termios api, no longer needs libcompat.
[bsdgames-darwin.git] / atc / graphics.c
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@berkeley.edu.
44 */
45
46 #ifndef lint
47 /*static char sccsid[] = "from: @(#)graphics.c 5.3 (Berkeley) 10/30/90";*/
48 static char rcsid[] = "$Id: graphics.c,v 1.2 1993/08/01 18:57:09 mycroft Exp $";
49 #endif /* not lint */
50
51 #include "include.h"
52 #ifdef SYSV
53 #include <errno.h>
54 #endif
55
56 #define C_TOPBOTTOM '-'
57 #define C_LEFTRIGHT '|'
58 #define C_AIRPORT '='
59 #define C_LINE '+'
60 #define C_BACKROUND '.'
61 #define C_BEACON '*'
62 #define C_CREDIT '*'
63
64 WINDOW *radar, *cleanradar, *credit, *input, *planes;
65
66 getAChar()
67 {
68 #ifdef BSD
69 return (getchar());
70 #endif
71 #ifdef SYSV
72 int c;
73
74 while ((c = getchar()) == -1 && errno == EINTR) ;
75 return(c);
76 #endif
77 }
78
79 erase_all()
80 {
81 PLANE *pp;
82
83 for (pp = air.head; pp != NULL; pp = pp->next) {
84 wmove(cleanradar, pp->ypos, pp->xpos * 2);
85 wmove(radar, pp->ypos, pp->xpos * 2);
86 waddch(radar, winch(cleanradar));
87 wmove(cleanradar, pp->ypos, pp->xpos * 2 + 1);
88 wmove(radar, pp->ypos, pp->xpos * 2 + 1);
89 waddch(radar, winch(cleanradar));
90 }
91 }
92
93 draw_all()
94 {
95 PLANE *pp;
96
97 for (pp = air.head; pp != NULL; pp = pp->next) {
98 if (pp->status == S_MARKED)
99 wstandout(radar);
100 wmove(radar, pp->ypos, pp->xpos * 2);
101 waddch(radar, name(pp));
102 waddch(radar, '0' + pp->altitude);
103 if (pp->status == S_MARKED)
104 wstandend(radar);
105 }
106 wrefresh(radar);
107 planewin();
108 wrefresh(input); /* return cursor */
109 fflush(stdout);
110 }
111
112 init_gr()
113 {
114 static char buffer[BUFSIZ];
115
116 initscr();
117 setbuf(stdout, buffer);
118 input = newwin(INPUT_LINES, COLS - PLANE_COLS, LINES - INPUT_LINES, 0);
119 credit = newwin(INPUT_LINES, PLANE_COLS, LINES - INPUT_LINES,
120 COLS - PLANE_COLS);
121 planes = newwin(LINES - INPUT_LINES, PLANE_COLS, 0, COLS - PLANE_COLS);
122 }
123
124 setup_screen(scp)
125 C_SCREEN *scp;
126 {
127 register int i, j;
128 char str[3], *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 draw_line(w, x, y, lx, ly, s)
216 WINDOW *w;
217 char *s;
218 {
219 int dx, dy;
220
221 dx = SGN(lx - x);
222 dy = SGN(ly - y);
223 for (;;) {
224 wmove(w, y, x * 2);
225 waddstr(w, s);
226 if (x == lx && y == ly)
227 break;
228 x += dx;
229 y += dy;
230 }
231 }
232
233 ioclrtoeol(pos)
234 {
235 wmove(input, 0, pos);
236 wclrtoeol(input);
237 wrefresh(input);
238 fflush(stdout);
239 }
240
241 iomove(pos)
242 {
243 wmove(input, 0, pos);
244 wrefresh(input);
245 fflush(stdout);
246 }
247
248 ioaddstr(pos, str)
249 char *str;
250 {
251 wmove(input, 0, pos);
252 waddstr(input, str);
253 wrefresh(input);
254 fflush(stdout);
255 }
256
257 ioclrtobot()
258 {
259 wclrtobot(input);
260 wrefresh(input);
261 fflush(stdout);
262 }
263
264 ioerror(pos, len, str)
265 char *str;
266 {
267 int i;
268
269 wmove(input, 1, pos);
270 for (i = 0; i < len; i++)
271 waddch(input, '^');
272 wmove(input, 2, 0);
273 waddstr(input, str);
274 wrefresh(input);
275 fflush(stdout);
276 }
277
278 quit()
279 {
280 int c, y, x;
281 #ifdef BSD
282 struct itimerval itv;
283 #endif
284
285 getyx(input, y, x);
286 wmove(input, 2, 0);
287 waddstr(input, "Really quit? (y/n) ");
288 wclrtobot(input);
289 wrefresh(input);
290 fflush(stdout);
291
292 c = getchar();
293 if (c == EOF || c == 'y') {
294 /* disable timer */
295 #ifdef BSD
296 itv.it_value.tv_sec = 0;
297 itv.it_value.tv_usec = 0;
298 setitimer(ITIMER_REAL, &itv, NULL);
299 #endif
300 #ifdef SYSV
301 alarm(0);
302 #endif
303 fflush(stdout);
304 clear();
305 refresh();
306 endwin();
307 log_score(0);
308 exit(0);
309 }
310 wmove(input, 2, 0);
311 wclrtobot(input);
312 wmove(input, y, x);
313 wrefresh(input);
314 fflush(stdout);
315 return;
316 }
317
318 planewin()
319 {
320 PLANE *pp;
321 char *command();
322 int warning = 0;
323
324 #ifdef BSD
325 wclear(planes);
326 #endif
327
328 wmove(planes, 0,0);
329
330 #ifdef SYSV
331 wclrtobot(planes);
332 #endif
333 wprintw(planes, "Time: %-4d Safe: %d", clck, safe_planes);
334 wmove(planes, 2, 0);
335
336 waddstr(planes, "pl dt comm");
337 for (pp = air.head; pp != NULL; pp = pp->next) {
338 if (waddch(planes, '\n') == ERR) {
339 warning++;
340 break;
341 }
342 waddstr(planes, command(pp));
343 }
344 waddch(planes, '\n');
345 for (pp = ground.head; pp != NULL; pp = pp->next) {
346 if (waddch(planes, '\n') == ERR) {
347 warning++;
348 break;
349 }
350 waddstr(planes, command(pp));
351 }
352 if (warning) {
353 wmove(planes, LINES - INPUT_LINES - 1, 0);
354 waddstr(planes, "---- more ----");
355 wclrtoeol(planes);
356 }
357 wrefresh(planes);
358 fflush(stdout);
359 }
360
361 loser(p, s)
362 PLANE *p;
363 char *s;
364 {
365 int c;
366 #ifdef BSD
367 struct itimerval itv;
368 #endif
369
370 /* disable timer */
371 #ifdef BSD
372 itv.it_value.tv_sec = 0;
373 itv.it_value.tv_usec = 0;
374 setitimer(ITIMER_REAL, &itv, NULL);
375 #endif
376 #ifdef SYSV
377 alarm(0);
378 #endif
379
380 wmove(input, 0, 0);
381 wclrtobot(input);
382 wprintw(input, "Plane '%c' %s\n\nHit space for top players list...",
383 name(p), s);
384 wrefresh(input);
385 fflush(stdout);
386 while ((c = getchar()) != EOF && c != ' ')
387 ;
388 clear(); /* move to top of screen */
389 refresh();
390 endwin();
391 log_score(0);
392 exit(0);
393 }
394
395 redraw()
396 {
397 clear();
398 refresh();
399
400 touchwin(radar);
401 wrefresh(radar);
402 touchwin(planes);
403 wrefresh(planes);
404 touchwin(credit);
405 wrefresh(credit);
406
407 /* refresh input last to get cursor in right place */
408 touchwin(input);
409 wrefresh(input);
410 fflush(stdout);
411 }
412
413
414 done_screen()
415 {
416 clear();
417 refresh();
418 endwin(); /* clean up curses */
419 }