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