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