xref: /openbsd-src/games/atc/graphics.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: graphics.c,v 1.11 2015/12/31 16:50:29 mestre Exp $	*/
2 /*	$NetBSD: graphics.c,v 1.3 1995/03/21 15:04:04 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Ed James.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 /*
37  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
38  *
39  * Copy permission is hereby granted provided that this notice is
40  * retained on all partial or complete copies.
41  *
42  * For more info on this and all of my stuff, mail edjames@berkeley.edu.
43  */
44 
45 #include <curses.h>
46 #include <err.h>
47 #include <stdlib.h>
48 
49 #include "def.h"
50 #include "extern.h"
51 
52 #define C_TOPBOTTOM		'-'
53 #define C_LEFTRIGHT		'|'
54 #define C_AIRPORT		'='
55 #define C_LINE			'+'
56 #define C_BACKROUND		'.'
57 #define C_BEACON		'*'
58 #define C_CREDIT		'*'
59 
60 WINDOW	*radar, *cleanradar, *credit, *input, *planes;
61 
62 int
63 getAChar(void)
64 {
65 	int c;
66 
67 	if ((c = getchar()) == EOF && feof(stdin))
68 		quit(0);
69 	return (c);
70 }
71 
72 void
73 erase_all(void)
74 {
75 	PLANE	*pp;
76 
77 	for (pp = air.head; pp != NULL; pp = pp->next) {
78 		wmove(cleanradar, pp->ypos, pp->xpos * 2);
79 		wmove(radar, pp->ypos, pp->xpos * 2);
80 		waddch(radar, winch(cleanradar));
81 		wmove(cleanradar, pp->ypos, pp->xpos * 2 + 1);
82 		wmove(radar, pp->ypos, pp->xpos * 2 + 1);
83 		waddch(radar, winch(cleanradar));
84 	}
85 }
86 
87 void
88 draw_all(void)
89 {
90 	PLANE	*pp;
91 
92 	for (pp = air.head; pp != NULL; pp = pp->next) {
93 		if (pp->status == S_MARKED)
94 			wstandout(radar);
95 		wmove(radar, pp->ypos, pp->xpos * 2);
96 		waddch(radar, name(pp));
97 		waddch(radar, '0' + pp->altitude);
98 		if (pp->status == S_MARKED)
99 			wstandend(radar);
100 	}
101 	wrefresh(radar);
102 	planewin();
103 	wrefresh(input);		/* return cursor */
104 	fflush(stdout);
105 }
106 
107 void
108 setup_screen(const C_SCREEN *scp)
109 {
110 	static char	buffer[BUFSIZ];
111 	int	i, j;
112 	char	str[3];
113 	const char *airstr;
114 
115 	initscr();
116 	/* size of screen depends on chosen game, but we need at least 80
117 	 * columns for "Information area" to work. */
118 	if (LINES < (INPUT_LINES + scp->height) ||
119 	    COLS < (PLANE_COLS + 2 * scp->width) ||
120 	    COLS < 80) {
121 		endwin();
122 		errx(1, "screen too small.");
123 	}
124 	setvbuf(stdout, buffer, _IOFBF, sizeof buffer);
125 	input = newwin(INPUT_LINES, COLS - PLANE_COLS, LINES - INPUT_LINES, 0);
126 	credit = newwin(INPUT_LINES, PLANE_COLS, LINES - INPUT_LINES,
127 		COLS - PLANE_COLS);
128 	planes = newwin(LINES - INPUT_LINES, PLANE_COLS, 0, COLS - PLANE_COLS);
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 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(int dummy)
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 
321 	wmove(planes, 0,0);
322 
323 	wprintw(planes, "Time: %-4d Safe: %d", clck, safe_planes);
324 	wmove(planes, 2, 0);
325 
326 	waddstr(planes, "pl dt  comm");
327 	for (pp = air.head; pp != NULL; pp = pp->next) {
328 		if (waddch(planes, '\n') == ERR) {
329 			warning++;
330 			break;
331 		}
332 		waddstr(planes, command(pp));
333 	}
334 	waddch(planes, '\n');
335 	for (pp = ground.head; pp != NULL; pp = pp->next) {
336 		if (waddch(planes, '\n') == ERR) {
337 			warning++;
338 			break;
339 		}
340 		waddstr(planes, command(pp));
341 	}
342 	if (warning) {
343 		wmove(planes, LINES - INPUT_LINES - 1, 0);
344 		waddstr(planes, "---- more ----");
345 		wclrtoeol(planes);
346 	}
347 	wrefresh(planes);
348 	fflush(stdout);
349 }
350 
351 void
352 loser(const PLANE *p, const char *s)
353 {
354 	int			c;
355 	struct itimerval	itv;
356 
357 	/* disable timer */
358 	itv.it_value.tv_sec = 0;
359 	itv.it_value.tv_usec = 0;
360 	setitimer(ITIMER_REAL, &itv, NULL);
361 
362 	wmove(input, 0, 0);
363 	wclrtobot(input);
364 	if (p == NULL)
365 		wprintw(input, "%s\n\nHit space for top players list...", s);
366 	else
367 		wprintw(input, "Plane '%c' %s\n\nHit space for top players list...",
368 			name(p), s);
369 	wrefresh(input);
370 	fflush(stdout);
371 	while ((c = getchar()) != EOF && c != ' ')
372 		;
373 	clear();	/* move to top of screen */
374 	refresh();
375 	endwin();
376 	log_score(0);
377 	exit(0);
378 }
379 
380 void
381 redraw(void)
382 {
383 	clear();
384 	refresh();
385 
386 	touchwin(radar);
387 	wrefresh(radar);
388 	touchwin(planes);
389 	wrefresh(planes);
390 	touchwin(credit);
391 	wrefresh(credit);
392 
393 	/* refresh input last to get cursor in right place */
394 	touchwin(input);
395 	wrefresh(input);
396 	fflush(stdout);
397 }
398 
399 void
400 done_screen(void)
401 {
402 	clear();
403 	refresh();
404 	endwin();	  /* clean up curses */
405 }
406