2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
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.
48 static char sccsid[] = "@(#)update.c 8.1 (Berkeley) 5/31/93";
50 static const char rcsid[] =
51 "$FreeBSD: src/games/atc/update.c,v 1.6 1999/11/30 03:48:21 billf Exp $";
61 int i, dir_diff, mask, unclean;
62 PLANE *pp, *p1, *p2, *p;
65 mask = sigblock(sigmask(SIGINT));
69 signal(SIGALRM, update);
76 /* put some planes in the air */
79 for (pp = ground.head; pp != NULL; pp = pp->next) {
80 if (pp->new_altitude > 0) {
89 /* do altitude change and basic movement */
90 for (pp = air.head; pp != NULL; pp = pp->next) {
91 /* type 0 only move every other turn */
92 if (pp->plane_type == 0 && clck & 1)
97 loser(pp, "ran out of fuel.");
99 pp->altitude += SGN(pp->new_altitude - pp->altitude);
102 dir_diff = pp->new_dir - pp->dir;
104 * Allow for circle commands
106 if (pp->new_dir >= 0 && pp->new_dir < MAXDIR) {
107 if (dir_diff > MAXDIR/2)
109 else if (dir_diff < -(MAXDIR/2))
114 else if (dir_diff < -2)
117 if (pp->dir >= MAXDIR)
119 else if (pp->dir < 0)
122 pp->xpos += displacement[pp->dir].dx;
123 pp->ypos += displacement[pp->dir].dy;
125 if (pp->delayd && pp->xpos == sp->beacon[pp->delayd_no].x &&
126 pp->ypos == sp->beacon[pp->delayd_no].y) {
128 if (pp->status == S_UNMARKED)
129 pp->status = S_MARKED;
132 switch (pp->dest_type) {
134 if (pp->xpos == sp->airport[pp->dest_no].x &&
135 pp->ypos == sp->airport[pp->dest_no].y &&
137 if (pp->dir != sp->airport[pp->dest_no].dir)
138 loser(pp, "landed in the wrong direction.");
146 if (pp->xpos == sp->exit[pp->dest_no].x &&
147 pp->ypos == sp->exit[pp->dest_no].y) {
148 if (pp->altitude != 9)
149 loser(pp, "exited at the wrong altitude.");
157 loser(pp, "has a bizarre destination, get help!");
159 if (pp->altitude > 9)
160 /* "this is impossible" */
161 loser(pp, "exceded flight ceiling.");
162 if (pp->altitude <= 0) {
163 for (i = 0; i < sp->num_airports; i++)
164 if (pp->xpos == sp->airport[i].x &&
165 pp->ypos == sp->airport[i].y) {
166 if (pp->dest_type == T_AIRPORT)
168 "landed at the wrong airport.");
171 "landed instead of exited.");
173 loser(pp, "crashed on the ground.");
175 if (pp->xpos < 1 || pp->xpos >= sp->width - 1 ||
176 pp->ypos < 1 || pp->ypos >= sp->height - 1) {
177 for (i = 0; i < sp->num_exits; i++)
178 if (pp->xpos == sp->exit[i].x &&
179 pp->ypos == sp->exit[i].y) {
180 if (pp->dest_type == T_EXIT)
182 "exited via the wrong exit.");
185 "exited instead of landed.");
187 loser(pp, "illegally left the flight arena.");
192 * Traverse the list once, deleting the planes that are gone.
194 for (pp = air.head; pp != NULL; pp = p2) {
196 if (pp->status == S_GONE) {
204 for (p1 = air.head; p1 != NULL; p1 = p1->next)
205 for (p2 = p1->next; p2 != NULL; p2 = p2->next)
206 if (too_close(p1, p2, 1)) {
209 (void)sprintf(buf, "collided with plane '%c'.",
214 * Check every other update. Actually, only add on even updates.
215 * Otherwise, prop jobs show up *on* entrance. Remember that
216 * we don't update props on odd updates.
218 if ((random() % sp->newplane_time) == 0)
225 alarm(sp->update_secs);
233 static char buf[50], *bp, *comm_start;
238 (void)sprintf(bp, "%c%d%c%c%d: ", name(pp), pp->altitude,
239 (pp->fuel < LOWFUEL) ? '*' : ' ',
240 (pp->dest_type == T_AIRPORT) ? 'A' : 'E', pp->dest_no);
242 comm_start = bp = index(buf, '\0');
243 if (pp->altitude == 0)
244 (void)sprintf(bp, "Holding @ A%d", pp->orig_no);
245 else if (pp->new_dir >= MAXDIR || pp->new_dir < 0)
246 strcpy(bp, "Circle");
247 else if (pp->new_dir != pp->dir)
248 (void)sprintf(bp, "%d", dir_deg(pp->new_dir));
250 bp = index(buf, '\0');
252 (void)sprintf(bp, " @ B%d", pp->delayd_no);
254 bp = index(buf, '\0');
255 if (*comm_start == '\0' &&
256 (pp->status == S_UNMARKED || pp->status == S_IGNORED))
257 strcpy(bp, "---------");
266 if (p->plane_type == 0)
267 return ('A' + p->plane_no);
269 return ('a' + p->plane_no);
274 if (l < 'a' && l > 'z' && l < 'A' && l > 'Z')
276 else if (l >= 'a' && l <= 'z')
284 static int last_plane = -1;
286 int found, start_plane = last_plane;
291 if (last_plane >= 26)
293 for (pp = air.head; pp != NULL; pp = pp->next)
294 if (pp->plane_no == last_plane) {
299 for (pp = ground.head; pp != NULL; pp = pp->next)
300 if (pp->plane_no == last_plane) {
304 } while (found && last_plane != start_plane);
305 if (last_plane == start_plane)
313 int i, num_starts, close, rnd, rnd2, pnum;
315 bzero(&p, sizeof (p));
318 p.plane_type = random() % 2;
320 num_starts = sp->num_exits + sp->num_airports;
321 rnd = random() % num_starts;
323 if (rnd < sp->num_exits) {
324 p.dest_type = T_EXIT;
327 p.dest_type = T_AIRPORT;
328 p.dest_no = rnd - sp->num_exits;
331 /* loop until we get a plane not near another */
332 for (i = 0; i < num_starts; i++) {
333 /* loop till we get a different start point */
334 while ((rnd2 = random() % num_starts) == rnd)
336 if (rnd2 < sp->num_exits) {
337 p.orig_type = T_EXIT;
339 p.xpos = sp->exit[rnd2].x;
340 p.ypos = sp->exit[rnd2].y;
341 p.new_dir = p.dir = sp->exit[rnd2].dir;
342 p.altitude = p.new_altitude = 7;
344 for (p1 = air.head; p1 != NULL; p1 = p1->next)
345 if (too_close(p1, &p, 4)) {
352 p.orig_type = T_AIRPORT;
353 p.orig_no = rnd2 - sp->num_exits;
354 p.xpos = sp->airport[p.orig_no].x;
355 p.ypos = sp->airport[p.orig_no].y;
356 p.new_dir = p.dir = sp->airport[p.orig_no].dir;
357 p.altitude = p.new_altitude = 0;
359 p.fuel = sp->width + sp->height;
370 bcopy(&p, pp, sizeof (p));
372 if (pp->orig_type == T_AIRPORT)
377 return (pp->dest_type);
386 for (pp = air.head; pp != NULL; pp = pp->next)
387 if (pp->plane_no == n)
389 for (pp = ground.head; pp != NULL; pp = pp->next)
390 if (pp->plane_no == n)
396 too_close(p1, p2, dist)
397 const PLANE *p1, *p2;
400 if (ABS(p1->altitude - p2->altitude) <= dist &&
401 ABS(p1->xpos - p2->xpos) <= dist && ABS(p1->ypos - p2->ypos) <= dist)
413 case 3: return (135);
414 case 4: return (180);
415 case 5: return (225);
416 case 6: return (270);
417 case 7: return (315);