|
|
1.1 ! root 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 are permitted ! 6: * provided that: (1) source distributions retain this entire copyright ! 7: * notice and comment, and (2) distributions including binaries display ! 8: * the following acknowledgement: ``This product includes software ! 9: * developed by the University of California, Berkeley and its contributors'' ! 10: * in the documentation or other materials provided with the distribution ! 11: * and in all advertising materials mentioning features or use of this ! 12: * software. Neither the name of the University nor the names of its ! 13: * contributors may be used to endorse or promote products derived ! 14: * from this software without specific prior written permission. ! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 18: */ ! 19: ! 20: #ifndef lint ! 21: static char sccsid[] = "@(#)fly.c 5.4 (Berkeley) 6/1/90"; ! 22: #endif /* not lint */ ! 23: ! 24: #include "externs.h" ! 25: #undef UP ! 26: #include <curses.h> ! 27: ! 28: #define abs(a) ((a) < 0 ? -(a) : (a)) ! 29: #define MIDR (LINES/2 - 1) ! 30: #define MIDC (COLS/2 - 1) ! 31: ! 32: int row, column; ! 33: int dr = 0, dc = 0; ! 34: char destroyed; ! 35: int clock = 120; /* time for all the flights in the game */ ! 36: char cross = 0; ! 37: sig_t oldsig; ! 38: ! 39: succumb() ! 40: { ! 41: if (oldsig == SIG_DFL) { ! 42: endfly(); ! 43: exit(1); ! 44: } ! 45: if (oldsig != SIG_IGN) { ! 46: endfly(); ! 47: (*oldsig)(); ! 48: } ! 49: } ! 50: ! 51: visual() ! 52: { ! 53: int moveenemy(); ! 54: ! 55: destroyed = 0; ! 56: savetty(); ! 57: if(initscr() == ERR){ ! 58: puts("Whoops! No more memory..."); ! 59: return(0); ! 60: } ! 61: oldsig = signal(SIGINT, succumb); ! 62: crmode(); ! 63: noecho(); ! 64: screen(); ! 65: row = rnd(LINES-3) + 1; ! 66: column = rnd(COLS-2) + 1; ! 67: moveenemy(); ! 68: for (;;) { ! 69: switch(getchar()){ ! 70: ! 71: case 'h': ! 72: case 'r': ! 73: dc = -1; ! 74: fuel--; ! 75: break; ! 76: ! 77: case 'H': ! 78: case 'R': ! 79: dc = -5; ! 80: fuel -= 10; ! 81: break; ! 82: ! 83: case 'l': ! 84: dc = 1; ! 85: fuel--; ! 86: break; ! 87: ! 88: case 'L': ! 89: dc = 5; ! 90: fuel -= 10; ! 91: break; ! 92: ! 93: case 'j': ! 94: case 'u': ! 95: dr = 1; ! 96: fuel--; ! 97: break; ! 98: ! 99: case 'J': ! 100: case 'U': ! 101: dr = 5; ! 102: fuel -= 10; ! 103: break; ! 104: ! 105: case 'k': ! 106: case 'd': ! 107: dr = -1; ! 108: fuel--; ! 109: break; ! 110: ! 111: case 'K': ! 112: case 'D': ! 113: dr = -5; ! 114: fuel -= 10; ! 115: break; ! 116: ! 117: case '+': ! 118: if (cross){ ! 119: cross = 0; ! 120: notarget(); ! 121: } ! 122: else ! 123: cross = 1; ! 124: break; ! 125: ! 126: case ' ': ! 127: case 'f': ! 128: if (torps){ ! 129: torps -= 2; ! 130: blast(); ! 131: if (row == MIDR && column - MIDC < 2 && MIDC - column < 2){ ! 132: destroyed = 1; ! 133: alarm(0); ! 134: } ! 135: } ! 136: else ! 137: mvaddstr(0,0,"*** Out of torpedoes. ***"); ! 138: break; ! 139: ! 140: case 'q': ! 141: endfly(); ! 142: return(0); ! 143: ! 144: default: ! 145: mvaddstr(0,26,"Commands = r,R,l,L,u,U,d,D,f,+,q"); ! 146: continue; ! 147: ! 148: case EOF: ! 149: break; ! 150: } ! 151: if (destroyed){ ! 152: endfly(); ! 153: return(1); ! 154: } ! 155: if (clock <= 0){ ! 156: endfly(); ! 157: die(); ! 158: } ! 159: } ! 160: } ! 161: ! 162: screen() ! 163: { ! 164: register int r,c,n; ! 165: int i; ! 166: ! 167: clear(); ! 168: i = rnd(100); ! 169: for (n=0; n < i; n++){ ! 170: r = rnd(LINES-3) + 1; ! 171: c = rnd(COLS); ! 172: mvaddch(r, c, '.'); ! 173: } ! 174: mvaddstr(LINES-1-1,21,"TORPEDOES FUEL TIME"); ! 175: refresh(); ! 176: } ! 177: ! 178: target() ! 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: notarget() ! 191: { ! 192: register int n; ! 193: ! 194: move(MIDR,MIDC-10); ! 195: addstr(" "); ! 196: for (n = MIDR-4; n < MIDR-1; n++){ ! 197: mvaddch(n,MIDC,' '); ! 198: mvaddch(n+6,MIDC,' '); ! 199: } ! 200: } ! 201: ! 202: blast() ! 203: { ! 204: register int n; ! 205: ! 206: alarm(0); ! 207: move(LINES-1, 24); ! 208: printw("%3d", torps); ! 209: for(n = LINES-1-2; n >= MIDR + 1; n--){ ! 210: mvaddch(n, MIDC+MIDR-n, '/'); ! 211: mvaddch(n, MIDC-MIDR+n, '\\'); ! 212: refresh(); ! 213: } ! 214: mvaddch(MIDR,MIDC,'*'); ! 215: for(n = LINES-1-2; n >= MIDR + 1; n--){ ! 216: mvaddch(n, MIDC+MIDR-n, ' '); ! 217: mvaddch(n, MIDC-MIDR+n, ' '); ! 218: refresh(); ! 219: } ! 220: alarm(1); ! 221: } ! 222: ! 223: moveenemy() ! 224: { ! 225: double d; ! 226: int oldr, oldc; ! 227: ! 228: oldr = row; ! 229: oldc = column; ! 230: if (fuel > 0){ ! 231: if (row + dr <= LINES-3 && row + dr > 0) ! 232: row += dr; ! 233: if (column + dc < COLS-1 && column + dc > 0) ! 234: column += dc; ! 235: } else if (fuel < 0){ ! 236: fuel = 0; ! 237: mvaddstr(0,60,"*** Out of fuel ***"); ! 238: } ! 239: d = (double) ((row - MIDR)*(row - MIDR) + (column - MIDC)*(column - MIDC)); ! 240: if (d < 16){ ! 241: row += (rnd(9) - 4) % (4 - abs(row - MIDR)); ! 242: column += (rnd(9) - 4) % (4 - abs(column - MIDC)); ! 243: } ! 244: clock--; ! 245: mvaddstr(oldr, oldc - 1, " "); ! 246: if (cross) ! 247: target(); ! 248: mvaddstr(row, column - 1, "/-\\"); ! 249: move(LINES-1, 24); ! 250: printw("%3d", torps); ! 251: move(LINES-1, 42); ! 252: printw("%3d", fuel); ! 253: move(LINES-1, 57); ! 254: printw("%3d", clock); ! 255: refresh(); ! 256: signal(SIGALRM, moveenemy); ! 257: alarm(1); ! 258: } ! 259: ! 260: endfly() ! 261: { ! 262: alarm(0); ! 263: signal(SIGALRM, SIG_DFL); ! 264: mvcur(0,COLS-1,LINES-1,0); ! 265: endwin(); ! 266: signal(SIGTSTP, SIG_DFL); ! 267: signal(SIGINT, oldsig); ! 268: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.