]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/update.c
2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
40 * Copy permission is hereby granted provided that this notice is
41 * retained on all partial or complete copies.
43 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
47 /*static char sccsid[] = "from: @(#)update.c 5.5 (Berkeley) 10/30/90";*/
48 static char rcsid
[] = "$Id: update.c,v 1.2 1993/08/01 18:57:02 mycroft Exp $";
55 int i
, dir_diff
, mask
, unclean
;
56 PLANE
*pp
, *p1
, *p2
, *p
;
59 mask
= sigblock(sigmask(SIGINT
));
63 signal(SIGALRM
, update
);
70 /* put some planes in the air */
73 for (pp
= ground
.head
; pp
!= NULL
; pp
= pp
->next
) {
74 if (pp
->new_altitude
> 0) {
83 /* do altitude change and basic movement */
84 for (pp
= air
.head
; pp
!= NULL
; pp
= pp
->next
) {
85 /* type 0 only move every other turn */
86 if (pp
->plane_type
== 0 && clck
& 1)
91 loser(pp
, "ran out of fuel.");
93 pp
->altitude
+= SGN(pp
->new_altitude
- pp
->altitude
);
96 dir_diff
= pp
->new_dir
- pp
->dir
;
98 * Allow for circle commands
100 if (pp
->new_dir
>= 0 && pp
->new_dir
< MAXDIR
) {
101 if (dir_diff
> MAXDIR
/2)
103 else if (dir_diff
< -(MAXDIR
/2))
108 else if (dir_diff
< -2)
111 if (pp
->dir
>= MAXDIR
)
113 else if (pp
->dir
< 0)
116 pp
->xpos
+= displacement
[pp
->dir
].dx
;
117 pp
->ypos
+= displacement
[pp
->dir
].dy
;
119 if (pp
->delayd
&& pp
->xpos
== sp
->beacon
[pp
->delayd_no
].x
&&
120 pp
->ypos
== sp
->beacon
[pp
->delayd_no
].y
) {
122 if (pp
->status
== S_UNMARKED
)
123 pp
->status
= S_MARKED
;
126 switch (pp
->dest_type
) {
128 if (pp
->xpos
== sp
->airport
[pp
->dest_no
].x
&&
129 pp
->ypos
== sp
->airport
[pp
->dest_no
].y
&&
131 if (pp
->dir
!= sp
->airport
[pp
->dest_no
].dir
)
132 loser(pp
, "landed in the wrong direction.");
140 if (pp
->xpos
== sp
->exit
[pp
->dest_no
].x
&&
141 pp
->ypos
== sp
->exit
[pp
->dest_no
].y
) {
142 if (pp
->altitude
!= 9)
143 loser(pp
, "exited at the wrong altitude.");
151 loser(pp
, "has a bizarre destination, get help!");
153 if (pp
->altitude
> 9)
154 /* "this is impossible" */
155 loser(pp
, "exceded flight ceiling.");
156 if (pp
->altitude
<= 0) {
157 for (i
= 0; i
< sp
->num_airports
; i
++)
158 if (pp
->xpos
== sp
->airport
[i
].x
&&
159 pp
->ypos
== sp
->airport
[i
].y
) {
160 if (pp
->dest_type
== T_AIRPORT
)
162 "landed at the wrong airport.");
165 "landed instead of exited.");
167 loser(pp
, "crashed on the ground.");
169 if (pp
->xpos
< 1 || pp
->xpos
>= sp
->width
- 1 ||
170 pp
->ypos
< 1 || pp
->ypos
>= sp
->height
- 1) {
171 for (i
= 0; i
< sp
->num_exits
; i
++)
172 if (pp
->xpos
== sp
->exit
[i
].x
&&
173 pp
->ypos
== sp
->exit
[i
].y
) {
174 if (pp
->dest_type
== T_EXIT
)
176 "exited via the wrong exit.");
179 "exited instead of landed.");
181 loser(pp
, "illegally left the flight arena.");
186 * Traverse the list once, deleting the planes that are gone.
188 for (pp
= air
.head
; pp
!= NULL
; pp
= p2
) {
190 if (pp
->status
== S_GONE
) {
198 for (p1
= air
.head
; p1
!= NULL
; p1
= p1
->next
)
199 for (p2
= p1
->next
; p2
!= NULL
; p2
= p2
->next
)
200 if (too_close(p1
, p2
, 1)) {
203 (void)sprintf(buf
, "collided with plane '%c'.",
208 * Check every other update. Actually, only add on even updates.
209 * Otherwise, prop jobs show up *on* entrance. Remember that
210 * we don't update props on odd updates.
212 if ((rand() % sp
->newplane_time
) == 0)
219 alarm(sp
->update_secs
);
227 static char buf
[50], *bp
, *comm_start
;
232 (void)sprintf(bp
, "%c%d%c%c%d: ", name(pp
), pp
->altitude
,
233 (pp
->fuel
< LOWFUEL
) ? '*' : ' ',
234 (pp
->dest_type
== T_AIRPORT
) ? 'A' : 'E', pp
->dest_no
);
236 comm_start
= bp
= index(buf
, '\0');
237 if (pp
->altitude
== 0)
238 (void)sprintf(bp
, "Holding @ A%d", pp
->orig_no
);
239 else if (pp
->new_dir
>= MAXDIR
|| pp
->new_dir
< 0)
240 strcpy(bp
, "Circle");
241 else if (pp
->new_dir
!= pp
->dir
)
242 (void)sprintf(bp
, "%d", dir_deg(pp
->new_dir
));
244 bp
= index(buf
, '\0');
246 (void)sprintf(bp
, " @ B%d", pp
->delayd_no
);
248 bp
= index(buf
, '\0');
249 if (*comm_start
== '\0' &&
250 (pp
->status
== S_UNMARKED
|| pp
->status
== S_IGNORED
))
251 strcpy(bp
, "---------");
259 if (p
->plane_type
== 0)
260 return ('A' + p
->plane_no
);
262 return ('a' + p
->plane_no
);
267 if (l
< 'a' && l
> 'z' && l
< 'A' && l
> 'Z')
269 else if (l
>= 'a' && l
<= 'z')
277 static int last_plane
= -1;
279 int found
, start_plane
= last_plane
;
284 if (last_plane
>= 26)
286 for (pp
= air
.head
; pp
!= NULL
; pp
= pp
->next
)
287 if (pp
->plane_no
== last_plane
) {
292 for (pp
= ground
.head
; pp
!= NULL
; pp
= pp
->next
)
293 if (pp
->plane_no
== last_plane
) {
297 } while (found
&& last_plane
!= start_plane
);
298 if (last_plane
== start_plane
)
306 int i
, num_starts
, close
, rnd
, rnd2
, pnum
;
308 bzero(&p
, sizeof (p
));
311 p
.plane_type
= random() % 2;
313 num_starts
= sp
->num_exits
+ sp
->num_airports
;
314 rnd
= random() % num_starts
;
316 if (rnd
< sp
->num_exits
) {
317 p
.dest_type
= T_EXIT
;
320 p
.dest_type
= T_AIRPORT
;
321 p
.dest_no
= rnd
- sp
->num_exits
;
324 /* loop until we get a plane not near another */
325 for (i
= 0; i
< num_starts
; i
++) {
326 /* loop till we get a different start point */
327 while ((rnd2
= random() % num_starts
) == rnd
)
329 if (rnd2
< sp
->num_exits
) {
330 p
.orig_type
= T_EXIT
;
332 p
.xpos
= sp
->exit
[rnd2
].x
;
333 p
.ypos
= sp
->exit
[rnd2
].y
;
334 p
.new_dir
= p
.dir
= sp
->exit
[rnd2
].dir
;
335 p
.altitude
= p
.new_altitude
= 7;
337 for (p1
= air
.head
; p1
!= NULL
; p1
= p1
->next
)
338 if (too_close(p1
, &p
, 4)) {
345 p
.orig_type
= T_AIRPORT
;
346 p
.orig_no
= rnd2
- sp
->num_exits
;
347 p
.xpos
= sp
->airport
[p
.orig_no
].x
;
348 p
.ypos
= sp
->airport
[p
.orig_no
].y
;
349 p
.new_dir
= p
.dir
= sp
->airport
[p
.orig_no
].dir
;
350 p
.altitude
= p
.new_altitude
= 0;
352 p
.fuel
= sp
->width
+ sp
->height
;
363 bcopy(&p
, pp
, sizeof (p
));
365 if (pp
->orig_type
== T_AIRPORT
)
370 return (pp
->dest_type
);
378 for (pp
= air
.head
; pp
!= NULL
; pp
= pp
->next
)
379 if (pp
->plane_no
== n
)
381 for (pp
= ground
.head
; pp
!= NULL
; pp
= pp
->next
)
382 if (pp
->plane_no
== n
)
387 too_close(p1
, p2
, dist
)
390 if (ABS(p1
->altitude
- p2
->altitude
) <= dist
&&
391 ABS(p1
->xpos
- p2
->xpos
) <= dist
&& ABS(p1
->ypos
- p2
->ypos
) <= dist
)
403 case 3: return (135);
404 case 4: return (180);
405 case 5: return (225);
406 case 6: return (270);
407 case 7: return (315);