Remove tm.h and xm.h handling, as it wasn't used. Use nm.h only when needed.
[dragonfly.git] / games / atc / update.c
blobbeebae126e07798d6a462338f3c42250ea361008
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. 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
34 * SUCH DAMAGE.
36 * @(#)update.c 8.1 (Berkeley) 5/31/93
37 * $FreeBSD: src/games/atc/update.c,v 1.6 1999/11/30 03:48:21 billf Exp $
38 * $DragonFly: src/games/atc/update.c,v 1.3 2006/08/08 15:03:02 pavalos Exp $
42 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
44 * Copy permission is hereby granted provided that this notice is
45 * retained on all partial or complete copies.
47 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
50 #include "include.h"
52 static int next_plane(void);
53 static int too_close(const PLANE *, const PLANE *, int);
54 static int dir_deg(int);
57 void
58 update(void)
60 int i, dir_diff, mask, unclean;
61 PLANE *pp, *p1, *p2;
63 #ifdef BSD
64 mask = sigblock(sigmask(SIGINT));
65 #endif
66 #ifdef SYSV
67 alarm(0);
68 signal(SIGALRM, update);
69 #endif
71 clck++;
73 erase_all();
75 /* put some planes in the air */
76 do {
77 unclean = 0;
78 for (pp = ground.head; pp != NULL; pp = pp->next) {
79 if (pp->new_altitude > 0) {
80 delete(&ground, pp);
81 append(&air, pp);
82 unclean = 1;
83 break;
86 } while (unclean);
88 /* do altitude change and basic movement */
89 for (pp = air.head; pp != NULL; pp = pp->next) {
90 /* type 0 only move every other turn */
91 if (pp->plane_type == 0 && clck & 1)
92 continue;
94 pp->fuel--;
95 if (pp->fuel < 0)
96 loser(pp, "ran out of fuel.");
98 pp->altitude += SGN(pp->new_altitude - pp->altitude);
100 if (!pp->delayd) {
101 dir_diff = pp->new_dir - pp->dir;
103 * Allow for circle commands
105 if (pp->new_dir >= 0 && pp->new_dir < MAXDIR) {
106 if (dir_diff > MAXDIR/2)
107 dir_diff -= MAXDIR;
108 else if (dir_diff < -(MAXDIR/2))
109 dir_diff += MAXDIR;
111 if (dir_diff > 2)
112 dir_diff = 2;
113 else if (dir_diff < -2)
114 dir_diff = -2;
115 pp->dir += dir_diff;
116 if (pp->dir >= MAXDIR)
117 pp->dir -= MAXDIR;
118 else if (pp->dir < 0)
119 pp->dir += MAXDIR;
121 pp->xpos += displacement[pp->dir].dx;
122 pp->ypos += displacement[pp->dir].dy;
124 if (pp->delayd && pp->xpos == sp->beacon[pp->delayd_no].x &&
125 pp->ypos == sp->beacon[pp->delayd_no].y) {
126 pp->delayd = 0;
127 if (pp->status == S_UNMARKED)
128 pp->status = S_MARKED;
131 switch (pp->dest_type) {
132 case T_AIRPORT:
133 if (pp->xpos == sp->airport[pp->dest_no].x &&
134 pp->ypos == sp->airport[pp->dest_no].y &&
135 pp->altitude == 0) {
136 if (pp->dir != sp->airport[pp->dest_no].dir)
137 loser(pp, "landed in the wrong direction.");
138 else {
139 pp->status = S_GONE;
140 continue;
143 break;
144 case T_EXIT:
145 if (pp->xpos == sp->exit[pp->dest_no].x &&
146 pp->ypos == sp->exit[pp->dest_no].y) {
147 if (pp->altitude != 9)
148 loser(pp, "exited at the wrong altitude.");
149 else {
150 pp->status = S_GONE;
151 continue;
154 break;
155 default:
156 loser(pp, "has a bizarre destination, get help!");
158 if (pp->altitude > 9)
159 /* "this is impossible" */
160 loser(pp, "exceded flight ceiling.");
161 if (pp->altitude <= 0) {
162 for (i = 0; i < sp->num_airports; i++)
163 if (pp->xpos == sp->airport[i].x &&
164 pp->ypos == sp->airport[i].y) {
165 if (pp->dest_type == T_AIRPORT)
166 loser(pp,
167 "landed at the wrong airport.");
168 else
169 loser(pp,
170 "landed instead of exited.");
172 loser(pp, "crashed on the ground.");
174 if (pp->xpos < 1 || pp->xpos >= sp->width - 1 ||
175 pp->ypos < 1 || pp->ypos >= sp->height - 1) {
176 for (i = 0; i < sp->num_exits; i++)
177 if (pp->xpos == sp->exit[i].x &&
178 pp->ypos == sp->exit[i].y) {
179 if (pp->dest_type == T_EXIT)
180 loser(pp,
181 "exited via the wrong exit.");
182 else
183 loser(pp,
184 "exited instead of landed.");
186 loser(pp, "illegally left the flight arena.");
191 * Traverse the list once, deleting the planes that are gone.
193 for (pp = air.head; pp != NULL; pp = p2) {
194 p2 = pp->next;
195 if (pp->status == S_GONE) {
196 safe_planes++;
197 delete(&air, pp);
201 draw_all();
203 for (p1 = air.head; p1 != NULL; p1 = p1->next)
204 for (p2 = p1->next; p2 != NULL; p2 = p2->next)
205 if (too_close(p1, p2, 1)) {
206 static char buf[80];
208 (void)sprintf(buf, "collided with plane '%c'.",
209 name(p2));
210 loser(p1, buf);
213 * Check every other update. Actually, only add on even updates.
214 * Otherwise, prop jobs show up *on* entrance. Remember that
215 * we don't update props on odd updates.
217 if ((random() % sp->newplane_time) == 0)
218 addplane();
220 #ifdef BSD
221 sigsetmask(mask);
222 #endif
223 #ifdef SYSV
224 alarm(sp->update_secs);
225 #endif
228 const char *
229 command(const PLANE *pp)
231 static char buf[50], *bp, *comm_start;
233 buf[0] = '\0';
234 bp = buf;
235 (void)sprintf(bp, "%c%d%c%c%d: ", name(pp), pp->altitude,
236 (pp->fuel < LOWFUEL) ? '*' : ' ',
237 (pp->dest_type == T_AIRPORT) ? 'A' : 'E', pp->dest_no);
239 comm_start = bp = index(buf, '\0');
240 if (pp->altitude == 0)
241 (void)sprintf(bp, "Holding @ A%d", pp->orig_no);
242 else if (pp->new_dir >= MAXDIR || pp->new_dir < 0)
243 strcpy(bp, "Circle");
244 else if (pp->new_dir != pp->dir)
245 (void)sprintf(bp, "%d", dir_deg(pp->new_dir));
247 bp = index(buf, '\0');
248 if (pp->delayd)
249 (void)sprintf(bp, " @ B%d", pp->delayd_no);
251 bp = index(buf, '\0');
252 if (*comm_start == '\0' &&
253 (pp->status == S_UNMARKED || pp->status == S_IGNORED))
254 strcpy(bp, "---------");
255 return (buf);
258 /* char */
259 char
260 name(const PLANE *p)
262 if (p->plane_type == 0)
263 return ('A' + p->plane_no);
264 else
265 return ('a' + p->plane_no);
268 char
269 number(char l)
271 if (l < 'a' && l > 'z' && l < 'A' && l > 'Z')
272 return (-1);
273 else if (l >= 'a' && l <= 'z')
274 return (l - 'a');
275 else
276 return (l - 'A');
279 static int
280 next_plane(void)
282 static int last_plane = -1;
283 PLANE *pp;
284 int found, start_plane = last_plane;
286 do {
287 found = 0;
288 last_plane++;
289 if (last_plane >= 26)
290 last_plane = 0;
291 for (pp = air.head; pp != NULL; pp = pp->next)
292 if (pp->plane_no == last_plane) {
293 found++;
294 break;
296 if (!found)
297 for (pp = ground.head; pp != NULL; pp = pp->next)
298 if (pp->plane_no == last_plane) {
299 found++;
300 break;
302 } while (found && last_plane != start_plane);
303 if (last_plane == start_plane)
304 return (-1);
305 return (last_plane);
309 addplane(void)
311 PLANE p, *pp, *p1;
312 int i, num_starts, is_close, rnd, rnd2, pnum;
314 bzero(&p, sizeof (p));
316 p.status = S_MARKED;
317 p.plane_type = random() % 2;
319 num_starts = sp->num_exits + sp->num_airports;
320 rnd = random() % num_starts;
322 if (rnd < sp->num_exits) {
323 p.dest_type = T_EXIT;
324 p.dest_no = rnd;
325 } else {
326 p.dest_type = T_AIRPORT;
327 p.dest_no = rnd - sp->num_exits;
330 /* loop until we get a plane not near another */
331 for (i = 0; i < num_starts; i++) {
332 /* loop till we get a different start point */
333 while ((rnd2 = random() % num_starts) == rnd)
335 if (rnd2 < sp->num_exits) {
336 p.orig_type = T_EXIT;
337 p.orig_no = rnd2;
338 p.xpos = sp->exit[rnd2].x;
339 p.ypos = sp->exit[rnd2].y;
340 p.new_dir = p.dir = sp->exit[rnd2].dir;
341 p.altitude = p.new_altitude = 7;
342 is_close = 0;
343 for (p1 = air.head; p1 != NULL; p1 = p1->next)
344 if (too_close(p1, &p, 4)) {
345 is_close++;
346 break;
348 if (is_close)
349 continue;
350 } else {
351 p.orig_type = T_AIRPORT;
352 p.orig_no = rnd2 - sp->num_exits;
353 p.xpos = sp->airport[p.orig_no].x;
354 p.ypos = sp->airport[p.orig_no].y;
355 p.new_dir = p.dir = sp->airport[p.orig_no].dir;
356 p.altitude = p.new_altitude = 0;
358 p.fuel = sp->width + sp->height;
359 break;
361 if (i >= num_starts)
362 return (-1);
363 pnum = next_plane();
364 if (pnum < 0)
365 return (-1);
366 p.plane_no = pnum;
368 pp = newplane();
369 bcopy(&p, pp, sizeof (p));
371 if (pp->orig_type == T_AIRPORT)
372 append(&ground, pp);
373 else
374 append(&air, pp);
376 return (pp->dest_type);
379 PLANE *
380 findplane(int n)
382 PLANE *pp;
384 for (pp = air.head; pp != NULL; pp = pp->next)
385 if (pp->plane_no == n)
386 return (pp);
387 for (pp = ground.head; pp != NULL; pp = pp->next)
388 if (pp->plane_no == n)
389 return (pp);
390 return (NULL);
393 static int
394 too_close(const PLANE *p1, const PLANE *p2, int dist)
396 if (ABS(p1->altitude - p2->altitude) <= dist &&
397 ABS(p1->xpos - p2->xpos) <= dist && ABS(p1->ypos - p2->ypos) <= dist)
398 return (1);
399 else
400 return (0);
403 static int
404 dir_deg(int d)
406 switch (d) {
407 case 0: return (0);
408 case 1: return (45);
409 case 2: return (90);
410 case 3: return (135);
411 case 4: return (180);
412 case 5: return (225);
413 case 6: return (270);
414 case 7: return (315);
415 default:
416 return (-1);