fly.c revision 1.5 1 /* $NetBSD: fly.c,v 1.5 1997/10/10 11:39:50 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)fly.c 8.2 (Berkeley) 4/28/95";
40 #else
41 __RCSID("$NetBSD: fly.c,v 1.5 1997/10/10 11:39:50 lukem Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include "extern.h"
46 #undef UP
47 #include <curses.h>
48
49 #define abs(a) ((a) < 0 ? -(a) : (a))
50 #define MIDR (LINES/2 - 1)
51 #define MIDC (COLS/2 - 1)
52
53 int row, column;
54 int dr = 0, dc = 0;
55 char destroyed;
56 int ourclock = 120; /* time for all the flights in the game */
57 char cross = 0;
58 sig_t oldsig;
59
60 void
61 succumb(dummy)
62 int dummy;
63 {
64 if (oldsig == SIG_DFL) {
65 endfly();
66 exit(1);
67 }
68 if (oldsig != SIG_IGN) {
69 endfly();
70 (*oldsig)(SIGINT);
71 }
72 }
73
74 int
75 visual()
76 {
77 destroyed = 0;
78 if(initscr() == ERR){
79 puts("Whoops! No more memory...");
80 return(0);
81 }
82 oldsig = signal(SIGINT, succumb);
83 crmode();
84 noecho();
85 screen();
86 row = rnd(LINES-3) + 1;
87 column = rnd(COLS-2) + 1;
88 moveenemy(0);
89 for (;;) {
90 switch(getchar()){
91
92 case 'h':
93 case 'r':
94 dc = -1;
95 fuel--;
96 break;
97
98 case 'H':
99 case 'R':
100 dc = -5;
101 fuel -= 10;
102 break;
103
104 case 'l':
105 dc = 1;
106 fuel--;
107 break;
108
109 case 'L':
110 dc = 5;
111 fuel -= 10;
112 break;
113
114 case 'j':
115 case 'u':
116 dr = 1;
117 fuel--;
118 break;
119
120 case 'J':
121 case 'U':
122 dr = 5;
123 fuel -= 10;
124 break;
125
126 case 'k':
127 case 'd':
128 dr = -1;
129 fuel--;
130 break;
131
132 case 'K':
133 case 'D':
134 dr = -5;
135 fuel -= 10;
136 break;
137
138 case '+':
139 if (cross){
140 cross = 0;
141 notarget();
142 }
143 else
144 cross = 1;
145 break;
146
147 case ' ':
148 case 'f':
149 if (torps){
150 torps -= 2;
151 blast();
152 if (row == MIDR && column - MIDC < 2 && MIDC - column < 2){
153 destroyed = 1;
154 alarm(0);
155 }
156 }
157 else
158 mvaddstr(0,0,"*** Out of torpedoes. ***");
159 break;
160
161 case 'q':
162 endfly();
163 return(0);
164
165 default:
166 mvaddstr(0,26,"Commands = r,R,l,L,u,U,d,D,f,+,q");
167 continue;
168
169 case EOF:
170 break;
171 }
172 if (destroyed){
173 endfly();
174 return(1);
175 }
176 if (ourclock <= 0){
177 endfly();
178 die();
179 }
180 }
181 }
182
183 void
184 screen()
185 {
186 int r,c,n;
187 int i;
188
189 clear();
190 i = rnd(100);
191 for (n=0; n < i; n++){
192 r = rnd(LINES-3) + 1;
193 c = rnd(COLS);
194 mvaddch(r, c, '.');
195 }
196 mvaddstr(LINES-1-1,21,"TORPEDOES FUEL TIME");
197 refresh();
198 }
199
200 void
201 target()
202 {
203 int n;
204
205 move(MIDR,MIDC-10);
206 addstr("------- + -------");
207 for (n = MIDR-4; n < MIDR-1; n++){
208 mvaddch(n,MIDC,'|');
209 mvaddch(n+6,MIDC,'|');
210 }
211 }
212
213 void
214 notarget()
215 {
216 int n;
217
218 move(MIDR,MIDC-10);
219 addstr(" ");
220 for (n = MIDR-4; n < MIDR-1; n++){
221 mvaddch(n,MIDC,' ');
222 mvaddch(n+6,MIDC,' ');
223 }
224 }
225
226 void
227 blast()
228 {
229 int n;
230
231 alarm(0);
232 move(LINES-1, 24);
233 printw("%3d", torps);
234 for(n = LINES-1-2; n >= MIDR + 1; n--){
235 mvaddch(n, MIDC+MIDR-n, '/');
236 mvaddch(n, MIDC-MIDR+n, '\\');
237 refresh();
238 }
239 mvaddch(MIDR,MIDC,'*');
240 for(n = LINES-1-2; n >= MIDR + 1; n--){
241 mvaddch(n, MIDC+MIDR-n, ' ');
242 mvaddch(n, MIDC-MIDR+n, ' ');
243 refresh();
244 }
245 alarm(1);
246 }
247
248 void
249 moveenemy(dummy)
250 int dummy;
251 {
252 double d;
253 int oldr, oldc;
254
255 oldr = row;
256 oldc = column;
257 if (fuel > 0){
258 if (row + dr <= LINES-3 && row + dr > 0)
259 row += dr;
260 if (column + dc < COLS-1 && column + dc > 0)
261 column += dc;
262 } else if (fuel < 0){
263 fuel = 0;
264 mvaddstr(0,60,"*** Out of fuel ***");
265 }
266 d = (double) ((row - MIDR)*(row - MIDR) + (column - MIDC)*(column - MIDC));
267 if (d < 16){
268 row += (rnd(9) - 4) % (4 - abs(row - MIDR));
269 column += (rnd(9) - 4) % (4 - abs(column - MIDC));
270 }
271 ourclock--;
272 mvaddstr(oldr, oldc - 1, " ");
273 if (cross)
274 target();
275 mvaddstr(row, column - 1, "/-\\");
276 move(LINES-1, 24);
277 printw("%3d", torps);
278 move(LINES-1, 42);
279 printw("%3d", fuel);
280 move(LINES-1, 57);
281 printw("%3d", ourclock);
282 refresh();
283 signal(SIGALRM, moveenemy);
284 alarm(1);
285 }
286
287 void
288 endfly()
289 {
290 alarm(0);
291 signal(SIGALRM, SIG_DFL);
292 mvcur(0,COLS-1,LINES-1,0);
293 endwin();
294 signal(SIGTSTP, SIG_DFL);
295 signal(SIGINT, oldsig);
296 }
297