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.4 (Berkeley) 06/01/90"; 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 succumb() 28 { 29 if (oldsig == SIG_DFL) { 30 endfly(); 31 exit(1); 32 } 33 if (oldsig != SIG_IGN) { 34 endfly(); 35 (*oldsig)(); 36 } 37 } 38 39 visual() 40 { 41 int moveenemy(); 42 43 destroyed = 0; 44 savetty(); 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 moveenemy() 212 { 213 double d; 214 int oldr, oldc; 215 216 oldr = row; 217 oldc = column; 218 if (fuel > 0){ 219 if (row + dr <= LINES-3 && row + dr > 0) 220 row += dr; 221 if (column + dc < COLS-1 && column + dc > 0) 222 column += dc; 223 } else if (fuel < 0){ 224 fuel = 0; 225 mvaddstr(0,60,"*** Out of fuel ***"); 226 } 227 d = (double) ((row - MIDR)*(row - MIDR) + (column - MIDC)*(column - MIDC)); 228 if (d < 16){ 229 row += (rnd(9) - 4) % (4 - abs(row - MIDR)); 230 column += (rnd(9) - 4) % (4 - abs(column - MIDC)); 231 } 232 clock--; 233 mvaddstr(oldr, oldc - 1, " "); 234 if (cross) 235 target(); 236 mvaddstr(row, column - 1, "/-\\"); 237 move(LINES-1, 24); 238 printw("%3d", torps); 239 move(LINES-1, 42); 240 printw("%3d", fuel); 241 move(LINES-1, 57); 242 printw("%3d", clock); 243 refresh(); 244 signal(SIGALRM, moveenemy); 245 alarm(1); 246 } 247 248 endfly() 249 { 250 alarm(0); 251 signal(SIGALRM, SIG_DFL); 252 mvcur(0,COLS-1,LINES-1,0); 253 endwin(); 254 signal(SIGTSTP, SIG_DFL); 255 signal(SIGINT, oldsig); 256 } 257