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