]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/update.c
1 /* $NetBSD: update.c,v 1.27 2015/06/25 05:33:02 dholland 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.27 2015/06/25 05:33:02 dholland Exp $");
63 static int next_plane(void);
64 static int too_close(const PLANE
*p1
, const PLANE
*p2
, int);
65 static int dir_deg(int);
69 update(int dummy __unused
)
71 int dir_diff
, unclean
;
77 signal(SIGALRM
, update
);
84 /* put some planes in the air */
87 for (pp
= ground
.head
; pp
!= NULL
; pp
= pp
->next
) {
88 if (pp
->new_altitude
> 0) {
97 /* do altitude change and basic movement */
98 for (pp
= air
.head
; pp
!= NULL
; pp
= pp
->next
) {
99 /* type 0 only move every other turn */
100 if (pp
->plane_type
== 0 && clck
& 1)
105 loser(pp
, "ran out of fuel.");
107 pp
->altitude
+= SGN(pp
->new_altitude
- pp
->altitude
);
110 dir_diff
= pp
->new_dir
- pp
->dir
;
112 * Allow for circle commands
114 if (pp
->new_dir
>= 0 && pp
->new_dir
< MAXDIR
) {
115 if (dir_diff
> MAXDIR
/2)
117 else if (dir_diff
< -(MAXDIR
/2))
122 else if (dir_diff
< -2)
125 if (pp
->dir
>= MAXDIR
)
127 else if (pp
->dir
< 0)
130 pp
->xpos
+= displacement
[pp
->dir
].dx
;
131 pp
->ypos
+= displacement
[pp
->dir
].dy
;
133 if (pp
->delayd
&& pp
->xpos
== sp
->beacon
[pp
->delayd_no
].x
&&
134 pp
->ypos
== sp
->beacon
[pp
->delayd_no
].y
) {
136 if (pp
->status
== S_UNMARKED
)
137 pp
->status
= S_MARKED
;
140 switch (pp
->dest_type
) {
142 if (pp
->xpos
== sp
->airport
[pp
->dest_no
].x
&&
143 pp
->ypos
== sp
->airport
[pp
->dest_no
].y
&&
145 if (pp
->dir
!= sp
->airport
[pp
->dest_no
].dir
)
146 loser(pp
, "landed in the wrong direction.");
154 if (pp
->xpos
== sp
->exit
[pp
->dest_no
].x
&&
155 pp
->ypos
== sp
->exit
[pp
->dest_no
].y
) {
156 if (pp
->altitude
!= 9)
157 loser(pp
, "exited at the wrong altitude.");
165 loser(pp
, "has a bizarre destination, get help!");
167 if (pp
->altitude
> 9)
168 /* "this is impossible" */
169 loser(pp
, "exceeded flight ceiling.");
170 if (pp
->altitude
<= 0) {
171 for (i
= 0; i
< sp
->num_airports
; i
++)
172 if (pp
->xpos
== sp
->airport
[i
].x
&&
173 pp
->ypos
== sp
->airport
[i
].y
) {
174 if (pp
->dest_type
== T_AIRPORT
)
176 "landed at the wrong airport.");
179 "landed instead of exited.");
181 loser(pp
, "crashed on the ground.");
183 if (pp
->xpos
< 1 || pp
->xpos
>= sp
->width
- 1 ||
184 pp
->ypos
< 1 || pp
->ypos
>= sp
->height
- 1) {
185 for (i
= 0; i
< sp
->num_exits
; i
++)
186 if (pp
->xpos
== sp
->exit
[i
].x
&&
187 pp
->ypos
== sp
->exit
[i
].y
) {
188 if (pp
->dest_type
== T_EXIT
)
190 "exited via the wrong exit.");
193 "exited instead of landed.");
195 loser(pp
, "illegally left the flight arena.");
200 * Traverse the list once, deleting the planes that are gone.
202 for (pp
= air
.head
; pp
!= NULL
; pp
= p2
) {
204 if (pp
->status
== S_GONE
) {
212 for (p1
= air
.head
; p1
!= NULL
; p1
= p1
->next
)
213 for (p2
= p1
->next
; p2
!= NULL
; p2
= p2
->next
)
214 if (too_close(p1
, p2
, 1)) {
217 (void)snprintf(buf
, sizeof(buf
),
218 "collided with plane '%c'.",
223 * Check every other update. Actually, only add on even updates.
224 * Otherwise, prop jobs show up *on* entrance. Remember that
225 * we don't update props on odd updates.
227 if ((rand() % sp
->newplane_time
) == 0)
231 alarm(sp
->update_secs
);
236 loser(const PLANE
*p
, const char *s
)
240 struct itimerval itv
;
245 itv
.it_value
.tv_sec
= 0;
246 itv
.it_value
.tv_usec
= 0;
247 (void)setitimer(ITIMER_REAL
, &itv
, NULL
);
254 while ((c
= getAChar()) != EOF
&& c
!= ' ')
262 command(const PLANE
*pp
)
264 static char buf
[50], *bp
, *comm_start
;
269 bpsize
= sizeof(buf
);
270 (void)snprintf(bp
, bpsize
, "%c%d%c%c%u: ", name(pp
), pp
->altitude
,
271 (pp
->fuel
< LOWFUEL
) ? '*' : ' ',
272 (pp
->dest_type
== T_AIRPORT
) ? 'A' : 'E', pp
->dest_no
);
274 comm_start
= bp
= strchr(buf
, '\0');
275 bpsize
= buf
+ sizeof(buf
) - bp
;
276 if (pp
->altitude
== 0)
277 (void)snprintf(bp
, bpsize
, "Holding @ A%u", pp
->orig_no
);
278 else if (pp
->new_dir
>= MAXDIR
|| pp
->new_dir
< 0)
279 (void)snprintf(bp
, bpsize
, "Circle");
280 else if (pp
->new_dir
!= pp
->dir
)
281 (void)snprintf(bp
, bpsize
, "%d", dir_deg(pp
->new_dir
));
283 bp
= strchr(buf
, '\0');
284 bpsize
= buf
+ sizeof(buf
) - bp
;
286 (void)snprintf(bp
, bpsize
, " @ B%u", pp
->delayd_no
);
288 bp
= strchr(buf
, '\0');
289 bpsize
= buf
+ sizeof(buf
) - bp
;
290 if (*comm_start
== '\0' &&
291 (pp
->status
== S_UNMARKED
|| pp
->status
== S_IGNORED
))
292 (void)snprintf(bp
, bpsize
, "---------");
299 if (p
->plane_type
== 0)
300 return ('A' + p
->plane_no
);
302 return ('a' + p
->plane_no
);
308 if (islower((unsigned char)l
))
310 else if (isupper((unsigned char)l
))
319 static int last_plane
= -1;
321 int found
, start_plane
= last_plane
;
326 if (last_plane
>= 26)
328 for (pp
= air
.head
; pp
!= NULL
; pp
= pp
->next
)
329 if (pp
->plane_no
== last_plane
) {
334 for (pp
= ground
.head
; pp
!= NULL
; pp
= pp
->next
)
335 if (pp
->plane_no
== last_plane
) {
339 } while (found
&& last_plane
!= start_plane
);
350 unsigned num_starts
, rnd
, rnd2
, i
;
352 (void)memset(&p
, 0, sizeof (p
));
355 p
.plane_type
= random() % 2;
357 num_starts
= sp
->num_exits
+ sp
->num_airports
;
358 rnd
= random() % num_starts
;
360 if (rnd
< sp
->num_exits
) {
361 p
.dest_type
= T_EXIT
;
364 p
.dest_type
= T_AIRPORT
;
365 p
.dest_no
= rnd
- sp
->num_exits
;
368 /* loop until we get a plane not near another */
369 for (i
= 0; i
< num_starts
; i
++) {
370 /* loop till we get a different start point */
371 while ((rnd2
= random() % num_starts
) == rnd
)
373 if (rnd2
< sp
->num_exits
) {
374 p
.orig_type
= T_EXIT
;
376 p
.xpos
= sp
->exit
[rnd2
].x
;
377 p
.ypos
= sp
->exit
[rnd2
].y
;
378 p
.new_dir
= p
.dir
= sp
->exit
[rnd2
].dir
;
379 p
.altitude
= p
.new_altitude
= 7;
381 for (p1
= air
.head
; p1
!= NULL
; p1
= p1
->next
)
382 if (too_close(p1
, &p
, 4)) {
389 p
.orig_type
= T_AIRPORT
;
390 p
.orig_no
= rnd2
- sp
->num_exits
;
391 p
.xpos
= sp
->airport
[p
.orig_no
].x
;
392 p
.ypos
= sp
->airport
[p
.orig_no
].y
;
393 p
.new_dir
= p
.dir
= sp
->airport
[p
.orig_no
].dir
;
394 p
.altitude
= p
.new_altitude
= 0;
396 p
.fuel
= sp
->width
+ sp
->height
;
408 loser(NULL
, "Out of memory!");
409 (void)memcpy(pp
, &p
, sizeof (p
));
411 if (pp
->orig_type
== T_AIRPORT
)
422 for (pp
= air
.head
; pp
!= NULL
; pp
= pp
->next
)
423 if (pp
->plane_no
== n
)
425 for (pp
= ground
.head
; pp
!= NULL
; pp
= pp
->next
)
426 if (pp
->plane_no
== n
)
432 too_close(const PLANE
*p1
, const PLANE
*p2
, int dist
)
434 if (ABS(p1
->altitude
- p2
->altitude
) <= dist
&&
435 ABS(p1
->xpos
- p2
->xpos
) <= dist
&&
436 ABS(p1
->ypos
- p2
->ypos
) <= dist
)
449 case 3: return (135);
450 case 4: return (180);
451 case 5: return (225);
452 case 6: return (270);
453 case 7: return (315);