]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/update.c
1 /* $NetBSD: update.c,v 1.12 2003/08/07 09:36:55 agc 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
38 * Copy permission is hereby granted provided that this notice is
39 * retained on all partial or complete copies.
41 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
44 #include <sys/cdefs.h>
47 static char sccsid
[] = "@(#)update.c 8.1 (Berkeley) 5/31/93";
49 __RCSID("$NetBSD: update.c,v 1.12 2003/08/07 09:36:55 agc Exp $");
57 int dummy
__attribute__((__unused__
));
59 int i
, dir_diff
, unclean
;
64 signal(SIGALRM
, update
);
71 /* put some planes in the air */
74 for (pp
= ground
.head
; pp
!= NULL
; pp
= pp
->next
) {
75 if (pp
->new_altitude
> 0) {
84 /* do altitude change and basic movement */
85 for (pp
= air
.head
; pp
!= NULL
; pp
= pp
->next
) {
86 /* type 0 only move every other turn */
87 if (pp
->plane_type
== 0 && clck
& 1)
92 loser(pp
, "ran out of fuel.");
94 pp
->altitude
+= SGN(pp
->new_altitude
- pp
->altitude
);
97 dir_diff
= pp
->new_dir
- pp
->dir
;
99 * Allow for circle commands
101 if (pp
->new_dir
>= 0 && pp
->new_dir
< MAXDIR
) {
102 if (dir_diff
> MAXDIR
/2)
104 else if (dir_diff
< -(MAXDIR
/2))
109 else if (dir_diff
< -2)
112 if (pp
->dir
>= MAXDIR
)
114 else if (pp
->dir
< 0)
117 pp
->xpos
+= displacement
[pp
->dir
].dx
;
118 pp
->ypos
+= displacement
[pp
->dir
].dy
;
120 if (pp
->delayd
&& pp
->xpos
== sp
->beacon
[pp
->delayd_no
].x
&&
121 pp
->ypos
== sp
->beacon
[pp
->delayd_no
].y
) {
123 if (pp
->status
== S_UNMARKED
)
124 pp
->status
= S_MARKED
;
127 switch (pp
->dest_type
) {
129 if (pp
->xpos
== sp
->airport
[pp
->dest_no
].x
&&
130 pp
->ypos
== sp
->airport
[pp
->dest_no
].y
&&
132 if (pp
->dir
!= sp
->airport
[pp
->dest_no
].dir
)
133 loser(pp
, "landed in the wrong direction.");
141 if (pp
->xpos
== sp
->exit
[pp
->dest_no
].x
&&
142 pp
->ypos
== sp
->exit
[pp
->dest_no
].y
) {
143 if (pp
->altitude
!= 9)
144 loser(pp
, "exited at the wrong altitude.");
152 loser(pp
, "has a bizarre destination, get help!");
154 if (pp
->altitude
> 9)
155 /* "this is impossible" */
156 loser(pp
, "exceded flight ceiling.");
157 if (pp
->altitude
<= 0) {
158 for (i
= 0; i
< sp
->num_airports
; i
++)
159 if (pp
->xpos
== sp
->airport
[i
].x
&&
160 pp
->ypos
== sp
->airport
[i
].y
) {
161 if (pp
->dest_type
== T_AIRPORT
)
163 "landed at the wrong airport.");
166 "landed instead of exited.");
168 loser(pp
, "crashed on the ground.");
170 if (pp
->xpos
< 1 || pp
->xpos
>= sp
->width
- 1 ||
171 pp
->ypos
< 1 || pp
->ypos
>= sp
->height
- 1) {
172 for (i
= 0; i
< sp
->num_exits
; i
++)
173 if (pp
->xpos
== sp
->exit
[i
].x
&&
174 pp
->ypos
== sp
->exit
[i
].y
) {
175 if (pp
->dest_type
== T_EXIT
)
177 "exited via the wrong exit.");
180 "exited instead of landed.");
182 loser(pp
, "illegally left the flight arena.");
187 * Traverse the list once, deleting the planes that are gone.
189 for (pp
= air
.head
; pp
!= NULL
; pp
= p2
) {
191 if (pp
->status
== S_GONE
) {
199 for (p1
= air
.head
; p1
!= NULL
; p1
= p1
->next
)
200 for (p2
= p1
->next
; p2
!= NULL
; p2
= p2
->next
)
201 if (too_close(p1
, p2
, 1)) {
204 (void)sprintf(buf
, "collided with plane '%c'.",
209 * Check every other update. Actually, only add on even updates.
210 * Otherwise, prop jobs show up *on* entrance. Remember that
211 * we don't update props on odd updates.
213 if ((rand() % sp
->newplane_time
) == 0)
217 alarm(sp
->update_secs
);
225 static char buf
[50], *bp
, *comm_start
;
229 (void)sprintf(bp
, "%c%d%c%c%d: ", name(pp
), pp
->altitude
,
230 (pp
->fuel
< LOWFUEL
) ? '*' : ' ',
231 (pp
->dest_type
== T_AIRPORT
) ? 'A' : 'E', pp
->dest_no
);
233 comm_start
= bp
= strchr(buf
, '\0');
234 if (pp
->altitude
== 0)
235 (void)sprintf(bp
, "Holding @ A%d", pp
->orig_no
);
236 else if (pp
->new_dir
>= MAXDIR
|| pp
->new_dir
< 0)
237 strcpy(bp
, "Circle");
238 else if (pp
->new_dir
!= pp
->dir
)
239 (void)sprintf(bp
, "%d", dir_deg(pp
->new_dir
));
241 bp
= strchr(buf
, '\0');
243 (void)sprintf(bp
, " @ B%d", pp
->delayd_no
);
245 bp
= strchr(buf
, '\0');
246 if (*comm_start
== '\0' &&
247 (pp
->status
== S_UNMARKED
|| pp
->status
== S_IGNORED
))
248 strcpy(bp
, "---------");
256 if (p
->plane_type
== 0)
257 return ('A' + p
->plane_no
);
259 return ('a' + p
->plane_no
);
266 if (l
< 'a' && l
> 'z' && l
< 'A' && l
> 'Z')
268 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
)
307 int i
, num_starts
, close
, rnd
, rnd2
, pnum
;
309 memset(&p
, 0, sizeof (p
));
312 p
.plane_type
= random() % 2;
314 num_starts
= sp
->num_exits
+ sp
->num_airports
;
315 rnd
= random() % num_starts
;
317 if (rnd
< sp
->num_exits
) {
318 p
.dest_type
= T_EXIT
;
321 p
.dest_type
= T_AIRPORT
;
322 p
.dest_no
= rnd
- sp
->num_exits
;
325 /* loop until we get a plane not near another */
326 for (i
= 0; i
< num_starts
; i
++) {
327 /* loop till we get a different start point */
328 while ((rnd2
= random() % num_starts
) == rnd
)
330 if (rnd2
< sp
->num_exits
) {
331 p
.orig_type
= T_EXIT
;
333 p
.xpos
= sp
->exit
[rnd2
].x
;
334 p
.ypos
= sp
->exit
[rnd2
].y
;
335 p
.new_dir
= p
.dir
= sp
->exit
[rnd2
].dir
;
336 p
.altitude
= p
.new_altitude
= 7;
338 for (p1
= air
.head
; p1
!= NULL
; p1
= p1
->next
)
339 if (too_close(p1
, &p
, 4)) {
346 p
.orig_type
= T_AIRPORT
;
347 p
.orig_no
= rnd2
- sp
->num_exits
;
348 p
.xpos
= sp
->airport
[p
.orig_no
].x
;
349 p
.ypos
= sp
->airport
[p
.orig_no
].y
;
350 p
.new_dir
= p
.dir
= sp
->airport
[p
.orig_no
].dir
;
351 p
.altitude
= p
.new_altitude
= 0;
353 p
.fuel
= sp
->width
+ sp
->height
;
365 loser(NULL
, "Out of memory!");
366 memcpy(pp
, &p
, sizeof (p
));
368 if (pp
->orig_type
== T_AIRPORT
)
373 return (pp
->dest_type
);
382 for (pp
= air
.head
; pp
!= NULL
; pp
= pp
->next
)
383 if (pp
->plane_no
== n
)
385 for (pp
= ground
.head
; pp
!= NULL
; pp
= pp
->next
)
386 if (pp
->plane_no
== n
)
392 too_close(p1
, p2
, dist
)
393 const PLANE
*p1
, *p2
;
396 if (ABS(p1
->altitude
- p2
->altitude
) <= dist
&&
397 ABS(p1
->xpos
- p2
->xpos
) <= dist
&& ABS(p1
->ypos
- p2
->ypos
) <= dist
)
411 case 3: return (135);
412 case 4: return (180);
413 case 5: return (225);
414 case 6: return (270);
415 case 7: return (315);