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