add sanity checks for old file systems
[unix-history] / usr / src / games / battlestar / fly.c
CommitLineData
fdc7d56f
EW
1/*
2 * Copyright (c) 1983 Regents of the University of California,
3 * All rights reserved. Redistribution permitted subject to
4 * the terms of the Berkeley Software License Agreement.
5 */
6
77eecccb 7#ifndef lint
fdc7d56f 8static char sccsid[] = "@(#)fly.c 1.3 %G%";
77eecccb
EW
9#endif
10
11#include "externs.h"
12#undef UP
13#include <curses.h>
14
15#define abs(a) ((a) < 0 ? -(a) : (a))
16#define MIDR (LINES/2 - 1)
17#define MIDC (COLS/2 - 1)
18
19int row, column;
20int dr = 0, dc = 0;
21char destroyed;
22int clock = 120; /* time for all the flights in the game */
23char cross = 0;
24int (*oldsig)();
25
26succumb()
27{
a7c71d1e
EW
28 switch (oldsig) {
29 case SIG_DFL:
30 endfly();
31 exit(1);
32 case SIG_IGN:
33 break;
34 default:
35 endfly();
36 (*oldsig)();
37 }
77eecccb
EW
38}
39
40visual()
41{
42 int 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
151screen()
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
167target()
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
179notarget()
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
191blast()
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
212moveenemy()
213{
214 double d;
215 int oldr, oldc;
216
217 oldr = row;
218 oldc = column;
219 if (fuel > 0){
220 if (row + dr <= LINES-3 && row + dr > 0)
221 row += dr;
222 if (column + dc < COLS-1 && column + dc > 0)
223 column += dc;
224 } else if (fuel < 0){
225 fuel = 0;
226 mvaddstr(0,60,"*** Out of fuel ***");
227 }
228 d = (double) ((row - MIDR)*(row - MIDR) + (column - MIDC)*(column - MIDC));
229 if (d < 16){
230 row += (rnd(9) - 4) % (4 - abs(row - MIDR));
231 column += (rnd(9) - 4) % (4 - abs(column - MIDC));
232 }
233 clock--;
234 mvaddstr(oldr, oldc - 1, " ");
235 if (cross)
236 target();
237 mvaddstr(row, column - 1, "/-\\");
238 move(LINES-1, 24);
239 printw("%3d", torps);
240 move(LINES-1, 42);
241 printw("%3d", fuel);
242 move(LINES-1, 57);
243 printw("%3d", clock);
244 refresh();
245 signal(SIGALRM, moveenemy);
246 alarm(1);
247}
248
249endfly()
250{
251 alarm(0);
252 signal(SIGALRM, SIG_DFL);
253 mvcur(0,COLS-1,LINES-1,0);
254 endwin();
255 signal(SIGTSTP, SIG_DFL);
256 signal(SIGINT, oldsig);
257}