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