]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/update.c
1 /* $NetBSD: update.c,v 1.5 1997/01/13 06:50:27 tls Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
42 * Copy permission is hereby granted provided that this notice is
43 * retained on all partial or complete copies.
45 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
50 static char sccsid
[] = "@(#)update.c 8.1 (Berkeley) 5/31/93";
52 static char rcsid
[] = "$NetBSD: update.c,v 1.5 1997/01/13 06:50:27 tls Exp $";
60 int i
, dir_diff
, mask
, unclean
;
61 PLANE
*pp
, *p1
, *p2
, *p
;
65 signal(SIGALRM
, update
);
72 /* put some planes in the air */
75 for (pp
= ground
.head
; pp
!= NULL
; pp
= pp
->next
) {
76 if (pp
->new_altitude
> 0) {
85 /* do altitude change and basic movement */
86 for (pp
= air
.head
; pp
!= NULL
; pp
= pp
->next
) {
87 /* type 0 only move every other turn */
88 if (pp
->plane_type
== 0 && clck
& 1)
93 loser(pp
, "ran out of fuel.");
95 pp
->altitude
+= SGN(pp
->new_altitude
- pp
->altitude
);
98 dir_diff
= pp
->new_dir
- pp
->dir
;
100 * Allow for circle commands
102 if (pp
->new_dir
>= 0 && pp
->new_dir
< MAXDIR
) {
103 if (dir_diff
> MAXDIR
/2)
105 else if (dir_diff
< -(MAXDIR
/2))
110 else if (dir_diff
< -2)
113 if (pp
->dir
>= MAXDIR
)
115 else if (pp
->dir
< 0)
118 pp
->xpos
+= displacement
[pp
->dir
].dx
;
119 pp
->ypos
+= displacement
[pp
->dir
].dy
;
121 if (pp
->delayd
&& pp
->xpos
== sp
->beacon
[pp
->delayd_no
].x
&&
122 pp
->ypos
== sp
->beacon
[pp
->delayd_no
].y
) {
124 if (pp
->status
== S_UNMARKED
)
125 pp
->status
= S_MARKED
;
128 switch (pp
->dest_type
) {
130 if (pp
->xpos
== sp
->airport
[pp
->dest_no
].x
&&
131 pp
->ypos
== sp
->airport
[pp
->dest_no
].y
&&
133 if (pp
->dir
!= sp
->airport
[pp
->dest_no
].dir
)
134 loser(pp
, "landed in the wrong direction.");
142 if (pp
->xpos
== sp
->exit
[pp
->dest_no
].x
&&
143 pp
->ypos
== sp
->exit
[pp
->dest_no
].y
) {
144 if (pp
->altitude
!= 9)
145 loser(pp
, "exited at the wrong altitude.");
153 loser(pp
, "has a bizarre destination, get help!");
155 if (pp
->altitude
> 9)
156 /* "this is impossible" */
157 loser(pp
, "exceded flight ceiling.");
158 if (pp
->altitude
<= 0) {
159 for (i
= 0; i
< sp
->num_airports
; i
++)
160 if (pp
->xpos
== sp
->airport
[i
].x
&&
161 pp
->ypos
== sp
->airport
[i
].y
) {
162 if (pp
->dest_type
== T_AIRPORT
)
164 "landed at the wrong airport.");
167 "landed instead of exited.");
169 loser(pp
, "crashed on the ground.");
171 if (pp
->xpos
< 1 || pp
->xpos
>= sp
->width
- 1 ||
172 pp
->ypos
< 1 || pp
->ypos
>= sp
->height
- 1) {
173 for (i
= 0; i
< sp
->num_exits
; i
++)
174 if (pp
->xpos
== sp
->exit
[i
].x
&&
175 pp
->ypos
== sp
->exit
[i
].y
) {
176 if (pp
->dest_type
== T_EXIT
)
178 "exited via the wrong exit.");
181 "exited instead of landed.");
183 loser(pp
, "illegally left the flight arena.");
188 * Traverse the list once, deleting the planes that are gone.
190 for (pp
= air
.head
; pp
!= NULL
; pp
= p2
) {
192 if (pp
->status
== S_GONE
) {
200 for (p1
= air
.head
; p1
!= NULL
; p1
= p1
->next
)
201 for (p2
= p1
->next
; p2
!= NULL
; p2
= p2
->next
)
202 if (too_close(p1
, p2
, 1)) {
205 (void)sprintf(buf
, "collided with plane '%c'.",
210 * Check every other update. Actually, only add on even updates.
211 * Otherwise, prop jobs show up *on* entrance. Remember that
212 * we don't update props on odd updates.
214 if ((rand() % sp
->newplane_time
) == 0)
218 alarm(sp
->update_secs
);
226 static char buf
[50], *bp
, *comm_start
;
231 (void)sprintf(bp
, "%c%d%c%c%d: ", name(pp
), pp
->altitude
,
232 (pp
->fuel
< LOWFUEL
) ? '*' : ' ',
233 (pp
->dest_type
== T_AIRPORT
) ? 'A' : 'E', pp
->dest_no
);
235 comm_start
= bp
= index(buf
, '\0');
236 if (pp
->altitude
== 0)
237 (void)sprintf(bp
, "Holding @ A%d", pp
->orig_no
);
238 else if (pp
->new_dir
>= MAXDIR
|| pp
->new_dir
< 0)
239 strcpy(bp
, "Circle");
240 else if (pp
->new_dir
!= pp
->dir
)
241 (void)sprintf(bp
, "%d", dir_deg(pp
->new_dir
));
243 bp
= index(buf
, '\0');
245 (void)sprintf(bp
, " @ B%d", pp
->delayd_no
);
247 bp
= index(buf
, '\0');
248 if (*comm_start
== '\0' &&
249 (pp
->status
== S_UNMARKED
|| pp
->status
== S_IGNORED
))
250 strcpy(bp
, "---------");
258 if (p
->plane_type
== 0)
259 return ('A' + p
->plane_no
);
261 return ('a' + p
->plane_no
);
266 if (l
< 'a' && l
> 'z' && l
< 'A' && l
> 'Z')
268 else if (l
>= 'a' && l
<= 'z')
276 static int last_plane
= -1;
278 int found
, start_plane
= last_plane
;
283 if (last_plane
>= 26)
285 for (pp
= air
.head
; pp
!= NULL
; pp
= pp
->next
)
286 if (pp
->plane_no
== last_plane
) {
291 for (pp
= ground
.head
; pp
!= NULL
; pp
= pp
->next
)
292 if (pp
->plane_no
== last_plane
) {
296 } while (found
&& last_plane
!= start_plane
);
297 if (last_plane
== start_plane
)
305 int i
, num_starts
, close
, rnd
, rnd2
, pnum
;
307 memset(&p
, 0, sizeof (p
));
310 p
.plane_type
= random() % 2;
312 num_starts
= sp
->num_exits
+ sp
->num_airports
;
313 rnd
= random() % num_starts
;
315 if (rnd
< sp
->num_exits
) {
316 p
.dest_type
= T_EXIT
;
319 p
.dest_type
= T_AIRPORT
;
320 p
.dest_no
= rnd
- sp
->num_exits
;
323 /* loop until we get a plane not near another */
324 for (i
= 0; i
< num_starts
; i
++) {
325 /* loop till we get a different start point */
326 while ((rnd2
= random() % num_starts
) == rnd
)
328 if (rnd2
< sp
->num_exits
) {
329 p
.orig_type
= T_EXIT
;
331 p
.xpos
= sp
->exit
[rnd2
].x
;
332 p
.ypos
= sp
->exit
[rnd2
].y
;
333 p
.new_dir
= p
.dir
= sp
->exit
[rnd2
].dir
;
334 p
.altitude
= p
.new_altitude
= 7;
336 for (p1
= air
.head
; p1
!= NULL
; p1
= p1
->next
)
337 if (too_close(p1
, &p
, 4)) {
344 p
.orig_type
= T_AIRPORT
;
345 p
.orig_no
= rnd2
- sp
->num_exits
;
346 p
.xpos
= sp
->airport
[p
.orig_no
].x
;
347 p
.ypos
= sp
->airport
[p
.orig_no
].y
;
348 p
.new_dir
= p
.dir
= sp
->airport
[p
.orig_no
].dir
;
349 p
.altitude
= p
.new_altitude
= 0;
351 p
.fuel
= sp
->width
+ sp
->height
;
362 memcpy(pp
, &p
, sizeof (p
));
364 if (pp
->orig_type
== T_AIRPORT
)
369 return (pp
->dest_type
);
377 for (pp
= air
.head
; pp
!= NULL
; pp
= pp
->next
)
378 if (pp
->plane_no
== n
)
380 for (pp
= ground
.head
; pp
!= NULL
; pp
= pp
->next
)
381 if (pp
->plane_no
== n
)
386 too_close(p1
, p2
, dist
)
389 if (ABS(p1
->altitude
- p2
->altitude
) <= dist
&&
390 ABS(p1
->xpos
- p2
->xpos
) <= dist
&& ABS(p1
->ypos
- p2
->ypos
) <= dist
)
402 case 3: return (135);
403 case 4: return (180);
404 case 5: return (225);
405 case 6: return (270);
406 case 7: return (315);