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