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