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