Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / games / atc / update.c
blob32e05b9a91166f91c5ff07b043ab37003fedcb4d
1 /* $NetBSD: update.c,v 1.20 2009/05/26 00:00:56 dholland Exp $ */
3 /*-
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
8 * Ed James.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
32 * SUCH DAMAGE.
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>
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)update.c 8.1 (Berkeley) 5/31/93";
48 #else
49 __RCSID("$NetBSD: update.c,v 1.20 2009/05/26 00:00:56 dholland Exp $");
50 #endif
51 #endif /* not lint */
53 #include "include.h"
55 static int next_plane(void);
56 static int too_close(const PLANE *p1, const PLANE *p2, int);
57 static int dir_deg(int);
59 /* ARGSUSED */
60 void
61 update(int dummy __unused)
63 int i, dir_diff, unclean;
64 PLANE *pp, *p1, *p2;
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, "exceeded 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)snprintf(buf, sizeof(buf),
209 "collided with plane '%c'.",
210 name(p2));
211 loser(p1, buf);
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 ((rand() % sp->newplane_time) == 0)
219 (void)addplane();
221 #ifdef SYSV
222 alarm(sp->update_secs);
223 #endif
226 const char *
227 command(const PLANE *pp)
229 static char buf[50], *bp, *comm_start;
230 size_t bpsize;
232 buf[0] = '\0';
233 bp = buf;
234 bpsize = sizeof(buf);
235 (void)snprintf(bp, bpsize, "%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 = strchr(buf, '\0');
240 bpsize = buf + sizeof(buf) - bp;
241 if (pp->altitude == 0)
242 (void)snprintf(bp, bpsize, "Holding @ A%d", pp->orig_no);
243 else if (pp->new_dir >= MAXDIR || pp->new_dir < 0)
244 (void)snprintf(bp, bpsize, "Circle");
245 else if (pp->new_dir != pp->dir)
246 (void)snprintf(bp, bpsize, "%d", dir_deg(pp->new_dir));
248 bp = strchr(buf, '\0');
249 bpsize = buf + sizeof(buf) - bp;
250 if (pp->delayd)
251 (void)snprintf(bp, bpsize, " @ B%d", pp->delayd_no);
253 bp = strchr(buf, '\0');
254 bpsize = buf + sizeof(buf) - bp;
255 if (*comm_start == '\0' &&
256 (pp->status == S_UNMARKED || pp->status == S_IGNORED))
257 (void)snprintf(bp, bpsize, "---------");
258 return (buf);
261 char
262 name(const PLANE *p)
264 if (p->plane_type == 0)
265 return ('A' + p->plane_no);
266 else
267 return ('a' + p->plane_no);
271 number(int l)
273 if (islower((unsigned char)l))
274 return (l - 'a');
275 else if (isupper((unsigned char)l))
276 return (l - 'A');
277 else
278 return (-1);
281 static int
282 next_plane(void)
284 static int last_plane = -1;
285 PLANE *pp;
286 int found, start_plane = last_plane;
288 do {
289 found = 0;
290 last_plane++;
291 if (last_plane >= 26)
292 last_plane = 0;
293 for (pp = air.head; pp != NULL; pp = pp->next)
294 if (pp->plane_no == last_plane) {
295 found++;
296 break;
298 if (!found)
299 for (pp = ground.head; pp != NULL; pp = pp->next)
300 if (pp->plane_no == last_plane) {
301 found++;
302 break;
304 } while (found && last_plane != start_plane);
305 if (last_plane == start_plane)
306 return (-1);
307 return (last_plane);
311 addplane(void)
313 PLANE p, *pp, *p1;
314 int i, num_starts, isclose, rnd, rnd2, pnum;
316 (void)memset(&p, 0, sizeof (p));
318 p.status = S_MARKED;
319 p.plane_type = random() % 2;
321 num_starts = sp->num_exits + sp->num_airports;
322 rnd = random() % num_starts;
324 if (rnd < sp->num_exits) {
325 p.dest_type = T_EXIT;
326 p.dest_no = rnd;
327 } else {
328 p.dest_type = T_AIRPORT;
329 p.dest_no = rnd - sp->num_exits;
332 /* loop until we get a plane not near another */
333 for (i = 0; i < num_starts; i++) {
334 /* loop till we get a different start point */
335 while ((rnd2 = random() % num_starts) == rnd)
337 if (rnd2 < sp->num_exits) {
338 p.orig_type = T_EXIT;
339 p.orig_no = rnd2;
340 p.xpos = sp->exit[rnd2].x;
341 p.ypos = sp->exit[rnd2].y;
342 p.new_dir = p.dir = sp->exit[rnd2].dir;
343 p.altitude = p.new_altitude = 7;
344 isclose = 0;
345 for (p1 = air.head; p1 != NULL; p1 = p1->next)
346 if (too_close(p1, &p, 4)) {
347 isclose++;
348 break;
350 if (isclose)
351 continue;
352 } else {
353 p.orig_type = T_AIRPORT;
354 p.orig_no = rnd2 - sp->num_exits;
355 p.xpos = sp->airport[p.orig_no].x;
356 p.ypos = sp->airport[p.orig_no].y;
357 p.new_dir = p.dir = sp->airport[p.orig_no].dir;
358 p.altitude = p.new_altitude = 0;
360 p.fuel = sp->width + sp->height;
361 break;
363 if (i >= num_starts)
364 return (-1);
365 pnum = next_plane();
366 if (pnum < 0)
367 return (-1);
368 p.plane_no = pnum;
370 pp = newplane();
371 if (pp == NULL)
372 loser(NULL, "Out of memory!");
373 (void)memcpy(pp, &p, sizeof (p));
375 if (pp->orig_type == T_AIRPORT)
376 append(&ground, pp);
377 else
378 append(&air, pp);
380 return (pp->dest_type);
383 PLANE *
384 findplane(int n)
386 PLANE *pp;
388 for (pp = air.head; pp != NULL; pp = pp->next)
389 if (pp->plane_no == n)
390 return (pp);
391 for (pp = ground.head; pp != NULL; pp = pp->next)
392 if (pp->plane_no == n)
393 return (pp);
394 return (NULL);
397 static int
398 too_close(const PLANE *p1, const PLANE *p2, int dist)
400 if (ABS(p1->altitude - p2->altitude) <= dist &&
401 ABS(p1->xpos - p2->xpos) <= dist &&
402 ABS(p1->ypos - p2->ypos) <= dist)
403 return (1);
404 else
405 return (0);
408 static int
409 dir_deg(int d)
411 switch (d) {
412 case 0: return (0);
413 case 1: return (45);
414 case 2: return (90);
415 case 3: return (135);
416 case 4: return (180);
417 case 5: return (225);
418 case 6: return (270);
419 case 7: return (315);
420 default:
421 return (-1);