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