1 /* $NetBSD: fly.c,v 1.6 1997/10/11 02:07:20 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.6 1997/10/11 02:07:20 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 } else 143 cross = 1; 144 break; 145 146 case ' ': 147 case 'f': 148 if (torps) { 149 torps -= 2; 150 blast(); 151 if (row == MIDR && column - MIDC < 2 && MIDC - column < 2) { 152 destroyed = 1; 153 alarm(0); 154 } 155 } else 156 mvaddstr(0, 0, "*** Out of torpedoes. ***"); 157 break; 158 159 case 'q': 160 endfly(); 161 return (0); 162 163 default: 164 mvaddstr(0, 26, "Commands = r,R,l,L,u,U,d,D,f,+,q"); 165 continue; 166 167 case EOF: 168 break; 169 } 170 if (destroyed) { 171 endfly(); 172 return (1); 173 } 174 if (ourclock <= 0) { 175 endfly(); 176 die(); 177 } 178 } 179 } 180 181 void 182 screen() 183 { 184 int r, c, n; 185 int i; 186 187 clear(); 188 i = rnd(100); 189 for (n = 0; n < i; n++) { 190 r = rnd(LINES - 3) + 1; 191 c = rnd(COLS); 192 mvaddch(r, c, '.'); 193 } 194 mvaddstr(LINES - 1 - 1, 21, "TORPEDOES FUEL TIME"); 195 refresh(); 196 } 197 198 void 199 target() 200 { 201 int n; 202 203 move(MIDR, MIDC - 10); 204 addstr("------- + -------"); 205 for (n = MIDR - 4; n < MIDR - 1; n++) { 206 mvaddch(n, MIDC, '|'); 207 mvaddch(n + 6, MIDC, '|'); 208 } 209 } 210 211 void 212 notarget() 213 { 214 int n; 215 216 move(MIDR, MIDC - 10); 217 addstr(" "); 218 for (n = MIDR - 4; n < MIDR - 1; n++) { 219 mvaddch(n, MIDC, ' '); 220 mvaddch(n + 6, MIDC, ' '); 221 } 222 } 223 224 void 225 blast() 226 { 227 int n; 228 229 alarm(0); 230 move(LINES - 1, 24); 231 printw("%3d", torps); 232 for (n = LINES - 1 - 2; n >= MIDR + 1; n--) { 233 mvaddch(n, MIDC + MIDR - n, '/'); 234 mvaddch(n, MIDC - MIDR + n, '\\'); 235 refresh(); 236 } 237 mvaddch(MIDR, MIDC, '*'); 238 for (n = LINES - 1 - 2; n >= MIDR + 1; n--) { 239 mvaddch(n, MIDC + MIDR - n, ' '); 240 mvaddch(n, MIDC - MIDR + n, ' '); 241 refresh(); 242 } 243 alarm(1); 244 } 245 246 void 247 moveenemy(dummy) 248 int dummy; 249 { 250 double d; 251 int oldr, oldc; 252 253 oldr = row; 254 oldc = column; 255 if (fuel > 0) { 256 if (row + dr <= LINES - 3 && row + dr > 0) 257 row += dr; 258 if (column + dc < COLS - 1 && column + dc > 0) 259 column += dc; 260 } else 261 if (fuel < 0) { 262 fuel = 0; 263 mvaddstr(0, 60, "*** Out of fuel ***"); 264 } 265 d = (double) ((row - MIDR) * (row - MIDR) + (column - MIDC) * (column - MIDC)); 266 if (d < 16) { 267 row += (rnd(9) - 4) % (4 - abs(row - MIDR)); 268 column += (rnd(9) - 4) % (4 - abs(column - MIDC)); 269 } 270 ourclock--; 271 mvaddstr(oldr, oldc - 1, " "); 272 if (cross) 273 target(); 274 mvaddstr(row, column - 1, "/-\\"); 275 move(LINES - 1, 24); 276 printw("%3d", torps); 277 move(LINES - 1, 42); 278 printw("%3d", fuel); 279 move(LINES - 1, 57); 280 printw("%3d", ourclock); 281 refresh(); 282 signal(SIGALRM, moveenemy); 283 alarm(1); 284 } 285 286 void 287 endfly() 288 { 289 alarm(0); 290 signal(SIGALRM, SIG_DFL); 291 mvcur(0, COLS - 1, LINES - 1, 0); 292 endwin(); 293 signal(SIGTSTP, SIG_DFL); 294 signal(SIGINT, oldsig); 295 } 296