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