if_iwm - Process multiple frames per RX buffer.
[dragonfly.git] / games / atc / update.c
blob76d4d1177d03c48d4cca1c69f1b0cd173fc18146
1 /*-
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
6 * Ed James.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#)update.c 8.1 (Berkeley) 5/31/93
33 * $FreeBSD: src/games/atc/update.c,v 1.6 1999/11/30 03:48:21 billf Exp $
37 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
39 * Copy permission is hereby granted provided that this notice is
40 * retained on all partial or complete copies.
42 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
45 #include "include.h"
47 static int next_plane(void);
48 static int too_close(const PLANE *, const PLANE *, int);
49 static int dir_deg(int);
52 void
53 update(__unused int sig)
55 int i, dir_diff, mask, unclean;
56 PLANE *pp, *p1, *p2;
58 mask = sigblock(sigmask(SIGINT));
60 clck++;
62 erase_all();
64 /* put some planes in the air */
65 do {
66 unclean = 0;
67 for (pp = ground.head; pp != NULL; pp = pp->next) {
68 if (pp->new_altitude > 0) {
69 delete(&ground, pp);
70 append(&air, pp);
71 unclean = 1;
72 break;
75 } while (unclean);
77 /* do altitude change and basic movement */
78 for (pp = air.head; pp != NULL; pp = pp->next) {
79 /* type 0 only move every other turn */
80 if (pp->plane_type == 0 && clck & 1)
81 continue;
83 pp->fuel--;
84 if (pp->fuel < 0)
85 loser(pp, "ran out of fuel.");
87 pp->altitude += SGN(pp->new_altitude - pp->altitude);
89 if (!pp->delayd) {
90 dir_diff = pp->new_dir - pp->dir;
92 * Allow for circle commands
94 if (pp->new_dir >= 0 && pp->new_dir < MAXDIR) {
95 if (dir_diff > MAXDIR/2)
96 dir_diff -= MAXDIR;
97 else if (dir_diff < -(MAXDIR/2))
98 dir_diff += MAXDIR;
100 if (dir_diff > 2)
101 dir_diff = 2;
102 else if (dir_diff < -2)
103 dir_diff = -2;
104 pp->dir += dir_diff;
105 if (pp->dir >= MAXDIR)
106 pp->dir -= MAXDIR;
107 else if (pp->dir < 0)
108 pp->dir += MAXDIR;
110 pp->xpos += displacement[pp->dir].dx;
111 pp->ypos += displacement[pp->dir].dy;
113 if (pp->delayd && pp->xpos == sp->beacon[pp->delayd_no].x &&
114 pp->ypos == sp->beacon[pp->delayd_no].y) {
115 pp->delayd = 0;
116 if (pp->status == S_UNMARKED)
117 pp->status = S_MARKED;
120 switch (pp->dest_type) {
121 case T_AIRPORT:
122 if (pp->xpos == sp->airport[pp->dest_no].x &&
123 pp->ypos == sp->airport[pp->dest_no].y &&
124 pp->altitude == 0) {
125 if (pp->dir != sp->airport[pp->dest_no].dir)
126 loser(pp, "landed in the wrong direction.");
127 else {
128 pp->status = S_GONE;
129 continue;
132 break;
133 case T_EXIT:
134 if (pp->xpos == sp->exit[pp->dest_no].x &&
135 pp->ypos == sp->exit[pp->dest_no].y) {
136 if (pp->altitude != 9)
137 loser(pp, "exited at the wrong altitude.");
138 else {
139 pp->status = S_GONE;
140 continue;
143 break;
144 default:
145 loser(pp, "has a bizarre destination, get help!");
147 if (pp->altitude > 9)
148 /* "this is impossible" */
149 loser(pp, "exceded flight ceiling.");
150 if (pp->altitude <= 0) {
151 for (i = 0; i < sp->num_airports; i++)
152 if (pp->xpos == sp->airport[i].x &&
153 pp->ypos == sp->airport[i].y) {
154 if (pp->dest_type == T_AIRPORT)
155 loser(pp,
156 "landed at the wrong airport.");
157 else
158 loser(pp,
159 "landed instead of exited.");
161 loser(pp, "crashed on the ground.");
163 if (pp->xpos < 1 || pp->xpos >= sp->width - 1 ||
164 pp->ypos < 1 || pp->ypos >= sp->height - 1) {
165 for (i = 0; i < sp->num_exits; i++)
166 if (pp->xpos == sp->exit[i].x &&
167 pp->ypos == sp->exit[i].y) {
168 if (pp->dest_type == T_EXIT)
169 loser(pp,
170 "exited via the wrong exit.");
171 else
172 loser(pp,
173 "exited instead of landed.");
175 loser(pp, "illegally left the flight arena.");
180 * Traverse the list once, deleting the planes that are gone.
182 for (pp = air.head; pp != NULL; pp = p2) {
183 p2 = pp->next;
184 if (pp->status == S_GONE) {
185 safe_planes++;
186 delete(&air, pp);
190 draw_all();
192 for (p1 = air.head; p1 != NULL; p1 = p1->next)
193 for (p2 = p1->next; p2 != NULL; p2 = p2->next)
194 if (too_close(p1, p2, 1)) {
195 static char buf[80];
197 (void)sprintf(buf, "collided with plane '%c'.",
198 name(p2));
199 loser(p1, buf);
202 * Check every other update. Actually, only add on even updates.
203 * Otherwise, prop jobs show up *on* entrance. Remember that
204 * we don't update props on odd updates.
206 if ((random() % sp->newplane_time) == 0)
207 addplane();
209 sigsetmask(mask);
212 const char *
213 command(const PLANE *pp)
215 static char buf[50], *bp, *comm_start;
217 buf[0] = '\0';
218 bp = buf;
219 (void)sprintf(bp, "%c%d%c%c%d: ", name(pp), pp->altitude,
220 (pp->fuel < LOWFUEL) ? '*' : ' ',
221 (pp->dest_type == T_AIRPORT) ? 'A' : 'E', pp->dest_no);
223 comm_start = bp = index(buf, '\0');
224 if (pp->altitude == 0)
225 (void)sprintf(bp, "Holding @ A%d", pp->orig_no);
226 else if (pp->new_dir >= MAXDIR || pp->new_dir < 0)
227 strcpy(bp, "Circle");
228 else if (pp->new_dir != pp->dir)
229 (void)sprintf(bp, "%d", dir_deg(pp->new_dir));
231 bp = index(buf, '\0');
232 if (pp->delayd)
233 (void)sprintf(bp, " @ B%d", pp->delayd_no);
235 bp = index(buf, '\0');
236 if (*comm_start == '\0' &&
237 (pp->status == S_UNMARKED || pp->status == S_IGNORED))
238 strcpy(bp, "---------");
239 return (buf);
242 /* char */
243 char
244 name(const PLANE *p)
246 if (p->plane_type == 0)
247 return ('A' + p->plane_no);
248 else
249 return ('a' + p->plane_no);
252 char
253 number(char l)
255 if (islower(l))
256 return (l - 'a');
257 else if (isupper(l))
258 return (l - 'A');
259 else
260 return (-1);
263 static int
264 next_plane(void)
266 static int last_plane = -1;
267 PLANE *pp;
268 int found, start_plane = last_plane;
270 do {
271 found = 0;
272 last_plane++;
273 if (last_plane >= 26)
274 last_plane = 0;
275 for (pp = air.head; pp != NULL; pp = pp->next)
276 if (pp->plane_no == last_plane) {
277 found++;
278 break;
280 if (!found)
281 for (pp = ground.head; pp != NULL; pp = pp->next)
282 if (pp->plane_no == last_plane) {
283 found++;
284 break;
286 } while (found && last_plane != start_plane);
287 if (last_plane == start_plane)
288 return (-1);
289 return (last_plane);
293 addplane(void)
295 PLANE p, *pp, *p1;
296 int i, num_starts, is_close, rnd, rnd2, pnum;
298 bzero(&p, sizeof (p));
300 p.status = S_MARKED;
301 p.plane_type = random() % 2;
303 num_starts = sp->num_exits + sp->num_airports;
304 rnd = random() % num_starts;
306 if (rnd < sp->num_exits) {
307 p.dest_type = T_EXIT;
308 p.dest_no = rnd;
309 } else {
310 p.dest_type = T_AIRPORT;
311 p.dest_no = rnd - sp->num_exits;
314 /* loop until we get a plane not near another */
315 for (i = 0; i < num_starts; i++) {
316 /* loop till we get a different start point */
317 while ((rnd2 = random() % num_starts) == rnd)
319 if (rnd2 < sp->num_exits) {
320 p.orig_type = T_EXIT;
321 p.orig_no = rnd2;
322 p.xpos = sp->exit[rnd2].x;
323 p.ypos = sp->exit[rnd2].y;
324 p.new_dir = p.dir = sp->exit[rnd2].dir;
325 p.altitude = p.new_altitude = 7;
326 is_close = 0;
327 for (p1 = air.head; p1 != NULL; p1 = p1->next)
328 if (too_close(p1, &p, 4)) {
329 is_close++;
330 break;
332 if (is_close)
333 continue;
334 } else {
335 p.orig_type = T_AIRPORT;
336 p.orig_no = rnd2 - sp->num_exits;
337 p.xpos = sp->airport[p.orig_no].x;
338 p.ypos = sp->airport[p.orig_no].y;
339 p.new_dir = p.dir = sp->airport[p.orig_no].dir;
340 p.altitude = p.new_altitude = 0;
342 p.fuel = sp->width + sp->height;
343 break;
345 if (i >= num_starts)
346 return (-1);
347 pnum = next_plane();
348 if (pnum < 0)
349 return (-1);
350 p.plane_no = pnum;
352 pp = newplane();
353 bcopy(&p, pp, sizeof (p));
355 if (pp->orig_type == T_AIRPORT)
356 append(&ground, pp);
357 else
358 append(&air, pp);
360 return (pp->dest_type);
363 PLANE *
364 findplane(int n)
366 PLANE *pp;
368 for (pp = air.head; pp != NULL; pp = pp->next)
369 if (pp->plane_no == n)
370 return (pp);
371 for (pp = ground.head; pp != NULL; pp = pp->next)
372 if (pp->plane_no == n)
373 return (pp);
374 return (NULL);
377 static int
378 too_close(const PLANE *p1, const PLANE *p2, int dist)
380 if (ABS(p1->altitude - p2->altitude) <= dist &&
381 ABS(p1->xpos - p2->xpos) <= dist && ABS(p1->ypos - p2->ypos) <= dist)
382 return (1);
383 else
384 return (0);
387 static int
388 dir_deg(int d)
390 switch (d) {
391 case 0: return (0);
392 case 1: return (45);
393 case 2: return (90);
394 case 3: return (135);
395 case 4: return (180);
396 case 5: return (225);
397 case 6: return (270);
398 case 7: return (315);
399 default:
400 return (-1);