update.c revision 1.23 1 /* $NetBSD: update.c,v 1.23 2014/03/22 22:24:21 dholland Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Ed James.
9 *
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.
21 *
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.
33 */
34
35 /*
36 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
37 *
38 * Copy permission is hereby granted provided that this notice is
39 * retained on all partial or complete copies.
40 *
41 * For more info on this and all of my stuff, mail edjames (at) berkeley.edu.
42 */
43
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.23 2014/03/22 22:24:21 dholland Exp $");
50 #endif
51 #endif /* not lint */
52
53 #include "include.h"
54
55 static int next_plane(void);
56 static int too_close(const PLANE *p1, const PLANE *p2, int);
57 static int dir_deg(int);
58
59 /* ARGSUSED */
60 void
61 update(int dummy __unused)
62 {
63 int dir_diff, unclean;
64 unsigned i;
65 PLANE *pp, *p1, *p2;
66
67 #ifdef SYSV
68 alarm(0);
69 signal(SIGALRM, update);
70 #endif
71
72 clck++;
73
74 erase_all();
75
76 /* put some planes in the air */
77 do {
78 unclean = 0;
79 for (pp = ground.head; pp != NULL; pp = pp->next) {
80 if (pp->new_altitude > 0) {
81 delete(&ground, pp);
82 append(&air, pp);
83 unclean = 1;
84 break;
85 }
86 }
87 } while (unclean);
88
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)
93 continue;
94
95 pp->fuel--;
96 if (pp->fuel < 0)
97 loser(pp, "ran out of fuel.");
98
99 pp->altitude += SGN(pp->new_altitude - pp->altitude);
100
101 if (!pp->delayd) {
102 dir_diff = pp->new_dir - pp->dir;
103 /*
104 * Allow for circle commands
105 */
106 if (pp->new_dir >= 0 && pp->new_dir < MAXDIR) {
107 if (dir_diff > MAXDIR/2)
108 dir_diff -= MAXDIR;
109 else if (dir_diff < -(MAXDIR/2))
110 dir_diff += MAXDIR;
111 }
112 if (dir_diff > 2)
113 dir_diff = 2;
114 else if (dir_diff < -2)
115 dir_diff = -2;
116 pp->dir += dir_diff;
117 if (pp->dir >= MAXDIR)
118 pp->dir -= MAXDIR;
119 else if (pp->dir < 0)
120 pp->dir += MAXDIR;
121 }
122 pp->xpos += displacement[pp->dir].dx;
123 pp->ypos += displacement[pp->dir].dy;
124
125 if (pp->delayd && pp->xpos == sp->beacon[pp->delayd_no].x &&
126 pp->ypos == sp->beacon[pp->delayd_no].y) {
127 pp->delayd = 0;
128 if (pp->status == S_UNMARKED)
129 pp->status = S_MARKED;
130 }
131
132 switch (pp->dest_type) {
133 case T_AIRPORT:
134 if (pp->xpos == sp->airport[pp->dest_no].x &&
135 pp->ypos == sp->airport[pp->dest_no].y &&
136 pp->altitude == 0) {
137 if (pp->dir != sp->airport[pp->dest_no].dir)
138 loser(pp, "landed in the wrong direction.");
139 else {
140 pp->status = S_GONE;
141 continue;
142 }
143 }
144 break;
145 case T_EXIT:
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.");
150 else {
151 pp->status = S_GONE;
152 continue;
153 }
154 }
155 break;
156 default:
157 loser(pp, "has a bizarre destination, get help!");
158 }
159 if (pp->altitude > 9)
160 /* "this is impossible" */
161 loser(pp, "exceeded 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)
167 loser(pp,
168 "landed at the wrong airport.");
169 else
170 loser(pp,
171 "landed instead of exited.");
172 }
173 loser(pp, "crashed on the ground.");
174 }
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)
181 loser(pp,
182 "exited via the wrong exit.");
183 else
184 loser(pp,
185 "exited instead of landed.");
186 }
187 loser(pp, "illegally left the flight arena.");
188 }
189 }
190
191 /*
192 * Traverse the list once, deleting the planes that are gone.
193 */
194 for (pp = air.head; pp != NULL; pp = p2) {
195 p2 = pp->next;
196 if (pp->status == S_GONE) {
197 safe_planes++;
198 delete(&air, pp);
199 }
200 }
201
202 draw_all();
203
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)) {
207 static char buf[80];
208
209 (void)snprintf(buf, sizeof(buf),
210 "collided with plane '%c'.",
211 name(p2));
212 loser(p1, buf);
213 }
214 /*
215 * Check every other update. Actually, only add on even updates.
216 * Otherwise, prop jobs show up *on* entrance. Remember that
217 * we don't update props on odd updates.
218 */
219 if ((rand() % sp->newplane_time) == 0)
220 (void)addplane();
221
222 #ifdef SYSV
223 alarm(sp->update_secs);
224 #endif
225 }
226
227 const char *
228 command(const PLANE *pp)
229 {
230 static char buf[50], *bp, *comm_start;
231 size_t bpsize;
232
233 buf[0] = '\0';
234 bp = buf;
235 bpsize = sizeof(buf);
236 (void)snprintf(bp, bpsize, "%c%d%c%c%d: ", name(pp), pp->altitude,
237 (pp->fuel < LOWFUEL) ? '*' : ' ',
238 (pp->dest_type == T_AIRPORT) ? 'A' : 'E', pp->dest_no);
239
240 comm_start = bp = strchr(buf, '\0');
241 bpsize = buf + sizeof(buf) - bp;
242 if (pp->altitude == 0)
243 (void)snprintf(bp, bpsize, "Holding @ A%d", pp->orig_no);
244 else if (pp->new_dir >= MAXDIR || pp->new_dir < 0)
245 (void)snprintf(bp, bpsize, "Circle");
246 else if (pp->new_dir != pp->dir)
247 (void)snprintf(bp, bpsize, "%d", dir_deg(pp->new_dir));
248
249 bp = strchr(buf, '\0');
250 bpsize = buf + sizeof(buf) - bp;
251 if (pp->delayd)
252 (void)snprintf(bp, bpsize, " @ B%d", pp->delayd_no);
253
254 bp = strchr(buf, '\0');
255 bpsize = buf + sizeof(buf) - bp;
256 if (*comm_start == '\0' &&
257 (pp->status == S_UNMARKED || pp->status == S_IGNORED))
258 (void)snprintf(bp, bpsize, "---------");
259 return (buf);
260 }
261
262 char
263 name(const PLANE *p)
264 {
265 if (p->plane_type == 0)
266 return ('A' + p->plane_no);
267 else
268 return ('a' + p->plane_no);
269 }
270
271 int
272 number(int l)
273 {
274 if (islower((unsigned char)l))
275 return (l - 'a');
276 else if (isupper((unsigned char)l))
277 return (l - 'A');
278 else
279 return (-1);
280 }
281
282 static int
283 next_plane(void)
284 {
285 static int last_plane = -1;
286 PLANE *pp;
287 int found, start_plane = last_plane;
288
289 do {
290 found = 0;
291 last_plane++;
292 if (last_plane >= 26)
293 last_plane = 0;
294 for (pp = air.head; pp != NULL; pp = pp->next)
295 if (pp->plane_no == last_plane) {
296 found++;
297 break;
298 }
299 if (!found)
300 for (pp = ground.head; pp != NULL; pp = pp->next)
301 if (pp->plane_no == last_plane) {
302 found++;
303 break;
304 }
305 } while (found && last_plane != start_plane);
306 if (found)
307 return (-1);
308 return (last_plane);
309 }
310
311 int
312 addplane(void)
313 {
314 PLANE p, *pp, *p1;
315 int isclose, pnum;
316 unsigned num_starts, rnd, rnd2, i;
317
318 (void)memset(&p, 0, sizeof (p));
319
320 p.status = S_MARKED;
321 p.plane_type = random() % 2;
322
323 num_starts = sp->num_exits + sp->num_airports;
324 rnd = random() % num_starts;
325
326 if (rnd < sp->num_exits) {
327 p.dest_type = T_EXIT;
328 p.dest_no = rnd;
329 } else {
330 p.dest_type = T_AIRPORT;
331 p.dest_no = rnd - sp->num_exits;
332 }
333
334 /* loop until we get a plane not near another */
335 for (i = 0; i < num_starts; i++) {
336 /* loop till we get a different start point */
337 while ((rnd2 = random() % num_starts) == rnd)
338 ;
339 if (rnd2 < sp->num_exits) {
340 p.orig_type = T_EXIT;
341 p.orig_no = rnd2;
342 p.xpos = sp->exit[rnd2].x;
343 p.ypos = sp->exit[rnd2].y;
344 p.new_dir = p.dir = sp->exit[rnd2].dir;
345 p.altitude = p.new_altitude = 7;
346 isclose = 0;
347 for (p1 = air.head; p1 != NULL; p1 = p1->next)
348 if (too_close(p1, &p, 4)) {
349 isclose++;
350 break;
351 }
352 if (isclose)
353 continue;
354 } else {
355 p.orig_type = T_AIRPORT;
356 p.orig_no = rnd2 - sp->num_exits;
357 p.xpos = sp->airport[p.orig_no].x;
358 p.ypos = sp->airport[p.orig_no].y;
359 p.new_dir = p.dir = sp->airport[p.orig_no].dir;
360 p.altitude = p.new_altitude = 0;
361 }
362 p.fuel = sp->width + sp->height;
363 break;
364 }
365 if (i >= num_starts)
366 return (-1);
367 pnum = next_plane();
368 if (pnum < 0)
369 return (-1);
370 p.plane_no = pnum;
371
372 pp = newplane();
373 if (pp == NULL)
374 loser(NULL, "Out of memory!");
375 (void)memcpy(pp, &p, sizeof (p));
376
377 if (pp->orig_type == T_AIRPORT)
378 append(&ground, pp);
379 else
380 append(&air, pp);
381
382 return (pp->dest_type);
383 }
384
385 PLANE *
386 findplane(int n)
387 {
388 PLANE *pp;
389
390 for (pp = air.head; pp != NULL; pp = pp->next)
391 if (pp->plane_no == n)
392 return (pp);
393 for (pp = ground.head; pp != NULL; pp = pp->next)
394 if (pp->plane_no == n)
395 return (pp);
396 return (NULL);
397 }
398
399 static int
400 too_close(const PLANE *p1, const PLANE *p2, int dist)
401 {
402 if (ABS(p1->altitude - p2->altitude) <= dist &&
403 ABS(p1->xpos - p2->xpos) <= dist &&
404 ABS(p1->ypos - p2->ypos) <= dist)
405 return (1);
406 else
407 return (0);
408 }
409
410 static int
411 dir_deg(int d)
412 {
413 switch (d) {
414 case 0: return (0);
415 case 1: return (45);
416 case 2: return (90);
417 case 3: return (135);
418 case 4: return (180);
419 case 5: return (225);
420 case 6: return (270);
421 case 7: return (315);
422 default:
423 return (-1);
424 }
425 }
426