fly.c revision 1.10 1 1.10 blymn /* $NetBSD: fly.c,v 1.10 2001/12/04 13:00:24 blymn Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1983, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.5 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.3 cgd #if 0
39 1.4 tls static char sccsid[] = "@(#)fly.c 8.2 (Berkeley) 4/28/95";
40 1.3 cgd #else
41 1.10 blymn __RCSID("$NetBSD: fly.c,v 1.10 2001/12/04 13:00:24 blymn Exp $");
42 1.3 cgd #endif
43 1.6 lukem #endif /* not lint */
44 1.1 cgd
45 1.4 tls #include "extern.h"
46 1.1 cgd #undef UP
47 1.1 cgd #include <curses.h>
48 1.1 cgd
49 1.1 cgd #define MIDR (LINES/2 - 1)
50 1.1 cgd #define MIDC (COLS/2 - 1)
51 1.1 cgd
52 1.9 jsm static int row, column;
53 1.9 jsm static int dr = 0, dc = 0;
54 1.9 jsm static char destroyed;
55 1.6 lukem int ourclock = 120; /* time for all the flights in the game */
56 1.9 jsm static char cross = 0;
57 1.9 jsm static sig_t oldsig;
58 1.1 cgd
59 1.9 jsm static void blast __P((void));
60 1.9 jsm static void endfly __P((void));
61 1.9 jsm static void moveenemy __P((int));
62 1.9 jsm static void notarget __P((void));
63 1.9 jsm static void screen __P((void));
64 1.9 jsm static void succumb __P((int));
65 1.9 jsm static void target __P((void));
66 1.9 jsm
67 1.9 jsm static void
68 1.5 lukem succumb(dummy)
69 1.8 jsm int dummy __attribute__((__unused__));
70 1.1 cgd {
71 1.1 cgd if (oldsig == SIG_DFL) {
72 1.1 cgd endfly();
73 1.1 cgd exit(1);
74 1.1 cgd }
75 1.1 cgd if (oldsig != SIG_IGN) {
76 1.1 cgd endfly();
77 1.6 lukem (*oldsig) (SIGINT);
78 1.1 cgd }
79 1.1 cgd }
80 1.1 cgd
81 1.5 lukem int
82 1.1 cgd visual()
83 1.1 cgd {
84 1.1 cgd destroyed = 0;
85 1.7 simonb if (initscr() == NULL) {
86 1.1 cgd puts("Whoops! No more memory...");
87 1.6 lukem return (0);
88 1.1 cgd }
89 1.1 cgd oldsig = signal(SIGINT, succumb);
90 1.10 blymn cbreak();
91 1.1 cgd noecho();
92 1.1 cgd screen();
93 1.6 lukem row = rnd(LINES - 3) + 1;
94 1.6 lukem column = rnd(COLS - 2) + 1;
95 1.5 lukem moveenemy(0);
96 1.1 cgd for (;;) {
97 1.6 lukem switch (getchar()) {
98 1.1 cgd
99 1.6 lukem case 'h':
100 1.6 lukem case 'r':
101 1.6 lukem dc = -1;
102 1.6 lukem fuel--;
103 1.6 lukem break;
104 1.6 lukem
105 1.6 lukem case 'H':
106 1.6 lukem case 'R':
107 1.6 lukem dc = -5;
108 1.6 lukem fuel -= 10;
109 1.6 lukem break;
110 1.6 lukem
111 1.6 lukem case 'l':
112 1.6 lukem dc = 1;
113 1.6 lukem fuel--;
114 1.6 lukem break;
115 1.6 lukem
116 1.6 lukem case 'L':
117 1.6 lukem dc = 5;
118 1.6 lukem fuel -= 10;
119 1.6 lukem break;
120 1.6 lukem
121 1.6 lukem case 'j':
122 1.6 lukem case 'u':
123 1.6 lukem dr = 1;
124 1.6 lukem fuel--;
125 1.6 lukem break;
126 1.6 lukem
127 1.6 lukem case 'J':
128 1.6 lukem case 'U':
129 1.6 lukem dr = 5;
130 1.6 lukem fuel -= 10;
131 1.6 lukem break;
132 1.6 lukem
133 1.6 lukem case 'k':
134 1.6 lukem case 'd':
135 1.6 lukem dr = -1;
136 1.6 lukem fuel--;
137 1.6 lukem break;
138 1.6 lukem
139 1.6 lukem case 'K':
140 1.6 lukem case 'D':
141 1.6 lukem dr = -5;
142 1.6 lukem fuel -= 10;
143 1.6 lukem break;
144 1.6 lukem
145 1.6 lukem case '+':
146 1.6 lukem if (cross) {
147 1.6 lukem cross = 0;
148 1.6 lukem notarget();
149 1.6 lukem } else
150 1.6 lukem cross = 1;
151 1.6 lukem break;
152 1.6 lukem
153 1.6 lukem case ' ':
154 1.6 lukem case 'f':
155 1.6 lukem if (torps) {
156 1.6 lukem torps -= 2;
157 1.6 lukem blast();
158 1.6 lukem if (row == MIDR && column - MIDC < 2 && MIDC - column < 2) {
159 1.6 lukem destroyed = 1;
160 1.6 lukem alarm(0);
161 1.1 cgd }
162 1.6 lukem } else
163 1.6 lukem mvaddstr(0, 0, "*** Out of torpedoes. ***");
164 1.6 lukem break;
165 1.6 lukem
166 1.6 lukem case 'q':
167 1.6 lukem endfly();
168 1.6 lukem return (0);
169 1.6 lukem
170 1.6 lukem default:
171 1.6 lukem mvaddstr(0, 26, "Commands = r,R,l,L,u,U,d,D,f,+,q");
172 1.6 lukem continue;
173 1.1 cgd
174 1.6 lukem case EOF:
175 1.6 lukem break;
176 1.1 cgd }
177 1.6 lukem if (destroyed) {
178 1.1 cgd endfly();
179 1.6 lukem return (1);
180 1.1 cgd }
181 1.6 lukem if (ourclock <= 0) {
182 1.1 cgd endfly();
183 1.1 cgd die();
184 1.1 cgd }
185 1.1 cgd }
186 1.1 cgd }
187 1.1 cgd
188 1.9 jsm static void
189 1.1 cgd screen()
190 1.1 cgd {
191 1.6 lukem int r, c, n;
192 1.6 lukem int i;
193 1.1 cgd
194 1.1 cgd clear();
195 1.1 cgd i = rnd(100);
196 1.6 lukem for (n = 0; n < i; n++) {
197 1.6 lukem r = rnd(LINES - 3) + 1;
198 1.1 cgd c = rnd(COLS);
199 1.1 cgd mvaddch(r, c, '.');
200 1.1 cgd }
201 1.6 lukem mvaddstr(LINES - 1 - 1, 21, "TORPEDOES FUEL TIME");
202 1.1 cgd refresh();
203 1.1 cgd }
204 1.1 cgd
205 1.9 jsm static void
206 1.1 cgd target()
207 1.1 cgd {
208 1.6 lukem int n;
209 1.1 cgd
210 1.6 lukem move(MIDR, MIDC - 10);
211 1.1 cgd addstr("------- + -------");
212 1.6 lukem for (n = MIDR - 4; n < MIDR - 1; n++) {
213 1.6 lukem mvaddch(n, MIDC, '|');
214 1.6 lukem mvaddch(n + 6, MIDC, '|');
215 1.1 cgd }
216 1.1 cgd }
217 1.1 cgd
218 1.9 jsm static void
219 1.1 cgd notarget()
220 1.1 cgd {
221 1.6 lukem int n;
222 1.1 cgd
223 1.6 lukem move(MIDR, MIDC - 10);
224 1.1 cgd addstr(" ");
225 1.6 lukem for (n = MIDR - 4; n < MIDR - 1; n++) {
226 1.6 lukem mvaddch(n, MIDC, ' ');
227 1.6 lukem mvaddch(n + 6, MIDC, ' ');
228 1.1 cgd }
229 1.1 cgd }
230 1.1 cgd
231 1.9 jsm static void
232 1.1 cgd blast()
233 1.1 cgd {
234 1.6 lukem int n;
235 1.1 cgd
236 1.1 cgd alarm(0);
237 1.6 lukem move(LINES - 1, 24);
238 1.1 cgd printw("%3d", torps);
239 1.6 lukem for (n = LINES - 1 - 2; n >= MIDR + 1; n--) {
240 1.6 lukem mvaddch(n, MIDC + MIDR - n, '/');
241 1.6 lukem mvaddch(n, MIDC - MIDR + n, '\\');
242 1.1 cgd refresh();
243 1.1 cgd }
244 1.6 lukem mvaddch(MIDR, MIDC, '*');
245 1.6 lukem for (n = LINES - 1 - 2; n >= MIDR + 1; n--) {
246 1.6 lukem mvaddch(n, MIDC + MIDR - n, ' ');
247 1.6 lukem mvaddch(n, MIDC - MIDR + n, ' ');
248 1.1 cgd refresh();
249 1.1 cgd }
250 1.1 cgd alarm(1);
251 1.1 cgd }
252 1.1 cgd
253 1.9 jsm static void
254 1.5 lukem moveenemy(dummy)
255 1.8 jsm int dummy __attribute__((__unused__));
256 1.1 cgd {
257 1.6 lukem double d;
258 1.6 lukem int oldr, oldc;
259 1.1 cgd
260 1.1 cgd oldr = row;
261 1.1 cgd oldc = column;
262 1.6 lukem if (fuel > 0) {
263 1.6 lukem if (row + dr <= LINES - 3 && row + dr > 0)
264 1.1 cgd row += dr;
265 1.6 lukem if (column + dc < COLS - 1 && column + dc > 0)
266 1.1 cgd column += dc;
267 1.6 lukem } else
268 1.6 lukem if (fuel < 0) {
269 1.6 lukem fuel = 0;
270 1.6 lukem mvaddstr(0, 60, "*** Out of fuel ***");
271 1.6 lukem }
272 1.6 lukem d = (double) ((row - MIDR) * (row - MIDR) + (column - MIDC) * (column - MIDC));
273 1.6 lukem if (d < 16) {
274 1.1 cgd row += (rnd(9) - 4) % (4 - abs(row - MIDR));
275 1.1 cgd column += (rnd(9) - 4) % (4 - abs(column - MIDC));
276 1.1 cgd }
277 1.5 lukem ourclock--;
278 1.1 cgd mvaddstr(oldr, oldc - 1, " ");
279 1.1 cgd if (cross)
280 1.1 cgd target();
281 1.1 cgd mvaddstr(row, column - 1, "/-\\");
282 1.6 lukem move(LINES - 1, 24);
283 1.1 cgd printw("%3d", torps);
284 1.6 lukem move(LINES - 1, 42);
285 1.1 cgd printw("%3d", fuel);
286 1.6 lukem move(LINES - 1, 57);
287 1.5 lukem printw("%3d", ourclock);
288 1.1 cgd refresh();
289 1.1 cgd signal(SIGALRM, moveenemy);
290 1.1 cgd alarm(1);
291 1.1 cgd }
292 1.1 cgd
293 1.9 jsm static void
294 1.1 cgd endfly()
295 1.1 cgd {
296 1.1 cgd alarm(0);
297 1.1 cgd signal(SIGALRM, SIG_DFL);
298 1.6 lukem mvcur(0, COLS - 1, LINES - 1, 0);
299 1.1 cgd endwin();
300 1.1 cgd signal(SIGTSTP, SIG_DFL);
301 1.1 cgd signal(SIGINT, oldsig);
302 1.1 cgd }
303