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