1 1.16 rillig /* $NetBSD: fly.c,v 1.16 2021/05/02 12:50:43 rillig 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.16 rillig __RCSID("$NetBSD: fly.c,v 1.16 2021/05/02 12:50:43 rillig 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.15 dholland #include <assert.h> 45 1.1 cgd 46 1.1 cgd #define MIDR (LINES/2 - 1) 47 1.1 cgd #define MIDC (COLS/2 - 1) 48 1.1 cgd 49 1.9 jsm static int row, column; 50 1.9 jsm static int dr = 0, dc = 0; 51 1.9 jsm static char destroyed; 52 1.6 lukem int ourclock = 120; /* time for all the flights in the game */ 53 1.9 jsm static char cross = 0; 54 1.9 jsm static sig_t oldsig; 55 1.1 cgd 56 1.12 jsm static void blast(void); 57 1.12 jsm static void endfly(void); 58 1.12 jsm static void moveenemy(int); 59 1.12 jsm static void notarget(void); 60 1.12 jsm static void screen(void); 61 1.12 jsm static void succumb(int); 62 1.12 jsm static void target(void); 63 1.9 jsm 64 1.9 jsm static void 65 1.14 perry succumb(int dummy __unused) 66 1.1 cgd { 67 1.1 cgd if (oldsig == SIG_DFL) { 68 1.1 cgd endfly(); 69 1.1 cgd exit(1); 70 1.1 cgd } 71 1.1 cgd if (oldsig != SIG_IGN) { 72 1.1 cgd endfly(); 73 1.6 lukem (*oldsig) (SIGINT); 74 1.1 cgd } 75 1.1 cgd } 76 1.1 cgd 77 1.5 lukem int 78 1.13 jmc visual(void) 79 1.1 cgd { 80 1.1 cgd destroyed = 0; 81 1.7 simonb if (initscr() == NULL) { 82 1.1 cgd puts("Whoops! No more memory..."); 83 1.6 lukem return (0); 84 1.1 cgd } 85 1.1 cgd oldsig = signal(SIGINT, succumb); 86 1.10 blymn cbreak(); 87 1.1 cgd noecho(); 88 1.1 cgd screen(); 89 1.6 lukem row = rnd(LINES - 3) + 1; 90 1.6 lukem column = rnd(COLS - 2) + 1; 91 1.5 lukem moveenemy(0); 92 1.1 cgd for (;;) { 93 1.6 lukem switch (getchar()) { 94 1.1 cgd 95 1.6 lukem case 'h': 96 1.6 lukem case 'r': 97 1.6 lukem dc = -1; 98 1.6 lukem fuel--; 99 1.6 lukem break; 100 1.6 lukem 101 1.6 lukem case 'H': 102 1.6 lukem case 'R': 103 1.6 lukem dc = -5; 104 1.6 lukem fuel -= 10; 105 1.6 lukem break; 106 1.6 lukem 107 1.6 lukem case 'l': 108 1.6 lukem dc = 1; 109 1.6 lukem fuel--; 110 1.6 lukem break; 111 1.6 lukem 112 1.6 lukem case 'L': 113 1.6 lukem dc = 5; 114 1.6 lukem fuel -= 10; 115 1.6 lukem break; 116 1.6 lukem 117 1.6 lukem case 'j': 118 1.6 lukem case 'u': 119 1.6 lukem dr = 1; 120 1.6 lukem fuel--; 121 1.6 lukem break; 122 1.6 lukem 123 1.6 lukem case 'J': 124 1.6 lukem case 'U': 125 1.6 lukem dr = 5; 126 1.6 lukem fuel -= 10; 127 1.6 lukem break; 128 1.6 lukem 129 1.6 lukem case 'k': 130 1.6 lukem case 'd': 131 1.6 lukem dr = -1; 132 1.6 lukem fuel--; 133 1.6 lukem break; 134 1.6 lukem 135 1.6 lukem case 'K': 136 1.6 lukem case 'D': 137 1.6 lukem dr = -5; 138 1.6 lukem fuel -= 10; 139 1.6 lukem break; 140 1.6 lukem 141 1.6 lukem case '+': 142 1.6 lukem if (cross) { 143 1.6 lukem cross = 0; 144 1.6 lukem notarget(); 145 1.6 lukem } else 146 1.6 lukem cross = 1; 147 1.6 lukem break; 148 1.6 lukem 149 1.6 lukem case ' ': 150 1.6 lukem case 'f': 151 1.6 lukem if (torps) { 152 1.6 lukem torps -= 2; 153 1.6 lukem blast(); 154 1.16 rillig if (row == MIDR && column < MIDC + 2 && 155 1.15 dholland column > MIDC - 2) { 156 1.6 lukem destroyed = 1; 157 1.6 lukem alarm(0); 158 1.1 cgd } 159 1.6 lukem } else 160 1.6 lukem mvaddstr(0, 0, "*** Out of torpedoes. ***"); 161 1.6 lukem break; 162 1.6 lukem 163 1.6 lukem case 'q': 164 1.6 lukem endfly(); 165 1.6 lukem return (0); 166 1.6 lukem 167 1.6 lukem default: 168 1.6 lukem mvaddstr(0, 26, "Commands = r,R,l,L,u,U,d,D,f,+,q"); 169 1.6 lukem continue; 170 1.1 cgd 171 1.6 lukem case EOF: 172 1.6 lukem break; 173 1.1 cgd } 174 1.6 lukem if (destroyed) { 175 1.1 cgd endfly(); 176 1.6 lukem return (1); 177 1.1 cgd } 178 1.6 lukem if (ourclock <= 0) { 179 1.1 cgd endfly(); 180 1.1 cgd die(); 181 1.1 cgd } 182 1.1 cgd } 183 1.1 cgd } 184 1.1 cgd 185 1.9 jsm static void 186 1.13 jmc screen(void) 187 1.1 cgd { 188 1.6 lukem int r, c, n; 189 1.6 lukem int i; 190 1.1 cgd 191 1.1 cgd clear(); 192 1.1 cgd i = rnd(100); 193 1.6 lukem for (n = 0; n < i; n++) { 194 1.6 lukem r = rnd(LINES - 3) + 1; 195 1.1 cgd c = rnd(COLS); 196 1.1 cgd mvaddch(r, c, '.'); 197 1.1 cgd } 198 1.6 lukem mvaddstr(LINES - 1 - 1, 21, "TORPEDOES FUEL TIME"); 199 1.1 cgd refresh(); 200 1.1 cgd } 201 1.1 cgd 202 1.9 jsm static void 203 1.13 jmc target(void) 204 1.1 cgd { 205 1.6 lukem int n; 206 1.1 cgd 207 1.6 lukem move(MIDR, MIDC - 10); 208 1.1 cgd addstr("------- + -------"); 209 1.6 lukem for (n = MIDR - 4; n < MIDR - 1; n++) { 210 1.6 lukem mvaddch(n, MIDC, '|'); 211 1.6 lukem mvaddch(n + 6, MIDC, '|'); 212 1.1 cgd } 213 1.1 cgd } 214 1.1 cgd 215 1.9 jsm static void 216 1.13 jmc notarget(void) 217 1.1 cgd { 218 1.6 lukem int n; 219 1.1 cgd 220 1.6 lukem move(MIDR, MIDC - 10); 221 1.1 cgd addstr(" "); 222 1.6 lukem for (n = MIDR - 4; n < MIDR - 1; n++) { 223 1.6 lukem mvaddch(n, MIDC, ' '); 224 1.6 lukem mvaddch(n + 6, MIDC, ' '); 225 1.1 cgd } 226 1.1 cgd } 227 1.1 cgd 228 1.9 jsm static void 229 1.13 jmc blast(void) 230 1.1 cgd { 231 1.6 lukem int n; 232 1.1 cgd 233 1.1 cgd alarm(0); 234 1.6 lukem move(LINES - 1, 24); 235 1.1 cgd printw("%3d", torps); 236 1.6 lukem for (n = LINES - 1 - 2; n >= MIDR + 1; n--) { 237 1.6 lukem mvaddch(n, MIDC + MIDR - n, '/'); 238 1.6 lukem mvaddch(n, MIDC - MIDR + n, '\\'); 239 1.1 cgd refresh(); 240 1.1 cgd } 241 1.6 lukem mvaddch(MIDR, MIDC, '*'); 242 1.6 lukem for (n = LINES - 1 - 2; n >= MIDR + 1; n--) { 243 1.6 lukem mvaddch(n, MIDC + MIDR - n, ' '); 244 1.6 lukem mvaddch(n, MIDC - MIDR + n, ' '); 245 1.1 cgd refresh(); 246 1.1 cgd } 247 1.1 cgd alarm(1); 248 1.1 cgd } 249 1.1 cgd 250 1.9 jsm static void 251 1.14 perry moveenemy(int dummy __unused) 252 1.1 cgd { 253 1.6 lukem double d; 254 1.6 lukem int oldr, oldc; 255 1.1 cgd 256 1.1 cgd oldr = row; 257 1.1 cgd oldc = column; 258 1.6 lukem if (fuel > 0) { 259 1.6 lukem if (row + dr <= LINES - 3 && row + dr > 0) 260 1.1 cgd row += dr; 261 1.6 lukem if (column + dc < COLS - 1 && column + dc > 0) 262 1.1 cgd column += dc; 263 1.6 lukem } else 264 1.6 lukem if (fuel < 0) { 265 1.6 lukem fuel = 0; 266 1.6 lukem mvaddstr(0, 60, "*** Out of fuel ***"); 267 1.6 lukem } 268 1.16 rillig d = (double) ((row - MIDR) * (row - MIDR) + (column - MIDC) * 269 1.13 jmc (column - MIDC)); 270 1.6 lukem if (d < 16) { 271 1.1 cgd row += (rnd(9) - 4) % (4 - abs(row - MIDR)); 272 1.1 cgd column += (rnd(9) - 4) % (4 - abs(column - MIDC)); 273 1.1 cgd } 274 1.5 lukem ourclock--; 275 1.1 cgd mvaddstr(oldr, oldc - 1, " "); 276 1.1 cgd if (cross) 277 1.1 cgd target(); 278 1.1 cgd mvaddstr(row, column - 1, "/-\\"); 279 1.6 lukem move(LINES - 1, 24); 280 1.1 cgd printw("%3d", torps); 281 1.6 lukem move(LINES - 1, 42); 282 1.1 cgd printw("%3d", fuel); 283 1.6 lukem move(LINES - 1, 57); 284 1.5 lukem printw("%3d", ourclock); 285 1.1 cgd refresh(); 286 1.1 cgd signal(SIGALRM, moveenemy); 287 1.1 cgd alarm(1); 288 1.1 cgd } 289 1.1 cgd 290 1.9 jsm static void 291 1.13 jmc endfly(void) 292 1.1 cgd { 293 1.1 cgd alarm(0); 294 1.1 cgd signal(SIGALRM, SIG_DFL); 295 1.6 lukem mvcur(0, COLS - 1, LINES - 1, 0); 296 1.1 cgd endwin(); 297 1.1 cgd signal(SIGTSTP, SIG_DFL); 298 1.1 cgd signal(SIGINT, oldsig); 299 1.1 cgd } 300